Skip to content

Commit

Permalink
Convert tests to use stdlib's AsyncMock()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
marmarek committed Jul 4, 2021
1 parent fce4685 commit 1a2ce72
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
1 change: 0 additions & 1 deletion ci/requirements.txt
Expand Up @@ -9,6 +9,5 @@ mock
lxml
PyYAML
xcffib
asynctest
tqdm
pyxdg
1 change: 0 additions & 1 deletion qubesadmin/tests/tools/__init__.py
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions qubesadmin/tests/tools/qvm_backup.py
Expand Up @@ -22,7 +22,6 @@
import unittest.mock as mock

import asyncio
import asynctest

import qubesadmin.tests
import qubesadmin.tests.tools
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions qubesadmin/tests/tools/qvm_shutdown.py
Expand Up @@ -18,7 +18,6 @@
# You should have received a copy of the GNU Lesser General Public License along
# with this program; if not, see <http://www.gnu.org/licenses/>.
import asyncio
import asynctest
import unittest.mock

import qubesadmin.tests
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 5 additions & 13 deletions qubesadmin/tests/tools/qvm_start_daemon.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 1a2ce72

Please sign in to comment.