From ad63b3135d25ce341605ea93b889b65265804e0e Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Mar 15 2018 13:09:33 +0000 Subject: Return just single containerImage per each NVR when finding them in LB. --- diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py index c83d540..2a84083 100644 --- a/freshmaker/lightblue.py +++ b/freshmaker/lightblue.py @@ -27,6 +27,7 @@ import re import requests import six import dogpile.cache +from itertools import groupby from six.moves import http_client import concurrent.futures @@ -835,6 +836,17 @@ class LightBlue(object): return [] images = self.find_images_with_included_srpm(repos, srpm_name, published) + # There can be multi-arch images which share the same + # image['brew']['build']. Freshmaker is not interested in the image + # architecture, it is only interested in NVR, so group the images + # by the same image['brew']['build'] and include just first one in the + # image list. + sorted_images = sorted( + images, key=lambda image: image['brew']['build'], reverse=True) + images = [] + for k, v in groupby(sorted_images, key=lambda x: x['brew']['build']): + images.append(v.next()) + # In case we query for unpublished images, we need to return just # the latest NVR for given name-version, otherwise images would # contain all the versions which ever containing the srpm_name. diff --git a/tests/test_lightblue.py b/tests/test_lightblue.py index 2d168e4..61f2376 100644 --- a/tests/test_lightblue.py +++ b/tests/test_lightblue.py @@ -416,12 +416,12 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): ContainerImage.create(data) for data in self.fake_images_with_parsed_data] - self.fake_koji_builds = [{"task_id": 123456}, {"task_id": 654321}] + self.fake_koji_builds = [{"task_id": 654321}, {"task_id": 123456}] self.fake_koji_task_requests = [ - ["git://pkgs.devel.redhat.com/rpms/repo-1#commit_hash1", - "target1", {"git_branch": "mybranch"}], ["git://pkgs.devel.redhat.com/rpms/repo-2#commit_hash2", - "target2", {"git_branch": "mybranch"}]] + "target2", {"git_branch": "mybranch"}], + ["git://pkgs.devel.redhat.com/rpms/repo-1#commit_hash1", + "target1", {"git_branch": "mybranch"}]] @patch('freshmaker.lightblue.requests.post') def test_find_container_images(self, post): @@ -704,6 +704,9 @@ 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"}})] + # 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 koji_task_request.side_effect = self.fake_koji_task_requests koji_get_build.side_effect = self.fake_koji_builds @@ -717,24 +720,29 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): self.assertEqual(ret, [ { - "repository": "rpms/repo-1", - "commit": "commit_hash1", - "target": "target1", + "repository": "rpms/repo-2", + "commit": "commit_hash2", + "target": "target2", "git_branch": "mybranch", "error": None, "brew": { "completion_date": u"20170421T04:27:51.000-0400", - "build": "package-name-1-4-12.10", - "package": "package-name-1" + "build": "package-name-2-4-12.10", + "package": "package-name-2" }, - 'repositories': [{'repository': 'product1/repo1', 'published': True}], 'content_sets': ['dummy-content-set-1', 'dummy-content-set-2'], + 'repositories': [{'repository': 'product2/repo2', 'published': True}], 'parsed_data': { 'files': [ { 'key': 'buildfile', - 'content_url': 'http://git.repo.com/cgit/rpms/repo-1/plain/Dockerfile?id=commit_hash1', - 'filename': u'Dockerfile' + 'content_url': 'http://git.repo.com/cgit/rpms/repo-2/plain/Dockerfile?id=commit_hash2', + 'filename': 'Dockerfile' + }, + { + 'key': 'bogusfile', + 'content_url': 'bogus_test_url', + 'filename': 'bogus.file' } ] }, @@ -742,39 +750,34 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): 'rpms': [ { "srpm_name": "openssl", - "srpm_nevra": "openssl-0:1.2.3-1.src" + "srpm_nevra": "openssl-1:1.2.3-1.src" }, { - "srpm_name": "tespackage", - "srpm_nevra": "testpackage-10:1.2.3-1.src" + "srpm_name": "tespackage2", + "srpm_nevra": "testpackage2-10:1.2.3-1.src" } ] }] }, { - "repository": "rpms/repo-2", - "commit": "commit_hash2", - "target": "target2", + "repository": "rpms/repo-1", + "commit": "commit_hash1", + "target": "target1", "git_branch": "mybranch", "error": None, "brew": { "completion_date": u"20170421T04:27:51.000-0400", - "build": "package-name-2-4-12.10", - "package": "package-name-2" + "build": "package-name-1-4-12.10", + "package": "package-name-1" }, + 'repositories': [{'repository': 'product1/repo1', 'published': True}], 'content_sets': ['dummy-content-set-1', 'dummy-content-set-2'], - 'repositories': [{'repository': 'product2/repo2', 'published': True}], 'parsed_data': { 'files': [ { 'key': 'buildfile', - 'content_url': 'http://git.repo.com/cgit/rpms/repo-2/plain/Dockerfile?id=commit_hash2', - 'filename': 'Dockerfile' - }, - { - 'key': 'bogusfile', - 'content_url': 'bogus_test_url', - 'filename': 'bogus.file' + 'content_url': 'http://git.repo.com/cgit/rpms/repo-1/plain/Dockerfile?id=commit_hash1', + 'filename': u'Dockerfile' } ] }, @@ -782,15 +785,15 @@ class TestQueryEntityFromLightBlue(helpers.FreshmakerTestCase): 'rpms': [ { "srpm_name": "openssl", - "srpm_nevra": "openssl-1:1.2.3-1.src" + "srpm_nevra": "openssl-0:1.2.3-1.src" }, { - "srpm_name": "tespackage2", - "srpm_nevra": "testpackage2-10:1.2.3-1.src" + "srpm_name": "tespackage", + "srpm_nevra": "testpackage-10:1.2.3-1.src" } ] }] - } + }, ]) @patch('freshmaker.lightblue.LightBlue.find_content_sets_for_repository')