@@ -805,8 +805,10 @@ def test_010_on_qdb_change_multiple_assignments_including_full(self):
805
805
qubesusbproxy .core3ext .USBDevice (back , '1-1' ))
806
806
807
807
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 ))
810
812
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].options ,
811
813
{'pid' : 'did' })
812
814
@@ -828,8 +830,10 @@ def test_011_on_qdb_change_multiple_assignments_port_vs_dev(self):
828
830
qubesusbproxy .core3ext .USBDevice (back , '1-1' ))
829
831
830
832
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 ))
833
837
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].options ,
834
838
{'pid' : 'any' })
835
839
@@ -853,15 +857,14 @@ def test_012_on_qdb_change_multiple_assignments_dev(self):
853
857
qubesusbproxy .core3ext .USBDevice (back , '1-2' ))
854
858
855
859
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 ))
858
864
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].options ,
859
865
{'any' : 'did' })
860
866
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 ):
865
868
back , front = self .added_assign_setup ()
866
869
867
870
exp_dev = qubesusbproxy .core3ext .USBDevice (back , '1-1' )
@@ -871,47 +874,58 @@ def test_013_on_qdb_change_two_fronts_failed(self, _confirm):
871
874
back .devices ['usb' ]._assigned .append (assign )
872
875
back .devices ['usb' ]._exposed .append (exp_dev )
873
876
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 }})
878
883
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'' )
883
899
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 }}))
884
904
self .ext .attach_and_notify .assert_not_called ()
885
905
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 ):
890
908
back , front = self .added_assign_setup ()
891
909
892
910
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' )
894
912
895
913
front .devices ['usb' ]._assigned .append (assign )
896
914
back .devices ['usb' ]._assigned .append (assign )
897
915
back .devices ['usb' ]._exposed .append (exp_dev )
898
916
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'' )
908
921
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 }}))
909
926
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' )
913
927
914
- def test_015_on_qdb_change_ask (self ):
928
+ def test_016_on_qdb_change_ask (self ):
915
929
back , front = self .added_assign_setup ()
916
930
917
931
exp_dev = qubesusbproxy .core3ext .USBDevice (back , '1-1' )
@@ -920,11 +934,12 @@ def test_015_on_qdb_change_ask(self):
920
934
front .devices ['usb' ]._assigned .append (assign )
921
935
back .devices ['usb' ]._exposed .append (exp_dev )
922
936
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 }})
928
943
929
944
def test_020_on_startup_multiple_assignments_including_full (self ):
930
945
back , front = self .added_assign_setup ()
0 commit comments