From ef3b00ed254eb82de377494fc652b3f7d28c789e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sun, 4 Jul 2021 03:28:34 +0200 Subject: [PATCH] Convert tests to use stdlib's AsyncMock() asynctest doesn't work with Python 3.8+, but AsyncMock() and few other parts are available in the standard library already. See Martiusweb/asynctest#144 and Martiusweb/asynctest#126 --- qubesadmin/tests/tools/__init__.py | 1 - qubesadmin/tests/tools/qvm_backup.py | 3 +-- qubesadmin/tests/tools/qvm_shutdown.py | 7 +++---- qubesadmin/tests/tools/qvm_start_daemon.py | 18 +++++------------- 4 files changed, 9 insertions(+), 20 deletions(-) diff --git a/qubesadmin/tests/tools/__init__.py b/qubesadmin/tests/tools/__init__.py index f9125aa8..7648c353 100644 --- a/qubesadmin/tests/tools/__init__.py +++ b/qubesadmin/tests/tools/__init__.py @@ -80,6 +80,5 @@ def readuntil(self, delim): self.current_event = rest return data + delim - @asyncio.coroutine def __call__(self, vm=None): return self, (lambda: None) diff --git a/qubesadmin/tests/tools/qvm_backup.py b/qubesadmin/tests/tools/qvm_backup.py index 20634df5..47e053f8 100644 --- a/qubesadmin/tests/tools/qvm_backup.py +++ b/qubesadmin/tests/tools/qvm_backup.py @@ -22,7 +22,6 @@ import unittest.mock as mock import asyncio -import asynctest import qubesadmin.tests import qubesadmin.tests.tools @@ -178,7 +177,7 @@ def test_012_main_existing_profile(self, mock_getpass, mock_input): None)] = \ b'0\0' try: - mock_events = asynctest.CoroutineMock() + mock_events = mock.AsyncMock() patch = mock.patch( 'qubesadmin.events.EventsDispatcher._get_events_reader', mock_events) diff --git a/qubesadmin/tests/tools/qvm_shutdown.py b/qubesadmin/tests/tools/qvm_shutdown.py index 963a303f..b20dd0bf 100644 --- a/qubesadmin/tests/tools/qvm_shutdown.py +++ b/qubesadmin/tests/tools/qvm_shutdown.py @@ -18,7 +18,6 @@ # You should have received a copy of the GNU Lesser General Public License along # with this program; if not, see . import asyncio -import asynctest import unittest.mock import qubesadmin.tests @@ -87,7 +86,7 @@ def test_010_wait(self): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) - mock_events = asynctest.CoroutineMock() + mock_events = unittest.mock.AsyncMock() patch = unittest.mock.patch( 'qubesadmin.events.EventsDispatcher._get_events_reader', mock_events) @@ -118,7 +117,7 @@ def test_012_wait_all(self): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) - mock_events = asynctest.CoroutineMock() + mock_events = unittest.mock.AsyncMock() patch = unittest.mock.patch( 'qubesadmin.events.EventsDispatcher._get_events_reader', mock_events) @@ -165,7 +164,7 @@ def test_015_wait_all_kill_timeout(self): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) - mock_events = asynctest.CoroutineMock() + mock_events = unittest.mock.AsyncMock() patch = unittest.mock.patch( 'qubesadmin.events.EventsDispatcher._get_events_reader', mock_events) diff --git a/qubesadmin/tests/tools/qvm_start_daemon.py b/qubesadmin/tests/tools/qvm_start_daemon.py index 743c3d8e..265d4e6a 100644 --- a/qubesadmin/tests/tools/qvm_start_daemon.py +++ b/qubesadmin/tests/tools/qvm_start_daemon.py @@ -25,8 +25,6 @@ import re import asyncio -import asynctest - import qubesadmin.tests import qubesadmin.tools.qvm_start_daemon from qubesadmin.tools.qvm_start_daemon import GUI_DAEMON_OPTIONS @@ -208,7 +206,7 @@ def test_013_common_args_guid_config(self): } ''') - @asynctest.patch('asyncio.create_subprocess_exec') + @unittest.mock.patch('asyncio.create_subprocess_exec') def test_020_start_gui_for_vm(self, proc_mock): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) @@ -243,7 +241,7 @@ def test_020_start_gui_for_vm(self, proc_mock): self.assertAllCalled() - @asynctest.patch('asyncio.create_subprocess_exec') + @unittest.mock.patch('asyncio.create_subprocess_exec') def test_021_start_gui_for_vm_hvm(self, proc_mock): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) @@ -316,7 +314,7 @@ def test_022_start_gui_for_vm_hvm_stubdom(self): pidfile.flush() self.addCleanup(pidfile.close) - patch_proc = asynctest.patch('asyncio.create_subprocess_exec') + patch_proc = unittest.mock.patch('asyncio.create_subprocess_exec') patch_monitor_layout = unittest.mock.patch.object( qubesadmin.tools.qvm_start_daemon, 'get_monitor_layout', @@ -363,10 +361,7 @@ def test_030_start_gui_for_stubdomain(self): ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated', None)] = \ b'2\x00QubesFeatureNotFoundError\x00\x00Feature not set\x00' - proc_mock = unittest.mock.Mock() - with asynctest.patch('asyncio.create_subprocess_exec', - lambda *args: self.mock_coroutine(proc_mock, - *args)): + with unittest.mock.patch('asyncio.create_subprocess_exec') as proc_mock: with unittest.mock.patch.object(self.launcher, 'common_guid_args', lambda vm: []): loop.run_until_complete(self.launcher.start_gui_for_stubdomain( @@ -397,10 +392,7 @@ def test_031_start_gui_for_stubdomain_forced(self): ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'gui-emulated', None)] = \ b'0\x001' - proc_mock = unittest.mock.Mock() - with asynctest.patch('asyncio.create_subprocess_exec', - lambda *args: self.mock_coroutine(proc_mock, - *args)): + with unittest.mock.patch('asyncio.create_subprocess_exec') as proc_mock: with unittest.mock.patch.object(self.launcher, 'common_guid_args', lambda vm: []): loop.run_until_complete(self.launcher.start_gui_for_stubdomain(