Skip to content

Commit 21cf824

Browse files
committed
q-dev: update tests
1 parent 4296c51 commit 21cf824

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

qubesusbproxy/tests.py

+56-41
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,10 @@ def test_010_on_qdb_change_multiple_assignments_including_full(self):
805805
qubesusbproxy.core3ext.USBDevice(back, '1-1'))
806806

807807
self.ext.attach_and_notify = Mock()
808-
with mock.patch('asyncio.ensure_future'):
809-
self.ext.on_qdb_change(back, None, None)
808+
loop = asyncio.get_event_loop()
809+
with mock.patch('asyncio.wait'):
810+
with mock.patch('asyncio.ensure_future'):
811+
loop.run_until_complete(self.ext.on_domain_start(front, None))
810812
self.assertEqual(self.ext.attach_and_notify.call_args[0][1].options,
811813
{'pid': 'did'})
812814

@@ -828,8 +830,10 @@ def test_011_on_qdb_change_multiple_assignments_port_vs_dev(self):
828830
qubesusbproxy.core3ext.USBDevice(back, '1-1'))
829831

830832
self.ext.attach_and_notify = Mock()
831-
with mock.patch('asyncio.ensure_future'):
832-
self.ext.on_qdb_change(back, None, None)
833+
loop = asyncio.get_event_loop()
834+
with mock.patch('asyncio.wait'):
835+
with mock.patch('asyncio.ensure_future'):
836+
loop.run_until_complete(self.ext.on_domain_start(front, None))
833837
self.assertEqual(self.ext.attach_and_notify.call_args[0][1].options,
834838
{'pid': 'any'})
835839

@@ -853,15 +857,14 @@ def test_012_on_qdb_change_multiple_assignments_dev(self):
853857
qubesusbproxy.core3ext.USBDevice(back, '1-2'))
854858

855859
self.ext.attach_and_notify = Mock()
856-
with mock.patch('asyncio.ensure_future'):
857-
self.ext.on_qdb_change(back, None, None)
860+
loop = asyncio.get_event_loop()
861+
with mock.patch('asyncio.wait'):
862+
with mock.patch('asyncio.ensure_future'):
863+
loop.run_until_complete(self.ext.on_domain_start(front, None))
858864
self.assertEqual(self.ext.attach_and_notify.call_args[0][1].options,
859865
{'any': 'did'})
860866

861-
# replace async function with sync one
862-
@unittest.mock.patch('qubes.ext.utils.confirm_device_attachment',
863-
new_callable=Mock)
864-
def test_013_on_qdb_change_two_fronts_failed(self, _confirm):
867+
def test_013_on_qdb_change_two_fronts(self):
865868
back, front = self.added_assign_setup()
866869

867870
exp_dev = qubesusbproxy.core3ext.USBDevice(back, '1-1')
@@ -871,47 +874,58 @@ def test_013_on_qdb_change_two_fronts_failed(self, _confirm):
871874
back.devices['usb']._assigned.append(assign)
872875
back.devices['usb']._exposed.append(exp_dev)
873876

874-
class quick_mock:
875-
@staticmethod
876-
def result():
877-
return "nonsense"
877+
resolver_path = 'qubes.ext.utils.resolve_conflicts_and_attach'
878+
with mock.patch(resolver_path, new_callable=Mock) as resolver:
879+
with mock.patch('asyncio.ensure_future'):
880+
self.ext.on_qdb_change(back, None, None)
881+
resolver.assert_called_once_with(
882+
self.ext, {'1-1': {front: assign, back: assign}})
878883

