2020-01-23 10:42:26 +01:00
|
|
|
# -*- encoding: utf-8 -*-
|
2017-06-04 01:13:57 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@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.
|
2017-06-04 01:13:57 +02:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2017-06-04 01:13:57 +02: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.
|
2017-06-04 01:13:57 +02: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/>.
|
2017-06-04 01:13:57 +02:00
|
|
|
|
|
|
|
from unittest import mock
|
|
|
|
|
2017-09-03 03:11:48 +02:00
|
|
|
import lxml.etree
|
|
|
|
|
2017-07-12 12:40:43 +02:00
|
|
|
import qubes.storage
|
2017-06-04 01:13:57 +02:00
|
|
|
import qubes.tests
|
|
|
|
import qubes.tests.vm.qubesvm
|
|
|
|
import qubes.vm.appvm
|
|
|
|
import qubes.vm.templatevm
|
|
|
|
|
|
|
|
class TestApp(object):
|
|
|
|
labels = {1: qubes.Label(1, '0xcc0000', 'red')}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.domains = {}
|
|
|
|
|
|
|
|
class TestProp(object):
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
__name__ = 'testprop'
|
|
|
|
|
|
|
|
class TestVM(object):
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
app = TestApp()
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.running = False
|
|
|
|
self.installed_by_rpm = False
|
|
|
|
for k, v in kwargs.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
return self.running
|
|
|
|
|
2017-07-12 12:40:43 +02:00
|
|
|
class TestPool(qubes.storage.Pool):
|
|
|
|
def init_volume(self, vm, volume_config):
|
|
|
|
vid = '{}/{}'.format(vm.name, volume_config['name'])
|
|
|
|
assert volume_config.pop('pool', None) == self
|
|
|
|
return qubes.storage.Volume(vid=vid, pool=self, **volume_config)
|
|
|
|
|
|
|
|
|
2017-06-04 01:13:57 +02:00
|
|
|
class TC_90_AppVM(qubes.tests.vm.qubesvm.QubesVMTestsMixin,
|
|
|
|
qubes.tests.QubesTestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2020-06-22 16:03:21 +02:00
|
|
|
self.app.pools['default'] = TestPool(name='default')
|
2017-06-04 01:13:57 +02:00
|
|
|
self.app.pools['linux-kernel'] = mock.Mock(**{
|
|
|
|
'init_volume.return_value.pool': 'linux-kernel'})
|
|
|
|
self.template = qubes.vm.templatevm.TemplateVM(self.app, None,
|
|
|
|
qid=1, name=qubes.tests.VMPREFIX + 'template')
|
|
|
|
self.app.domains[self.template.name] = self.template
|
|
|
|
self.app.domains[self.template] = self.template
|
2017-09-19 17:01:29 +02:00
|
|
|
self.addCleanup(self.cleanup_appvm)
|
|
|
|
|
|
|
|
def cleanup_appvm(self):
|
|
|
|
self.template.close()
|
|
|
|
del self.template
|
|
|
|
self.app.domains.clear()
|
|
|
|
self.app.pools.clear()
|
2017-06-04 01:13:57 +02:00
|
|
|
|
|
|
|
def get_vm(self, **kwargs):
|
2017-09-19 17:01:29 +02:00
|
|
|
vm = qubes.vm.appvm.AppVM(self.app, None,
|
2017-06-04 01:13:57 +02:00
|
|
|
qid=2, name=qubes.tests.VMPREFIX + 'test',
|
|
|
|
template=self.template,
|
|
|
|
**kwargs)
|
2017-09-19 17:01:29 +02:00
|
|
|
self.addCleanup(vm.close)
|
|
|
|
return vm
|
2017-06-04 01:13:57 +02:00
|
|
|
|
|
|
|
def test_000_init(self):
|
|
|
|
self.get_vm()
|
|
|
|
|
|
|
|
def test_001_storage_init(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
self.assertTrue(vm.volume_config['private']['save_on_stop'])
|
|
|
|
self.assertFalse(vm.volume_config['private']['snap_on_start'])
|
|
|
|
self.assertIsNone(vm.volume_config['private'].get('source', None))
|
|
|
|
|
|
|
|
self.assertFalse(vm.volume_config['root']['save_on_stop'])
|
|
|
|
self.assertTrue(vm.volume_config['root']['snap_on_start'])
|
|
|
|
self.assertEqual(vm.volume_config['root'].get('source', None),
|
2017-06-09 04:46:46 +02:00
|
|
|
self.template.volumes['root'])
|
2017-06-04 01:13:57 +02:00
|
|
|
|
|
|
|
self.assertFalse(
|
|
|
|
vm.volume_config['volatile'].get('save_on_stop', False))
|
|
|
|
self.assertFalse(
|
|
|
|
vm.volume_config['volatile'].get('snap_on_start', False))
|
|
|
|
self.assertIsNone(vm.volume_config['volatile'].get('source', None))
|
|
|
|
|
|
|
|
def test_002_storage_template_change(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
# create new mock, so new template will get different volumes
|
|
|
|
self.app.pools['default'] = mock.Mock(**{
|
|
|
|
'init_volume.return_value.pool': 'default'})
|
|
|
|
template2 = qubes.vm.templatevm.TemplateVM(self.app, None,
|
|
|
|
qid=3, name=qubes.tests.VMPREFIX + 'template2')
|
|
|
|
self.app.domains[template2.name] = template2
|
|
|
|
self.app.domains[template2] = template2
|
|
|
|
|
|
|
|
vm.template = template2
|
|
|
|
self.assertFalse(vm.volume_config['root']['save_on_stop'])
|
|
|
|
self.assertTrue(vm.volume_config['root']['snap_on_start'])
|
|
|
|
self.assertNotEqual(vm.volume_config['root'].get('source', None),
|
|
|
|
self.template.volumes['root'].source)
|
|
|
|
self.assertEqual(vm.volume_config['root'].get('source', None),
|
2017-07-25 05:29:26 +02:00
|
|
|
template2.volumes['root'])
|
2017-06-04 01:13:57 +02:00
|
|
|
|
|
|
|
def test_003_template_change_running(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
with mock.patch.object(vm, 'get_power_state') as mock_power:
|
|
|
|
mock_power.return_value = 'Running'
|
|
|
|
with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
|
|
|
|
vm.template = self.template
|
2017-09-03 03:11:48 +02:00
|
|
|
|
2017-09-23 16:02:01 +02:00
|
|
|
def test_004_template_reset(self):
|
|
|
|
vm = self.get_vm()
|
|
|
|
with self.assertRaises(qubes.exc.QubesValueError):
|
|
|
|
vm.template = qubes.property.DEFAULT
|
|
|
|
|
2017-09-03 03:11:48 +02:00
|
|
|
def test_500_property_migrate_template_for_dispvms(self):
|
|
|
|
xml_template = '''
|
|
|
|
<domain class="AppVM" id="domain-1">
|
|
|
|
<properties>
|
|
|
|
<property name="qid">1</property>
|
|
|
|
<property name="name">testvm</property>
|
|
|
|
<property name="label" ref="label-1" />
|
|
|
|
<property name="dispvm_allowed">{value}</property>
|
|
|
|
</properties>
|
|
|
|
</domain>
|
|
|
|
'''
|
|
|
|
xml = lxml.etree.XML(xml_template.format(value='True'))
|
|
|
|
vm = qubes.vm.appvm.AppVM(self.app, xml)
|
|
|
|
self.assertEqual(vm.template_for_dispvms, True)
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
vm.dispvm_allowed
|
|
|
|
|
|
|
|
xml = lxml.etree.XML(xml_template.format(value='False'))
|
|
|
|
vm = qubes.vm.appvm.AppVM(self.app, xml)
|
|
|
|
self.assertEqual(vm.template_for_dispvms, False)
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
vm.dispvm_allowed
|
2020-01-23 10:42:26 +01:00
|
|
|
|
|
|
|
def test_600_load_volume_config(self):
|
|
|
|
xml_template = '''
|
|
|
|
<domain class="AppVM" id="domain-1">
|
|
|
|
<properties>
|
|
|
|
<property name="qid">1</property>
|
|
|
|
<property name="name">testvm</property>
|
|
|
|
<property name="label" ref="label-1" />
|
|
|
|
</properties>
|
|
|
|
<volume-config>
|
|
|
|
<volume name="root" pool="lvm" revisions_to_keep="3" rw="True"
|
|
|
|
size="1234" vid="qubes_dom0/vm-testvm-root" />
|
|
|
|
</volume-config>
|
|
|
|
</domain>
|
|
|
|
'''
|
|
|
|
xml = lxml.etree.XML(xml_template)
|
|
|
|
vm = qubes.vm.appvm.AppVM(self.app, xml)
|
|
|
|
self.assertEqual(vm.volume_config['root']['revisions_to_keep'], '3')
|
|
|
|
self.assertEqual(vm.volume_config['root']['rw'], True)
|
|
|
|
self.assertEqual(vm.volume_config['root']['size'], '1234')
|
|
|
|
self.assertEqual(vm.volume_config['root']['vid'],
|
|
|
|
'qubes_dom0/vm-testvm-root')
|