From aec57dc72b5b105e5a3dd378026d4587c8be315e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Wed, 10 Feb 2016 09:06:15 +0100 Subject: [PATCH] [koji-wrapper] Only parse output on success MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If Koji fails runroot task for some reason, the output will most likely not have the required format and will crash Pungi. Pagure: #140 Signed-off-by: Lubomír Sedlář --- pungi/wrappers/kojiwrapper.py | 2 +- tests/test_koji_wrapper.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pungi/wrappers/kojiwrapper.py b/pungi/wrappers/kojiwrapper.py index 1400f73b..4a4848dd 100644 --- a/pungi/wrappers/kojiwrapper.py +++ b/pungi/wrappers/kojiwrapper.py @@ -82,7 +82,7 @@ class KojiWrapper(object): task_id = None retcode, output = run(command, can_fail=True, logfile=log_file) - if "--task-id" in command: + 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:]) diff --git a/tests/test_koji_wrapper.py b/tests/test_koji_wrapper.py index bb9a29bd..106c05f9 100755 --- a/tests/test_koji_wrapper.py +++ b/tests/test_koji_wrapper.py @@ -345,8 +345,7 @@ class RunrootKojiWrapperTest(unittest.TestCase): run.return_value = (0, output) result = self.koji.run_runroot_cmd(cmd) - self.assertDictEqual(result, - {'retcode': 0, 'output': output, 'task_id': None}) + self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': None}) @mock.patch('pungi.wrappers.kojiwrapper.run') def test_run_runroot_cmd_with_task_id(self, run): @@ -355,8 +354,16 @@ class RunrootKojiWrapperTest(unittest.TestCase): run.return_value = (0, '1234\n' + output) result = self.koji.run_runroot_cmd(cmd) - self.assertDictEqual(result, - {'retcode': 0, 'output': output, 'task_id': 1234}) + self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234}) + + @mock.patch('pungi.wrappers.kojiwrapper.run') + def test_run_runroot_cmd_with_task_id_and_fail(self, run): + cmd = ['koji', 'runroot', '--task-id'] + output = 'You are not authorized to run this\n' + run.return_value = (1, output) + + result = self.koji.run_runroot_cmd(cmd) + self.assertDictEqual(result, {'retcode': 1, 'output': output, 'task_id': None}) if __name__ == "__main__":