def json(self, check=True): encoding = guess_json_utf(self.content) text = self.content.decode(encoding) if check and self.status_code not in (0, requests.codes.OK, requests.codes.CREATED): raise OsbsResponseException(text, self.status_code) try: return json.loads(text) except ValueError: msg = '{0}Headers {1}\nContent {2}'.format('HtttpResponse has corrupt json:\n', self.headers, self.content) logger.exception(msg) raise OsbsResponseException(msg, self.status_code)
def test_store_metadata_fail_update_annotations(tmpdir, caplog): workflow = prepare() workflow.exit_results = {} df_content = """ FROM fedora RUN yum install -y python-django CMD blabla""" df = df_parser(str(tmpdir)) df.content = df_content workflow.builder = X workflow.builder.df_path = df.dockerfile_path workflow.builder.df_dir = str(tmpdir) runner = ExitPluginsRunner(None, workflow, [{ 'name': StoreMetadataInOSv3Plugin.key, "args": { "url": "http://example.com/" } }]) (flexmock(OSBS).should_receive('set_annotations_on_build').and_raise( OsbsResponseException('/', 'failed', 0))) with pytest.raises(PluginFailedException): runner.run() assert 'annotations:' in caplog.text()
def test_create_image(insecure_registry, namespace, monkeypatch): """ Test that an ImageStream is created if not found """ runner = prepare(insecure_registry=insecure_registry) kwargs = {} build_json = {"metadata": {}} if namespace is not None: kwargs['namespace'] = namespace build_json['metadata']['namespace'] = namespace monkeypatch.setenv("BUILD", json.dumps(build_json)) (flexmock(OSBS).should_receive('get_image_stream').once().with_args( TEST_IMAGESTREAM, **kwargs).and_raise(OsbsResponseException('none', 404))) if insecure_registry is not None: kwargs['insecure_registry'] = insecure_registry (flexmock(OSBS).should_receive('create_image_stream').once().with_args( TEST_IMAGESTREAM, TEST_REPO, **kwargs)) (flexmock(OSBS).should_receive('import_image').never()) runner.run()
def test_create_image(tmpdir, insecure_registry, namespace, organization, monkeypatch, reactor_config_map): """ Test that an ImageStream is created if not found """ runner = prepare(tmpdir, insecure_registry=insecure_registry, namespace=namespace, organization=organization, reactor_config_map=reactor_config_map) kwargs = {} build_json = {"metadata": {}} if namespace is not None: build_json['metadata']['namespace'] = namespace monkeypatch.setenv("BUILD", json.dumps(build_json)) (flexmock(OSBS).should_receive('get_image_stream').once().with_args( TEST_IMAGESTREAM).and_raise(OsbsResponseException('none', 404))) if insecure_registry is not None: kwargs['insecure_registry'] = insecure_registry enclose_repo = ImageName.parse(TEST_REPO_WITH_REGISTRY) if reactor_config_map and organization: enclose_repo.enclose(organization) (flexmock(OSBS).should_receive('create_image_stream').once().with_args( TEST_IMAGESTREAM, enclose_repo.to_str(registry=True, tag=False), **kwargs).and_return(ImageStreamResponse())) (flexmock(OSBS).should_receive('import_image_tags').once().and_return(True) ) runner.run()
def json(self, check=True): encoding = guess_json_utf(self.content) text = self.content.decode(encoding) if check and self.status_code not in (0, requests.codes.OK, requests.codes.CREATED): raise OsbsResponseException(text, self.status_code) return json.loads(text)
def test_create_image(tmpdir, insecure_registry, namespace, monkeypatch, reactor_config_map): """ Test that an ImageStream is created if not found """ runner = prepare(tmpdir, insecure_registry=insecure_registry, namespace=namespace, reactor_config_map=reactor_config_map) kwargs = {} build_json = {"metadata": {}} if namespace is not None: build_json['metadata']['namespace'] = namespace monkeypatch.setenv("BUILD", json.dumps(build_json)) (flexmock(OSBS).should_receive('get_image_stream').once().with_args( TEST_IMAGESTREAM).and_raise(OsbsResponseException('none', 404))) if insecure_registry is not None: kwargs['insecure_registry'] = insecure_registry (flexmock(OSBS).should_receive('create_image_stream').once().with_args( TEST_IMAGESTREAM, TEST_REPO, **kwargs).and_return(ImageStreamResponse())) (flexmock(OSBS).should_receive('ensure_image_stream_tag').times( DEFAULT_TAGS_AMOUNT)) (flexmock(OSBS).should_receive('import_image').once().and_return(True)) runner.run()
def check_response(response): if response.status_code not in (httplib.OK, httplib.CREATED): if hasattr(response, 'content'): content = response.content else: content = ''.join(response.iter_lines()) logger.error("[%d] %s", response.status_code, content) raise OsbsResponseException(message=content, status_code=response.status_code)
def check_response(response, log_level=logging.ERROR): if response.status_code not in (http_client.OK, http_client.CREATED): if hasattr(response, 'content'): content = response.content else: content = b''.join(response.iter_lines()) logger.log(log_level, "[%d] %s", response.status_code, content) raise OsbsResponseException(message=content, status_code=response.status_code)
def test_ensure_primary(tmpdir, monkeypatch, osbs_error, tag_conf, annotations, tag_prefix, reactor_config_map): """ Test that primary image tags are ensured """ runner = prepare(tmpdir, primary_images_annotations=annotations, primary_images_tag_conf=tag_conf, reactor_config_map=reactor_config_map) monkeypatch.setenv("BUILD", json.dumps({ "metadata": {} })) tags = [] floating_images = runner.workflow.tag_conf.floating_images if not floating_images: floating_images = [ ImageName.parse(floating) for floating in runner.workflow.build_result.annotations['repositories']['floating']] for floating_image in floating_images: tag = floating_image.tag tags.append(tag) (flexmock(OSBS) .should_receive('get_image_stream') .once() .with_args(TEST_IMAGESTREAM) .and_return(ImageStreamResponse())) # By using a combination of ordered and once, we verify that # ensure_image_stream_tag is not called with version-release tag for x in range(DEFAULT_TAGS_AMOUNT): expectation = ( flexmock(OSBS) .should_receive('ensure_image_stream_tag') .with_args(dict, tag_prefix + str(x)) .once() .ordered() ) if osbs_error: expectation.and_raise(OsbsResponseException('None', 500)) (flexmock(OSBS) .should_receive('import_image_tags') .once() .and_raise(AttributeError)) (flexmock(OSBS) .should_receive('import_image') .with_args(TEST_IMAGESTREAM, tags=tags) .times(0 if osbs_error else 1) .and_return(True)) if osbs_error: with pytest.raises(PluginFailedException): runner.run() else: runner.run()
def test_create_source_container_start_fails(self, osbs_source): error_msg = 'failed to create pipeline run' (flexmock(PipelineRun).should_receive('start_pipeline_run').and_raise( OsbsResponseException(error_msg, 400))) with pytest.raises(OsbsResponseException) as exc: osbs_source.create_source_container_build( **REQUIRED_SOURCE_CONTAINER_BUILD_ARGS) assert error_msg == str(exc.value)
def check_response(response, log_level=logging.ERROR): if response.status_code not in ( requests.status_codes.codes.ok, requests.status_codes.codes.created, ): if hasattr(response, 'content'): content = response.content else: content = b''.join(response.iter_lines()) logger.log(log_level, "[%d] %s", response.status_code, content) raise OsbsResponseException(message=content, status_code=response.status_code)
def test_create_binary_container_start_fails(self, osbs_binary): error_msg = 'failed to create pipeline run' (flexmock(utils).should_receive('get_repo_info').with_args( TEST_GIT_URI, TEST_GIT_REF, git_branch=TEST_GIT_BRANCH, depth=None).and_return(self.mock_repo_info())) (flexmock(PipelineRun).should_receive('start_pipeline_run').and_raise( OsbsResponseException(error_msg, 400))) with pytest.raises(OsbsResponseException) as exc: osbs_binary.create_binary_container_build(**REQUIRED_BUILD_ARGS) assert error_msg == str(exc.value)
def test_exception_during_import(monkeypatch): """ The plugin should fail if image import fails. """ runner = prepare() monkeypatch.setenv("BUILD", json.dumps({"metadata": {}})) (flexmock(OSBS).should_receive('get_image_stream').with_args( TEST_IMAGESTREAM).and_raise(OsbsResponseException('none', 404))) (flexmock(OSBS).should_receive('create_image_stream').once().with_args( TEST_IMAGESTREAM, TEST_REPO).and_raise(RuntimeError)) (flexmock(OSBS).should_receive('import_image').never()) with pytest.raises(PluginFailedException): runner.run()
def test_store_metadata_fail_update_labels(workflow, caplog): prepare(workflow) workflow.data.labels = {'some-label': 'some-value'} runner = ExitPluginsRunner(workflow, [{ 'name': StoreMetadataPlugin.key, "args": { "url": "http://example.com/" } }]) (flexmock(OSBS).should_receive('update_labels_on_build').and_raise( OsbsResponseException('/', 'failed', 0))) with pytest.raises(PluginFailedException): runner.run() assert 'labels:' in caplog.text
def test_store_metadata_fail_update_annotations(tmpdir, caplog): workflow = prepare() workflow.exit_results = {} runner = ExitPluginsRunner(None, workflow, [{ 'name': StoreMetadataInOSv3Plugin.key, "args": { "url": "http://example.com/" } }]) (flexmock(OSBS).should_receive('set_annotations_on_build').and_raise( OsbsResponseException('/', 'failed', 0))) output = runner.run() assert 'annotations:' in caplog.text()
def test_exception_during_create(tmpdir, monkeypatch, reactor_config_map): # noqa """ The plugin should fail if the ImageStream creation fails. """ runner = prepare(tmpdir, reactor_config_map=reactor_config_map) monkeypatch.setenv("BUILD", json.dumps({"metadata": {}})) (flexmock(OSBS).should_receive('get_image_stream').with_args( TEST_IMAGESTREAM).and_raise(OsbsResponseException('none', 404))) (flexmock(OSBS).should_receive('create_image_stream').once().with_args( TEST_IMAGESTREAM, TEST_REPO_WITH_REGISTRY).and_raise(RuntimeError)) (flexmock(OSBS).should_receive('import_image').never()) with pytest.raises(PluginFailedException): runner.run()
def test_store_metadata_fail_update_annotations(workflow, source_dir, caplog): env = (MockEnv(workflow).for_plugin( StoreMetadataPlugin.key).set_plugin_args( {"url": "http://example.com/"})) prepare(workflow) df_content = dedent("""\ FROM fedora RUN yum install -y python-django CMD blabla """) mock_dockerfile(workflow, df_content) (flexmock(OSBS).should_receive('update_annotations_on_build').and_raise( OsbsResponseException('/', 'failed', 0))) with pytest.raises(PluginFailedException): env.create_runner().run() assert 'annotations:' in caplog.text
def wait_for_new_build_config_instance(self, build_config_id, prev_version): logger.info("waiting for build config %s to get instantiated", build_config_id) for changetype, obj in self.watch_resource("buildconfigs", build_config_id): if changetype == WATCH_MODIFIED: version = graceful_chain_get(obj, 'status', 'lastVersion') if not isinstance(version, numbers.Integral): logger.error("BuildConfig %s has unexpected lastVersion: %s", build_config_id, version) continue if version > prev_version: return "%s-%s" % (build_config_id, version) if changetype == WATCH_DELETED: logger.error("BuildConfig deleted while waiting for new build instance") break raise OsbsResponseException("New BuildConfig instance not found", httplib.NOT_FOUND)
def test_409_response(self, monkeypatch): key = 'is_autorebuild' workflow, runner = self.prepare(key, 'true') (flexmock(OSBS) .should_receive('set_labels_on_build_config') .twice() .and_raise(OsbsResponseException('conflict', 409)) .and_return(None)) monkeypatch.setenv("BUILD", json.dumps({ "metadata": { "labels": { "buildconfig": "buildconfig1", key: 'false', } } })) runner.run()
def test_store_metadata_fail_update_labels(tmpdir, caplog, koji_plugin): workflow = prepare() workflow.exit_results = { koji_plugin: 1234, } runner = ExitPluginsRunner(None, workflow, [{ 'name': StoreMetadataInOSv3Plugin.key, "args": { "url": "http://example.com/" } }]) (flexmock(OSBS).should_receive('update_labels_on_build').and_raise( OsbsResponseException('/', 'failed', 0))) with pytest.raises(PluginFailedException): runner.run() assert 'labels:' in caplog.text()
def test_ensure_primary(monkeypatch, osbs_error, tag_conf, annotations, tag_prefix): """ Test that primary image tags are ensured """ runner = prepare(primary_images_annotations=annotations, primary_images_tag_conf=tag_conf) monkeypatch.setenv("BUILD", json.dumps({ "metadata": {} })) (flexmock(OSBS) .should_receive('get_image_stream') .once() .with_args(TEST_IMAGESTREAM) .and_return(ImageStreamResponse())) # By using a combination of ordered and once, we verify that # ensure_image_stream_tag is not called with version-release tag for x in range(DEFAULT_TAGS_AMOUNT): expectation = ( flexmock(OSBS) .should_receive('ensure_image_stream_tag') .with_args(dict, tag_prefix + str(x)) .once() .ordered() ) if osbs_error: expectation.and_raise(OsbsResponseException('None', 500)) (flexmock(OSBS) .should_receive('import_image') .with_args(TEST_IMAGESTREAM) .times(0 if osbs_error else 1) .and_return(True)) if osbs_error: with pytest.raises(PluginFailedException): runner.run() else: runner.run()
def test_ensure_primary(monkeypatch, osbs_error): """ Test that primary image tags are ensured """ runner = prepare() monkeypatch.setenv("BUILD", json.dumps({"metadata": {}})) (flexmock(OSBS).should_receive('get_image_stream').once().with_args( TEST_IMAGESTREAM).and_return(ImageStreamResponse())) if osbs_error: (flexmock(OSBS).should_receive('ensure_image_stream_tag').times( 6).and_raise(OsbsResponseException('None', 500))) (flexmock(OSBS).should_receive('import_image').never()) with pytest.raises(PluginFailedException): runner.run() else: (flexmock(OSBS).should_receive('ensure_image_stream_tag').times(6)) (flexmock(OSBS).should_receive('import_image').once().with_args( TEST_IMAGESTREAM).and_return(True)) runner.run()
def json(self, check=True): if check and self.status_code not in (0, httplib.OK, httplib.CREATED): raise OsbsResponseException(self.content, self.status_code) return json.loads(self.content)
def delete_config_map(self, name): if name: return name in self.config_map else: raise OsbsResponseException("Failed to delete config map", 404)
def test_remove_worker_plugin(tmpdir, caplog, user_params, platforms, fragment_annotation, fragment_key, cm_not_found): workflow = mock_workflow(tmpdir) annotations = {'worker-builds': {}} log = logging.getLogger("atomic_reactor.plugins." + OrchestrateBuildPlugin.key) build = None cluster = None load = None workspace = { 'build_info': {}, 'koji_upload_dir': 'foo', } for platform in platforms: build_name = 'build-1-%s' % platform metadata_fragment = None if platform: config_name = 'build-1-%s-md' % platform metadata_fragment = 'configmap/%s' % config_name osbs = MockOSBS() cluster_info = ClusterInfo(cluster, platform, osbs, load) worker_info = WorkerBuildInfo(build, cluster_info, log) workspace['build_info'][platform] = worker_info if fragment_key and fragment_annotation: if cm_not_found: (flexmock(osbs) .should_receive("delete_config_map") .with_args(config_name) .once() .and_raise(OsbsResponseException('none', 404))) else: (flexmock(osbs) .should_receive("delete_config_map") .with_args(config_name) .once() .and_return(True)) annotations['worker-builds'][platform] = {'build': {'build-name': build_name}} if fragment_annotation: annotations['worker-builds'][platform]['metadata_fragment'] = metadata_fragment annotations['worker-builds'][platform]['metadata_fragment_key'] = fragment_key workflow.build_result = BuildResult(annotations=annotations, image_id="id1234") workflow.plugin_workspace[OrchestrateBuildPlugin.key] = workspace runner = ExitPluginsRunner( None, workflow, [{ 'name': PLUGIN_REMOVE_WORKER_METADATA_KEY, "args": {} }] ) runner.run() for platform in platforms: if platform and fragment_key: cm_name = 'build-1-%s-md' % platform if not fragment_annotation: continue if cm_not_found: msg = "Failed to delete ConfigMap {} on platform {}:".format(cm_name, platform) assert msg in caplog.text else: msg = "ConfigMap {} on platform {} deleted". format(cm_name, platform) assert msg in caplog.text