From 1db1abbb82086b9d2765c5aac8da5e1accec60e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Mon, 5 Jun 2017 12:46:45 +0200 Subject: [PATCH] koji-wrapper: Stop mangling env variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When koji is authenticated with a keytab, by setting the private directory we erased rest of existing environment. In non-keytab path, the environment variables got removed as well. This patch makes sure that the environment will not be modified more than necessary (by setting KRB5CCNAME if needed). Signed-off-by: Lubomír Sedlář --- pungi/wrappers/kojiwrapper.py | 6 ++++-- tests/test_koji_wrapper.py | 30 ++++++++++++++++-------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pungi/wrappers/kojiwrapper.py b/pungi/wrappers/kojiwrapper.py index fc8aba6f..4596b666 100644 --- a/pungi/wrappers/kojiwrapper.py +++ b/pungi/wrappers/kojiwrapper.py @@ -121,9 +121,11 @@ class KojiWrapper(object): """ if getattr(self.koji_module.config, 'keytab', None): with util.temp_dir(prefix='krb_ccache') as tempdir: - yield {'KRB5CCNAME': 'DIR:%s' % tempdir} + env = os.environ.copy() + env['KRB5CCNAME'] = 'DIR:%s' % tempdir + yield env else: - yield {} + yield None def run_runroot_cmd(self, command, log_file=None): """ diff --git a/tests/test_koji_wrapper.py b/tests/test_koji_wrapper.py index bd89df3c..e5bbf62b 100644 --- a/tests/test_koji_wrapper.py +++ b/tests/test_koji_wrapper.py @@ -391,7 +391,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': None}) self.assertEqual( run.call_args_list, - [mock.call(cmd, can_fail=True, env={}, logfile=None, show_cmd=True)] + [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True)] ) @mock.patch('pungi.wrappers.kojiwrapper.run') @@ -404,7 +404,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234}) self.assertEqual( run.call_args_list, - [mock.call(cmd, can_fail=True, env={}, logfile=None, show_cmd=True)] + [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True)] ) @mock.patch('pungi.wrappers.kojiwrapper.run') @@ -417,7 +417,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': None}) self.assertEqual( run.call_args_list, - [mock.call(cmd, can_fail=True, env={}, logfile=None, show_cmd=True)] + [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True)] ) @mock.patch('pungi.wrappers.kojiwrapper.run') @@ -430,9 +430,10 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': 12345}) self.assertEqual( run.call_args_list, - [mock.call(cmd, can_fail=True, env={}, logfile=None, show_cmd=True)] + [mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True)] ) + @mock.patch.dict('os.environ', {'FOO': 'BAR'}, clear=True) @mock.patch('shutil.rmtree') @mock.patch('tempfile.mkdtemp') @mock.patch('pungi.wrappers.kojiwrapper.run') @@ -448,7 +449,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': None}) self.assertEqual( run.call_args_list, - [mock.call(cmd, can_fail=True, env={'KRB5CCNAME': 'DIR:/tmp/foo'}, + [mock.call(cmd, can_fail=True, env={'KRB5CCNAME': 'DIR:/tmp/foo', 'FOO': 'BAR'}, logfile=None, show_cmd=True)] ) @@ -463,8 +464,9 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234}) self.assertItemsEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile=None, env={})]) + [mock.call('cmd', can_fail=True, logfile=None, env=None)]) + @mock.patch.dict('os.environ', {'FOO': 'BAR'}, clear=True) @mock.patch('pungi.util.temp_dir') @mock.patch('pungi.wrappers.kojiwrapper.run') def test_with_keytab(self, run, temp_dir): @@ -478,7 +480,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234}) self.assertItemsEqual(run.mock_calls, [mock.call('cmd', can_fail=True, logfile=None, - env={'KRB5CCNAME': 'DIR:/tmp/foo'})]) + env={'KRB5CCNAME': 'DIR:/tmp/foo', 'FOO': 'BAR'})]) @mock.patch('pungi.wrappers.kojiwrapper.run') def test_with_log(self, run): @@ -489,7 +491,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234}) self.assertItemsEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile='logfile', env={})]) + [mock.call('cmd', can_fail=True, logfile='logfile', env=None)]) @mock.patch('pungi.wrappers.kojiwrapper.run') def test_fail_with_task_id(self, run): @@ -500,7 +502,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': 1234}) self.assertItemsEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile=None, env={})]) + [mock.call('cmd', can_fail=True, logfile=None, env=None)]) @mock.patch('pungi.wrappers.kojiwrapper.run') def test_fail_without_task_id(self, run): @@ -511,7 +513,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.koji.run_blocking_cmd('cmd') self.assertItemsEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile=None, env={})]) + [mock.call('cmd', can_fail=True, logfile=None, env=None)]) self.assertIn('Could not find task ID', str(ctx.exception)) @mock.patch('pungi.wrappers.kojiwrapper.run') @@ -524,7 +526,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': retry, 'task_id': 1234}) self.assertEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile=None, env={}), + [mock.call('cmd', can_fail=True, logfile=None, env=None), mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], can_fail=True, logfile=None)]) @@ -538,7 +540,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 1, 'output': retry, 'task_id': 1234}) self.assertEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile=None, env={}), + [mock.call('cmd', can_fail=True, logfile=None, env=None), mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], can_fail=True, logfile=None)]) @@ -553,7 +555,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertDictEqual(result, {'retcode': 0, 'output': retry, 'task_id': 1234}) self.assertEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile=None, env={}), + [mock.call('cmd', can_fail=True, logfile=None, env=None), mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], can_fail=True, logfile=None), mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], @@ -574,7 +576,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase): self.assertIn('Failed to wait', str(ctx.exception)) self.assertEqual(run.mock_calls, - [mock.call('cmd', can_fail=True, logfile=None, env={}), + [mock.call('cmd', can_fail=True, logfile=None, env=None), mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], can_fail=True, logfile=None), mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'],