|
@@ -2018,44 +2018,40 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
|
|
|
@unittest.mock.patch('asyncio.create_subprocess_exec')
|
|
|
def test_700_run_service(self, mock_subprocess):
|
|
|
- func_mock = unittest.mock.Mock()
|
|
|
- mock_subprocess.side_effect = functools.partial(
|
|
|
- self.coroutine_mock, func_mock)
|
|
|
-
|
|
|
- start_mock = unittest.mock.Mock()
|
|
|
+ start_mock = unittest.mock.AsyncMock()
|
|
|
|
|
|
vm = self.get_vm(cls=qubes.vm.standalonevm.StandaloneVM,
|
|
|
name='vm')
|
|
|
vm.is_running = lambda: True
|
|
|
vm.is_qrexec_running = lambda: True
|
|
|
- vm.start = functools.partial(self.coroutine_mock, start_mock)
|
|
|
+ vm.start = start_mock
|
|
|
with self.subTest('running'):
|
|
|
self.loop.run_until_complete(vm.run_service('test.service'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_subprocess.assert_called_once_with(
|
|
|
'/usr/bin/qrexec-client', '-d', 'test-inst-vm',
|
|
|
'user:QUBESRPC test.service dom0')
|
|
|
self.assertFalse(start_mock.called)
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_subprocess.reset_mock()
|
|
|
start_mock.reset_mock()
|
|
|
with self.subTest('not_running'):
|
|
|
vm.is_running = lambda: False
|
|
|
with self.assertRaises(qubes.exc.QubesVMNotRunningError):
|
|
|
self.loop.run_until_complete(vm.run_service('test.service'))
|
|
|
- self.assertFalse(func_mock.called)
|
|
|
+ self.assertFalse(mock_subprocess.called)
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_subprocess.reset_mock()
|
|
|
start_mock.reset_mock()
|
|
|
with self.subTest('autostart'):
|
|
|
vm.is_running = lambda: False
|
|
|
self.loop.run_until_complete(vm.run_service(
|
|
|
'test.service', autostart=True))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_subprocess.assert_called_once_with(
|
|
|
'/usr/bin/qrexec-client', '-d', 'test-inst-vm',
|
|
|
'user:QUBESRPC test.service dom0')
|
|
|
self.assertTrue(start_mock.called)
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_subprocess.reset_mock()
|
|
|
start_mock.reset_mock()
|
|
|
with self.subTest('no_qrexec'):
|
|
|
vm.is_running = lambda: True
|
|
@@ -2063,28 +2059,28 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
with self.assertRaises(qubes.exc.QubesVMError):
|
|
|
self.loop.run_until_complete(vm.run_service('test.service'))
|
|
|
self.assertFalse(start_mock.called)
|
|
|
- self.assertFalse(func_mock.called)
|
|
|
+ self.assertFalse(mock_subprocess.called)
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_subprocess.reset_mock()
|
|
|
start_mock.reset_mock()
|
|
|
with self.subTest('other_user'):
|
|
|
vm.is_running = lambda: True
|
|
|
vm.is_qrexec_running = lambda: True
|
|
|
self.loop.run_until_complete(vm.run_service('test.service',
|
|
|
user='other'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_subprocess.assert_called_once_with(
|
|
|
'/usr/bin/qrexec-client', '-d', 'test-inst-vm',
|
|
|
'other:QUBESRPC test.service dom0')
|
|
|
self.assertFalse(start_mock.called)
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_subprocess.reset_mock()
|
|
|
start_mock.reset_mock()
|
|
|
with self.subTest('other_source'):
|
|
|
vm.is_running = lambda: True
|
|
|
vm.is_qrexec_running = lambda: True
|
|
|
self.loop.run_until_complete(vm.run_service('test.service',
|
|
|
source='test-inst-vm'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_subprocess.assert_called_once_with(
|
|
|
'/usr/bin/qrexec-client', '-d', 'test-inst-vm',
|
|
|
'user:QUBESRPC test.service test-inst-vm')
|
|
|
self.assertFalse(start_mock.called)
|
|
@@ -2094,19 +2090,14 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
vm = self.get_vm(cls=qubes.vm.standalonevm.StandaloneVM,
|
|
|
name='vm')
|
|
|
|
|
|
- func_mock = unittest.mock.Mock()
|
|
|
- mock_run.side_effect = functools.partial(
|
|
|
- self.coroutine_mock, func_mock)
|
|
|
- communicate_mock = unittest.mock.Mock()
|
|
|
- func_mock.return_value.communicate.side_effect = functools.partial(
|
|
|
- self.coroutine_mock, communicate_mock)
|
|
|
+ communicate_mock = mock_run.return_value.communicate
|
|
|
communicate_mock.return_value = (b'stdout', b'stderr')
|
|
|
- func_mock.return_value.returncode = 0
|
|
|
+ mock_run.return_value.returncode = 0
|
|
|
|
|
|
with self.subTest('default'):
|
|
|
value = self.loop.run_until_complete(
|
|
|
vm.run_for_stdio('cat'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_run.assert_called_once_with(
|
|
|
'cat',
|
|
|
stdout=subprocess.PIPE,
|
|
|
stderr=subprocess.PIPE,
|
|
@@ -2114,12 +2105,12 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
communicate_mock.assert_called_once_with(input=b'')
|
|
|
self.assertEqual(value, (b'stdout', b'stderr'))
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_run.reset_mock()
|
|
|
communicate_mock.reset_mock()
|
|
|
with self.subTest('with_input'):
|
|
|
value = self.loop.run_until_complete(
|
|
|
vm.run_for_stdio('cat', input=b'abc'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_run.assert_called_once_with(
|
|
|
'cat',
|
|
|
stdout=subprocess.PIPE,
|
|
|
stderr=subprocess.PIPE,
|
|
@@ -2127,14 +2118,14 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
communicate_mock.assert_called_once_with(input=b'abc')
|
|
|
self.assertEqual(value, (b'stdout', b'stderr'))
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_run.reset_mock()
|
|
|
communicate_mock.reset_mock()
|
|
|
with self.subTest('error'):
|
|
|
- func_mock.return_value.returncode = 1
|
|
|
+ mock_run.return_value.returncode = 1
|
|
|
with self.assertRaises(subprocess.CalledProcessError) as exc:
|
|
|
self.loop.run_until_complete(
|
|
|
vm.run_for_stdio('cat'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_run.assert_called_once_with(
|
|
|
'cat',
|
|
|
stdout=subprocess.PIPE,
|
|
|
stderr=subprocess.PIPE,
|
|
@@ -2149,19 +2140,14 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
vm = self.get_vm(cls=qubes.vm.standalonevm.StandaloneVM,
|
|
|
name='vm')
|
|
|
|
|
|
- func_mock = unittest.mock.Mock()
|
|
|
- mock_run_service.side_effect = functools.partial(
|
|
|
- self.coroutine_mock, func_mock)
|
|
|
- communicate_mock = unittest.mock.Mock()
|
|
|
- func_mock.return_value.communicate.side_effect = functools.partial(
|
|
|
- self.coroutine_mock, communicate_mock)
|
|
|
+ communicate_mock = mock_run_service.return_value.communicate
|
|
|
communicate_mock.return_value = (b'stdout', b'stderr')
|
|
|
- func_mock.return_value.returncode = 0
|
|
|
+ mock_run_service.return_value.returncode = 0
|
|
|
|
|
|
with self.subTest('default'):
|
|
|
value = self.loop.run_until_complete(
|
|
|
vm.run_service_for_stdio('test.service'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_run_service.assert_called_once_with(
|
|
|
'test.service',
|
|
|
stdout=subprocess.PIPE,
|
|
|
stderr=subprocess.PIPE,
|
|
@@ -2169,12 +2155,12 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
communicate_mock.assert_called_once_with(input=b'')
|
|
|
self.assertEqual(value, (b'stdout', b'stderr'))
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_run_service.reset_mock()
|
|
|
communicate_mock.reset_mock()
|
|
|
with self.subTest('with_input'):
|
|
|
value = self.loop.run_until_complete(
|
|
|
vm.run_service_for_stdio('test.service', input=b'abc'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_run_service.assert_called_once_with(
|
|
|
'test.service',
|
|
|
stdout=subprocess.PIPE,
|
|
|
stderr=subprocess.PIPE,
|
|
@@ -2182,14 +2168,14 @@ class TC_90_QubesVM(QubesVMTestsMixin, qubes.tests.QubesTestCase):
|
|
|
communicate_mock.assert_called_once_with(input=b'abc')
|
|
|
self.assertEqual(value, (b'stdout', b'stderr'))
|
|
|
|
|
|
- func_mock.reset_mock()
|
|
|
+ mock_run_service.reset_mock()
|
|
|
communicate_mock.reset_mock()
|
|
|
with self.subTest('error'):
|
|
|
- func_mock.return_value.returncode = 1
|
|
|
+ mock_run_service.return_value.returncode = 1
|
|
|
with self.assertRaises(subprocess.CalledProcessError) as exc:
|
|
|
self.loop.run_until_complete(
|
|
|
vm.run_service_for_stdio('test.service'))
|
|
|
- func_mock.assert_called_once_with(
|
|
|
+ mock_run_service.assert_called_once_with(
|
|
|
'test.service',
|
|
|
stdout=subprocess.PIPE,
|
|
|
stderr=subprocess.PIPE,
|