update cli tests

This commit is contained in:
Tomas Kopecek 2017-06-06 18:21:49 +02:00
parent ac3f76a4de
commit c02eb739aa
30 changed files with 667 additions and 695 deletions

View file

@ -0,0 +1,28 @@
import os
import sys
def load_plugin(plugin_type, plugin_name):
# 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.
# https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path
mod_name = "%s_%s" % (plugin_name, plugin_type)
CLI_FILENAME = os.path.join(
os.path.dirname(__file__),
"../../plugins",
plugin_type,
"%s.py" % plugin_name)
sys.path = [os.path.dirname(CLI_FILENAME),
os.path.join(os.path.dirname(__file__), "../..", plugin_type)] + \
sys.path
if sys.version_info[0] >= 3:
import importlib.machinery
loader = importlib.machinery.SourceFileLoader(mod_name, CLI_FILENAME)
spec = importlib.util.spec_from_loader(loader.name, loader)
kojid = importlib.util.module_from_spec(spec)
spec.loader.exec_module(kojid)
loader.exec_module(kojid)
sys.modules[mod_name] = kojid
else:
import imp
plugin = imp.load_source(mod_name, CLI_FILENAME)
return plugin

View file

@ -0,0 +1,56 @@
from __future__ import absolute_import
import mock
import six
import unittest
import koji
from . import load_plugin
runroot = load_plugin.load_plugin('cli', 'runroot')
class TestListCommands(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()
runroot.OptionParser = mock.MagicMock()
self.parser = runroot.OptionParser.return_value
# Show long diffs in error output...
maxDiff = None
@mock.patch('sys.stdout', new_callable=six.StringIO)
def test_handle_runroot(self, stdout):
tag = 'tag'
arch = 'arch'
command = 'command'
arguments = [tag, arch, command]
options = mock.MagicMock()
options.new_chroot = False
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': ['DEFAULT']}
self.session.runroot.return_value = 1
# Run it and check immediate output
runroot.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, all_volumes=True)
self.session.downloadTaskOutput.assert_called_once_with(
1, 'runroot.log', volume='DEFAULT')
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,
)

View file

