Beispiel #1
0
    def get(self, project, repo):
        """
        Verifies whether the container image repository is handled
        by Freshmaker. If not, returns explanation why.

        **Sample request**:

        .. sourcecode:: http

            GET /api/1/verify-image-repository/foo/bar HTTP/1.1
            Accept: application/json

        **Sample response**:

        .. sourcecode:: none

            {
                "repository: {
                    "auto_rebuild_tags": [
                        "latest"
                    ],
                },
                "images": {
                    "foo-1-1": {
                        "content_sets": [
                            "content-set-1",
                            "content-set-2"
                        ],
                        "tags": [
                            "latest",
                            "2.0"
                        ]
                    }
                },
                "msg": "Found 1 images which are handled by Freshmaker."
            }

        :statuscode 200: Image repository is handled by Freshmaker.
        :statuscode 400: Image repository is not handled by Freshmaker or not
            found.
        """
        if not project and not repo:
            raise ValueError("No image repository name provided")

        verifier = ImageVerifier()
        data = verifier.verify_repository("%s/%s" % (project, repo))
        ret = {
            "msg":
            "Found %d images which are handled by Freshmaker for "
            "defined content_sets." % len(data["images"]),
            "images":
            data["images"],
            "repository":
            data["repository"]
        }
        return jsonify(ret), 200
Beispiel #2
0
    def get(self, image):
        """
        Verifies whether the container image defined by the NVR is handled
        by Freshmaker. If not, returns explanation why.

        **Sample request**:

        .. sourcecode:: http

            GET /api/1/verify-image/foo-1-1 HTTP/1.1
            Accept: application/json

        **Sample response**:

        .. sourcecode:: none

            {
                "images": {
                    "foo-1-1": [
                        "content-set-1",
                        "content-set-2"
                    ]
                },
                "msg": "Found 1 images which are handled by Freshmaker."
            }

        :statuscode 200: Image is handled by Freshmaker.
        :statuscode 400: Image is not handled by Freshmaker or not found.
        """
        if not image:
            raise ValueError("No image name provided")

        verifier = ImageVerifier()
        images = verifier.verify_image(image)
        ret = {
            "msg":
            "Found %d images which are handled by Freshmaker for "
            "defined content_sets." % len(images),
            "images":
            images
        }
        return jsonify(ret), 200
