tests: adjust cleanup to added in-use storage pool detection on removal

This commit is contained in:
Marek Marczykowski-Górecki 2019-02-19 00:52:39 +01:00
parent 80e57e16be
commit 2bd709501b
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 29 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import asyncio
import os import os
import shutil import shutil
import subprocess import subprocess
from contextlib import suppress
import qubes.storage.lvm import qubes.storage.lvm
import qubes.tests import qubes.tests
@ -34,6 +35,7 @@ class StorageTestMixin(object):
def setUp(self): def setUp(self):
super(StorageTestMixin, self).setUp() super(StorageTestMixin, self).setUp()
self.init_default_template() self.init_default_template()
self.old_default_pool = self.app.default_pool
self.vm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM, self.vm1 = self.app.add_new_vm(qubes.vm.appvm.AppVM,
name=self.make_vm_name('vm1'), name=self.make_vm_name('vm1'),
label='red') label='red')
@ -47,8 +49,16 @@ class StorageTestMixin(object):
self.app.save() self.app.save()
def tearDown(self): def tearDown(self):
with suppress(qubes.exc.QubesException):
self.loop.run_until_complete(self.vm1.kill())
with suppress(qubes.exc.QubesException):
self.loop.run_until_complete(self.vm2.kill())
del self.app.domains[self.vm1]
del self.app.domains[self.vm2]
del self.vm1 del self.vm1
del self.vm2 del self.vm2
self.app.default_pool = self.old_default_pool
self.cleanup_pool()
del self.pool del self.pool
super(StorageTestMixin, self).tearDown() super(StorageTestMixin, self).tearDown()
@ -56,6 +66,10 @@ class StorageTestMixin(object):
''' Initialize storage pool to be tested, store it in self.pool''' ''' Initialize storage pool to be tested, store it in self.pool'''
raise NotImplementedError raise NotImplementedError
def cleanup_pool(self):
''' Remove tested storage pool'''
raise NotImplementedError
def test_000_volatile(self): def test_000_volatile(self):
'''Test if volatile volume is really volatile''' '''Test if volatile volume is really volatile'''
return self.loop.run_until_complete(self._test_000_volatile()) return self.loop.run_until_complete(self._test_000_volatile())
@ -320,16 +334,14 @@ class StorageFile(StorageTestMixin, qubes.tests.SystemTestCase):
os.makedirs(os.path.join(self.dir_path, 'appvms', self.vm2.name), os.makedirs(os.path.join(self.dir_path, 'appvms', self.vm2.name),
exist_ok=True) exist_ok=True)
def tearDown(self): def cleanup_pool(self):
self.loop.run_until_complete(self.app.remove_pool('test-pool')) self.loop.run_until_complete(self.app.remove_pool('test-pool'))
shutil.rmtree(self.dir_path) shutil.rmtree(self.dir_path)
super(StorageFile, self).tearDown()
class StorageReflinkMixin(StorageTestMixin): class StorageReflinkMixin(StorageTestMixin):
def tearDown(self): def cleanup_pool(self):
self.loop.run_until_complete(self.app.remove_pool(self.pool.name)) self.loop.run_until_complete(self.app.remove_pool(self.pool.name))
super().tearDown()
def init_pool(self, fs_type, **kwargs): def init_pool(self, fs_type, **kwargs):
name = 'test-reflink-integration-on-' + fs_type name = 'test-reflink-integration-on-' + fs_type
@ -352,6 +364,7 @@ class StorageReflinkOnExt4(StorageReflinkMixin, qubes.tests.SystemTestCase):
@qubes.tests.storage_lvm.skipUnlessLvmPoolExists @qubes.tests.storage_lvm.skipUnlessLvmPoolExists
class StorageLVM(StorageTestMixin, qubes.tests.SystemTestCase): class StorageLVM(StorageTestMixin, qubes.tests.SystemTestCase):
def init_pool(self): def init_pool(self):
self.created_pool = False
# check if the default LVM Thin pool qubes_dom0/pool00 exists # check if the default LVM Thin pool qubes_dom0/pool00 exists
volume_group, thin_pool = \ volume_group, thin_pool = \
qubes.tests.storage_lvm.DEFAULT_LVM_POOL.split('/', 1) qubes.tests.storage_lvm.DEFAULT_LVM_POOL.split('/', 1)
@ -361,11 +374,10 @@ class StorageLVM(StorageTestMixin, qubes.tests.SystemTestCase):
self.app.add_pool(**qubes.tests.storage_lvm.POOL_CONF)) self.app.add_pool(**qubes.tests.storage_lvm.POOL_CONF))
self.created_pool = True self.created_pool = True
def tearDown(self): def cleanup_pool(self):
''' Remove the default lvm pool if it was created only for this test ''' ''' Remove the default lvm pool if it was created only for this test '''
if self.created_pool: if self.created_pool:
self.loop.run_until_complete(self.app.remove_pool(self.pool.name)) self.loop.run_until_complete(self.app.remove_pool(self.pool.name))
super(StorageLVM, self).tearDown()
def _find_pool(self, volume_group, thin_pool): def _find_pool(self, volume_group, thin_pool):
''' Returns the pool matching the specified ``volume_group`` & ''' Returns the pool matching the specified ``volume_group`` &

View File

@ -120,6 +120,12 @@ class TC_01_FileVolumes(qubes.tests.QubesTestCase):
def tearDown(self): def tearDown(self):
""" Remove the file based storage pool after testing """ """ Remove the file based storage pool after testing """
for vm in list(self.app.domains):
if vm.name.startswith(qubes.tests.VMPREFIX):
del self.app.domains[vm]
self.app.default_template = None
del self.app.domains['test-template']
self.app.default_pool = 'varlibqubes'
self.loop.run_until_complete(self.app.remove_pool("test-pool")) self.loop.run_until_complete(self.app.remove_pool("test-pool"))
self.app.cleanup() self.app.cleanup()
self.app.close() self.app.close()
@ -393,6 +399,11 @@ class TC_03_FilePool(qubes.tests.QubesTestCase):
def tearDown(self): def tearDown(self):
""" Remove the file based storage pool after testing """ """ Remove the file based storage pool after testing """
for vm in list(self.app.domains):
if vm.name.startswith(qubes.tests.VMPREFIX):
del self.app.domains[vm]
self.app.default_template = None
del self.app.domains['test-template']
self.loop.run_until_complete(self.app.remove_pool("test-pool")) self.loop.run_until_complete(self.app.remove_pool("test-pool"))
self.app.cleanup() self.app.cleanup()
self.app.close() self.app.close()