appvm.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.tests
  22. import qubes.tests.vm.qubesvm
  23. import qubes.vm.appvm
  24. import qubes.vm.templatevm
  25. class TestApp(object):
  26. labels = {1: qubes.Label(1, '0xcc0000', 'red')}
  27. def __init__(self):
  28. self.domains = {}
  29. class TestProp(object):
  30. # pylint: disable=too-few-public-methods
  31. __name__ = 'testprop'
  32. class TestVM(object):
  33. # pylint: disable=too-few-public-methods
  34. app = TestApp()
  35. def __init__(self, **kwargs):
  36. self.running = False
  37. self.installed_by_rpm = False
  38. for k, v in kwargs.items():
  39. setattr(self, k, v)
  40. def is_running(self):
  41. return self.running
  42. class TC_90_AppVM(qubes.tests.vm.qubesvm.QubesVMTestsMixin,
  43. qubes.tests.QubesTestCase):
  44. def setUp(self):
  45. super().setUp()
  46. self.app.pools['default'] = mock.Mock(**{
  47. 'init_volume.return_value.pool': 'default'})
  48. self.app.pools['linux-kernel'] = mock.Mock(**{
  49. 'init_volume.return_value.pool': 'linux-kernel'})
  50. self.template = qubes.vm.templatevm.TemplateVM(self.app, None,
  51. qid=1, name=qubes.tests.VMPREFIX + 'template')
  52. self.app.domains[self.template.name] = self.template
  53. self.app.domains[self.template] = self.template
  54. def get_vm(self, **kwargs):
  55. return qubes.vm.appvm.AppVM(self.app, None,
  56. qid=2, name=qubes.tests.VMPREFIX + 'test',
  57. template=self.template,
  58. **kwargs)
  59. def test_000_init(self):
  60. self.get_vm()
  61. def test_001_storage_init(self):
  62. vm = self.get_vm()
  63. self.assertTrue(vm.volume_config['private']['save_on_stop'])
  64. self.assertFalse(vm.volume_config['private']['snap_on_start'])
  65. self.assertIsNone(vm.volume_config['private'].get('source', None))
  66. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  67. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  68. self.assertEqual(vm.volume_config['root'].get('source', None),
  69. self.template.volumes['root'])
  70. self.assertFalse(
  71. vm.volume_config['volatile'].get('save_on_stop', False))
  72. self.assertFalse(
  73. vm.volume_config['volatile'].get('snap_on_start', False))
  74. self.assertIsNone(vm.volume_config['volatile'].get('source', None))
  75. def test_002_storage_template_change(self):
  76. vm = self.get_vm()
  77. # create new mock, so new template will get different volumes
  78. self.app.pools['default'] = mock.Mock(**{
  79. 'init_volume.return_value.pool': 'default'})
  80. template2 = qubes.vm.templatevm.TemplateVM(self.app, None,
  81. qid=3, name=qubes.tests.VMPREFIX + 'template2')
  82. self.app.domains[template2.name] = template2
  83. self.app.domains[template2] = template2
  84. vm.template = template2
  85. self.assertFalse(vm.volume_config['root']['save_on_stop'])
  86. self.assertTrue(vm.volume_config['root']['snap_on_start'])
  87. self.assertNotEqual(vm.volume_config['root'].get('source', None),
  88. self.template.volumes['root'].source)
  89. self.assertEqual(vm.volume_config['root'].get('source', None),
  90. template2.volumes['root'].source)
  91. def test_003_template_change_running(self):
  92. vm = self.get_vm()
  93. with mock.patch.object(vm, 'get_power_state') as mock_power:
  94. mock_power.return_value = 'Running'
  95. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  96. vm.template = self.template