storage.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import shutil
  20. import unittest.mock
  21. import qubes.log
  22. import qubes.storage
  23. from qubes.exc import QubesException
  24. from qubes.storage import pool_drivers
  25. from qubes.storage.file import FilePool
  26. from qubes.storage.reflink import ReflinkPool
  27. from qubes.tests import SystemTestCase, QubesTestCase
  28. # :pylint: disable=invalid-name
  29. class TestPool(unittest.mock.Mock):
  30. def __init__(self, *args, **kwargs):
  31. super(TestPool, self).__init__(*args, spec=qubes.storage.Pool, **kwargs)
  32. try:
  33. self.name = kwargs['name']
  34. except KeyError:
  35. pass
  36. def __str__(self):
  37. return 'test'
  38. def init_volume(self, vm, volume_config):
  39. vol = unittest.mock.Mock(spec=qubes.storage.Volume)
  40. vol.configure_mock(**volume_config)
  41. vol.pool = self
  42. vol.import_data.return_value = '/tmp/test-' + vm.name
  43. return vol
  44. class TestVM(object):
  45. def __init__(self, test, template=None):
  46. self.app = test.app
  47. self.name = test.make_vm_name('appvm')
  48. self.dir_path = '/var/lib/qubes/appvms/' + self.name
  49. self.log = qubes.log.get_vm_logger(self.name)
  50. if template:
  51. self.template = template
  52. def is_template(self):
  53. # :pylint: disable=no-self-use
  54. return False
  55. def is_disposablevm(self):
  56. # :pylint: disable=no-self-use
  57. return False
  58. class TestTemplateVM(TestVM):
  59. dir_path_prefix = qubes.config.system_path['qubes_templates_dir']
  60. def __init__(self, test, template=None):
  61. super(TestTemplateVM, self).__init__(test, template)
  62. self.dir_path = '/var/lib/qubes/vm-templates/' + self.name
  63. def is_template(self):
  64. return True
  65. class TestDisposableVM(TestVM):
  66. def is_disposablevm(self):
  67. return True
  68. class TestApp(qubes.Qubes):
  69. def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
  70. super(TestApp, self).__init__('/tmp/qubes-test.xml',
  71. load=False, offline_mode=True, **kwargs)
  72. self.load_initial_values()
  73. self.default_pool = self.pools['varlibqubes']
  74. class TC_00_Pool(QubesTestCase):
  75. """ This class tests the utility methods from :mod:``qubes.storage`` """
  76. def setUp(self):
  77. super(TC_00_Pool, self).setUp()
  78. self.basedir_patch = unittest.mock.patch('qubes.config.qubes_base_dir',
  79. '/tmp/qubes-test-basedir')
  80. self.basedir_patch.start()
  81. self.app = TestApp()
  82. def tearDown(self):
  83. self.basedir_patch.stop()
  84. self.app.close()
  85. del self.app
  86. shutil.rmtree('/tmp/qubes-test-basedir', ignore_errors=True)
  87. super().tearDown()
  88. def test_000_unknown_pool_driver(self):
  89. # :pylint: disable=protected-access
  90. """ Expect an exception when unknown pool is requested"""
  91. with self.assertRaises(QubesException):
  92. self.app.get_pool('foo-bar')
  93. def test_001_all_pool_drivers(self):
  94. """ Expect all our pool drivers (and only them) """
  95. self.assertCountEqual(
  96. ['linux-kernel', 'lvm_thin', 'file', 'file-reflink'],
  97. pool_drivers())
  98. def test_002_get_pool_klass(self):
  99. """ Expect the default pool to be `FilePool` or `ReflinkPool` """
  100. # :pylint: disable=protected-access
  101. result = self.app.get_pool('varlibqubes')
  102. self.assertTrue(isinstance(result, FilePool)
  103. or isinstance(result, ReflinkPool))
  104. def test_003_pool_exists_default(self):
  105. """ Expect the default pool to exists """
  106. self.assertPoolExists('varlibqubes')
  107. def test_004_add_remove_pool(self):
  108. """ Tries to adding and removing a pool. """
  109. pool_name = 'asdjhrp89132'
  110. # make sure it's really does not exist
  111. self.loop.run_until_complete(self.app.remove_pool(pool_name))
  112. self.assertFalse(self.assertPoolExists(pool_name))
  113. self.loop.run_until_complete(
  114. self.app.add_pool(name=pool_name,
  115. driver='file',
  116. dir_path='/tmp/asdjhrp89132'))
  117. self.assertTrue(self.assertPoolExists(pool_name))
  118. self.loop.run_until_complete(self.app.remove_pool(pool_name))
  119. self.assertFalse(self.assertPoolExists(pool_name))
  120. def assertPoolExists(self, pool):
  121. """ Check if specified pool exists """
  122. return pool in self.app.pools.keys()
  123. def test_005_remove_used(self):
  124. pool_name = 'test-pool-asdf'
  125. dir_path = '/tmp/{}'.format(pool_name)
  126. pool = self.loop.run_until_complete(
  127. self.app.add_pool(name=pool_name,
  128. driver='file',
  129. dir_path=dir_path))
  130. self.addCleanup(shutil.rmtree, dir_path)
  131. vm = self.app.add_new_vm('StandaloneVM', label='red',
  132. name=self.make_vm_name('vm'))
  133. self.loop.run_until_complete(vm.create_on_disk(pool=pool))
  134. with self.assertRaises(qubes.exc.QubesPoolInUseError):
  135. self.loop.run_until_complete(self.app.remove_pool(pool_name))