tests: minor formating

This commit is contained in:
Marek Marczykowski-Górecki 2015-11-25 01:08:15 +01:00
parent 0c476f014d
commit 760786d999
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -133,14 +133,12 @@ class QubesTestCase(unittest.TestCase):
self.__class__.__name__, self.__class__.__name__,
self._testMethodName)) self._testMethodName))
def __str__(self): def __str__(self):
return '{}/{}/{}'.format( return '{}/{}/{}'.format(
'.'.join(self.__class__.__module__.split('.')[2:]), '.'.join(self.__class__.__module__.split('.')[2:]),
self.__class__.__name__, self.__class__.__name__,
self._testMethodName) self._testMethodName)
def tearDown(self): def tearDown(self):
super(QubesTestCase, self).tearDown() super(QubesTestCase, self).tearDown()
@ -153,7 +151,6 @@ class QubesTestCase(unittest.TestCase):
and filter((lambda (tc, exc): tc is self), l): and filter((lambda (tc, exc): tc is self), l):
raise BeforeCleanExit() raise BeforeCleanExit()
def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs): def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs):
"""Fail if an exception of class excClass is raised """Fail if an exception of class excClass is raised
by callableObj when invoked with arguments args and keyword by callableObj when invoked with arguments args and keyword
@ -183,15 +180,14 @@ class QubesTestCase(unittest.TestCase):
with context: with context:
callableObj(*args, **kwargs) callableObj(*args, **kwargs)
def assertXMLEqual(self, xml1, xml2): def assertXMLEqual(self, xml1, xml2):
'''Check for equality of two XML objects. """Check for equality of two XML objects.
:param xml1: first element :param xml1: first element
:param xml2: second element :param xml2: second element
:type xml1: :py:class:`lxml.etree._Element` :type xml1: :py:class:`lxml.etree._Element`
:type xml2: :py:class:`lxml.etree._Element` :type xml2: :py:class:`lxml.etree._Element`
''' # pylint: disable=invalid-name """ # pylint: disable=invalid-name
self.assertEqual(xml1.tag, xml2.tag) self.assertEqual(xml1.tag, xml2.tag)
self.assertEqual(xml1.text, xml2.text) self.assertEqual(xml1.text, xml2.text)
@ -202,13 +198,13 @@ class QubesTestCase(unittest.TestCase):
class SystemTestsMixin(object): class SystemTestsMixin(object):
def setUp(self): def setUp(self):
'''Set up the test. """Set up the test.
.. warning:: .. warning::
This method instantiates QubesVmCollection acquires write lock for This method instantiates QubesVmCollection acquires write lock for
it. You can use is as :py:attr:`qc`. You can (and probably it. You can use is as :py:attr:`qc`. You can (and probably
should) release the lock at the end of setUp in subclass should) release the lock at the end of setUp in subclass
''' """
super(SystemTestsMixin, self).setUp() super(SystemTestsMixin, self).setUp()
@ -220,12 +216,13 @@ class SystemTestsMixin(object):
self.remove_test_vms() self.remove_test_vms()
def tearDown(self): def tearDown(self):
super(SystemTestsMixin, self).tearDown() super(SystemTestsMixin, self).tearDown()
try: self.qc.lock_db_for_writing() try:
except qubes.qubes.QubesException: pass self.qc.lock_db_for_writing()
except qubes.qubes.QubesException:
pass
self.qc.load() self.qc.load()
self.remove_test_vms() self.remove_test_vms()
@ -236,7 +233,6 @@ class SystemTestsMixin(object):
self.conn.close() self.conn.close()
def make_vm_name(self, name): def make_vm_name(self, name):
return VMPREFIX + name return VMPREFIX + name
@ -253,13 +249,18 @@ class SystemTestsMixin(object):
# XXX .is_running() may throw libvirtError if undefined # XXX .is_running() may throw libvirtError if undefined
if vm.is_running(): if vm.is_running():
vm.force_shutdown() vm.force_shutdown()
except: pass except:
pass
try: vm.remove_from_disk() try:
except: pass vm.remove_from_disk()
except:
pass
try: vm.libvirt_domain.undefine() try:
except libvirt.libvirtError: pass vm.libvirt_domain.undefine()
except libvirt.libvirtError:
pass
self.qc.pop(vm.qid) self.qc.pop(vm.qid)
del vm del vm
@ -268,13 +269,13 @@ class SystemTestsMixin(object):
# for example if vm.libvirtDomain malfunctioned. # for example if vm.libvirtDomain malfunctioned.
try: try:
dom = self.conn.lookupByName(vmname) dom = self.conn.lookupByName(vmname)
except: pass except:
pass
else: else:
self._remove_vm_libvirt(dom) self._remove_vm_libvirt(dom)
self._remove_vm_disk(vmname) self._remove_vm_disk(vmname)
def _remove_vm_libvirt(self, dom): def _remove_vm_libvirt(self, dom):
try: try:
dom.destroy() dom.destroy()
@ -282,7 +283,6 @@ class SystemTestsMixin(object):
pass pass
dom.undefine() dom.undefine()
def _remove_vm_disk(self, vmname): def _remove_vm_disk(self, vmname):
for dirspec in ( for dirspec in (
'qubes_appvms_dir', 'qubes_appvms_dir',
@ -296,21 +296,19 @@ class SystemTestsMixin(object):
else: else:
os.unlink(dirpath) os.unlink(dirpath)
def remove_vms(self, vms): def remove_vms(self, vms):
for vm in vms: self._remove_vm_qubes(vm) for vm in vms: self._remove_vm_qubes(vm)
self.save_and_reload_db() self.save_and_reload_db()
def remove_test_vms(self): def remove_test_vms(self):
'''Aggresively remove any domain that has name in testing namespace. """Aggresively remove any domain that has name in testing namespace.
.. warning:: .. warning::
The test suite hereby claims any domain whose name starts with The test suite hereby claims any domain whose name starts with
:py:data:`VMPREFIX` as fair game. This is needed to enforce sane :py:data:`VMPREFIX` as fair game. This is needed to enforce sane
test executing environment. If you have domains named ``test-*``, test executing environment. If you have domains named ``test-*``,
don't run the tests. don't run the tests.
''' """
# first, remove them Qubes-way # first, remove them Qubes-way
something_removed = False something_removed = False
@ -392,28 +390,23 @@ class BackupTestsMixin(SystemTestsMixin):
shutil.rmtree(self.backupdir) shutil.rmtree(self.backupdir)
os.mkdir(self.backupdir) os.mkdir(self.backupdir)
def tearDown(self): def tearDown(self):
super(BackupTestsMixin, self).tearDown() super(BackupTestsMixin, self).tearDown()
shutil.rmtree(self.backupdir) shutil.rmtree(self.backupdir)
def print_progress(self, progress): def print_progress(self, progress):
if self.verbose: if self.verbose:
print >> sys.stderr, "\r-> Backing up files: {0}%...".format(progress) print >> sys.stderr, "\r-> Backing up files: {0}%...".format(progress)
def error_callback(self, message): def error_callback(self, message):
self.error_detected.put(message) self.error_detected.put(message)
if self.verbose: if self.verbose:
print >> sys.stderr, "ERROR: {0}".format(message) print >> sys.stderr, "ERROR: {0}".format(message)
def print_callback(self, msg): def print_callback(self, msg):
if self.verbose: if self.verbose:
print msg print msg
def fill_image(self, path, size=None, sparse=False): def fill_image(self, path, size=None, sparse=False):
block_size = 4096 block_size = 4096
@ -432,7 +425,6 @@ class BackupTestsMixin(SystemTestsMixin):
f.close() f.close()
# NOTE: this was create_basic_vms # NOTE: this was create_basic_vms
def create_backup_vms(self): def create_backup_vms(self):
template=self.qc.get_default_template() template=self.qc.get_default_template()
@ -459,7 +451,6 @@ class BackupTestsMixin(SystemTestsMixin):
return vms return vms
def make_backup(self, vms, prepare_kwargs=dict(), do_kwargs=dict(), def make_backup(self, vms, prepare_kwargs=dict(), do_kwargs=dict(),
target=None): target=None):
# XXX: bakup_prepare and backup_do don't support host_collection # XXX: bakup_prepare and backup_do don't support host_collection
@ -472,19 +463,24 @@ class BackupTestsMixin(SystemTestsMixin):
print_callback=self.print_callback, print_callback=self.print_callback,
**prepare_kwargs) **prepare_kwargs)
except qubes.qubes.QubesException as e: except qubes.qubes.QubesException as e:
if not expect_failure:
self.fail("QubesException during backup_prepare: %s" % str(e)) self.fail("QubesException during backup_prepare: %s" % str(e))
else:
raise
try: try:
qubes.backup.backup_do(target, files_to_backup, "qubes", qubes.backup.backup_do(target, files_to_backup, "qubes",
progress_callback=self.print_progress, progress_callback=self.print_progress,
**do_kwargs) **do_kwargs)
except qubes.qubes.QubesException as e: except qubes.qubes.QubesException as e:
if not expect_failure:
self.fail("QubesException during backup_do: %s" % str(e)) self.fail("QubesException during backup_do: %s" % str(e))
else:
raise
self.qc.lock_db_for_writing() self.qc.lock_db_for_writing()
self.qc.load() self.qc.load()
def restore_backup(self, source=None, appvm=None, options=None, def restore_backup(self, source=None, appvm=None, options=None,
expect_errors=None): expect_errors=None):
if source is None: if source is None:
@ -528,7 +524,6 @@ class BackupTestsMixin(SystemTestsMixin):
if not appvm and not os.path.isdir(backupfile): if not appvm and not os.path.isdir(backupfile):
os.unlink(backupfile) os.unlink(backupfile)
def create_sparse(self, path, size): def create_sparse(self, path, size):
f = open(path, "w") f = open(path, "w")
f.truncate(size) f.truncate(size)