class TestImageVerifier(helpers.FreshmakerTestCase):

    def setUp(self):
        super(TestImageVerifier, self).setUp()
        self.lb = MagicMock()
        self.verifier = ImageVerifier(self.lb)

    def test_verify_repository_no_repo(self):
        self.lb.find_container_repositories.return_value = None
        self.assertRaisesRegex(
            ValueError, r'Cannot get repository.*',
            self.verifier.verify_repository, "foo/bar")

    def test_get_verify_repository_multiple_repos(self):
        self.lb.find_container_repositories.return_value = ["foo", "bar"]
        self.assertRaisesRegex(
            ValueError, r'Multiple records found.*',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_deprecated(self):
        self.lb.find_container_repositories.return_value = [{
            "release_categories": ["Deprecated"],
            "published": True,
            "auto_rebuild_tags": "latest"}]
        self.assertRaisesRegex(
            ValueError, r'.*but found \[\'Deprecated\'\].',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_not_published(self):
        self.lb.find_container_repositories.return_value = [{
            "release_categories": ["Generally Available"],
            "published": False,
            "auto_rebuild_tags": "latest"}]
        self.assertRaisesRegex(
            ValueError, r'.*is not published.',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_no_auto_rebuild_tags(self):
        self.lb.find_container_repositories.return_value = [{
            "release_categories": ["Generally Available"],
            "published": True,
            "auto_rebuild_tags": []}]
        self.assertRaisesRegex(
            ValueError, r'.*this repository are disabled.',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_no_images(self):
        self.lb.find_container_repositories.return_value = [{
            "repository": "foo/bar",
            "release_categories": ["Generally Available"],
            "published": True,
            "auto_rebuild_tags": ["latest"]}]
        self.lb.get_images_by_nvrs.return_value = []
        self.assertRaisesRegex(
            ValueError, r'No published images tagged by.*',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_no_content_sets(self):
        self.lb.find_container_repositories.return_value = [
            ContainerRepository({
                "repository": "foo/bar",
                "release_categories": ["Generally Available"],
                "published": True,
                "auto_rebuild_tags": ["latest"]
            })
        ]
        self.lb.find_images_with_included_srpms.return_value = [
            ContainerImage({
                "brew": {"build": "foo-1-1"},
                "content_sets": []
            })
        ]
        self.assertRaisesRegex(
            ValueError, r'.*are not set for this image.',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository(self):
        self.lb.find_container_repositories.return_value = [
            ContainerRepository({
                "repository": "foo/bar",
                "release_categories": ["Generally Available"],
                "published": True,
                "auto_rebuild_tags": ["latest"]
            })
        ]
        self.lb.find_images_with_included_srpms.return_value = [
            ContainerImage({
                "brew": {"build": "foo-1-1"},
                "content_sets": ["content-set"]
            })
        ]
        ret = self.verifier.verify_repository("foo/bar")
        self.assertEqual(ret, {"foo-1-1": ["content-set"]})

    def test_get_verify_image(self):
        self.lb.find_container_repositories.return_value = [
            ContainerRepository({
                "repository": "foo/bar",
                "release_categories": ["Generally Available"],
                "published": True,
                "auto_rebuild_tags": ["latest"]
            })
        ]
        self.lb.get_images_by_nvrs.return_value = [
            ContainerImage({
                "brew": {"build": "foo-1-1"},
                "content_sets": ["content-set"]
            })
        ]
        ret = self.verifier.verify_image("foo-1-1")
        self.assertEqual(ret, {"foo-1-1": ["content-set"]})

    def test_get_verify_image_no_repo(self):
        self.lb.find_container_repositories.return_value = []
        self.assertRaisesRegex(
            ValueError, r'Cannot get repository.*',
            self.verifier.verify_image, "foo/bar")

    def test_get_verify_image_multiple_repos(self):
        self.lb.find_container_repositories.return_value = ["foo", "bar"]
        self.assertRaisesRegex(
            ValueError, r'.*found in multiple repositories in Lightblue.',
            self.verifier.verify_image, "foo/bar")

    def test_verify_image_no_images(self):
        self.lb.find_container_repositories.return_value = [{
            "repository": "foo/bar",
            "release_categories": ["Generally Available"],
            "published": True,
            "auto_rebuild_tags": ["latest"]}]
        self.lb.get_images_by_nvrs.return_value = []
        self.assertRaisesRegex(
            ValueError, r'No published images tagged by.*',
            self.verifier.verify_image, "foo/bar")
 def setUp(self):
     super(TestImageVerifier, self).setUp()
     self.lb = MagicMock()
     self.verifier = ImageVerifier(self.lb)
class TestImageVerifier(helpers.FreshmakerTestCase):

    def setUp(self):
        super(TestImageVerifier, self).setUp()
        self.lb = MagicMock()
        self.verifier = ImageVerifier(self.lb)

    def test_verify_repository_no_repo(self):
        self.lb.find_container_repositories.return_value = None
        self.assertRaisesRegex(
            ValueError, r'Cannot get repository.*',
            self.verifier.verify_repository, "foo/bar")

    def test_get_verify_repository_multiple_repos(self):
        self.lb.find_container_repositories.return_value = ["foo", "bar"]
        self.assertRaisesRegex(
            ValueError, r'Multiple records found.*',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_deprecated(self):
        self.lb.find_container_repositories.return_value = [{
            "release_categories": ["Deprecated"],
            "published": True,
            "auto_rebuild_tags": "latest"}]
        self.assertRaisesRegex(
            ValueError, r'.*but found \[\'Deprecated\'\].',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_not_published(self):
        self.lb.find_container_repositories.return_value = [{
            "registry": "motor-vehical.registry.local",
            "repository": "long/wait",
            "release_categories": ["Generally Available"],
            "published": False,
            "auto_rebuild_tags": "latest"}]
        self.assertRaisesRegex(
            ValueError, r'.*is not published.',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_data(self):
        repo = {
            "registry": "motor-vehical.registry.local",
            "repository": "long/wait",
            "release_categories": ["Generally Available"],
            "published": False,
            "auto_rebuild_tags": "latest",
        }
        exceptions = [{"registry": "motor-vehical.registry.local", "repository": "long/wait"}]
        target = "freshmaker.image_verifier.conf.unpublished_exceptions"
        with patch(target, new=exceptions):
            self.verifier._verify_repository_data(repo)

    def test_verify_repository_no_auto_rebuild_tags(self):
        self.lb.find_container_repositories.return_value = [{
            "release_categories": ["Generally Available"],
            "published": True,
            "auto_rebuild_tags": []}]
        self.assertRaisesRegex(
            ValueError, r'.*this repository are disabled.',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_no_images(self):
        self.lb.find_container_repositories.return_value = [{
            "repository": "foo/bar",
            "release_categories": ["Generally Available"],
            "published": True,
            "auto_rebuild_tags": ["latest"]}]
        self.lb.get_images_by_nvrs.return_value = []
        self.assertRaisesRegex(
            ValueError, r'No published images tagged by.*',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository_no_content_sets(self):
        self.lb.find_container_repositories.return_value = [
            ContainerRepository({
                "repository": "foo/bar",
                "release_categories": ["Generally Available"],
                "published": True,
                "auto_rebuild_tags": ["latest"]
            })
        ]
        self.lb.find_images_with_included_rpms.return_value = [
            ContainerImage({
                "brew": {"build": "foo-1-1"},
                "content_sets": []
            })
        ]
        self.assertRaisesRegex(
            ValueError, r'.*are not set for this image.',
            self.verifier.verify_repository, "foo/bar")

    def test_verify_repository(self):
        self.lb.find_container_repositories.return_value = [
            ContainerRepository({
                "repository": "foo/bar",
                "release_categories": ["Generally Available"],
                "published": True,
                "auto_rebuild_tags": ["latest"]
            })
        ]
        self.lb.find_images_with_included_rpms.return_value = [
            ContainerImage({
                "brew": {"build": "foo-1-1"},
                "content_sets": ["content-set"],
                "repositories": [
                    {
                        "registry": "registry.example.com",
                        "published": True,
                        "repository": "foo/bar",
                        "tags": [{"name": "1"}, {"name": "latest"}, {"name": "1-1"}],
                    },
                    {
                        "registry": "registry.build.example.com",
                        "published": False,
                        "repository": "buildsys/foobar",
                        "tags": [{"name": "1-1"}, {"name": "1.old"}],
                    },
                ]
            })
        ]
        ret = self.verifier.verify_repository("foo/bar")
        expected = {
            "repository": {"auto_rebuild_tags": ["latest"]},
            "images": {
                "foo-1-1": {"content_sets": ["content-set"], "tags": ["1", "latest", "1-1"]}
            },
        }
        self.assertEqual(ret, expected)

    def test_get_verify_image(self):
        self.lb.find_container_repositories.return_value = [
            ContainerRepository({
                "repository": "foo/bar",
                "release_categories": ["Generally Available"],
                "published": True,
                "auto_rebuild_tags": ["latest"]
            })
        ]
        self.lb.get_images_by_nvrs.return_value = [
            ContainerImage({
                "brew": {"build": "foo-1-1"},
                "content_sets": ["content-set"]
            })
        ]
        ret = self.verifier.verify_image("foo-1-1")
        self.assertEqual(ret, {"foo-1-1": ["content-set"]})

    def test_get_verify_image_no_repo(self):
        self.lb.find_container_repositories.return_value = []
        self.assertRaisesRegex(
            ValueError, r'Cannot get repository.*',
            self.verifier.verify_image, "foo/bar")

    def test_get_verify_image_multiple_repos(self):
        self.lb.find_container_repositories.return_value = ["foo", "bar"]
        self.assertRaisesRegex(
            ValueError, r'.*found in multiple repositories in Lightblue.',
            self.verifier.verify_image, "foo/bar")

    def test_verify_image_no_images(self):
        self.lb.find_container_repositories.return_value = [{
            "repository": "foo/bar",
            "release_categories": ["Generally Available"],
            "published": True,
            "auto_rebuild_tags": ["latest"]}]
        self.lb.get_images_by_nvrs.return_value = []
        self.assertRaisesRegex(
            ValueError, r'No published images tagged by.*',
            self.verifier.verify_image, "foo/bar")