storage_file.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. # The Qubes OS Project, https://www.qubes-os.org/
  2. #
  3. # Copyright (C) 2015 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. ''' Tests for the file storage backend '''
  19. import os
  20. import shutil
  21. import qubes.storage
  22. import qubes.tests.storage
  23. from qubes.config import defaults
  24. # :pylint: disable=invalid-name
  25. class TestApp(qubes.Qubes):
  26. ''' A Mock App object '''
  27. def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
  28. super(TestApp, self).__init__('/tmp/qubes-test.xml', load=False,
  29. offline_mode=True, **kwargs)
  30. self.load_initial_values()
  31. self.pools['linux-kernel'].dir_path = '/tmp/qubes-test-kernel'
  32. dummy_kernel = os.path.join(self.pools['linux-kernel'].dir_path,
  33. 'dummy')
  34. os.makedirs(dummy_kernel)
  35. open(os.path.join(dummy_kernel, 'vmlinuz'), 'w').close()
  36. open(os.path.join(dummy_kernel, 'modules.img'), 'w').close()
  37. open(os.path.join(dummy_kernel, 'initramfs'), 'w').close()
  38. self.default_kernel = 'dummy'
  39. def cleanup(self):
  40. ''' Remove temporary directories '''
  41. shutil.rmtree(self.pools['linux-kernel'].dir_path)
  42. def create_dummy_template(self):
  43. ''' Initalizes a dummy TemplateVM as the `default_template` '''
  44. template = self.add_new_vm(qubes.vm.templatevm.TemplateVM,
  45. name='test-template', label='red',
  46. memory=1024, maxmem=1024)
  47. self.default_template = template
  48. class TC_00_FilePool(qubes.tests.QubesTestCase):
  49. """ This class tests some properties of the 'default' pool.
  50. This test might become obsolete if we change the driver for the default
  51. pool to something else as 'file'.
  52. """
  53. def setUp(self):
  54. super(TC_00_FilePool, self).setUp()
  55. self.app = TestApp()
  56. def tearDown(self):
  57. self.app.cleanup()
  58. super(TC_00_FilePool, self).tearDown()
  59. def test000_default_pool_dir(self):
  60. """ The predefined dir for the default pool should be ``/var/lib/qubes``
  61. .. sealso::
  62. Data :data:``qubes.qubes.defaults['pool_config']``.
  63. """
  64. result = self.app.get_pool("default").dir_path
  65. expected = '/var/lib/qubes'
  66. self.assertEquals(result, expected)
  67. def test001_default_storage_class(self):
  68. """ Check when using default pool the Storage is
  69. ``qubes.storage.Storage``. """
  70. result = self._init_app_vm().storage
  71. self.assertIsInstance(result, qubes.storage.Storage)
  72. def _init_app_vm(self):
  73. """ Return initalised, but not created, AppVm. """
  74. vmname = self.make_vm_name('appvm')
  75. self.app.create_dummy_template()
  76. return self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
  77. template=self.app.default_template,
  78. label='red')
  79. class TC_01_FileVolumes(qubes.tests.QubesTestCase):
  80. ''' Test correct handling of different types of volumes '''
  81. POOL_DIR = '/tmp/test-pool'
  82. POOL_NAME = 'test-pool'
  83. POOL_CONF = {'driver': 'file', 'dir_path': POOL_DIR, 'name': POOL_NAME}
  84. def setUp(self):
  85. """ Add a test file based storage pool """
  86. super(TC_01_FileVolumes, self).setUp()
  87. self.app = TestApp()
  88. self.app.create_dummy_template()
  89. self.app.add_pool(**self.POOL_CONF)
  90. def tearDown(self):
  91. """ Remove the file based storage pool after testing """
  92. self.app.remove_pool("test-pool")
  93. self.app.cleanup()
  94. super(TC_01_FileVolumes, self).tearDown()
  95. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  96. def test_000_origin_volume(self):
  97. config = {
  98. 'name': 'root',
  99. 'pool': self.POOL_NAME,
  100. 'save_on_stop': True,
  101. 'rw': True,
  102. 'size': defaults['root_img_size'],
  103. }
  104. vm = qubes.tests.storage.TestVM(self)
  105. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  106. self.assertEqual(volume.name, 'root')
  107. self.assertEqual(volume.pool, self.POOL_NAME)
  108. self.assertEqual(volume.size, defaults['root_img_size'])
  109. self.assertFalse(volume.snap_on_start)
  110. self.assertTrue(volume.save_on_stop)
  111. self.assertTrue(volume.rw)
  112. def test_001_snapshot_volume(self):
  113. source = 'vm-templates/fedora-23/root'
  114. original_size = qubes.config.defaults['root_img_size']
  115. config = {
  116. 'name': 'root',
  117. 'pool': 'default',
  118. 'snap_on_start': True,
  119. 'rw': False,
  120. 'source': source,
  121. 'size': original_size,
  122. }
  123. template_vm = self.app.default_template
  124. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  125. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  126. self.assertEqual(volume.name, 'root')
  127. self.assertEqual(volume.pool, 'default')
  128. self.assertEqual(volume.size, original_size)
  129. self.assertTrue(volume.snap_on_start)
  130. self.assertTrue(volume.snap_on_start)
  131. self.assertFalse(volume.save_on_stop)
  132. self.assertFalse(volume.rw)
  133. self.assertEqual(volume.usage, 0)
  134. def test_002_read_write_volume(self):
  135. config = {
  136. 'name': 'root',
  137. 'pool': self.POOL_NAME,
  138. 'rw': True,
  139. 'save_on_stop': True,
  140. 'size': defaults['root_img_size'],
  141. }
  142. vm = qubes.tests.storage.TestVM(self)
  143. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  144. self.assertEqual(volume.name, 'root')
  145. self.assertEqual(volume.pool, self.POOL_NAME)
  146. self.assertEqual(volume.size, defaults['root_img_size'])
  147. self.assertFalse(volume.snap_on_start)
  148. self.assertTrue(volume.save_on_stop)
  149. self.assertTrue(volume.rw)
  150. def test_003_read_only_volume(self):
  151. template = self.app.default_template
  152. vid = template.volumes['root'].vid
  153. config = {'name': 'root', 'pool': 'default', 'rw': False, 'vid': vid}
  154. vm = qubes.tests.storage.TestVM(self, template=template)
  155. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  156. self.assertEqual(volume.name, 'root')
  157. self.assertEqual(volume.pool, 'default')
  158. # original_size = qubes.config.defaults['root_img_size']
  159. # FIXME: self.assertEqual(volume.size, original_size)
  160. self.assertFalse(volume.snap_on_start)
  161. self.assertFalse(volume.save_on_stop)
  162. self.assertFalse(volume.rw)
  163. def test_004_volatile_volume(self):
  164. config = {
  165. 'name': 'root',
  166. 'pool': self.POOL_NAME,
  167. 'size': defaults['root_img_size'],
  168. 'rw': True,
  169. }
  170. vm = qubes.tests.storage.TestVM(self)
  171. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  172. self.assertEqual(volume.name, 'root')
  173. self.assertEqual(volume.pool, self.POOL_NAME)
  174. self.assertEqual(volume.size, defaults['root_img_size'])
  175. self.assertFalse(volume.snap_on_start)
  176. self.assertFalse(volume.save_on_stop)
  177. self.assertTrue(volume.rw)
  178. def test_005_appvm_volumes(self):
  179. ''' Check if AppVM volumes are propertly initialized '''
  180. vmname = self.make_vm_name('appvm')
  181. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
  182. template=self.app.default_template,
  183. label='red')
  184. expected = vm.template.dir_path + '/root.img:' + vm.template.dir_path \
  185. + '/root-cow.img'
  186. self.assertVolumePath(vm, 'root', expected, rw=False)
  187. expected = vm.dir_path + '/private.img:' + \
  188. vm.dir_path + '/private-cow.img'
  189. self.assertVolumePath(vm, 'private', expected, rw=True)
  190. expected = vm.dir_path + '/volatile.img'
  191. self.assertVolumePath(vm, 'volatile', expected, rw=True)
  192. def test_006_template_volumes(self):
  193. ''' Check if TemplateVM volumes are propertly initialized '''
  194. vmname = self.make_vm_name('appvm')
  195. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=vmname,
  196. label='red')
  197. expected = vm.dir_path + '/root.img:' + vm.dir_path + '/root-cow.img'
  198. self.assertVolumePath(vm, 'root', expected, rw=True)
  199. expected = vm.dir_path + '/private.img'
  200. self.assertVolumePath(vm, 'private', expected, rw=True)
  201. expected = vm.dir_path + '/volatile.img'
  202. self.assertVolumePath(vm, 'volatile', expected, rw=True)
  203. def assertVolumePath(self, vm, dev_name, expected, rw=True):
  204. # :pylint: disable=invalid-name
  205. volumes = vm.volumes
  206. b_dev = volumes[dev_name].block_device()
  207. self.assertEqual(b_dev.rw, rw)
  208. self.assertEquals(b_dev.path, expected)
  209. class TC_03_FilePool(qubes.tests.QubesTestCase):
  210. """ Test the paths for the default file based pool (``FilePool``).
  211. """
  212. POOL_DIR = '/tmp/test-pool'
  213. APPVMS_DIR = '/tmp/test-pool/appvms'
  214. TEMPLATES_DIR = '/tmp/test-pool/vm-templates'
  215. SERVICE_DIR = '/tmp/test-pool/servicevms'
  216. POOL_NAME = 'test-pool'
  217. POOL_CONFIG = {'driver': 'file', 'dir_path': POOL_DIR, 'name': POOL_NAME}
  218. def setUp(self):
  219. """ Add a test file based storage pool """
  220. super(TC_03_FilePool, self).setUp()
  221. self._orig_qubes_base_dir = qubes.config.system_path['qubes_base_dir']
  222. qubes.config.system_path['qubes_base_dir'] = '/tmp/qubes-test'
  223. self.app = TestApp()
  224. self.app.create_dummy_template()
  225. self.app.add_pool(**self.POOL_CONFIG)
  226. def tearDown(self):
  227. """ Remove the file based storage pool after testing """
  228. self.app.remove_pool("test-pool")
  229. self.app.cleanup()
  230. super(TC_03_FilePool, self).tearDown()
  231. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  232. if os.path.exists('/tmp/qubes-test'):
  233. shutil.rmtree('/tmp/qubes-test')
  234. qubes.config.system_path['qubes_base_dir'] = self._orig_qubes_base_dir
  235. def test_001_pool_exists(self):
  236. """ Check if the storage pool was added to the storage pool config """
  237. self.assertIn('test-pool', self.app.pools.keys())
  238. def test_002_pool_dir_create(self):
  239. """ Check if the storage pool dir and subdirs were created """
  240. # The dir should not exists before
  241. pool_name = 'foo'
  242. pool_dir = '/tmp/foo'
  243. appvms_dir = '/tmp/foo/appvms'
  244. templates_dir = '/tmp/foo/vm-templates'
  245. self.assertFalse(os.path.exists(pool_dir))
  246. self.app.add_pool(name=pool_name, dir_path=pool_dir, driver='file')
  247. self.assertTrue(os.path.exists(pool_dir))
  248. self.assertTrue(os.path.exists(appvms_dir))
  249. self.assertTrue(os.path.exists(templates_dir))
  250. shutil.rmtree(pool_dir, ignore_errors=True)
  251. def test_011_appvm_file_images(self):
  252. """ Check if all the needed image files are created for an AppVm"""
  253. vmname = self.make_vm_name('appvm')
  254. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
  255. template=self.app.default_template,
  256. volume_config={
  257. 'private': {
  258. 'pool': 'test-pool'
  259. },
  260. 'volatile': {
  261. 'pool': 'test-pool'
  262. }
  263. }, label='red')
  264. vm.create_on_disk()
  265. expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name)
  266. expected_private_path = os.path.join(expected_vmdir, 'private.img')
  267. self.assertEquals(vm.volumes['private'].path, expected_private_path)
  268. expected_volatile_path = os.path.join(expected_vmdir, 'volatile.img')
  269. vm.storage.get_pool(vm.volumes['volatile'])\
  270. .reset(vm.volumes['volatile'])
  271. self.assertEqualsAndExists(vm.volumes['volatile'].path,
  272. expected_volatile_path)
  273. def test_013_template_file_images(self):
  274. """ Check if root.img, private.img, volatile.img and root-cow.img are
  275. created propertly by the storage system
  276. """
  277. vmname = self.make_vm_name('tmvm')
  278. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=vmname,
  279. volume_config={
  280. 'root': {
  281. 'pool': 'test-pool'
  282. },
  283. 'private': {
  284. 'pool': 'test-pool'
  285. },
  286. 'volatile': {
  287. 'pool': 'test-pool'
  288. }
  289. }, label='red')
  290. vm.create_on_disk()
  291. expected_vmdir = os.path.join(self.TEMPLATES_DIR, vm.name)
  292. expected_root_origin_path = os.path.join(expected_vmdir, 'root.img')
  293. expected_root_cow_path = os.path.join(expected_vmdir, 'root-cow.img')
  294. expected_root_path = '%s:%s' % (expected_root_origin_path,
  295. expected_root_cow_path)
  296. self.assertEquals(vm.volumes['root'].block_device().path,
  297. expected_root_path)
  298. self.assertExist(vm.volumes['root'].path)
  299. expected_private_path = os.path.join(expected_vmdir, 'private.img')
  300. self.assertEqualsAndExists(vm.volumes['private'].path,
  301. expected_private_path)
  302. expected_rootcow_path = os.path.join(expected_vmdir, 'root-cow.img')
  303. self.assertEqualsAndExists(vm.volumes['root'].path_cow,
  304. expected_rootcow_path)
  305. def assertEqualsAndExists(self, result_path, expected_path):
  306. """ Check if the ``result_path``, matches ``expected_path`` and exists.
  307. See also: :meth:``assertExist``
  308. """
  309. # :pylint: disable=invalid-name
  310. self.assertEquals(result_path, expected_path)
  311. self.assertExist(result_path)
  312. def assertExist(self, path):
  313. """ Assert that the given path exists. """
  314. # :pylint: disable=invalid-name
  315. self.assertTrue(
  316. os.path.exists(path), "Path {!s} does not exist".format(path))