@ -0,0 +1,155 @@
import mock
import StringIO
import unittest
import koji
import load_plugin
save_failed_tree = load_plugin.load_plugin('cli', 'save_failed_tree')
class TestSaveFailedTree(unittest.TestCase):
def setUp(self):
self.session = mock.MagicMock()
self.args = mock.MagicMock()
self.parser = mock.MagicMock()
save_failed_tree.activate_session = mock.MagicMock()
save_failed_tree.OptionParser = mock.MagicMock()
save_failed_tree.watch_tasks = mock.MagicMock()
self.parser = save_failed_tree.OptionParser.return_value
# Show long diffs in error output...
maxDiff = None
def test_handle_save_failed_tree_simple(self ):
# koji save-failed-tree 123456
task_id = 123456
broot_id = 321
arguments = [task_id]
options = mock.MagicMock()
options.full = False
options.nowait = True
self.parser.parse_args.return_value = [options, arguments]
self.session.getAPIVersion.return_value = koji.API_VERSION
self.session.listBuildroots.return_value = [{'id': 321}]
# Mock out the xmlrpc server
self.session.saveFailedTree.return_value = 123
# Run it and check immediate output
save_failed_tree.handle_save_failed_tree(options, self.session, self.args)
# Finally, assert that things were called as we expected.
save_failed_tree.activate_session.assert_called_once_with(self.session, options)
self.session.listBuildroots.assert_called_once_with(taskID=task_id)
self.session.saveFailedTree.assert_called_once_with(broot_id, options.full)
def test_handle_save_failed_tree_buildroots(self):
# koji save-failed-tree --buildroot 123456
broot_id = 321
arguments = [broot_id]
options = mock.MagicMock()
options.full = False
options.nowait = True
options.mode = "buildroot"
self.parser.parse_args.return_value = [options, arguments]
self.session.getAPIVersion.return_value = koji.API_VERSION
self.session.listBuildroots.return_value = [{'id': 321}]
# Mock out the xmlrpc server
self.session.saveFailedTree.return_value = 123
# Run it and check immediate output
save_failed_tree.handle_save_failed_tree(options, self.session, self.args)
# Finally, assert that things were called as we expected.
save_failed_tree.activate_session.assert_called_once_with(self.session, options)
self.session.listBuildroots.assert_not_called()
self.session.saveFailedTree.assert_called_once_with(broot_id, options.full)
def test_handle_save_failed_tree_full(self):
# koji save-failed-tree 123456 --full
task_id = 123456
broot_id = 321
arguments = [task_id]
options = mock.MagicMock()
options.full = True
options.nowait = True
self.parser.parse_args.return_value = [options, arguments]
self.session.getAPIVersion.return_value = koji.API_VERSION
self.session.listBuildroots.return_value = [{'id': 321}]
# Mock out the xmlrpc server
self.session.saveFailedTree.return_value = 123
# Run it and check immediate output
save_failed_tree.handle_save_failed_tree(options, self.session, self.args)
# Finally, assert that things were called as we expected.
save_failed_tree.activate_session.assert_called_once_with(self.session, options)
self.session.listBuildroots.assert_called_once_with(taskID=task_id)
self.session.saveFailedTree.assert_called_once_with(broot_id, options.full)
def test_handle_save_failed_tree_wait(self):
# koji save-failed-tree 123456 --full
task_id = 123456
broot_id = 321
arguments = [task_id]
options = mock.MagicMock()
options.full = True
options.nowait = False
options.quiet = False
self.parser.parse_args.return_value = [options, arguments]
self.session.getAPIVersion.return_value = koji.API_VERSION
self.session.listBuildroots.return_value = [{'id': 321}]
# Mock out the xmlrpc server
spawned_id = 123
self.session.saveFailedTree.return_value = spawned_id
# Run it and check immediate output
save_failed_tree.handle_save_failed_tree(options, self.session, self.args)
# Finally, assert that things were called as we expected.
self.session.listBuildroots.assert_called_once_with(taskID=task_id)
self.session.saveFailedTree.assert_called_once_with(broot_id, options.full)
save_failed_tree.activate_session.assert_called_once_with(self.session, options)
self.session.logout.assert_called_once_with()
save_failed_tree.watch_tasks.assert_called_once_with(self.session, [spawned_id],
poll_interval=options.poll_interval,
quiet=options.quiet)
@mock.patch('sys.stdout', new_callable=StringIO.StringIO)
def test_handle_save_failed_tree_errors(self, stdout):
# koji save-failed-tree 123 456
arguments = [123, 456]
options = mock.MagicMock()
self.parser.parse_args.return_value = [options, arguments]
self.parser.error.side_effect = Exception()
self.session.getAPIVersion.return_value = koji.API_VERSION
self.session.listBuildroots.return_value = [{'id': 321}]
self.assertRaises(Exception, save_failed_tree.handle_save_failed_tree,
options, self.session, self.args)
arguments = ["text"]
self.parser.parse_args.return_value = [options, arguments]
self.assertRaises(Exception, save_failed_tree.handle_save_failed_tree, options,
self.session, self.args)
save_failed_tree.logger = mock.MagicMock()
# plugin not installed
arguments = [123]
self.parser.parse_args.return_value = [options, arguments]
self.session.saveFailedTree.side_effect = koji.GenericError("Invalid method")
save_failed_tree.handle_save_failed_tree(options, self.session, self.args)
actual = stdout.getvalue()
self.assertTrue('The save_failed_tree plugin appears to not be installed' in actual)
# Task which is not FAILED, disabled in config, wrong owner
self.session.saveFailedTree.side_effect = koji.PreBuildError('placeholder')
with self.assertRaises(koji.PreBuildError) as cm:
save_failed_tree.handle_save_failed_tree(options, self.session, self.args)
e = cm.exception
self.assertEqual(e, self.session.saveFailedTree.side_effect)