From 69e2b60e201104b07b23a37ab08bb6a51f823463 Mon Sep 17 00:00:00 2001 From: Giulia Naponiello Date: Apr 22 2020 12:56:50 +0000 Subject: Bug fix: affected packages not correctly detected When the affected packages are modular RPMs, sfm2 uses a different format: `module_name:module_stream:affected_package_name` while normally it is simply `affected_package_name`. With this patch we enhance Freshmaker to compare differently this when it is for a modular RPM. ref: CLOUDWF-1038 Signed-off-by: Giulia Naponiello --- diff --git a/freshmaker/handlers/koji/rebuild_images_on_rpm_advisory_change.py b/freshmaker/handlers/koji/rebuild_images_on_rpm_advisory_change.py index a71c657..53440e6 100644 --- a/freshmaker/handlers/koji/rebuild_images_on_rpm_advisory_change.py +++ b/freshmaker/handlers/koji/rebuild_images_on_rpm_advisory_change.py @@ -36,6 +36,7 @@ from freshmaker.errata import Errata from freshmaker.types import ( ArtifactType, ArtifactBuildState, EventState, RebuildReason) from freshmaker.models import Event, Compose +from freshmaker.utils import is_pkg_modular class RebuildImagesOnRPMAdvisoryChange(ContainerBuildHandler): @@ -424,8 +425,19 @@ class RebuildImagesOnRPMAdvisoryChange(ContainerBuildHandler): # change was made, and rebuild everything. affected_pkgs = set([pkg['pkg_name'] for pkg in self.event.advisory.affected_pkgs]) if affected_pkgs: - tmp_srpm_nvrs = srpm_nvrs - srpm_nvrs = set([srpm_nvr for srpm_nvr in srpm_nvrs if kobo.rpmlib.parse_nvr(srpm_nvr)["name"] in affected_pkgs]) + tmp_srpm_nvrs = set(srpm_nvrs) + srpm_nvrs = set() + for srpm_nvr in tmp_srpm_nvrs: + srpm_name = kobo.rpmlib.parse_nvr(srpm_nvr)["name"] + # In case the SRPM NVR is modular, the `affected_pkgs` might contain + # modules which are in the "module_name:module_stream/pkg_name" format. + # We need to respect this format and only try to match the package name, it + # means only the "/pkg_name" part of affected_pkg. + if is_pkg_modular(srpm_nvr): + if any(affected_pkg.endswith(f"/{srpm_name}") for affected_pkg in affected_pkgs): + srpm_nvrs.add(srpm_nvr) + elif srpm_name in affected_pkgs: + srpm_nvrs.add(srpm_nvr) self.log_info(("Not going to rebuild container images with RPMS from these SRPMs " "because they're not affected: %r"), tmp_srpm_nvrs.difference(srpm_nvrs)) diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py index 3e78ed5..ec952eb 100644 --- a/freshmaker/lightblue.py +++ b/freshmaker/lightblue.py @@ -36,7 +36,7 @@ from itertools import groupby from freshmaker import log, conf from freshmaker.kojiservice import koji_service -from freshmaker.utils import sorted_by_nvr +from freshmaker.utils import sorted_by_nvr, is_pkg_modular import koji @@ -749,8 +749,8 @@ class LightBlue(object): # name in the image is also modular. Also, include the image if the opposite is true. for rpm in rpms or []: for srpm_nvr in srpm_name_to_nvrs.get(rpm.get("srpm_name"), []): - if (("module+" in srpm_nvr and "module+" in rpm["srpm_nevra"]) or - ("module+" not in srpm_nvr and "module+" not in rpm["srpm_nevra"])): + if ((is_pkg_modular(srpm_nvr) and is_pkg_modular(rpm["srpm_nevra"])) or + (not is_pkg_modular(srpm_nvr) and not is_pkg_modular(rpm["srpm_nevra"]))): ret.append(image) image_included = True break diff --git a/freshmaker/utils.py b/freshmaker/utils.py index 0c524bc..b4b581a 100644 --- a/freshmaker/utils.py +++ b/freshmaker/utils.py @@ -184,3 +184,8 @@ def _run_command(command, logger=None, rundir=None, output=subprocess.PIPE, erro raise OSError("Got an error (%d) from %s: %s" % (p1.returncode, command[0], err)) return out + + +def is_pkg_modular(nvr): + """ Returns True if the package is modular, False otherwise. """ + return "module+" in nvr diff --git a/tests/handlers/koji/test_rebuild_images_on_rpm_advisory_change.py b/tests/handlers/koji/test_rebuild_images_on_rpm_advisory_change.py index 7ab521c..9dd54fa 100644 --- a/tests/handlers/koji/test_rebuild_images_on_rpm_advisory_change.py +++ b/tests/handlers/koji/test_rebuild_images_on_rpm_advisory_change.py @@ -553,6 +553,23 @@ class TestFindImagesToRebuild(helpers.FreshmakerTestCase): published=True, release_categories=conf.lightblue_release_categories, leaf_container_images=None) + @patch.object(freshmaker.conf, 'handler_build_whitelist', new={ + 'RebuildImagesOnRPMAdvisoryChange': { + 'image': {'advisory_name': 'RHBA-*'} + } + }) + @patch('os.path.exists', return_value=True) + def test_affected_packages_with_modules(self, exists): + self.event.advisory.affected_pkgs = [{'product': 'rhel-8', 'pkg_name': 'nodejs:10/nodejs'}] + self.get_builds.return_value = ["nodejs-10.19.0-1.module+el8.1.0+5726+6ed65f8c.x86_64"] + self.handler._find_images_to_rebuild(123456) + + self.find_images_to_rebuild.assert_called_once_with( + set(['nodejs-10.19.0-1.module+el8.1.0+5726+6ed65f8c.x86_64']), ['content-set-1'], + filter_fnc=self.handler._filter_out_not_allowed_builds, + published=True, release_categories=conf.lightblue_release_categories, + leaf_container_images=None) + class TestAllowBuild(helpers.ModelsTestCase): """Test RebuildImagesOnRPMAdvisoryChange.allow_build"""