dispvm.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. import unittest.mock as mock
  21. import asyncio
  22. import qubes.vm.dispvm
  23. import qubes.vm.appvm
  24. import qubes.vm.templatevm
  25. import qubes.tests
  26. import qubes.tests.vm
  27. import qubes.tests.vm.appvm
  28. class TestApp(qubes.tests.vm.TestApp):
  29. def __init__(self):
  30. super(TestApp, self).__init__()
  31. self.qid_counter = 1
  32. def add_new_vm(self, cls, **kwargs):
  33. qid = self.qid_counter
  34. self.qid_counter += 1
  35. vm = cls(self, None, qid=qid, **kwargs)
  36. self.domains[vm.name] = vm
  37. self.domains[vm] = vm
  38. return vm
  39. class TC_00_DispVM(qubes.tests.QubesTestCase):
  40. def setUp(self):
  41. super(TC_00_DispVM, self).setUp()
  42. self.app = TestApp()
  43. self.app.save = mock.Mock()
  44. self.app.pools['default'] = qubes.tests.vm.appvm.TestPool(name='default')
  45. self.app.pools['linux-kernel'] = mock.Mock(**{
  46. 'init_volume.return_value.pool': 'linux-kernel'})
  47. self.app.vmm.offline_mode = True
  48. self.template = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM,
  49. name='test-template', label='red')
  50. self.appvm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  51. name='test-vm', template=self.template, label='red')
  52. self.app.domains[self.appvm.name] = self.appvm
  53. self.app.domains[self.appvm] = self.appvm
  54. self.addCleanup(self.cleanup_dispvm)
  55. def cleanup_dispvm(self):
  56. self.template.close()
  57. self.appvm.close()
  58. del self.template
  59. del self.appvm
  60. self.app.domains.clear()
  61. self.app.pools.clear()
  62. @asyncio.coroutine
  63. def mock_coro(self, *args, **kwargs):
  64. pass
  65. @mock.patch('os.symlink')
  66. @mock.patch('os.makedirs')
  67. @mock.patch('qubes.storage.Storage')
  68. def test_000_from_appvm(self, mock_storage, mock_makedirs, mock_symlink):
  69. mock_storage.return_value.create.side_effect = self.mock_coro
  70. self.appvm.template_for_dispvms = True
  71. orig_getitem = self.app.domains.__getitem__
  72. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  73. as mock_domains:
  74. mock_domains.configure_mock(**{
  75. 'get_new_unused_dispid': mock.Mock(return_value=42),
  76. '__getitem__.side_effect': orig_getitem
  77. })
  78. dispvm = self.loop.run_until_complete(
  79. qubes.vm.dispvm.DispVM.from_appvm(self.appvm))
  80. mock_domains.get_new_unused_dispid.assert_called_once_with()
  81. self.assertEqual(dispvm.name, 'disp42')
  82. self.assertEqual(dispvm.template, self.appvm)
  83. self.assertEqual(dispvm.label, self.appvm.label)
  84. self.assertEqual(dispvm.label, self.appvm.label)
  85. self.assertEqual(dispvm.auto_cleanup, True)
  86. mock_makedirs.assert_called_once_with(
  87. '/var/lib/qubes/appvms/' + dispvm.name, mode=0o775, exist_ok=True)
  88. mock_symlink.assert_not_called()
  89. def test_001_from_appvm_reject_not_allowed(self):
  90. with self.assertRaises(qubes.exc.QubesException):
  91. dispvm = self.loop.run_until_complete(
  92. qubes.vm.dispvm.DispVM.from_appvm(self.appvm))
  93. def test_002_template_change(self):
  94. self.appvm.template_for_dispvms = True
  95. orig_getitem = self.app.domains.__getitem__
  96. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  97. as mock_domains:
  98. mock_domains.configure_mock(**{
  99. 'get_new_unused_dispid': mock.Mock(return_value=42),
  100. '__getitem__.side_effect': orig_getitem
  101. })
  102. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  103. name='test-dispvm', template=self.appvm)
  104. with self.assertRaises(qubes.exc.QubesValueError):
  105. dispvm.template = self.appvm
  106. with self.assertRaises(qubes.exc.QubesValueError):
  107. dispvm.template = qubes.property.DEFAULT
  108. def test_010_create_direct(self):
  109. self.appvm.template_for_dispvms = True
  110. orig_getitem = self.app.domains.__getitem__
  111. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  112. as mock_domains:
  113. mock_domains.configure_mock(**{
  114. 'get_new_unused_dispid': mock.Mock(return_value=42),
  115. '__getitem__.side_effect': orig_getitem
  116. })
  117. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  118. name='test-dispvm', template=self.appvm)
  119. mock_domains.get_new_unused_dispid.assert_called_once_with()
  120. self.assertEqual(dispvm.name, 'test-dispvm')
  121. self.assertEqual(dispvm.template, self.appvm)
  122. self.assertEqual(dispvm.label, self.appvm.label)
  123. self.assertEqual(dispvm.label, self.appvm.label)
  124. self.assertEqual(dispvm.auto_cleanup, False)
  125. def test_011_create_direct_generate_name(self):
  126. self.appvm.template_for_dispvms = True
  127. orig_getitem = self.app.domains.__getitem__
  128. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  129. as mock_domains:
  130. mock_domains.configure_mock(**{
  131. 'get_new_unused_dispid': mock.Mock(return_value=42),
  132. '__getitem__.side_effect': orig_getitem
  133. })
  134. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  135. template=self.appvm)
  136. mock_domains.get_new_unused_dispid.assert_called_once_with()
  137. self.assertEqual(dispvm.name, 'disp42')
  138. self.assertEqual(dispvm.template, self.appvm)
  139. self.assertEqual(dispvm.label, self.appvm.label)
  140. self.assertEqual(dispvm.auto_cleanup, False)
  141. def test_011_create_direct_reject(self):
  142. orig_getitem = self.app.domains.__getitem__
  143. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  144. as mock_domains:
  145. mock_domains.configure_mock(**{
  146. 'get_new_unused_dispid': mock.Mock(return_value=42),
  147. '__getitem__.side_effect': orig_getitem
  148. })
  149. with self.assertRaises(qubes.exc.QubesException):
  150. self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  151. name='test-dispvm', template=self.appvm)
  152. self.assertFalse(mock_domains.get_new_unused_dispid.called)
  153. @mock.patch('os.symlink')
  154. @mock.patch('os.makedirs')
  155. def test_020_copy_storage_pool(self, mock_makedirs, mock_symlink):
  156. self.app.pools['alternative'] = qubes.tests.vm.appvm.TestPool(name='alternative')
  157. self.appvm.template_for_dispvms = True
  158. self.loop.run_until_complete(self.template.create_on_disk())
  159. self.loop.run_until_complete(self.appvm.create_on_disk(pool='alternative'))
  160. orig_getitem = self.app.domains.__getitem__
  161. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  162. as mock_domains:
  163. mock_domains.configure_mock(**{
  164. 'get_new_unused_dispid': mock.Mock(return_value=42),
  165. '__getitem__.side_effect': orig_getitem
  166. })
  167. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  168. name='test-dispvm', template=self.appvm)
  169. self.loop.run_until_complete(dispvm.create_on_disk())
  170. self.assertEqual(dispvm.template, self.appvm)
  171. self.assertEqual(dispvm.volumes['private'].pool,
  172. self.appvm.volumes['private'].pool)
  173. self.assertEqual(dispvm.volumes['root'].pool,
  174. self.appvm.volumes['root'].pool)
  175. self.assertEqual(dispvm.volumes['volatile'].pool,
  176. self.appvm.volumes['volatile'].pool)