appvm.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # -*- encoding: utf-8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. from unittest import mock
  21. import lxml.etree
  22. import qubes.storage
  23. import qubes.tests
  24. import qubes.tests.vm.qubesvm
  25. import qubes.vm.appvm
  26. import qubes.vm.templatevm
  27. class TestApp(object):
  28. labels = {1: qubes.Label(1, '0xcc0000', 'red')}
  29. def __init__(self):
  30. self.domains = {}
  31. class TestProp(object):
  32. # pylint: disable=too-few-public-methods
  33. __name__ = 'testprop'
  34. class TestVM(object):
  35. # pylint: disable=too-few-public-methods
  36. app = TestApp()
  37. def __init__(self, **kwargs):
  38. self.running = False
  39. self.installed_by_rpm = False
  40. for k, v in kwargs.items():
  41. setattr(self, k, v)
  42. def is_running(self):
  43. return self.running
  44. class TestPool(qubes.storage.Pool):
  45. def init_volume(self, vm, volume_config):
  46. vid = '{}/{}'.format(vm.name, volume_config['name'])
  47. assert volume_config.pop('pool', None) == self
  48. return qubes.storage.Volume(vid=vid, pool=self, **volume_config)
  49. class TC_90_AppVM(qubes.tests.vm.qubesvm.QubesVMTestsMixin,
  50. qubes.tests.QubesTestCase):
  51. def setUp(self):
  52. super().setUp()
  53. self.app.pools['default'] = TestPool(name='default')
  54. self.app.pools['linux-kernel'] = mock.Mock(**{
  55. 'init_volume.return_value.pool': 'linux-kernel'})
  56. self.template = qubes.vm.templatevm.TemplateVM(self.app, None,
  57. qid=1, name=qubes.tests.VMPREFIX + 'template')
  58. self.app.domains[self.template.name] = self.template
  59. self.app.domains[self.template] = self.template
  60. self.addCleanup(self.cleanup_appvm)
  61. def cleanup_appvm(self):
  62. self.template.close()
  63. del self.template
  64. self.app.domains.clear()
  65. self.app.pools.clear()
  66. def get_vm(self, **kwargs):
  67. vm = qubes.vm.appvm.AppVM(self.app, None,
  68. qid=2, name=qubes.tests.VMPREFIX + 'test',
  69. template=self.template,
  70. **kwargs)
  71. self.addCleanup(vm.close)
  72. return vm
  73. def test_000_init(self):
  74. self.get_vm()
  75. def test_001_storage_init(self):
  76. vm = self.get_vm()
  77. self.assertTrue(vm.volume_config['private']['save_on_stop'])
  78. self.assertFalse(vm.volume_config['private']['snap_on_start'])
  79. self.assertIsNone(vm.volume_config['private'].get('source', None))
  80. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  81. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  82. self.assertEqual(vm.volume_config['root'].get('source', None),
  83. self.template.volumes['root'])
  84. self.assertFalse(
  85. vm.volume_config['volatile'].get('save_on_stop', False))
  86. self.assertFalse(
  87. vm.volume_config['volatile'].get('snap_on_start', False))
  88. self.assertIsNone(vm.volume_config['volatile'].get('source', None))
  89. def test_002_storage_template_change(self):
  90. vm = self.get_vm()
  91. # create new mock, so new template will get different volumes
  92. self.app.pools['default'] = mock.Mock(**{
  93. 'init_volume.return_value.pool': 'default'})
  94. template2 = qubes.vm.templatevm.TemplateVM(self.app, None,
  95. qid=3, name=qubes.tests.VMPREFIX + 'template2')
  96. self.app.domains[template2.name] = template2
  97. self.app.domains[template2] = template2
  98. vm.template = template2
  99. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  100. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  101. self.assertNotEqual(vm.volume_config['root'].get('source', None),
  102. self.template.volumes['root'].source)
  103. self.assertEqual(vm.volume_config['root'].get('source', None),
  104. template2.volumes['root'])
  105. def test_003_template_change_running(self):
  106. vm = self.get_vm()
  107. with mock.patch.object(vm, 'get_power_state') as mock_power:
  108. mock_power.return_value = 'Running'
  109. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  110. vm.template = self.template
  111. def test_004_template_reset(self):
  112. vm = self.get_vm()
  113. with self.assertRaises(qubes.exc.QubesValueError):
  114. vm.template = qubes.property.DEFAULT
  115. def test_500_property_migrate_template_for_dispvms(self):
  116. xml_template = '''
  117. <domain class="AppVM" id="domain-1">
  118. <properties>
  119. <property name="qid">1</property>
  120. <property name="name">testvm</property>
  121. <property name="label" ref="label-1" />
  122. <property name="dispvm_allowed">{value}</property>
  123. </properties>
  124. </domain>
  125. '''
  126. xml = lxml.etree.XML(xml_template.format(value='True'))
  127. vm = qubes.vm.appvm.AppVM(self.app, xml)
  128. self.assertEqual(vm.template_for_dispvms, True)
  129. with self.assertRaises(AttributeError):
  130. vm.dispvm_allowed
  131. xml = lxml.etree.XML(xml_template.format(value='False'))
  132. vm = qubes.vm.appvm.AppVM(self.app, xml)
  133. self.assertEqual(vm.template_for_dispvms, False)
  134. with self.assertRaises(AttributeError):
  135. vm.dispvm_allowed
  136. def test_600_load_volume_config(self):
  137. xml_template = '''
  138. <domain class="AppVM" id="domain-1">
  139. <properties>
  140. <property name="qid">1</property>
  141. <property name="name">testvm</property>
  142. <property name="label" ref="label-1" />
  143. </properties>
  144. <volume-config>
  145. <volume name="root" pool="lvm" revisions_to_keep="3" rw="True"
  146. size="1234" vid="qubes_dom0/vm-testvm-root" />
  147. </volume-config>
  148. </domain>
  149. '''
  150. xml = lxml.etree.XML(xml_template)
  151. vm = qubes.vm.appvm.AppVM(self.app, xml)
  152. self.assertEqual(vm.volume_config['root']['revisions_to_keep'], '3')
  153. self.assertEqual(vm.volume_config['root']['rw'], True)
  154. self.assertEqual(vm.volume_config['root']['size'], '1234')
  155. self.assertEqual(vm.volume_config['root']['vid'],
  156. 'qubes_dom0/vm-testvm-root')