storage.py 3.6 KB

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