koji-wrapper: Run all blocking commands with fresh ccache

If keytab is used for authentication, other commands than runroot can
possibly fail due to the credentials cache being overwritten.

Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2017-05-15 19:36:43 +02:00
parent 68351fa5a8
commit f27f3ce4ba
2 changed files with 27 additions and 11 deletions

View File

@ -113,7 +113,7 @@ class KojiWrapper(object):
return cmd return cmd
@contextlib.contextmanager @contextlib.contextmanager
def get_runroot_env(self): def get_koji_cmd_env(self):
"""Get environment variables for running a koji command. """Get environment variables for running a koji command.
If we are authenticated with a keytab, we need a fresh credentials If we are authenticated with a keytab, we need a fresh credentials
@ -133,7 +133,7 @@ class KojiWrapper(object):
contains the id, it will be captured and returned. contains the id, it will be captured and returned.
""" """
task_id = None task_id = None
with self.get_runroot_env() as env: with self.get_koji_cmd_env() as env:
retcode, output = run(command, can_fail=True, logfile=log_file, show_cmd=True, env=env) retcode, output = run(command, can_fail=True, logfile=log_file, show_cmd=True, env=env)
if "--task-id" in command: if "--task-id" in command:
first_line = output.splitlines()[0] first_line = output.splitlines()[0]
@ -316,7 +316,8 @@ class KojiWrapper(object):
its exit code and parsed task id. This method will block until the its exit code and parsed task id. This method will block until the
command finishes. command finishes.
""" """
retcode, output = run(command, can_fail=True, logfile=log_file) with self.get_koji_cmd_env() as env:
retcode, output = run(command, can_fail=True, logfile=log_file, env=env)
match = re.search(r"Created task: (\d+)", output) match = re.search(r"Created task: (\d+)", output)
if not match: if not match:

View File

@ -463,7 +463,22 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234}) self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234})
self.assertItemsEqual(run.mock_calls, self.assertItemsEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile=None)]) [mock.call('cmd', can_fail=True, logfile=None, env={})])
@mock.patch('pungi.util.temp_dir')
@mock.patch('pungi.wrappers.kojiwrapper.run')
def test_with_keytab(self, run, temp_dir):
temp_dir.return_value.__enter__.return_value = '/tmp/foo'
self.koji.koji_module.config.keytab = 'foo'
output = 'Created task: 1234\nHello\n'
run.return_value = (0, output)
result = self.koji.run_blocking_cmd('cmd')
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'})])
@mock.patch('pungi.wrappers.kojiwrapper.run') @mock.patch('pungi.wrappers.kojiwrapper.run')
def test_with_log(self, run): def test_with_log(self, run):
@ -474,7 +489,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234}) self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234})
self.assertItemsEqual(run.mock_calls, self.assertItemsEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile='logfile')]) [mock.call('cmd', can_fail=True, logfile='logfile', env={})])
@mock.patch('pungi.wrappers.kojiwrapper.run') @mock.patch('pungi.wrappers.kojiwrapper.run')
def test_fail_with_task_id(self, run): def test_fail_with_task_id(self, run):
@ -485,7 +500,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': 1234}) self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': 1234})
self.assertItemsEqual(run.mock_calls, self.assertItemsEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile=None)]) [mock.call('cmd', can_fail=True, logfile=None, env={})])
@mock.patch('pungi.wrappers.kojiwrapper.run') @mock.patch('pungi.wrappers.kojiwrapper.run')
def test_fail_without_task_id(self, run): def test_fail_without_task_id(self, run):
@ -496,7 +511,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.koji.run_blocking_cmd('cmd') self.koji.run_blocking_cmd('cmd')
self.assertItemsEqual(run.mock_calls, self.assertItemsEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile=None)]) [mock.call('cmd', can_fail=True, logfile=None, env={})])
self.assertIn('Could not find task ID', str(ctx.exception)) self.assertIn('Could not find task ID', str(ctx.exception))
@mock.patch('pungi.wrappers.kojiwrapper.run') @mock.patch('pungi.wrappers.kojiwrapper.run')
@ -509,7 +524,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.assertDictEqual(result, {'retcode': 0, 'output': retry, 'task_id': 1234}) self.assertDictEqual(result, {'retcode': 0, 'output': retry, 'task_id': 1234})
self.assertEqual(run.mock_calls, self.assertEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile=None), [mock.call('cmd', can_fail=True, logfile=None, env={}),
mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'],
can_fail=True, logfile=None)]) can_fail=True, logfile=None)])
@ -523,7 +538,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.assertDictEqual(result, {'retcode': 1, 'output': retry, 'task_id': 1234}) self.assertDictEqual(result, {'retcode': 1, 'output': retry, 'task_id': 1234})
self.assertEqual(run.mock_calls, self.assertEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile=None), [mock.call('cmd', can_fail=True, logfile=None, env={}),
mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'],
can_fail=True, logfile=None)]) can_fail=True, logfile=None)])
@ -538,7 +553,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.assertDictEqual(result, {'retcode': 0, 'output': retry, 'task_id': 1234}) self.assertDictEqual(result, {'retcode': 0, 'output': retry, 'task_id': 1234})
self.assertEqual(run.mock_calls, self.assertEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile=None), [mock.call('cmd', can_fail=True, logfile=None, env={}),
mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'],
can_fail=True, logfile=None), can_fail=True, logfile=None),
mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'],
@ -559,7 +574,7 @@ class RunBlockingCmdTest(KojiWrapperBaseTestCase):
self.assertIn('Failed to wait', str(ctx.exception)) self.assertIn('Failed to wait', str(ctx.exception))
self.assertEqual(run.mock_calls, self.assertEqual(run.mock_calls,
[mock.call('cmd', can_fail=True, logfile=None), [mock.call('cmd', can_fail=True, logfile=None, env={}),
mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'],
can_fail=True, logfile=None), can_fail=True, logfile=None),
mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'], mock.call(['koji', '--profile=custom-koji', 'watch-task', '1234'],