4 Integration testing with Avocado
5 ================================
7 The ``tests/avocado`` directory hosts integration tests. They're usually
8 higher level tests, and may interact with external resources and with
9 various guest operating systems.
11 These tests are written using the Avocado Testing Framework (which must be
12 installed separately) in conjunction with a the ``avocado_qemu.QemuSystemTest``
13 class, implemented at ``tests/avocado/avocado_qemu``.
15 Tests based on ``avocado_qemu.QemuSystemTest`` can easily:
17 * Customize the command line arguments given to the convenience
18 ``self.vm`` attribute (a QEMUMachine instance)
20 * Interact with the QEMU monitor, send QMP commands and check
23 * Interact with the guest OS, using the convenience console device
24 (which may be useful to assert the effectiveness and correctness of
25 command line arguments or QMP commands)
27 * Interact with external data files that accompany the test itself
28 (see ``self.get_data()``)
30 * Download (and cache) remote data files, such as firmware and kernel
33 * Have access to a library of guest OS images (by means of the
34 ``avocado.utils.vmimage`` library)
36 * Make use of various other test related utilities available at the
37 test class itself and at the utility library:
39 - http://avocado-framework.readthedocs.io/en/latest/api/test/avocado.html#avocado.Test
40 - http://avocado-framework.readthedocs.io/en/latest/api/utils/avocado.utils.html
45 You can run the avocado tests simply by executing:
51 This involves the automatic installation, from PyPI, of all the
52 necessary avocado-framework dependencies into the QEMU venv within the
53 build tree (at ``./pyvenv``). Test results are also saved within the
54 build tree (at ``tests/results``).
56 Note: the build environment must be using a Python 3 stack, and have
57 the ``venv`` and ``pip`` packages installed. If necessary, make sure
58 ``configure`` is called with ``--python=`` and that those modules are
59 available. On Debian and Ubuntu based systems, depending on the
60 specific version, they may be on packages named ``python3-venv`` and
63 It is also possible to run tests based on tags using the
64 ``make check-avocado`` command and the ``AVOCADO_TAGS`` environment
69 make check-avocado AVOCADO_TAGS=quick
71 Note that tags separated with commas have an AND behavior, while tags
72 separated by spaces have an OR behavior. For more information on Avocado
75 https://avocado-framework.readthedocs.io/en/latest/guides/user/chapters/tags.html
77 To run a single test file, a couple of them, or a test within a file
78 using the ``make check-avocado`` command, set the ``AVOCADO_TESTS``
79 environment variable with the test files or test names. To run all
80 tests from a single file, use:
84 make check-avocado AVOCADO_TESTS=$FILEPATH
86 The same is valid to run tests from multiple test files:
90 make check-avocado AVOCADO_TESTS='$FILEPATH1 $FILEPATH2'
92 To run a single test within a file, use:
96 make check-avocado AVOCADO_TESTS=$FILEPATH:$TESTCLASS.$TESTNAME
98 The same is valid to run single tests from multiple test files:
102 make check-avocado AVOCADO_TESTS='$FILEPATH1:$TESTCLASS1.$TESTNAME1 $FILEPATH2:$TESTCLASS2.$TESTNAME2'
104 The scripts installed inside the virtual environment may be used
105 without an "activation". For instance, the Avocado test runner
106 may be invoked by running:
110 pyvenv/bin/avocado run $OPTION1 $OPTION2 tests/avocado/
112 Note that if ``make check-avocado`` was not executed before, it is
113 possible to create the Python virtual environment with the dependencies
120 It is also possible to run tests from a single file or a single test within
121 a test file. To run tests from a single file within the build tree, use:
125 pyvenv/bin/avocado run tests/avocado/$TESTFILE
127 To run a single test within a test file, use:
131 pyvenv/bin/avocado run tests/avocado/$TESTFILE:$TESTCLASS.$TESTNAME
133 Valid test names are visible in the output from any previous execution
134 of Avocado or ``make check-avocado``, and can also be queried using:
138 pyvenv/bin/avocado list tests/avocado
143 To manually install Avocado and its dependencies, run:
147 pip install --user avocado-framework
149 Alternatively, follow the instructions on this link:
151 https://avocado-framework.readthedocs.io/en/latest/guides/user/chapters/installing.html
156 The ``tests/avocado/avocado_qemu`` directory provides the
157 ``avocado_qemu`` Python module, containing the ``avocado_qemu.QemuSystemTest``
158 class. Here's a simple usage example:
162 from avocado_qemu import QemuSystemTest
165 class Version(QemuSystemTest):
169 def test_qmp_human_info_version(self):
171 res = self.vm.cmd('human-monitor-command',
172 command_line='info version')
173 self.assertRegex(res, r'^(\d+\.\d+\.\d)')
175 To execute your test, run:
179 avocado run version.py
181 Tests may be classified according to a convention by using docstring
182 directives such as ``:avocado: tags=TAG1,TAG2``. To run all tests
183 in the current directory, tagged as "quick", run:
187 avocado run -t quick .
189 The ``avocado_qemu.QemuSystemTest`` base test class
190 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192 The ``avocado_qemu.QemuSystemTest`` class has a number of characteristics
193 that are worth being mentioned right away.
195 First of all, it attempts to give each test a ready to use QEMUMachine
196 instance, available at ``self.vm``. Because many tests will tweak the
197 QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``)
198 is left to the test writer.
200 The base test class has also support for tests with more than one
201 QEMUMachine. The way to get machines is through the ``self.get_vm()``
202 method which will return a QEMUMachine instance. The ``self.get_vm()``
203 method accepts arguments that will be passed to the QEMUMachine creation
204 and also an optional ``name`` attribute so you can identify a specific
205 machine and get it more than once through the tests methods. A simple
206 and hypothetical example follows:
210 from avocado_qemu import QemuSystemTest
213 class MultipleMachines(QemuSystemTest):
214 def test_multiple_machines(self):
215 first_machine = self.get_vm()
216 second_machine = self.get_vm()
217 self.get_vm(name='third_machine').launch()
219 first_machine.launch()
220 second_machine.launch()
222 first_res = first_machine.cmd(
223 'human-monitor-command',
224 command_line='info version')
226 second_res = second_machine.cmd(
227 'human-monitor-command',
228 command_line='info version')
230 third_res = self.get_vm(name='third_machine').cmd(
231 'human-monitor-command',
232 command_line='info version')
234 self.assertEqual(first_res, second_res, third_res)
236 At test "tear down", ``avocado_qemu.QemuSystemTest`` handles all the
237 QEMUMachines shutdown.
239 The ``avocado_qemu.LinuxTest`` base test class
240 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
242 The ``avocado_qemu.LinuxTest`` is further specialization of the
243 ``avocado_qemu.QemuSystemTest`` class, so it contains all the characteristics
244 of the later plus some extra features.
246 First of all, this base class is intended for tests that need to
247 interact with a fully booted and operational Linux guest. At this
248 time, it uses a Fedora 31 guest image. The most basic example looks
253 from avocado_qemu import LinuxTest
256 class SomeTest(LinuxTest):
259 self.launch_and_wait()
260 self.ssh_command('some_command_to_be_run_in_the_guest')
262 Please refer to tests that use ``avocado_qemu.LinuxTest`` under
263 ``tests/avocado`` for more examples.
268 The QEMUMachine API is already widely used in the Python iotests,
269 device-crash-test and other Python scripts. It's a wrapper around the
270 execution of a QEMU binary, giving its users:
272 * the ability to set command line arguments to be given to the QEMU
275 * a ready to use QMP connection and interface, which can be used to
276 send commands and inspect its results, as well as asynchronous
279 * convenience methods to set commonly used command line arguments in
280 a more succinct and intuitive way
282 QEMU binary selection
283 ^^^^^^^^^^^^^^^^^^^^^
285 The QEMU binary used for the ``self.vm`` QEMUMachine instance will
286 primarily depend on the value of the ``qemu_bin`` parameter. If it's
287 not explicitly set, its default value will be the result of a dynamic
288 probe in the same source tree. A suitable binary will be one that
289 targets the architecture matching host machine.
291 Based on this description, test writers will usually rely on one of
292 the following approaches:
294 1) Set ``qemu_bin``, and use the given binary
296 2) Do not set ``qemu_bin``, and use a QEMU binary named like
297 "qemu-system-${arch}", either in the current
298 working directory, or in the current source tree.
300 The resulting ``qemu_bin`` value will be preserved in the
301 ``avocado_qemu.QemuSystemTest`` as an attribute with the same name.
309 Besides the attributes and methods that are part of the base
310 ``avocado.Test`` class, the following attributes are available on any
311 ``avocado_qemu.QemuSystemTest`` instance.
316 A QEMUMachine instance, initially configured according to the given
317 ``qemu_bin`` parameter.
322 The architecture can be used on different levels of the stack, e.g. by
323 the framework or by the test itself. At the framework level, it will
324 currently influence the selection of a QEMU binary (when one is not
327 Tests are also free to use this attribute value, for their own needs.
328 A test may, for instance, use the same value when selecting the
329 architecture of a kernel or disk image to boot a VM with.
331 The ``arch`` attribute will be set to the test parameter of the same
332 name. If one is not given explicitly, it will either be set to
333 ``None``, or, if the test is tagged with one (and only one)
334 ``:avocado: tags=arch:VALUE`` tag, it will be set to ``VALUE``.
339 The cpu model that will be set to all QEMUMachine instances created
342 The ``cpu`` attribute will be set to the test parameter of the same
343 name. If one is not given explicitly, it will either be set to
344 ``None ``, or, if the test is tagged with one (and only one)
345 ``:avocado: tags=cpu:VALUE`` tag, it will be set to ``VALUE``.
350 The machine type that will be set to all QEMUMachine instances created
353 The ``machine`` attribute will be set to the test parameter of the same
354 name. If one is not given explicitly, it will either be set to
355 ``None``, or, if the test is tagged with one (and only one)
356 ``:avocado: tags=machine:VALUE`` tag, it will be set to ``VALUE``.
361 The preserved value of the ``qemu_bin`` parameter or the result of the
362 dynamic probe for a QEMU binary in the current working directory or
368 Besides the attributes present on the ``avocado_qemu.QemuSystemTest`` base
369 class, the ``avocado_qemu.LinuxTest`` adds the following attributes:
374 The name of the Linux distribution used as the guest image for the
375 test. The name should match the **Provider** column on the list
376 of images supported by the avocado.utils.vmimage library:
378 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
383 The version of the Linux distribution as the guest image for the
384 test. The name should match the **Version** column on the list
385 of images supported by the avocado.utils.vmimage library:
387 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
392 The sha256 hash of the guest image file used for the test.
394 If this value is not set in the code or by a test parameter (with the
395 same name), no validation on the integrity of the image will be
401 To understand how Avocado parameters are accessed by tests, and how
402 they can be passed to tests, please refer to::
404 https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#accessing-test-parameters
406 Parameter values can be easily seen in the log files, and will look
411 PARAMS (key=qemu_bin, path=*, default=./qemu-system-x86_64) => './qemu-system-x86_64
419 The architecture that will influence the selection of a QEMU binary
420 (when one is not explicitly given).
422 Tests are also free to use this parameter value, for their own needs.
423 A test may, for instance, use the same value when selecting the
424 architecture of a kernel or disk image to boot a VM with.
426 This parameter has a direct relation with the ``arch`` attribute. If
427 not given, it will default to None.
432 The cpu model that will be set to all QEMUMachine instances created
438 The machine type that will be set to all QEMUMachine instances created
444 The exact QEMU binary to be used on QEMUMachine.
449 Besides the parameters present on the ``avocado_qemu.QemuSystemTest`` base
450 class, the ``avocado_qemu.LinuxTest`` adds the following parameters:
455 The name of the Linux distribution used as the guest image for the
456 test. The name should match the **Provider** column on the list
457 of images supported by the avocado.utils.vmimage library:
459 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
464 The version of the Linux distribution as the guest image for the
465 test. The name should match the **Version** column on the list
466 of images supported by the avocado.utils.vmimage library:
468 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
473 The sha256 hash of the guest image file used for the test.
475 If this value is not set in the code or by this parameter no
476 validation on the integrity of the image will be performed.
481 The Avocado framework provides Python decorators which allow for easily skip
482 tests running under certain conditions. For example, on the lack of a binary
483 on the test system or when the running environment is a CI system. For further
484 information about those decorators, please refer to::
486 https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#skipping-tests
488 While the conditions for skipping tests are often specifics of each one, there
489 are recurring scenarios identified by the QEMU developers and the use of
490 environment variables became a kind of standard way to enable/disable tests.
492 Here is a list of the most used variables:
494 AVOCADO_ALLOW_LARGE_STORAGE
495 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
496 Tests which are going to fetch or produce assets considered *large* are not
497 going to run unless that ``AVOCADO_ALLOW_LARGE_STORAGE=1`` is exported on
500 The definition of *large* is a bit arbitrary here, but it usually means an
501 asset which occupies at least 1GB of size on disk when uncompressed.
505 Tests which have a long runtime will not be run unless ``SPEED=slow`` is
506 exported on the environment.
508 The definition of *long* is a bit arbitrary here, and it depends on the
509 usefulness of the test too. A unique test is worth spending more time on,
510 small variations on existing tests perhaps less so. As a rough guide,
511 a test or set of similar tests which take more than 100 seconds to
514 AVOCADO_ALLOW_UNTRUSTED_CODE
515 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
516 There are tests which will boot a kernel image or firmware that can be
517 considered not safe to run on the developer's workstation, thus they are
518 skipped by default. The definition of *not safe* is also arbitrary but
519 usually it means a blob which either its source or build process aren't
522 You should export ``AVOCADO_ALLOW_UNTRUSTED_CODE=1`` on the environment in
523 order to allow tests which make use of those kind of assets.
525 AVOCADO_TIMEOUT_EXPECTED
526 ^^^^^^^^^^^^^^^^^^^^^^^^
527 The Avocado framework has a timeout mechanism which interrupts tests to avoid the
528 test suite of getting stuck. The timeout value can be set via test parameter or
529 property defined in the test class, for further details::
531 https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#setting-a-test-timeout
533 Even though the timeout can be set by the test developer, there are some tests
534 that may not have a well-defined limit of time to finish under certain
535 conditions. For example, tests that take longer to execute when QEMU is
536 compiled with debug flags. Therefore, the ``AVOCADO_TIMEOUT_EXPECTED`` variable
537 has been used to determine whether those tests should run or not.
539 QEMU_TEST_FLAKY_TESTS
540 ^^^^^^^^^^^^^^^^^^^^^
541 Some tests are not working reliably and thus are disabled by default.
542 This includes tests that don't run reliably on GitLab's CI which
543 usually expose real issues that are rarely seen on developer machines
544 due to the constraints of the CI environment. If you encounter a
545 similar situation then raise a bug and then mark the test as shown on
546 the code snippet below:
550 # See https://gitlab.com/qemu-project/qemu/-/issues/nnnn
551 @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
555 You can also add ``:avocado: tags=flaky`` to the test meta-data so
556 only the flaky tests can be run as a group:
560 env QEMU_TEST_FLAKY_TESTS=1 ./pyvenv/bin/avocado \
561 run tests/avocado -filter-by-tags=flaky
563 Tests should not live in this state forever and should either be fixed
564 or eventually removed.
570 If you've followed the manual installation instructions above, you can
571 easily uninstall Avocado. Start by listing the packages you have
576 And remove any package you want with::
578 pip uninstall <package_name>
580 If you've used ``make check-avocado``, the Python virtual environment where
581 Avocado is installed will be cleaned up as part of ``make check-clean``.