storage_lvm.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # vim: fileencoding=utf-8
  2. # pylint: disable=missing-docstring
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2016 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. ''' Tests for lvm storage driver. By default tests are going to use the
  23. 'qubes_dom0/pool00'. An alternative LVM thin pool may be provided via
  24. :envvar:`DEFAULT_LVM_POOL` shell variable.
  25. Any pool variables prefixed with 'LVM_' or 'lvm_' represent a LVM
  26. 'volume_group/thin_pool' combination. Pool variables without a prefix
  27. represent a :py:class:`qubes.storage.lvm.ThinPool`.
  28. '''
  29. import os
  30. import unittest
  31. import qubes.tests
  32. from qubes.storage.lvm import ThinPool, ThinVolume
  33. if 'DEFAULT_LVM_POOL' in os.environ.keys():
  34. DEFAULT_LVM_POOL = os.environ['DEFAULT_LVM_POOL']
  35. else:
  36. DEFAULT_LVM_POOL = 'qubes_dom0/pool00'
  37. def lvm_pool_exists(volume_group, thin_pool):
  38. ''' Returns ``True`` if thin pool exists in the volume group. '''
  39. path = "/dev/mapper/{!s}-{!s}".format(volume_group, thin_pool)
  40. return os.path.exists(path)
  41. def skipUnlessLvmPoolExists(test_item): # pylint: disable=invalid-name
  42. ''' Decorator that skips LVM tests if the default pool is missing. '''
  43. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  44. result = lvm_pool_exists(volume_group, thin_pool)
  45. msg = 'LVM thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  46. return unittest.skipUnless(result, msg)(test_item)
  47. POOL_CONF = {'name': 'test-lvm',
  48. 'driver': 'lvm_thin',
  49. 'volume_group': 'qubes_dom0',
  50. 'thin_pool': 'pool00'}
  51. @skipUnlessLvmPoolExists
  52. class TC_00_ThinPool(qubes.tests.SystemTestsMixin,
  53. qubes.tests.QubesTestCase):
  54. ''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''
  55. created_pool = False
  56. def setUp(self):
  57. super(TC_00_ThinPool, self).setUp()
  58. volume_group, thin_pool = DEFAULT_LVM_POOL.split('/', 1)
  59. self.pool = self._find_pool(volume_group, thin_pool)
  60. if not self.pool:
  61. self.pool = self.app.add_pool(**POOL_CONF)
  62. self.created_pool = True
  63. self.init_default_template()
  64. def tearDown(self):
  65. ''' Remove the default lvm pool if it was created only for this test '''
  66. if self.created_pool:
  67. self.app.remove_pool(self.pool.name)
  68. super(TC_00_ThinPool, self).tearDown()
  69. def _find_pool(self, volume_group, thin_pool):
  70. ''' Returns the pool matching the specified ``volume_group`` &
  71. ``thin_pool``, or None.
  72. '''
  73. pools = [p for p in self.app.pools
  74. if issubclass(p.__class__, ThinPool)]
  75. for pool in pools:
  76. if pool.volume_group == volume_group \
  77. and pool.thin_pool == thin_pool:
  78. return pool
  79. return None
  80. def test_000_default_thin_pool(self):
  81. ''' Check whether :py:data`DEFAULT_LVM_POOL` exists. This pool is
  82. created by default, if at installation time LVM + Thin was chosen.
  83. '''
  84. msg = 'Thin pool {!r} does not exist'.format(DEFAULT_LVM_POOL)
  85. self.assertTrue(self.pool, msg)
  86. def test_001_origin_volume(self):
  87. ''' Test origin volume creation '''
  88. config = {
  89. 'name': 'root',
  90. 'pool': self.pool.name,
  91. 'save_on_stop': True,
  92. 'rw': True,
  93. 'size': qubes.config.defaults['root_img_size'],
  94. }
  95. vm = qubes.tests.storage.TestVM(self)
  96. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  97. self.assertIsInstance(volume, ThinVolume)
  98. self.assertEqual(volume.name, 'root')
  99. self.assertEqual(volume.pool, self.pool.name)
  100. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  101. self.pool.create(volume)
  102. path = "/dev/%s" % volume.vid
  103. self.assertTrue(os.path.exists(path))
  104. self.pool.remove(volume)
  105. def test_003_read_write_volume(self):
  106. ''' Test read-write volume creation '''
  107. config = {
  108. 'name': 'root',
  109. 'pool': self.pool.name,
  110. 'rw': True,
  111. 'save_on_stop': True,
  112. 'size': qubes.config.defaults['root_img_size'],
  113. }
  114. vm = qubes.tests.storage.TestVM(self)
  115. volume = self.app.get_pool(self.pool.name).init_volume(vm, config)
  116. self.assertIsInstance(volume, ThinVolume)
  117. self.assertEqual(volume.name, 'root')
  118. self.assertEqual(volume.pool, self.pool.name)
  119. self.assertEqual(volume.size, qubes.config.defaults['root_img_size'])
  120. self.pool.create(volume)
  121. path = "/dev/%s" % volume.vid
  122. self.assertTrue(os.path.exists(path))
  123. self.pool.remove(volume)
  124. def test_004_import(self):
  125. template_vm = self.app.default_template
  126. name = self.make_vm_name('import')
  127. vm = self.app.add_new_vm(qubes.vm.templatevm.TemplateVM, name=name,
  128. label='red')
  129. vm.clone_properties(template_vm)
  130. vm.clone_disk_files(template_vm, pool='test-lvm')
  131. for v_name, volume in vm.volumes.items():
  132. if volume.save_on_stop:
  133. expected = "/dev/qubes_dom0/{!s}-{!s}".format(vm.name, v_name)
  134. self.assertEqual(volume.path, expected)
  135. with self.assertNotRaises(qubes.exc.QubesException):
  136. vm.start()
  137. def test_005_create_appvm(self):
  138. vm = self.app.add_new_vm(cls=qubes.vm.appvm.AppVM,
  139. name=self.make_vm_name('appvm'), label='red')
  140. vm.create_on_disk(pool='test-lvm')
  141. for v_name, volume in vm.volumes.items():
  142. if volume.save_on_stop:
  143. expected = "/dev/qubes_dom0/{!s}-{!s}".format(vm.name, v_name)
  144. self.assertEqual(volume.path, expected)
  145. with self.assertNotRaises(qubes.exc.QubesException):
  146. vm.start()