879-
self.ext.attach_and_notify = Mock()
880-
with mock.patch('asyncio.ensure_future') as future:
881-
future.return_value = quick_mock
882-
self.ext.on_qdb_change(back, None, None)
884+
@unittest.mock.patch('asyncio.create_subprocess_shell')
885+
def test_014_failed_confirmation(self, shell):
886+
back, front = self.added_assign_setup()
887+
888+
exp_dev = qubesusbproxy.core3ext.USBDevice(back, '1-1')
889+
assign = DeviceAssignment(exp_dev, mode='auto-attach')
890+
891+
front.devices['usb']._assigned.append(assign)
892+
back.devices['usb']._assigned.append(assign)
893+
back.devices['usb']._exposed.append(exp_dev)
894+
895+
proc = AsyncMock()
896+
shell.return_value = proc
897+
proc.communicate = AsyncMock()
898+
proc.communicate.return_value = (b'nonsense', b'')
883899

900+
loop = asyncio.get_event_loop()
901+
self.ext.attach_and_notify = AsyncMock()
902+
loop.run_until_complete(qubes.ext.utils.resolve_conflicts_and_attach(
903+
self.ext, {'1-1': {front: assign, back: assign}}))
884904
self.ext.attach_and_notify.assert_not_called()
885905

886-
# replace async function with sync one
887-
@unittest.mock.patch('qubes.ext.utils.confirm_device_attachment',
888-
new_callable=Mock)
889-
def test_014_on_qdb_change_two_fronts(self, _confirm):
906+
@unittest.mock.patch('asyncio.create_subprocess_shell')
907+
def test_015_successful_confirmation(self, shell):
890908
back, front = self.added_assign_setup()
891909

892910
exp_dev = qubesusbproxy.core3ext.USBDevice(back, '1-1')
893-
assign = DeviceAssignment(exp_dev, mode='ask-to-attach')
911+
assign = DeviceAssignment(exp_dev, mode='auto-attach')
894912

895913
front.devices['usb']._assigned.append(assign)
896914
back.devices['usb']._assigned.append(assign)
897915
back.devices['usb']._exposed.append(exp_dev)
898916

899-
class quick_mock:
900-
@staticmethod
901-
def result():
902-
return "front-vm"
903-
904-
self.ext.attach_and_notify = Mock()
905-
with mock.patch('asyncio.ensure_future') as future:
906-
future.return_value = quick_mock
907-
self.ext.on_qdb_change(back, None, None)
917+
proc = AsyncMock()
918+
shell.return_value = proc
919+
proc.communicate = AsyncMock()
920+
proc.communicate.return_value = (b'front-vm', b'')
908921

922+
loop = asyncio.get_event_loop()
923+
self.ext.attach_and_notify = AsyncMock()
924+
loop.run_until_complete(qubes.ext.utils.resolve_conflicts_and_attach(
925+
self.ext, {'1-1': {front: assign, back: assign}}))
909926
self.ext.attach_and_notify.assert_called_once_with(front, assign)
910-
# don't ask again
911-
self.assertEqual(self.ext.attach_and_notify.call_args[0][1].mode.value,
912-
'auto-attach')
913927

914-
def test_015_on_qdb_change_ask(self):
928+
def test_016_on_qdb_change_ask(self):
915929
back, front = self.added_assign_setup()
916930

917931
exp_dev = qubesusbproxy.core3ext.USBDevice(back, '1-1')
@@ -920,11 +934,12 @@ def test_015_on_qdb_change_ask(self):
920934
front.devices['usb']._assigned.append(assign)
921935
back.devices['usb']._exposed.append(exp_dev)
922936

923-
self.ext.attach_and_notify = Mock()
924-
with mock.patch('asyncio.ensure_future'):
925-
self.ext.on_qdb_change(back, None, None)
926-
self.assertEqual(self.ext.attach_and_notify.call_args[0][1].mode.value,
927-
'ask-to-attach')
937+
resolver_path = 'qubes.ext.utils.resolve_conflicts_and_attach'
938+
with mock.patch(resolver_path, new_callable=Mock) as resolver:
939+
with mock.patch('asyncio.ensure_future'):
940+
self.ext.on_qdb_change(back, None, None)
941+
resolver.assert_called_once_with(
942+
self.ext, {'1-1': {front: assign}})
928943

929944
def test_020_on_startup_multiple_assignments_including_full(self):
930945
back, front = self.added_assign_setup()

0 commit comments

Comments
 (0)