From e1895bff26a1ae79a5cc55fd93225ecfe0cd143d Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: Feb 12 2016 11:38:45 +0000 Subject: [koji-wrapper] Get task id from failed runroot Even when runroot task fails, it can emit task_id. Signed-off-by: Lubomír Sedlář --- diff --git a/pungi/wrappers/kojiwrapper.py b/pungi/wrappers/kojiwrapper.py index 15b12cb..505f7e1 100644 --- a/pungi/wrappers/kojiwrapper.py +++ b/pungi/wrappers/kojiwrapper.py @@ -78,16 +78,23 @@ class KojiWrapper(object): return cmd def run_runroot_cmd(self, command, log_file=None): - # runroot is blocking -> you probably want to run it in a thread + """ + Run koji runroot command and wait for results. + If the command specified --task-id, and the first line of output + contains the id, it will be captured and returned. + """ task_id = None retcode, output = run(command, can_fail=True, logfile=log_file) - if retcode == 0 and "--task-id" in command: - task_id = int(output.splitlines()[0]) - output_ends_with_eol = output.endswith("\n") - output = "\n".join(output.splitlines()[1:]) - if output_ends_with_eol: - output += "\n" + if "--task-id" in command: + first_line = output.splitlines()[0] + if re.match(r'^\d+$', first_line): + task_id = int(first_line) + # Remove first line from the output, preserving any trailing newlines. + output_ends_with_eol = output.endswith("\n") + output = "\n".join(output.splitlines()[1:]) + if output_ends_with_eol: + output += "\n" return { "retcode": retcode, diff --git a/tests/test_koji_wrapper.py b/tests/test_koji_wrapper.py index df6d867..1ae5ceb 100755 --- a/tests/test_koji_wrapper.py +++ b/tests/test_koji_wrapper.py @@ -382,6 +382,15 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase): result = self.koji.run_runroot_cmd(cmd) self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': None}) + @mock.patch('pungi.wrappers.kojiwrapper.run') + def test_run_runroot_cmd_with_task_id_and_fail_but_emit_id(self, run): + cmd = ['koji', 'runroot', '--task-id'] + output = 'Nope, does not work.\n' + run.return_value = (1, '12345\n' + output) + + result = self.koji.run_runroot_cmd(cmd) + self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': 12345}) + if __name__ == "__main__": unittest.main()