storage.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.storage
  19. import qubes.log
  20. from qubes.config import defaults
  21. from qubes.storage.xen import XenPool, XenStorage
  22. from qubes.tests import QubesTestCase, SystemTestsMixin
  23. class TestApp(qubes.tests.TestEmitter):
  24. pass
  25. class TestVM(object):
  26. def __init__(self, app, qid, name, pool_name, template=None):
  27. super(TestVM, self).__init__()
  28. self.app = app
  29. self.qid = qid
  30. self.name = name
  31. self.pool_name = pool_name
  32. self.template = template
  33. self.hvm = False
  34. self.storage = qubes.storage.get_pool(
  35. self.pool_name, self).get_storage()
  36. self.log = qubes.log.get_vm_logger(self.name)
  37. def is_template(self):
  38. return False
  39. def is_disposablevm(self):
  40. return False
  41. @property
  42. def dir_path(self):
  43. return self.storage.vmdir
  44. class TestTemplateVM(TestVM):
  45. def is_template(self):
  46. return True
  47. class TestDisposableVM(TestVM):
  48. def is_disposablevm(self):
  49. return True
  50. class TC_00_Storage(QubesTestCase):
  51. """ This class tests the utility methods from :mod:``qubes.storage`` """
  52. def setUp(self):
  53. super(TC_00_Storage, self).setUp()
  54. self.app = TestApp()
  55. def test_000_dump(self):
  56. """ Dumps storage instance to a storage string """
  57. vmname = self.make_vm_name('appvm')
  58. template = TestTemplateVM(self.app, 1,
  59. qubes.tests.VMPREFIX + 'template', pool_name='default')
  60. vm = TestVM(self.app, qid=2, name=vmname, pool_name='default',
  61. template=template)
  62. storage = vm.storage
  63. result = qubes.storage.dump(storage)
  64. expected = 'qubes.storage.xen.XenStorage'
  65. self.assertEquals(result, expected)
  66. def test_001_load(self):
  67. """ Loads storage driver from a storage string """
  68. result = qubes.storage.load('qubes.storage.xen.XenStorage')
  69. self.assertTrue(result is XenStorage)
  70. def test_002_default_pool_drivers(self):
  71. """ The only predifined pool driver is xen """
  72. result = defaults['pool_drivers'].keys()
  73. expected = ["xen"]
  74. self.assertEquals(result, expected)
  75. def test_003_get_pool_klass(self):
  76. """ Expect the default pool to be `XenPool` """
  77. result = qubes.storage._get_pool_klass('default')
  78. self.assertTrue(result is XenPool)
  79. def test_004_pool_exists_default(self):
  80. """ Expect the default pool to exists """
  81. self.assertTrue(qubes.storage.pool_exists('default'))
  82. def test_005_pool_exists_random(self):
  83. """ Expect this pool to not a exist """
  84. self.assertFalse(
  85. qubes.storage.pool_exists('asdh312096r832598213iudhas'))
  86. def test_006_add_remove_pool(self):
  87. """ Tries to adding and removing a pool. """
  88. pool_name = 'asdjhrp89132'
  89. # make sure it's really does not exist
  90. qubes.storage.remove_pool(pool_name)
  91. qubes.storage.add_pool(pool_name, driver='xen')
  92. self.assertTrue(qubes.storage.pool_exists(pool_name))
  93. qubes.storage.remove_pool(pool_name)
  94. self.assertFalse(qubes.storage.pool_exists(pool_name))