From c0c0e0022e411ee671d14f267f31f11b3f2f2f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Thu, 11 Feb 2016 05:41:42 +0100 Subject: [PATCH] tests: convert storage tests to core3 API QubesOS/qubes-issues# --- {tests => qubes/tests}/storage.py | 50 +++++++++++++++-- {tests => qubes/tests}/storage_xen.py | 79 +++++++++++++++------------ rpm_spec/core-dom0.spec | 2 + 3 files changed, 92 insertions(+), 39 deletions(-) rename {tests => qubes/tests}/storage.py (69%) rename {tests => qubes/tests}/storage_xen.py (77%) diff --git a/tests/storage.py b/qubes/tests/storage.py similarity index 69% rename from tests/storage.py rename to qubes/tests/storage.py index 2c32fa73..0d9b91b3 100644 --- a/tests/storage.py +++ b/qubes/tests/storage.py @@ -18,21 +18,61 @@ import qubes.storage -from qubes.qubes import defaults +import qubes.log +from qubes.config import defaults from qubes.storage.xen import XenPool, XenStorage from qubes.tests import QubesTestCase, SystemTestsMixin +class TestApp(qubes.tests.TestEmitter): + pass -class TC_00_Storage(SystemTestsMixin, QubesTestCase): +class TestVM(object): + def __init__(self, app, qid, name, pool_name, template=None): + super(TestVM, self).__init__() + self.app = app + self.qid = qid + self.name = name + self.pool_name = pool_name + self.template = template + self.hvm = False + self.storage = qubes.storage.get_pool( + self.pool_name, self).get_storage() + self.log = qubes.log.get_vm_logger(self.name) + + def is_template(self): + return False + + def is_disposablevm(self): + return False + + @property + def dir_path(self): + return self.storage.vmdir + + +class TestTemplateVM(TestVM): + def is_template(self): + return True + +class TestDisposableVM(TestVM): + def is_disposablevm(self): + return True + +class TC_00_Storage(QubesTestCase): """ This class tests the utility methods from :mod:``qubes.storage`` """ + def setUp(self): + super(TC_00_Storage, self).setUp() + self.app = TestApp() + def test_000_dump(self): """ Dumps storage instance to a storage string """ vmname = self.make_vm_name('appvm') - template = self.qc.get_default_template() - vm = self.qc.add_new_vm('QubesAppVm', name=vmname, - pool_name='default', template=template) + template = TestTemplateVM(self.app, 1, + qubes.tests.VMPREFIX + 'template', pool_name='default') + vm = TestVM(self.app, qid=2, name=vmname, pool_name='default', + template=template) storage = vm.storage result = qubes.storage.dump(storage) expected = 'qubes.storage.xen.XenStorage' diff --git a/tests/storage_xen.py b/qubes/tests/storage_xen.py similarity index 77% rename from tests/storage_xen.py rename to qubes/tests/storage_xen.py index a6503929..5954fba6 100644 --- a/tests/storage_xen.py +++ b/qubes/tests/storage_xen.py @@ -18,12 +18,13 @@ import os import shutil +import unittest import qubes.storage +import qubes.tests.storage from qubes.tests import QubesTestCase, SystemTestsMixin from qubes.storage.xen import XenStorage - -class TC_00_XenPool(SystemTestsMixin, QubesTestCase): +class TC_00_XenPool(QubesTestCase): """ This class tests some properties of the 'default' pool. """ @@ -35,7 +36,7 @@ class TC_00_XenPool(SystemTestsMixin, QubesTestCase): """ vm = self._init_app_vm() result = qubes.storage.get_pool("default", vm).dir_path - expected = '/var/lib/qubes/' + expected = '/var/lib/qubes' self.assertEquals(result, expected) def test001_default_storage_class(self): @@ -50,13 +51,15 @@ class TC_00_XenPool(SystemTestsMixin, QubesTestCase): def _init_app_vm(self): """ Return initalised, but not created, AppVm. """ + app = qubes.tests.storage.TestApp() vmname = self.make_vm_name('appvm') - template = self.qc.get_default_template() - return self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, - pool_name='default') + template = qubes.tests.storage.TestTemplateVM(app, 1, + self.make_vm_name('template'), 'default') + return qubes.tests.storage.TestVM(app, qid=2, name=vmname, + template=template, pool_name='default') - -class TC_01_XenPool(SystemTestsMixin, QubesTestCase): +@qubes.tests.skipUnlessDom0 +class TC_01_XenPool(QubesTestCase): """ Test the paths for the default Xen file based storage (``XenStorage``). """ @@ -71,6 +74,10 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): super(TC_01_XenPool, self).setUp() qubes.storage.add_pool('test-pool', driver='xen', dir_path=self.POOL_DIR) + self.app = qubes.tests.storage.TestApp() + self.template = qubes.tests.storage.TestTemplateVM(self.app, 1, + self.make_vm_name('template'), 'default') + def tearDown(self): """ Remove the file based storage pool after testing """ @@ -89,9 +96,8 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): self.assertFalse(os.path.exists(self.POOL_DIR)) vmname = self.make_vm_name('appvm') - template = self.qc.get_default_template() - self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, - pool_name='test-pool') + qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + template=self.template, pool_name='test-pool') self.assertTrue(os.path.exists(self.POOL_DIR)) self.assertTrue(os.path.exists(self.APPVMS_DIR)) @@ -101,18 +107,16 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): def test_003_pool_dir(self): """ Check if the vm storage pool_dir is the same as specified """ vmname = self.make_vm_name('appvm') - template = self.qc.get_default_template() - vm = self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, - pool_name='test-pool') + vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + template=self.template, pool_name='test-pool') result = qubes.storage.get_pool('test-pool', vm).dir_path self.assertEquals(self.POOL_DIR, result) def test_004_app_vmdir(self): """ Check the vm storage dir for an AppVm""" vmname = self.make_vm_name('appvm') - template = self.qc.get_default_template() - vm = self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, - pool_name='test-pool') + vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + template=self.template, pool_name='test-pool') expected = os.path.join(self.APPVMS_DIR, vm.name) result = vm.storage.vmdir @@ -121,28 +125,31 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): def test_005_hvm_vmdir(self): """ Check the vm storage dir for a HVM""" vmname = self.make_vm_name('hvm') - vm = self.qc.add_new_vm('QubesHVm', name=vmname, - pool_name='test-pool') + vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + template=self.template, pool_name='test-pool') + vm.hvm = True expected = os.path.join(self.APPVMS_DIR, vm.name) result = vm.storage.vmdir self.assertEquals(expected, result) + @unittest.skip('TODO - servicevms dir?') def test_006_net_vmdir(self): """ Check the vm storage dir for a Netvm""" vmname = self.make_vm_name('hvm') - vm = self.qc.add_new_vm('QubesNetVm', name=vmname, - pool_name='test-pool') + vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + template=self.template, pool_name='test-pool') expected = os.path.join(self.SERVICE_DIR, vm.name) result = vm.storage.vmdir self.assertEquals(expected, result) + @unittest.skip('TODO - servicevms dir?') def test_007_proxy_vmdir(self): """ Check the vm storage dir for a ProxyVm""" vmname = self.make_vm_name('proxyvm') - vm = self.qc.add_new_vm('QubesProxyVm', name=vmname, - pool_name='test-pool') + vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + template=self.template, pool_name='test-pool') expected = os.path.join(self.SERVICE_DIR, vm.name) result = vm.storage.vmdir @@ -156,8 +163,8 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): def test_009_template_vmdir(self): """ Check the vm storage dir for a TemplateVm""" vmname = self.make_vm_name('templatevm') - vm = self.qc.add_new_vm('QubesTemplateVm', name=vmname, - pool_name='test-pool') + vm = qubes.tests.storage.TestTemplateVM(self.app, qid=2, name=vmname, + pool_name='test-pool') expected = os.path.join(self.TEMPLATES_DIR, vm.name) result = vm.storage.vmdir @@ -166,8 +173,8 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): def test_010_template_hvm_vmdir(self): """ Check the vm storage dir for a TemplateHVm""" vmname = self.make_vm_name('templatehvm') - vm = self.qc.add_new_vm('QubesTemplateHVm', name=vmname, - pool_name='test-pool') + vm = qubes.tests.storage.TestTemplateVM(self.app, qid=2, name=vmname, + pool_name='test-pool') expected = os.path.join(self.TEMPLATES_DIR, vm.name) result = vm.storage.vmdir @@ -177,10 +184,9 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): """ Check if all the needed image files are created for an AppVm""" vmname = self.make_vm_name('appvm') - template = self.qc.get_default_template() - vm = self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, - pool_name='test-pool') - vm.create_on_disk(verbose=False) + vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + pool_name='test-pool') + vm.storage.create_on_disk() expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name) self.assertEqualsAndExists(vm.storage.vmdir, expected_vmdir) @@ -197,9 +203,10 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): """ Check if all the needed image files are created for a HVm""" vmname = self.make_vm_name('hvm') - vm = self.qc.add_new_vm('QubesHVm', name=vmname, - pool_name='test-pool') - vm.create_on_disk(verbose=False) + vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname, + pool_name='test-pool') + vm.hvm = True + vm.storage.create_on_disk() expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name) self.assertEqualsAndExists(vm.storage.vmdir, expected_vmdir) @@ -215,6 +222,10 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase): self.assertEqualsAndExists(vm.storage.volatile_img, expected_volatile_path) + @unittest.skip('test not implemented') # TODO + def test_013_template_based_file_images(self): + pass + def assertEqualsAndExists(self, result_path, expected_path): """ Check if the ``result_path``, matches ``expected_path`` and exists. diff --git a/rpm_spec/core-dom0.spec b/rpm_spec/core-dom0.spec index a7b91710..75c960c7 100644 --- a/rpm_spec/core-dom0.spec +++ b/rpm_spec/core-dom0.spec @@ -254,6 +254,8 @@ fi %{python_sitelib}/qubes/tests/events.py* %{python_sitelib}/qubes/tests/init1.py* %{python_sitelib}/qubes/tests/init2.py* +%{python_sitelib}/qubes/tests/storage.py* +%{python_sitelib}/qubes/tests/storage_xen.py* %dir %{python_sitelib}/qubes/tests/vm %{python_sitelib}/qubes/tests/vm/__init__.py*