Skip to content

Commit

Permalink
tests: various fixes for storage tests
Browse files Browse the repository at this point in the history
 - improve TestPool mock - init_volume now return appropriate mock type,
 instead of TestPool
 - improve patching base directory (/var/lib/qubes) - it is stored in
 more than one place...
 - fix inheritance in TC_01_ThinPool class
 - fix expected LVM volume names ('vm-' prefix)
 - fix cleanup after FilePool tests - remove temporary qubes.xml

QubesOS/qubes-issues#2256
  • Loading branch information
marmarek committed Jul 25, 2017
1 parent 8c848fc commit eda7ce7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
12 changes: 12 additions & 0 deletions qubes/tests/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#
import unittest.mock
import qubes.log
import qubes.storage
from qubes.exc import QubesException
from qubes.storage import pool_drivers
from qubes.storage.file import FilePool
Expand All @@ -30,10 +31,21 @@
class TestPool(unittest.mock.Mock):
def __init__(self, *args, **kwargs):
super(TestPool, self).__init__(*args, spec=qubes.storage.Pool, **kwargs)
try:
self.name = kwargs['name']
except KeyError:
pass

def __str__(self):
return 'test'

def init_volume(self, vm, volume_config):
vol = unittest.mock.Mock(spec=qubes.storage.Volume)
vol.configure_mock(**volume_config)
vol.pool = self
vol.import_data.return_value = '/tmp/test-' + vm.name
return vol


class TestVM(object):
def __init__(self, test, template=None):
Expand Down
22 changes: 18 additions & 4 deletions qubes/tests/storage_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import shutil

import asyncio
import unittest.mock

import qubes.storage
import qubes.tests.storage
Expand All @@ -50,6 +51,8 @@ def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
def cleanup(self):
''' Remove temporary directories '''
shutil.rmtree(self.pools['linux-kernel'].dir_path)
if os.path.exists(self.store):
os.unlink(self.store)

def create_dummy_template(self):
''' Initalizes a dummy TemplateVM as the `default_template` '''
Expand Down Expand Up @@ -306,21 +309,32 @@ class TC_03_FilePool(qubes.tests.QubesTestCase):
def setUp(self):
""" Add a test file based storage pool """
super(TC_03_FilePool, self).setUp()
self._orig_qubes_base_dir = qubes.config.qubes_base_dir
qubes.config.qubes_base_dir = '/tmp/qubes-test'
self.test_base_dir = '/tmp/qubes-test-dir'
self.base_dir_patch = unittest.mock.patch.dict(qubes.config.system_path,
{'qubes_base_dir': self.test_base_dir})
self.base_dir_patch2 = unittest.mock.patch(
'qubes.config.qubes_base_dir', self.test_base_dir)
self.base_dir_patch3 = unittest.mock.patch.dict(
qubes.config.defaults['pool_configs']['varlibqubes'],
{'dir_path': self.test_base_dir})
self.base_dir_patch.start()
self.base_dir_patch2.start()
self.base_dir_patch3.start()
self.app = TestApp()
self.app.create_dummy_template()
self.app.add_pool(**self.POOL_CONFIG)
self.app.create_dummy_template()

def tearDown(self):
""" Remove the file based storage pool after testing """
self.app.remove_pool("test-pool")
self.app.cleanup()
self.base_dir_patch3.stop()
self.base_dir_patch2.stop()
self.base_dir_patch.stop()
super(TC_03_FilePool, self).tearDown()
shutil.rmtree(self.POOL_DIR, ignore_errors=True)
if os.path.exists('/tmp/qubes-test'):
shutil.rmtree('/tmp/qubes-test')
qubes.config.qubes_base_dir = self._orig_qubes_base_dir

def test_001_pool_exists(self):
""" Check if the storage pool was added to the storage pool config """
Expand Down
6 changes: 3 additions & 3 deletions qubes/tests/storage_lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_003_read_write_volume(self):
volume.remove()

@skipUnlessLvmPoolExists
class TC_01_ThinPool(qubes.tests.SystemTestCase, ThinPoolBase):
class TC_01_ThinPool(ThinPoolBase, qubes.tests.SystemTestCase):
''' Sanity tests for :py:class:`qubes.storage.lvm.ThinPool` '''

def setUp(self):
Expand All @@ -176,7 +176,7 @@ def test_004_import(self):
vm.clone_disk_files(template_vm, pool='test-lvm')
for v_name, volume in vm.volumes.items():
if volume.save_on_stop:
expected = "/dev/{!s}/{!s}-{!s}".format(
expected = "/dev/{!s}/vm-{!s}-{!s}".format(
DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
self.assertEqual(volume.path, expected)
with self.assertNotRaises(qubes.exc.QubesException):
Expand All @@ -188,7 +188,7 @@ def test_005_create_appvm(self):
vm.create_on_disk(pool='test-lvm')
for v_name, volume in vm.volumes.items():
if volume.save_on_stop:
expected = "/dev/{!s}/{!s}-{!s}".format(
expected = "/dev/{!s}/vm-{!s}-{!s}".format(
DEFAULT_LVM_POOL.split('/')[0], vm.name, v_name)
self.assertEqual(volume.path, expected)
with self.assertNotRaises(qubes.exc.QubesException):
Expand Down

0 comments on commit eda7ce7

Please sign in to comment.