From 2c4f09d8910b4005af2fcca181a8c11031f1ccee Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Jun 07 2017 19:08:06 +0000 Subject: PR#442 list-channels CLI command Merges #442 https://pagure.io/koji/pull-request/442 Fixes #437 PR#442 list-channels CLI command --- diff --git a/cli/koji b/cli/koji index f142af3..c4a22cd 100755 --- a/cli/koji +++ b/cli/koji @@ -709,7 +709,7 @@ def handle_add_host_to_channel(options, session, args): usage = _("usage: %prog add-host-to-channel [options] hostname channel") usage += _("\n(Specify the --help global option for a list of other help options)") parser = OptionParser(usage=usage) - parser.add_option("--list", action="store_true", help=_("List possible channels")) + parser.add_option("--list", action="store_true", help=optparse.SUPPRESS_HELP) parser.add_option("--new", action="store_true", help=_("Create channel if needed")) (options, args) = parser.parse_args(args) if not options.list and len(args) != 2: @@ -3189,6 +3189,25 @@ def handle_unblock_group_req(options, session, args): activate_session(session) session.groupReqListUnblock(tag, group, req) +def anon_handle_list_channels(options, session, args): + "[info] Print channels listing" + usage = _("usage: %prog list-channels") + usage += _("\n(Specify the --help global option for a list of other help options)") + parser = OptionParser(usage=usage) + parser.add_option("--quiet", action="store_true", help=_("Do not print header information"), default=options.quiet) + (options, args) = parser.parse_args(args) + activate_session(session) + channels = session.listChannels() + session.multicall = True + for channel in channels: + session.listHosts(channelID=channel['id']) + for channel, hosts in zip(channels, session.multiCall()): + channel['hosts'] = len(hosts[0]) + if not options.quiet: + print('Channel Hosts') + for channel in channels: + print("%(name)-15s %(hosts) 5d" % channel) + def anon_handle_list_hosts(options, session, args): "[info] Print the host listing" usage = _("usage: %prog list-hosts [options]") diff --git a/tests/test_cli/data/list-commands.txt b/tests/test_cli/data/list-commands.txt index 5db98c4..6046042 100644 --- a/tests/test_cli/data/list-commands.txt +++ b/tests/test_cli/data/list-commands.txt @@ -95,6 +95,7 @@ info commands: latest-build Print the latest builds for a tag list-api Print the list of XML-RPC APIs list-buildroot List the rpms used in or built in a buildroot + list-channels Print channels listing list-external-repos List external repos list-groups Print the group listings list-history Display historical data diff --git a/tests/test_cli/test_list_channels.py b/tests/test_cli/test_list_channels.py new file mode 100644 index 0000000..aa2674d --- /dev/null +++ b/tests/test_cli/test_list_channels.py @@ -0,0 +1,41 @@ +from __future__ import absolute_import +import mock +import unittest +from six.moves import StringIO + +import koji + +from . import loadcli +cli = loadcli.cli + +class TestListChannels(unittest.TestCase): + def setUp(self): + self.options = mock.MagicMock() + self.session = mock.MagicMock() + self.session.getAPIVersion.return_value = koji.API_VERSION + self.args = mock.MagicMock() + self.original_parser = cli.OptionParser + cli.OptionParser = mock.MagicMock() + self.parser = cli.OptionParser.return_value + cli.options = self.options # globals!!! + + def tearDown(self): + cli.OptionParser = self.original_parser + + @mock.patch('sys.stdout', new_callable=StringIO) + def test_list_channels(self, stdout): + options = mock.MagicMock() + options.quiet = True + self.parser.parse_args.return_value = [options, []] + + # mock xmlrpc + self.session.listChannels.return_value = [ + {'id': 1, 'name': 'default'}, + {'id': 2, 'name': 'test'}, + ] + self.session.multiCall.return_value = [[[1,2,3]], [[4,5]]] + + cli.anon_handle_list_channels(self.options, self.session, self.args) + actual = stdout.getvalue() + expected = 'successfully connected to hub\ndefault 3\ntest 2\n' + self.assertMultiLineEqual(actual, expected)