Check CLI arguments for enable/disable host

Fixes: https://pagure.io/koji/issue/1364
This commit is contained in:
Tomas Kopecek 2019-03-25 16:08:15 +01:00 committed by Mike McLean
parent b92fa538dd
commit 0b30e73053
3 changed files with 44 additions and 9 deletions

View file

@ -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:

View file

@ -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__

View file

@ -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__