Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
[ART-4755] mangle dockerfile (micro)dnf cmds like yum
Browse files Browse the repository at this point in the history
  • Loading branch information
sosiouxme committed Nov 15, 2022
1 parent 6b4bb19 commit 57721c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
16 changes: 10 additions & 6 deletions doozerlib/distgit.py
Expand Up @@ -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]:]
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
23 changes: 16 additions & 7 deletions tests/test_distgit/test_image_distgit/test_image_distgit.py
Expand Up @@ -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\
Expand All @@ -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'")
Expand Down

0 comments on commit 57721c7

Please sign in to comment.