storage_kernels.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. os.makedirs(self.POOL_DIR + '/dummy', exist_ok=True)
  64. open('/tmp/test-pool/dummy/modules.img', 'w').close()
  65. def tearDown(self):
  66. """ Remove the file based storage pool after testing """
  67. self.app.remove_pool("test-pool")
  68. self.app.cleanup()
  69. self.app.close()
  70. del self.app
  71. super(TC_01_KernelVolumes, self).tearDown()
  72. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  73. def test_000_reject_rw(self):
  74. config = {
  75. 'name': 'root',
  76. 'pool': self.POOL_NAME,
  77. 'save_on_stop': True,
  78. 'rw': True,
  79. }
  80. vm = qubes.tests.storage.TestVM(self)
  81. vm.kernel = 'dummy'
  82. with self.assertRaises(AssertionError):
  83. self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  84. def test_001_simple_volume(self):
  85. config = {
  86. 'name': 'kernel',
  87. 'pool': self.POOL_NAME,
  88. 'rw': False,
  89. }
  90. template_vm = self.app.default_template
  91. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  92. vm.kernel = 'dummy'
  93. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  94. self.assertEqual(volume.name, 'kernel')
  95. self.assertEqual(volume.pool, self.POOL_NAME)
  96. self.assertFalse(volume.snap_on_start)
  97. self.assertFalse(volume.save_on_stop)
  98. self.assertFalse(volume.rw)
  99. self.assertEqual(volume.usage, 0)
  100. expected_path = '/tmp/test-pool/dummy/modules.img'
  101. self.assertEqual(volume.path, expected_path)
  102. block_dev = volume.block_device()
  103. self.assertIsInstance(block_dev, qubes.storage.BlockDevice)
  104. self.assertEqual(block_dev.devtype, 'disk')
  105. self.assertEqual(block_dev.path, expected_path)
  106. self.assertEqual(block_dev.name, 'kernel')
  107. def test_002_follow_kernel_change(self):
  108. config = {
  109. 'name': 'kernel',
  110. 'pool': self.POOL_NAME,
  111. 'rw': False,
  112. }
  113. template_vm = self.app.default_template
  114. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  115. vm.kernel = 'dummy'
  116. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  117. self.assertEqual(volume.name, 'kernel')
  118. self.assertEqual(volume.pool, self.POOL_NAME)
  119. self.assertEqual(volume.path, '/tmp/test-pool/dummy/modules.img')
  120. vm.kernel = 'updated'
  121. self.assertEqual(volume.path, '/tmp/test-pool/updated/modules.img')
  122. def test_003_kernel_none(self):
  123. config = {
  124. 'name': 'kernel',
  125. 'pool': self.POOL_NAME,
  126. 'rw': False,
  127. }
  128. template_vm = self.app.default_template
  129. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  130. vm.kernel = None
  131. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  132. self.assertEqual(volume.name, 'kernel')
  133. self.assertEqual(volume.pool, self.POOL_NAME)
  134. self.assertFalse(volume.snap_on_start)
  135. self.assertFalse(volume.save_on_stop)
  136. self.assertFalse(volume.rw)
  137. self.assertEqual(volume.usage, 0)
  138. self.assertIsNone(volume.path)
  139. self.assertIsNone(volume.vid)
  140. block_dev = volume.block_device()
  141. self.assertIsNone(block_dev)
  142. def test_004_kernel_none_change(self):
  143. config = {
  144. 'name': 'kernel',
  145. 'pool': self.POOL_NAME,
  146. 'rw': False,
  147. }
  148. template_vm = self.app.default_template
  149. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  150. vm.kernel = None
  151. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  152. self.assertIsNone(volume.path)
  153. self.assertIsNone(volume.vid)
  154. block_dev = volume.block_device()
  155. self.assertIsNone(block_dev)
  156. vm.kernel = 'dummy'
  157. expected_path = '/tmp/test-pool/dummy/modules.img'
  158. self.assertEqual(volume.path, expected_path)
  159. block_dev = volume.block_device()
  160. self.assertIsInstance(block_dev, qubes.storage.BlockDevice)
  161. self.assertEqual(block_dev.devtype, 'disk')
  162. self.assertEqual(block_dev.path, expected_path)
  163. self.assertEqual(block_dev.name, 'kernel')
  164. def test_005_kernel_none_change(self):
  165. config = {
  166. 'name': 'kernel',
  167. 'pool': self.POOL_NAME,
  168. 'rw': False,
  169. }
  170. template_vm = self.app.default_template
  171. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  172. vm.kernel = 'dummy'
  173. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  174. expected_path = '/tmp/test-pool/dummy/modules.img'
  175. self.assertEqual(volume.path, expected_path)
  176. block_dev = volume.block_device()
  177. self.assertIsInstance(block_dev, qubes.storage.BlockDevice)
  178. self.assertEqual(block_dev.devtype, 'disk')
  179. self.assertEqual(block_dev.path, expected_path)
  180. self.assertEqual(block_dev.name, 'kernel')
  181. vm.kernel = None
  182. self.assertIsNone(volume.path)
  183. self.assertIsNone(volume.vid)
  184. block_dev = volume.block_device()
  185. self.assertIsNone(block_dev)
  186. class TC_03_KernelPool(qubes.tests.QubesTestCase):
  187. """ Test the paths for the default file based pool (``FilePool``).
  188. """
  189. POOL_DIR = '/tmp/test-pool'
  190. POOL_NAME = 'test-pool'
  191. POOL_CONFIG = {'driver': 'linux-kernel', 'dir_path': POOL_DIR, 'name':
  192. POOL_NAME}
  193. def setUp(self):
  194. """ Add a test file based storage pool """
  195. super(TC_03_KernelPool, self).setUp()
  196. self.app = TestApp()
  197. self.app.create_dummy_template()
  198. dummy_kernel = os.path.join(self.POOL_DIR, 'dummy')
  199. os.makedirs(dummy_kernel)
  200. open(os.path.join(dummy_kernel, 'vmlinuz'), 'w').close()
  201. open(os.path.join(dummy_kernel, 'modules.img'), 'w').close()
  202. open(os.path.join(dummy_kernel, 'initramfs'), 'w').close()
  203. self.app.add_pool(**self.POOL_CONFIG)
  204. def tearDown(self):
  205. """ Remove the file based storage pool after testing """
  206. self.app.remove_pool("test-pool")
  207. self.app.cleanup()
  208. self.app.close()
  209. del self.app
  210. super(TC_03_KernelPool, self).tearDown()
  211. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  212. if os.path.exists('/tmp/qubes-test'):
  213. shutil.rmtree('/tmp/qubes-test')
  214. def test_001_pool_exists(self):
  215. """ Check if the storage pool was added to the storage pool config """
  216. self.assertIn('test-pool', self.app.pools.keys())
  217. def test_002_pool_volumes(self):
  218. """ List volumes """
  219. volumes = self.app.pools[self.POOL_NAME].volumes
  220. self.assertEqual(len(volumes), 1)
  221. vol = volumes[0]
  222. self.assertEqual(vol.vid, 'dummy')
  223. self.assertEqual(vol.path, '/tmp/test-pool/dummy/modules.img')