From 74b02a903ce94960d878d78f13d5f7f6b0ce2742 Mon Sep 17 00:00:00 2001 From: Giulia Naponiello Date: Feb 17 2020 12:23:57 +0000 Subject: Filter out images based on the content_set Freshmaker queries Lightblue to get images containing affected RPMs installed from a particular content_set. At the same time Freshmaker asks to Lightblue also all the images with enabled the auto_rebuild_tags tag (when not enabled the rebuilds of images in this repository are disabled). This gets done only because the Lightblue query will be easier and cleaner this way. But because of that some images returned by that query will not have the correct content_sets, for this reason we need to filter out images based on the content_sets. Signed-off-by: Giulia Naponiello --- diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py index b51a91b..4755c8e 100644 --- a/freshmaker/lightblue.py +++ b/freshmaker/lightblue.py @@ -821,6 +821,34 @@ class LightBlue(object): image["brew"]["build"], srpm_name_to_nvrs.values())) return ret + def filter_out_images_based_on_content_set(self, images, content_sets): + """ + Filter out container images based on the content_set. + + Freshmaker queries Lightblue to get images containing affected RPMs installed from a + particular content_set. At the same time Freshmaker asks to Lightblue also all the images + with enabled the auto_rebuild_tags tag (when not enabled the rebuilds of images in this + repository are disabled). + This gets done only because the Lightblue query will be easier and cleaner this way. + But because of that some images returned by that query will not have the correct + content_sets, for this reason we need to filter out images based on the content_sets. + + :param list images: List of ContainerImage instances. + :param set content_sets: List of content_sets the image includes RPMs + from. + :rtype: list + :return: List of ContainerImage instances without the filtered images. + """ + ret = [] + for image in images: + if not content_sets & set(image["content_sets"]): + log.info(f"Will not rebuild {image['brew']['build']} because its content_sets " + "({image['content_sets']}) are not related to the requested content_sets" + " ({content_sets})") + else: + ret.append(image) + return ret + def find_images_with_included_srpms( self, content_sets, srpm_nvrs, repositories, published=True, include_rpms=True): @@ -943,6 +971,8 @@ class LightBlue(object): images = list(image_nvr_to_image.values()) images = self.filter_out_images_with_higher_srpm_nvr(images, srpm_name_to_nvrs) images = self.filter_out_modularity_mismatch(images, srpm_name_to_nvrs) + if content_sets: + images = self.filter_out_images_based_on_content_set(images, set(content_sets)) return images def get_images_by_nvrs(self, nvrs, published=True, content_sets=None, diff --git a/tests/test_lightblue.py b/tests/test_lightblue.py index c9eed04..edec2bd 100644 --- a/tests/test_lightblue.py +++ b/tests/test_lightblue.py @@ -1070,7 +1070,7 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): self.fake_container_images.append(self.fake_container_images[1]) cont_images.return_value = self.fake_container_images ret = lb.find_images_with_included_srpms( - ["content-set-1", "content-set-2"], ["openssl-1.2.3-2"], repositories) + ["dummy-content-set-1", "dummy-content-set-2"], ["openssl-1.2.3-2"], repositories) expected_image_request = { "objectType": "containerImage", @@ -1105,12 +1105,12 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): { "field": "content_sets.*", "op": "=", - "rvalue": "content-set-1" + "rvalue": "dummy-content-set-1" }, { "field": "content_sets.*", "op": "=", - "rvalue": "content-set-2" + "rvalue": "dummy-content-set-2" }, ], }, @@ -1165,7 +1165,7 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): self.fake_container_images + self.fake_container_images_floating_tag) ret = lb.find_images_with_included_srpms( - ["content-set-1", "content-set-2"], ["openssl-1.2.3-2"], repositories) + ["dummy-content-set-1", "dummy-content-set-2"], ["openssl-1.2.3-2"], repositories) self.assertEqual( [image["brew"]["build"] for image in ret], @@ -1206,7 +1206,7 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): self.fake_container_images + self.fake_container_images_floating_tag) ret = lb.find_images_with_included_srpms( - ["content-set-1", "content-set-2"], + ["dummy-content-set-1", "dummy-content-set-2"], ["openssl-1.2.3-1", "openssl-1.2.3-50"], repositories) self.assertEqual( [image["brew"]["build"] for image in ret], @@ -1229,10 +1229,11 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): # "filtered_x-1-23" image will be filtered by filter_fnc. cont_images.return_value = self.fake_container_images + [ ContainerImage.create( - {"brew": {"build": "filtered_x-1-23"}, - 'repositories': [ - {'repository': 'product/repo1', 'published': True, - 'tags': [{"name": "latest"}]}]})] + {"content_sets": ["dummy-content-set-1"], + "brew": {"build": "filtered_x-1-23"}, + "repositories": [ + {"repository": "product/repo1", "published": True, + "tags": [{"name": "latest"}]}]})] # Include the images for second time to ensure that they will be # returned only once. This can happen when the image is multiarch. cont_images.return_value += self.fake_container_images @@ -1797,16 +1798,48 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): cont_images.return_value = ( self.fake_images_with_modules) ret = lb.find_images_with_included_srpms( - ["content-set-1", "content-set-2"], ["openssl-1.2.3-2.module+el8.0.0+3248+9d514f3b.src"], repositories) + ["dummy-content-set-1", "dummy-content-set-2"], ["openssl-1.2.3-2.module+el8.0.0+3248+9d514f3b.src"], repositories) self.assertEqual( [image["brew"]["build"] for image in ret], ["package-name-3-4-12.10"]) ret = lb.find_images_with_included_srpms( - ["content-set-1", "content-set-2"], ["openssl-1.2.3-2.el8.0.0+3248+9d514f3b.src"], repositories) + ["dummy-content-set-1", "dummy-content-set-2"], ["openssl-1.2.3-2.el8.0.0+3248+9d514f3b.src"], repositories) self.assertEqual( [image["brew"]["build"] for image in ret], []) + @patch('freshmaker.lightblue.LightBlue.find_container_images') + @patch('os.path.exists') + def test_filter_out_by_content_sets( + self, exists, cont_images): + + repos = [{ + "repository": "product/repo1", "published": True, + 'tags': [{"name": "latest"}]}] + exists.return_value = True + lb = LightBlue(server_url=self.fake_server_url, + cert=self.fake_cert_file, + private_key=self.fake_private_key) + repositories = { + repo["repository"]: repo for repo in + self.fake_repositories_with_content_sets} + parent = ContainerImage.create({ + "brew": {"build": "parent-1-2"}, "repositories": repos, + "content_sets": ["dummy-content-set-1"]}) + latest_parent = ContainerImage.create({ + "brew": {"build": "parent-1-3"}, "repositories": repos, + "content_sets": ["dummy-content-set-1"]}) + older_parent = ContainerImage.create({ + "brew": {"build": "parent-1-1"}, "repositories": repos, + "content_sets": ["dummy-content-set-2"]}) + cont_images.return_value = [parent, latest_parent, older_parent] + + ret = lb.find_images_with_included_srpms( + ["dummy-content-set-1"], ["openssl-1.2.3-2.module+el8.0.0+3248+9d514f3b.src"], repositories) + self.assertEqual( + [image["brew"]["build"] for image in ret], + ["parent-1-2", "parent-1-3"]) + class TestEntityVersion(helpers.FreshmakerTestCase): """Test case for ensuring correct entity version in request"""