storage.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 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. import qubes.log
  21. from qubes.exc import QubesException
  22. from qubes.storage import pool_drivers
  23. from qubes.storage.file import FilePool
  24. from qubes.tests import QubesTestCase
  25. # :pylint: disable=invalid-name
  26. class TestVM(object):
  27. def __init__(self, test, template=None):
  28. self.app = test.app
  29. self.name = test.make_vm_name('appvm')
  30. self.dir_path = '/var/lib/qubes/appvms/' + self.name
  31. self.log = qubes.log.get_vm_logger(self.name)
  32. if template:
  33. self.template = template
  34. def is_template(self):
  35. # :pylint: disable=no-self-use
  36. return False
  37. def is_disposablevm(self):
  38. # :pylint: disable=no-self-use
  39. return False
  40. class TestTemplateVM(TestVM):
  41. dir_path_prefix = qubes.config.system_path['qubes_templates_dir']
  42. def __init__(self, test, template=None):
  43. super(TestTemplateVM, self).__init__(test, template)
  44. self.dir_path = '/var/lib/qubes/vm-templates/' + self.name
  45. def is_template(self):
  46. return True
  47. class TestDisposableVM(TestVM):
  48. def is_disposablevm(self):
  49. return True
  50. class TestApp(qubes.Qubes):
  51. def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
  52. super(TestApp, self).__init__('/tmp/qubes-test.xml',
  53. load=False, offline_mode=True, **kwargs)
  54. self.load_initial_values()
  55. class TC_00_Pool(QubesTestCase):
  56. """ This class tests the utility methods from :mod:``qubes.storage`` """
  57. def setUp(self):
  58. super(TC_00_Pool, self).setUp()
  59. self.app = TestApp()
  60. def test_000_unknown_pool_driver(self):
  61. # :pylint: disable=protected-access
  62. """ Expect an exception when unknown pool is requested"""
  63. with self.assertRaises(QubesException):
  64. self.app.get_pool('foo-bar')
  65. def test_001_all_pool_drivers(self):
  66. """ The only predefined pool driver is file """
  67. self.assertCountEqual(['linux-kernel', 'lvm_thin', 'file'], pool_drivers())
  68. def test_002_get_pool_klass(self):
  69. """ Expect the default pool to be `FilePool` """
  70. # :pylint: disable=protected-access
  71. result = self.app.get_pool('default')
  72. self.assertIsInstance(result, FilePool)
  73. def test_003_pool_exists_default(self):
  74. """ Expect the default pool to exists """
  75. self.assertPoolExists('default')
  76. def test_004_add_remove_pool(self):
  77. """ Tries to adding and removing a pool. """
  78. pool_name = 'asdjhrp89132'
  79. # make sure it's really does not exist
  80. self.app.remove_pool(pool_name)
  81. self.assertFalse(self.assertPoolExists(pool_name))
  82. self.app.add_pool(name=pool_name,
  83. driver='file',
  84. dir_path='/tmp/asdjhrp89132')
  85. self.assertTrue(self.assertPoolExists(pool_name))
  86. self.app.remove_pool(pool_name)
  87. self.assertFalse(self.assertPoolExists(pool_name))
  88. def assertPoolExists(self, pool):
  89. """ Check if specified pool exists """
  90. return pool in self.app.pools.keys()