dispvm.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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('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_called_once_with(
  89. '/usr/share/icons/hicolor/128x128/devices/appvm-red.png',
  90. '/var/lib/qubes/appvms/{}/icon.png'.format(dispvm.name))
  91. def test_001_from_appvm_reject_not_allowed(self):
  92. with self.assertRaises(qubes.exc.QubesException):
  93. dispvm = self.loop.run_until_complete(
  94. qubes.vm.dispvm.DispVM.from_appvm(self.appvm))
  95. def test_002_template_change(self):
  96. self.appvm.template_for_dispvms = True
  97. orig_getitem = self.app.domains.__getitem__
  98. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  99. as mock_domains:
  100. mock_domains.configure_mock(**{
  101. 'get_new_unused_dispid': mock.Mock(return_value=42),
  102. '__getitem__.side_effect': orig_getitem
  103. })
  104. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  105. name='test-dispvm', template=self.appvm)
  106. with self.assertRaises(qubes.exc.QubesValueError):
  107. dispvm.template = self.appvm
  108. with self.assertRaises(qubes.exc.QubesValueError):
  109. dispvm.template = qubes.property.DEFAULT
  110. def test_010_create_direct(self):
  111. self.appvm.template_for_dispvms = True
  112. orig_getitem = self.app.domains.__getitem__
  113. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  114. as mock_domains:
  115. mock_domains.configure_mock(**{
  116. 'get_new_unused_dispid': mock.Mock(return_value=42),
  117. '__getitem__.side_effect': orig_getitem
  118. })
  119. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  120. name='test-dispvm', template=self.appvm)
  121. mock_domains.get_new_unused_dispid.assert_called_once_with()
  122. self.assertEqual(dispvm.name, 'test-dispvm')
  123. self.assertEqual(dispvm.template, self.appvm)
  124. self.assertEqual(dispvm.label, self.appvm.label)
  125. self.assertEqual(dispvm.label, self.appvm.label)
  126. self.assertEqual(dispvm.auto_cleanup, False)
  127. def test_011_create_direct_generate_name(self):
  128. self.appvm.template_for_dispvms = True
  129. orig_getitem = self.app.domains.__getitem__
  130. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  131. as mock_domains:
  132. mock_domains.configure_mock(**{
  133. 'get_new_unused_dispid': mock.Mock(return_value=42),
  134. '__getitem__.side_effect': orig_getitem
  135. })
  136. dispvm = self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  137. template=self.appvm)
  138. mock_domains.get_new_unused_dispid.assert_called_once_with()
  139. self.assertEqual(dispvm.name, 'disp42')
  140. self.assertEqual(dispvm.template, self.appvm)
  141. self.assertEqual(dispvm.label, self.appvm.label)
  142. self.assertEqual(dispvm.auto_cleanup, False)
  143. def test_011_create_direct_reject(self):
  144. orig_getitem = self.app.domains.__getitem__
  145. with mock.patch.object(self.app, 'domains', wraps=self.app.domains) \
  146. as mock_domains:
  147. mock_domains.configure_mock(**{
  148. 'get_new_unused_dispid': mock.Mock(return_value=42),
  149. '__getitem__.side_effect': orig_getitem
  150. })
  151. with self.assertRaises(qubes.exc.QubesException):
  152. self.app.add_new_vm(qubes.vm.dispvm.DispVM,
  153. name='test-dispvm', template=self.appvm)
  154. self.assertFalse(mock_domains.get_new_unused_dispid.called)