From 8a273b2cae34da0d81d9cf1d4c4e669325c8d157 Mon Sep 17 00:00:00 2001 From: Franz Chih-Ping Hsieh Date: Mon, 4 Dec 2017 11:56:06 -0500 Subject: [PATCH] add unit test for cli commands, coverage(40%) --- tests/test_cli/test_add_group_pkg.py | 62 +++++++ tests/test_cli/test_add_group_req.py | 60 +++++++ tests/test_cli/test_add_user.py | 93 +++++++++++ tests/test_cli/test_add_volume.py | 74 +++++++++ tests/test_cli/test_block_group_pkg.py | 62 +++++++ tests/test_cli/test_block_group_req.py | 60 +++++++ tests/test_cli/test_disable_user.py | 71 ++++++++ tests/test_cli/test_enable_user.py | 71 ++++++++ tests/test_cli/test_grant_cg_access.py | 77 +++++++++ tests/test_cli/test_grant_permission.py | 82 ++++++++++ tests/test_cli/test_list_api.py | 85 ++++++++++ tests/test_cli/test_list_groups.py | 196 +++++++++++++++++++++++ tests/test_cli/test_list_permissions.py | 99 ++++++++++++ tests/test_cli/test_list_volumes.py | 46 ++++++ tests/test_cli/test_revoke_cg_access.py | 75 +++++++++ tests/test_cli/test_revoke_permission.py | 80 +++++++++ tests/test_cli/test_set_build_volume.py | 113 +++++++++++++ tests/test_cli/test_unblock_group_pkg.py | 62 +++++++ tests/test_cli/test_unblock_group_req.py | 60 +++++++ 19 files changed, 1528 insertions(+) create mode 100644 tests/test_cli/test_add_group_pkg.py create mode 100644 tests/test_cli/test_add_group_req.py create mode 100644 tests/test_cli/test_add_user.py create mode 100644 tests/test_cli/test_add_volume.py create mode 100644 tests/test_cli/test_block_group_pkg.py create mode 100644 tests/test_cli/test_block_group_req.py create mode 100644 tests/test_cli/test_disable_user.py create mode 100644 tests/test_cli/test_enable_user.py create mode 100644 tests/test_cli/test_grant_cg_access.py create mode 100644 tests/test_cli/test_grant_permission.py create mode 100644 tests/test_cli/test_list_api.py create mode 100644 tests/test_cli/test_list_groups.py create mode 100644 tests/test_cli/test_list_permissions.py create mode 100644 tests/test_cli/test_list_volumes.py create mode 100644 tests/test_cli/test_revoke_cg_access.py create mode 100644 tests/test_cli/test_revoke_permission.py create mode 100644 tests/test_cli/test_set_build_volume.py create mode 100644 tests/test_cli/test_unblock_group_pkg.py create mode 100644 tests/test_cli/test_unblock_group_req.py diff --git a/tests/test_cli/test_add_group_pkg.py b/tests/test_cli/test_add_group_pkg.py new file mode 100644 index 00000000..30f9307b --- /dev/null +++ b/tests/test_cli/test_add_group_pkg.py @@ -0,0 +1,62 @@ +from __future__ import absolute_import +import mock +import unittest +from koji_cli.commands import handle_add_group_pkg +from . import utils + + +class TestAddGroupPkg(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.session = mock.MagicMock() + self.options = mock.MagicMock() + self.activate_session = mock.patch('koji_cli.commands.activate_session').start() + + self.error_format = """Usage: %s add-group-pkg [options] [...] +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + def tearDown(self): + mock.patch.stopall() + + def test_handle_add_group_pkg(self): + """Test handle_add_group_pkg function""" + arguments = ['fedora-build', 'build'] + for pkg in [['bash'], ['sed', 'less', 'awk']]: + handle_add_group_pkg(self.options, self.session, arguments + pkg) + calls = [mock.call(arguments[0], arguments[1], p) for p in pkg] + self.session.groupPackageListAdd.assert_has_calls(calls) + self.activate_session.assert_called_with(self.session, self.options) + + def test_handle_add_group_pkg_argument_error(self): + """Test handle_add_group_pkg function with wrong argument""" + expected = self.format_error_message( + "You must specify a tag name, group name, and one or more package names") + for arg in [[], ['tag'], ['tag', 'grp']]: + self.assert_system_exit( + handle_add_group_pkg, + self.options, + self.session, + arg, + stderr=expected, + activate_session=None) + self.activate_session.assert_not_called() + + def test_handle_add_group_pkg_help(self): + self.assert_help( + handle_add_group_pkg, + """Usage: %s add-group-pkg [options] [...] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_add_group_req.py b/tests/test_cli/test_add_group_req.py new file mode 100644 index 00000000..e713bb25 --- /dev/null +++ b/tests/test_cli/test_add_group_req.py @@ -0,0 +1,60 @@ +from __future__ import absolute_import +import mock +import unittest +from koji_cli.commands import handle_add_group_req +from . import utils + + +class TestAddGroupReq(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.session = mock.MagicMock() + self.options = mock.MagicMock() + self.activate_session = mock.patch('koji_cli.commands.activate_session').start() + + self.error_format = """Usage: %s add-group-req [options] +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + def tearDown(self): + mock.patch.stopall() + + def test_handle_add_group_req(self): + """Test handle_add_group_req function""" + arguments = ['fedora-build', 'build', 'srpm-build'] + handle_add_group_req(self.options, self.session, arguments) + self.session.groupReqListAdd.assert_called_with(*arguments) + self.activate_session.assert_called_with(self.session, self.options) + + def test_handle_add_group_req_argument_error(self): + """Test handle_add_group_req function with wrong argument""" + expected = self.format_error_message( + "You must specify a tag name and two group names") + for arg in [[], ['tag'], ['tag', 'grp', 'opt1', 'opt2']]: + self.assert_system_exit( + handle_add_group_req, + self.options, + self.session, + arg, + stderr=expected, + activate_session=None) + self.activate_session.assert_not_called() + + def test_handle_add_group_req_help(self): + self.assert_help( + handle_add_group_req, + """Usage: %s add-group-req [options] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_add_user.py b/tests/test_cli/test_add_user.py new file mode 100644 index 00000000..6ec1b02b --- /dev/null +++ b/tests/test_cli/test_add_user.py @@ -0,0 +1,93 @@ +from __future__ import absolute_import +import mock +import six +import unittest +from koji_cli.commands import handle_add_user +from . import utils + + +class TestAddUser(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s add-user username [options] +(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_add_user( + self, + activate_session_mock, + stdout): + """Test handle_add_user function""" + session = mock.MagicMock() + options = mock.MagicMock() + username = 'user' + user_id = 1001 + principal = 'krb-pricipal' + + # Case 1. no argument error + expected = self.format_error_message( + "You must specify the username of the user to add") + self.assert_system_exit( + handle_add_user, + options, + session, + [], + stderr=expected, + activate_session=None) + + # Case 2. Too many argument error + expected = self.format_error_message( + "This command only accepts one argument (username)") + self.assert_system_exit( + handle_add_user, + options, + session, + ['user-1', 'user-2', 'user-3'], + stderr=expected, + activate_session=None) + + # Case 3. Add user test + expected = "Added user %s (%i)" % (username, user_id) + "\n" + arguments = [username, '--principal', principal] + session.createUser.return_value = user_id + handle_add_user(options, session, arguments) + session.createUser.assert_called_with( + username, + status=0, + krb_principal=principal) + self.assert_console_message(stdout, expected) + activate_session_mock.assert_called_with(session, options) + + # Case 3. Add blocked user + arguments = [username, '--principal', principal, '--disable'] + handle_add_user(options, session, arguments) + session.createUser.assert_called_with( + username, + status=1, # 0: normal, 1: disabled + krb_principal=principal) + self.assert_console_message(stdout, expected) + activate_session_mock.assert_called_with(session, options) + + def test_handle_add_user_help(self): + self.assert_help( + handle_add_user, + """Usage: %s add-user username [options] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + --principal=PRINCIPAL + The Kerberos principal for this user + --disable Prohibit logins by this user +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_add_volume.py b/tests/test_cli/test_add_volume.py new file mode 100644 index 00000000..f7021af3 --- /dev/null +++ b/tests/test_cli/test_add_volume.py @@ -0,0 +1,74 @@ +from __future__ import absolute_import +import mock +import six +import unittest +from koji_cli.commands import handle_add_volume +from . import utils + + +class TestAddVolume(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s add-volume volume-name +(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_add_volume( + self, + activate_session_mock, + stdout): + """Test handle_add_volume function""" + session = mock.MagicMock() + options = mock.MagicMock() + vol_name = 'vol-test-01' + vol_info = {'id': 1, 'name': vol_name} + + # Case 1. argument error + expected = self.format_error_message( + "Command requires exactly one volume-name.") + for arg in [[], ['test-1', 'test-2']]: + self.assert_system_exit( + handle_add_volume, + options, + session, + arg, + stderr=expected, + activate_session=None) + + # Case 2. volume already exists + expected = "Volume %s already exists" % vol_name + "\n" + session.getVolume.return_value = vol_info + self.assertEqual(1, handle_add_volume(options, session, [vol_name])) + self.assert_console_message(stdout, expected) + session.getVolume.assert_called_with(vol_name) + activate_session_mock.assert_not_called() + + # Case 3. Add volume + expected = "Added volume %(name)s with id %(id)i" % vol_info + "\n" + session.getVolume.return_value = {} + session.addVolume.return_value = vol_info + handle_add_volume(options, session, [vol_name]) + self.assert_console_message(stdout, expected) + session.addVolume(vol_name) + activate_session_mock.assert_called_with(session, options) + + def test_handle_add_volume_help(self): + self.assert_help( + handle_add_volume, + """Usage: %s add-volume volume-name +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_block_group_pkg.py b/tests/test_cli/test_block_group_pkg.py new file mode 100644 index 00000000..47a641fc --- /dev/null +++ b/tests/test_cli/test_block_group_pkg.py @@ -0,0 +1,62 @@ +from __future__ import absolute_import +import mock +import unittest +from koji_cli.commands import handle_block_group_pkg +from . import utils + + +class TestBlockGroupPkg(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.session = mock.MagicMock() + self.options = mock.MagicMock() + self.activate_session = mock.patch('koji_cli.commands.activate_session').start() + + self.error_format = """Usage: %s block-group-pkg [options] [...] +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + def tearDown(self): + mock.patch.stopall() + + def test_handle_block_group_pkg(self): + """Test handle_block_group_pkg function""" + arguments = ['fedora-build', 'build'] + for pkg in [['bash'], ['sed', 'less', 'awk']]: + handle_block_group_pkg(self.options, self.session, arguments + pkg) + calls = [mock.call(arguments[0], arguments[1], p) for p in pkg] + self.session.groupPackageListBlock.assert_has_calls(calls) + self.activate_session.assert_called_with(self.session, self.options) + + def test_handle_block_group_pkg_argument_error(self): + """Test handle_block_group_pkg function with wrong argument""" + expected = self.format_error_message( + "You must specify a tag name, group name, and one or more package names") + for arg in [[], ['tag'], ['tag', 'grp']]: + self.assert_system_exit( + handle_block_group_pkg, + self.options, + self.session, + arg, + stderr=expected, + activate_session=None) + self.activate_session.assert_not_called() + + def test_handle_block_group_pkg_help(self): + self.assert_help( + handle_block_group_pkg, + """Usage: %s block-group-pkg [options] [...] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_block_group_req.py b/tests/test_cli/test_block_group_req.py new file mode 100644 index 00000000..e1a96dc2 --- /dev/null +++ b/tests/test_cli/test_block_group_req.py @@ -0,0 +1,60 @@ +from __future__ import absolute_import +import mock +import unittest +from koji_cli.commands import handle_block_group_req +from . import utils + + +class TestBlockGroupReq(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.session = mock.MagicMock() + self.options = mock.MagicMock() + self.activate_session = mock.patch('koji_cli.commands.activate_session').start() + + self.error_format = """Usage: %s block-group-req [options] +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + def tearDown(self): + mock.patch.stopall() + + def test_handle_block_group_req(self): + """Test handle_block_group_req function""" + arguments = ['fedora-build', 'build', 'srpm-build'] + handle_block_group_req(self.options, self.session, arguments) + self.session.groupReqListBlock.assert_called_with(*arguments) + self.activate_session.assert_called_with(self.session, self.options) + + def test_handle_block_group_req_argument_error(self): + """Test handle_block_group_req function with wrong argument""" + expected = self.format_error_message( + "You must specify a tag name and two group names") + for arg in [[], ['tag'], ['tag', 'grp', 'opt1', 'opt2']]: + self.assert_system_exit( + handle_block_group_req, + self.options, + self.session, + arg, + stderr=expected, + activate_session=None) + self.activate_session.assert_not_called() + + def test_handle_block_group_req_help(self): + self.assert_help( + handle_block_group_req, + """Usage: %s block-group-req [options] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_disable_user.py b/tests/test_cli/test_disable_user.py new file mode 100644 index 00000000..ebcd03d2 --- /dev/null +++ b/tests/test_cli/test_disable_user.py @@ -0,0 +1,71 @@ +from __future__ import absolute_import +import mock +import six +import unittest +from koji_cli.commands import handle_disable_user +from . import utils + + +class TestDisableUser(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s disable-user username +(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_user( + self, + activate_session_mock, + stdout): + """Test handle_disable_user function""" + session = mock.MagicMock() + options = mock.MagicMock() + username = 'user' + + # Case 1. no argument error + expected = self.format_error_message( + "You must specify the username of the user to disable") + self.assert_system_exit( + handle_disable_user, + options, + session, + [], + stderr=expected, + activate_session=None) + + # Case 2. Too many argument error + expected = self.format_error_message( + "This command only accepts one argument (username)") + self.assert_system_exit( + handle_disable_user, + options, + session, + ['user-1', 'user-2', 'user-3'], + stderr=expected, + activate_session=None) + + # Case 3. Disable user test + handle_disable_user(options, session, [username]) + session.disableUser.assert_called_with(username) + activate_session_mock.assert_called_with(session, options) + + def test_handle_disable_user_help(self): + self.assert_help( + handle_disable_user, + """Usage: %s disable-user username +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_enable_user.py b/tests/test_cli/test_enable_user.py new file mode 100644 index 00000000..e61eb3a0 --- /dev/null +++ b/tests/test_cli/test_enable_user.py @@ -0,0 +1,71 @@ +from __future__ import absolute_import +import mock +import six +import unittest +from koji_cli.commands import handle_enable_user +from . import utils + + +class TestEnableUser(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s enable-user username +(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_user( + self, + activate_session_mock, + stdout): + """Test handle_enable_user function""" + session = mock.MagicMock() + options = mock.MagicMock() + username = 'user' + + # Case 1. no argument error + expected = self.format_error_message( + "You must specify the username of the user to enable") + self.assert_system_exit( + handle_enable_user, + options, + session, + [], + stderr=expected, + activate_session=None) + + # Case 2. Too many argument error + expected = self.format_error_message( + "This command only accepts one argument (username)") + self.assert_system_exit( + handle_enable_user, + options, + session, + ['user-1', 'user-2', 'user-3'], + stderr=expected, + activate_session=None) + + # Case 3. Enable user test + handle_enable_user(options, session, [username]) + session.enableUser.assert_called_with(username) + activate_session_mock.assert_called_with(session, options) + + def test_handle_enable_user_help(self): + self.assert_help( + handle_enable_user, + """Usage: %s enable-user username +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_grant_cg_access.py b/tests/test_cli/test_grant_cg_access.py new file mode 100644 index 00000000..2b63d897 --- /dev/null +++ b/tests/test_cli/test_grant_cg_access.py @@ -0,0 +1,77 @@ +from __future__ import absolute_import +import mock +import six +import unittest + +from koji_cli.commands import handle_grant_cg_access +from . import utils + + +class TestGrantCGAccess(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s grant-cg-access +(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_grant_cg_access( + self, + activate_session_mock, + stdout): + """Test handle_grant_cg_access function""" + session = mock.MagicMock() + options = mock.MagicMock() + cg = 'cg' + user = 'user' + + # Case 1. argument error + expected = self.format_error_message( + "Please specify a user and content generator") + for args in [[], [user]]: + self.assert_system_exit( + handle_grant_cg_access, + options, + session, + args, + stderr=expected, + activate_session=None) + + # Case 2. user not exists + expected = self.format_error_message( + "No such user: %s" % user) + session.getUser.return_value = None + self.assert_system_exit( + handle_grant_cg_access, + options, + session, + [user, cg], + stderr=expected) + + # Case 3. grant permission with --new + cg = 'content-generator' + session.getUser.return_value = {'id': 101, 'name': user} + handle_grant_cg_access(options, session, [user, cg, '--new']) + calls = [mock.call(user, cg, create=True)] + session.grantCGAccess.assert_has_calls(calls) + + def test_handle_grant_cg_access_help(self): + self.assert_help( + handle_grant_cg_access, + """Usage: %s grant-cg-access +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + --new Create a new content generator +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_grant_permission.py b/tests/test_cli/test_grant_permission.py new file mode 100644 index 00000000..ecd38cea --- /dev/null +++ b/tests/test_cli/test_grant_permission.py @@ -0,0 +1,82 @@ +from __future__ import absolute_import +import mock +import six +import unittest + +from koji_cli.commands import handle_grant_permission +from . import utils + + +class TestGrantPermission(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s grant-permission [ ...] +(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_grant_permission( + self, + activate_session_mock, + stdout): + """Test handle_grant_permission function""" + session = mock.MagicMock() + options = mock.MagicMock() + perm = 'createrepo' + users = 'user' + + # Case 1. argument error + expected = self.format_error_message( + "Please specify a permission and at least one user") + for args in [[], [perm]]: + self.assert_system_exit( + handle_grant_permission, + options, + session, + args, + stderr=expected, + activate_session=None) + + # Case 2. user not exists + expected = self.format_error_message( + "No such user: %s" % users) + session.getUser.return_value = None + self.assert_system_exit( + handle_grant_permission, + options, + session, + [perm, users], + stderr=expected) + + # Case 3. grant permission with --new + users = ['user1', 'user2', 'user3'] + perm = 'build_iso' + session.getUser.side_effect = [ + {'id': 101, 'name': users[0]}, + {'id': 111, 'name': users[1]}, + {'id': 121, 'name': users[2]}, + ] + handle_grant_permission(options, session, [perm, '--new'] + users) + calls = [mock.call(p, perm, create=True) for p in users] + session.grantPermission.assert_has_calls(calls) + + def test_handle_grant_permission_help(self): + self.assert_help( + handle_grant_permission, + """Usage: %s grant-permission [ ...] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + --new Create a new permission +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_list_api.py b/tests/test_cli/test_list_api.py new file mode 100644 index 00000000..77ef6e3f --- /dev/null +++ b/tests/test_cli/test_list_api.py @@ -0,0 +1,85 @@ +from __future__ import absolute_import +import mock +import six +import unittest + +from koji_cli.commands import anon_handle_list_api +from . import utils + + +class TestListApi(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s list-api [options] +(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_anon_handle_list_api( + self, + activate_session_mock, + stdout): + """Test anon_handle_list_api function""" + session = mock.MagicMock() + options = mock.MagicMock() + + # Case 1. argument error + expected = self.format_error_message( + "This command takes no arguments") + self.assert_system_exit( + anon_handle_list_api, + options, + session, + ['arg'], + stderr=expected, + activate_session=None) + + # Case 2. + session._listapi.return_value = [ + { + 'argdesc': '(name, *args, **kwargs)', + 'doc': 'A debug function', + 'argspec': [['name'], 'args', 'kwargs', None], + 'args': ['name'], + 'name': 'debugFunction' + }, + { + 'doc': 'Add user to group', + 'argspec': [['group', 'user', 'strict'], None, None, [True]], + 'args': ['group', 'user', ['strict', True]], + 'name': 'addGroupMember' + }, + { + 'doc': None, + 'argspec': [[], None, None, None], + 'args': [], + 'name': 'host.getID' + } + ] + expected = "addGroupMember(group, user, strict=True)\n" + expected += " description: Add user to group\n" + expected += "debugFunction(name, *args, **kwargs)\n" + expected += " description: A debug function\n" + expected += "host.getID()\n" + anon_handle_list_api(options, session, []) + self.assert_console_message(stdout, expected) + + def test_anon_handle_list_api_help(self): + self.assert_help( + anon_handle_list_api, + """Usage: %s list-api [options] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_list_groups.py b/tests/test_cli/test_list_groups.py new file mode 100644 index 00000000..61e12d00 --- /dev/null +++ b/tests/test_cli/test_list_groups.py @@ -0,0 +1,196 @@ +from __future__ import absolute_import +import mock +import six +import unittest +import time + +from koji_cli.commands import anon_handle_list_groups +from . import utils + + +class TestListGroups(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.session = mock.MagicMock() + self.options = mock.MagicMock() + + self.activate_session = mock.patch('koji_cli.commands.activate_session').start() + self.event_from_opts = mock.patch('koji.util.eventFromOpts').start() + + self.error_format = """Usage: %s list-groups [options] [group] +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + def tearDown(self): + mock.patch.stopall() + + @mock.patch('sys.stdout', new_callable=six.StringIO) + @mock.patch('koji_cli.commands.activate_session') + def test_anon_handle_list_groups_argument_error( + self, + activate_session_mock, + stdout): + """Test anon_handle_list_groups function""" + expected = self.format_error_message( + "Incorrect number of arguments") + for arg in [[], ['tag', 'grp', 'etc']]: + self.assert_system_exit( + anon_handle_list_groups, + self.options, + self.session, + arg, + stderr=expected, + activate_session=None) + activate_session_mock.assert_not_called() + + def test_anon_handle_list_groups_list_all(self): + self.event_from_opts.return_value = {} + self.__list_groups('', [], '') + + def test_anon_handle_list_groups_list_with_group(self): + self.event_from_opts.return_value = {} + self.__list_groups('build', [], '') + + # output should be blank + self.__list_groups('wrong-grp', [], '') + + def test_anon_handle_list_groups_list_with_event(self): + self.event_from_opts.return_value = { + 'id': 4, + 'ts': 1234567 + } + event = {'id': 4, 'timestr': time.asctime(time.localtime(1234567))} + expected = "Querying at event %(id)i (%(timestr)s)" % event + "\n" + self.__list_groups('build', ['--ts', '1234567'], expected) + + def __list_groups(self, query_group, options, expected): + _list_tags = [ + { + 'maven_support': False, + 'locked': False, + 'name': 'fedora', + 'perm': None, + 'id': 1, + 'arches': None, + 'maven_include_all': False, + 'perm_id': None + }, { + 'maven_support': False, + 'locked': False, + 'name': 'fedora-build', + 'perm': None, + 'id': 2, + 'arches': 'x86_64, i386, ppc, ppc64', + 'maven_include_all': False, + 'perm_id': None + } + ] + + _get_tag_groups = [ + { + "grouplist": [], + "packagelist": [ + { + "package": "bash", + "requires": None, + "tag_id": 2, + "group_id": 1, + "type": "mandatory", + "basearchonly": None, + "blocked": False + }, + ], + "display_name": "build", + "name": "build", + "uservisible": True, + "description": None, + "tag_id": 2, + "is_default": None, + "biarchonly": False, + "exported": True, + "langonly": None, + "group_id": 1, + "blocked": False + }, + { + "grouplist": [ + { + 'is_metapkg': False, + 'name': 'build-base', + 'tag_id': 2, + 'req_id': 4, + 'group_id': 2, + 'type': 'mandatory', + 'blocked': False + } + ], + "packagelist": [ + { + "package": "bash", + "requires": None, + "tag_id": 2, + "group_id": 2, + "type": "mandatory", + "basearchonly": None, + "blocked": False + } + ], + "display_name": "srpm-build", + "name": "srpm-build", + "uservisible": True, + "description": None, + "tag_id": 2, + "is_default": None, + "biarchonly": False, + "exported": True, + "langonly": None, + "group_id": 2, + "blocked": False + }, + ] + + tags = dict([(x['id'], x['name']) for x in _list_tags]) + _group_list = [(x['name'], x) for x in _get_tag_groups] + _group_list.sort() + groups = [x[1] for x in _group_list] + for group in groups: + if query_group != '' and group['name'] != query_group: + continue + expected += "%s [%s]" % (group['name'], tags.get(group['tag_id'], group['tag_id'])) + "\n" + for grp in group["grouplist"]: + grp['tag_name'] = tags.get(grp['tag_id'], grp['tag_id']) + expected += " @%(name)s [%(tag_name)s]" % grp + "\n" + for pkg in group["packagelist"]: + pkg['tag_name'] = tags.get(pkg['tag_id'], pkg['tag_id']) + expected += " %(package)s: %(basearchonly)s, %(type)s [%(tag_name)s]" % pkg + "\n" + + self.session.listTags.return_value = _list_tags + self.session.getTagGroups.return_value = _get_tag_groups + args = ['fedora26-build'] + args += [query_group] if query_group != '' else [] + args += options if options else [] + with mock.patch('sys.stdout', new_callable=six.StringIO) as stdout: + anon_handle_list_groups(self.options, self.session, args) + self.assert_console_message(stdout, expected) + + def test_anon_handle_list_groups_help(self): + self.assert_help( + anon_handle_list_groups, + """Usage: %s list-groups [options] [group] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + --event=EVENT# query at event + --ts=TIMESTAMP query at timestamp + --repo=REPO# query at event for a repo +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_list_permissions.py b/tests/test_cli/test_list_permissions.py new file mode 100644 index 00000000..a131ded8 --- /dev/null +++ b/tests/test_cli/test_list_permissions.py @@ -0,0 +1,99 @@ +from __future__ import absolute_import +import mock +import six +import unittest +from koji_cli.commands import handle_list_permissions +from . import utils + + +class TestListPermissions(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s list-permissions [options] +(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_list_permissions( + self, + activate_session_mock, + stdout): + """Test handle_list_permissions function""" + session = mock.MagicMock() + options = mock.MagicMock() + user = 'tester' + all_perms = [ + {'id': 0, 'name': 'admin'}, + {'id': 1, 'name': 'build'}, + {'id': 2, 'name': 'repo'}, + {'id': 3, 'name': 'image'}, + {'id': 4, 'name': 'livecd'}, + {'id': 5, 'name': 'appliance'} + ] + + # case 1. argument error (no argument is required) + expected = self.format_error_message("This command takes no arguments") + self.assert_system_exit( + handle_list_permissions, + options, + session, + ['arg-1', 'arg-2'], + stderr=expected, + activate_session=None) + + # case 2. user does not exists + expected = "User %s does not exist" % user + "\n" + session.getUser.return_value = None + self.assertEqual(1, handle_list_permissions(options, session, ['--user', user])) + self.assert_console_message(stdout, expected) + + # case 3. List user permission + perms = [p['name'] for p in all_perms[::1]] + session.getUserPerms.return_value = perms + session.getUser.return_value = {'id': 101, 'name': user} + expected = "\n".join([p for p in perms]) + "\n" + handle_list_permissions(options, session, ['--user', user]) + self.assert_console_message(stdout, expected) + + session.getUserPerms.reset_mock() + + # case 4. List my permission + perms = [p['name'] for p in all_perms[1:3]] + session.getPerms.return_value = perms + expected = "\n".join([p for p in perms]) + "\n" + handle_list_permissions(options, session, ['--mine']) + self.assert_console_message(stdout, expected) + session.getUserPerms.assert_not_called() + + session.getPerms.reset_mock() + + # case 5. List all permission + session.getAllPerms.return_value = all_perms + expected = "\n".join([p['name'] for p in all_perms]) + "\n" + handle_list_permissions(options, session, []) + self.assert_console_message(stdout, expected) + session.getUserPerms.assert_not_called() + session.getPerms.assert_not_called() + session.getAllPerms.assert_called_once() + + def test_handle_list_permissions_help(self): + self.assert_help( + handle_list_permissions, + """Usage: %s list-permissions [options] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + --user=USER List permissions for the given user + --mine List your permissions +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_list_volumes.py b/tests/test_cli/test_list_volumes.py new file mode 100644 index 00000000..714d5b83 --- /dev/null +++ b/tests/test_cli/test_list_volumes.py @@ -0,0 +1,46 @@ +from __future__ import absolute_import +import mock +import six +import unittest +from koji_cli.commands import handle_list_volumes +from . import utils + + +class TestListVolumes(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + @mock.patch('sys.stdout', new_callable=six.StringIO) + @mock.patch('koji_cli.commands.activate_session') + def test_handle_list_volumes( + self, + activate_session_mock, + stdout): + """Test handle_list_volumes function""" + session = mock.MagicMock() + options = mock.MagicMock() + vol_info = [ + {'id': 1, 'name': 'DEFAULT'}, + {'id': 2, 'name': 'TEST-1'}, + {'id': 3, 'name': 'TEST-2'}, + ] + + expected = "\n".join([v['name'] for v in vol_info]) + "\n" + session.listVolumes.return_value = vol_info + handle_list_volumes(options, session, []) + self.assert_console_message(stdout, expected) + + def test_handle_list_volumes_help(self): + self.assert_help( + handle_list_volumes, + """Usage: %s list-volumes +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_revoke_cg_access.py b/tests/test_cli/test_revoke_cg_access.py new file mode 100644 index 00000000..8b3107c4 --- /dev/null +++ b/tests/test_cli/test_revoke_cg_access.py @@ -0,0 +1,75 @@ +from __future__ import absolute_import +import mock +import six +import unittest + +from koji_cli.commands import handle_revoke_cg_access +from . import utils + + +class TestRevokeCGAccess(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s revoke-cg-access +(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_revoke_cg_access( + self, + activate_session_mock, + stdout): + """Test handle_revoke_cg_access function""" + session = mock.MagicMock() + options = mock.MagicMock() + cg = 'content-generator' + user = 'user' + + # Case 1. argument error + expected = self.format_error_message( + "Please specify a user and content generator") + for args in [[], [user]]: + self.assert_system_exit( + handle_revoke_cg_access, + options, + session, + args, + stderr=expected, + activate_session=None) + + # Case 2. user not exists + expected = self.format_error_message( + "No such user: %s" % user) + session.getUser.return_value = None + self.assert_system_exit( + handle_revoke_cg_access, + options, + session, + [user, cg], + stderr=expected) + + # Case 3. grant cgission with --new + session.getUser.return_value = {'id': 101, 'name': user} + handle_revoke_cg_access(options, session, [user, cg]) + calls = [mock.call(user, cg)] + session.revokeCGAccess.assert_has_calls(calls) + + def test_handle_revoke_cg_access_help(self): + self.assert_help( + handle_revoke_cg_access, + """Usage: %s revoke-cg-access +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_revoke_permission.py b/tests/test_cli/test_revoke_permission.py new file mode 100644 index 00000000..e532df0b --- /dev/null +++ b/tests/test_cli/test_revoke_permission.py @@ -0,0 +1,80 @@ +from __future__ import absolute_import +import mock +import six +import unittest + +from koji_cli.commands import handle_revoke_permission +from . import utils + + +class TestRevokePermission(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s revoke-permission [ ...] +(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_revoke_permission( + self, + activate_session_mock, + stdout): + """Test handle_revoke_permission function""" + session = mock.MagicMock() + options = mock.MagicMock() + perm = 'createrepo' + users = 'user' + + # Case 1. argument error + expected = self.format_error_message( + "Please specify a permission and at least one user") + for args in [[], [perm]]: + self.assert_system_exit( + handle_revoke_permission, + options, + session, + args, + stderr=expected, + activate_session=None) + + # Case 2. user not exists + expected = self.format_error_message( + "No such user: %s" % users) + session.getUser.return_value = None + self.assert_system_exit( + handle_revoke_permission, + options, + session, + [perm, users], + stderr=expected) + + # Case 3. grant permission with --new + users = ['user1', 'user2', 'user3'] + session.getUser.side_effect = [ + {'id': 101, 'name': users[0]}, + {'id': 111, 'name': users[1]}, + {'id': 121, 'name': users[2]}, + ] + handle_revoke_permission(options, session, [perm] + users) + calls = [mock.call(p, perm) for p in users] + session.revokePermission.assert_has_calls(calls) + + def test_handle_revoke_permission_help(self): + self.assert_help( + handle_revoke_permission, + """Usage: %s revoke-permission [ ...] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_set_build_volume.py b/tests/test_cli/test_set_build_volume.py new file mode 100644 index 00000000..a02c0669 --- /dev/null +++ b/tests/test_cli/test_set_build_volume.py @@ -0,0 +1,113 @@ +from __future__ import absolute_import +import mock +import six +import unittest +from koji_cli.commands import handle_set_build_volume +from . import utils + + +class TestSetBuildVolume(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.error_format = """Usage: %s set-build-volume volume n-v-r [n-v-r ...] +(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_set_build_volume(self, activate_session_mock, stdout): + """Test handle_set_build_volume function""" + session = mock.MagicMock() + options = mock.MagicMock() + volume = 'DEFAULT' + build = 'bash-4.4.12-7.fc26' + volinfo = {'id': 0, 'name': volume} + + # Case 1. argument error + expected = self.format_error_message( + "You must provide a volume and at least one build") + self.assert_system_exit( + handle_set_build_volume, + options, + session, + [], + stderr=expected, + activate_session=None) + activate_session_mock.assert_not_called() + + # Case 2. wrong volume + session.getVolume.return_value = {} + expected = "No such volume: %s" % volume + "\n" + self.assertEqual( + 1, handle_set_build_volume(options, session, [volume, build])) + self.assert_console_message(stdout, expected) + activate_session_mock.assert_not_called() + + # Case 3. no build found + session.getVolume.return_value = volinfo + session.getBuild.return_value = {} + expected = "No such build: %s" % build + "\n" + expected += "No builds to move" + "\n" + self.assertEqual( + 1, handle_set_build_volume(options, session, [volume, build])) + self.assert_console_message(stdout, expected) + + # Case 3. Build already in volume + session.getBuild.side_effect = [ + { + "id": 1, + "name": "bash", + "version": "4.4.12", + "release": "5.fc26", + "nvr": "bash-4.4.12-5.fc26", + "volume_id": 0, + "volume_name": "DEFAULT", + }, + ] + + expected = "Build %s already on volume %s" % \ + (build, volinfo['name']) + "\n" + expected += "No builds to move" + "\n" + self.assertEqual( + 1, handle_set_build_volume(options, session, [volume, build])) + self.assert_console_message(stdout, expected) + + # Case 4. Change build volume + build = "sed-4.4-1.fc26" + build_info = { + "id": 2, + "name": "sed", + "version": "4.4", + "release": "1.fc26", + "nvr": "sed-4.4-1.fc26", + "volume_id": 1, + "volume_name": "CUSTOM", + } + + session.getBuild.side_effect = [build_info] + expected = "%s: %s -> %s" % \ + (build, build_info['volume_name'], volinfo['name']) + "\n" + handle_set_build_volume(options, session, [volume, build, '--verbose']) + self.assert_console_message(stdout, expected) + session.changeBuildVolume.assert_called_with( + build_info['id'], volinfo['id']) + + def test_handle_set_build_volume_help(self): + self.assert_help( + handle_set_build_volume, + """Usage: %s set-build-volume volume n-v-r [n-v-r ...] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit + -v, --verbose Be verbose +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_unblock_group_pkg.py b/tests/test_cli/test_unblock_group_pkg.py new file mode 100644 index 00000000..60870412 --- /dev/null +++ b/tests/test_cli/test_unblock_group_pkg.py @@ -0,0 +1,62 @@ +from __future__ import absolute_import +import mock +import unittest +from koji_cli.commands import handle_unblock_group_pkg +from . import utils + + +class TestBlockGroupPkg(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.session = mock.MagicMock() + self.options = mock.MagicMock() + self.activate_session = mock.patch('koji_cli.commands.activate_session').start() + + self.error_format = """Usage: %s unblock-group-pkg [options] [...] +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + def tearDown(self): + mock.patch.stopall() + + def test_handle_unblock_group_pkg(self): + """Test handle_unblock_group_pkg function""" + arguments = ['fedora-build', 'build'] + for pkg in [['bash'], ['sed', 'less', 'awk']]: + handle_unblock_group_pkg(self.options, self.session, arguments + pkg) + calls = [mock.call(arguments[0], arguments[1], p) for p in pkg] + self.session.groupPackageListUnblock.assert_has_calls(calls) + self.activate_session.assert_called_with(self.session, self.options) + + def test_handle_unblock_group_pkg_argument_error(self): + """Test handle_unblock_group_pkg function with wrong argument""" + expected = self.format_error_message( + "You must specify a tag name, group name, and one or more package names") + for arg in [[], ['tag'], ['tag', 'grp']]: + self.assert_system_exit( + handle_unblock_group_pkg, + self.options, + self.session, + arg, + stderr=expected, + activate_session=None) + self.activate_session.assert_not_called() + + def test_handle_unblock_group_pkg_help(self): + self.assert_help( + handle_unblock_group_pkg, + """Usage: %s unblock-group-pkg [options] [...] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_cli/test_unblock_group_req.py b/tests/test_cli/test_unblock_group_req.py new file mode 100644 index 00000000..7b7147bf --- /dev/null +++ b/tests/test_cli/test_unblock_group_req.py @@ -0,0 +1,60 @@ +from __future__ import absolute_import +import mock +import unittest +from koji_cli.commands import handle_unblock_group_req +from . import utils + + +class TestUnblockGroupReq(utils.CliTestCase): + + # Show long diffs in error output... + maxDiff = None + + def setUp(self): + self.session = mock.MagicMock() + self.options = mock.MagicMock() + self.activate_session = mock.patch('koji_cli.commands.activate_session').start() + + self.error_format = """Usage: %s unblock-group-req [options] +(Specify the --help global option for a list of other help options) + +%s: error: {message} +""" % (self.progname, self.progname) + + def tearDown(self): + mock.patch.stopall() + + def test_handle_unblock_group_req(self): + """Test handle_unblock_group_req function""" + arguments = ['fedora-build', 'build', 'srpm-build'] + handle_unblock_group_req(self.options, self.session, arguments) + self.session.groupReqListUnblock.assert_called_with(*arguments) + self.activate_session.assert_called_with(self.session, self.options) + + def test_handle_unblock_group_req_argument_error(self): + """Test handle_unblock_group_req function with wrong argument""" + expected = self.format_error_message( + "You must specify a tag name and two group names") + for arg in [[], ['tag'], ['tag', 'grp', 'opt1', 'opt2']]: + self.assert_system_exit( + handle_unblock_group_req, + self.options, + self.session, + arg, + stderr=expected, + activate_session=None) + self.activate_session.assert_not_called() + + def test_handle_unblock_group_req_help(self): + self.assert_help( + handle_unblock_group_req, + """Usage: %s unblock-group-req [options] +(Specify the --help global option for a list of other help options) + +Options: + -h, --help show this help message and exit +""" % self.progname) + + +if __name__ == '__main__': + unittest.main()