Esempio n. 1
0
    def test_import_image(self, openshift, imagestream_name, expect_update,
                          expect_import):
        """
        tests that import_image return True
        regardless if tags were changed
        """
        this_file = inspect.getfile(TestCheckResponse)
        this_dir = os.path.dirname(this_file)

        json_path = os.path.join(this_dir, "mock_jsons",
                                 openshift._con.version, 'imagestream.json')
        with open(json_path) as f:
            template_resource_json = json.load(f)

        initial_resource_json = deepcopy(template_resource_json)

        modified_resource_json = deepcopy(template_resource_json)
        source_repo = modified_resource_json['spec'].pop(
            'dockerImageRepository')
        modified_resource_json['metadata']['annotations'][
            ANNOTATION_SOURCE_REPO] = source_repo
        if not expect_import:
            modified_resource_json['spec']['tags'] = []

        put_url = openshift._build_url("imagestreams/%s" % imagestream_name)
        (flexmock(openshift).should_call('_put').times(
            1 if expect_update else 0).with_args(
                put_url,
                data=JsonMatcher(modified_resource_json),
                use_json=True))

        stream_import = {'metadata': {'name': 'FOO'}, 'spec': {'images': []}}
        assert openshift.import_image(imagestream_name,
                                      stream_import) is expect_import
Esempio n. 2
0
 def test_get_missing_build_config(self, openshift):  # noqa
     build_config_name = 'some-build-config-name'
     expected_url = openshift._build_url("buildconfigs/%s/" %
                                         build_config_name)
     (flexmock(openshift).should_receive("_get").with_args(
         expected_url).once().and_return(HttpResponse(404, {}, b'')))
     with pytest.raises(OsbsResponseException):
         openshift.get_build_config(build_config_name)
Esempio n. 3
0
 def test_get_build_config(self, openshift):  # noqa
     mock_response = {"spam": "maps"}
     build_config_name = 'some-build-config-name'
     expected_url = openshift._build_url("buildconfigs/%s/" %
                                         build_config_name)
     (flexmock(openshift).should_receive("_get").with_args(
         expected_url).once().and_return(make_json_response(mock_response)))
     response = openshift.get_build_config(build_config_name)
     assert response['spam'] == 'maps'
Esempio n. 4
0
 def test_get_missing_build_config(self, openshift):
     build_config_name = 'some-build-config-name'
     expected_url = openshift._build_url("buildconfigs/%s/" % build_config_name)
     (flexmock(openshift)
         .should_receive("_get")
         .with_args(expected_url)
         .once()
         .and_return(HttpResponse(404, {}, '')))
     with pytest.raises(OsbsResponseException):
         openshift.get_build_config(build_config_name)
Esempio n. 5
0
 def test_get_build_config(self, openshift):
     mock_response = {"spam": "maps"}
     build_config_name = 'some-build-config-name'
     expected_url = openshift._build_url("buildconfigs/%s/" % build_config_name)
     (flexmock(openshift)
         .should_receive("_get")
         .with_args(expected_url)
         .once()
         .and_return(HttpResponse(200, {}, json.dumps(mock_response))))
     response = openshift.get_build_config(build_config_name)
     assert response['spam'] == 'maps'
Esempio n. 6
0
 def test_get_build_config_by_labels(self, openshift):  # noqa
     mock_response = {"items": [{"spam": "maps"}]}
     label_selectors = (
         ('label-1', 'value-1'),
         ('label-2', 'value-2'),
     )
     expected_url = openshift._build_url(
         "buildconfigs/?labelSelector=label-1%3Dvalue-1%2Clabel-2%3Dvalue-2"
     )
     (flexmock(openshift).should_receive("_get").with_args(
         expected_url).once().and_return(make_json_response(mock_response)))
     response = openshift.get_build_config_by_labels(label_selectors)
     assert response['spam'] == 'maps'
Esempio n. 7
0
    def test_get_multiple_build_config_by_labels(self, openshift):  # noqa
        mock_response = {"items": [{"spam": "maps"}, {"eggs": "sgge"}]}
        label_selectors = (
            ('label-1', 'value-1'),
            ('label-2', 'value-2'),
        )
        expected_url = openshift._build_url(
            "buildconfigs/?labelSelector=label-1%3Dvalue-1%2Clabel-2%3Dvalue-2"
        )
        (flexmock(openshift).should_receive("_get").with_args(
            expected_url).once().and_return(make_json_response(mock_response)))

        with pytest.raises(OsbsException) as exc:
            openshift.get_build_config_by_labels(label_selectors)
        assert str(exc.value).startswith('More than one build config found')
