xref: /openbmc/qemu/docs/devel/testing/avocado.rst (revision 7d87775f)
1.. _checkavocado-ref:
2
3
4Integration testing with Avocado
5================================
6
7The ``tests/avocado`` directory hosts integration tests. They're usually
8higher level tests, and may interact with external resources and with
9various guest operating systems.
10
11These tests are written using the Avocado Testing Framework (which must be
12installed separately) in conjunction with a the ``avocado_qemu.QemuSystemTest``
13class, implemented at ``tests/avocado/avocado_qemu``.
14
15Tests based on ``avocado_qemu.QemuSystemTest`` can easily:
16
17 * Customize the command line arguments given to the convenience
18   ``self.vm`` attribute (a QEMUMachine instance)
19
20 * Interact with the QEMU monitor, send QMP commands and check
21   their results
22
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)
26
27 * Interact with external data files that accompany the test itself
28   (see ``self.get_data()``)
29
30 * Download (and cache) remote data files, such as firmware and kernel
31   images
32
33 * Have access to a library of guest OS images (by means of the
34   ``avocado.utils.vmimage`` library)
35
36 * Make use of various other test related utilities available at the
37   test class itself and at the utility library:
38
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
41
42Running tests
43-------------
44
45You can run the avocado tests simply by executing:
46
47.. code::
48
49  make check-avocado
50
51This involves the automatic installation, from PyPI, of all the
52necessary avocado-framework dependencies into the QEMU venv within the
53build tree (at ``./pyvenv``). Test results are also saved within the
54build tree (at ``tests/results``).
55
56Note: the build environment must be using a Python 3 stack, and have
57the ``venv`` and ``pip`` packages installed.  If necessary, make sure
58``configure`` is called with ``--python=`` and that those modules are
59available.  On Debian and Ubuntu based systems, depending on the
60specific version, they may be on packages named ``python3-venv`` and
61``python3-pip``.
62
63It is also possible to run tests based on tags using the
64``make check-avocado`` command and the ``AVOCADO_TAGS`` environment
65variable:
66
67.. code::
68
69   make check-avocado AVOCADO_TAGS=quick
70
71Note that tags separated with commas have an AND behavior, while tags
72separated by spaces have an OR behavior. For more information on Avocado
73tags, see:
74
75 https://avocado-framework.readthedocs.io/en/latest/guides/user/chapters/tags.html
76
77To run a single test file, a couple of them, or a test within a file
78using the ``make check-avocado`` command, set the ``AVOCADO_TESTS``
79environment variable with the test files or test names. To run all
80tests from a single file, use:
81
82 .. code::
83
84  make check-avocado AVOCADO_TESTS=$FILEPATH
85
86The same is valid to run tests from multiple test files:
87
88 .. code::
89
90  make check-avocado AVOCADO_TESTS='$FILEPATH1 $FILEPATH2'
91
92To run a single test within a file, use:
93
94 .. code::
95
96  make check-avocado AVOCADO_TESTS=$FILEPATH:$TESTCLASS.$TESTNAME
97
98The same is valid to run single tests from multiple test files:
99
100 .. code::
101
102  make check-avocado AVOCADO_TESTS='$FILEPATH1:$TESTCLASS1.$TESTNAME1 $FILEPATH2:$TESTCLASS2.$TESTNAME2'
103
104The scripts installed inside the virtual environment may be used
105without an "activation".  For instance, the Avocado test runner
106may be invoked by running:
107
108 .. code::
109
110  pyvenv/bin/avocado run $OPTION1 $OPTION2 tests/avocado/
111
112Note that if ``make check-avocado`` was not executed before, it is
113possible to create the Python virtual environment with the dependencies
114needed running:
115
116 .. code::
117
118  make check-venv
119
120It is also possible to run tests from a single file or a single test within
121a test file. To run tests from a single file within the build tree, use:
122
123 .. code::
124
125  pyvenv/bin/avocado run tests/avocado/$TESTFILE
126
127To run a single test within a test file, use:
128
129 .. code::
130
131  pyvenv/bin/avocado run tests/avocado/$TESTFILE:$TESTCLASS.$TESTNAME
132
133Valid test names are visible in the output from any previous execution
134of Avocado or ``make check-avocado``, and can also be queried using:
135
136 .. code::
137
138  pyvenv/bin/avocado list tests/avocado
139
140Manual Installation
141-------------------
142
143To manually install Avocado and its dependencies, run:
144
145.. code::
146
147  pip install --user avocado-framework
148
149Alternatively, follow the instructions on this link:
150
151  https://avocado-framework.readthedocs.io/en/latest/guides/user/chapters/installing.html
152
153Overview
154--------
155
156The ``tests/avocado/avocado_qemu`` directory provides the
157``avocado_qemu`` Python module, containing the ``avocado_qemu.QemuSystemTest``
158class.  Here's a simple usage example:
159
160.. code::
161
162  from avocado_qemu import QemuSystemTest
163
164
165  class Version(QemuSystemTest):
166      """
167      :avocado: tags=quick
168      """
169      def test_qmp_human_info_version(self):
170          self.vm.launch()
171          res = self.vm.cmd('human-monitor-command',
172                            command_line='info version')
173          self.assertRegex(res, r'^(\d+\.\d+\.\d)')
174
175To execute your test, run:
176
177.. code::
178
179  avocado run version.py
180
181Tests may be classified according to a convention by using docstring
182directives such as ``:avocado: tags=TAG1,TAG2``.  To run all tests
183in the current directory, tagged as "quick", run:
184
185.. code::
186
187  avocado run -t quick .
188
189The ``avocado_qemu.QemuSystemTest`` base test class
190^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191
192The ``avocado_qemu.QemuSystemTest`` class has a number of characteristics
193that are worth being mentioned right away.
194
195First of all, it attempts to give each test a ready to use QEMUMachine
196instance, available at ``self.vm``.  Because many tests will tweak the
197QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``)
198is left to the test writer.
199
200The base test class has also support for tests with more than one
201QEMUMachine. The way to get machines is through the ``self.get_vm()``
202method which will return a QEMUMachine instance. The ``self.get_vm()``
203method accepts arguments that will be passed to the QEMUMachine creation
204and also an optional ``name`` attribute so you can identify a specific
205machine and get it more than once through the tests methods. A simple
206and hypothetical example follows:
207
208.. code::
209
210  from avocado_qemu import QemuSystemTest
211
212
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()
218
219          first_machine.launch()
220          second_machine.launch()
221
222          first_res = first_machine.cmd(
223              'human-monitor-command',
224              command_line='info version')
225
226          second_res = second_machine.cmd(
227              'human-monitor-command',
228              command_line='info version')
229
230          third_res = self.get_vm(name='third_machine').cmd(
231              'human-monitor-command',
232              command_line='info version')
233
234          self.assertEqual(first_res, second_res, third_res)
235
236At test "tear down", ``avocado_qemu.QemuSystemTest`` handles all the
237QEMUMachines shutdown.
238
239The ``avocado_qemu.LinuxTest`` base test class
240^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
241
242The ``avocado_qemu.LinuxTest`` is further specialization of the
243``avocado_qemu.QemuSystemTest`` class, so it contains all the characteristics
244of the later plus some extra features.
245
246First of all, this base class is intended for tests that need to
247interact with a fully booted and operational Linux guest.  At this
248time, it uses a Fedora 31 guest image.  The most basic example looks
249like this:
250
251.. code::
252
253  from avocado_qemu import LinuxTest
254
255
256  class SomeTest(LinuxTest):
257
258      def test(self):
259          self.launch_and_wait()
260          self.ssh_command('some_command_to_be_run_in_the_guest')
261
262Please refer to tests that use ``avocado_qemu.LinuxTest`` under
263``tests/avocado`` for more examples.
264
265QEMUMachine
266-----------
267
268The QEMUMachine API is already widely used in the Python iotests,
269device-crash-test and other Python scripts.  It's a wrapper around the
270execution of a QEMU binary, giving its users:
271
272 * the ability to set command line arguments to be given to the QEMU
273   binary
274
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
277   events
278
279 * convenience methods to set commonly used command line arguments in
280   a more succinct and intuitive way
281
282QEMU binary selection
283^^^^^^^^^^^^^^^^^^^^^
284
285The QEMU binary used for the ``self.vm`` QEMUMachine instance will
286primarily depend on the value of the ``qemu_bin`` parameter.  If it's
287not explicitly set, its default value will be the result of a dynamic
288probe in the same source tree.  A suitable binary will be one that
289targets the architecture matching host machine.
290
291Based on this description, test writers will usually rely on one of
292the following approaches:
293
2941) Set ``qemu_bin``, and use the given binary
295
2962) 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.
299
300The resulting ``qemu_bin`` value will be preserved in the
301``avocado_qemu.QemuSystemTest`` as an attribute with the same name.
302
303Attribute reference
304-------------------
305
306Test
307^^^^
308
309Besides 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.
312
313vm
314""
315
316A QEMUMachine instance, initially configured according to the given
317``qemu_bin`` parameter.
318
319arch
320""""
321
322The architecture can be used on different levels of the stack, e.g. by
323the framework or by the test itself.  At the framework level, it will
324currently influence the selection of a QEMU binary (when one is not
325explicitly given).
326
327Tests are also free to use this attribute value, for their own needs.
328A test may, for instance, use the same value when selecting the
329architecture of a kernel or disk image to boot a VM with.
330
331The ``arch`` attribute will be set to the test parameter of the same
332name.  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``.
335
336cpu
337"""
338
339The cpu model that will be set to all QEMUMachine instances created
340by the test.
341
342The ``cpu`` attribute will be set to the test parameter of the same
343name. 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``.
346
347machine
348"""""""
349
350The machine type that will be set to all QEMUMachine instances created
351by the test.
352
353The ``machine`` attribute will be set to the test parameter of the same
354name.  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``.
357
358qemu_bin
359""""""""
360
361The preserved value of the ``qemu_bin`` parameter or the result of the
362dynamic probe for a QEMU binary in the current working directory or
363source tree.
364
365LinuxTest
366^^^^^^^^^
367
368Besides the attributes present on the ``avocado_qemu.QemuSystemTest`` base
369class, the ``avocado_qemu.LinuxTest`` adds the following attributes:
370
371distro
372""""""
373
374The name of the Linux distribution used as the guest image for the
375test.  The name should match the **Provider** column on the list
376of images supported by the avocado.utils.vmimage library:
377
378https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
379
380distro_version
381""""""""""""""
382
383The version of the Linux distribution as the guest image for the
384test.  The name should match the **Version** column on the list
385of images supported by the avocado.utils.vmimage library:
386
387https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
388
389distro_checksum
390"""""""""""""""
391
392The sha256 hash of the guest image file used for the test.
393
394If this value is not set in the code or by a test parameter (with the
395same name), no validation on the integrity of the image will be
396performed.
397
398Parameter reference
399-------------------
400
401To understand how Avocado parameters are accessed by tests, and how
402they can be passed to tests, please refer to::
403
404  https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#accessing-test-parameters
405
406Parameter values can be easily seen in the log files, and will look
407like the following:
408
409.. code::
410
411  PARAMS (key=qemu_bin, path=*, default=./qemu-system-x86_64) => './qemu-system-x86_64
412
413Test
414^^^^
415
416arch
417""""
418
419The architecture that will influence the selection of a QEMU binary
420(when one is not explicitly given).
421
422Tests are also free to use this parameter value, for their own needs.
423A test may, for instance, use the same value when selecting the
424architecture of a kernel or disk image to boot a VM with.
425
426This parameter has a direct relation with the ``arch`` attribute.  If
427not given, it will default to None.
428
429cpu
430"""
431
432The cpu model that will be set to all QEMUMachine instances created
433by the test.
434
435machine
436"""""""
437
438The machine type that will be set to all QEMUMachine instances created
439by the test.
440
441qemu_bin
442""""""""
443
444The exact QEMU binary to be used on QEMUMachine.
445
446LinuxTest
447^^^^^^^^^
448
449Besides the parameters present on the ``avocado_qemu.QemuSystemTest`` base
450class, the ``avocado_qemu.LinuxTest`` adds the following parameters:
451
452distro
453""""""
454
455The name of the Linux distribution used as the guest image for the
456test.  The name should match the **Provider** column on the list
457of images supported by the avocado.utils.vmimage library:
458
459https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
460
461distro_version
462""""""""""""""
463
464The version of the Linux distribution as the guest image for the
465test.  The name should match the **Version** column on the list
466of images supported by the avocado.utils.vmimage library:
467
468https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
469
470distro_checksum
471"""""""""""""""
472
473The sha256 hash of the guest image file used for the test.
474
475If this value is not set in the code or by this parameter no
476validation on the integrity of the image will be performed.
477
478Skipping tests
479--------------
480
481The Avocado framework provides Python decorators which allow for easily skip
482tests running under certain conditions. For example, on the lack of a binary
483on the test system or when the running environment is a CI system. For further
484information about those decorators, please refer to::
485
486  https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#skipping-tests
487
488While the conditions for skipping tests are often specifics of each one, there
489are recurring scenarios identified by the QEMU developers and the use of
490environment variables became a kind of standard way to enable/disable tests.
491
492Here is a list of the most used variables:
493
494AVOCADO_ALLOW_LARGE_STORAGE
495^^^^^^^^^^^^^^^^^^^^^^^^^^^
496Tests which are going to fetch or produce assets considered *large* are not
497going to run unless that ``AVOCADO_ALLOW_LARGE_STORAGE=1`` is exported on
498the environment.
499
500The definition of *large* is a bit arbitrary here, but it usually means an
501asset which occupies at least 1GB of size on disk when uncompressed.
502
503SPEED
504^^^^^
505Tests which have a long runtime will not be run unless ``SPEED=slow`` is
506exported on the environment.
507
508The definition of *long* is a bit arbitrary here, and it depends on the
509usefulness of the test too. A unique test is worth spending more time on,
510small variations on existing tests perhaps less so. As a rough guide,
511a test or set of similar tests which take more than 100 seconds to
512complete.
513
514AVOCADO_ALLOW_UNTRUSTED_CODE
515^^^^^^^^^^^^^^^^^^^^^^^^^^^^
516There are tests which will boot a kernel image or firmware that can be
517considered not safe to run on the developer's workstation, thus they are
518skipped by default. The definition of *not safe* is also arbitrary but
519usually it means a blob which either its source or build process aren't
520public available.
521
522You should export ``AVOCADO_ALLOW_UNTRUSTED_CODE=1`` on the environment in
523order to allow tests which make use of those kind of assets.
524
525AVOCADO_TIMEOUT_EXPECTED
526^^^^^^^^^^^^^^^^^^^^^^^^
527The Avocado framework has a timeout mechanism which interrupts tests to avoid the
528test suite of getting stuck. The timeout value can be set via test parameter or
529property defined in the test class, for further details::
530
531  https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#setting-a-test-timeout
532
533Even though the timeout can be set by the test developer, there are some tests
534that may not have a well-defined limit of time to finish under certain
535conditions. For example, tests that take longer to execute when QEMU is
536compiled with debug flags. Therefore, the ``AVOCADO_TIMEOUT_EXPECTED`` variable
537has been used to determine whether those tests should run or not.
538
539QEMU_TEST_FLAKY_TESTS
540^^^^^^^^^^^^^^^^^^^^^
541Some tests are not working reliably and thus are disabled by default.
542This includes tests that don't run reliably on GitLab's CI which
543usually expose real issues that are rarely seen on developer machines
544due to the constraints of the CI environment. If you encounter a
545similar situation then raise a bug and then mark the test as shown on
546the code snippet below:
547
548.. code::
549
550  # See https://gitlab.com/qemu-project/qemu/-/issues/nnnn
551  @skipUnless(os.getenv('QEMU_TEST_FLAKY_TESTS'), 'Test is unstable on GitLab')
552  def test(self):
553      do_something()
554
555You can also add ``:avocado: tags=flaky`` to the test meta-data so
556only the flaky tests can be run as a group:
557
558.. code::
559
560   env QEMU_TEST_FLAKY_TESTS=1 ./pyvenv/bin/avocado \
561      run tests/avocado -filter-by-tags=flaky
562
563Tests should not live in this state forever and should either be fixed
564or eventually removed.
565
566
567Uninstalling Avocado
568--------------------
569
570If you've followed the manual installation instructions above, you can
571easily uninstall Avocado.  Start by listing the packages you have
572installed::
573
574  pip list --user
575
576And remove any package you want with::
577
578  pip uninstall <package_name>
579
580If you've used ``make check-avocado``, the Python virtual environment where
581Avocado is installed will be cleaned up as part of ``make check-clean``.
582