1 .. _checkfunctional-ref:
3 Functional testing with Python
4 ==============================
6 The ``tests/functional`` directory hosts functional tests written in
7 Python. They are usually higher level tests, and may interact with
8 external resources and with various guest operating systems.
9 The functional tests have initially evolved from the Avocado tests, so there
10 is a lot of similarity to those tests here (see :ref:`checkavocado-ref` for
11 details about the Avocado tests).
13 The tests should be written in the style of the Python `unittest`_ framework,
14 using stdio for the TAP protocol. The folder ``tests/functional/qemu_test``
15 provides classes (e.g. the ``QemuBaseTest``, ``QemuUserTest`` and the
16 ``QemuSystemTest`` classes) and utility functions that help to get your test
17 into the right shape, e.g. by replacing the 'stdout' python object to redirect
18 the normal output of your test to stderr instead.
20 Note that if you don't use one of the QemuBaseTest based classes for your
21 test, or if you spawn subprocesses from your test, you have to make sure
22 that there is no TAP-incompatible output written to stdio, e.g. either by
23 prefixing every line with a "# " to mark the output as a TAP comment, or
24 e.g. by capturing the stdout output of subprocesses (redirecting it to
27 Tests based on ``qemu_test.QemuSystemTest`` can easily:
29 * Customize the command line arguments given to the convenience
30 ``self.vm`` attribute (a QEMUMachine instance)
32 * Interact with the QEMU monitor, send QMP commands and check
35 * Interact with the guest OS, using the convenience console device
36 (which may be useful to assert the effectiveness and correctness of
37 command line arguments or QMP commands)
39 * Download (and cache) remote data files, such as firmware and kernel
45 You can run the functional tests simply by executing:
51 It is also possible to run tests for a certain target only, for example
52 the following line will only run the tests for the x86_64 target:
56 make check-functional-x86_64
58 To run a single test file without the meson test runner, you can also
59 execute the file directly by specifying two environment variables first,
60 the PYTHONPATH that has to include the python folder and the tests/functional
61 folder of the source tree, and QEMU_TEST_QEMU_BINARY that has to point
62 to the QEMU binary that should be used for the test, for example::
64 $ export PYTHONPATH=../python:../tests/functional
65 $ export QEMU_TEST_QEMU_BINARY=$PWD/qemu-system-x86_64
66 $ python3 ../tests/functional/test_file.py
71 The ``tests/functional/qemu_test`` directory provides the ``qemu_test``
72 Python module, containing the ``qemu_test.QemuSystemTest`` class.
73 Here is a simple usage example:
77 #!/usr/bin/env python3
79 from qemu_test import QemuSystemTest
81 class Version(QemuSystemTest):
83 def test_qmp_human_info_version(self):
85 res = self.vm.cmd('human-monitor-command',
86 command_line='info version')
87 self.assertRegex(res, r'^(\d+\.\d+\.\d)')
89 if __name__ == '__main__':
92 By providing the "hash bang" line at the beginning of the script, marking
93 the file as executable and by calling into QemuSystemTest.main(), the test
94 can also be run stand-alone, without a test runner. OTOH when run via a test
95 runner, the QemuSystemTest.main() function takes care of running the test
96 functions in the right fassion (e.g. with TAP output that is required by the
99 The ``qemu_test.QemuSystemTest`` base test class
100 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102 The ``qemu_test.QemuSystemTest`` class has a number of characteristics
103 that are worth being mentioned.
105 First of all, it attempts to give each test a ready to use QEMUMachine
106 instance, available at ``self.vm``. Because many tests will tweak the
107 QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``)
108 is left to the test writer.
110 The base test class has also support for tests with more than one
111 QEMUMachine. The way to get machines is through the ``self.get_vm()``
112 method which will return a QEMUMachine instance. The ``self.get_vm()``
113 method accepts arguments that will be passed to the QEMUMachine creation
114 and also an optional ``name`` attribute so you can identify a specific
115 machine and get it more than once through the tests methods. A simple
116 and hypothetical example follows:
120 from qemu_test import QemuSystemTest
122 class MultipleMachines(QemuSystemTest):
123 def test_multiple_machines(self):
124 first_machine = self.get_vm()
125 second_machine = self.get_vm()
126 self.get_vm(name='third_machine').launch()
128 first_machine.launch()
129 second_machine.launch()
131 first_res = first_machine.cmd(
132 'human-monitor-command',
133 command_line='info version')
135 second_res = second_machine.cmd(
136 'human-monitor-command',
137 command_line='info version')
139 third_res = self.get_vm(name='third_machine').cmd(
140 'human-monitor-command',
141 command_line='info version')
143 self.assertEqual(first_res, second_res, third_res)
145 At test "tear down", ``qemu_test.QemuSystemTest`` handles all the QEMUMachines
151 The QEMUMachine API is already widely used in the Python iotests,
152 device-crash-test and other Python scripts. It's a wrapper around the
153 execution of a QEMU binary, giving its users:
155 * the ability to set command line arguments to be given to the QEMU
158 * a ready to use QMP connection and interface, which can be used to
159 send commands and inspect its results, as well as asynchronous
162 * convenience methods to set commonly used command line arguments in
163 a more succinct and intuitive way
165 QEMU binary selection
166 ^^^^^^^^^^^^^^^^^^^^^
168 The QEMU binary used for the ``self.vm`` QEMUMachine instance will
169 primarily depend on the value of the ``qemu_bin`` class attribute.
170 If it is not explicitly set by the test code, its default value will
171 be the result the QEMU_TEST_QEMU_BINARY environment variable.
179 The following attributes are available on any ``qemu_test.QemuBaseTest``
185 The target architecture of the QEMU binary.
187 Tests are also free to use this attribute value, for their own needs.
188 A test may, for instance, use this value when selecting the architecture
189 of a kernel or disk image to boot a VM with.
194 The preserved value of the ``QEMU_TEST_QEMU_BINARY`` environment
200 The QemuUserTest class can be used for running an executable via the
201 usermode emulation binaries.
206 The QemuSystemTest class can be used for running tests via one of the
207 qemu-system-* binaries.
212 A QEMUMachine instance, initially configured according to the given
213 ``qemu_bin`` parameter.
218 The cpu model that will be set to all QEMUMachine instances created
224 The machine type that will be set to all QEMUMachine instances created
225 by the test. By using the set_machine() function of the QemuSystemTest
226 class to set this attribute, you can automatically check whether the
227 machine is available to skip the test in case it is not built into the
233 Many functional tests download assets (e.g. Linux kernels, initrds,
234 firmware images, etc.) from the internet to be able to run tests with
235 them. This imposes additional challenges to the test framework.
237 First there is the the problem that some people might not have an
238 unconstrained internet connection, so such tests should not be run by
239 default when running ``make check``. To accomplish this situation,
240 the tests that download files should only be added to the "thorough"
241 speed mode in the meson.build file, while the "quick" speed mode is
242 fine for functional tests that can be run without downloading files.
243 ``make check`` then only runs the quick functional tests along with
244 the other quick tests from the other test suites. If you choose to
245 run only run ``make check-functional``, the "thorough" tests will be
246 executed, too. And to run all functional tests along with the others,
247 you can use something like::
249 make -j$(nproc) check SPEED=thorough
251 The second problem with downloading files from the internet are time
252 constraints. The time for downloading files should not be taken into
253 account when the test is running and the timeout of the test is ticking
254 (since downloading can be very slow, depending on the network bandwidth).
255 This problem is solved by downloading the assets ahead of time, before
256 the tests are run. This pre-caching is done with the qemu_test.Asset
257 class. To use it in your test, declare an asset in your test class with
258 its URL and SHA256 checksum like this::
261 ('https://www.qemu.org/assets/images/qemu_head_200.png'),
262 '34b74cad46ea28a2966c1d04e102510daf1fd73e6582b6b74523940d5da029dd')
264 In your test function, you can then get the file name of the cached
267 def test_function(self):
268 file_path = self.ASSET_somename.fetch()
270 The pre-caching will be done automatically when running
271 ``make check-functional`` (but not when running e.g.
272 ``make check-functional-<target>``). In case you just want to download
273 the assets without running the tests, you can do so by running::
275 make precache-functional
277 The cache is populated in the ``~/.cache/qemu/download`` directory by
278 default, but the location can be changed by setting the
279 ``QEMU_TEST_CACHE_DIR`` environment variable.
284 Since the test framework is based on the common Python unittest framework,
285 you can use the usual Python decorators which allow for easily skipping
286 tests running under certain conditions, for example, on the lack of a binary
287 on the test system or when the running environment is a CI system. For further
288 information about those decorators, please refer to:
290 https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures
292 While the conditions for skipping tests are often specifics of each one, there
293 are recurring scenarios identified by the QEMU developers and the use of
294 environment variables became a kind of standard way to enable/disable tests.
296 Here is a list of the most used variables:
298 QEMU_TEST_ALLOW_LARGE_STORAGE
299 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
300 Tests which are going to fetch or produce assets considered *large* are not
301 going to run unless that ``QEMU_TEST_ALLOW_LARGE_STORAGE=1`` is exported on
304 The definition of *large* is a bit arbitrary here, but it usually means an
305 asset which occupies at least 1GB of size on disk when uncompressed.
307 QEMU_TEST_ALLOW_UNTRUSTED_CODE
308 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
309 There are tests which will boot a kernel image or firmware that can be
310 considered not safe to run on the developer's workstation, thus they are
311 skipped by default. The definition of *not safe* is also arbitrary but
312 usually it means a blob which either its source or build process aren't
315 You should export ``QEMU_TEST_ALLOW_UNTRUSTED_CODE=1`` on the environment in
316 order to allow tests which make use of those kind of assets.
318 QEMU_TEST_FLAKY_TESTS
319 ^^^^^^^^^^^^^^^^^^^^^
320 Some tests are not working reliably and thus are disabled by default.
321 This includes tests that don't run reliably on GitLab's CI which
322 usually expose real issues that are rarely seen on developer machines
323 due to the constraints of the CI environment. If you encounter a
324 similar situation then raise a bug and then mark the test as shown on
325 the code snippet below:
329 # See https://gitlab.com/qemu-project/qemu/-/issues/nnnn
330 @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
334 Tests should not live in this state forever and should either be fixed
335 or eventually removed.
338 .. _unittest: https://docs.python.org/3/library/unittest.html