storage_lvm.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 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 lvm storage driver. By default tests are going to use the
  20. 'qubes_dom0/pool00'. An alternative LVM thin pool may be provided via
  21. :envvar:`DEFAULT_LVM_POOL` shell variable.
  22. Any pool variables prefixed with 'LVM_' or 'lvm_' represent a LVM
  23. 'volume_group/thin_pool' combination. Pool variables without a prefix
  24. represent a :py:class:`qubes.storage.lvm.ThinPool`.
  25. '''
  26. import os
  27. import subprocess
  28. import tempfile
  29. import unittest
  30. import unittest.mock
  31. import asyncio
  32. import qubes.tests
  33. import qubes.tests.storage
  34. import qubes.storage
  35. from qubes.storage.lvm import ThinPool, ThinVolume, qubes_lvm
  36. if 'DEFAULT_LVM_POOL' in os.environ.keys():
  37. DEFAULT_LVM_POOL = os.environ['DEFAULT_LVM_POOL']
  38. else:
  39. DEFAULT_LVM_POOL = 'qubes_dom0/pool00'
  40. def lvm_pool_exists(volume_group, thin_pool):
  41. ''' Returns ``True`` if thin pool exists in the volume group. '''
  42. path = "/dev/mapper/{!s}-{!s}".format(
  43. volume_group.replace('-', '--'),
  44. thin_pool.replace('-', '--'))
  45. return os.path.exists(path)
  46. def skipUnlessLvmPoolExists(test_item): # pylint: disable=invalid-name
  47. ''' Decorator that skips LVM tests if the default pool is missing. '''
  48. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  49. result = lvm_pool_exists(volume_group, thin_pool)
  50. msg = 'LVM thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  51. return unittest.skipUnless(result, msg)(test_item)
  52. POOL_CONF = {'name': 'test-lvm',
  53. 'driver': 'lvm_thin',
  54. 'volume_group': DEFAULT_LVM_POOL.split('/')[0],
  55. 'thin_pool': DEFAULT_LVM_POOL.split('/')[1]}
  56. class ThinPoolBase(qubes.tests.QubesTestCase):
  57. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  58. created_pool = False
  59. def setUp(self):
  60. super(ThinPoolBase, self).setUp()
  61. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  62. self.pool = self._find_pool(volume_group, thin_pool)
  63. if not self.pool:
  64. self.pool = self.loop.run_until_complete(
  65. self.app.add_pool(**POOL_CONF))
  66. self.created_pool = True
  67. def tearDown(self):
  68. ''' Remove the default lvm pool if it was created only for this test '''
  69. if self.created_pool:
  70. self.loop.run_until_complete(self.app.remove_pool(self.pool.name))
  71. super(ThinPoolBase, self).tearDown()
  72. def _find_pool(self, volume_group, thin_pool):
  73. ''' Returns the pool matching the specified ``volume_group`` &
  74. ``thin_pool``, or None.
  75. '''
  76. pools = [p for p in self.app.pools.values()
  77. if issubclass(p.__class__, ThinPool)]
  78. for pool in pools:
  79. if pool.volume_group == volume_group \
  80. and pool.thin_pool == thin_pool:
  81. return pool
  82. return None
  83. @skipUnlessLvmPoolExists
  84. class TC_00_ThinPool(ThinPoolBase):
  85. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  86. def setUp(self):
  87. xml_path = '/tmp/qubes-test.xml'
  88. self.app = qubes.Qubes.create_empty_store(store=xml_path,
  89. clockvm=None,
  90. updatevm=None,
  91. offline_mode=True,
  92. )
  93. os.environ['QUBES_XML_PATH'] = xml_path
  94. super(TC_00_ThinPool, self).setUp()
  95. def tearDown(self):
  96. super(TC_00_ThinPool, self).tearDown()
  97. os.unlink(self.app.store)
  98. del self.app
  99. for attr in dir(self):
  100. if isinstance(getattr(self, attr), qubes.vm.BaseVM):
  101. delattr(self, attr)
  102. def test_000_default_thin_pool(self):
  103. ''' Check whether :py:data`DEFAULT_LVM_POOL` exists. This pool is
  104. created by default, if at installation time LVM + Thin was chosen.
  105. '''
  106. msg = 'Thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  107. self.assertTrue(self.pool, msg)
  108. def test_001_origin_volume(self):
  109. ''' Test origin volume creation '''
  110. config = {
  111. 'name': 'root',
  112. 'pool': self.pool.name,
  113. 'save_on_stop': True,
  114. 'rw': True,
  115. 'size': qubes.config.defaults['root_img_size'],
  116. }
  117. vm = qubes.tests.storage.TestVM(self)
  118. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  119. self.assertIsInstance(volume, ThinVolume)
  120. self.assertEqual(volume.name, 'root')
  121. self.assertEqual(volume.pool, self.pool.name)
  122. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  123. self.loop.run_until_complete(volume.create())
  124. path = "/dev/%s" % volume.vid
  125. self.assertTrue(os.path.exists(path), path)
  126. self.loop.run_until_complete(volume.remove())
  127. def test_003_read_write_volume(self):
  128. ''' Test read-write volume creation '''
  129. config = {
  130. 'name': 'root',
  131. 'pool': self.pool.name,
  132. 'rw': True,
  133. 'save_on_stop': True,
  134. 'size': qubes.config.defaults['root_img_size'],
  135. }
  136. vm = qubes.tests.storage.TestVM(self)
  137. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  138. self.assertIsInstance(volume, ThinVolume)
  139. self.assertEqual(volume.name, 'root')
  140. self.assertEqual(volume.pool, self.pool.name)
  141. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  142. self.loop.run_until_complete(volume.create())
  143. path = "/dev/%s" % volume.vid
  144. self.assertTrue(os.path.exists(path), path)
  145. self.loop.run_until_complete(volume.remove())
  146. def test_004_size(self):
  147. with self.assertNotRaises(NotImplementedError):
  148. size = self.pool.size
  149. environ = os.environ.copy()
  150. environ['LC_ALL'] = 'C.utf8'
  151. pool_size = subprocess.check_output(['sudo', 'lvs', '--noheadings',
  152. '-o', 'lv_size',
  153. '--units', 'b', self.pool.volume_group + '/' + self.pool.thin_pool],
  154. env=environ)
  155. self.assertEqual(size, int(pool_size.strip()[:-1]))
  156. def test_005_usage(self):
  157. with self.assertNotRaises(NotImplementedError):
  158. usage = self.pool.usage
  159. environ = os.environ.copy()
  160. environ['LC_ALL'] = 'C.utf8'
  161. pool_info = subprocess.check_output(['sudo', 'lvs', '--noheadings',
  162. '-o', 'lv_size,data_percent',
  163. '--units', 'b', self.pool.volume_group + '/' + self.pool.thin_pool],
  164. env=environ)
  165. pool_size, pool_usage = pool_info.strip().split()
  166. pool_size = int(pool_size[:-1])
  167. pool_usage = float(pool_usage)
  168. self.assertEqual(usage, int(pool_size * pool_usage / 100))
  169. def _get_size(self, path):
  170. if os.getuid() != 0:
  171. return int(
  172. subprocess.check_output(
  173. ['sudo', 'blockdev', '--getsize64', path]))
  174. fd = os.open(path, os.O_RDONLY)
  175. try:
  176. return os.lseek(fd, 0, os.SEEK_END)
  177. finally:
  178. os.close(fd)
  179. def test_006_resize(self):
  180. config = {
  181. 'name': 'root',
  182. 'pool': self.pool.name,
  183. 'rw': True,
  184. 'save_on_stop': True,
  185. 'size': 32 * 1024**2,
  186. }
  187. vm = qubes.tests.storage.TestVM(self)
  188. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  189. self.loop.run_until_complete(volume.create())
  190. self.addCleanup(self.loop.run_until_complete, volume.remove())
  191. path = "/dev/%s" % volume.vid
  192. new_size = 64 * 1024 ** 2
  193. self.loop.run_until_complete(volume.resize(new_size))
  194. self.assertEqual(self._get_size(path), new_size)
  195. self.assertEqual(volume.size, new_size)
  196. def test_007_resize_running(self):
  197. old_size = 32 * 1024**2
  198. config = {
  199. 'name': 'root',
  200. 'pool': self.pool.name,
  201. 'rw': True,
  202. 'save_on_stop': True,
  203. 'size': old_size,
  204. }
  205. vm = qubes.tests.storage.TestVM(self)
  206. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  207. self.loop.run_until_complete(volume.create())
  208. self.addCleanup(self.loop.run_until_complete, volume.remove())
  209. self.loop.run_until_complete(volume.start())
  210. path = "/dev/%s" % volume.vid
  211. path2 = "/dev/%s" % volume._vid_snap
  212. new_size = 64 * 1024 ** 2
  213. self.loop.run_until_complete(volume.resize(new_size))
  214. self.assertEqual(self._get_size(path), old_size)
  215. self.assertEqual(self._get_size(path2), new_size)
  216. self.assertEqual(volume.size, new_size)
  217. self.loop.run_until_complete(volume.stop())
  218. self.assertEqual(self._get_size(path), new_size)
  219. self.assertEqual(volume.size, new_size)
  220. def _get_lv_uuid(self, lv):
  221. sudo = [] if os.getuid() == 0 else ['sudo']
  222. lvs_output = subprocess.check_output(
  223. sudo + ['lvs', '--noheadings', '-o', 'lv_uuid', lv])
  224. return lvs_output.strip()
  225. def _get_lv_origin_uuid(self, lv):
  226. sudo = [] if os.getuid() == 0 else ['sudo']
  227. if qubes.storage.lvm.lvm_is_very_old:
  228. # no support for origin_uuid directly
  229. lvs_output = subprocess.check_output(
  230. sudo + ['lvs', '--noheadings', '-o', 'origin', lv])
  231. lvs_output = subprocess.check_output(
  232. sudo + ['lvs', '--noheadings', '-o', 'lv_uuid',
  233. lv.rsplit('/', 1)[0] + '/' + lvs_output.strip().decode()])
  234. else:
  235. lvs_output = subprocess.check_output(
  236. sudo + ['lvs', '--noheadings', '-o', 'origin_uuid', lv])
  237. return lvs_output.strip()
  238. def test_008_commit(self):
  239. ''' Test volume changes commit'''
  240. config = {
  241. 'name': 'root',
  242. 'pool': self.pool.name,
  243. 'save_on_stop': True,
  244. 'rw': True,
  245. 'size': qubes.config.defaults['root_img_size'],
  246. }
  247. vm = qubes.tests.storage.TestVM(self)
  248. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  249. self.loop.run_until_complete(volume.create())
  250. path_snap = '/dev/' + volume._vid_snap
  251. self.assertFalse(os.path.exists(path_snap), path_snap)
  252. origin_uuid = self._get_lv_uuid(volume.path)
  253. self.loop.run_until_complete(volume.start())
  254. snap_uuid = self._get_lv_uuid(path_snap)
  255. self.assertNotEqual(origin_uuid, snap_uuid)
  256. path = volume.path
  257. self.assertTrue(path.startswith('/dev/' + volume.vid),
  258. '{} does not start with /dev/{}'.format(path, volume.vid))
  259. self.assertTrue(os.path.exists(path), path)
  260. self.loop.run_until_complete(volume.remove())
  261. def test_009_interrupted_commit(self):
  262. ''' Test volume changes commit'''
  263. config = {
  264. 'name': 'root',
  265. 'pool': self.pool.name,
  266. 'save_on_stop': True,
  267. 'rw': True,
  268. 'revisions_to_keep': 2,
  269. 'size': qubes.config.defaults['root_img_size'],
  270. }
  271. vm = qubes.tests.storage.TestVM(self)
  272. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  273. # mock logging, to not interfere with time.time() mock
  274. volume.log = unittest.mock.Mock()
  275. # do not call volume.create(), do it manually to simulate
  276. # interrupted commit
  277. revisions = ['-1521065904-back', '-1521065905-back', '-snap']
  278. orig_uuids = {}
  279. for rev in revisions:
  280. cmd = ['create', self.pool._pool_id,
  281. volume.vid.split('/')[1] + rev, str(config['size'])]
  282. qubes_lvm(cmd)
  283. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  284. qubes.storage.lvm.reset_cache()
  285. path_snap = '/dev/' + volume._vid_snap
  286. self.assertTrue(volume.is_dirty())
  287. self.assertEqual(volume.path,
  288. '/dev/' + volume.vid + revisions[1])
  289. expected_revisions = {
  290. revisions[0].lstrip('-'): '2018-03-14T22:18:24',
  291. revisions[1].lstrip('-'): '2018-03-14T22:18:25',
  292. }
  293. self.assertEqual(volume.revisions, expected_revisions)
  294. self.loop.run_until_complete(volume.start())
  295. self.assertEqual(volume.revisions, expected_revisions)
  296. snap_uuid = self._get_lv_uuid(path_snap)
  297. self.assertEqual(orig_uuids['-snap'], snap_uuid)
  298. self.assertTrue(volume.is_dirty())
  299. self.assertEqual(volume.path,
  300. '/dev/' + volume.vid + revisions[1])
  301. with unittest.mock.patch('time.time') as mock_time:
  302. mock_time.side_effect = [521065906]
  303. self.loop.run_until_complete(volume.stop())
  304. expected_revisions = {
  305. revisions[0].lstrip('-'): '2018-03-14T22:18:24',
  306. revisions[1].lstrip('-'): '2018-03-14T22:18:25',
  307. }
  308. self.assertFalse(volume.is_dirty())
  309. self.assertEqual(volume.revisions, expected_revisions)
  310. self.assertEqual(volume.path, '/dev/' + volume.vid)
  311. self.assertEqual(snap_uuid, self._get_lv_uuid(volume.path))
  312. self.assertFalse(os.path.exists(path_snap), path_snap)
  313. self.loop.run_until_complete(volume.remove())
  314. def test_010_migration1(self):
  315. '''Start with old revisions, then start interacting using new code'''
  316. config = {
  317. 'name': 'root',
  318. 'pool': self.pool.name,
  319. 'save_on_stop': True,
  320. 'rw': True,
  321. 'revisions_to_keep': 2,
  322. 'size': qubes.config.defaults['root_img_size'],
  323. }
  324. vm = qubes.tests.storage.TestVM(self)
  325. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  326. # mock logging, to not interfere with time.time() mock
  327. volume.log = unittest.mock.Mock()
  328. # do not call volume.create(), do it manually to have old LV naming
  329. revisions = ['', '-1521065904-back', '-1521065905-back']
  330. orig_uuids = {}
  331. for rev in revisions:
  332. cmd = ['create', self.pool._pool_id,
  333. volume.vid.split('/')[1] + rev, str(config['size'])]
  334. qubes_lvm(cmd)
  335. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  336. qubes.storage.lvm.reset_cache()
  337. path_snap = '/dev/' + volume._vid_snap
  338. self.assertFalse(os.path.exists(path_snap), path_snap)
  339. expected_revisions = {
  340. revisions[1].lstrip('-'): '2018-03-14T22:18:24',
  341. revisions[2].lstrip('-'): '2018-03-14T22:18:25',
  342. }
  343. self.assertEqual(volume.revisions, expected_revisions)
  344. self.assertEqual(volume.path, '/dev/' + volume.vid)
  345. self.loop.run_until_complete(volume.start())
  346. snap_uuid = self._get_lv_uuid(path_snap)
  347. self.assertNotEqual(orig_uuids[''], snap_uuid)
  348. snap_origin_uuid = self._get_lv_origin_uuid(path_snap)
  349. self.assertEqual(orig_uuids[''], snap_origin_uuid)
  350. path = volume.path
  351. self.assertEqual(path, '/dev/' + volume.vid)
  352. self.assertTrue(os.path.exists(path), path)
  353. with unittest.mock.patch('time.time') as mock_time:
  354. mock_time.side_effect = ('1521065906', '1521065907')
  355. self.loop.run_until_complete(volume.stop())
  356. revisions.extend(['-1521065906-back'])
  357. expected_revisions = {
  358. revisions[2].lstrip('-'): '2018-03-14T22:18:25',
  359. revisions[3].lstrip('-'): '2018-03-14T22:18:26',
  360. }
  361. self.assertEqual(volume.revisions, expected_revisions)
  362. self.assertEqual(volume.path, '/dev/' + volume.vid)
  363. path_snap = '/dev/' + volume._vid_snap
  364. self.assertFalse(os.path.exists(path_snap), path_snap)
  365. self.assertTrue(os.path.exists('/dev/' + volume.vid))
  366. self.assertEqual(self._get_lv_uuid(volume.path), snap_uuid)
  367. prev_path = '/dev/' + volume.vid + revisions[3]
  368. self.assertEqual(self._get_lv_uuid(prev_path), orig_uuids[''])
  369. self.loop.run_until_complete(volume.remove())
  370. for rev in revisions:
  371. path = '/dev/' + volume.vid + rev
  372. self.assertFalse(os.path.exists(path), path)
  373. def test_011_migration2(self):
  374. '''VM started with old code, stopped with new'''
  375. config = {
  376. 'name': 'root',
  377. 'pool': self.pool.name,
  378. 'save_on_stop': True,
  379. 'rw': True,
  380. 'revisions_to_keep': 1,
  381. 'size': qubes.config.defaults['root_img_size'],
  382. }
  383. vm = qubes.tests.storage.TestVM(self)
  384. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  385. # mock logging, to not interfere with time.time() mock
  386. volume.log = unittest.mock.Mock()
  387. # do not call volume.create(), do it manually to have old LV naming
  388. revisions = ['', '-snap']
  389. orig_uuids = {}
  390. for rev in revisions:
  391. cmd = ['create', self.pool._pool_id,
  392. volume.vid.split('/')[1] + rev, str(config['size'])]
  393. qubes_lvm(cmd)
  394. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  395. qubes.storage.lvm.reset_cache()
  396. path_snap = '/dev/' + volume._vid_snap
  397. self.assertTrue(os.path.exists(path_snap), path_snap)
  398. expected_revisions = {}
  399. self.assertEqual(volume.revisions, expected_revisions)
  400. self.assertEqual(volume.path, '/dev/' + volume.vid)
  401. self.assertTrue(volume.is_dirty())
  402. path = volume.path
  403. self.assertEqual(path, '/dev/' + volume.vid)
  404. self.assertTrue(os.path.exists(path), path)
  405. with unittest.mock.patch('time.time') as mock_time:
  406. mock_time.side_effect = ('1521065906', '1521065907')
  407. self.loop.run_until_complete(volume.stop())
  408. revisions.extend(['-1521065906-back'])
  409. expected_revisions = {
  410. revisions[2].lstrip('-'): '2018-03-14T22:18:26',
  411. }
  412. self.assertEqual(volume.revisions, expected_revisions)
  413. self.assertEqual(volume.path, '/dev/' + volume.vid)
  414. path_snap = '/dev/' + volume._vid_snap
  415. self.assertFalse(os.path.exists(path_snap), path_snap)
  416. self.assertTrue(os.path.exists('/dev/' + volume.vid))
  417. self.assertEqual(self._get_lv_uuid(volume.path), orig_uuids['-snap'])
  418. prev_path = '/dev/' + volume.vid + revisions[2]
  419. self.assertEqual(self._get_lv_uuid(prev_path), orig_uuids[''])
  420. self.loop.run_until_complete(volume.remove())
  421. for rev in revisions:
  422. path = '/dev/' + volume.vid + rev
  423. self.assertFalse(os.path.exists(path), path)
  424. def test_012_migration3(self):
  425. '''VM started with old code, started again with new, stopped with new'''
  426. config = {
  427. 'name': 'root',
  428. 'pool': self.pool.name,
  429. 'save_on_stop': True,
  430. 'rw': True,
  431. 'revisions_to_keep': 1,
  432. 'size': qubes.config.defaults['root_img_size'],
  433. }
  434. vm = qubes.tests.storage.TestVM(self)
  435. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  436. # mock logging, to not interfere with time.time() mock
  437. volume.log = unittest.mock.Mock()
  438. # do not call volume.create(), do it manually to have old LV naming
  439. revisions = ['', '-snap']
  440. orig_uuids = {}
  441. for rev in revisions:
  442. cmd = ['create', self.pool._pool_id,
  443. volume.vid.split('/')[1] + rev, str(config['size'])]
  444. qubes_lvm(cmd)
  445. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  446. qubes.storage.lvm.reset_cache()
  447. path_snap = '/dev/' + volume._vid_snap
  448. self.assertTrue(os.path.exists(path_snap), path_snap)
  449. expected_revisions = {}
  450. self.assertEqual(volume.revisions, expected_revisions)
  451. self.assertTrue(volume.path, '/dev/' + volume.vid)
  452. self.assertTrue(volume.is_dirty())
  453. self.loop.run_until_complete(volume.start())
  454. self.assertEqual(volume.revisions, expected_revisions)
  455. self.assertEqual(volume.path, '/dev/' + volume.vid)
  456. # -snap LV should be unchanged
  457. self.assertEqual(self._get_lv_uuid(volume._vid_snap),
  458. orig_uuids['-snap'])
  459. self.loop.run_until_complete(volume.remove())
  460. for rev in revisions:
  461. path = '/dev/' + volume.vid + rev
  462. self.assertFalse(os.path.exists(path), path)
  463. def test_013_migration4(self):
  464. '''revisions_to_keep=0, VM started with old code, stopped with new'''
  465. config = {
  466. 'name': 'root',
  467. 'pool': self.pool.name,
  468. 'save_on_stop': True,
  469. 'rw': True,
  470. 'revisions_to_keep': 0,
  471. 'size': qubes.config.defaults['root_img_size'],
  472. }
  473. vm = qubes.tests.storage.TestVM(self)
  474. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  475. # mock logging, to not interfere with time.time() mock
  476. volume.log = unittest.mock.Mock()
  477. # do not call volume.create(), do it manually to have old LV naming
  478. revisions = ['', '-snap']
  479. orig_uuids = {}
  480. for rev in revisions:
  481. cmd = ['create', self.pool._pool_id,
  482. volume.vid.split('/')[1] + rev, str(config['size'])]
  483. qubes_lvm(cmd)
  484. orig_uuids[rev] = self._get_lv_uuid(volume.vid + rev)
  485. qubes.storage.lvm.reset_cache()
  486. path_snap = '/dev/' + volume._vid_snap
  487. self.assertTrue(os.path.exists(path_snap), path_snap)
  488. expected_revisions = {}
  489. self.assertEqual(volume.revisions, expected_revisions)
  490. self.assertEqual(volume.path, '/dev/' + volume.vid)
  491. self.assertTrue(volume.is_dirty())
  492. with unittest.mock.patch('time.time') as mock_time:
  493. mock_time.side_effect = ('1521065906', '1521065907')
  494. self.loop.run_until_complete(volume.stop())
  495. expected_revisions = {}
  496. self.assertEqual(volume.revisions, expected_revisions)
  497. self.assertEqual(volume.path, '/dev/' + volume.vid)
  498. self.loop.run_until_complete(volume.remove())
  499. for rev in revisions:
  500. path = '/dev/' + volume.vid + rev
  501. self.assertFalse(os.path.exists(path), path)
  502. def test_014_commit_keep_0(self):
  503. ''' Test volume changes commit, with revisions_to_keep=0'''
  504. config = {
  505. 'name': 'root',
  506. 'pool': self.pool.name,
  507. 'save_on_stop': True,
  508. 'rw': True,
  509. 'revisions_to_keep': 0,
  510. 'size': qubes.config.defaults['root_img_size'],
  511. }
  512. vm = qubes.tests.storage.TestVM(self)
  513. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  514. # mock logging, to not interfere with time.time() mock
  515. volume.log = unittest.mock.Mock()
  516. self.loop.run_until_complete(volume.create())
  517. self.assertFalse(volume.is_dirty())
  518. path = volume.path
  519. expected_revisions = {}
  520. self.assertEqual(volume.revisions, expected_revisions)
  521. self.loop.run_until_complete(volume.start())
  522. self.assertEqual(volume.revisions, expected_revisions)
  523. path_snap = '/dev/' + volume._vid_snap
  524. snap_uuid = self._get_lv_uuid(path_snap)
  525. self.assertTrue(volume.is_dirty())
  526. self.assertEqual(volume.path, path)
  527. with unittest.mock.patch('time.time') as mock_time:
  528. mock_time.side_effect = [521065906]
  529. self.loop.run_until_complete(volume.stop())
  530. self.assertFalse(volume.is_dirty())
  531. self.assertEqual(volume.revisions, {})
  532. self.assertEqual(volume.path, '/dev/' + volume.vid)
  533. self.assertEqual(snap_uuid, self._get_lv_uuid(volume.path))
  534. self.assertFalse(os.path.exists(path_snap), path_snap)
  535. self.loop.run_until_complete(volume.remove())
  536. def test_020_revert_last(self):
  537. ''' Test volume revert'''
  538. config = {
  539. 'name': 'root',
  540. 'pool': self.pool.name,
  541. 'save_on_stop': True,
  542. 'rw': True,
  543. 'revisions_to_keep': 2,
  544. 'size': qubes.config.defaults['root_img_size'],
  545. }
  546. vm = qubes.tests.storage.TestVM(self)
  547. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  548. self.loop.run_until_complete(volume.create())
  549. self.loop.run_until_complete(volume.start())
  550. self.loop.run_until_complete(volume.stop())
  551. self.loop.run_until_complete(volume.start())
  552. self.loop.run_until_complete(volume.stop())
  553. self.assertEqual(len(volume.revisions), 2)
  554. revisions = volume.revisions
  555. revision_id = max(revisions.keys())
  556. current_path = volume.path
  557. current_uuid = self._get_lv_uuid(volume.path)
  558. rev_uuid = self._get_lv_uuid(volume.vid + '-' + revision_id)
  559. self.assertFalse(volume.is_dirty())
  560. self.assertNotEqual(current_uuid, rev_uuid)
  561. self.loop.run_until_complete(volume.revert())
  562. path_snap = '/dev/' + volume._vid_snap
  563. self.assertFalse(os.path.exists(path_snap), path_snap)
  564. self.assertEqual(current_path, volume.path)
  565. new_uuid = self._get_lv_origin_uuid(volume.path)
  566. self.assertEqual(new_uuid, rev_uuid)
  567. self.assertEqual(volume.revisions, revisions)
  568. self.loop.run_until_complete(volume.remove())
  569. def test_021_revert_earlier(self):
  570. ''' Test volume revert'''
  571. config = {
  572. 'name': 'root',
  573. 'pool': self.pool.name,
  574. 'save_on_stop': True,
  575. 'rw': True,
  576. 'revisions_to_keep': 2,
  577. 'size': qubes.config.defaults['root_img_size'],
  578. }
  579. vm = qubes.tests.storage.TestVM(self)
  580. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  581. self.loop.run_until_complete(volume.create())
  582. self.loop.run_until_complete(volume.start())
  583. self.loop.run_until_complete(volume.stop())
  584. self.loop.run_until_complete(volume.start())
  585. self.loop.run_until_complete(volume.stop())
  586. self.assertEqual(len(volume.revisions), 2)
  587. revisions = volume.revisions
  588. revision_id = min(revisions.keys())
  589. current_path = volume.path
  590. current_uuid = self._get_lv_uuid(volume.path)
  591. rev_uuid = self._get_lv_uuid(volume.vid + '-' + revision_id)
  592. self.assertFalse(volume.is_dirty())
  593. self.assertNotEqual(current_uuid, rev_uuid)
  594. self.loop.run_until_complete(volume.revert(revision_id))
  595. path_snap = '/dev/' + volume._vid_snap
  596. self.assertFalse(os.path.exists(path_snap), path_snap)
  597. self.assertEqual(current_path, volume.path)
  598. new_uuid = self._get_lv_origin_uuid(volume.path)
  599. self.assertEqual(new_uuid, rev_uuid)
  600. self.assertEqual(volume.revisions, revisions)
  601. self.loop.run_until_complete(volume.remove())
  602. def test_030_import_data(self):
  603. ''' Test volume import'''
  604. config = {
  605. 'name': 'root',
  606. 'pool': self.pool.name,
  607. 'save_on_stop': True,
  608. 'rw': True,
  609. 'revisions_to_keep': 2,
  610. 'size': qubes.config.defaults['root_img_size'],
  611. }
  612. vm = qubes.tests.storage.TestVM(self)
  613. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  614. self.loop.run_until_complete(volume.create())
  615. current_uuid = self._get_lv_uuid(volume.path)
  616. self.assertFalse(volume.is_dirty())
  617. import_path = self.loop.run_until_complete(volume.import_data())
  618. import_uuid = self._get_lv_uuid(import_path)
  619. self.assertNotEqual(current_uuid, import_uuid)
  620. # success - commit data
  621. self.loop.run_until_complete(volume.import_data_end(True))
  622. new_current_uuid = self._get_lv_uuid(volume.path)
  623. self.assertEqual(new_current_uuid, import_uuid)
  624. revisions = volume.revisions
  625. self.assertEqual(len(revisions), 1)
  626. revision = revisions.popitem()[0]
  627. self.assertEqual(current_uuid,
  628. self._get_lv_uuid(volume.vid + '-' + revision))
  629. self.assertFalse(os.path.exists(import_path), import_path)
  630. self.loop.run_until_complete(volume.remove())
  631. def test_031_import_data_fail(self):
  632. ''' Test volume import'''
  633. config = {
  634. 'name': 'root',
  635. 'pool': self.pool.name,
  636. 'save_on_stop': True,
  637. 'rw': True,
  638. 'revisions_to_keep': 2,
  639. 'size': qubes.config.defaults['root_img_size'],
  640. }
  641. vm = qubes.tests.storage.TestVM(self)
  642. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  643. self.loop.run_until_complete(volume.create())
  644. current_uuid = self._get_lv_uuid(volume.path)
  645. self.assertFalse(volume.is_dirty())
  646. import_path = self.loop.run_until_complete(volume.import_data())
  647. import_uuid = self._get_lv_uuid(import_path)
  648. self.assertNotEqual(current_uuid, import_uuid)
  649. # fail - discard data
  650. self.loop.run_until_complete(volume.import_data_end(False))
  651. new_current_uuid = self._get_lv_uuid(volume.path)
  652. self.assertEqual(new_current_uuid, current_uuid)
  653. revisions = volume.revisions
  654. self.assertEqual(len(revisions), 0)
  655. self.assertFalse(os.path.exists(import_path), import_path)
  656. self.loop.run_until_complete(volume.remove())
  657. def test_032_import_volume_same_pool(self):
  658. '''Import volume from the same pool'''
  659. # source volume
  660. config = {
  661. 'name': 'root',
  662. 'pool': self.pool.name,
  663. 'save_on_stop': True,
  664. 'rw': True,
  665. 'revisions_to_keep': 2,
  666. 'size': qubes.config.defaults['root_img_size'],
  667. }
  668. vm = qubes.tests.storage.TestVM(self)
  669. source_volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  670. self.loop.run_until_complete(source_volume.create())
  671. source_uuid = self._get_lv_uuid(source_volume.path)
  672. # destination volume
  673. config = {
  674. 'name': 'root2',
  675. 'pool': self.pool.name,
  676. 'save_on_stop': True,
  677. 'rw': True,
  678. 'revisions_to_keep': 2,
  679. 'size': qubes.config.defaults['root_img_size'],
  680. }
  681. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  682. volume.log = unittest.mock.Mock()
  683. with unittest.mock.patch('time.time') as mock_time:
  684. mock_time.side_effect = [1521065905]
  685. self.loop.run_until_complete(volume.create())
  686. self.assertEqual(volume.revisions, {})
  687. uuid_before = self._get_lv_uuid(volume.path)
  688. with unittest.mock.patch('time.time') as mock_time:
  689. mock_time.side_effect = [1521065906]
  690. self.loop.run_until_complete(
  691. volume.import_volume(source_volume))
  692. uuid_after = self._get_lv_uuid(volume.path)
  693. self.assertNotEqual(uuid_after, uuid_before)
  694. # also should be different than source volume (clone, not the same LV)
  695. self.assertNotEqual(uuid_after, source_uuid)
  696. self.assertEqual(self._get_lv_origin_uuid(volume.path), source_uuid)
  697. expected_revisions = {
  698. '1521065906-back': '2018-03-14T22:18:26',
  699. }
  700. self.assertEqual(volume.revisions, expected_revisions)
  701. self.loop.run_until_complete(volume.remove())
  702. self.loop.run_until_complete(source_volume.remove())
  703. def test_033_import_volume_different_pool(self):
  704. '''Import volume from a different pool'''
  705. source_volume = unittest.mock.Mock()
  706. # destination volume
  707. config = {
  708. 'name': 'root2',
  709. 'pool': self.pool.name,
  710. 'save_on_stop': True,
  711. 'rw': True,
  712. 'revisions_to_keep': 2,
  713. 'size': qubes.config.defaults['root_img_size'],
  714. }
  715. vm = qubes.tests.storage.TestVM(self)
  716. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  717. volume.log = unittest.mock.Mock()
  718. with unittest.mock.patch('time.time') as mock_time:
  719. mock_time.side_effect = [1521065905]
  720. self.loop.run_until_complete(volume.create())
  721. self.assertEqual(volume.revisions, {})
  722. uuid_before = self._get_lv_uuid(volume.path)
  723. with tempfile.NamedTemporaryFile() as source_volume_file:
  724. source_volume_file.write(b'test-content')
  725. source_volume_file.flush()
  726. source_volume.size = 16 * 1024 * 1024 # 16MiB
  727. source_volume.export.return_value = source_volume_file.name
  728. with unittest.mock.patch('time.time') as mock_time:
  729. mock_time.side_effect = [1521065906]
  730. self.loop.run_until_complete(
  731. volume.import_volume(source_volume))
  732. uuid_after = self._get_lv_uuid(volume.path)
  733. self.assertNotEqual(uuid_after, uuid_before)
  734. self.assertEqual(volume.size, 16 * 1024 * 1024)
  735. volume_content = subprocess.check_output(['sudo', 'cat', volume.path])
  736. self.assertEqual(volume_content.rstrip(b'\0'), b'test-content')
  737. expected_revisions = {
  738. '1521065906-back': '2018-03-14T22:18:26',
  739. }
  740. self.assertEqual(volume.revisions, expected_revisions)
  741. self.loop.run_until_complete(volume.remove())
  742. def test_040_volatile(self):
  743. '''Volatile volume test'''
  744. config = {
  745. 'name': 'volatile',
  746. 'pool': self.pool.name,
  747. 'rw': True,
  748. 'size': qubes.config.defaults['root_img_size'],
  749. }
  750. vm = qubes.tests.storage.TestVM(self)
  751. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  752. # volatile volume don't need any file, verify should succeed
  753. self.assertTrue(volume.verify())
  754. self.loop.run_until_complete(volume.create())
  755. self.assertTrue(volume.verify())
  756. self.assertFalse(volume.save_on_stop)
  757. self.assertFalse(volume.snap_on_start)
  758. path = volume.path
  759. self.assertEqual(path, '/dev/' + volume.vid)
  760. self.assertFalse(os.path.exists(path))
  761. self.loop.run_until_complete(volume.start())
  762. self.assertTrue(os.path.exists(path))
  763. vol_uuid = self._get_lv_uuid(path)
  764. self.loop.run_until_complete(volume.start())
  765. self.assertTrue(os.path.exists(path))
  766. vol_uuid2 = self._get_lv_uuid(path)
  767. self.assertNotEqual(vol_uuid, vol_uuid2)
  768. self.loop.run_until_complete(volume.stop())
  769. self.assertFalse(os.path.exists(path))
  770. def test_050_snapshot_volume(self):
  771. ''' Test snapshot volume creation '''
  772. config_origin = {
  773. 'name': 'root',
  774. 'pool': self.pool.name,
  775. 'save_on_stop': True,
  776. 'rw': True,
  777. 'size': qubes.config.defaults['root_img_size'],
  778. }
  779. vm = qubes.tests.storage.TestVM(self)
  780. volume_origin = self.app.get_pool(self.pool.name).init_volume(
  781. vm, config_origin)
  782. self.loop.run_until_complete(volume_origin.create())
  783. config_snapshot = {
  784. 'name': 'root2',
  785. 'pool': self.pool.name,
  786. 'snap_on_start': True,
  787. 'source': volume_origin,
  788. 'rw': True,
  789. 'size': qubes.config.defaults['root_img_size'],
  790. }
  791. volume = self.app.get_pool(self.pool.name).init_volume(
  792. vm, config_snapshot)
  793. self.assertIsInstance(volume, ThinVolume)
  794. self.assertEqual(volume.name, 'root2')
  795. self.assertEqual(volume.pool, self.pool.name)
  796. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  797. # only origin volume really needs to exist, verify should succeed
  798. # even before create
  799. self.assertTrue(volume.verify())
  800. self.loop.run_until_complete(volume.create())
  801. path = volume.path
  802. self.assertEqual(path, '/dev/' + volume.vid)
  803. self.assertFalse(os.path.exists(path), path)
  804. self.loop.run_until_complete(volume.start())
  805. # snapshot volume isn't considered dirty at any time
  806. self.assertFalse(volume.is_dirty())
  807. # not outdated yet
  808. self.assertFalse(volume.is_outdated())
  809. origin_uuid = self._get_lv_uuid(volume_origin.path)
  810. snap_origin_uuid = self._get_lv_origin_uuid(volume._vid_snap)
  811. self.assertEqual(origin_uuid, snap_origin_uuid)
  812. # now make it outdated
  813. self.loop.run_until_complete(volume_origin.start())
  814. self.loop.run_until_complete(volume_origin.stop())
  815. self.assertTrue(volume.is_outdated())
  816. origin_uuid = self._get_lv_uuid(volume_origin.path)
  817. self.assertNotEqual(origin_uuid, snap_origin_uuid)
  818. self.loop.run_until_complete(volume.stop())
  819. # stopped volume is never outdated
  820. self.assertFalse(volume.is_outdated())
  821. path = volume.path
  822. self.assertFalse(os.path.exists(path), path)
  823. path = '/dev/' + volume._vid_snap
  824. self.assertFalse(os.path.exists(path), path)
  825. self.loop.run_until_complete(volume.remove())
  826. self.loop.run_until_complete(volume_origin.remove())
  827. def test_100_pool_list_volumes(self):
  828. config = {
  829. 'name': 'root',
  830. 'pool': self.pool.name,
  831. 'save_on_stop': True,
  832. 'rw': True,
  833. 'revisions_to_keep': 2,
  834. 'size': qubes.config.defaults['root_img_size'],
  835. }
  836. config2 = config.copy()
  837. vm = qubes.tests.storage.TestVM(self)
  838. volume1 = self.app.get_pool(self.pool.name).init_volume(vm, config)
  839. self.loop.run_until_complete(volume1.create())
  840. config2['name'] = 'private'
  841. volume2 = self.app.get_pool(self.pool.name).init_volume(vm, config2)
  842. self.loop.run_until_complete(volume2.create())
  843. # create some revisions
  844. self.loop.run_until_complete(volume1.start())
  845. self.loop.run_until_complete(volume1.stop())
  846. # and have one in dirty state
  847. self.loop.run_until_complete(volume2.start())
  848. self.assertIn(volume1, list(self.pool.volumes))
  849. self.assertIn(volume2, list(self.pool.volumes))
  850. self.loop.run_until_complete(volume1.remove())
  851. self.assertNotIn(volume1, list(self.pool.volumes))
  852. self.assertIn(volume2, list(self.pool.volumes))
  853. self.loop.run_until_complete(volume2.remove())
  854. self.assertNotIn(volume1, list(self.pool.volumes))
  855. self.assertNotIn(volume1, list(self.pool.volumes))
  856. @skipUnlessLvmPoolExists
  857. class TC_01_ThinPool(ThinPoolBase, qubes.tests.SystemTestCase):
  858. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  859. def setUp(self):
  860. super(TC_01_ThinPool, self).setUp()
  861. self.init_default_template()
  862. def test_004_import(self):
  863. template_vm = self.app.default_template
  864. name = self.make_vm_name('import')
  865. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=name,
  866. label='red')
  867. vm.clone_properties(template_vm)
  868. self.loop.run_until_complete(
  869. vm.clone_disk_files(template_vm, pool=self.pool.name))
  870. for v_name, volume in vm.volumes.items():
  871. if volume.save_on_stop:
  872. expected = "/dev/{!s}/vm-{!s}-{!s}".format(
  873. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  874. self.assertEqual(volume.path, expected)
  875. with self.assertNotRaises(qubes.exc.QubesException):
  876. self.loop.run_until_complete(vm.start())
  877. def test_005_create_appvm(self):
  878. vm = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  879. name=self.make_vm_name('appvm'), label='red')
  880. self.loop.run_until_complete(vm.create_on_disk(pool=self.pool.name))
  881. for v_name, volume in vm.volumes.items():
  882. if volume.save_on_stop:
  883. expected = "/dev/{!s}/vm-{!s}-{!s}".format(
  884. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  885. self.assertEqual(volume.path, expected)
  886. with self.assertNotRaises(qubes.exc.QubesException):
  887. self.loop.run_until_complete(vm.start())
  888. def test_006_name_suffix_parse(self):
  889. '''Regression test for #4680'''
  890. vm1 = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  891. name=self.make_vm_name('appvm'), label='red')
  892. vm2 = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  893. name=self.make_vm_name('appvm-root'), label='red')
  894. self.loop.run_until_complete(asyncio.wait([
  895. vm1.create_on_disk(pool=self.pool.name),
  896. vm2.create_on_disk(pool=self.pool.name)]))
  897. self.loop.run_until_complete(vm2.start())
  898. self.loop.run_until_complete(vm2.shutdown(wait=True))
  899. with self.assertNotRaises(ValueError):
  900. vm1.volumes['root'].size
  901. @skipUnlessLvmPoolExists
  902. class TC_02_StorageHelpers(ThinPoolBase):
  903. def setUp(self):
  904. xml_path = '/tmp/qubes-test.xml'
  905. self.app = qubes.Qubes.create_empty_store(store=xml_path,
  906. clockvm=None,
  907. updatevm=None,
  908. offline_mode=True,
  909. )
  910. os.environ['QUBES_XML_PATH'] = xml_path
  911. super(TC_02_StorageHelpers, self).setUp()
  912. # reset cache
  913. qubes.storage.DirectoryThinPool._thin_pool = {}
  914. self.thin_dir = tempfile.TemporaryDirectory()
  915. subprocess.check_call(
  916. ['sudo', 'lvcreate', '-q', '-V', '32M',
  917. '-T', DEFAULT_LVM_POOL, '-n',
  918. 'test-file-pool'], stdout=subprocess.DEVNULL)
  919. self.thin_dev = '/dev/{}/test-file-pool'.format(
  920. DEFAULT_LVM_POOL.split('/')[0])
  921. subprocess.check_call(['udevadm', 'settle'])
  922. subprocess.check_call(
  923. ['sudo', 'mkfs.ext4', '-q', self.thin_dev])
  924. subprocess.check_call(['sudo', 'mount', self.thin_dev,
  925. self.thin_dir.name])
  926. subprocess.check_call(['sudo', 'chmod', '777',
  927. self.thin_dir.name])
  928. def tearDown(self):
  929. subprocess.check_call(['sudo', 'umount', self.thin_dir.name])
  930. subprocess.check_call(
  931. ['sudo', 'lvremove', '-q', '-f', self.thin_dev],
  932. stdout = subprocess.DEVNULL)
  933. self.thin_dir.cleanup()
  934. super(TC_02_StorageHelpers, self).tearDown()
  935. os.unlink(self.app.store)
  936. del self.app
  937. for attr in dir(self):
  938. if isinstance(getattr(self, attr), qubes.vm.BaseVM):
  939. delattr(self, attr)
  940. def test_000_search_thin_pool(self):
  941. pool = qubes.storage.search_pool_containing_dir(
  942. self.app.pools.values(), self.thin_dir.name)
  943. self.assertEqual(pool, self.pool)
  944. def test_001_search_none(self):
  945. pool = qubes.storage.search_pool_containing_dir(
  946. self.app.pools.values(), '/tmp')
  947. self.assertIsNone(pool)
  948. def test_002_search_subdir(self):
  949. subdir = os.path.join(self.thin_dir.name, 'some-dir')
  950. os.mkdir(subdir)
  951. pool = qubes.storage.search_pool_containing_dir(
  952. self.app.pools.values(), subdir)
  953. self.assertEqual(pool, self.pool)
  954. def test_003_search_file_pool(self):
  955. subdir = os.path.join(self.thin_dir.name, 'some-dir')
  956. file_pool_config = {
  957. 'name': 'test-file-pool',
  958. 'driver': 'file',
  959. 'dir_path': subdir
  960. }
  961. pool2 = self.loop.run_until_complete(
  962. self.app.add_pool(**file_pool_config))
  963. pool = qubes.storage.search_pool_containing_dir(
  964. self.app.pools.values(), subdir)
  965. self.assertEqual(pool, pool2)
  966. pool = qubes.storage.search_pool_containing_dir(
  967. self.app.pools.values(), self.thin_dir.name)
  968. self.assertEqual(pool, self.pool)