dispvm.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if hasattr(self, 'dispvm'):
  57. self.dispvm.close()
  58. del self.dispvm
  59. self.template.close()
  60. self.appvm.close()
  61. del self.template
  62. del self.appvm
  63. self.app.domains.clear()
  64. self.app.pools.clear()
  65. @asyncio.coroutine
  66. def mock_coro(self, *args, **kwargs):
  67. pass
  68. @mock.patch('os.symlink')
  69. @mock.patch('os.makedirs')
  70. @mock.patch('qubes.storage.Storage')
  71. def test_000_from_appvm(self, mock_storage, mock_makedirs, mock_symlink):
  72. mock_storage.return_value.create.side_effect = self.mock_coro
  73. self.appvm.template_for_dispvms = True
  74. orig_getitem = self.app.domains.__getitem__
  75. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  76. as mock_domains:
  77. mock_domains.configure_mock(**{
  78. 'get_new_unused_dispid': mock.Mock(return_value=42),
  79. '__getitem__.side_effect': orig_getitem
  80. })
  81. dispvm = self.loop.run_until_complete(
  82. qubes.vm.dispvm.DispVM.from_appvm(self.appvm))
  83. mock_domains.get_new_unused_dispid.assert_called_once_with()
  84. self.assertEqual(dispvm.name, 'disp42')
  85. self.assertEqual(dispvm.template, self.appvm)
  86. self.assertEqual(dispvm.label, self.appvm.label)
  87. self.assertEqual(dispvm.label, self.appvm.label)
  88. self.assertEqual(dispvm.auto_cleanup, True)
  89. mock_makedirs.assert_called_once_with(
  90. '/var/lib/qubes/appvms/' + dispvm.name, mode=0o775, exist_ok=True)
  91. mock_symlink.assert_not_called()
  92. def test_001_from_appvm_reject_not_allowed(self):
  93. with self.assertRaises(qubes.exc.QubesException):
  94. dispvm = self.loop.run_until_complete(
  95. qubes.vm.dispvm.DispVM.from_appvm(self.appvm))
  96. def test_002_template_change(self):
  97. self.appvm.template_for_dispvms = True
  98. orig_getitem = self.app.domains.__getitem__
  99. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  100. as mock_domains:
  101. mock_domains.configure_mock(**{
  102. 'get_new_unused_dispid': mock.Mock(return_value=42),
  103. '__getitem__.side_effect': orig_getitem
  104. })
  105. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  106. name='test-dispvm', template=self.appvm)
  107. with self.assertRaises(qubes.exc.QubesValueError):
  108. dispvm.template = self.appvm
  109. with self.assertRaises(qubes.exc.QubesValueError):
  110. dispvm.template = qubes.property.DEFAULT
  111. def test_003_dvmtemplate_template_change(self):
  112. self.appvm.template_for_dispvms = True
  113. orig_domains = self.app.domains
  114. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  115. as mock_domains:
  116. mock_domains.configure_mock(**{
  117. 'get_new_unused_dispid': mock.Mock(return_value=42),
  118. '__getitem__.side_effect': orig_domains.__getitem__,
  119. '__iter__.side_effect': orig_domains.__iter__,
  120. '__setitem__.side_effect': orig_domains.__setitem__,
  121. })
  122. self.dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  123. name='test-dispvm', template=self.appvm)
  124. with self.assertRaises(qubes.exc.QubesVMInUseError):
  125. self.appvm.template = self.template
  126. with self.assertRaises(qubes.exc.QubesValueError):
  127. self.appvm.template = qubes.property.DEFAULT
  128. def test_004_dvmtemplate_allowed_change(self):
  129. self.appvm.template_for_dispvms = True
  130. orig_domains = self.app.domains
  131. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  132. as mock_domains:
  133. mock_domains.configure_mock(**{
  134. 'get_new_unused_dispid': mock.Mock(return_value=42),
  135. '__getitem__.side_effect': orig_domains.__getitem__,
  136. '__iter__.side_effect': orig_domains.__iter__,
  137. '__setitem__.side_effect': orig_domains.__setitem__,
  138. })
  139. self.dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  140. name='test-dispvm', template=self.appvm)
  141. with self.assertRaises(qubes.exc.QubesVMInUseError):
  142. self.appvm.template_for_dispvms = False
  143. def test_010_create_direct(self):
  144. self.appvm.template_for_dispvms = True
  145. orig_getitem = self.app.domains.__getitem__
  146. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  147. as mock_domains:
  148. mock_domains.configure_mock(**{
  149. 'get_new_unused_dispid': mock.Mock(return_value=42),
  150. '__getitem__.side_effect': orig_getitem
  151. })
  152. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  153. name='test-dispvm', template=self.appvm)
  154. mock_domains.get_new_unused_dispid.assert_called_once_with()
  155. self.assertEqual(dispvm.name, 'test-dispvm')
  156. self.assertEqual(dispvm.template, self.appvm)
  157. self.assertEqual(dispvm.label, self.appvm.label)
  158. self.assertEqual(dispvm.label, self.appvm.label)
  159. self.assertEqual(dispvm.auto_cleanup, False)
  160. def test_011_create_direct_generate_name(self):
  161. self.appvm.template_for_dispvms = True
  162. orig_getitem = self.app.domains.__getitem__
  163. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  164. as mock_domains:
  165. mock_domains.configure_mock(**{
  166. 'get_new_unused_dispid': mock.Mock(return_value=42),
  167. '__getitem__.side_effect': orig_getitem
  168. })
  169. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  170. template=self.appvm)
  171. mock_domains.get_new_unused_dispid.assert_called_once_with()
  172. self.assertEqual(dispvm.name, 'disp42')
  173. self.assertEqual(dispvm.template, self.appvm)
  174. self.assertEqual(dispvm.label, self.appvm.label)
  175. self.assertEqual(dispvm.auto_cleanup, False)
  176. def test_011_create_direct_reject(self):
  177. orig_getitem = self.app.domains.__getitem__
  178. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  179. as mock_domains:
  180. mock_domains.configure_mock(**{
  181. 'get_new_unused_dispid': mock.Mock(return_value=42),
  182. '__getitem__.side_effect': orig_getitem
  183. })
  184. with self.assertRaises(qubes.exc.QubesException):
  185. self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  186. name='test-dispvm', template=self.appvm)
  187. self.assertFalse(mock_domains.get_new_unused_dispid.called)
  188. @mock.patch('os.symlink')
  189. @mock.patch('os.makedirs')
  190. def test_020_copy_storage_pool(self, mock_makedirs, mock_symlink):
  191. self.app.pools['alternative'] = qubes.tests.vm.appvm.TestPool(name='alternative')
  192. self.appvm.template_for_dispvms = True
  193. self.loop.run_until_complete(self.template.create_on_disk())
  194. self.loop.run_until_complete(self.appvm.create_on_disk(pool='alternative'))
  195. orig_getitem = self.app.domains.__getitem__
  196. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  197. as mock_domains:
  198. mock_domains.configure_mock(**{
  199. 'get_new_unused_dispid': mock.Mock(return_value=42),
  200. '__getitem__.side_effect': orig_getitem
  201. })
  202. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  203. name='test-dispvm', template=self.appvm)
  204. self.loop.run_until_complete(dispvm.create_on_disk())
  205. self.assertEqual(dispvm.template, self.appvm)
  206. self.assertEqual(dispvm.volumes['private'].pool,
  207. self.appvm.volumes['private'].pool)
  208. self.assertEqual(dispvm.volumes['root'].pool,
  209. self.appvm.volumes['root'].pool)
  210. self.assertEqual(dispvm.volumes['volatile'].pool,
  211. self.appvm.volumes['volatile'].pool)