storage_file.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library 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 GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. #
  19. ''' Tests for the file storage backend '''
  20. import os
  21. import shutil
  22. import unittest.mock
  23. import subprocess
  24. import qubes.storage
  25. import qubes.utils
  26. import qubes.tests.storage
  27. from qubes.config import defaults
  28. # :pylint: disable=invalid-name
  29. class TestApp(qubes.Qubes):
  30. ''' A Mock App object '''
  31. def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
  32. super(TestApp, self).__init__('/tmp/qubes-test.xml', load=False,
  33. offline_mode=True, **kwargs)
  34. self.load_initial_values()
  35. self.pools['linux-kernel'].dir_path = '/tmp/qubes-test-kernel'
  36. dummy_kernel = os.path.join(self.pools['linux-kernel'].dir_path,
  37. 'dummy')
  38. os.makedirs(dummy_kernel, exist_ok=True)
  39. open(os.path.join(dummy_kernel, 'vmlinuz'), 'w').close()
  40. open(os.path.join(dummy_kernel, 'modules.img'), 'w').close()
  41. open(os.path.join(dummy_kernel, 'initramfs'), 'w').close()
  42. self.default_kernel = 'dummy'
  43. def cleanup(self):
  44. ''' Remove temporary directories '''
  45. shutil.rmtree(self.pools['linux-kernel'].dir_path)
  46. if os.path.exists(self.store):
  47. os.unlink(self.store)
  48. def create_dummy_template(self):
  49. ''' Initalizes a dummy TemplateVM as the `default_template` '''
  50. template = self.add_new_vm(qubes.vm.templatevm.TemplateVM,
  51. name='test-template', label='red',
  52. memory=1024, maxmem=1024)
  53. self.default_template = template
  54. class TC_00_FilePool(qubes.tests.QubesTestCase):
  55. """ This class tests some properties of the 'default' pool.
  56. This test might become obsolete if we change the driver for the default
  57. pool to something else as 'file'.
  58. """
  59. def setUp(self):
  60. super(TC_00_FilePool, self).setUp()
  61. self.app = TestApp()
  62. def tearDown(self):
  63. self.app.cleanup()
  64. self.app.close()
  65. del self.app
  66. super(TC_00_FilePool, self).tearDown()
  67. def test000_default_pool_dir(self):
  68. """ The predefined dir for the default pool should be ``/var/lib/qubes``
  69. .. sealso::
  70. Data :data:``qubes.qubes.defaults['pool_config']``.
  71. """
  72. result = self.app.get_pool("varlibqubes").dir_path
  73. expected = '/var/lib/qubes'
  74. self.assertEqual(result, expected)
  75. def test001_default_storage_class(self):
  76. """ Check when using default pool the Storage is
  77. ``qubes.storage.Storage``. """
  78. result = self._init_app_vm().storage
  79. self.assertIsInstance(result, qubes.storage.Storage)
  80. def _init_app_vm(self):
  81. """ Return initalised, but not created, AppVm. """
  82. vmname = self.make_vm_name('appvm')
  83. self.app.create_dummy_template()
  84. return self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
  85. template=self.app.default_template,
  86. label='red')
  87. class TC_01_FileVolumes(qubes.tests.QubesTestCase):
  88. ''' Test correct handling of different types of volumes '''
  89. POOL_DIR = '/tmp/test-pool'
  90. POOL_NAME = 'test-pool'
  91. POOL_CONF = {'driver': 'file', 'dir_path': POOL_DIR, 'name': POOL_NAME}
  92. def setUp(self):
  93. """ Add a test file based storage pool """
  94. super(TC_01_FileVolumes, self).setUp()
  95. self.app = TestApp()
  96. self.loop.run_until_complete(self.app.add_pool(**self.POOL_CONF))
  97. self.app.default_pool = self.app.get_pool(self.POOL_NAME)
  98. self.app.create_dummy_template()
  99. def tearDown(self):
  100. """ Remove the file based storage pool after testing """
  101. for vm in list(self.app.domains):
  102. if vm.name.startswith(qubes.tests.VMPREFIX):
  103. del self.app.domains[vm]
  104. self.app.default_template = None
  105. del self.app.domains['test-template']
  106. self.app.default_pool = 'varlibqubes'
  107. self.loop.run_until_complete(self.app.remove_pool("test-pool"))
  108. self.app.cleanup()
  109. self.app.close()
  110. del self.app
  111. super(TC_01_FileVolumes, self).tearDown()
  112. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  113. def test_000_origin_volume(self):
  114. config = {
  115. 'name': 'root',
  116. 'pool': self.POOL_NAME,
  117. 'save_on_stop': True,
  118. 'rw': True,
  119. 'size': defaults['root_img_size'],
  120. }
  121. vm = qubes.tests.storage.TestVM(self)
  122. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  123. self.assertEqual(volume.name, 'root')
  124. self.assertEqual(volume.pool, self.POOL_NAME)
  125. self.assertEqual(volume.size, defaults['root_img_size'])
  126. self.assertFalse(volume.snap_on_start)
  127. self.assertTrue(volume.save_on_stop)
  128. self.assertTrue(volume.rw)
  129. block = volume.block_device()
  130. self.assertEqual(block.path,
  131. '{base}.img:{base}-cow.img'.format(
  132. base=self.POOL_DIR + '/appvms/' + vm.name + '/root'))
  133. self.assertEqual(block.script, 'block-origin')
  134. self.assertEqual(block.rw, True)
  135. self.assertEqual(block.name, 'root')
  136. self.assertEqual(block.devtype, 'disk')
  137. self.assertIsNone(block.domain)
  138. def test_001_snapshot_volume(self):
  139. template_vm = self.app.default_template
  140. vm = qubes.tests.storage.TestVM(self, template=template_vm)
  141. original_size = qubes.config.defaults['root_img_size']
  142. source_config = {
  143. 'name': 'root',
  144. 'pool': self.POOL_NAME,
  145. 'save_on_stop': True,
  146. 'rw': False,
  147. 'size': original_size,
  148. }
  149. source = self.app.get_pool(self.POOL_NAME).init_volume(template_vm,
  150. source_config)
  151. config = {
  152. 'name': 'root',
  153. 'pool': self.POOL_NAME,
  154. 'snap_on_start': True,
  155. 'rw': False,
  156. 'source': source,
  157. 'size': original_size,
  158. }
  159. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  160. self.assertEqual(volume.name, 'root')
  161. self.assertEqual(volume.pool, self.POOL_NAME)
  162. self.assertEqual(volume.size, original_size)
  163. self.assertTrue(volume.snap_on_start)
  164. self.assertTrue(volume.snap_on_start)
  165. self.assertFalse(volume.save_on_stop)
  166. self.assertFalse(volume.rw)
  167. self.assertEqual(volume.usage, 0)
  168. block = volume.block_device()
  169. assert isinstance(block, qubes.storage.BlockDevice)
  170. self.assertEqual(block.path,
  171. '{base}/{src}.img:{base}/{src}-cow.img:'
  172. '{base}/{dst}-cow.img'.format(
  173. base=self.POOL_DIR,
  174. src='vm-templates/' + template_vm.name + '/root',
  175. dst='appvms/' + vm.name + '/root',
  176. ))
  177. self.assertEqual(block.name, 'root')
  178. self.assertEqual(block.script, 'block-snapshot')
  179. self.assertEqual(block.rw, False)
  180. self.assertEqual(block.devtype, 'disk')
  181. def test_002_read_write_volume(self):
  182. config = {
  183. 'name': 'root',
  184. 'pool': self.POOL_NAME,
  185. 'rw': True,
  186. 'save_on_stop': True,
  187. 'size': defaults['root_img_size'],
  188. }
  189. vm = qubes.tests.storage.TestVM(self)
  190. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  191. self.assertEqual(volume.name, 'root')
  192. self.assertEqual(volume.pool, self.POOL_NAME)
  193. self.assertEqual(volume.size, defaults['root_img_size'])
  194. self.assertFalse(volume.snap_on_start)
  195. self.assertTrue(volume.save_on_stop)
  196. self.assertTrue(volume.rw)
  197. block = volume.block_device()
  198. self.assertEqual(block.name, 'root')
  199. self.assertEqual(block.path, '{base}.img:{base}-cow.img'.format(
  200. base=self.POOL_DIR + '/appvms/' + vm.name + '/root'))
  201. self.assertEqual(block.rw, True)
  202. self.assertEqual(block.script, 'block-origin')
  203. def test_003_read_only_volume(self):
  204. template = self.app.default_template
  205. vid = template.volumes['root'].vid
  206. config = {
  207. 'name': 'root',
  208. 'pool': self.POOL_NAME,
  209. 'rw': False,
  210. 'vid': vid,
  211. }
  212. vm = qubes.tests.storage.TestVM(self, template=template)
  213. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  214. self.assertEqual(volume.name, 'root')
  215. self.assertEqual(volume.pool, self.POOL_NAME)
  216. # original_size = qubes.config.defaults['root_img_size']
  217. # FIXME: self.assertEqual(volume.size, original_size)
  218. self.assertFalse(volume.snap_on_start)
  219. self.assertFalse(volume.save_on_stop)
  220. self.assertFalse(volume.rw)
  221. block = volume.block_device()
  222. self.assertEqual(block.rw, False)
  223. def test_004_volatile_volume(self):
  224. config = {
  225. 'name': 'root',
  226. 'pool': self.POOL_NAME,
  227. 'size': defaults['root_img_size'],
  228. 'rw': True,
  229. }
  230. vm = qubes.tests.storage.TestVM(self)
  231. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  232. self.assertEqual(volume.name, 'root')
  233. self.assertEqual(volume.pool, self.POOL_NAME)
  234. self.assertEqual(volume.size, defaults['root_img_size'])
  235. self.assertFalse(volume.snap_on_start)
  236. self.assertFalse(volume.save_on_stop)
  237. self.assertTrue(volume.rw)
  238. def test_005_appvm_volumes(self):
  239. ''' Check if AppVM volumes are propertly initialized '''
  240. vmname = self.make_vm_name('appvm')
  241. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
  242. template=self.app.default_template,
  243. label='red')
  244. template_dir = os.path.join(self.POOL_DIR, 'vm-templates',
  245. vm.template.name)
  246. vm_dir = os.path.join(self.POOL_DIR, 'appvms', vmname)
  247. expected = template_dir + '/root.img:' + \
  248. template_dir + '/root-cow.img:' + \
  249. vm_dir + '/root-cow.img'
  250. self.assertVolumePath(vm, 'root', expected, rw=True)
  251. expected = vm_dir + '/private.img:' + \
  252. vm_dir + '/private-cow.img'
  253. self.assertVolumePath(vm, 'private', expected, rw=True)
  254. expected = vm_dir + '/volatile.img'
  255. self.assertVolumePath(vm, 'volatile', expected, rw=True)
  256. def test_006_template_volumes(self):
  257. ''' Check if TemplateVM volumes are propertly initialized '''
  258. vmname = self.make_vm_name('appvm')
  259. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=vmname,
  260. label='red')
  261. vm_dir = os.path.join(self.POOL_DIR, 'vm-templates', vmname)
  262. expected = vm_dir + '/root.img:' + vm_dir + '/root-cow.img'
  263. self.assertVolumePath(vm, 'root', expected, rw=True)
  264. expected = vm_dir + '/private.img:' + \
  265. vm_dir + '/private-cow.img'
  266. self.assertVolumePath(vm, 'private', expected, rw=True)
  267. expected = vm_dir + '/volatile.img'
  268. self.assertVolumePath(vm, 'volatile', expected, rw=True)
  269. def test_010_revisions_to_keep_reject_invalid(self):
  270. ''' Check if TemplateVM volumes are propertly initialized '''
  271. config = {
  272. 'name': 'root',
  273. 'pool': self.POOL_NAME,
  274. 'save_on_stop': True,
  275. 'rw': True,
  276. 'size': defaults['root_img_size'],
  277. }
  278. vm = qubes.tests.storage.TestVM(self)
  279. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  280. self.assertEqual(volume.revisions_to_keep, 1)
  281. with self.assertRaises((NotImplementedError, ValueError)):
  282. volume.revisions_to_keep = 2
  283. self.assertEqual(volume.revisions_to_keep, 1)
  284. def test_020_import_data(self):
  285. config = {
  286. 'name': 'root',
  287. 'pool': self.POOL_NAME,
  288. 'save_on_stop': True,
  289. 'rw': True,
  290. 'size': 1024 * 1024,
  291. }
  292. vm = qubes.tests.storage.TestVM(self)
  293. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  294. volume.create()
  295. import_path = volume.import_data(volume.size)
  296. self.assertNotEqual(volume.path, import_path)
  297. with open(import_path, 'w+') as import_file:
  298. import_file.write('test')
  299. volume.import_data_end(True)
  300. self.assertFalse(os.path.exists(import_path), import_path)
  301. with open(volume.path) as volume_file:
  302. volume_data = volume_file.read().strip('\0')
  303. self.assertEqual(volume_data, 'test')
  304. def test_021_import_data_fail(self):
  305. config = {
  306. 'name': 'root',
  307. 'pool': self.POOL_NAME,
  308. 'save_on_stop': True,
  309. 'rw': True,
  310. 'size': 1024 * 1024,
  311. }
  312. vm = qubes.tests.storage.TestVM(self)
  313. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  314. volume.create()
  315. import_path = volume.import_data(volume.size)
  316. self.assertNotEqual(volume.path, import_path)
  317. with open(import_path, 'w+') as import_file:
  318. import_file.write('test')
  319. volume.import_data_end(False)
  320. self.assertFalse(os.path.exists(import_path), import_path)
  321. with open(volume.path) as volume_file:
  322. volume_data = volume_file.read().strip('\0')
  323. self.assertNotEqual(volume_data, 'test')
  324. def test_022_import_data_empty(self):
  325. config = {
  326. 'name': 'root',
  327. 'pool': self.POOL_NAME,
  328. 'save_on_stop': True,
  329. 'rw': True,
  330. 'size': 1024 * 1024,
  331. }
  332. vm = qubes.tests.storage.TestVM(self)
  333. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  334. volume.create()
  335. with open(volume.path, 'w') as vol_file:
  336. vol_file.write('test data')
  337. import_path = volume.import_data(volume.size)
  338. self.assertNotEqual(volume.path, import_path)
  339. with open(import_path, 'w+'):
  340. pass
  341. volume.import_data_end(True)
  342. self.assertFalse(os.path.exists(import_path), import_path)
  343. with open(volume.path) as volume_file:
  344. volume_data = volume_file.read().strip('\0')
  345. self.assertNotEqual(volume_data, 'test data')
  346. def test_023_resize(self):
  347. config = {
  348. 'name': 'root',
  349. 'pool': self.POOL_NAME,
  350. 'rw': True,
  351. 'save_on_stop': True,
  352. 'size': 32 * 1024**2,
  353. }
  354. vm = qubes.tests.storage.TestVM(self)
  355. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  356. self.loop.run_until_complete(
  357. qubes.utils.coro_maybe(volume.create()))
  358. new_size = 64 * 1024 ** 2
  359. self.loop.run_until_complete(
  360. qubes.utils.coro_maybe(volume.resize(new_size)))
  361. self.assertEqual(os.path.getsize(volume.path), new_size)
  362. self.assertEqual(volume.size, new_size)
  363. def test_024_import_data_with_new_size(self):
  364. config = {
  365. 'name': 'root',
  366. 'pool': self.POOL_NAME,
  367. 'save_on_stop': True,
  368. 'rw': True,
  369. 'size': 1024 * 1024,
  370. }
  371. new_size = 2 * 1024 * 1024
  372. vm = qubes.tests.storage.TestVM(self)
  373. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  374. volume.create()
  375. import_path = volume.import_data(new_size)
  376. self.assertNotEqual(volume.path, import_path)
  377. with open(import_path, 'r+b') as import_file:
  378. import_file.write(b'test')
  379. volume.import_data_end(True)
  380. self.assertFalse(os.path.exists(import_path), import_path)
  381. with open(volume.path, 'rb') as volume_file:
  382. volume_data = volume_file.read()
  383. self.assertEqual(volume_data.strip(b'\0'), b'test')
  384. self.assertEqual(len(volume_data), new_size)
  385. def _get_loop_size(self, path):
  386. sudo = [] if os.getuid() == 0 else ['sudo']
  387. try:
  388. loop_name = subprocess.check_output(
  389. sudo + ['losetup', '--associated', path]).decode().split(':')[0]
  390. if os.getuid() != 0:
  391. return int(
  392. subprocess.check_output(
  393. ['sudo', 'blockdev', '--getsize64', loop_name]))
  394. fd = os.open(loop_name, os.O_RDONLY)
  395. try:
  396. return os.lseek(fd, 0, os.SEEK_END)
  397. finally:
  398. os.close(fd)
  399. except subprocess.CalledProcessError:
  400. return None
  401. def _setup_loop(self, path):
  402. sudo = [] if os.getuid() == 0 else ['sudo']
  403. loop_name = subprocess.check_output(
  404. sudo + ['losetup', '--show', '--find', path]).decode().strip()
  405. self.addCleanup(subprocess.call, sudo + ['losetup', '-d', loop_name])
  406. def test_007_resize_running(self):
  407. old_size = 32 * 1024**2
  408. config = {
  409. 'name': 'root',
  410. 'pool': self.POOL_NAME,
  411. 'rw': True,
  412. 'save_on_stop': True,
  413. 'size': old_size,
  414. }
  415. vm = qubes.tests.storage.TestVM(self)
  416. volume = self.app.get_pool(self.POOL_NAME).init_volume(vm, config)
  417. self.loop.run_until_complete(qubes.utils.coro_maybe(volume.create()))
  418. self.loop.run_until_complete(qubes.utils.coro_maybe(volume.start()))
  419. self._setup_loop(volume.path)
  420. new_size = 64 * 1024 ** 2
  421. orig_check_call = subprocess.check_call
  422. with unittest.mock.patch('subprocess.check_call') as mock_subprocess:
  423. sudo = [] if os.getuid() == 0 else ['sudo']
  424. mock_subprocess.side_effect = (lambda *args, **kwargs:
  425. orig_check_call(sudo + args[0], *args[1:], **kwargs))
  426. self.loop.run_until_complete(
  427. qubes.utils.coro_maybe(volume.resize(new_size)))
  428. self.assertEqual(os.path.getsize(volume.path), new_size)
  429. self.assertEqual(self._get_loop_size(volume.path), new_size)
  430. self.assertEqual(volume.size, new_size)
  431. self.loop.run_until_complete(qubes.utils.coro_maybe(volume.stop()))
  432. self.assertEqual(os.path.getsize(volume.path), new_size)
  433. self.assertEqual(volume.size, new_size)
  434. def assertVolumePath(self, vm, dev_name, expected, rw=True):
  435. # :pylint: disable=invalid-name
  436. volumes = vm.volumes
  437. b_dev = volumes[dev_name].block_device()
  438. self.assertEqual(b_dev.rw, rw)
  439. self.assertEqual(b_dev.path, expected)
  440. class TC_03_FilePool(qubes.tests.QubesTestCase):
  441. """ Test the paths for the default file based pool (``FilePool``).
  442. """
  443. POOL_DIR = '/tmp/test-pool'
  444. APPVMS_DIR = '/tmp/test-pool/appvms'
  445. TEMPLATES_DIR = '/tmp/test-pool/vm-templates'
  446. POOL_NAME = 'test-pool'
  447. POOL_CONFIG = {'driver': 'file', 'dir_path': POOL_DIR, 'name': POOL_NAME}
  448. def setUp(self):
  449. """ Add a test file based storage pool """
  450. super(TC_03_FilePool, self).setUp()
  451. self.test_base_dir = '/tmp/qubes-test-dir'
  452. self.base_dir_patch = unittest.mock.patch.dict(qubes.config.system_path,
  453. {'qubes_base_dir': self.test_base_dir})
  454. self.base_dir_patch2 = unittest.mock.patch(
  455. 'qubes.config.qubes_base_dir', self.test_base_dir)
  456. self.base_dir_patch3 = unittest.mock.patch.dict(
  457. qubes.config.defaults['pool_configs']['varlibqubes'],
  458. {'dir_path': self.test_base_dir})
  459. self.base_dir_patch.start()
  460. self.base_dir_patch2.start()
  461. self.base_dir_patch3.start()
  462. self.app = TestApp()
  463. self.loop.run_until_complete(self.app.add_pool(**self.POOL_CONFIG))
  464. self.app.create_dummy_template()
  465. def tearDown(self):
  466. """ Remove the file based storage pool after testing """
  467. for vm in list(self.app.domains):
  468. if vm.name.startswith(qubes.tests.VMPREFIX):
  469. del self.app.domains[vm]
  470. self.app.default_template = None
  471. del self.app.domains['test-template']
  472. self.loop.run_until_complete(self.app.remove_pool("test-pool"))
  473. self.app.cleanup()
  474. self.app.close()
  475. del self.app
  476. self.base_dir_patch3.stop()
  477. self.base_dir_patch2.stop()
  478. self.base_dir_patch.stop()
  479. super(TC_03_FilePool, self).tearDown()
  480. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  481. if os.path.exists('/tmp/qubes-test-dir'):
  482. shutil.rmtree('/tmp/qubes-test-dir')
  483. def test_001_pool_exists(self):
  484. """ Check if the storage pool was added to the storage pool config """
  485. self.assertIn('test-pool', self.app.pools.keys())
  486. def test_002_pool_dir_create(self):
  487. """ Check if the storage pool dir and subdirs were created """
  488. # The dir should not exists before
  489. pool_name = 'foo'
  490. pool_dir = '/tmp/foo'
  491. appvms_dir = '/tmp/foo/appvms'
  492. templates_dir = '/tmp/foo/vm-templates'
  493. self.assertFalse(os.path.exists(pool_dir))
  494. self.loop.run_until_complete(
  495. self.app.add_pool(name=pool_name, dir_path=pool_dir, driver='file'))
  496. self.assertTrue(os.path.exists(pool_dir))
  497. self.assertTrue(os.path.exists(appvms_dir))
  498. self.assertTrue(os.path.exists(templates_dir))
  499. shutil.rmtree(pool_dir, ignore_errors=True)
  500. def test_003_size(self):
  501. pool = self.app.get_pool(self.POOL_NAME)
  502. with self.assertNotRaises(NotImplementedError):
  503. size = pool.size
  504. statvfs = os.statvfs(self.POOL_DIR)
  505. self.assertEqual(size, statvfs.f_blocks * statvfs.f_frsize)
  506. def test_004_usage(self):
  507. pool = self.app.get_pool(self.POOL_NAME)
  508. with self.assertNotRaises(NotImplementedError):
  509. usage = pool.usage
  510. statvfs = os.statvfs(self.POOL_DIR)
  511. self.assertEqual(usage,
  512. statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree))
  513. def test_005_revisions_to_keep(self):
  514. pool = self.app.get_pool(self.POOL_NAME)
  515. self.assertEqual(pool.revisions_to_keep, 1)
  516. with self.assertRaises((NotImplementedError, ValueError)):
  517. pool.revisions_to_keep = 2
  518. self.assertEqual(pool.revisions_to_keep, 1)
  519. def test_011_appvm_file_images(self):
  520. """ Check if all the needed image files are created for an AppVm"""
  521. vmname = self.make_vm_name('appvm')
  522. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM, name=vmname,
  523. template=self.app.default_template,
  524. volume_config={
  525. 'private': {
  526. 'pool': 'test-pool'
  527. },
  528. 'volatile': {
  529. 'pool': 'test-pool'
  530. }
  531. }, label='red')
  532. self.loop.run_until_complete(vm.create_on_disk())
  533. expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name)
  534. expected_private_path = os.path.join(expected_vmdir, 'private.img')
  535. self.assertEqual(vm.volumes['private'].path, expected_private_path)
  536. expected_volatile_path = os.path.join(expected_vmdir, 'volatile.img')
  537. vm.volumes['volatile'].reset()
  538. self.assertEqualAndExists(vm.volumes['volatile'].path,
  539. expected_volatile_path)
  540. def test_013_template_file_images(self):
  541. """ Check if root.img, private.img, volatile.img and root-cow.img are
  542. created propertly by the storage system
  543. """
  544. vmname = self.make_vm_name('tmvm')
  545. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=vmname,
  546. volume_config={
  547. 'root': {
  548. 'pool': 'test-pool'
  549. },
  550. 'private': {
  551. 'pool': 'test-pool'
  552. },
  553. 'volatile': {
  554. 'pool': 'test-pool'
  555. }
  556. }, label='red')
  557. self.loop.run_until_complete(vm.create_on_disk())
  558. expected_vmdir = os.path.join(self.TEMPLATES_DIR, vm.name)
  559. expected_root_origin_path = os.path.join(expected_vmdir, 'root.img')
  560. expected_root_cow_path = os.path.join(expected_vmdir, 'root-cow.img')
  561. expected_root_path = '%s:%s' % (expected_root_origin_path,
  562. expected_root_cow_path)
  563. self.assertEqual(vm.volumes['root'].block_device().path,
  564. expected_root_path)
  565. self.assertExist(vm.volumes['root'].path)
  566. expected_private_path = os.path.join(expected_vmdir, 'private.img')
  567. self.assertEqualAndExists(vm.volumes['private'].path,
  568. expected_private_path)
  569. expected_rootcow_path = os.path.join(expected_vmdir, 'root-cow.img')
  570. self.assertEqual(vm.volumes['root'].path_cow, expected_rootcow_path)
  571. def assertEqualAndExists(self, result_path, expected_path):
  572. """ Check if the ``result_path``, matches ``expected_path`` and exists.
  573. See also: :meth:``assertExist``
  574. """
  575. # :pylint: disable=invalid-name
  576. self.assertEqual(result_path, expected_path)
  577. self.assertExist(result_path)
  578. def assertExist(self, path):
  579. """ Assert that the given path exists. """
  580. # :pylint: disable=invalid-name
  581. self.assertTrue(
  582. os.path.exists(path), "Path {!s} does not exist".format(path))