Enable/disable channel

Fixes: https://pagure.io/koji/issue/1851
This commit is contained in:
Jana Cupova 2021-06-21 11:11:11 +02:00 committed by Tomas Kopecek
parent a844de42ff
commit bc2a51350d
23 changed files with 659 additions and 80 deletions

View file

@ -20,6 +20,7 @@ admin commands:
block-group-req Block a group's requirement listing
block-pkg Block a package in the listing for tag
clone-tag Duplicate the contents of one tag onto another tag
disable-channel Mark one or more channels as disabled
disable-host Mark one or more hosts as disabled
disable-user Disable logins by a user
edit-channel Edit a channel
@ -29,6 +30,7 @@ admin commands:
edit-tag-inheritance Edit tag inheritance
edit-target Set the name, build_tag, and/or dest_tag of an existing build target to new values
edit-user Alter user information
enable-channel Mark one or more channels as enabled
enable-host Mark one or more hosts as enabled
enable-user Enable logins by a user
free-task Free a task

View file

@ -20,6 +20,7 @@ admin commands:
block-group-req Block a group's requirement listing
block-pkg Block a package in the listing for tag
clone-tag Duplicate the contents of one tag onto another tag
disable-channel Mark one or more channels as disabled
disable-host Mark one or more hosts as disabled
disable-user Disable logins by a user
edit-channel Edit a channel
@ -29,6 +30,7 @@ admin commands:
edit-tag-inheritance Edit tag inheritance
edit-target Set the name, build_tag, and/or dest_tag of an existing build target to new values
edit-user Alter user information
enable-channel Mark one or more channels as enabled
enable-host Mark one or more hosts as enabled
enable-user Enable logins by a user
free-task Free a task

View file

@ -9,6 +9,7 @@ import unittest
from koji_cli.commands import handle_add_host_to_channel
from . import utils
class TestAddHostToChannel(utils.CliTestCase):
# Show long diffs in error output...
@ -80,7 +81,6 @@ class TestAddHostToChannel(utils.CliTestCase):
channel = 'channel'
new_arg = '--new'
args = [host, channel, new_arg]
kwargs = {'create': True}
options = mock.MagicMock()
# Mock out the xmlrpc server
@ -98,8 +98,7 @@ class TestAddHostToChannel(utils.CliTestCase):
activate_session_mock.assert_called_once_with(session, options)
session.getChannel.assert_not_called()
session.getHost.assert_called_once_with(host)
session.addHostToChannel.assert_called_once_with(
host, channel, **kwargs)
session.addHostToChannel.assert_called_once_with(host, channel, create=True)
self.assertNotEqual(rv, 1)
@mock.patch('sys.stderr', new_callable=six.StringIO)

View file

@ -0,0 +1,115 @@
from __future__ import absolute_import
import unittest
import mock
import six
import koji
from koji_cli.commands import handle_disable_channel
from . import utils
class TestDisableChannel(utils.CliTestCase):
# Show long diffs in error output...
maxDiff = None
def setUp(self):
self.error_format = """Usage: %s disable-channel [options] <channelname> [<channelname> ...]
(Specify the --help global option for a list of other help options)
%s: error: {message}
""" % (self.progname, self.progname)
self.channelinfo = [
{'comment': None, 'description': None, 'enabled': False, 'id': 1,
'name': 'test-channel'}
]
def __vm(self, result):
m = koji.VirtualCall('mcall_method', [], {})
if isinstance(result, dict) and result.get('faultCode'):
m._result = result
else:
m._result = (result,)
return m
@mock.patch('sys.stderr', new_callable=six.StringIO)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_disable_channel(self, activate_session_mock, stdout, stderr):
"""Test disable-channel function"""
options = mock.MagicMock()
session = mock.MagicMock()
mcall = session.multicall.return_value.__enter__.return_value
mcall.getChannel.return_value = self.__vm(None)
arguments = ['test-channel']
with self.assertRaises(SystemExit) as ex:
handle_disable_channel(options, session, arguments)
self.assertExitCode(ex, 1)
activate_session_mock.assert_called_once()
session.multicall.assert_called_once()
session.disableChannel.assert_not_called()
expect = ''
for host in arguments:
expect += "No such channel: %s\n" % host
stderr_exp = "No changes made. Please correct the command line.\n"
self.assert_console_message(stdout, expect)
self.assert_console_message(stderr, stderr_exp)
# reset session mocks
activate_session_mock.reset_mock()
session.disableChannel.reset_mock()
session.multicall.reset_mock()
mcall = session.multicall.return_value.__enter__.return_value
mcall.getChannel.return_value = self.__vm(self.channelinfo)
arguments = ['test-channel', '--comment', 'enable channel test']
handle_disable_channel(options, session, arguments)
activate_session_mock.assert_called_once()
self.assertEqual(2, session.multicall.call_count)
self.assert_console_message(stdout, '')
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_disable_host_no_argument(self, activate_session_mock, stdout):
"""Test disable-channel function without arguments"""
options = mock.MagicMock()
session = mock.MagicMock()
session.getChannel.return_value = None
session.multicall.return_value = [[None]]
session.disableChannel.return_value = True
expected = self.format_error_message("At least one channel must be specified")
self.assert_system_exit(
handle_disable_channel,
options,
session,
[],
stderr=expected,
activate_session=None)
activate_session_mock.assert_not_called()
session.getChannel.assert_not_called()
session.multicall.assert_not_called()
session.disableChannel.assert_not_called()
def test_handle_disable_channel_help(self):
"""Test disable-channel help message"""
self.assert_help(
handle_disable_channel,
"""Usage: %s disable-channel [options] <channelname> [<channelname> ...]
(Specify the --help global option for a list of other help options)
Options:
-h, --help show this help message and exit
--comment=COMMENT Comment indicating why the channel(s) are being disabled
""" % self.progname)
if __name__ == '__main__':
unittest.main()

