From 31f4a5c04da1b7074b877cfffd292ac109288e79 Mon Sep 17 00:00:00 2001
From: Jan Kaluza <jkaluza@redhat.com>
Date: Jan 22 2018 07:01:02 +0000
Subject: Log error in case we cannot find unpublished version of container image in lightblue


---

diff --git a/freshmaker/handlers/__init__.py b/freshmaker/handlers/__init__.py
index 71ebd0e..a58842b 100644
--- a/freshmaker/handlers/__init__.py
+++ b/freshmaker/handlers/__init__.py
@@ -288,7 +288,7 @@ class BaseHandler(object):
                     set(criteria.keys())):
                 return False
             if whitelist and any([match_rule(criteria, rule) for rule in whitelist]):
-                log.debug('%r, type=%r is not whitelisted.',
+                log.debug('%r, type=%r is whitelisted.',
                           criteria, artifact_type.name.lower())
                 return True
         except re.error as exc:
@@ -300,6 +300,8 @@ class BaseHandler(object):
             log.error(err_msg)
             raise UnprocessableEntity(err_msg)
 
+        log.debug('%r, type=%r is not whitelisted.',
+                  criteria, artifact_type.name.lower())
         return False
 
 
diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py
index 2910496..0c293c1 100644
--- a/freshmaker/lightblue.py
+++ b/freshmaker/lightblue.py
@@ -118,6 +118,21 @@ class ContainerImage(dict):
     def __hash__(self):
         return hash((self['brew']['build']))
 
+    def log_error(self, err):
+        """
+        Logs the error associated with this image and sets self["error"].
+        If there has been previous call of log_error, new `err` is appended
+        to self['error'] with ';' separator.
+        """
+        prefix = ""
+        if 'brew' in self and 'build' in self['brew']:
+            prefix = self['brew']['build'] + ": "
+        log.error("%s%s", prefix, err)
+        if 'error' not in self or not self['error']:
+            self['error'] = str(err)
+        else:
+            self['error'] += "; " + str(err)
+
     @property
     def is_base_image(self):
         return (self['parent'] is None and
@@ -817,7 +832,10 @@ class LightBlue(object):
             unpublished = self.find_unpublished_image_for_build(
                 image['brew']['build'])
             if not unpublished:
-                return []
+                image.log_error(
+                    "Cannot find unpublished version of image, Lightblue "
+                    "data is probably incomplete")
+                return [image]
 
             layers = unpublished["parsed_data"]["layers"]
             rebuild_list = self.find_parent_images_with_package(
@@ -830,14 +848,10 @@ class LightBlue(object):
                     parent.resolve_content_sets(self, children=[image])
                     parent.resolve_commit(srpm_name)
                 elif len(layers) != 2:
-                    err = "Cannot find parent of image %s with layer %s " \
-                          "and layer count %d in Lightblue, Lightblue data " \
-                          "is probably incomplete" % (
-                              image['brew']['build'], layers[1],
-                              len(layers) - 1)
-                    log.error(err)
-                    if not image['error']:
-                        image['error'] = err
+                    image.log_error(
+                        "Cannot find parent image with layer %s and layer "
+                        "count %d in Lightblue, Lightblue data is probably "
+                        "incomplete" % (layers[1], len(layers) - 1))
                 image['parent'] = parent
             rebuild_list.insert(0, image)
             return rebuild_list
diff --git a/tests/test_lightblue.py b/tests/test_lightblue.py
index 2960275..a2e285c 100644
--- a/tests/test_lightblue.py
+++ b/tests/test_lightblue.py
@@ -132,6 +132,19 @@ class TestContainerImageObject(unittest.TestCase):
         self.assertEqual('1233829', image['_id'])
         self.assertEqual('20151210T10:09:35.000-0500', image['brew']['completion_date'])
 
+    def test_log_error(self):
+        image = ContainerImage.create({
+            'brew': {
+                'build': 'package-name-1-4-12.10',
+            },
+        })
+
+        image.log_error("foo")
+        self.assertEqual(image['error'], "foo")
+
+        image.log_error("bar")
+        self.assertEqual(image['error'], "foo; bar")
+
     @patch('freshmaker.kojiservice.KojiService.get_build')
     @patch('freshmaker.kojiservice.KojiService.get_task_request')
     def test_resolve_commit_koji_fallback(self, get_task_request, get_build):
@@ -993,6 +1006,35 @@ class TestQueryEntityFromLightBlue(unittest.TestCase):
             [sorted(images, key=lambda image: image['brew']['build'])
              for images in batches])
 
+    @patch('freshmaker.lightblue.LightBlue.find_images_with_package_from_content_set')
+    @patch('freshmaker.lightblue.LightBlue.find_unpublished_image_for_build')
+    @patch('os.path.exists')
+    def test_images_to_rebuild_cannot_find_unpublished(
+            self, exists, find_unpublished_image_for_build,
+            find_images_with_package_from_content_set):
+        exists.return_value = True
+
+        image_a = ContainerImage.create({
+            'brew': {'package': 'image-a', 'build': 'image-a-v-r1'},
+            'repository': 'repo-1',
+            'commit': 'image-a-commit'
+        })
+
+        find_unpublished_image_for_build.return_value = None
+        find_images_with_package_from_content_set.return_value = [image_a]
+
+        lb = LightBlue(server_url=self.fake_server_url,
+                       cert=self.fake_cert_file,
+                       private_key=self.fake_private_key)
+        batches = lb.find_images_to_rebuild("dummy", "dummy")
+
+        self.assertEqual(len(batches), 1)
+        self.assertEqual(len(batches[0]), 1)
+        self.assertEqual(
+            batches[0][0]["error"],
+            "Cannot find unpublished version of image, "
+            "Lightblue data is probably incomplete")
+
     @patch('freshmaker.lightblue.LightBlue.find_container_repositories')
     @patch('freshmaker.lightblue.LightBlue.find_container_images')
     @patch('os.path.exists')

