storage.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # The Qubes OS Project, https://www.qubes-os.org/
  2. #
  3. # Copyright (C) 2015 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. import qubes.log
  19. from qubes.exc import QubesException
  20. from qubes.storage import pool_drivers
  21. from qubes.storage.file import FilePool
  22. from qubes.tests import QubesTestCase
  23. # :pylint: disable=invalid-name
  24. class TestVM(object):
  25. def __init__(self, test, template=None):
  26. self.app = test.app
  27. self.name = test.make_vm_name('appvm')
  28. self.dir_path = '/var/lib/qubes/appvms/' + self.name
  29. self.log = qubes.log.get_vm_logger(self.name)
  30. if template:
  31. self.template = template
  32. def is_template(self):
  33. # :pylint: disable=no-self-use
  34. return False
  35. def is_disposablevm(self):
  36. # :pylint: disable=no-self-use
  37. return False
  38. class TestTemplateVM(TestVM):
  39. dir_path_prefix = qubes.config.system_path['qubes_templates_dir']
  40. def __init__(self, test, template=None):
  41. super(TestTemplateVM, self).__init__(test, template)
  42. self.dir_path = '/var/lib/qubes/vm-templates/' + self.name
  43. def is_template(self):
  44. return True
  45. class TestDisposableVM(TestVM):
  46. def is_disposablevm(self):
  47. return True
  48. class TestApp(qubes.Qubes):
  49. def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
  50. super(TestApp, self).__init__('/tmp/qubes-test.xml',
  51. load=False, offline_mode=True, **kwargs)
  52. self.load_initial_values()
  53. class TC_00_Pool(QubesTestCase):
  54. """ This class tests the utility methods from :mod:``qubes.storage`` """
  55. def setUp(self):
  56. super(TC_00_Pool, self).setUp()
  57. self.app = TestApp()
  58. def test_000_unknown_pool_driver(self):
  59. # :pylint: disable=protected-access
  60. """ Expect an exception when unknown pool is requested"""
  61. with self.assertRaises(QubesException):
  62. self.app.get_pool('foo-bar')
  63. def test_001_all_pool_drivers(self):
  64. """ The only predefined pool driver is file """
  65. self.assertEquals(['linux-kernel', 'lvm_thin', 'file'], pool_drivers())
  66. def test_002_get_pool_klass(self):
  67. """ Expect the default pool to be `FilePool` """
  68. # :pylint: disable=protected-access
  69. result = self.app.get_pool('default')
  70. self.assertIsInstance(result, FilePool)
  71. def test_003_pool_exists_default(self):
  72. """ Expect the default pool to exists """
  73. self.assertPoolExists('default')
  74. def test_004_add_remove_pool(self):
  75. """ Tries to adding and removing a pool. """
  76. pool_name = 'asdjhrp89132'
  77. # make sure it's really does not exist
  78. self.app.remove_pool(pool_name)
  79. self.assertFalse(self.assertPoolExists(pool_name))
  80. self.app.add_pool(name=pool_name,
  81. driver='file',
  82. dir_path='/tmp/asdjhrp89132')
  83. self.assertTrue(self.assertPoolExists(pool_name))
  84. self.app.remove_pool(pool_name)
  85. self.assertFalse(self.assertPoolExists(pool_name))
  86. def assertPoolExists(self, pool):
  87. """ Check if specified pool exists """
  88. return pool in self.app.pools.keys()