View file

@ -34,6 +34,7 @@ Options:
--name=NAME New channel name
--description=DESCRIPTION
Description of channel
--comment=COMMENT Comment of channel
""" % self.progname)
@mock.patch('sys.stderr', new_callable=six.StringIO)

View file

@ -0,0 +1,115 @@
from __future__ import absolute_import
import unittest
import mock
import six
import koji
from koji_cli.commands import handle_enable_channel
from . import utils
class TestEnableChannel(utils.CliTestCase):
# Show long diffs in error output...
maxDiff = None
def setUp(self):
self.error_format = """Usage: %s enable-channel [options] <channelname> [<channelname> ...]
(Specify the --help global option for a list of other help options)
%s: error: {message}
""" % (self.progname, self.progname)
self.channelinfo = [
{'comment': None, 'description': None, 'enabled': False, 'id': 1,
'name': 'test-channel'}
]
def __vm(self, result):
m = koji.VirtualCall('mcall_method', [], {})
if isinstance(result, dict) and result.get('faultCode'):
m._result = result
else:
m._result = (result,)
return m
@mock.patch('sys.stderr', new_callable=six.StringIO)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_enable_channel(self, activate_session_mock, stdout, stderr):
"""Test enable-channel function"""
options = mock.MagicMock()
session = mock.MagicMock()
mcall = session.multicall.return_value.__enter__.return_value
mcall.getChannel.return_value = self.__vm(None)
arguments = ['channel1', 'channel2']
with self.assertRaises(SystemExit) as ex:
handle_enable_channel(options, session, arguments)
self.assertExitCode(ex, 1)
activate_session_mock.assert_called_once()
session.multicall.assert_called_once()
session.enableChannel.assert_not_called()
expect = ''
for host in arguments:
expect += "No such channel: %s\n" % host
stderr_exp = "No changes made. Please correct the command line.\n"
self.assert_console_message(stdout, expect)
self.assert_console_message(stderr, stderr_exp)
# reset session mocks
activate_session_mock.reset_mock()
session.multicall.reset_mock()
session.enableChannel.reset_mock()
mcall = session.multicall.return_value.__enter__.return_value
mcall.getChannel.return_value = self.__vm(self.channelinfo)
arguments = ['channel1', 'channel2', '--comment', 'enable channel test']
handle_enable_channel(options, session, arguments)
activate_session_mock.assert_called_once()
self.assertEqual(2, session.multicall.call_count)
self.assert_console_message(stdout, '')
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_enable_host_no_argument(self, activate_session_mock, stdout):
"""Test enable-channel function without arguments"""
options = mock.MagicMock()
session = mock.MagicMock()
session.getChannel.return_value = None
session.multicall.return_value = [[None]]
session.enableChannel.return_value = True
expected = self.format_error_message("At least one channel must be specified")
self.assert_system_exit(
handle_enable_channel,
options,
session,
[],
stderr=expected,
activate_session=None)
activate_session_mock.assert_not_called()
session.getChannel.assert_not_called()
session.multicall.assert_not_called()
session.enableChannel.assert_not_called()
def test_handle_enable_channel_help(self):
"""Test enable-channel help message"""
self.assert_help(
handle_enable_channel,
"""Usage: %s enable-channel [options] <channelname> [<channelname> ...]
(Specify the --help global option for a list of other help options)
Options:
-h, --help show this help message and exit
--comment=COMMENT Comment indicating why the channel(s) are being enabled
""" % self.progname)
if __name__ == '__main__':
unittest.main()

View file

@ -1,28 +1,30 @@
from __future__ import absolute_import
import mock
import unittest
import mock
from six.moves import StringIO
import koji
from koji_cli.commands import anon_handle_list_channels
from . import utils
class TestListChannels(utils.CliTestCase):
maxDiff = None
class TestListChannels(unittest.TestCase):
def setUp(self):
self.options = mock.MagicMock()
self.options.quiet = True
self.session = mock.MagicMock()
self.session.getAPIVersion.return_value = koji.API_VERSION
self.args = []
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('koji_cli.commands.ensure_connection')
def test_list_channels(self, ensure_connection_mock, stdout):
self.session.listChannels.return_value = [
{'id': 1, 'name': 'default'},
{'id': 2, 'name': 'test'},
self.list_channels = [
{'id': 1, 'name': 'default', 'enabled': True, 'comment': 'test-comment-1',
'description': 'test-description-1'},
{'id': 2, 'name': 'test', 'enabled': False, 'comment': 'test-comment-2',
'description': 'test-description-2'},
]
self.session.multiCall.return_value = [
self.list_hosts_mc = [
[[
{'enabled': True, 'ready': True, 'capacity': 2.0, 'task_load': 1.34},
{'enabled': True, 'ready': False, 'capacity': 2.0, 'task_load': 0.0},
@ -32,16 +34,78 @@ class TestListChannels(unittest.TestCase):
{'enabled': True, 'ready': True, 'capacity': 2.0, 'task_load': 1.34},
{'enabled': False, 'ready': True, 'capacity': 2.0, 'task_load': 0.34},
{'enabled': True, 'ready': False, 'capacity': 2.0, 'task_load': 0.0},
]],
]]
]
anon_handle_list_channels(self.options, self.session, self.args)
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('koji_cli.commands.ensure_connection')
def test_list_channels(self, ensure_connection_mock, stdout):
self.session.listChannels.return_value = self.list_channels
self.session.multiCall.return_value = self.list_hosts_mc
args = []
anon_handle_list_channels(self.options, self.session, args)
actual = stdout.getvalue()
print(actual)
expected = """\
default 3 1 0 1 6 22%
test 2 2 1 1 6 28%
test [disabled] 2 2 1 1 6 28%
"""
self.assertMultiLineEqual(actual, expected)
ensure_connection_mock.assert_called_once_with(self.session, self.options)
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('koji_cli.commands.ensure_connection')
def test_list_channels_with_comment(self, ensure_connection_mock, stdout):
self.session.listChannels.return_value = self.list_channels
self.session.multiCall.return_value = self.list_hosts_mc
args = ['--comment']
anon_handle_list_channels(self.options, self.session, args)
actual = stdout.getvalue()
print(actual)
expected = 'default 3 1 0 1 6 22% ' \
'test-comment-1 \n' \
'test [disabled] 2 2 1 1 6 28% ' \
'test-comment-2 \n'
self.assertMultiLineEqual(actual, expected)
ensure_connection_mock.assert_called_once_with(self.session, self.options)
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('koji_cli.commands.ensure_connection')
def test_list_channels_with_description(self, ensure_connection_mock, stdout):
self.session.listChannels.return_value = self.list_channels
self.session.multiCall.return_value = self.list_hosts_mc
args = ['--description']
anon_handle_list_channels(self.options, self.session, args)
actual = stdout.getvalue()
print(actual)
expected = 'default 3 1 0 1 6 22% ' \
'test-description-1 \n' \
'test [disabled] 2 2 1 1 6 28% ' \
'test-description-2 \n'
self.assertMultiLineEqual(actual, expected)
ensure_connection_mock.assert_called_once_with(self.session, self.options)
def test_list_channels_help(self):
self.assert_help(
anon_handle_list_channels,
"""Usage: %s list-channels [options]
(Specify the --help global option for a list of other help options)
Options:
-h, --help show this help message and exit
--simple Print just list of channels without additional info
--quiet Do not print header information
--comment Show comments
--description Show descriptions
--enabled Limit to enabled channels
--not-enabled Limit to not enabled channels
--disabled Alias for --not-enabled
""" % self.progname)
if __name__ == '__main__':
unittest.main()

View file

@ -34,8 +34,8 @@ class TestListHosts(utils.CliTestCase):
@mock.patch('koji_cli.commands.ensure_connection')
def test_list_hosts_valid(self, ensure_connection, stdout):
host_update = 1615875554.862938
expected = """kojibuilder Y Y 0.0/2.0 x86_64 Tue, 16 Mar 2021 06:19:14 UTC
"""
expected = "kojibuilder Y Y 0.0/2.0 x86_64 " \
"Tue, 16 Mar 2021 06:19:14 UTC \n"
list_hosts = [{'arches': 'x86_64',
'capacity': 2.0,
'comment': None,