Preserve environment when running koji commands

JIRA: RHELBLD-2479
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2020-08-25 09:07:28 +02:00
parent e628bb91ec
commit a6a96e40db
2 changed files with 16 additions and 15 deletions

View File

@ -249,14 +249,15 @@ class KojiWrapper(object):
If we are authenticated with a keytab, we need a fresh credentials If we are authenticated with a keytab, we need a fresh credentials
cache to avoid possible race condition. cache to avoid possible race condition.
""" """
env = os.environ.copy()
if getattr(self.koji_module.config, "keytab", None): if getattr(self.koji_module.config, "keytab", None):
with util.temp_dir(prefix="krb_ccache") as tempdir: with util.temp_dir(prefix="krb_ccache") as tempdir:
env = os.environ.copy()
env["KRB5CCNAME"] = "DIR:%s" % tempdir env["KRB5CCNAME"] = "DIR:%s" % tempdir
env["PYTHONUNBUFFERED"] = "1" env["PYTHONUNBUFFERED"] = "1"
yield env yield env
else: else:
yield {"PYTHONUNBUFFERED": "1"} env["PYTHONUNBUFFERED"] = "1"
yield env
def run_runroot_cmd(self, command, log_file=None): def run_runroot_cmd(self, command, log_file=None):
"""Run koji runroot command and wait for results. """Run koji runroot command and wait for results.

View File

@ -525,6 +525,7 @@ class LiveImageKojiWrapperTest(KojiWrapperBaseTestCase):
) )
@mock.patch.dict("os.environ", {"FOO": "BAR"}, clear=True)
class RunrootKojiWrapperTest(KojiWrapperBaseTestCase): class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
def test_get_cmd_minimal(self): def test_get_cmd_minimal(self):
cmd = self.koji.get_runroot_cmd("tgt", "s390x", "date", use_shell=False) cmd = self.koji.get_runroot_cmd("tgt", "s390x", "date", use_shell=False)
@ -625,7 +626,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
mock.call( mock.call(
cmd, cmd,
can_fail=True, can_fail=True,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
logfile=None, logfile=None,
show_cmd=True, show_cmd=True,
@ -654,7 +655,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
mock.call( mock.call(
cmd, cmd,
can_fail=True, can_fail=True,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
logfile=None, logfile=None,
show_cmd=True, show_cmd=True,
@ -663,7 +664,6 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
], ],
) )
@mock.patch.dict("os.environ", {"FOO": "BAR"}, clear=True)
@mock.patch("shutil.rmtree") @mock.patch("shutil.rmtree")
@mock.patch("tempfile.mkdtemp") @mock.patch("tempfile.mkdtemp")
@mock.patch("pungi.wrappers.kojiwrapper.run") @mock.patch("pungi.wrappers.kojiwrapper.run")
@ -698,6 +698,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
) )
@mock.patch.dict("os.environ", {"FOO": "BAR"}, clear=True)
class RunBlockingCmdTest(KojiWrapperBaseTestCase): class RunBlockingCmdTest(KojiWrapperBaseTestCase):
@mock.patch("pungi.wrappers.kojiwrapper.run") @mock.patch("pungi.wrappers.kojiwrapper.run")
def test_minimal(self, run): def test_minimal(self, run):
@ -715,14 +716,13 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
) )
], ],
) )
@mock.patch.dict("os.environ", {"FOO": "BAR"}, clear=True)
@mock.patch("pungi.util.temp_dir") @mock.patch("pungi.util.temp_dir")
@mock.patch("pungi.wrappers.kojiwrapper.run") @mock.patch("pungi.wrappers.kojiwrapper.run")
def test_with_keytab(self, run, temp_dir): def test_with_keytab(self, run, temp_dir):
@ -769,7 +769,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile="logfile", logfile="logfile",
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
) )
@ -792,7 +792,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
) )
@ -815,7 +815,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
) )
@ -839,7 +839,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
), ),
@ -868,7 +868,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
), ),
@ -898,7 +898,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
), ),
@ -941,7 +941,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
), ),
@ -977,7 +977,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
"cmd", "cmd",
can_fail=True, can_fail=True,
logfile=None, logfile=None,
env={"PYTHONUNBUFFERED": "1"}, env={"FOO": "BAR", "PYTHONUNBUFFERED": "1"},
buffer_size=-1, buffer_size=-1,
universal_newlines=True, universal_newlines=True,
), ),