Automated Tests

Unit and Integration Tests

Starting with Qubes R3 we use python unittest to perform automatic tests of Qubes OS. Despite the name, we use it for both unit tests and integration tests. The main purpose is, of course, to deliver much more stable releases.

Integration tests are written with the assumption that they will be called on dedicated hardware. Do not run these tests on installations with important data, because you might lose it. Since these tests were written with this expectation, all the VMs with a name starting with test- on the installation are removed during the process, and all the tests are recklessly started from dom0, even when testing VM components.

Most of the tests are stored in the core-admin repository in the qubes/tests directory. To start them you can use standard python unittest runner: python3 -m unittest -v qubes.tests Or our custom one: python3 -m -v

Our test runner runs mostly the same as the standard one, but it has some nice additional features like color output and not needing the “qubes.test” prefix. It also has the ability to run lone selected template tests.

You can use python3 -m -h to get usage information:

[user@dom0 ~]$ python3 -m -h
usage: [-h] [--verbose] [--quiet] [--list] [--failfast] [--no-failfast]
              [--do-not-clean] [--do-clean] [--loglevel LEVEL]
              [--logfile FILE] [--syslog] [--no-syslog] [--kmsg] [--no-kmsg]
              [TESTNAME [TESTNAME ...]]

positional arguments:
  TESTNAME              list of tests to run named like in description
                        (default: run all tests)

optional arguments:
  -h, --help            show this help message and exit
  --verbose, -v         increase console verbosity level
  --quiet, -q           decrease console verbosity level
  --list, -l            list all available tests and exit
  --failfast, -f        stop on the first fail, error or unexpected success
  --no-failfast         disable --failfast
  --loglevel LEVEL, -L LEVEL
                        logging level for file and syslog forwarding (one of:
                        default: DEBUG)
  --logfile FILE, -o FILE
                        if set, test run will be also logged to file
  --syslog              reenable logging to syslog
  --no-syslog           disable logging to syslog
  --kmsg, --very-brave-or-very-stupid
                        log most important things to kernel ring-buffer
  --no-kmsg, --i-am-smarter-than-kay-sievers
                        do not abuse kernel ring-buffer
                        allow running in parallel with qubesd; this is
  --break-to-repl       break to REPL after tests

When running only specific tests, write their names like in log, in format:
MODULE+"/"+CLASS+"/"+FUNCTION. MODULE should omit initial "qubes.tests.".
Example: basic/TC_00_Basic/test_000_create

For instance, to run only the tests for the fedora-21 template, you can use the -l option, then filter the list:

[user@dom0 ~]$ python3 -m -l | grep fedora-21
[user@dom0 ~]$ python3 -m -v `python3 -m -l | grep fedora-21`

Example test run:


Qubes 4.0

Tests on Qubes 4.0 require stopping qubesd service first, because special instance of it is started as part of the test run. Additionally, tests needs to be started as root. The full command to run the tests is:

sudo systemctl stop qubesd; sudo -E python3 -m -v ; sudo systemctl start qubesd

On Qubes 4.0 tests are also compatible with nose2 test runner, so you can use this instead:

sudo systemctl stop qubesd; sudo -E nose2 -v --plugin nose2.plugins.loader.loadtests qubes.tests; sudo systemctl start qubesd

This may be especially useful together with various nose2 plugins to store tests results (for example nose2.plugins.junitxml), to ease presenting results. This is what we use on OpenQA.

Tests configuration

Test run can be altered using environment variables:

  • DEFAULT_LVM_POOL - LVM thin pool to use for tests, in VolumeGroup/ThinPool format
  • QUBES_TEST_PCIDEV - PCI device to be used in PCI passthrough tests (for example sound card)
  • QUBES_TEST_TEMPLATES - space separated list of templates to run tests on; if not set, all installed templates are tested
  • QUBES_TEST_LOAD_ALL - load all tests (including tests for all templates) when relevant test modules are imported; this needs to be set for test runners not supporting load_tests protocol

Adding a new test to core-admin

After adding a new unit test to core-admin/qubes/tests you’ll have to include it in core-admin/qubes/tests/


You’ll also need to add your test at the bottom of the file, in the method def load_tests, in the for loop with modname. Again, given the hypothetical test:

    for modname in (
            'qubes.tests.example', # This is our newly added test

Testing PyQt applications

When testing (Py)QT application, it’s useful to create separate QApplication object for each test. But QT framework does not allow to have multiple QApplication objects in the same process at the same time. This means it’s critical to reliably cleanup previous instance before creating the new one. This turns out to be non-trivial task, especially if any test uses event loop. Failure to perform proper cleanup in many cases results in SEGV. Below you can find steps for the proper cleanup:

import asyncio
import quamash
import unittest
import gc

class SomeTestCase(unittest.TestCase):
    def setUp(self):

        # force "cleanlooks" style, the default one on Xfce (GtkStyle) use
        # static variable internally and caches pointers to later destroyed
        # objects (result: SEGV)
        self.qtapp = QtGui.QApplication(["test", "-style", "cleanlooks"])

        # construct event loop even if this particular test doesn't use it,
        # otherwise events with qtapp references will be queued there anyway and the
        # first test that actually use event loop will try to dereference (already
        # destroyed) objects, resulting in SEGV
        self.loop = quamash.QEventLoop(self.qtapp)
    def tearDown(self):
        # process any pending events before destroying the object

        # queue destroying the QApplication object, do that for any other QT
        # related objects here too

        # process any pending events (other than just queued destroy), just in case

        # execute main loop, which will process all events, _including just queued destroy_

        # at this point it QT objects are destroyed, cleanup all remaining references;
        # del other QT object here too
        del self.qtapp
        del self.loop

Installation Tests with openQA


Manually testing the installation of Qubes OS is a time-consuming process. We use openQA to automate this process. It works by installing Qubes in KVM and interacting with it as a user would, including simulating mouse clicks and keyboard presses. Then, it checks the output to see whether various tests were passed, e.g., by comparing the virtual screen output to screenshots of a successful installation.

Using openQA to automatically test the Qubes installation process works as of Qubes 4.0-rc4 on 2018-01-26, provided that the versions of KVM and QEMU are new enough and the hardware has VT-x and EPT. KVM also supports nested virtualization, so HVM should theoretically work. In practice, however, either Xen or QEMU crashes when this is attempted. Nonetheless, PV works well, which is sufficient for automated installation testing.

Thanks to an anonymous donor, our openQA system is hosted in a datacenter on hardware that meets these requirements.