qubes/tests: skip env-dependent tests using decorator

Gtk segfaults without X11 display. Some tests need dedicated PCI device.
This commit is contained in:
Wojtek Porczyk 2017-08-14 23:07:25 +02:00
parent 044e10a6ec
commit 8547d06cc9
3 changed files with 17 additions and 5 deletions

View File

@ -109,6 +109,7 @@ except OSError:
# command not found; let's assume we're outside # command not found; let's assume we're outside
pass pass
def skipUnlessDom0(test_item): def skipUnlessDom0(test_item):
'''Decorator that skips test outside dom0. '''Decorator that skips test outside dom0.
@ -118,7 +119,6 @@ def skipUnlessDom0(test_item):
return unittest.skipUnless(in_dom0, 'outside dom0')(test_item) return unittest.skipUnless(in_dom0, 'outside dom0')(test_item)
def skipUnlessGit(test_item): def skipUnlessGit(test_item):
'''Decorator that skips test outside git repo. '''Decorator that skips test outside git repo.
@ -128,6 +128,16 @@ def skipUnlessGit(test_item):
return unittest.skipUnless(in_git, 'outside git tree')(test_item) return unittest.skipUnless(in_git, 'outside git tree')(test_item)
def skipUnlessEnv(varname):
'''Decorator generator for skipping tests without environment variable set.
Some tests require working X11 display, like those using GTK library, which
segfaults without connection to X.
Other require their own, custom variables.
'''
return unittest.skipUnless(os.getenv(varname), 'no {} set'.format(varname))
class TestEmitter(qubes.events.Emitter): class TestEmitter(qubes.events.Emitter):
'''Dummy event emitter which records events fired on it. '''Dummy event emitter which records events fired on it.

View File

@ -31,14 +31,12 @@ import qubes.ext.pci
import qubes.tests import qubes.tests
@qubes.tests.skipUnlessEnv('QUBES_TEST_PCIDEV')
class TC_00_Devices_PCI(qubes.tests.SystemTestCase): class TC_00_Devices_PCI(qubes.tests.SystemTestCase):
def setUp(self): def setUp(self):
super(TC_00_Devices_PCI, self).setUp() super(TC_00_Devices_PCI, self).setUp()
if self._testMethodName not in ['test_000_list']: if self._testMethodName not in ['test_000_list']:
pcidev = os.environ.get('QUBES_TEST_PCIDEV', None) pcidev = os.environ['QUBES_TEST_PCIDEV']
if pcidev is None:
self.skipTest('Specify PCI device with QUBES_TEST_PCIDEV '
'environment variable')
self.dev = self.app.domains[0].devices['pci'][pcidev] self.dev = self.app.domains[0].devices['pci'][pcidev]
self.assignment = qubes.devices.DeviceAssignment(backend_domain=self.dev.backend_domain, ident=self.dev.ident, persistent=True) self.assignment = qubes.devices.DeviceAssignment(backend_domain=self.dev.backend_domain, ident=self.dev.ident, persistent=True)
if isinstance(self.dev, qubes.devices.UnknownDevice): if isinstance(self.dev, qubes.devices.UnknownDevice):

View File

@ -26,6 +26,8 @@ import gi # isort:skip
gi.require_version('Gtk', '3.0') # isort:skip gi.require_version('Gtk', '3.0') # isort:skip
from gi.repository import Gtk # isort:skip pylint: from gi.repository import Gtk # isort:skip pylint:
from qubes.tests import skipUnlessEnv
from qubespolicy.gtkhelpers import VMListModeler, GtkOneTimerHelper, \ from qubespolicy.gtkhelpers import VMListModeler, GtkOneTimerHelper, \
FocusStealingHelper FocusStealingHelper
@ -81,6 +83,7 @@ class GtkTestCase(unittest.TestCase):
return iterations, time_length return iterations, time_length
@skipUnlessEnv('DISPLAY')
class VMListModelerTest(VMListModeler, unittest.TestCase): class VMListModelerTest(VMListModeler, unittest.TestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs) unittest.TestCase.__init__(self, *args, **kwargs)
@ -305,6 +308,7 @@ class FocusStealingHelperMock(FocusStealingHelper):
self._window_changed_focus(True) self._window_changed_focus(True)
@skipUnlessEnv('DISPLAY')
class FocusStealingHelperTest(FocusStealingHelperMock, GtkTestCase): class FocusStealingHelperTest(FocusStealingHelperMock, GtkTestCase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
GtkTestCase.__init__(self, *args, **kwargs) GtkTestCase.__init__(self, *args, **kwargs)