From 6c9b942b6f73e4253141e9ff3987b2f522f97ce9 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Oct 27 2017 13:01:06 +0000 Subject: [PATCH 1/4] unify runroot CLI interface Related: https://pagure.io/koji/issue/564 --- diff --git a/plugins/cli/runroot.py b/plugins/cli/runroot.py index 04c02fb..d9dc146 100644 --- a/plugins/cli/runroot.py +++ b/plugins/cli/runroot.py @@ -3,7 +3,8 @@ import time import koji from koji.plugin import export_cli -from koji_cli.lib import _, activate_session, OptionParser, list_task_output_all_volumes +from koji_cli.lib import _, activate_session, OptionParser, watch_tasks, \ + _running_in_bg, list_task_output_all_volumes @export_cli def handle_runroot(options, session, args): @@ -25,11 +26,17 @@ def handle_runroot(options, session, args): parser.add_option("--new-chroot", action="store_true", default=False, help=_("Run command with the --new-chroot (systemd-nspawn) option to mock")) parser.add_option("--repo-id", type="int", help=_("ID of the repo to use")) + parser.add_option("--nowait", action="store_false", dest="wait", help=_("Do not wait on task")) + parser.add_option("--watch", action="store_true", help=_("Watch task instead of printing runroot.log")) + parser.add_option("--quiet", action="store_true", default=options.quiet, + help=_("Do not print the task information")) (opts, args) = parser.parse_args(args) if len(args) < 3: parser.error(_("Incorrect number of arguments")) + return + activate_session(session, options) tag = args[0] arch = args[1] @@ -59,6 +66,14 @@ def handle_runroot(options, session, args): if opts.task_id: print(task_id) + if not opts.wait: + return + + if opts.watch: + session.logout() + return watch_tasks(session, [task_id], quiet=opts.quiet, + poll_interval=options.poll_interval) + try: while True: # wait for the task to finish @@ -81,4 +96,3 @@ def handle_runroot(options, session, args): state = koji.TASK_STATES[info['state']] if state in ('FAILED', 'CANCELED'): sys.exit(1) - return diff --git a/tests/test_plugins/test_runroot_cli.py b/tests/test_plugins/test_runroot_cli.py index 905e5dc..fcb6f21 100644 --- a/tests/test_plugins/test_runroot_cli.py +++ b/tests/test_plugins/test_runroot_cli.py @@ -29,6 +29,7 @@ class TestListCommands(unittest.TestCase): arguments = [tag, arch, command] options = mock.MagicMock() options.new_chroot = False + options.watch = False self.parser.parse_args.return_value = [options, arguments] # Mock out the xmlrpc server @@ -54,3 +55,62 @@ class TestListCommands(unittest.TestCase): mounts=mock.ANY, packages=mock.ANY, skip_setarch=mock.ANY, channel=mock.ANY, ) + + def test_handle_runroot_watch(self): + arguments = ['tag', 'arch', 'command'] + options = mock.MagicMock() + options.new_chroot = True + options.watch = True + options.use_shell = False + self.parser.parse_args.return_value = [options, arguments] + runroot.watch_tasks = mock.MagicMock(name='watch_tasks') + + # Mock out the xmlrpc server + self.session.runroot.return_value = 1 + + # Run it and check immediate output + runroot.handle_runroot(self.options, self.session, self.args) + + # Finally, assert that things were called as we expected. + runroot.watch_tasks.assert_called_once() + self.session.getTaskInfo.assert_not_called() + self.session.listTaskOutput.assert_not_called() + self.session.downloadTaskOutput.assert_not_called() + self.session.runroot.assert_called_once() + + def test_invalid_arguments(self): + arguments = ['tag', 'arch'] # just two + options = mock.MagicMock() + options.new_chroot = False + options.watch = True + options.use_shell = False + self.parser.parse_args.return_value = [options, arguments] + + # Run it and check immediate output + runroot.handle_runroot(self.options, self.session, self.args) + + # Finally, assert that things were called as we expected. + self.session.getTaskInfo.assert_not_called() + self.session.listTaskOutput.assert_not_called() + self.session.downloadTaskOutput.assert_not_called() + self.session.runroot.assert_not_called() + + def test_nowait(self): + arguments = ['tag', 'arch', 'command'] + options = mock.MagicMock() + options.wait = False + self.parser.parse_args.return_value = [options, arguments] + runroot.watch_tasks = mock.MagicMock(name='watch_tasks') + + # Mock out the xmlrpc server + self.session.runroot.return_value = 1 + + # Run it and check immediate output + runroot.handle_runroot(self.options, self.session, self.args) + + # Finally, assert that things were called as we expected. + runroot.watch_tasks.assert_not_called() + self.session.getTaskInfo.assert_not_called() + self.session.listTaskOutput.assert_not_called() + self.session.downloadTaskOutput.assert_not_called() + self.session.runroot.assert_called_once() From 88f83b4c983581da0ad34342037a3521df500b4a Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Oct 27 2017 13:01:06 +0000 Subject: [PATCH 2/4] correct default for --wait --- diff --git a/plugins/cli/runroot.py b/plugins/cli/runroot.py index d9dc146..bb6cf77 100644 --- a/plugins/cli/runroot.py +++ b/plugins/cli/runroot.py @@ -26,7 +26,8 @@ def handle_runroot(options, session, args): parser.add_option("--new-chroot", action="store_true", default=False, help=_("Run command with the --new-chroot (systemd-nspawn) option to mock")) parser.add_option("--repo-id", type="int", help=_("ID of the repo to use")) - parser.add_option("--nowait", action="store_false", dest="wait", help=_("Do not wait on task")) + parser.add_option("--nowait", action="store_false", dest="wait", + default=True, help=_("Do not wait on task")) parser.add_option("--watch", action="store_true", help=_("Watch task instead of printing runroot.log")) parser.add_option("--quiet", action="store_true", default=options.quiet, help=_("Do not print the task information")) From a43f9bc8aaa7759d30ec3116346eef4e2a2a24f5 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Oct 27 2017 13:01:06 +0000 Subject: [PATCH 3/4] clean up runroot unit test --- diff --git a/plugins/cli/runroot.py b/plugins/cli/runroot.py index bb6cf77..6ba802f 100644 --- a/plugins/cli/runroot.py +++ b/plugins/cli/runroot.py @@ -36,7 +36,7 @@ def handle_runroot(options, session, args): if len(args) < 3: parser.error(_("Incorrect number of arguments")) - return + assert False # pragma: no cover activate_session(session, options) tag = args[0] diff --git a/tests/test_plugins/test_runroot_cli.py b/tests/test_plugins/test_runroot_cli.py index fcb6f21..7005e63 100644 --- a/tests/test_plugins/test_runroot_cli.py +++ b/tests/test_plugins/test_runroot_cli.py @@ -8,30 +8,39 @@ from . import load_plugin runroot = load_plugin.load_plugin('cli', 'runroot') + +class ParserError(Exception): + pass + + class TestListCommands(unittest.TestCase): def setUp(self): self.options = mock.MagicMock() + self.options.debug = False self.session = mock.MagicMock() self.session.getAPIVersion.return_value = koji.API_VERSION - self.args = mock.MagicMock() - runroot.OptionParser = mock.MagicMock() - self.parser = runroot.OptionParser.return_value + self.args = ['TAG', 'ARCH', 'COMMAND'] + self.old_OptionParser = runroot.OptionParser + runroot.OptionParser = mock.MagicMock(side_effect=self.get_parser) + self.old_watch_tasks = runroot.watch_tasks + runroot.watch_tasks = mock.MagicMock(name='watch_tasks') + + def tearDown(self): + runroot.OptionParser = self.old_OptionParser + runroot.watch_tasks = self.old_watch_tasks + + def get_parser(self, *a, **kw): + # we don't want parser.error to exit + parser = self.old_OptionParser(*a, **kw) + parser.error = mock.MagicMock(side_effect=ParserError) + return parser # Show long diffs in error output... maxDiff = None @mock.patch('sys.stdout', new_callable=six.StringIO) def test_handle_runroot(self, stdout): - tag = 'tag' - arch = 'arch' - command = 'command' - arguments = [tag, arch, command] - options = mock.MagicMock() - options.new_chroot = False - options.watch = False - self.parser.parse_args.return_value = [options, arguments] - # Mock out the xmlrpc server self.session.getTaskInfo.return_value = {'state': 1} self.session.downloadTaskOutput.return_value = 'task output' @@ -42,7 +51,7 @@ class TestListCommands(unittest.TestCase): runroot.handle_runroot(self.options, self.session, self.args) actual = stdout.getvalue() actual = actual.replace('nosetests', 'koji') - expected = 'successfully connected to hub\n1\ntask output' + expected = 'task output' self.assertMultiLineEqual(actual, expected) # Finally, assert that things were called as we expected. @@ -51,25 +60,19 @@ class TestListCommands(unittest.TestCase): self.session.downloadTaskOutput.assert_called_once_with( 1, 'runroot.log', volume='DEFAULT') self.session.runroot.assert_called_once_with( - tag, arch, command, repo_id=mock.ANY, weight=mock.ANY, + 'TAG', 'ARCH', ['COMMAND'], repo_id=mock.ANY, weight=mock.ANY, mounts=mock.ANY, packages=mock.ANY, skip_setarch=mock.ANY, channel=mock.ANY, ) def test_handle_runroot_watch(self): - arguments = ['tag', 'arch', 'command'] - options = mock.MagicMock() - options.new_chroot = True - options.watch = True - options.use_shell = False - self.parser.parse_args.return_value = [options, arguments] - runroot.watch_tasks = mock.MagicMock(name='watch_tasks') + args = ['--watch', 'TAG', 'ARCH', 'COMMAND'] # Mock out the xmlrpc server self.session.runroot.return_value = 1 # Run it and check immediate output - runroot.handle_runroot(self.options, self.session, self.args) + runroot.handle_runroot(self.options, self.session, args) # Finally, assert that things were called as we expected. runroot.watch_tasks.assert_called_once() @@ -79,15 +82,11 @@ class TestListCommands(unittest.TestCase): self.session.runroot.assert_called_once() def test_invalid_arguments(self): - arguments = ['tag', 'arch'] # just two - options = mock.MagicMock() - options.new_chroot = False - options.watch = True - options.use_shell = False - self.parser.parse_args.return_value = [options, arguments] + args = ['TAG', 'COMMAND'] # no arch # Run it and check immediate output - runroot.handle_runroot(self.options, self.session, self.args) + with self.assertRaises(ParserError): + runroot.handle_runroot(self.options, self.session, args) # Finally, assert that things were called as we expected. self.session.getTaskInfo.assert_not_called() @@ -96,17 +95,13 @@ class TestListCommands(unittest.TestCase): self.session.runroot.assert_not_called() def test_nowait(self): - arguments = ['tag', 'arch', 'command'] - options = mock.MagicMock() - options.wait = False - self.parser.parse_args.return_value = [options, arguments] - runroot.watch_tasks = mock.MagicMock(name='watch_tasks') + args = ['--nowait', 'TAG', 'ARCH', 'COMMAND'] # Mock out the xmlrpc server self.session.runroot.return_value = 1 # Run it and check immediate output - runroot.handle_runroot(self.options, self.session, self.args) + runroot.handle_runroot(self.options, self.session, args) # Finally, assert that things were called as we expected. runroot.watch_tasks.assert_not_called() From 1eb9e6c2168019319b5f5dcd5ce9c84cfc1d23e7 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Oct 27 2017 13:01:06 +0000 Subject: [PATCH 4/4] remove unused import --- diff --git a/plugins/cli/runroot.py b/plugins/cli/runroot.py index 6ba802f..f8d4b50 100644 --- a/plugins/cli/runroot.py +++ b/plugins/cli/runroot.py @@ -4,7 +4,7 @@ import time import koji from koji.plugin import export_cli from koji_cli.lib import _, activate_session, OptionParser, watch_tasks, \ - _running_in_bg, list_task_output_all_volumes + list_task_output_all_volumes @export_cli def handle_runroot(options, session, args):