From f860bfd8f6a45281678247d7b57100811a17184d Mon Sep 17 00:00:00 2001 From: Giulia Naponiello Date: Feb 27 2020 15:04:01 +0000 Subject: Merge #483 `Override parent image` --- diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py index 2f915b7..738ddf6 100644 --- a/freshmaker/lightblue.py +++ b/freshmaker/lightblue.py @@ -1250,6 +1250,33 @@ class LightBlue(object): return latest_parent + def find_parent_image_from_child(self, child_image): + """ + Returns the parent of the input image. If the parent is not found it returns None. + + :param ContainerImage child_image: ContainerImage object, image for which we need to find the parent. + + :return: parent of the input image. + :rtype: ContainerImage object + + """ + parent_brew_build = child_image.get("parent_brew_build") + if parent_brew_build: + return parent_brew_build + # We need to resolve the image in here because "parent_image_builds" needs to be there + # and it gets populated when the image gets resolved. + child_image.resolve_commit() + # If the parent is not in `parent_brew_build` we can try to look for the parent in Brew, + # using the field `parent_image_builds` (searching for the nvr), which should always be there. + # In case parent_brew_build is None and child_image["parent_image_builds"] == {}, + # it means we found a base image and there's no parent image. + if child_image["parent_image_builds"]: + parent_brew_build = [ + i["nvr"] for i in child_image["parent_image_builds"].values() + if i["id"] == child_image["parent_build_id"]][0] + + return parent_brew_build + def find_parent_images_with_package(self, child_image, srpm_name, images=None): """ Returns the chain of all parent images of the image which contain the @@ -1267,18 +1294,7 @@ class LightBlue(object): children = images if images else [child_image] # We first try to find the parent from the `parent_brew_build` field in Lightblue. - parent_brew_build = child_image.get("parent_brew_build") - # We need to resolve the image in here because "parent_image_builds" needs to be there - # and it gets populated when the image gets resolved. - child_image.resolve(self, children) - # If the parent is not in `parent_brew_build` we can try to look for the parent in Brew, - # using the field `parent_image_builds` (searching for the nvr), which should always be there. - # In case parent_brew_build is None and child_image["parent_image_builds"] == {}, - # it means we found a base image, so we'll just continue and return the children. - if not parent_brew_build and child_image["parent_image_builds"]: - parent_brew_build = [ - i["nvr"] for i in child_image["parent_image_builds"].values() - if i["id"] == child_image["parent_build_id"]][0] + parent_brew_build = self.find_parent_image_from_child(child_image) # We've reached the base image, stop recursion if not parent_brew_build: return children @@ -1687,6 +1703,14 @@ class LightBlue(object): image, srpm_name, []) if rebuild_list[srpm_name]: image['parent'] = rebuild_list[srpm_name][0] + else: + parent_brew_build = self.find_parent_image_from_child(image) + if parent_brew_build: + parent = self.get_images_by_nvrs([parent_brew_build], published=None) + if parent: + parent = parent[0] + parent.resolve(self, images) + image['parent'] = parent rebuild_list[srpm_name].insert(0, image) return rebuild_list diff --git a/tests/test_lightblue.py b/tests/test_lightblue.py index be1a2fe..1a26fb6 100644 --- a/tests/test_lightblue.py +++ b/tests/test_lightblue.py @@ -1627,6 +1627,42 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): "Couldn't find parent image some-original-nvr-7.6-252.1561619826. " "Lightblue data is probably incomplete")) + @patch("freshmaker.lightblue.LightBlue.get_images_by_nvrs") + @patch('freshmaker.lightblue.LightBlue.find_images_with_packages_from_content_set') + @patch('freshmaker.lightblue.LightBlue.find_unpublished_image_for_build') + @patch('freshmaker.lightblue.LightBlue.find_parent_images_with_package') + @patch('os.path.exists') + def test_parent_images_with_package_using_field_parent_brew_build_parent_empty( + self, exists, find_parent_images_with_package, find_unpublished_image_for_build, + find_images_with_packages_from_content_set, cont_images): + exists.return_value = True + + image_a = ContainerImage.create({ + "brew": {"package": "image-a", "build": "image-a-v-r1"}, + "parent_brew_build": "some-original-nvr-7.6-252.1561619826", + "repository": "repo-1", + "commit": "image-a-commit", + "repositories": [{"repository": "foo/bar"}], + "rpm_manifest": [{ + "rpms": [ + {"srpm_name": "dummy"} + ] + }] + }) + + find_parent_images_with_package.return_value = [] + find_unpublished_image_for_build.return_value = image_a + find_images_with_packages_from_content_set.return_value = [image_a] + cont_images.side_effect = [self.fake_container_images_with_parent_brew_build, [], []] + + lb = LightBlue(server_url=self.fake_server_url, + cert=self.fake_cert_file, + private_key=self.fake_private_key) + ret = lb.find_images_to_rebuild(["dummy-1-1"], ["dummy"]) + + self.assertEqual(len(ret), 1) + self.assertIsNotNone(ret[0][0].get("parent")) + @patch("freshmaker.lightblue.ContainerImage.resolve_published") @patch("freshmaker.lightblue.LightBlue.get_images_by_nvrs") @patch("os.path.exists")