tests: convert storage tests to core3 API

QubesOS/qubes-issues#
This commit is contained in:
Marek Marczykowski-Górecki 2016-02-11 05:41:42 +01:00 committed by Wojtek Porczyk
parent 14f31134c0
commit c0c0e0022e
3 changed files with 92 additions and 39 deletions

View File

@ -18,21 +18,61 @@
import qubes.storage 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.storage.xen import XenPool, XenStorage
from qubes.tests import QubesTestCase, SystemTestsMixin 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`` """ """ 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): def test_000_dump(self):
""" Dumps storage instance to a storage string """ """ Dumps storage instance to a storage string """
vmname = self.make_vm_name('appvm') vmname = self.make_vm_name('appvm')
template = self.qc.get_default_template() template = TestTemplateVM(self.app, 1,
vm = self.qc.add_new_vm('QubesAppVm', name=vmname, qubes.tests.VMPREFIX + 'template', pool_name='default')
pool_name='default', template=template) vm = TestVM(self.app, qid=2, name=vmname, pool_name='default',
template=template)
storage = vm.storage storage = vm.storage
result = qubes.storage.dump(storage) result = qubes.storage.dump(storage)
expected = 'qubes.storage.xen.XenStorage' expected = 'qubes.storage.xen.XenStorage'

View File

