Skip to content

Commit 4539f63

Browse files
committed
q-dev: update device tests
1 parent be25600 commit 4539f63

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

qubes/tests/devices.py

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def dev_testclass_list_attached(self, event, persistent=False):
7373
def dev_testclass_list(self, event):
7474
yield self.device
7575

76+
@qubes.events.handler('device-get:testclass')
77+
def dev_testclass_get(self, event, **kwargs):
78+
yield self.device
79+
7680
def is_halted(self):
7781
return not self.running
7882

qubes/tests/devices_block.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,10 @@ def test_069_on_qdb_change_removed_attached(self):
943943
('device-removed:block', frozenset({('port', exp_dev.port)}))],
944944
1)
945945

946-
@unittest.mock.patch('subprocess.Popen')
947-
def test_070_on_qdb_change_two_fronts_failed(self, mock_confirm):
946+
# with `new_callable=Mock` we override async function with synchronous Mock
947+
@unittest.mock.patch(
948+
'qubes.ext.utils.confirm_device_attachment', new_callable=Mock)
949+
def test_070_on_qdb_change_two_fronts_failed(self, _mock_confirm):
948950
back, front = self.added_assign_setup()
949951

950952
exp_dev = qubes.ext.block.BlockDevice(back, 'sda')
@@ -954,18 +956,20 @@ def test_070_on_qdb_change_two_fronts_failed(self, mock_confirm):
954956
back.devices['block']._assigned.append(assign)
955957
back.devices['block']._exposed.append(exp_dev)
956958

957-
proc = Mock()
958-
proc.communicate = Mock()
959-
proc.communicate.return_value = (b'nonsense', b'')
960-
mock_confirm.return_value = proc
961959
self.ext.attach_and_notify = Mock()
962-
with mock.patch('asyncio.ensure_future'):
960+
961+
with mock.patch('qubes.ext.utils.asyncio.ensure_future') as future:
962+
future.return_value = Mock()
963+
future.return_value.result = Mock()
964+
future.return_value.result.return_value = "nonsense"
963965
self.ext.on_qdb_change(back, None, None)
964-
proc.communicate.assert_called_once()
966+
965967
self.ext.attach_and_notify.assert_not_called()
966968

967-
@unittest.mock.patch('subprocess.Popen')
968-
def test_071_on_qdb_change_two_fronts(self, mock_confirm):
969+
# with `new_callable=Mock` we override async function with synchronous Mock
970+
@unittest.mock.patch(
971+
'qubes.ext.utils.confirm_device_attachment', new_callable=Mock)
972+
def test_071_on_qdb_change_two_fronts(self, _mock_confirm):
969973
back, front = self.added_assign_setup()
970974

971975
exp_dev = qubes.ext.block.BlockDevice(back, 'sda')
@@ -975,16 +979,15 @@ def test_071_on_qdb_change_two_fronts(self, mock_confirm):
975979
back.devices['block']._assigned.append(assign)
976980
back.devices['block']._exposed.append(exp_dev)
977981

978-
proc = Mock()
979-
proc.communicate = Mock()
980-
proc.communicate.return_value = (b'front-vm', b'')
981-
mock_confirm.return_value = proc
982982
self.ext.attach_and_notify = Mock()
983-
with mock.patch('asyncio.ensure_future'):
983+
984+
with mock.patch('asyncio.ensure_future') as future:
985+
future.return_value = Mock()
986+
future.return_value.result = Mock()
987+
future.return_value.result.return_value = "front-vm"
984988
self.ext.on_qdb_change(back, None, None)
985-
proc.communicate.assert_called_once()
986-
self.ext.attach_and_notify.assert_called_once_with(
987-
front, assign)
989+
990+
self.ext.attach_and_notify.assert_called_once_with(front, assign)
988991
# don't ask again
989992
self.assertEqual(self.ext.attach_and_notify.call_args[0][1].mode.value,
990993
'auto-attach')

0 commit comments

Comments
 (0)