storage_xen.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 os
  19. import shutil
  20. import unittest
  21. import qubes.storage
  22. import qubes.tests.storage
  23. from qubes.tests import QubesTestCase, SystemTestsMixin
  24. from qubes.storage.xen import XenStorage
  25. class TC_00_XenPool(QubesTestCase):
  26. """ This class tests some properties of the 'default' pool. """
  27. def test000_default_pool_dir(self):
  28. """ The predefined dir for the default pool should be ``/var/lib/qubes``
  29. .. sealso::
  30. Data :data:``qubes.qubes.defaults['pool_config']``.
  31. """
  32. vm = self._init_app_vm()
  33. result = qubes.storage.get_pool("default", vm).dir_path
  34. expected = '/var/lib/qubes'
  35. self.assertEquals(result, expected)
  36. def test001_default_storage_class(self):
  37. """ Check when using default pool the Storage is ``XenStorage``. """
  38. result = self._init_app_vm().storage
  39. self.assertIsInstance(result, XenStorage)
  40. def test_002_default_pool_name(self):
  41. """ Default pool_name is 'default'. """
  42. vm = self._init_app_vm()
  43. self.assertEquals(vm.pool_name, "default")
  44. def _init_app_vm(self):
  45. """ Return initalised, but not created, AppVm. """
  46. app = qubes.tests.storage.TestApp()
  47. vmname = self.make_vm_name('appvm')
  48. template = qubes.tests.storage.TestTemplateVM(app, 1,
  49. self.make_vm_name('template'), 'default')
  50. return qubes.tests.storage.TestVM(app, qid=2, name=vmname,
  51. template=template, pool_name='default')
  52. @qubes.tests.skipUnlessDom0
  53. class TC_01_XenPool(QubesTestCase):
  54. """ Test the paths for the default Xen file based storage (``XenStorage``).
  55. """
  56. POOL_DIR = '/var/lib/qubes/test-pool'
  57. APPVMS_DIR = '/var/lib/qubes/test-pool/appvms'
  58. TEMPLATES_DIR = '/var/lib/qubes/test-pool/vm-templates'
  59. SERVICE_DIR = '/var/lib/qubes/test-pool/servicevms'
  60. def setUp(self):
  61. """ Add a test file based storage pool """
  62. super(TC_01_XenPool, self).setUp()
  63. qubes.storage.add_pool('test-pool', driver='xen',
  64. dir_path=self.POOL_DIR)
  65. self.app = qubes.tests.storage.TestApp()
  66. self.template = qubes.tests.storage.TestTemplateVM(self.app, 1,
  67. self.make_vm_name('template'), 'default')
  68. def tearDown(self):
  69. """ Remove the file based storage pool after testing """
  70. super(TC_01_XenPool, self).tearDown()
  71. qubes.storage.remove_pool("test-pool")
  72. shutil.rmtree(self.POOL_DIR, ignore_errors=True)
  73. def test_001_pool_exists(self):
  74. """ Check if the storage pool was added to the storage pool config """
  75. self.assertTrue(qubes.storage.pool_exists('test-pool'))
  76. def test_002_pool_dir_create(self):
  77. """ Check if the storage pool dir and subdirs were created """
  78. # The dir should not exists before
  79. self.assertFalse(os.path.exists(self.POOL_DIR))
  80. vmname = self.make_vm_name('appvm')
  81. qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  82. template=self.template, pool_name='test-pool')
  83. self.assertTrue(os.path.exists(self.POOL_DIR))
  84. self.assertTrue(os.path.exists(self.APPVMS_DIR))
  85. self.assertTrue(os.path.exists(self.SERVICE_DIR))
  86. self.assertTrue(os.path.exists(self.TEMPLATES_DIR))
  87. def test_003_pool_dir(self):
  88. """ Check if the vm storage pool_dir is the same as specified """
  89. vmname = self.make_vm_name('appvm')
  90. vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  91. template=self.template, pool_name='test-pool')
  92. result = qubes.storage.get_pool('test-pool', vm).dir_path
  93. self.assertEquals(self.POOL_DIR, result)
  94. def test_004_app_vmdir(self):
  95. """ Check the vm storage dir for an AppVm"""
  96. vmname = self.make_vm_name('appvm')
  97. vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  98. template=self.template, pool_name='test-pool')
  99. expected = os.path.join(self.APPVMS_DIR, vm.name)
  100. result = vm.storage.vmdir
  101. self.assertEquals(expected, result)
  102. def test_005_hvm_vmdir(self):
  103. """ Check the vm storage dir for a HVM"""
  104. vmname = self.make_vm_name('hvm')
  105. vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  106. template=self.template, pool_name='test-pool')
  107. vm.hvm = True
  108. expected = os.path.join(self.APPVMS_DIR, vm.name)
  109. result = vm.storage.vmdir
  110. self.assertEquals(expected, result)
  111. @unittest.skip('TODO - servicevms dir?')
  112. def test_006_net_vmdir(self):
  113. """ Check the vm storage dir for a Netvm"""
  114. vmname = self.make_vm_name('hvm')
  115. vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  116. template=self.template, pool_name='test-pool')
  117. expected = os.path.join(self.SERVICE_DIR, vm.name)
  118. result = vm.storage.vmdir
  119. self.assertEquals(expected, result)
  120. @unittest.skip('TODO - servicevms dir?')
  121. def test_007_proxy_vmdir(self):
  122. """ Check the vm storage dir for a ProxyVm"""
  123. vmname = self.make_vm_name('proxyvm')
  124. vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  125. template=self.template, pool_name='test-pool')
  126. expected = os.path.join(self.SERVICE_DIR, vm.name)
  127. result = vm.storage.vmdir
  128. self.assertEquals(expected, result)
  129. def test_008_admin_vmdir(self):
  130. """ Check the vm storage dir for a AdminVm"""
  131. # TODO How to test AdminVm?
  132. pass
  133. def test_009_template_vmdir(self):
  134. """ Check the vm storage dir for a TemplateVm"""
  135. vmname = self.make_vm_name('templatevm')
  136. vm = qubes.tests.storage.TestTemplateVM(self.app, qid=2, name=vmname,
  137. pool_name='test-pool')
  138. expected = os.path.join(self.TEMPLATES_DIR, vm.name)
  139. result = vm.storage.vmdir
  140. self.assertEquals(expected, result)
  141. def test_010_template_hvm_vmdir(self):
  142. """ Check the vm storage dir for a TemplateHVm"""
  143. vmname = self.make_vm_name('templatehvm')
  144. vm = qubes.tests.storage.TestTemplateVM(self.app, qid=2, name=vmname,
  145. pool_name='test-pool')
  146. expected = os.path.join(self.TEMPLATES_DIR, vm.name)
  147. result = vm.storage.vmdir
  148. self.assertEquals(expected, result)
  149. def test_011_appvm_file_images(self):
  150. """ Check if all the needed image files are created for an AppVm"""
  151. vmname = self.make_vm_name('appvm')
  152. vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  153. pool_name='test-pool')
  154. vm.storage.create_on_disk()
  155. expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name)
  156. self.assertEqualsAndExists(vm.storage.vmdir, expected_vmdir)
  157. expected_private_path = os.path.join(expected_vmdir, 'private.img')
  158. self.assertEqualsAndExists(vm.storage.private_img,
  159. expected_private_path)
  160. expected_volatile_path = os.path.join(expected_vmdir, 'volatile.img')
  161. self.assertEqualsAndExists(vm.storage.volatile_img,
  162. expected_volatile_path)
  163. def test_012_hvm_file_images(self):
  164. """ Check if all the needed image files are created for a HVm"""
  165. vmname = self.make_vm_name('hvm')
  166. vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
  167. pool_name='test-pool')
  168. vm.hvm = True
  169. vm.storage.create_on_disk()
  170. expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name)
  171. self.assertEqualsAndExists(vm.storage.vmdir, expected_vmdir)
  172. expected_private_path = os.path.join(expected_vmdir, 'private.img')
  173. self.assertEqualsAndExists(vm.storage.private_img,
  174. expected_private_path)
  175. expected_root_path = os.path.join(expected_vmdir, 'root.img')
  176. self.assertEqualsAndExists(vm.storage.root_img, expected_root_path)
  177. expected_volatile_path = os.path.join(expected_vmdir, 'volatile.img')
  178. self.assertEqualsAndExists(vm.storage.volatile_img,
  179. expected_volatile_path)
  180. @unittest.skip('test not implemented') # TODO
  181. def test_013_template_based_file_images(self):
  182. pass
  183. def assertEqualsAndExists(self, result_path, expected_path):
  184. """ Check if the ``result_path``, matches ``expected_path`` and exists.
  185. See also: :meth:``assertExist``
  186. """
  187. self.assertEquals(result_path, expected_path)
  188. self.assertExist(result_path)
  189. def assertExist(self, path):
  190. """ Assert that the given path exists. """
  191. self.assertTrue(os.path.exists(path))