@ -18,12 +18,13 @@
import os import os
import shutil import shutil
import unittest
import qubes.storage import qubes.storage
import qubes.tests.storage
from qubes.tests import QubesTestCase, SystemTestsMixin from qubes.tests import QubesTestCase, SystemTestsMixin
from qubes.storage.xen import XenStorage from qubes.storage.xen import XenStorage
class TC_00_XenPool(QubesTestCase):
class TC_00_XenPool(SystemTestsMixin, QubesTestCase):
""" This class tests some properties of the 'default' pool. """ """ This class tests some properties of the 'default' pool. """
@ -35,7 +36,7 @@ class TC_00_XenPool(SystemTestsMixin, QubesTestCase):
""" """
vm = self._init_app_vm() vm = self._init_app_vm()
result = qubes.storage.get_pool("default", vm).dir_path result = qubes.storage.get_pool("default", vm).dir_path
expected = '/var/lib/qubes/' expected = '/var/lib/qubes'
self.assertEquals(result, expected) self.assertEquals(result, expected)
def test001_default_storage_class(self): def test001_default_storage_class(self):
@ -50,13 +51,15 @@ class TC_00_XenPool(SystemTestsMixin, QubesTestCase):
def _init_app_vm(self): def _init_app_vm(self):
""" Return initalised, but not created, AppVm. """ """ Return initalised, but not created, AppVm. """
app = qubes.tests.storage.TestApp()
vmname = self.make_vm_name('appvm') vmname = self.make_vm_name('appvm')
template = self.qc.get_default_template() template = qubes.tests.storage.TestTemplateVM(app, 1,
return self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, self.make_vm_name('template'), 'default')
pool_name='default') return qubes.tests.storage.TestVM(app, qid=2, name=vmname,
template=template, pool_name='default')
@qubes.tests.skipUnlessDom0
class TC_01_XenPool(SystemTestsMixin, QubesTestCase): class TC_01_XenPool(QubesTestCase):
""" Test the paths for the default Xen file based storage (``XenStorage``). """ 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() super(TC_01_XenPool, self).setUp()
qubes.storage.add_pool('test-pool', driver='xen', qubes.storage.add_pool('test-pool', driver='xen',
dir_path=self.POOL_DIR) 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): def tearDown(self):
""" Remove the file based storage pool after testing """ """ 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)) self.assertFalse(os.path.exists(self.POOL_DIR))
vmname = self.make_vm_name('appvm') vmname = self.make_vm_name('appvm')
template = self.qc.get_default_template() qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, template=self.template, pool_name='test-pool')
pool_name='test-pool')
self.assertTrue(os.path.exists(self.POOL_DIR)) self.assertTrue(os.path.exists(self.POOL_DIR))
self.assertTrue(os.path.exists(self.APPVMS_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): def test_003_pool_dir(self):
""" Check if the vm storage pool_dir is the same as specified """ """ Check if the vm storage pool_dir is the same as specified """
vmname = self.make_vm_name('appvm') vmname = self.make_vm_name('appvm')
template = self.qc.get_default_template() vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
vm = self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, template=self.template, pool_name='test-pool')
pool_name='test-pool')
result = qubes.storage.get_pool('test-pool', vm).dir_path result = qubes.storage.get_pool('test-pool', vm).dir_path
self.assertEquals(self.POOL_DIR, result) self.assertEquals(self.POOL_DIR, result)
def test_004_app_vmdir(self): def test_004_app_vmdir(self):
""" Check the vm storage dir for an AppVm""" """ Check the vm storage dir for an AppVm"""
vmname = self.make_vm_name('appvm') vmname = self.make_vm_name('appvm')
template = self.qc.get_default_template() vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
vm = self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, template=self.template, pool_name='test-pool')
pool_name='test-pool')
expected = os.path.join(self.APPVMS_DIR, vm.name) expected = os.path.join(self.APPVMS_DIR, vm.name)
result = vm.storage.vmdir result = vm.storage.vmdir
@ -121,28 +125,31 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase):
def test_005_hvm_vmdir(self): def test_005_hvm_vmdir(self):
""" Check the vm storage dir for a HVM""" """ Check the vm storage dir for a HVM"""
vmname = self.make_vm_name('hvm') vmname = self.make_vm_name('hvm')
vm = self.qc.add_new_vm('QubesHVm', name=vmname, vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
pool_name='test-pool') template=self.template, pool_name='test-pool')
vm.hvm = True
expected = os.path.join(self.APPVMS_DIR, vm.name) expected = os.path.join(self.APPVMS_DIR, vm.name)
result = vm.storage.vmdir result = vm.storage.vmdir
self.assertEquals(expected, result) self.assertEquals(expected, result)
@unittest.skip('TODO - servicevms dir?')
def test_006_net_vmdir(self): def test_006_net_vmdir(self):
""" Check the vm storage dir for a Netvm""" """ Check the vm storage dir for a Netvm"""
vmname = self.make_vm_name('hvm') vmname = self.make_vm_name('hvm')
vm = self.qc.add_new_vm('QubesNetVm', name=vmname, vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
pool_name='test-pool') template=self.template, pool_name='test-pool')
expected = os.path.join(self.SERVICE_DIR, vm.name) expected = os.path.join(self.SERVICE_DIR, vm.name)
result = vm.storage.vmdir result = vm.storage.vmdir
self.assertEquals(expected, result) self.assertEquals(expected, result)
@unittest.skip('TODO - servicevms dir?')
def test_007_proxy_vmdir(self): def test_007_proxy_vmdir(self):
""" Check the vm storage dir for a ProxyVm""" """ Check the vm storage dir for a ProxyVm"""
vmname = self.make_vm_name('proxyvm') vmname = self.make_vm_name('proxyvm')
vm = self.qc.add_new_vm('QubesProxyVm', name=vmname, vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
pool_name='test-pool') template=self.template, pool_name='test-pool')
expected = os.path.join(self.SERVICE_DIR, vm.name) expected = os.path.join(self.SERVICE_DIR, vm.name)
result = vm.storage.vmdir result = vm.storage.vmdir
@ -156,8 +163,8 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase):
def test_009_template_vmdir(self): def test_009_template_vmdir(self):
""" Check the vm storage dir for a TemplateVm""" """ Check the vm storage dir for a TemplateVm"""
vmname = self.make_vm_name('templatevm') vmname = self.make_vm_name('templatevm')
vm = self.qc.add_new_vm('QubesTemplateVm', name=vmname, vm = qubes.tests.storage.TestTemplateVM(self.app, qid=2, name=vmname,
pool_name='test-pool') pool_name='test-pool')
expected = os.path.join(self.TEMPLATES_DIR, vm.name) expected = os.path.join(self.TEMPLATES_DIR, vm.name)
result = vm.storage.vmdir result = vm.storage.vmdir
@ -166,8 +173,8 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase):
def test_010_template_hvm_vmdir(self): def test_010_template_hvm_vmdir(self):
""" Check the vm storage dir for a TemplateHVm""" """ Check the vm storage dir for a TemplateHVm"""
vmname = self.make_vm_name('templatehvm') vmname = self.make_vm_name('templatehvm')
vm = self.qc.add_new_vm('QubesTemplateHVm', name=vmname, vm = qubes.tests.storage.TestTemplateVM(self.app, qid=2, name=vmname,
pool_name='test-pool') pool_name='test-pool')
expected = os.path.join(self.TEMPLATES_DIR, vm.name) expected = os.path.join(self.TEMPLATES_DIR, vm.name)
result = vm.storage.vmdir 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""" """ Check if all the needed image files are created for an AppVm"""
vmname = self.make_vm_name('appvm') vmname = self.make_vm_name('appvm')
template = self.qc.get_default_template() vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
vm = self.qc.add_new_vm('QubesAppVm', name=vmname, template=template, pool_name='test-pool')
pool_name='test-pool') vm.storage.create_on_disk()
vm.create_on_disk(verbose=False)
expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name) expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name)
self.assertEqualsAndExists(vm.storage.vmdir, expected_vmdir) 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""" """ Check if all the needed image files are created for a HVm"""
vmname = self.make_vm_name('hvm') vmname = self.make_vm_name('hvm')
vm = self.qc.add_new_vm('QubesHVm', name=vmname, vm = qubes.tests.storage.TestVM(self.app, qid=2, name=vmname,
pool_name='test-pool') pool_name='test-pool')
vm.create_on_disk(verbose=False) vm.hvm = True
vm.storage.create_on_disk()
expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name) expected_vmdir = os.path.join(self.APPVMS_DIR, vm.name)
self.assertEqualsAndExists(vm.storage.vmdir, expected_vmdir) self.assertEqualsAndExists(vm.storage.vmdir, expected_vmdir)
@ -215,6 +222,10 @@ class TC_01_XenPool(SystemTestsMixin, QubesTestCase):
self.assertEqualsAndExists(vm.storage.volatile_img, self.assertEqualsAndExists(vm.storage.volatile_img,
expected_volatile_path) 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): def assertEqualsAndExists(self, result_path, expected_path):
""" Check if the ``result_path``, matches ``expected_path`` and exists. """ Check if the ``result_path``, matches ``expected_path`` and exists.

View File

@ -254,6 +254,8 @@ fi
%{python_sitelib}/qubes/tests/events.py* %{python_sitelib}/qubes/tests/events.py*
%{python_sitelib}/qubes/tests/init1.py* %{python_sitelib}/qubes/tests/init1.py*
%{python_sitelib}/qubes/tests/init2.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 %dir %{python_sitelib}/qubes/tests/vm
%{python_sitelib}/qubes/tests/vm/__init__.py* %{python_sitelib}/qubes/tests/vm/__init__.py*