From 70b3c49bb9ddb8b8cec3c37366a43451608c0594 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Oct 10 2017 06:46:49 +0000 Subject: Resolve content_sets also for parent images and handle situation when there are no repositories set for image. --- diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py index 4f53289..8b8cc2b 100644 --- a/freshmaker/lightblue.py +++ b/freshmaker/lightblue.py @@ -199,6 +199,26 @@ class ContainerImage(dict): data["srpm_nevra"] = srpm_nevra self.update(data) + def resolve_content_sets(self, lb_instance): + """ + Find out the content_sets this image uses and store it as + "content_sets" key in image. + """ + # Checking only the first repository is OK, because if an image + # is in multiple repositories, the content_sets of all of them + # must be the same by definition. + if "repositories" not in self or len(self["repositories"]) == 0: + log.warning("Container image %s does not have 'repositories' set " + "in Lightblue, this is suspicious.") + self.update({"content_sets": []}) + return + + image_content_sets = lb_instance.find_content_sets_for_repository( + self["repositories"][0]["repository"]) + log.info("Container image %s uses following content sets: %r", + self["brew"]["build"], image_content_sets) + self.update({"content_sets": image_content_sets}) + class LightBlue(object): """Interface to query lightblue""" @@ -638,6 +658,7 @@ class LightBlue(object): parent_build_layers_count, srpm_name=srpm_name) if image: + image.resolve_content_sets(self) image.resolve_commit(srpm_name) if images: @@ -659,6 +680,7 @@ class LightBlue(object): parent_top_layer, parent_build_layers_count) if parent: + parent.resolve_content_sets(self) parent.resolve_commit(srpm_name) images[-1]['parent'] = parent if not image: @@ -703,16 +725,7 @@ class LightBlue(object): images = [image for image in images if not filter_fnc(image)] for image in images: - # Find out the content_sets this image uses and store it as - # "content_sets" key in image. - # Checking only the first repository is OK, because if an image - # is in multiple repositories, the content_sets of all of them - # must be the same by definition. - image_content_sets = self.find_content_sets_for_repository( - image["repositories"][0]["repository"]) - log.info("Container image %s uses following content sets: %r", - image["brew"]["build"], image_content_sets) - image.update({"content_sets": image_content_sets}) + image.resolve_content_sets(self) image.resolve_commit(srpm_name) return images @@ -759,6 +772,7 @@ class LightBlue(object): else: parent = self.get_image_by_layer(layers[1], len(layers) - 1) if parent: + parent.resolve_content_sets(self) parent.resolve_commit(srpm_name) image['parent'] = parent rebuild_list.insert(0, image) diff --git a/tests/test_lightblue.py b/tests/test_lightblue.py index df1ddaf..3aebc40 100644 --- a/tests/test_lightblue.py +++ b/tests/test_lightblue.py @@ -24,7 +24,7 @@ import json import six import unittest -from mock import call, patch +from mock import call, patch, Mock from six.moves import http_client from freshmaker.lightblue import ContainerImage @@ -236,6 +236,33 @@ class TestContainerImageObject(unittest.TestCase): "Cannot find task_id or container_koji_task_id in the Koji build " "{'task_id': None}") != -1) + def test_resolve_content_sets_no_repositories(self): + image = ContainerImage.create({ + '_id': '1233829', + 'brew': { + 'build': 'package-name-1-4-12.10', + }, + }) + self.assertTrue("content_sets" not in image) + + lb = Mock() + image.resolve_content_sets(lb) + self.assertEqual(image["content_sets"], []) + + def test_resolve_content_sets_empty_repositories(self): + image = ContainerImage.create({ + '_id': '1233829', + 'brew': { + 'build': 'package-name-1-4-12.10', + }, + 'repositories': [] + }) + self.assertTrue("content_sets" not in image) + + lb = Mock() + image.resolve_content_sets(lb) + self.assertEqual(image["content_sets"], []) + class TestContainerRepository(unittest.TestCase): @@ -719,12 +746,13 @@ class TestQueryEntityFromLightBlue(unittest.TestCase): } ]) + @patch('freshmaker.lightblue.LightBlue.find_content_sets_for_repository') @patch('freshmaker.lightblue.LightBlue.find_container_images') @patch('os.path.exists') @patch('freshmaker.kojiservice.KojiService.get_build') @patch('freshmaker.kojiservice.KojiService.get_task_request') def test_parent_images_with_package(self, get_task_request, get_build, - exists, cont_images): + exists, cont_images, cont_sets): get_build.return_value = {"task_id": 123456} get_task_request.return_value = [ @@ -732,6 +760,7 @@ class TestQueryEntityFromLightBlue(unittest.TestCase): exists.return_value = True cont_images.side_effect = [self.fake_container_images, [], self.fake_container_images] + cont_sets.return_value = set(["content-set"]) lb = LightBlue(server_url=self.fake_server_url, cert=self.fake_cert_file,