From 57721c72b3ddd08e6493323fcce065f55327fd69 Mon Sep 17 00:00:00 2001 From: Luke Meyer Date: Fri, 11 Nov 2022 17:41:33 -0500 Subject: [PATCH] [ART-4755] mangle dockerfile (micro)dnf cmds like yum --- doozerlib/distgit.py | 16 ++++++++----- .../test_image_distgit/test_image_distgit.py | 23 +++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/doozerlib/distgit.py b/doozerlib/distgit.py index 17ed787b1..bd0d0a067 100644 --- a/doozerlib/distgit.py +++ b/doozerlib/distgit.py @@ -1463,7 +1463,7 @@ def _detect_permanent_build_failures(self, scanner): self.logger.warning("Exception while trying to analyze build logs in {}: {}".format(logs_dir, e)) @staticmethod - def _mangle_yum(cmd): + def _mangle_pkgmgr(cmd): # alter the arg by splicing its content def splice(pos, replacement): return cmd[:pos[0]] + replacement + cmd[pos[1]:] @@ -1494,14 +1494,18 @@ def append_nodes_from(node): cmd = splice(subcmd.pos, "\\\n " + subcmd.op) continue # not "changed" logically however - # replace yum-config-manager with a no-op - if re.search(r'(^|/)yum-config-manager$', subcmd.parts[0].word): + # replace package manager config with a no-op + if re.search(r'(^|/)(microdnf\s+|dnf\s+|yum-)config-manager$', subcmd.parts[0].word): cmd = splice(subcmd.pos, ": 'removed yum-config-manager'") changed = True continue + if re.search(r'(^|/)(micro)?dnf$', subcmd.parts[0].word) and len(subcmd.parts) > 1 and subcmd.parts[1].word == "config-manager": + cmd = splice(subcmd.pos, ": 'removed dnf config-manager'") + changed = True + continue - # clear repo options from yum commands - if not re.search(r'(^|/)yum$', subcmd.parts[0].word): + # clear repo options from yum and dnf commands + if not re.search(r'(^|/)(yum|dnf|microdnf)$', subcmd.parts[0].word): continue next_word = None for word in reversed(subcmd.parts): @@ -1532,7 +1536,7 @@ def _clean_repos(self, dfp): """ for entry in reversed(dfp.structure): if entry['instruction'] == 'RUN': - changed, new_value = self._mangle_yum(entry['value']) + changed, new_value = self._mangle_pkgmgr(entry['value']) if changed: dfp.add_lines_at(entry, "RUN " + new_value, replace=True) diff --git a/tests/test_distgit/test_image_distgit/test_image_distgit.py b/tests/test_distgit/test_image_distgit/test_image_distgit.py index 0a55a70da..cb9bddcaa 100644 --- a/tests/test_distgit/test_image_distgit/test_image_distgit.py +++ b/tests/test_distgit/test_image_distgit/test_image_distgit.py @@ -260,24 +260,33 @@ def test_detect_permanent_build_failures_borkage(self): output = self.stream.getvalue() self.assertIn("Exception while trying to analyze build logs", output) - def test_mangle_yum_cmds_unchanged(self): + def test_mangle_pkgmgr_cmds_unchanged(self): unchanging = [ "yum install foo", + "dnf install foo", + "microdnf install foo", "ignore yum-config-manager here", "foo && yum install && bar", + "ignore dnf config-manager here", + "ignore microdnf config-manager here", ] for cmd in unchanging: - self.assertFalse(distgit.ImageDistGitRepo._mangle_yum(cmd)[0]) + self.assertFalse(distgit.ImageDistGitRepo._mangle_pkgmgr(cmd)[0]) - def test_mangle_yum_cmds_changed(self): + def test_mangle_pkgmgr_cmds_changed(self): # note: adjacent spaces are not removed, so removals may result in redundant spaces changes = { "yum-config-manager foo bar baz": ": 'removed yum-config-manager'", + "dnf config-manager foo bar baz": ": 'removed dnf config-manager'", "yum --enablerepo=bar install foo --disablerepo baz": "yum install foo ", + "microdnf --enablerepo=bar install foo --disablerepo baz": "microdnf install foo ", "yum-config-manager foo bar baz && yum --enablerepo=bar install foo && build stuff": ": 'removed yum-config-manager' \\\n && yum install foo \\\n && build stuff", + "dnf config-manager foo bar baz && microdnf --enablerepo=bar install foo && build stuff": + ": 'removed dnf config-manager' \\\n && microdnf install foo \\\n && build stuff", + """yum repolist && yum-config-manager\ --enable blah\ && yum install 'foo < 42' >& whatever\ @@ -292,13 +301,13 @@ def test_mangle_yum_cmds_changed(self): ( foo \\\n || : 'removed yum-config-manager' \\\n && verify-something )""" } for cmd, expect in changes.items(): - changed, result = distgit.ImageDistGitRepo._mangle_yum(cmd) - self.assertTrue(changed) + changed, result = distgit.ImageDistGitRepo._mangle_pkgmgr(cmd) self.assertEqual(result, expect) + self.assertTrue(changed) - def test_mangle_yum_parse_err(self): + def test_mangle_pkgmgr_parse_err(self): with self.assertRaises(IOError) as e: - distgit.ImageDistGitRepo._mangle_yum("! ( && || $ ) totally broken") + distgit.ImageDistGitRepo._mangle_pkgmgr("! ( && || $ ) totally broken") self.assertIn("totally broken", str(e.exception)) @unittest.skip("raising IOError: [Errno 2] No such file or directory: '/tmp/ocp-cd-test-logsMpNctA/test_file'")