appvm.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- encoding: utf8 -*-
  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 program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program 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
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. from unittest import mock
  21. import qubes.storage
  22. import qubes.tests
  23. import qubes.tests.vm.qubesvm
  24. import qubes.vm.appvm
  25. import qubes.vm.templatevm
  26. class TestApp(object):
  27. labels = {1: qubes.Label(1, '0xcc0000', 'red')}
  28. def __init__(self):
  29. self.domains = {}
  30. class TestProp(object):
  31. # pylint: disable=too-few-public-methods
  32. __name__ = 'testprop'
  33. class TestVM(object):
  34. # pylint: disable=too-few-public-methods
  35. app = TestApp()
  36. def __init__(self, **kwargs):
  37. self.running = False
  38. self.installed_by_rpm = False
  39. for k, v in kwargs.items():
  40. setattr(self, k, v)
  41. def is_running(self):
  42. return self.running
  43. class TestPool(qubes.storage.Pool):
  44. def init_volume(self, vm, volume_config):
  45. vid = '{}/{}'.format(vm.name, volume_config['name'])
  46. assert volume_config.pop('pool', None) == self
  47. return qubes.storage.Volume(vid=vid, pool=self, **volume_config)
  48. class TC_90_AppVM(qubes.tests.vm.qubesvm.QubesVMTestsMixin,
  49. qubes.tests.QubesTestCase):
  50. def setUp(self):
  51. super().setUp()
  52. self.app.pools['default'] = TestPool('default')
  53. self.app.pools['linux-kernel'] = mock.Mock(**{
  54. 'init_volume.return_value.pool': 'linux-kernel'})
  55. self.template = qubes.vm.templatevm.TemplateVM(self.app, None,
  56. qid=1, name=qubes.tests.VMPREFIX + 'template')
  57. self.app.domains[self.template.name] = self.template
  58. self.app.domains[self.template] = self.template
  59. def get_vm(self, **kwargs):
  60. return qubes.vm.appvm.AppVM(self.app, None,
  61. qid=2, name=qubes.tests.VMPREFIX + 'test',
  62. template=self.template,
  63. **kwargs)
  64. def test_000_init(self):
  65. self.get_vm()
  66. def test_001_storage_init(self):
  67. vm = self.get_vm()
  68. self.assertTrue(vm.volume_config['private']['save_on_stop'])
  69. self.assertFalse(vm.volume_config['private']['snap_on_start'])
  70. self.assertIsNone(vm.volume_config['private'].get('source', None))
  71. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  72. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  73. self.assertEqual(vm.volume_config['root'].get('source', None),
  74. self.template.volumes['root'])
  75. self.assertFalse(
  76. vm.volume_config['volatile'].get('save_on_stop', False))
  77. self.assertFalse(
  78. vm.volume_config['volatile'].get('snap_on_start', False))
  79. self.assertIsNone(vm.volume_config['volatile'].get('source', None))
  80. def test_002_storage_template_change(self):
  81. vm = self.get_vm()
  82. # create new mock, so new template will get different volumes
  83. self.app.pools['default'] = mock.Mock(**{
  84. 'init_volume.return_value.pool': 'default'})
  85. template2 = qubes.vm.templatevm.TemplateVM(self.app, None,
  86. qid=3, name=qubes.tests.VMPREFIX + 'template2')
  87. self.app.domains[template2.name] = template2
  88. self.app.domains[template2] = template2
  89. vm.template = template2
  90. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  91. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  92. self.assertNotEqual(vm.volume_config['root'].get('source', None),
  93. self.template.volumes['root'].source)
  94. self.assertEqual(vm.volume_config['root'].get('source', None),
  95. template2.volumes['root'])
  96. def test_003_template_change_running(self):
  97. vm = self.get_vm()
  98. with mock.patch.object(vm, 'get_power_state') as mock_power:
  99. mock_power.return_value = 'Running'
  100. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  101. vm.template = self.template