fix unit test for runroot cli plugin

This commit is contained in:
Mike McLean 2019-03-01 13:04:12 -05:00
parent 2b5f953430
commit 89b6c01c47
2 changed files with 30 additions and 10 deletions

View file

@ -90,6 +90,7 @@ def handle_runroot(options, session, args):
print("User interrupt: canceling runroot task") print("User interrupt: canceling runroot task")
session.cancelTask(task_id) session.cancelTask(task_id)
raise raise
sys.stdout.flush()
if not opts.quiet: if not opts.quiet:
output = list_task_output_all_volumes(session, task_id) output = list_task_output_all_volumes(session, task_id)
if 'runroot.log' in output: if 'runroot.log' in output:

View file

@ -1,6 +1,8 @@
from __future__ import absolute_import from __future__ import absolute_import
import io
import mock import mock
import six import six
import threading
try: try:
import unittest2 as unittest import unittest2 as unittest
except ImportError: except ImportError:
@ -16,6 +18,23 @@ class ParserError(Exception):
pass pass
def mock_stdout():
def get_mock():
if six.PY2:
return six.StringIO()
else:
return io.TextIOWrapper(six.BytesIO())
return mock.patch('sys.stdout', new_callable=get_mock)
def get_stdout_value(stdout):
if six.PY2:
return stdout.getvalue()
else:
# we have to force the TextIOWrapper to stop buffering
return stdout.detach().getvalue()
class TestListCommands(unittest.TestCase): class TestListCommands(unittest.TestCase):
def setUp(self): def setUp(self):
@ -56,21 +75,21 @@ class TestListCommands(unittest.TestCase):
maxDiff = None maxDiff = None
@mock.patch('time.sleep') @mock.patch('time.sleep')
@mock.patch('sys.stdout', new_callable=six.StringIO) @mock_stdout()
def test_handle_runroot(self, stdout, sleep): def test_handle_runroot(self, stdout, sleep):
# Mock out the xmlrpc server # Mock out the xmlrpc server
self.session.getTaskInfo.return_value = {'state': 1} self.session.getTaskInfo.return_value = {'state': 1}
self.session.downloadTaskOutput.return_value = 'task output' self.session.downloadTaskOutput.return_value = six.b('task output')
self.session.listTaskOutput.return_value = {'runroot.log': ['DEFAULT']} self.session.listTaskOutput.return_value = {'runroot.log': ['DEFAULT']}
self.session.runroot.return_value = 1 self.session.runroot.return_value = 1
self.session.taskFinished.side_effect = [False, True] self.session.taskFinished.side_effect = [False, True]
# Run it and check immediate output # Run it and check immediate output
runroot.handle_runroot(self.options, self.session, self.args) runroot.handle_runroot(self.options, self.session, self.args)
actual = stdout.getvalue() actual = get_stdout_value(stdout)
actual = actual.replace('nosetests', 'koji') actual = actual.replace(b'nosetests', b'koji')
expected = '1\ntask output' expected = b'1\ntask output'
self.assertMultiLineEqual(actual, expected) self.assertEqual(actual, expected)
# Finally, assert that things were called as we expected. # Finally, assert that things were called as we expected.
self.session.getTaskInfo.assert_called_once_with(1) self.session.getTaskInfo.assert_called_once_with(1)
@ -141,7 +160,7 @@ class TestListCommands(unittest.TestCase):
with self.assertRaises(koji.GenericError): with self.assertRaises(koji.GenericError):
runroot.handle_runroot(self.options, self.session, args) runroot.handle_runroot(self.options, self.session, args)
@mock.patch('sys.stdout', new_callable=six.StringIO) @mock_stdout()
def test_missing_plugin(self, stdout): def test_missing_plugin(self, stdout):
args = ['--nowait', 'TAG', 'ARCH', 'COMMAND'] args = ['--nowait', 'TAG', 'ARCH', 'COMMAND']
@ -151,7 +170,7 @@ class TestListCommands(unittest.TestCase):
with self.assertRaises(koji.GenericError): with self.assertRaises(koji.GenericError):
runroot.handle_runroot(self.options, self.session, args) runroot.handle_runroot(self.options, self.session, args)
actual = stdout.getvalue().strip() actual = get_stdout_value(stdout).strip()
self.assertEqual(actual, self.assertEqual(actual,
"* The runroot plugin appears to not be installed on the" b"* The runroot plugin appears to not be installed on the"
" koji hub. Please contact the administrator.") b" koji hub. Please contact the administrator.")