Wait on runroot tasks with wait-task command

JIRA: COMPOSE-4027
Signed-off-by: Haibo Lin <hlin@redhat.com>
This commit is contained in:
Haibo Lin 2020-01-17 17:06:13 +08:00
parent 52f82ccc6e
commit bce57c2e66
2 changed files with 33 additions and 51 deletions

View File

@ -73,7 +73,7 @@ class KojiWrapper(object):
def get_runroot_cmd(self, target, arch, command, quiet=False, use_shell=True,
channel=None, packages=None, mounts=None, weight=None,
task_id=True, new_chroot=False, chown_paths=None):
cmd = self._get_cmd("runroot")
cmd = self._get_cmd("runroot", "--nowait")
if quiet:
cmd.append("--quiet")
@ -152,15 +152,18 @@ class KojiWrapper(object):
with self.get_koji_cmd_env() as env:
retcode, output = run(command, can_fail=True, logfile=log_file,
show_cmd=True, env=env, universal_newlines=True)
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"
first_line = output.splitlines()[0]
match = re.search(r'^(\d+)$', first_line)
if not match:
raise RuntimeError(
"Could not find task ID in output. Command '%s' returned '%s'."
% (" ".join(command), output)
)
task_id = int(match.groups()[0])
retcode, output = self._wait_for_task(task_id, logfile=log_file)
return {
"retcode": retcode,

View File

@ -425,26 +425,26 @@ class LiveImageKojiWrapperTest(KojiWrapperBaseTestCase):
class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
def test_get_cmd_minimal(self):
cmd = self.koji.get_runroot_cmd('tgt', 's390x', 'date', use_shell=False, task_id=False)
self.assertEqual(len(cmd), 7)
self.assertEqual(len(cmd), 8)
self.assertEqual(cmd[:3], ['koji', '--profile=custom-koji', 'runroot'])
self.assertEqual(cmd[-3], 'tgt')
self.assertEqual(cmd[-2], 's390x')
self.assertEqual(cmd[-1], 'rm -f /var/lib/rpm/__db*; rm -rf /var/cache/yum/*; set -x; date')
six.assertCountEqual(self, cmd[3:-3], ["--channel-override=runroot-local"])
six.assertCountEqual(self, cmd[4:-3], ["--channel-override=runroot-local"])
def test_get_cmd_full(self):
cmd = self.koji.get_runroot_cmd('tgt', 's390x', ['/bin/echo', '&'],
quiet=True, channel='chan',
packages=['lorax', 'some_other_package'],
mounts=['/tmp'], weight=1000)
self.assertEqual(len(cmd), 14)
self.assertEqual(len(cmd), 15)
self.assertEqual(cmd[:3], ['koji', '--profile=custom-koji', 'runroot'])
self.assertEqual(cmd[-3], 'tgt')
self.assertEqual(cmd[-2], 's390x')
self.assertEqual(cmd[-1], 'rm -f /var/lib/rpm/__db*; rm -rf /var/cache/yum/*; set -x; /bin/echo \'&\'')
six.assertCountEqual(
self,
cmd[3:-3],
cmd[4:-3],
["--channel-override=chan", "--quiet", "--use-shell",
"--task-id", "--weight=1000", "--package=some_other_package",
"--package=lorax", "--mount=/tmp"],
@ -456,7 +456,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
quiet=True, channel='chan',
packages=['lorax', 'some_other_package'],
mounts=['/tmp'], weight=1000, chown_paths=["/output dir", "/foo"])
self.assertEqual(len(cmd), 14)
self.assertEqual(len(cmd), 15)
self.assertEqual(cmd[:3], ['koji', '--profile=custom-koji', 'runroot'])
self.assertEqual(cmd[-3], 'tgt')
self.assertEqual(cmd[-2], 's390x')
@ -466,7 +466,7 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
)
six.assertCountEqual(
self,
cmd[3:-3],
cmd[4:-3],
["--channel-override=chan", "--quiet", "--use-shell",
"--task-id", "--weight=1000", "--package=some_other_package",
"--package=lorax", "--mount=/tmp"],
@ -476,21 +476,27 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
def test_run_runroot_cmd_no_task_id(self, run):
cmd = ['koji', 'runroot']
output = 'Output ...'
run.return_value = (0, output)
run.return_value = (1, output)
with self.assertRaises(RuntimeError) as ctx:
self.koji.run_runroot_cmd(cmd)
result = self.koji.run_runroot_cmd(cmd)
self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': None})
self.assertEqual(
run.call_args_list,
[mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True,
universal_newlines=True)]
)
self.assertEqual(
"Could not find task ID in output. Command '%s' returned '%s'." % (" ".join(cmd), output),
str(ctx.exception)
)
@mock.patch('pungi.wrappers.kojiwrapper.run')
def test_run_runroot_cmd_with_task_id(self, run):
cmd = ['koji', 'runroot', '--task-id']
output = 'Output ...\n'
run.return_value = (0, '1234\n' + output)
run.return_value = (0, '1234\n')
output = "Output ..."
self.koji._wait_for_task = mock.Mock(return_value=(0, output))
result = self.koji.run_runroot_cmd(cmd)
self.assertDictEqual(result, {'retcode': 0, 'output': output, 'task_id': 1234})
@ -500,34 +506,6 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
universal_newlines=True)]
)
@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})
self.assertEqual(
run.call_args_list,
[mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True,
universal_newlines=True)]
)
@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})
self.assertEqual(
run.call_args_list,
[mock.call(cmd, can_fail=True, env=None, logfile=None, show_cmd=True,
universal_newlines=True)]
)
@mock.patch.dict('os.environ', {'FOO': 'BAR'}, clear=True)
@mock.patch('shutil.rmtree')
@mock.patch('tempfile.mkdtemp')
@ -538,10 +516,11 @@ class RunrootKojiWrapperTest(KojiWrapperBaseTestCase):
self.koji.koji_module.config.keytab = 'foo'
cmd = ['koji', 'runroot']
output = 'Output ...'
run.return_value = (0, output)
run.return_value = (0, '1234\n')
self.koji._wait_for_task = mock.Mock(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': 1234})
self.assertEqual(
run.call_args_list,
[mock.call(cmd, can_fail=True, env={'KRB5CCNAME': 'DIR:/tmp/foo', 'FOO': 'BAR'},