Browse Source

qubes/tests: skip env-dependent tests using decorator

Gtk segfaults without X11 display. Some tests need dedicated PCI device.
Wojtek Porczyk 7 years ago
parent
commit
8547d06cc9
3 changed files with 17 additions and 5 deletions
  1. 11 1
      qubes/tests/__init__.py
  2. 2 4
      qubes/tests/integ/devices_pci.py
  3. 4 0
      qubespolicy/tests/gtkhelpers.py

+ 11 - 1
qubes/tests/__init__.py

@@ -109,6 +109,7 @@ except OSError:
     # command not found; let's assume we're outside
     pass
 
+
 def skipUnlessDom0(test_item):
     '''Decorator that skips test outside dom0.
 
@@ -118,7 +119,6 @@ def skipUnlessDom0(test_item):
 
     return unittest.skipUnless(in_dom0, 'outside dom0')(test_item)
 
-
 def skipUnlessGit(test_item):
     '''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)
 
+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):
     '''Dummy event emitter which records events fired on it.

+ 2 - 4
qubes/tests/integ/devices_pci.py

@@ -31,14 +31,12 @@ import qubes.ext.pci
 import qubes.tests
 
 
+@qubes.tests.skipUnlessEnv('QUBES_TEST_PCIDEV')
 class TC_00_Devices_PCI(qubes.tests.SystemTestCase):
     def setUp(self):
         super(TC_00_Devices_PCI, self).setUp()
         if self._testMethodName not in ['test_000_list']:
-            pcidev = os.environ.get('QUBES_TEST_PCIDEV', None)
-            if pcidev is None:
-                self.skipTest('Specify PCI device with QUBES_TEST_PCIDEV '
-                              'environment variable')
+            pcidev = os.environ['QUBES_TEST_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)
             if isinstance(self.dev, qubes.devices.UnknownDevice):

+ 4 - 0
qubespolicy/tests/gtkhelpers.py

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