From 0b30e73053f8bee292c50b646eea50ddfec380d0 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Apr 13 2019 01:30:02 +0000 Subject: Check CLI arguments for enable/disable host Fixes: https://pagure.io/koji/issue/1364 --- diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py index 866ec6a..c263a75 100644 --- a/cli/koji_cli/commands.py +++ b/cli/koji_cli/commands.py @@ -1016,6 +1016,9 @@ def handle_disable_host(goptions, session, args): parser.add_option("--comment", help=_("Comment indicating why the host(s) are being disabled")) (options, args) = parser.parse_args(args) + if not args: + parser.error(_("At least one host must be specified")) + activate_session(session, goptions) session.multicall = True for host in args: @@ -1044,6 +1047,9 @@ def handle_enable_host(goptions, session, args): parser.add_option("--comment", help=_("Comment indicating why the host(s) are being enabled")) (options, args) = parser.parse_args(args) + if not args: + parser.error(_("At least one host must be specified")) + activate_session(session, goptions) session.multicall = True for host in args: diff --git a/tests/test_cli/test_disable_host.py b/tests/test_cli/test_disable_host.py index 5b39e6f..dbb23ff 100644 --- a/tests/test_cli/test_disable_host.py +++ b/tests/test_cli/test_disable_host.py @@ -16,6 +16,14 @@ class TestDisableHost(utils.CliTestCase): # Show long diffs in error output... maxDiff = None + def setUp(self): + self.error_format = """Usage: %s disable-host [options] hostname ... +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_handle_disable_host( @@ -84,7 +92,7 @@ class TestDisableHost(utils.CliTestCase): call('host2', comment='disable host test')]) self.assert_console_message(stdout, '') - @mock.patch('sys.stdout', new_callable=six.StringIO) + @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_handle_disable_host_no_argument(self, activate_session_mock, stdout): """Test %s function without arguments""" % handle_disable_host.__name__ @@ -96,13 +104,20 @@ class TestDisableHost(utils.CliTestCase): session.disableHost.return_value = True session.editHost.return_value = True - handle_disable_host(options, session, []) - activate_session_mock.assert_called_once() + expected = self.format_error_message("At least one host must be specified") + self.assert_system_exit( + handle_disable_host, + options, + session, + [], + stderr=expected, + activate_session=None) + + activate_session_mock.assert_not_called() session.getHost.assert_not_called() - session.multiCall.assert_called() + session.multiCall.assert_not_called() session.disableHost.assert_not_called() session.editHost.assert_not_called() - self.assert_console_message(stdout, '') def test_handle_disable_host_help(self): """Test %s help message""" % handle_disable_host.__name__ diff --git a/tests/test_cli/test_enable_host.py b/tests/test_cli/test_enable_host.py index f153584..c274bee 100644 --- a/tests/test_cli/test_enable_host.py +++ b/tests/test_cli/test_enable_host.py @@ -16,6 +16,13 @@ class TestEnableHost(utils.CliTestCase): # Show long diffs in error output... maxDiff = None + def setUp(self): + self.error_format = """Usage: %s enable-host [options] hostname ... +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') def test_handle_enable_host( @@ -96,13 +103,20 @@ class TestEnableHost(utils.CliTestCase): session.enableHost.return_value = True session.editHost.return_value = True - handle_enable_host(options, session, []) - activate_session_mock.assert_called_once() + expected = self.format_error_message("At least one host must be specified") + self.assert_system_exit( + handle_enable_host, + options, + session, + [], + stderr=expected, + activate_session=None) + + activate_session_mock.assert_not_called() session.getHost.assert_not_called() - session.multiCall.assert_called() + session.multiCall.assert_not_called() session.enableHost.assert_not_called() session.editHost.assert_not_called() - self.assert_console_message(stdout, '') def test_handle_enable_host_help(self): """Test %s help message""" % handle_enable_host.__name__