storage_xen.py 9.0 KB

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