storage_lvm.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 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 lvm storage driver. By default tests are going to use the
  21. 'qubes_dom0/pool00'. An alternative LVM thin pool may be provided via
  22. :envvar:`DEFAULT_LVM_POOL` shell variable.
  23. Any pool variables prefixed with 'LVM_' or 'lvm_' represent a LVM
  24. 'volume_group/thin_pool' combination. Pool variables without a prefix
  25. represent a :py:class:`qubes.storage.lvm.ThinPool`.
  26. '''
  27. import os
  28. import unittest
  29. import qubes.tests
  30. from qubes.storage.lvm import ThinPool, ThinVolume
  31. if 'DEFAULT_LVM_POOL' in os.environ.keys():
  32. DEFAULT_LVM_POOL = os.environ['DEFAULT_LVM_POOL']
  33. else:
  34. DEFAULT_LVM_POOL = 'qubes_dom0/pool00'
  35. def lvm_pool_exists(volume_group, thin_pool):
  36. ''' Returns ``True`` if thin pool exists in the volume group. '''
  37. path = "/dev/mapper/{!s}-{!s}".format(volume_group, thin_pool)
  38. return os.path.exists(path)
  39. def skipUnlessLvmPoolExists(test_item): # pylint: disable=invalid-name
  40. ''' Decorator that skips LVM tests if the default pool is missing. '''
  41. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  42. result = lvm_pool_exists(volume_group, thin_pool)
  43. msg = 'LVM thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  44. return unittest.skipUnless(result, msg)(test_item)
  45. POOL_CONF = {'name': 'test-lvm',
  46. 'driver': 'lvm_thin',
  47. 'volume_group': DEFAULT_LVM_POOL.split('/')[0],
  48. 'thin_pool': DEFAULT_LVM_POOL.split('/')[1]}
  49. @skipUnlessLvmPoolExists
  50. class TC_00_ThinPool(qubes.tests.SystemTestsMixin,
  51. qubes.tests.QubesTestCase):
  52. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  53. created_pool = False
  54. def setUp(self):
  55. super(TC_00_ThinPool, self).setUp()
  56. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  57. self.pool = self._find_pool(volume_group, thin_pool)
  58. if not self.pool:
  59. self.pool = self.app.add_pool(**POOL_CONF)
  60. self.created_pool = True
  61. self.init_default_template()
  62. def tearDown(self):
  63. ''' Remove the default lvm pool if it was created only for this test '''
  64. if self.created_pool:
  65. self.app.remove_pool(self.pool.name)
  66. super(TC_00_ThinPool, self).tearDown()
  67. def _find_pool(self, volume_group, thin_pool):
  68. ''' Returns the pool matching the specified ``volume_group`` &
  69. ``thin_pool``, or None.
  70. '''
  71. pools = [p for p in self.app.pools
  72. if issubclass(p.__class__, ThinPool)]
  73. for pool in pools:
  74. if pool.volume_group == volume_group \
  75. and pool.thin_pool == thin_pool:
  76. return pool
  77. return None
  78. def test_000_default_thin_pool(self):
  79. ''' Check whether :py:data`DEFAULT_LVM_POOL` exists. This pool is
  80. created by default, if at installation time LVM + Thin was chosen.
  81. '''
  82. msg = 'Thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  83. self.assertTrue(self.pool, msg)
  84. def test_001_origin_volume(self):
  85. ''' Test origin volume creation '''
  86. config = {
  87. 'name': 'root',
  88. 'pool': self.pool.name,
  89. 'save_on_stop': True,
  90. 'rw': True,
  91. 'size': qubes.config.defaults['root_img_size'],
  92. }
  93. vm = qubes.tests.storage.TestVM(self)
  94. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  95. self.assertIsInstance(volume, ThinVolume)
  96. self.assertEqual(volume.name, 'root')
  97. self.assertEqual(volume.pool, self.pool.name)
  98. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  99. self.pool.create(volume)
  100. path = "/dev/%s" % volume.vid
  101. self.assertTrue(os.path.exists(path))
  102. self.pool.remove(volume)
  103. def test_003_read_write_volume(self):
  104. ''' Test read-write volume creation '''
  105. config = {
  106. 'name': 'root',
  107. 'pool': self.pool.name,
  108. 'rw': True,
  109. 'save_on_stop': True,
  110. 'size': qubes.config.defaults['root_img_size'],
  111. }
  112. vm = qubes.tests.storage.TestVM(self)
  113. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  114. self.assertIsInstance(volume, ThinVolume)
  115. self.assertEqual(volume.name, 'root')
  116. self.assertEqual(volume.pool, self.pool.name)
  117. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  118. self.pool.create(volume)
  119. path = "/dev/%s" % volume.vid
  120. self.assertTrue(os.path.exists(path))
  121. self.pool.remove(volume)
  122. def test_004_import(self):
  123. template_vm = self.app.default_template
  124. name = self.make_vm_name('import')
  125. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=name,
  126. label='red')
  127. vm.clone_properties(template_vm)
  128. vm.clone_disk_files(template_vm, pool='test-lvm')
  129. for v_name, volume in vm.volumes.items():
  130. if volume.save_on_stop:
  131. expected = "/dev/{!s}/{!s}-{!s}".format(
  132. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  133. self.assertEqual(volume.path, expected)
  134. with self.assertNotRaises(qubes.exc.QubesException):
  135. vm.start()
  136. def test_005_create_appvm(self):
  137. vm = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  138. name=self.make_vm_name('appvm'), label='red')
  139. vm.create_on_disk(pool='test-lvm')
  140. for v_name, volume in vm.volumes.items():
  141. if volume.save_on_stop:
  142. expected = "/dev/{!s}/{!s}-{!s}".format(
  143. DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
  144. self.assertEqual(volume.path, expected)
  145. with self.assertNotRaises(qubes.exc.QubesException):
  146. vm.start()