From 57d7dbf8b8c288fb84c697b2ed79d4870d4f97dc Mon Sep 17 00:00:00 2001 From: Ralph Bean Date: Sun, 5 Jun 2016 21:01:50 -0400 Subject: [PATCH] The beginnings of some cli tests. --- .gitignore | 1 + tests/test_cli/__init__.py | 0 tests/test_cli/data/list-commands-admin.txt | 71 +++++++++++ tests/test_cli/data/list-commands.txt | 133 ++++++++++++++++++++ tests/test_cli/test_list_commands.py | 60 +++++++++ tests/test_cli/test_runroot.py | 71 +++++++++++ 6 files changed, 336 insertions(+) create mode 100644 tests/test_cli/__init__.py create mode 100644 tests/test_cli/data/list-commands-admin.txt create mode 100644 tests/test_cli/data/list-commands.txt create mode 100644 tests/test_cli/test_list_commands.py create mode 100644 tests/test_cli/test_runroot.py diff --git a/.gitignore b/.gitignore index e28848bf..b0751967 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ tests/test.py .coverage docs/build/ +cli/kojic diff --git a/tests/test_cli/__init__.py b/tests/test_cli/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_cli/data/list-commands-admin.txt b/tests/test_cli/data/list-commands-admin.txt new file mode 100644 index 00000000..f6f7db24 --- /dev/null +++ b/tests/test_cli/data/list-commands-admin.txt @@ -0,0 +1,71 @@ +Available commands: + +admin commands: + add-external-repo Create an external repo and/or add one to a tag + add-group Add a group to a tag + add-group-pkg Add a package to a group's package listing + add-group-req Add a group to a group's required list + add-host Add a host + add-host-to-channel Add a host to a channel + add-pkg Add a package to the listing for tag + add-tag Add a new tag to the database + add-tag-inheritance Add to a tag's inheritance + add-target Create a new build target + add-user Add a user + add-volume Add a new storage volume + block-group-pkg Block a package from a group's package listing + 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-host Mark one or more hosts as disabled + disable-user Disable logins by a user + edit-external-repo Edit data for an external repo + edit-host Edit a host + edit-tag Alter tag information + 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 + enable-host Mark one or more hosts as enabled + enable-user Enable logins by a user + free-task Free a task + grant-cg-access Add a user to a content generator + grant-permission Grant a permission to a user + import Import externally built RPMs into the database + import-archive Import an archive file and associate it with a build + import-cg Import external builds with rich metadata + import-in-place Import RPMs that are already in place + import-sig Import signatures into the database + list-permissions List user permissions + list-signed List signed copies of rpms + lock-tag Lock a tag + make-task Create an arbitrary task + prune-signed-copies Prune signed copies + regen-repo Force a repo to be regenerated + remove-channel Remove a channel entirely + remove-external-repo Remove an external repo from a tag or tags, or remove entirely + remove-host-from-channel Remove a host from a channel + remove-pkg Remove a package from the listing for tag + remove-tag Remove a tag + remove-tag-inheritance Remove a tag inheritance link + remove-target Remove a build target + rename-channel Rename a channel + restart-hosts Restart enabled hosts + revoke-cg-access Remove a user from a content generator + revoke-permission Revoke a permission from a user + runroot Run a command in a buildroot + set-build-volume Move a build to a different volume + set-pkg-arches Set the list of extra arches for a package + set-pkg-owner Set the owner for a package + set-pkg-owner-global Set the owner for a package globally + set-task-priority Set task priority + spin-livemedia Create a livemedia image given a kickstart file + unblock-group-pkg Unblock a package from a group's package listing + unblock-group-req Unblock a group's requirement listing + unblock-pkg Unblock a package in the listing for tag + unlock-tag Unlock a tag + write-signed-rpm Write signed RPMs to disk + +Try "koji --help" for help about global options +Try "koji help" to get all available commands +Try "koji --help" for help about the options of a particular command +Try "koji help " to get commands under a particular category +Available categories are: admin, all, bind, build, download, info, misc, monitor, search diff --git a/tests/test_cli/data/list-commands.txt b/tests/test_cli/data/list-commands.txt new file mode 100644 index 00000000..d2b991f3 --- /dev/null +++ b/tests/test_cli/data/list-commands.txt @@ -0,0 +1,133 @@ +Available commands: + +admin commands: + add-external-repo Create an external repo and/or add one to a tag + add-group Add a group to a tag + add-group-pkg Add a package to a group's package listing + add-group-req Add a group to a group's required list + add-host Add a host + add-host-to-channel Add a host to a channel + add-pkg Add a package to the listing for tag + add-tag Add a new tag to the database + add-tag-inheritance Add to a tag's inheritance + add-target Create a new build target + add-user Add a user + add-volume Add a new storage volume + block-group-pkg Block a package from a group's package listing + 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-host Mark one or more hosts as disabled + disable-user Disable logins by a user + edit-external-repo Edit data for an external repo + edit-host Edit a host + edit-tag Alter tag information + 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 + enable-host Mark one or more hosts as enabled + enable-user Enable logins by a user + free-task Free a task + grant-cg-access Add a user to a content generator + grant-permission Grant a permission to a user + import Import externally built RPMs into the database + import-archive Import an archive file and associate it with a build + import-cg Import external builds with rich metadata + import-in-place Import RPMs that are already in place + import-sig Import signatures into the database + list-permissions List user permissions + list-signed List signed copies of rpms + lock-tag Lock a tag + make-task Create an arbitrary task + prune-signed-copies Prune signed copies + regen-repo Force a repo to be regenerated + remove-channel Remove a channel entirely + remove-external-repo Remove an external repo from a tag or tags, or remove entirely + remove-host-from-channel Remove a host from a channel + remove-pkg Remove a package from the listing for tag + remove-tag Remove a tag + remove-tag-inheritance Remove a tag inheritance link + remove-target Remove a build target + rename-channel Rename a channel + restart-hosts Restart enabled hosts + revoke-cg-access Remove a user from a content generator + revoke-permission Revoke a permission from a user + runroot Run a command in a buildroot + set-build-volume Move a build to a different volume + set-pkg-arches Set the list of extra arches for a package + set-pkg-owner Set the owner for a package + set-pkg-owner-global Set the owner for a package globally + set-task-priority Set task priority + spin-livemedia Create a livemedia image given a kickstart file + unblock-group-pkg Unblock a package from a group's package listing + unblock-group-req Unblock a group's requirement listing + unblock-pkg Unblock a package in the listing for tag + unlock-tag Unlock a tag + write-signed-rpm Write signed RPMs to disk + +bind commands: + move-build 'Move' one or more builds between tags + tag-build Apply a tag to one or more builds + untag-build Remove a tag from one or more builds + +build commands: + build Build a package from source + cancel Cancel tasks and/or builds + chain-build Build one or more packages from source + image-build Create a disk image given an install tree + image-build-indirection Create a disk image using other disk images via the Indirection plugin + maven-build Build a Maven package from source + maven-chain Run a set of Maven builds in dependency order + resubmit Retry a canceled or failed task, using the same parameter as the original task. + spin-appliance Create an appliance given a kickstart file + spin-livecd Create a live CD image given a kickstart file + win-build Build a Windows package from source + wrapper-rpm Build wrapper rpms for any archives associated with a build. + +download commands: + download-build Download a built package + download-logs Download a logs for package + download-task Download the output of a build task + +info commands: + buildinfo Print basic information about a build + help List available 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-external-repos List external repos + list-groups Print the group listings + list-history Display historical data + list-hosts Print the host listing + list-pkgs Print the package listing for tag or for owner + list-tag-history Print a history of tag operations + list-tag-inheritance Print the inheritance information for a tag + list-tagged List the builds or rpms in a tag + list-tags Print the list of tags + list-targets List the build targets + list-tasks Print the list of tasks + list-untagged List untagged builds + list-volumes List storage volumes + mock-config Create a mock config + rpminfo Print basic information about an RPM + show-groups Show groups data for a tag + taginfo Print basic information about a tag + taskinfo Show information about a task + +miscellaneous commands: + call Execute an arbitrary XML-RPC call + import-comps Import group/package information from a comps file + moshimoshi Introduce yourself + +monitor commands: + wait-repo Wait for a repo to be regenerated + watch-logs Watch logs in realtime + watch-task Track progress of particular tasks + +search commands: + search Search the system + +Try "koji --help" for help about global options +Try "koji help" to get all available commands +Try "koji --help" for help about the options of a particular command +Try "koji help " to get commands under a particular category +Available categories are: admin, all, bind, build, download, info, misc, monitor, search diff --git a/tests/test_cli/test_list_commands.py b/tests/test_cli/test_list_commands.py new file mode 100644 index 00000000..a4de0d99 --- /dev/null +++ b/tests/test_cli/test_list_commands.py @@ -0,0 +1,60 @@ +import os +import sys +import unittest + +import StringIO as stringio + +import mock + + +# We have to do this craziness because 'import koji' is ambiguous. Is it the +# koji module, or the koji cli module. Jump through hoops accordingly. +# http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path +CLI_FILENAME = os.path.dirname(__file__) + "/../../cli/koji" +if sys.version_info[0] >= 3: + import importlib.util + spec = importlib.util.spec_from_file_location("koji_cli", CLI_FILENAME) + cli = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cli) +else: + import imp + cli = imp.load_source('koji_cli', CLI_FILENAME) + + +class TestListCommands(unittest.TestCase): + def setUp(self): + self.options = mock.MagicMock() + self.session = mock.MagicMock() + self.args = mock.MagicMock() + self.original_parser = cli.OptionParser + cli.OptionParser = mock.MagicMock() + self.parser = cli.OptionParser.return_value + + def tearDown(self): + cli.OptionParser = self.original_parser + + # Show long diffs in error output... + maxDiff = None + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + def test_list_commands(self, stdout): + cli.list_commands() + actual = stdout.getvalue() + actual = actual.replace('nosetests', 'koji') + filename = os.path.dirname(__file__) + '/data/list-commands.txt' + with open(filename, 'rb') as f: + expected = f.read().decode('ascii') + self.assertMultiLineEqual(actual, expected) + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + def test_handle_admin_help(self, stdout): + options, arguments = mock.MagicMock(), mock.MagicMock() + options.admin = True + self.parser.parse_args.return_value = [options, arguments] + cli.handle_help(self.options, self.session, self.args) + actual = stdout.getvalue() + actual = actual.replace('nosetests', 'koji') + filename = os.path.dirname(__file__) + '/data/list-commands-admin.txt' + with open(filename, 'rb') as f: + expected = f.read().decode('ascii') + self.assertMultiLineEqual(actual, expected) diff --git a/tests/test_cli/test_runroot.py b/tests/test_cli/test_runroot.py new file mode 100644 index 00000000..2dd473ed --- /dev/null +++ b/tests/test_cli/test_runroot.py @@ -0,0 +1,71 @@ +import os +import sys +import unittest + +import StringIO as stringio + +import mock + + +# We have to do this craziness because 'import koji' is ambiguous. Is it the +# koji module, or the koji cli module. Jump through hoops accordingly. +# http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path +CLI_FILENAME = os.path.dirname(__file__) + "/../../cli/koji" +if sys.version_info[0] >= 3: + import importlib.util + spec = importlib.util.spec_from_file_location("koji_cli", CLI_FILENAME) + cli = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cli) +else: + import imp + cli = imp.load_source('koji_cli', CLI_FILENAME) + + +class TestListCommands(unittest.TestCase): + def setUp(self): + self.options = mock.MagicMock() + self.session = mock.MagicMock() + 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 + + # Show long diffs in error output... + maxDiff = None + + @mock.patch('sys.stdout', new_callable=stringio.StringIO) + def test_handle_runroot(self, stdout): + tag = 'tag' + arch = 'arch' + command = 'command' + arguments = [tag, arch, command] + options = mock.MagicMock() + options.admin = True + self.parser.parse_args.return_value = [options, arguments] + + # Mock out the xmlrpc server + self.session.getTaskInfo.return_value = {'state': 1} + self.session.downloadTaskOutput.return_value = 'task output' + self.session.listTaskOutput.return_value = ['runroot.log'] + self.session.runroot.return_value = 1 + + # Run it and check immediate output + cli.handle_runroot(self.options, self.session, self.args) + actual = stdout.getvalue() + actual = actual.replace('nosetests', 'koji') + expected = 'successfully connected to hub\n1\ntask output' + self.assertMultiLineEqual(actual, expected) + + # Finally, assert that things were called as we expected. + self.session.getTaskInfo.assert_called_once_with(1) + self.session.listTaskOutput.assert_called_once_with(1) + self.session.downloadTaskOutput.assert_called_once_with(1, 'runroot.log') + self.session.runroot.assert_called_once_with( + tag, arch, command, repo_id=mock.ANY, weight=mock.ANY, + mounts=mock.ANY, packages=mock.ANY, skip_setarch=mock.ANY, + channel=mock.ANY, + )