storage_file.py 15 KB

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