def test_retry_generator(exc, in_init, retry_times): def simplegen(): yield "log line" my_args = ('some', 'new') if not in_init: my_kwargs = { 'one': 'first', 'two': 'second', 'retry_times': retry_times } else: my_kwargs = {'one': 'first', 'two': 'second'} if in_init: t = DockerTasker(retry_times=retry_times) else: t = DockerTasker() (flexmock(time).should_receive('sleep').and_return(None)) if not exc: cr = CommandResult() cr._error = "cmd_error" cr._error_detail = {"message": "error_detail"} if exc == APIError: error_message = 'api_error' response = flexmock(content=error_message, status_code=408) (flexmock(atomic_reactor.util).should_receive( 'wait_for_command').times(retry_times + 1).and_raise( APIError, error_message, response)) elif exc == ProtocolError: error_message = 'protocol_error' (flexmock(atomic_reactor.util).should_receive( 'wait_for_command').times(retry_times + 1).and_raise( ProtocolError, error_message)) elif exc == ReadTimeoutError: pool = 'pool' message = 'read_timeout_error' error_message = '{}: {}'.format(pool, message) (flexmock(atomic_reactor.util).should_receive( 'wait_for_command').times(retry_times + 1).and_raise( ReadTimeoutError, pool, 'url', message)) else: (flexmock(atomic_reactor.util).should_receive( 'wait_for_command').times(retry_times + 1).and_return(cr)) error_message = 'cmd_error' if retry_times >= 0: with pytest.raises(RetryGeneratorException) as ex: t.retry_generator(lambda *args, **kwargs: simplegen(), *my_args, **my_kwargs) assert repr(error_message) in repr(ex.value) else: t.retry_generator(lambda *args, **kwargs: simplegen(), *my_args, **my_kwargs)
def test_try_with_library_pull_base_image(library, reactor_config_map): if MOCK: mock_docker(remember_images=True) tasker = DockerTasker(retry_times=0) workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image') workflow.builder = MockBuilder() if library: base_image = 'library/parent-image' else: base_image = 'parent-image' workflow.builder.base_image = ImageName.parse(base_image) workflow.builder.parent_images = {base_image: None} class MockResponse(object): content = '' cr = CommandResult() cr._error = "cmd_error" cr._error_detail = {"message": "error_detail"} if library: call_wait = 1 else: call_wait = 2 (flexmock(atomic_reactor.util).should_receive('wait_for_command').times( call_wait).and_return(cr)) error_message = 'registry.example.com/' + base_image if reactor_config_map: workflow.plugin_workspace[ReactorConfigPlugin.key] = {} workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig({'version': 1, 'source_registry': {'url': 'registry.example.com', 'insecure': True}}) runner = PreBuildPluginsRunner( tasker, workflow, [{ 'name': PullBaseImagePlugin.key, 'args': { 'parent_registry': 'registry.example.com', 'parent_registry_insecure': True }, }], ) with pytest.raises(PluginFailedException) as exc: runner.run() assert error_message in exc.value.args[0]
def test_try_with_library_pull_base_image(library, reactor_config_map): if MOCK: mock_docker(remember_images=True) tasker = DockerTasker(retry_times=0) workflow = DockerBuildWorkflow(MOCK_SOURCE, 'test-image') workflow.builder = MockBuilder() if library: base_image = 'library/parent-image' else: base_image = 'parent-image' workflow.builder.base_image = ImageName.parse(base_image) workflow.builder.parent_images = {ImageName.parse(base_image): None} class MockResponse(object): content = '' cr = CommandResult() cr._error = "cmd_error" cr._error_detail = {"message": "error_detail"} if library: call_wait = 1 else: call_wait = 2 (flexmock(atomic_reactor.util) .should_receive('wait_for_command') .times(call_wait) .and_return(cr)) error_message = 'registry.example.com/' + base_image if reactor_config_map: workflow.plugin_workspace[ReactorConfigPlugin.key] = {} workflow.plugin_workspace[ReactorConfigPlugin.key][WORKSPACE_CONF_KEY] =\ ReactorConfig({'version': 1, 'source_registry': {'url': 'registry.example.com', 'insecure': True}}) runner = PreBuildPluginsRunner( tasker, workflow, [{ 'name': PullBaseImagePlugin.key, 'args': {'parent_registry': 'registry.example.com', 'parent_registry_insecure': True}, }], ) with pytest.raises(PluginFailedException) as exc: runner.run() assert error_message in exc.value.args[0]
def test_retry_generator(exc, in_init, retry_times): def simplegen(): yield "log line" my_args = ('some', 'new') if not in_init: my_kwargs = {'one': 'first', 'two': 'second', 'retry_times': retry_times} else: my_kwargs = {'one': 'first', 'two': 'second'} if in_init: t = DockerTasker(retry_times=retry_times) else: t = DockerTasker() (flexmock(time) .should_receive('sleep') .and_return(None)) if not exc: cr = CommandResult() cr._error = "cmd_error" cr._error_detail = {"message": "error_detail"} if exc == APIError: error_message = 'api_error' response = flexmock(content=error_message, status_code=408) (flexmock(atomic_reactor.util) .should_receive('wait_for_command') .times(retry_times + 1) .and_raise(APIError, error_message, response)) elif exc == ProtocolError: error_message = 'protocol_error' (flexmock(atomic_reactor.util) .should_receive('wait_for_command') .times(retry_times + 1) .and_raise(ProtocolError, error_message)) else: (flexmock(atomic_reactor.util) .should_receive('wait_for_command') .times(retry_times + 1) .and_return(cr)) error_message = 'cmd_error' if retry_times >= 0: with pytest.raises(RetryGeneratorException) as exc: t.retry_generator(lambda *args, **kwargs: simplegen(), *my_args, **my_kwargs) assert repr(error_message) in repr(exc.value) else: t.retry_generator(lambda *args, **kwargs: simplegen(), *my_args, **my_kwargs)
def test_parse_item(self, item, expected): cr = CommandResult() cr.parse_item(item) assert cr.logs == [expected]