storage_kernels.py 9.3 KB

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