2015-01-19 18:03:23 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-01-19 18:03:23 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2019-06-21 20:32:18 +02:00
|
|
|
import subprocess
|
2015-01-15 16:05:42 +01:00
|
|
|
import unittest
|
2017-07-25 05:48:43 +02:00
|
|
|
import unittest.mock
|
2015-01-15 16:05:42 +01:00
|
|
|
|
2019-06-21 20:32:18 +02:00
|
|
|
import functools
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
2015-01-15 16:05:42 +01:00
|
|
|
import qubes
|
2015-10-14 22:02:11 +02:00
|
|
|
import qubes.exc
|
2015-12-29 20:35:04 +01:00
|
|
|
import qubes.vm
|
2015-01-15 16:05:42 +01:00
|
|
|
import qubes.vm.adminvm
|
|
|
|
|
|
|
|
import qubes.tests
|
|
|
|
|
|
|
|
class TC_00_AdminVM(qubes.tests.QubesTestCase):
|
|
|
|
def setUp(self):
|
2017-04-18 10:16:14 +02:00
|
|
|
super().setUp()
|
2015-01-15 16:05:42 +01:00
|
|
|
try:
|
2015-12-29 20:35:04 +01:00
|
|
|
self.app = qubes.tests.vm.TestApp()
|
2017-07-29 05:07:45 +02:00
|
|
|
with unittest.mock.patch.object(
|
|
|
|
qubes.vm.adminvm.AdminVM, 'start_qdb_watch') as mock_qdb:
|
|
|
|
self.vm = qubes.vm.adminvm.AdminVM(self.app,
|
|
|
|
xml=None)
|
2017-09-28 16:12:05 +02:00
|
|
|
mock_qdb.assert_called_once_with()
|
2017-09-19 17:01:29 +02:00
|
|
|
self.addCleanup(self.cleanup_adminvm)
|
2016-06-16 11:15:38 +02:00
|
|
|
except: # pylint: disable=bare-except
|
2015-01-15 16:05:42 +01:00
|
|
|
if self.id().endswith('.test_000_init'):
|
|
|
|
raise
|
|
|
|
self.skipTest('setup failed')
|
|
|
|
|
2019-06-21 20:32:18 +02:00
|
|
|
def tearDown(self) -> None:
|
|
|
|
self.app.domains.clear()
|
|
|
|
|
|
|
|
def add_vm(self, name, cls=qubes.vm.qubesvm.QubesVM, **kwargs):
|
|
|
|
vm = cls(self.app, None,
|
|
|
|
qid=kwargs.pop('qid', 1), name=qubes.tests.VMPREFIX + name,
|
|
|
|
**kwargs)
|
|
|
|
self.app.domains[vm.qid] = vm
|
|
|
|
self.app.domains[vm.uuid] = vm
|
|
|
|
self.app.domains[vm.name] = vm
|
|
|
|
self.app.domains[vm] = vm
|
|
|
|
self.addCleanup(vm.close)
|
|
|
|
return vm
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def coroutine_mock(self, mock, *args, **kwargs):
|
|
|
|
return mock(*args, **kwargs)
|
|
|
|
|
2017-09-19 17:01:29 +02:00
|
|
|
def cleanup_adminvm(self):
|
|
|
|
self.vm.close()
|
|
|
|
del self.vm
|
|
|
|
|
2015-01-15 16:05:42 +01:00
|
|
|
def test_000_init(self):
|
|
|
|
pass
|
|
|
|
|
2020-08-06 20:41:18 +02:00
|
|
|
def test_001_property_icon(self):
|
|
|
|
self.assertEqual(self.vm.icon, 'adminvm-black')
|
|
|
|
|
2015-01-15 16:05:42 +01:00
|
|
|
def test_100_xid(self):
|
|
|
|
self.assertEqual(self.vm.xid, 0)
|
|
|
|
|
|
|
|
def test_101_libvirt_domain(self):
|
2017-07-25 05:48:43 +02:00
|
|
|
with unittest.mock.patch.object(self.app, 'vmm') as mock_vmm:
|
|
|
|
self.assertIsNotNone(self.vm.libvirt_domain)
|
|
|
|
self.assertEqual(mock_vmm.mock_calls, [
|
|
|
|
('libvirt_conn.lookupByID', (0,), {}),
|
|
|
|
])
|
2015-01-15 16:05:42 +01:00
|
|
|
|
|
|
|
def test_300_is_running(self):
|
|
|
|
self.assertTrue(self.vm.is_running())
|
|
|
|
|
|
|
|
def test_301_get_power_state(self):
|
|
|
|
self.assertEqual(self.vm.get_power_state(), 'Running')
|
|
|
|
|
|
|
|
def test_302_get_mem(self):
|
|
|
|
self.assertGreater(self.vm.get_mem(), 0)
|
|
|
|
|
|
|
|
@unittest.skip('mock object does not support this')
|
|
|
|
def test_303_get_mem_static_max(self):
|
|
|
|
self.assertGreater(self.vm.get_mem_static_max(), 0)
|
|
|
|
|
|
|
|
def test_310_start(self):
|
2015-10-14 22:02:11 +02:00
|
|
|
with self.assertRaises(qubes.exc.QubesException):
|
2015-01-15 16:05:42 +01:00
|
|
|
self.vm.start()
|
|
|
|
|
|
|
|
@unittest.skip('this functionality is undecided')
|
|
|
|
def test_311_suspend(self):
|
2015-10-14 22:02:11 +02:00
|
|
|
with self.assertRaises(qubes.exc.QubesException):
|
2015-01-15 16:05:42 +01:00
|
|
|
self.vm.suspend()
|
2019-06-21 20:32:18 +02:00
|
|
|
|
|
|
|
@unittest.mock.patch('asyncio.create_subprocess_exec')
|
|
|
|
def test_700_run_service(self, mock_subprocess):
|
|
|
|
self.add_vm('vm')
|
|
|
|
|
|
|
|
with self.subTest('running'):
|
|
|
|
self.loop.run_until_complete(self.vm.run_service('test.service'))
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_subprocess.assert_called_once_with(
|
2019-06-21 20:32:18 +02:00
|
|
|
'/usr/lib/qubes/qubes-rpc-multiplexer',
|
|
|
|
'test.service', 'dom0', 'name', 'dom0')
|
|
|
|
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_subprocess.reset_mock()
|
2019-06-21 20:32:18 +02:00
|
|
|
with self.subTest('other_user'):
|
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.vm.run_service('test.service', user='other'))
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_subprocess.assert_called_once_with(
|
2019-06-21 20:32:18 +02:00
|
|
|
'runuser', '-u', 'other', '--',
|
|
|
|
'/usr/lib/qubes/qubes-rpc-multiplexer',
|
|
|
|
'test.service', 'dom0', 'name', 'dom0')
|
|
|
|
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_subprocess.reset_mock()
|
2019-06-21 20:32:18 +02:00
|
|
|
with self.subTest('other_source'):
|
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.vm.run_service('test.service', source='test-inst-vm'))
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_subprocess.assert_called_once_with(
|
2019-06-21 20:32:18 +02:00
|
|
|
'/usr/lib/qubes/qubes-rpc-multiplexer',
|
|
|
|
'test.service', 'test-inst-vm', 'name', 'dom0')
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubes.vm.adminvm.AdminVM.run_service')
|
|
|
|
def test_710_run_service_for_stdio(self, mock_run_service):
|
2020-11-25 03:51:52 +01:00
|
|
|
communicate_mock = mock_run_service.return_value.communicate
|
2019-06-21 20:32:18 +02:00
|
|
|
communicate_mock.return_value = (b'stdout', b'stderr')
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_run_service.return_value.returncode = 0
|
2019-06-21 20:32:18 +02:00
|
|
|
|
|
|
|
with self.subTest('default'):
|
|
|
|
value = self.loop.run_until_complete(
|
|
|
|
self.vm.run_service_for_stdio('test.service'))
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_run_service.assert_called_once_with(
|
2019-06-21 20:32:18 +02:00
|
|
|
'test.service',
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
communicate_mock.assert_called_once_with(input=None)
|
|
|
|
self.assertEqual(value, (b'stdout', b'stderr'))
|
|
|
|
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_run_service.reset_mock()
|
2019-06-21 20:32:18 +02:00
|
|
|
communicate_mock.reset_mock()
|
|
|
|
with self.subTest('with_input'):
|
|
|
|
value = self.loop.run_until_complete(
|
|
|
|
self.vm.run_service_for_stdio('test.service', input=b'abc'))
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_run_service.assert_called_once_with(
|
2019-06-21 20:32:18 +02:00
|
|
|
'test.service',
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
communicate_mock.assert_called_once_with(input=b'abc')
|
|
|
|
self.assertEqual(value, (b'stdout', b'stderr'))
|
|
|
|
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_run_service.reset_mock()
|
2019-06-21 20:32:18 +02:00
|
|
|
communicate_mock.reset_mock()
|
|
|
|
with self.subTest('error'):
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_run_service.return_value.returncode = 1
|
2019-06-21 20:32:18 +02:00
|
|
|
with self.assertRaises(subprocess.CalledProcessError) as exc:
|
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.vm.run_service_for_stdio('test.service'))
|
2020-11-25 03:51:52 +01:00
|
|
|
mock_run_service.assert_called_once_with(
|
2019-06-21 20:32:18 +02:00
|
|
|
'test.service',
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
stdin=subprocess.PIPE)
|
|
|
|
communicate_mock.assert_called_once_with(input=None)
|
|
|
|
self.assertEqual(exc.exception.returncode, 1)
|
|
|
|
self.assertEqual(exc.exception.output, b'stdout')
|
|
|
|
self.assertEqual(exc.exception.stderr, b'stderr')
|