From c8a27e525a1b820bd68cf8fdd29c54b3751bd63a Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Tue, 22 Oct 2024 23:51:50 +0800 Subject: [PATCH] unittest: enabling tests/test_lib for py2 --- tests/test_lib/test_client_session.py | 4 ++++ tests/test_lib/test_gen_mock_config.py | 4 ++-- tests/test_lib/test_plugin.py | 6 +++--- tests/test_lib/test_spliced_sig.py | 3 +++ tests/test_lib/test_tasks.py | 20 ++++++++++++++++---- tests/test_lib/test_utils.py | 7 +++++-- tests/test_lib/test_xmlrpcplus.py | 13 +++++++++++-- tox.ini | 2 +- 8 files changed, 45 insertions(+), 14 deletions(-) diff --git a/tests/test_lib/test_client_session.py b/tests/test_lib/test_client_session.py index dda17cbc..5bb90608 100644 --- a/tests/test_lib/test_client_session.py +++ b/tests/test_lib/test_client_session.py @@ -237,6 +237,10 @@ class TestMultiCall(unittest.TestCase): def test_MultiCallHack_weakref_validation(self): expected_exc = 'The session parameter must be a weak reference' + if six.PY2: + with self.assertRaisesRegexp(TypeError, expected_exc): + koji.MultiCallHack(self.ksession) + return with self.assertRaisesRegex(TypeError, expected_exc): koji.MultiCallHack(self.ksession) diff --git a/tests/test_lib/test_gen_mock_config.py b/tests/test_lib/test_gen_mock_config.py index ee1b895c..af699d5f 100644 --- a/tests/test_lib/test_gen_mock_config.py +++ b/tests/test_lib/test_gen_mock_config.py @@ -18,10 +18,10 @@ class TestGenMockConfig(unittest.TestCase): if not fn.endswith('.data'): continue path = os.path.join(datadir, fn) - with open(path, 'rt', encoding='utf-8') as fo: + with open(path, 'rt') as fo: s = fo.read() params = ast.literal_eval(s) - with open(path[:-5] + '.out', 'rt', encoding='utf-8') as fo: + with open(path[:-5] + '.out', 'rt') as fo: expected = fo.read() output = koji.genMockConfig(**params) self.assertMultiLineEqual(output, expected) diff --git a/tests/test_lib/test_plugin.py b/tests/test_lib/test_plugin.py index 1aec576c..24b36835 100644 --- a/tests/test_lib/test_plugin.py +++ b/tests/test_lib/test_plugin.py @@ -10,11 +10,11 @@ import sys import unittest try: - import importlib - imp = None -except ImportError: import imp importlib = None +except ImportError: + import importlib + imp = None import koji import koji.util diff --git a/tests/test_lib/test_spliced_sig.py b/tests/test_lib/test_spliced_sig.py index 107bcd8d..bcb7b50b 100644 --- a/tests/test_lib/test_spliced_sig.py +++ b/tests/test_lib/test_spliced_sig.py @@ -5,6 +5,8 @@ import shutil import tempfile import unittest +import six + import koji @@ -31,6 +33,7 @@ class TestCheckSigMD5(unittest.TestCase): self.assertEqual(contents_signed, contents_spliced) self.assertNotEqual(contents_signed, contents_orig) + @unittest.skipIf(six.PY2, "Python 2 not supported") def test_splice_rpm_sighdr(self): contents_signed = open(self.signed, 'rb').read() sighdr = koji.rip_rpm_sighdr(self.signed) diff --git a/tests/test_lib/test_tasks.py b/tests/test_lib/test_tasks.py index 7e76015f..47d900b0 100644 --- a/tests/test_lib/test_tasks.py +++ b/tests/test_lib/test_tasks.py @@ -320,9 +320,8 @@ class TasksTestCase(unittest.TestCase): @patch('time.time') @patch('time.sleep') - @patch('signal.sigtimedwait') @patch('signal.pause') - def test_BaseTaskHandler_wait_timeout(self, pause, sigtimedwait, sleep, time): + def test_BaseTaskHandler_wait_timeout(self, pause, sleep, time): """Tests timeout behavior in the wait function""" temp_path = self.get_tmp_dir_path('TaskTest') obj = TaskTest(95, 'some_method', ['random_arg'], None, None, temp_path) @@ -330,11 +329,18 @@ class TasksTestCase(unittest.TestCase): obj.session = MagicMock() obj.session.host.taskWait.return_value = [[], [99, 100, 101]] time.side_effect = list(range(0, 4000, 60)) + if six.PY3: + sigtimedwait = patch('signal.sigtimedwait').start() + try: obj.wait([99, 100, 101], timeout=3600) raise Exception('A GenericError was not raised.') except koji.GenericError as e: self.assertEqual(e.args[0][:24], 'Subtasks timed out after') + + if six.PY3: + sigtimedwait.stop() + obj.session.host.taskSetWait.assert_called_once_with(95, [99, 100, 101]) obj.session.cancelTaskChildren.assert_called_once_with(95) obj.session.getTaskResult.assert_not_called() @@ -342,9 +348,8 @@ class TasksTestCase(unittest.TestCase): @patch('time.time') @patch('time.sleep') - @patch('signal.sigtimedwait') @patch('signal.pause') - def test_BaseTaskHandler_wait_avoid_timeout(self, pause, sigtimedwait, sleep, time): + def test_BaseTaskHandler_wait_avoid_timeout(self, pause, sleep, time): """Tests that timeout does not happen if tasks finish in time""" temp_path = self.get_tmp_dir_path('TaskTest') obj = TaskTest(95, 'some_method', ['random_arg'], None, None, temp_path) @@ -359,8 +364,15 @@ class TasksTestCase(unittest.TestCase): # and then report all done taskWait_returns.append([[99, 100, 101], []]) obj.session.host.taskWait.side_effect = taskWait_returns + + if six.PY3: + sigtimedwait = patch('signal.sigtimedwait').start() + obj.wait([99, 100, 101], timeout=3600) + if six.PY3: + sigtimedwait.stop() + obj.session.host.taskSetWait.assert_called_once_with(95, [99, 100, 101]) obj.session.cancelTaskChildren.assert_not_called() pause.assert_not_called() diff --git a/tests/test_lib/test_utils.py b/tests/test_lib/test_utils.py index 9d59ba1d..f93f7f5a 100644 --- a/tests/test_lib/test_utils.py +++ b/tests/test_lib/test_utils.py @@ -758,7 +758,7 @@ class MavenUtilTestCase(unittest.TestCase): def _read_conf(self, cfile): path = os.path.dirname(__file__) - with open(path + cfile, 'rt', encoding='utf-8') as conf_file: + with open(path + cfile, 'rt') as conf_file: if six.PY2: config = six.moves.configparser.SafeConfigParser() config.readfp(conf_file) @@ -1513,7 +1513,10 @@ class TestRmtree(unittest.TestCase): rmtree_nofork.assert_called_once() self.assertEqual(rmtree_nofork.call_args[0][0], path) _exit.assert_called_once() - logger = rmtree_nofork.call_args.kwargs['logger'] + if mock.__package__ == 'unittest': + logger = rmtree_nofork.call_args.kwargs['logger'] + else: + logger = rmtree_nofork.call_args[1]['logger'] @mock.patch('tempfile.mkstemp') # avoid stray temp file @mock.patch('koji.util._rmtree_nofork') diff --git a/tests/test_lib/test_xmlrpcplus.py b/tests/test_lib/test_xmlrpcplus.py index af7ff305..f7454e0e 100644 --- a/tests/test_lib/test_xmlrpcplus.py +++ b/tests/test_lib/test_xmlrpcplus.py @@ -1,10 +1,16 @@ # coding=utf-8 from __future__ import absolute_import -from six.moves import range -import unittest + import re +import sys +import unittest +try: + from unittest.mock import ANY +except ImportError: + from mock import ANY from six.moves import xmlrpc_client + from koji import xmlrpcplus @@ -98,6 +104,9 @@ class TestDump(unittest.TestCase): 'RegexNameInternal.compiled': re.compile('^[A-Za-z0-9/_.+-]+$')} dist_data_output = ({'MaxNameLengthInternal': 15, 'RegexNameInternal.compiled': "re.compile('^[A-Za-z0-9/_.+-]+$')"},) + if sys.version_info < (3, 7): + dist_data_output[0]['RegexNameInternal.compiled'] = ANY + dict_data = (dict_data,) enc = xmlrpcplus.dumps(dict_data, methodresponse=1) params, method = xmlrpc_client.loads(enc) diff --git a/tox.ini b/tox.ini index 5b56a3a3..4cb79cb6 100644 --- a/tox.ini +++ b/tox.ini @@ -57,7 +57,7 @@ commands_pre = {envbindir}/coverage2 erase commands = {envbindir}/coverage2 run --source . -m pytest {posargs:\ - tests/test_builder tests/test_cli \ + tests/test_builder tests/test_cli tests/test_lib \ tests/test_plugins/test_runroot_builder.py \ tests/test_plugins/test_save_failed_tree_builder.py \ tests/test_plugins/test_runroot_cli.py \