appvm.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 TestVolume(qubes.storage.Volume):
  45. def create(self):
  46. pass
  47. class TestPool(qubes.storage.Pool):
  48. def __init__(self, *args, **kwargs):
  49. super(TestPool, self).__init__(*args, **kwargs)
  50. self._volumes = {}
  51. def init_volume(self, vm, volume_config):
  52. vid = '{}/{}'.format(vm.name, volume_config['name'])
  53. assert volume_config.pop('pool', None) == self
  54. vol = TestVolume(vid=vid, pool=self, **volume_config)
  55. self._volumes[vid] = vol
  56. return vol
  57. def get_volume(self, vid):
  58. return self._volumes[vid]
  59. class TC_90_AppVM(qubes.tests.vm.qubesvm.QubesVMTestsMixin,
  60. qubes.tests.QubesTestCase):
  61. def setUp(self):
  62. super().setUp()
  63. self.app.pools['default'] = TestPool(name='default')
  64. self.app.pools['linux-kernel'] = mock.Mock(**{
  65. 'init_volume.return_value.pool': 'linux-kernel'})
  66. self.template = qubes.vm.templatevm.TemplateVM(self.app, None,
  67. qid=1, name=qubes.tests.VMPREFIX + 'template')
  68. self.app.domains[self.template.name] = self.template
  69. self.app.domains[self.template] = self.template
  70. self.addCleanup(self.cleanup_appvm)
  71. def cleanup_appvm(self):
  72. self.template.close()
  73. del self.template
  74. self.app.domains.clear()
  75. self.app.pools.clear()
  76. def get_vm(self, **kwargs):
  77. vm = qubes.vm.appvm.AppVM(self.app, None,
  78. qid=2, name=qubes.tests.VMPREFIX + 'test',
  79. template=self.template,
  80. **kwargs)
  81. self.addCleanup(vm.close)
  82. return vm
  83. def test_000_init(self):
  84. self.get_vm()
  85. def test_001_storage_init(self):
  86. vm = self.get_vm()
  87. self.assertTrue(vm.volume_config['private']['save_on_stop'])
  88. self.assertFalse(vm.volume_config['private']['snap_on_start'])
  89. self.assertIsNone(vm.volume_config['private'].get('source', None))
  90. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  91. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  92. self.assertEqual(vm.volume_config['root'].get('source', None),
  93. self.template.volumes['root'])
  94. self.assertFalse(
  95. vm.volume_config['volatile'].get('save_on_stop', False))
  96. self.assertFalse(
  97. vm.volume_config['volatile'].get('snap_on_start', False))
  98. self.assertIsNone(vm.volume_config['volatile'].get('source', None))
  99. def test_002_storage_template_change(self):
  100. vm = self.get_vm()
  101. # create new mock, so new template will get different volumes
  102. self.app.pools['default'] = mock.Mock(**{
  103. 'init_volume.return_value.pool': 'default'})
  104. template2 = qubes.vm.templatevm.TemplateVM(self.app, None,
  105. qid=3, name=qubes.tests.VMPREFIX + 'template2')
  106. self.app.domains[template2.name] = template2
  107. self.app.domains[template2] = template2
  108. vm.template = template2
  109. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  110. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  111. self.assertNotEqual(vm.volume_config['root'].get('source', None),
  112. self.template.volumes['root'].source)
  113. self.assertEqual(vm.volume_config['root'].get('source', None),
  114. template2.volumes['root'])
  115. def test_003_template_change_running(self):
  116. vm = self.get_vm()
  117. with mock.patch.object(vm, 'get_power_state') as mock_power:
  118. mock_power.return_value = 'Running'
  119. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  120. vm.template = self.template
  121. def test_004_template_reset(self):
  122. vm = self.get_vm()
  123. with self.assertRaises(qubes.exc.QubesValueError):
  124. vm.template = qubes.property.DEFAULT
  125. def test_500_property_migrate_template_for_dispvms(self):
  126. xml_template = '''
  127. <domain class="AppVM" id="domain-1">
  128. <properties>
  129. <property name="qid">1</property>
  130. <property name="name">testvm</property>
  131. <property name="label" ref="label-1" />
  132. <property name="dispvm_allowed">{value}</property>
  133. </properties>
  134. </domain>
  135. '''
  136. xml = lxml.etree.XML(xml_template.format(value='True'))
  137. vm = qubes.vm.appvm.AppVM(self.app, xml)
  138. self.assertEqual(vm.template_for_dispvms, True)
  139. with self.assertRaises(AttributeError):
  140. vm.dispvm_allowed
  141. xml = lxml.etree.XML(xml_template.format(value='False'))
  142. vm = qubes.vm.appvm.AppVM(self.app, xml)
  143. self.assertEqual(vm.template_for_dispvms, False)
  144. with self.assertRaises(AttributeError):
  145. vm.dispvm_allowed
  146. def test_600_load_volume_config(self):
  147. xml_template = '''
  148. <domain class="AppVM" id="domain-1">
  149. <properties>
  150. <property name="qid">1</property>
  151. <property name="name">testvm</property>
  152. <property name="label" ref="label-1" />
  153. </properties>
  154. <volume-config>
  155. <volume name="root" pool="lvm" revisions_to_keep="3" rw="True"
  156. size="1234" vid="qubes_dom0/vm-testvm-root" />
  157. </volume-config>
  158. </domain>
  159. '''
  160. xml = lxml.etree.XML(xml_template)
  161. vm = qubes.vm.appvm.AppVM(self.app, xml)
  162. self.assertEqual(vm.volume_config['root']['revisions_to_keep'], '3')
  163. self.assertEqual(vm.volume_config['root']['rw'], True)
  164. self.assertEqual(vm.volume_config['root']['size'], '1234')
  165. self.assertEqual(vm.volume_config['root']['vid'],
  166. 'qubes_dom0/vm-testvm-root')