storage_kernels.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 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. ''' Tests for the kernels storage backend '''
  21. import os
  22. import shutil
  23. import asyncio
  24. import qubes.storage
  25. import qubes.tests.storage
  26. from qubes.config import defaults
  27. # :pylint: disable=invalid-name
  28. class TestApp(qubes.Qubes):
  29. ''' A Mock App object '''
  30. def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
  31. super(TestApp, self).__init__('/tmp/qubes-test.xml', load=False,
  32. offline_mode=True, **kwargs)
  33. self.load_initial_values()
  34. self.pools['linux-kernel'].dir_path = '/tmp/qubes-test-kernel'
  35. dummy_kernel = os.path.join(self.pools['linux-kernel'].dir_path,
  36. 'dummy')
  37. os.makedirs(dummy_kernel)
  38. open(os.path.join(dummy_kernel, 'vmlinuz'), 'w').close()
  39. open(os.path.join(dummy_kernel, 'modules.img'), 'w').close()
  40. open(os.path.join(dummy_kernel, 'initramfs'), 'w').close()
  41. self.default_kernel = 'dummy'
  42. def cleanup(self):
  43. ''' Remove temporary directories '''
  44. shutil.rmtree(self.pools['linux-kernel'].dir_path)
  45. def create_dummy_template(self):
  46. ''' Initalizes a dummy TemplateVM as the `default_template` '''
  47. template = self.add_new_vm(qubes.vm.templatevm.TemplateVM,
  48. name='test-template', label='red',
  49. memory=1024, maxmem=1024)
  50. self.default_template = template
  51. class TC_01_KernelVolumes(qubes.tests.QubesTestCase):
  52. ''' Test correct handling of different types of volumes '''
  53. POOL_DIR = '/tmp/test-pool'
  54. POOL_NAME = 'test-pool'
  55. POOL_CONF = {'driver': 'linux-kernel', 'dir_path': POOL_DIR, 'name':
  56. POOL_NAME}
  57. def setUp(self):
  58. """ Add a test file based storage pool """
  59. super(TC_01_KernelVolumes, self).setUp()
  60. self.app = TestApp()
  61. self.app.create_dummy_template()
  62. self.app.add_pool(**self.POOL_CONF)
  63. def tearDown(self):
  64. """ Remove the file based storage pool after testing """
  65. self.app.remove_pool("test-pool")
  66. self.app.cleanup()
  67. self.app.close()
  68. del self.app
  69. super(TC_01_KernelVolumes, self).tearDown()
  70. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  71. def test_000_reject_rw(self):
  72. config = {
  73. 'name': 'root',
  74. 'pool': self.POOL_NAME,
  75. 'save_on_stop': True,
  76. 'rw': True,
  77. }
  78. vm = qubes.tests.storage.TestVM(self)
  79. vm.kernel = 'dummy'
  80. with self.assertRaises(AssertionError):
  81. self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  82. def test_001_simple_volume(self):
  83. config = {
  84. 'name': 'kernel',
  85. 'pool': self.POOL_NAME,
  86. 'rw': False,
  87. }
  88. template_vm = self.app.default_template
  89. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  90. vm.kernel = 'dummy'
  91. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  92. self.assertEqual(volume.name, 'kernel')
  93. self.assertEqual(volume.pool, self.POOL_NAME)
  94. self.assertFalse(volume.snap_on_start)
  95. self.assertFalse(volume.save_on_stop)
  96. self.assertFalse(volume.rw)
  97. self.assertEqual(volume.usage, 0)
  98. expected_path = '/tmp/test-pool/dummy/modules.img'
  99. self.assertEqual(volume.path, expected_path)
  100. block_dev = volume.block_device()
  101. self.assertIsInstance(block_dev, qubes.storage.BlockDevice)
  102. self.assertEqual(block_dev.devtype, 'disk')
  103. self.assertEqual(block_dev.path, expected_path)
  104. self.assertEqual(block_dev.name, 'kernel')
  105. def test_002_follow_kernel_change(self):
  106. config = {
  107. 'name': 'kernel',
  108. 'pool': self.POOL_NAME,
  109. 'rw': False,
  110. }
  111. template_vm = self.app.default_template
  112. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  113. vm.kernel = 'dummy'
  114. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  115. self.assertEqual(volume.name, 'kernel')
  116. self.assertEqual(volume.pool, self.POOL_NAME)
  117. self.assertEqual(volume.path, '/tmp/test-pool/dummy/modules.img')
  118. vm.kernel = 'updated'
  119. self.assertEqual(volume.path, '/tmp/test-pool/updated/modules.img')
  120. def test_003_kernel_none(self):
  121. config = {
  122. 'name': 'kernel',
  123. 'pool': self.POOL_NAME,
  124. 'rw': False,
  125. }
  126. template_vm = self.app.default_template
  127. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  128. vm.kernel = None
  129. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  130. self.assertEqual(volume.name, 'kernel')
  131. self.assertEqual(volume.pool, self.POOL_NAME)
  132. self.assertFalse(volume.snap_on_start)
  133. self.assertFalse(volume.save_on_stop)
  134. self.assertFalse(volume.rw)
  135. self.assertEqual(volume.usage, 0)
  136. self.assertIsNone(volume.path)
  137. self.assertIsNone(volume.vid)
  138. block_dev = volume.block_device()
  139. self.assertIsNone(block_dev)
  140. def test_004_kernel_none_change(self):
  141. config = {
  142. 'name': 'kernel',
  143. 'pool': self.POOL_NAME,
  144. 'rw': False,
  145. }
  146. template_vm = self.app.default_template
  147. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  148. vm.kernel = None
  149. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  150. self.assertIsNone(volume.path)
  151. self.assertIsNone(volume.vid)
  152. block_dev = volume.block_device()
  153. self.assertIsNone(block_dev)
  154. vm.kernel = 'dummy'
  155. expected_path = '/tmp/test-pool/dummy/modules.img'
  156. self.assertEqual(volume.path, expected_path)
  157. block_dev = volume.block_device()
  158. self.assertIsInstance(block_dev, qubes.storage.BlockDevice)
  159. self.assertEqual(block_dev.devtype, 'disk')
  160. self.assertEqual(block_dev.path, expected_path)
  161. self.assertEqual(block_dev.name, 'kernel')
  162. def test_005_kernel_none_change(self):
  163. config = {
  164. 'name': 'kernel',
  165. 'pool': self.POOL_NAME,
  166. 'rw': False,
  167. }
  168. template_vm = self.app.default_template
  169. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  170. vm.kernel = 'dummy'
  171. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  172. expected_path = '/tmp/test-pool/dummy/modules.img'
  173. self.assertEqual(volume.path, expected_path)
  174. block_dev = volume.block_device()
  175. self.assertIsInstance(block_dev, qubes.storage.BlockDevice)
  176. self.assertEqual(block_dev.devtype, 'disk')
  177. self.assertEqual(block_dev.path, expected_path)
  178. self.assertEqual(block_dev.name, 'kernel')
  179. vm.kernel = None
  180. self.assertIsNone(volume.path)
  181. self.assertIsNone(volume.vid)
  182. block_dev = volume.block_device()
  183. self.assertIsNone(block_dev)
  184. class TC_03_KernelPool(qubes.tests.QubesTestCase):
  185. """ Test the paths for the default file based pool (``FilePool``).
  186. """
  187. POOL_DIR = '/tmp/test-pool'
  188. POOL_NAME = 'test-pool'
  189. POOL_CONFIG = {'driver': 'linux-kernel', 'dir_path': POOL_DIR, 'name':
  190. POOL_NAME}
  191. def setUp(self):
  192. """ Add a test file based storage pool """
  193. super(TC_03_KernelPool, self).setUp()
  194. self.app = TestApp()
  195. self.app.create_dummy_template()
  196. dummy_kernel = os.path.join(self.POOL_DIR, 'dummy')
  197. os.makedirs(dummy_kernel)
  198. open(os.path.join(dummy_kernel, 'vmlinuz'), 'w').close()
  199. open(os.path.join(dummy_kernel, 'modules.img'), 'w').close()
  200. open(os.path.join(dummy_kernel, 'initramfs'), 'w').close()
  201. self.app.add_pool(**self.POOL_CONFIG)
  202. def tearDown(self):
  203. """ Remove the file based storage pool after testing """
  204. self.app.remove_pool("test-pool")
  205. self.app.cleanup()
  206. self.app.close()
  207. del self.app
  208. super(TC_03_KernelPool, self).tearDown()
  209. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  210. if os.path.exists('/tmp/qubes-test'):
  211. shutil.rmtree('/tmp/qubes-test')
  212. def test_001_pool_exists(self):
  213. """ Check if the storage pool was added to the storage pool config """
  214. self.assertIn('test-pool', self.app.pools.keys())
  215. def test_002_pool_volumes(self):
  216. """ List volumes """
  217. volumes = self.app.pools[self.POOL_NAME].volumes
  218. self.assertEqual(len(volumes), 1)
  219. vol = volumes[0]
  220. self.assertEqual(vol.vid, 'dummy')
  221. self.assertEqual(vol.path, '/tmp/test-pool/dummy/modules.img')