koji-wrapper: Stop mangling env variables

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ář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-06-05 12:46:45 +02:00
parent 3ddfd4d172
commit 1db1abbb82
2 changed files with 20 additions and 16 deletions

View File

@ -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):
"""

View File

@ -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'],