storage_callback.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2020 David Hobach <david@hobach.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. ''' Tests for the callback storage driver.
  20. They are mostly identical to the lvm storage driver tests.
  21. '''
  22. # pylint: disable=line-too-long
  23. import os
  24. import json
  25. import subprocess
  26. import qubes.tests
  27. import qubes.tests.storage
  28. import qubes.tests.storage_lvm
  29. from qubes.tests.storage_lvm import skipUnlessLvmPoolExists
  30. import qubes.storage.callback
  31. POOL_CLASS = qubes.storage.callback.CallbackPool
  32. VOLUME_CLASS = qubes.storage.callback.CallbackVolume
  33. POOL_CONF = {'name': 'test-callback',
  34. 'driver': 'callback',
  35. 'conf_id': 'utest-callback'}
  36. CB_CONF = '/etc/qubes_callback.json'
  37. CB_DATA = {'utest-callback': {
  38. 'bdriver': 'lvm_thin',
  39. 'bdriver_args': {
  40. 'volume_group': qubes.tests.storage_lvm.DEFAULT_LVM_POOL.split('/')[0],
  41. 'thin_pool': qubes.tests.storage_lvm.DEFAULT_LVM_POOL.split('/')[1]
  42. },
  43. 'description': 'For unit testing of the callback pool driver.'
  44. }
  45. }
  46. class CallbackBase:
  47. ''' Mixin base class for callback tests. Has no base class. '''
  48. bak_pool_class = None
  49. bak_volume_class = None
  50. bak_pool_conf = None
  51. @classmethod
  52. def setUpClass(cls):
  53. CallbackBase.bak_pool_class = qubes.tests.storage_lvm.POOL_CLASS
  54. CallbackBase.bak_volume_class = qubes.tests.storage_lvm.VOLUME_CLASS
  55. CallbackBase.bak_pool_conf = qubes.tests.storage_lvm.POOL_CONF
  56. qubes.tests.storage_lvm.POOL_CLASS = POOL_CLASS
  57. qubes.tests.storage_lvm.VOLUME_CLASS = VOLUME_CLASS
  58. qubes.tests.storage_lvm.POOL_CONF = POOL_CONF
  59. assert not(os.path.exists(CB_CONF)), '%s must NOT exist. Please delete it, if you do not need it.' % CB_CONF
  60. sudo = [] if os.getuid() == 0 else ['sudo']
  61. subprocess.run(sudo + ['install', '-m', '666', '/dev/null', CB_CONF], check=True)
  62. with open(CB_CONF, 'w') as outfile:
  63. json.dump(CB_DATA, outfile)
  64. super().setUpClass()
  65. @classmethod
  66. def tearDownClass(cls):
  67. super().tearDownClass()
  68. if CallbackBase.bak_pool_class:
  69. qubes.tests.storage_lvm.POOL_CLASS = CallbackBase.bak_pool_class
  70. if CallbackBase.bak_volume_class:
  71. qubes.tests.storage_lvm.VOLUME_CLASS = CallbackBase.bak_volume_class
  72. if CallbackBase.bak_pool_conf:
  73. qubes.tests.storage_lvm.POOL_CONF = CallbackBase.bak_pool_conf
  74. sudo = [] if os.getuid() == 0 else ['sudo']
  75. subprocess.run(sudo + ['rm', '-f', CB_CONF], check=True)
  76. def setUp(self):
  77. super().setUp()
  78. #tests from other pools will assume that they're fully initialized after calling __init__()
  79. self.loop.run_until_complete(self.pool._assert_initialized())
  80. def test_000_000_callback_test_init(self):
  81. ''' Check whether the test init did work. '''
  82. self.assertIsInstance(self.pool, qubes.storage.callback.CallbackPool)
  83. self.assertTrue(os.path.isfile(CB_CONF))
  84. @skipUnlessLvmPoolExists
  85. class TC_00_CallbackPool(CallbackBase, qubes.tests.storage_lvm.TC_00_ThinPool):
  86. pass
  87. @skipUnlessLvmPoolExists
  88. class TC_01_CallbackPool(CallbackBase, qubes.tests.storage_lvm.TC_01_ThinPool):
  89. pass
  90. @skipUnlessLvmPoolExists
  91. class TC_02_cb_StorageHelpers(CallbackBase, qubes.tests.storage_lvm.TC_02_StorageHelpers):
  92. pass