Esempio n. 8
0
 def test_get_build_config_by_labels(self, openshift):
     mock_response = {"items": [{"spam": "maps"}]}
     build_config_name = 'some-build-config-name'
     label_selectors = (
         ('label-1', 'value-1'),
         ('label-2', 'value-2'),
     )
     expected_url = openshift._build_url(
         "buildconfigs/?labelSelector=label-1%3Dvalue-1%2Clabel-2%3Dvalue-2"
     )
     (flexmock(openshift).should_receive("_get").with_args(
         expected_url).once().and_return(
             HttpResponse(200, {}, json.dumps(mock_response))))
     response = openshift.get_build_config_by_labels(label_selectors)
     assert response['spam'] == 'maps'
Esempio n. 9
0
    def test_get_missing_build_config_by_labels(self, openshift):  # noqa:F811
        mock_response = {"items": []}
        label_selectors = (
            ('label-1', 'value-1'),
            ('label-2', 'value-2'),
        )
        expected_url = openshift._build_url(
            "buildconfigs/?labelSelector=label-1%3Dvalue-1%2Clabel-2%3Dvalue-2"
        )
        (flexmock(openshift).should_receive("_get").with_args(
            expected_url).once().and_return(make_json_response(mock_response)))

        with pytest.raises(OsbsException) as exc:
            openshift.get_build_config_by_labels(label_selectors)
        assert str(exc.value).startswith('Build config not found')
Esempio n. 10
0
 def test_get_build_config_by_labels(self, openshift):
     mock_response = {"items": [{"spam": "maps"}]}
     build_config_name = 'some-build-config-name'
     label_selectors = (
         ('label-1', 'value-1'),
         ('label-2', 'value-2'),
     )
     expected_url = openshift._build_url(
         "buildconfigs/?labelSelector=label-1%3Dvalue-1%2Clabel-2%3Dvalue-2")
     (flexmock(openshift)
         .should_receive("_get")
         .with_args(expected_url)
         .once()
         .and_return(HttpResponse(200, {}, json.dumps(mock_response))))
     response = openshift.get_build_config_by_labels(label_selectors)
     assert response['spam'] == 'maps'
Esempio n. 11
0
    def test_get_build_config_by_labels_filtered_fail(self, openshift, items,
                                                      filter_value, error):
        mock_response = {"items": items}
        label_selectors = (
            ('label-1', 'value-1'),
            ('label-2', 'value-2'),
        )
        expected_url = openshift._build_url(
            "buildconfigs/?labelSelector=label-1%3Dvalue-1%2Clabel-2%3Dvalue-2"
        )
        (flexmock(openshift).should_receive("_get").with_args(
            expected_url).once().and_return(make_json_response(mock_response)))

        with pytest.raises(OsbsException) as exc:
            openshift.get_build_config_by_labels_filtered(
                label_selectors, "maps.spam", filter_value)
        assert str(exc.value).startswith(error)
Esempio n. 12
0
    def test_get_multiple_build_config_by_labels(self, openshift):
        mock_response = {"items": [{"spam": "maps"}, {"eggs": "sgge"}]}
        build_config_name = 'some-build-config-name'
        label_selectors = (
            ('label-1', 'value-1'),
            ('label-2', 'value-2'),
        )
        expected_url = openshift._build_url(
            "buildconfigs/?labelSelector=label-1%3Dvalue-1%2Clabel-2%3Dvalue-2")
        (flexmock(openshift)
            .should_receive("_get")
            .with_args(expected_url)
            .once()
            .and_return(HttpResponse(200, {}, json.dumps(mock_response))))

        with pytest.raises(OsbsException) as exc:
            openshift.get_build_config_by_labels(label_selectors)
        assert str(exc.value).startswith('More than one build config found')
Esempio n. 13
0
    def test_put_image_stream_tag(self, openshift):  # noqa
        tag_name = 'spam'
        tag_id = 'maps:' + tag_name
        mock_data = {
            'kind': 'ImageStreamTag',
            'apiVersion': 'v1',
            'tag': {
                'name': tag_name
            }
        }

        expected_url = openshift._build_url('imagestreamtags/' + tag_id)
        (flexmock(openshift).should_receive("_put").with_args(
            expected_url,
            data=json.dumps(mock_data),
            headers={
                "Content-Type": "application/json"
            }).once().and_return(make_json_response(mock_data)))

        openshift.put_image_stream_tag(tag_id, mock_data)
Esempio n. 14
0
    def test_import_image(self, openshift, modify, state, imported):
        """
        tests that import_image return True
        regardless if tags were changed
        """
        this_file = inspect.getfile(TestCheckResponse)
        this_dir = os.path.dirname(this_file)

        json_path = os.path.join(this_dir, "mock_jsons",
                                 openshift._con.version, 'imagestream.json')
        resource_json = json.load(open(json_path))

        # keep just 1 tag, so it will be different from oldtags (3 tags)
        if modify:
            resource_json['status']['tags'] = [
                resource_json['status']['tags'][0]
            ]

        (flexmock(openshift).should_receive('watch_resource').and_yield(
            (state, resource_json)))

        SERIALIZED = object()

        def fake_json_dumps(imagestream_json):
            check_annotation = 'openshift.io/image.dockerRepositoryCheck'
            assert check_annotation not in imagestream_json['metadata'][
                'annotations']
            return SERIALIZED

        flexmock(json).should_receive('dumps').replace_with(fake_json_dumps)

        put_url = openshift._build_url("imagestreams/%s" % TEST_IMAGESTREAM)
        (flexmock(openshift).should_call('_put').with_args(put_url,
                                                           data=SERIALIZED,
                                                           use_json=True))

        assert openshift.import_image(TEST_IMAGESTREAM) is imported
Esempio n. 15
0
    def test_ensure_image_stream_tag(self, existing_scheduled,
                                     existing_insecure, expected_scheduled,
                                     s_annotations, expected_insecure,
                                     status_code, openshift):
        stream_name = 'spam'
        stream_repo = 'some.registry.com/spam'
        stream = {
            'metadata': {
                'name': stream_name
            },
            'spec': {
                'dockerImageRepository': stream_repo
            }
        }
        if s_annotations is not None:
            stream['metadata']['annotations'] = s_annotations

        tag_name = 'maps'
        tag_id = '{0}:{1}'.format(stream_name, tag_name)

        expected_url = openshift._build_url('imagestreamtags/' + tag_id)

        def verify_image_stream_tag(*args, **kwargs):
            data = json.loads(kwargs['data'])

            assert (bool(data['tag']['importPolicy'].get('insecure')) ==
                    expected_insecure)
            assert (bool(data['tag']['importPolicy'].get('scheduled')) ==
                    expected_scheduled)

            # Also verify new image stream tags are created properly.
            if status_code == 404:
                assert data['metadata']['name'] == tag_id
                assert data['tag']['name'] == tag_name
                assert (data['tag']['from']['name'] == '{0}:{1}'.format(
                    stream_repo, tag_name))

            return make_json_response({})

        expected_change = False
        expected_error = status_code == 500

        mock_response = {}

        expectation = (flexmock(openshift).should_receive("_get").with_args(
            expected_url).once())

        if status_code == 200:
            existing_image_stream_tag = {'tag': {'importPolicy': {}}}

            if existing_insecure is not None:
                existing_image_stream_tag['tag']['importPolicy']['insecure'] = \
                    existing_insecure

            if existing_scheduled is not None:
                existing_image_stream_tag['tag']['importPolicy']['scheduled'] = \
                    existing_scheduled

            mock_response = existing_image_stream_tag

            if expected_insecure != bool(existing_insecure) or \
               expected_scheduled != bool(existing_scheduled):
                expected_change = True

            expectation.and_return(make_json_response(mock_response))

        else:
            expectation.and_return(
                HttpResponse(status_code, headers={}, content=b''))

        if status_code == 404:
            expected_change = True

        if expected_change:
            (flexmock(openshift).should_receive("_put").with_args(
                expected_url,
                data=str,
                headers={
                    "Content-Type": "application/json"
                }).replace_with(verify_image_stream_tag).once())

        if expected_error:
            with pytest.raises(OsbsResponseException):
                openshift.ensure_image_stream_tag(stream, tag_name,
                                                  self._make_tag_template(),
                                                  expected_scheduled)

        else:
            assert (openshift.ensure_image_stream_tag(
                stream, tag_name, self._make_tag_template(),
                expected_scheduled) == expected_change)