]> git.proxmox.com Git - mirror_qemu.git/blob - docs/devel/testing.rst
docs/devel: testing: Document writing portable test cases
[mirror_qemu.git] / docs / devel / testing.rst
1 .. _testing:
2
3 Testing in QEMU
4 ===============
5
6 This document describes the testing infrastructure in QEMU.
7
8 Testing with "make check"
9 -------------------------
10
11 The "make check" testing family includes most of the C based tests in QEMU. For
12 a quick help, run ``make check-help`` from the source tree.
13
14 The usual way to run these tests is:
15
16 .. code::
17
18 make check
19
20 which includes QAPI schema tests, unit tests, QTests and some iotests.
21 Different sub-types of "make check" tests will be explained below.
22
23 Before running tests, it is best to build QEMU programs first. Some tests
24 expect the executables to exist and will fail with obscure messages if they
25 cannot find them.
26
27 Unit tests
28 ~~~~~~~~~~
29
30 Unit tests, which can be invoked with ``make check-unit``, are simple C tests
31 that typically link to individual QEMU object files and exercise them by
32 calling exported functions.
33
34 If you are writing new code in QEMU, consider adding a unit test, especially
35 for utility modules that are relatively stateless or have few dependencies. To
36 add a new unit test:
37
38 1. Create a new source file. For example, ``tests/unit/foo-test.c``.
39
40 2. Write the test. Normally you would include the header file which exports
41 the module API, then verify the interface behaves as expected from your
42 test. The test code should be organized with the glib testing framework.
43 Copying and modifying an existing test is usually a good idea.
44
45 3. Add the test to ``tests/unit/meson.build``. The unit tests are listed in a
46 dictionary called ``tests``. The values are any additional sources and
47 dependencies to be linked with the test. For a simple test whose source
48 is in ``tests/unit/foo-test.c``, it is enough to add an entry like::
49
50 {
51 ...
52 'foo-test': [],
53 ...
54 }
55
56 Since unit tests don't require environment variables, the simplest way to debug
57 a unit test failure is often directly invoking it or even running it under
58 ``gdb``. However there can still be differences in behavior between ``make``
59 invocations and your manual run, due to ``$MALLOC_PERTURB_`` environment
60 variable (which affects memory reclamation and catches invalid pointers better)
61 and gtester options. If necessary, you can run
62
63 .. code::
64
65 make check-unit V=1
66
67 and copy the actual command line which executes the unit test, then run
68 it from the command line.
69
70 QTest
71 ~~~~~
72
73 QTest is a device emulation testing framework. It can be very useful to test
74 device models; it could also control certain aspects of QEMU (such as virtual
75 clock stepping), with a special purpose "qtest" protocol. Refer to
76 :doc:`qtest` for more details.
77
78 QTest cases can be executed with
79
80 .. code::
81
82 make check-qtest
83
84 Writing portable test cases
85 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
86 Both unit tests and qtests can run on POSIX hosts as well as Windows hosts.
87 Care must be taken when writing portable test cases that can be built and run
88 successfully on various hosts. The following list shows some best practices:
89
90 * Use portable APIs from glib whenever necessary, e.g.: g_setenv(),
91 g_mkdtemp(), g_mkdir().
92 * Avoid using hardcoded /tmp for temporary file directory.
93 Use g_get_tmp_dir() instead.
94 * Bear in mind that Windows has different special string representation for
95 stdin/stdout/stderr and null devices. For example if your test case uses
96 "/dev/fd/2" and "/dev/null" on Linux, remember to use "2" and "nul" on
97 Windows instead. Also IO redirection does not work on Windows, so avoid
98 using "2>nul" whenever necessary.
99 * If your test cases uses the blkdebug feature, use relative path to pass
100 the config and image file paths in the command line as Windows absolute
101 path contains the delimiter ":" which will confuse the blkdebug parser.
102 * Use double quotes in your extra QEMU commmand line in your test cases
103 instead of single quotes, as Windows does not drop single quotes when
104 passing the command line to QEMU.
105 * Windows opens a file in text mode by default, while a POSIX compliant
106 implementation treats text files and binary files the same. So if your
107 test cases opens a file to write some data and later wants to compare the
108 written data with the original one, be sure to pass the letter 'b' as
109 part of the mode string to fopen(), or O_BINARY flag for the open() call.
110 * If a certain test case can only run on POSIX or Linux hosts, use a proper
111 #ifdef in the codes. If the whole test suite cannot run on Windows, disable
112 the build in the meson.build file.
113
114 QAPI schema tests
115 ~~~~~~~~~~~~~~~~~
116
117 The QAPI schema tests validate the QAPI parser used by QMP, by feeding
118 predefined input to the parser and comparing the result with the reference
119 output.
120
121 The input/output data is managed under the ``tests/qapi-schema`` directory.
122 Each test case includes four files that have a common base name:
123
124 * ``${casename}.json`` - the file contains the JSON input for feeding the
125 parser
126 * ``${casename}.out`` - the file contains the expected stdout from the parser
127 * ``${casename}.err`` - the file contains the expected stderr from the parser
128 * ``${casename}.exit`` - the expected error code
129
130 Consider adding a new QAPI schema test when you are making a change on the QAPI
131 parser (either fixing a bug or extending/modifying the syntax). To do this:
132
133 1. Add four files for the new case as explained above. For example:
134
135 ``$EDITOR tests/qapi-schema/foo.{json,out,err,exit}``.
136
137 2. Add the new test in ``tests/Makefile.include``. For example:
138
139 ``qapi-schema += foo.json``
140
141 check-block
142 ~~~~~~~~~~~
143
144 ``make check-block`` runs a subset of the block layer iotests (the tests that
145 are in the "auto" group).
146 See the "QEMU iotests" section below for more information.
147
148 QEMU iotests
149 ------------
150
151 QEMU iotests, under the directory ``tests/qemu-iotests``, is the testing
152 framework widely used to test block layer related features. It is higher level
153 than "make check" tests and 99% of the code is written in bash or Python
154 scripts. The testing success criteria is golden output comparison, and the
155 test files are named with numbers.
156
157 To run iotests, make sure QEMU is built successfully, then switch to the
158 ``tests/qemu-iotests`` directory under the build directory, and run ``./check``
159 with desired arguments from there.
160
161 By default, "raw" format and "file" protocol is used; all tests will be
162 executed, except the unsupported ones. You can override the format and protocol
163 with arguments:
164
165 .. code::
166
167 # test with qcow2 format
168 ./check -qcow2
169 # or test a different protocol
170 ./check -nbd
171
172 It's also possible to list test numbers explicitly:
173
174 .. code::
175
176 # run selected cases with qcow2 format
177 ./check -qcow2 001 030 153
178
179 Cache mode can be selected with the "-c" option, which may help reveal bugs
180 that are specific to certain cache mode.
181
182 More options are supported by the ``./check`` script, run ``./check -h`` for
183 help.
184
185 Writing a new test case
186 ~~~~~~~~~~~~~~~~~~~~~~~
187
188 Consider writing a tests case when you are making any changes to the block
189 layer. An iotest case is usually the choice for that. There are already many
190 test cases, so it is possible that extending one of them may achieve the goal
191 and save the boilerplate to create one. (Unfortunately, there isn't a 100%
192 reliable way to find a related one out of hundreds of tests. One approach is
193 using ``git grep``.)
194
195 Usually an iotest case consists of two files. One is an executable that
196 produces output to stdout and stderr, the other is the expected reference
197 output. They are given the same number in file names. E.g. Test script ``055``
198 and reference output ``055.out``.
199
200 In rare cases, when outputs differ between cache mode ``none`` and others, a
201 ``.out.nocache`` file is added. In other cases, when outputs differ between
202 image formats, more than one ``.out`` files are created ending with the
203 respective format names, e.g. ``178.out.qcow2`` and ``178.out.raw``.
204
205 There isn't a hard rule about how to write a test script, but a new test is
206 usually a (copy and) modification of an existing case. There are a few
207 commonly used ways to create a test:
208
209 * A Bash script. It will make use of several environmental variables related
210 to the testing procedure, and could source a group of ``common.*`` libraries
211 for some common helper routines.
212
213 * A Python unittest script. Import ``iotests`` and create a subclass of
214 ``iotests.QMPTestCase``, then call ``iotests.main`` method. The downside of
215 this approach is that the output is too scarce, and the script is considered
216 harder to debug.
217
218 * A simple Python script without using unittest module. This could also import
219 ``iotests`` for launching QEMU and utilities etc, but it doesn't inherit
220 from ``iotests.QMPTestCase`` therefore doesn't use the Python unittest
221 execution. This is a combination of 1 and 2.
222
223 Pick the language per your preference since both Bash and Python have
224 comparable library support for invoking and interacting with QEMU programs. If
225 you opt for Python, it is strongly recommended to write Python 3 compatible
226 code.
227
228 Both Python and Bash frameworks in iotests provide helpers to manage test
229 images. They can be used to create and clean up images under the test
230 directory. If no I/O or any protocol specific feature is needed, it is often
231 more convenient to use the pseudo block driver, ``null-co://``, as the test
232 image, which doesn't require image creation or cleaning up. Avoid system-wide
233 devices or files whenever possible, such as ``/dev/null`` or ``/dev/zero``.
234 Otherwise, image locking implications have to be considered. For example,
235 another application on the host may have locked the file, possibly leading to a
236 test failure. If using such devices are explicitly desired, consider adding
237 ``locking=off`` option to disable image locking.
238
239 Debugging a test case
240 ~~~~~~~~~~~~~~~~~~~~~
241
242 The following options to the ``check`` script can be useful when debugging
243 a failing test:
244
245 * ``-gdb`` wraps every QEMU invocation in a ``gdbserver``, which waits for a
246 connection from a gdb client. The options given to ``gdbserver`` (e.g. the
247 address on which to listen for connections) are taken from the ``$GDB_OPTIONS``
248 environment variable. By default (if ``$GDB_OPTIONS`` is empty), it listens on
249 ``localhost:12345``.
250 It is possible to connect to it for example with
251 ``gdb -iex "target remote $addr"``, where ``$addr`` is the address
252 ``gdbserver`` listens on.
253 If the ``-gdb`` option is not used, ``$GDB_OPTIONS`` is ignored,
254 regardless of whether it is set or not.
255
256 * ``-valgrind`` attaches a valgrind instance to QEMU. If it detects
257 warnings, it will print and save the log in
258 ``$TEST_DIR/<valgrind_pid>.valgrind``.
259 The final command line will be ``valgrind --log-file=$TEST_DIR/
260 <valgrind_pid>.valgrind --error-exitcode=99 $QEMU ...``
261
262 * ``-d`` (debug) just increases the logging verbosity, showing
263 for example the QMP commands and answers.
264
265 * ``-p`` (print) redirects QEMU’s stdout and stderr to the test output,
266 instead of saving it into a log file in
267 ``$TEST_DIR/qemu-machine-<random_string>``.
268
269 Test case groups
270 ~~~~~~~~~~~~~~~~
271
272 "Tests may belong to one or more test groups, which are defined in the form
273 of a comment in the test source file. By convention, test groups are listed
274 in the second line of the test file, after the "#!/..." line, like this:
275
276 .. code::
277
278 #!/usr/bin/env python3
279 # group: auto quick
280 #
281 ...
282
283 Another way of defining groups is creating the tests/qemu-iotests/group.local
284 file. This should be used only for downstream (this file should never appear
285 in upstream). This file may be used for defining some downstream test groups
286 or for temporarily disabling tests, like this:
287
288 .. code::
289
290 # groups for some company downstream process
291 #
292 # ci - tests to run on build
293 # down - our downstream tests, not for upstream
294 #
295 # Format of each line is:
296 # TEST_NAME TEST_GROUP [TEST_GROUP ]...
297
298 013 ci
299 210 disabled
300 215 disabled
301 our-ugly-workaround-test down ci
302
303 Note that the following group names have a special meaning:
304
305 - quick: Tests in this group should finish within a few seconds.
306
307 - auto: Tests in this group are used during "make check" and should be
308 runnable in any case. That means they should run with every QEMU binary
309 (also non-x86), with every QEMU configuration (i.e. must not fail if
310 an optional feature is not compiled in - but reporting a "skip" is ok),
311 work at least with the qcow2 file format, work with all kind of host
312 filesystems and users (e.g. "nobody" or "root") and must not take too
313 much memory and disk space (since CI pipelines tend to fail otherwise).
314
315 - disabled: Tests in this group are disabled and ignored by check.
316
317 .. _container-ref:
318
319 Container based tests
320 ---------------------
321
322 Introduction
323 ~~~~~~~~~~~~
324
325 The container testing framework in QEMU utilizes public images to
326 build and test QEMU in predefined and widely accessible Linux
327 environments. This makes it possible to expand the test coverage
328 across distros, toolchain flavors and library versions. The support
329 was originally written for Docker although we also support Podman as
330 an alternative container runtime. Although many of the target
331 names and scripts are prefixed with "docker" the system will
332 automatically run on whichever is configured.
333
334 The container images are also used to augment the generation of tests
335 for testing TCG. See :ref:`checktcg-ref` for more details.
336
337 Docker Prerequisites
338 ~~~~~~~~~~~~~~~~~~~~
339
340 Install "docker" with the system package manager and start the Docker service
341 on your development machine, then make sure you have the privilege to run
342 Docker commands. Typically it means setting up passwordless ``sudo docker``
343 command or login as root. For example:
344
345 .. code::
346
347 $ sudo yum install docker
348 $ # or `apt-get install docker` for Ubuntu, etc.
349 $ sudo systemctl start docker
350 $ sudo docker ps
351
352 The last command should print an empty table, to verify the system is ready.
353
354 An alternative method to set up permissions is by adding the current user to
355 "docker" group and making the docker daemon socket file (by default
356 ``/var/run/docker.sock``) accessible to the group:
357
358 .. code::
359
360 $ sudo groupadd docker
361 $ sudo usermod $USER -a -G docker
362 $ sudo chown :docker /var/run/docker.sock
363
364 Note that any one of above configurations makes it possible for the user to
365 exploit the whole host with Docker bind mounting or other privileged
366 operations. So only do it on development machines.
367
368 Podman Prerequisites
369 ~~~~~~~~~~~~~~~~~~~~
370
371 Install "podman" with the system package manager.
372
373 .. code::
374
375 $ sudo dnf install podman
376 $ podman ps
377
378 The last command should print an empty table, to verify the system is ready.
379
380 Quickstart
381 ~~~~~~~~~~
382
383 From source tree, type ``make docker-help`` to see the help. Testing
384 can be started without configuring or building QEMU (``configure`` and
385 ``make`` are done in the container, with parameters defined by the
386 make target):
387
388 .. code::
389
390 make docker-test-build@centos8
391
392 This will create a container instance using the ``centos8`` image (the image
393 is downloaded and initialized automatically), in which the ``test-build`` job
394 is executed.
395
396 Registry
397 ~~~~~~~~
398
399 The QEMU project has a container registry hosted by GitLab at
400 ``registry.gitlab.com/qemu-project/qemu`` which will automatically be
401 used to pull in pre-built layers. This avoids unnecessary strain on
402 the distro archives created by multiple developers running the same
403 container build steps over and over again. This can be overridden
404 locally by using the ``NOCACHE`` build option:
405
406 .. code::
407
408 make docker-image-debian-arm64-cross NOCACHE=1
409
410 Images
411 ~~~~~~
412
413 Along with many other images, the ``centos8`` image is defined in a Dockerfile
414 in ``tests/docker/dockerfiles/``, called ``centos8.docker``. ``make docker-help``
415 command will list all the available images.
416
417 A ``.pre`` script can be added beside the ``.docker`` file, which will be
418 executed before building the image under the build context directory. This is
419 mainly used to do necessary host side setup. One such setup is ``binfmt_misc``,
420 for example, to make qemu-user powered cross build containers work.
421
422 Most of the existing Dockerfiles were written by hand, simply by creating a
423 a new ``.docker`` file under the ``tests/docker/dockerfiles/`` directory.
424 This has led to an inconsistent set of packages being present across the
425 different containers.
426
427 Thus going forward, QEMU is aiming to automatically generate the Dockerfiles
428 using the ``lcitool`` program provided by the ``libvirt-ci`` project:
429
430 https://gitlab.com/libvirt/libvirt-ci
431
432 In that project, there is a ``mappings.yml`` file defining the distro native
433 package names for a wide variety of third party projects. This is processed
434 in combination with a project defined list of build pre-requisites to determine
435 the list of native packages to install on each distribution. This can be used
436 to generate dockerfiles, VM package lists and Cirrus CI variables needed to
437 setup build environments across OS distributions with a consistent set of
438 packages present.
439
440 When preparing a patch series that adds a new build pre-requisite to QEMU,
441 updates to various lcitool data files may be required.
442
443
444 Adding new build pre-requisites
445 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
446
447 In the simple case where the pre-requisite is already known to ``libvirt-ci``
448 the following steps are needed
449
450 * Edit ``tests/lcitool/projects/qemu.yml`` and add the pre-requisite
451
452 * Run ``make lcitool-refresh`` to re-generate all relevant build environment
453 manifests
454
455 In some cases ``libvirt-ci`` will not know about the build pre-requisite and
456 thus some extra preparation steps will be required first
457
458 * Fork the ``libvirt-ci`` project on gitlab
459
460 * Edit the ``mappings.yml`` change to add an entry for the new build
461 prerequisite, listing its native package name on as many OS distros
462 as practical.
463
464 * Commit the ``mappings.yml`` change and submit a merge request to
465 the ``libvirt-ci`` project, noting in the description that this
466 is a new build pre-requisite desired for use with QEMU
467
468 * CI pipeline will run to validate that the changes to ``mappings.yml``
469 are correct, by attempting to install the newly listed package on
470 all OS distributions supported by ``libvirt-ci``.
471
472 * Once the merge request is accepted, go back to QEMU and update
473 the ``libvirt-ci`` submodule to point to a commit that contains
474 the ``mappings.yml`` update.
475
476
477 Adding new OS distros
478 ^^^^^^^^^^^^^^^^^^^^^
479
480 In some cases ``libvirt-ci`` will not know about the OS distro that is
481 desired to be tested. Before adding a new OS distro, discuss the proposed
482 addition:
483
484 * Send a mail to qemu-devel, copying people listed in the
485 MAINTAINERS file for ``Build and test automation``.
486
487 There are limited CI compute resources available to QEMU, so the
488 cost/benefit tradeoff of adding new OS distros needs to be considered.
489
490 * File an issue at https://gitlab.com/libvirt/libvirt-ci/-/issues
491 pointing to the qemu-devel mail thread in the archives.
492
493 This alerts other people who might be interested in the work
494 to avoid duplication, as well as to get feedback from libvirt-ci
495 maintainers on any tips to ease the addition
496
497 Assuming there is agreement to add a new OS distro then
498
499 * Fork the ``libvirt-ci`` project on gitlab
500
501 * Add metadata under ``guests/lcitool/lcitool/ansible/group_vars/``
502 for the new OS distro. There might be code changes required if
503 the OS distro uses a package format not currently known. The
504 ``libvirt-ci`` maintainers can advise on this when the issue
505 is file.
506
507 * Edit the ``mappings.yml`` change to update all the existing package
508 entries, providing details of the new OS distro
509
510 * Commit the ``mappings.yml`` change and submit a merge request to
511 the ``libvirt-ci`` project, noting in the description that this
512 is a new build pre-requisite desired for use with QEMU
513
514 * CI pipeline will run to validate that the changes to ``mappings.yml``
515 are correct, by attempting to install the newly listed package on
516 all OS distributions supported by ``libvirt-ci``.
517
518 * Once the merge request is accepted, go back to QEMU and update
519 the ``libvirt-ci`` submodule to point to a commit that contains
520 the ``mappings.yml`` update.
521
522
523 Tests
524 ~~~~~
525
526 Different tests are added to cover various configurations to build and test
527 QEMU. Docker tests are the executables under ``tests/docker`` named
528 ``test-*``. They are typically shell scripts and are built on top of a shell
529 library, ``tests/docker/common.rc``, which provides helpers to find the QEMU
530 source and build it.
531
532 The full list of tests is printed in the ``make docker-help`` help.
533
534 Debugging a Docker test failure
535 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
536
537 When CI tasks, maintainers or yourself report a Docker test failure, follow the
538 below steps to debug it:
539
540 1. Locally reproduce the failure with the reported command line. E.g. run
541 ``make docker-test-mingw@fedora J=8``.
542 2. Add "V=1" to the command line, try again, to see the verbose output.
543 3. Further add "DEBUG=1" to the command line. This will pause in a shell prompt
544 in the container right before testing starts. You could either manually
545 build QEMU and run tests from there, or press Ctrl-D to let the Docker
546 testing continue.
547 4. If you press Ctrl-D, the same building and testing procedure will begin, and
548 will hopefully run into the error again. After that, you will be dropped to
549 the prompt for debug.
550
551 Options
552 ~~~~~~~
553
554 Various options can be used to affect how Docker tests are done. The full
555 list is in the ``make docker`` help text. The frequently used ones are:
556
557 * ``V=1``: the same as in top level ``make``. It will be propagated to the
558 container and enable verbose output.
559 * ``J=$N``: the number of parallel tasks in make commands in the container,
560 similar to the ``-j $N`` option in top level ``make``. (The ``-j`` option in
561 top level ``make`` will not be propagated into the container.)
562 * ``DEBUG=1``: enables debug. See the previous "Debugging a Docker test
563 failure" section.
564
565 Thread Sanitizer
566 ----------------
567
568 Thread Sanitizer (TSan) is a tool which can detect data races. QEMU supports
569 building and testing with this tool.
570
571 For more information on TSan:
572
573 https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual
574
575 Thread Sanitizer in Docker
576 ~~~~~~~~~~~~~~~~~~~~~~~~~~
577 TSan is currently supported in the ubuntu2004 docker.
578
579 The test-tsan test will build using TSan and then run make check.
580
581 .. code::
582
583 make docker-test-tsan@ubuntu2004
584
585 TSan warnings under docker are placed in files located at build/tsan/.
586
587 We recommend using DEBUG=1 to allow launching the test from inside the docker,
588 and to allow review of the warnings generated by TSan.
589
590 Building and Testing with TSan
591 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
592
593 It is possible to build and test with TSan, with a few additional steps.
594 These steps are normally done automatically in the docker.
595
596 There is a one time patch needed in clang-9 or clang-10 at this time:
597
598 .. code::
599
600 sed -i 's/^const/static const/g' \
601 /usr/lib/llvm-10/lib/clang/10.0.0/include/sanitizer/tsan_interface.h
602
603 To configure the build for TSan:
604
605 .. code::
606
607 ../configure --enable-tsan --cc=clang-10 --cxx=clang++-10 \
608 --disable-werror --extra-cflags="-O0"
609
610 The runtime behavior of TSAN is controlled by the TSAN_OPTIONS environment
611 variable.
612
613 More information on the TSAN_OPTIONS can be found here:
614
615 https://github.com/google/sanitizers/wiki/ThreadSanitizerFlags
616
617 For example:
618
619 .. code::
620
621 export TSAN_OPTIONS=suppressions=<path to qemu>/tests/tsan/suppressions.tsan \
622 detect_deadlocks=false history_size=7 exitcode=0 \
623 log_path=<build path>/tsan/tsan_warning
624
625 The above exitcode=0 has TSan continue without error if any warnings are found.
626 This allows for running the test and then checking the warnings afterwards.
627 If you want TSan to stop and exit with error on warnings, use exitcode=66.
628
629 TSan Suppressions
630 ~~~~~~~~~~~~~~~~~
631 Keep in mind that for any data race warning, although there might be a data race
632 detected by TSan, there might be no actual bug here. TSan provides several
633 different mechanisms for suppressing warnings. In general it is recommended
634 to fix the code if possible to eliminate the data race rather than suppress
635 the warning.
636
637 A few important files for suppressing warnings are:
638
639 tests/tsan/suppressions.tsan - Has TSan warnings we wish to suppress at runtime.
640 The comment on each suppression will typically indicate why we are
641 suppressing it. More information on the file format can be found here:
642
643 https://github.com/google/sanitizers/wiki/ThreadSanitizerSuppressions
644
645 tests/tsan/blacklist.tsan - Has TSan warnings we wish to disable
646 at compile time for test or debug.
647 Add flags to configure to enable:
648
649 "--extra-cflags=-fsanitize-blacklist=<src path>/tests/tsan/blacklist.tsan"
650
651 More information on the file format can be found here under "Blacklist Format":
652
653 https://github.com/google/sanitizers/wiki/ThreadSanitizerFlags
654
655 TSan Annotations
656 ~~~~~~~~~~~~~~~~
657 include/qemu/tsan.h defines annotations. See this file for more descriptions
658 of the annotations themselves. Annotations can be used to suppress
659 TSan warnings or give TSan more information so that it can detect proper
660 relationships between accesses of data.
661
662 Annotation examples can be found here:
663
664 https://github.com/llvm/llvm-project/tree/master/compiler-rt/test/tsan/
665
666 Good files to start with are: annotate_happens_before.cpp and ignore_race.cpp
667
668 The full set of annotations can be found here:
669
670 https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp
671
672 docker-binfmt-image-debian-% targets
673 ------------------------------------
674
675 It is possible to combine Debian's bootstrap scripts with a configured
676 ``binfmt_misc`` to bootstrap a number of Debian's distros including
677 experimental ports not yet supported by a released OS. This can
678 simplify setting up a rootfs by using docker to contain the foreign
679 rootfs rather than manually invoking chroot.
680
681 Setting up ``binfmt_misc``
682 ~~~~~~~~~~~~~~~~~~~~~~~~~~
683
684 You can use the script ``qemu-binfmt-conf.sh`` to configure a QEMU
685 user binary to automatically run binaries for the foreign
686 architecture. While the scripts will try their best to work with
687 dynamically linked QEMU's a statically linked one will present less
688 potential complications when copying into the docker image. Modern
689 kernels support the ``F`` (fix binary) flag which will open the QEMU
690 executable on setup and avoids the need to find and re-open in the
691 chroot environment. This is triggered with the ``--persistent`` flag.
692
693 Example invocation
694 ~~~~~~~~~~~~~~~~~~
695
696 For example to setup the HPPA ports builds of Debian::
697
698 make docker-binfmt-image-debian-sid-hppa \
699 DEB_TYPE=sid DEB_ARCH=hppa \
700 DEB_URL=http://ftp.ports.debian.org/debian-ports/ \
701 DEB_KEYRING=/usr/share/keyrings/debian-ports-archive-keyring.gpg \
702 EXECUTABLE=(pwd)/qemu-hppa V=1
703
704 The ``DEB_`` variables are substitutions used by
705 ``debian-boostrap.pre`` which is called to do the initial debootstrap
706 of the rootfs before it is copied into the container. The second stage
707 is run as part of the build. The final image will be tagged as
708 ``qemu/debian-sid-hppa``.
709
710 VM testing
711 ----------
712
713 This test suite contains scripts that bootstrap various guest images that have
714 necessary packages to build QEMU. The basic usage is documented in ``Makefile``
715 help which is displayed with ``make vm-help``.
716
717 Quickstart
718 ~~~~~~~~~~
719
720 Run ``make vm-help`` to list available make targets. Invoke a specific make
721 command to run build test in an image. For example, ``make vm-build-freebsd``
722 will build the source tree in the FreeBSD image. The command can be executed
723 from either the source tree or the build dir; if the former, ``./configure`` is
724 not needed. The command will then generate the test image in ``./tests/vm/``
725 under the working directory.
726
727 Note: images created by the scripts accept a well-known RSA key pair for SSH
728 access, so they SHOULD NOT be exposed to external interfaces if you are
729 concerned about attackers taking control of the guest and potentially
730 exploiting a QEMU security bug to compromise the host.
731
732 QEMU binaries
733 ~~~~~~~~~~~~~
734
735 By default, ``qemu-system-x86_64`` is searched in $PATH to run the guest. If
736 there isn't one, or if it is older than 2.10, the test won't work. In this case,
737 provide the QEMU binary in env var: ``QEMU=/path/to/qemu-2.10+``.
738
739 Likewise the path to ``qemu-img`` can be set in QEMU_IMG environment variable.
740
741 Make jobs
742 ~~~~~~~~~
743
744 The ``-j$X`` option in the make command line is not propagated into the VM,
745 specify ``J=$X`` to control the make jobs in the guest.
746
747 Debugging
748 ~~~~~~~~~
749
750 Add ``DEBUG=1`` and/or ``V=1`` to the make command to allow interactive
751 debugging and verbose output. If this is not enough, see the next section.
752 ``V=1`` will be propagated down into the make jobs in the guest.
753
754 Manual invocation
755 ~~~~~~~~~~~~~~~~~
756
757 Each guest script is an executable script with the same command line options.
758 For example to work with the netbsd guest, use ``$QEMU_SRC/tests/vm/netbsd``:
759
760 .. code::
761
762 $ cd $QEMU_SRC/tests/vm
763
764 # To bootstrap the image
765 $ ./netbsd --build-image --image /var/tmp/netbsd.img
766 <...>
767
768 # To run an arbitrary command in guest (the output will not be echoed unless
769 # --debug is added)
770 $ ./netbsd --debug --image /var/tmp/netbsd.img uname -a
771
772 # To build QEMU in guest
773 $ ./netbsd --debug --image /var/tmp/netbsd.img --build-qemu $QEMU_SRC
774
775 # To get to an interactive shell
776 $ ./netbsd --interactive --image /var/tmp/netbsd.img sh
777
778 Adding new guests
779 ~~~~~~~~~~~~~~~~~
780
781 Please look at existing guest scripts for how to add new guests.
782
783 Most importantly, create a subclass of BaseVM and implement ``build_image()``
784 method and define ``BUILD_SCRIPT``, then finally call ``basevm.main()`` from
785 the script's ``main()``.
786
787 * Usually in ``build_image()``, a template image is downloaded from a
788 predefined URL. ``BaseVM._download_with_cache()`` takes care of the cache and
789 the checksum, so consider using it.
790
791 * Once the image is downloaded, users, SSH server and QEMU build deps should
792 be set up:
793
794 - Root password set to ``BaseVM.ROOT_PASS``
795 - User ``BaseVM.GUEST_USER`` is created, and password set to
796 ``BaseVM.GUEST_PASS``
797 - SSH service is enabled and started on boot,
798 ``$QEMU_SRC/tests/keys/id_rsa.pub`` is added to ssh's ``authorized_keys``
799 file of both root and the normal user
800 - DHCP client service is enabled and started on boot, so that it can
801 automatically configure the virtio-net-pci NIC and communicate with QEMU
802 user net (10.0.2.2)
803 - Necessary packages are installed to untar the source tarball and build
804 QEMU
805
806 * Write a proper ``BUILD_SCRIPT`` template, which should be a shell script that
807 untars a raw virtio-blk block device, which is the tarball data blob of the
808 QEMU source tree, then configure/build it. Running "make check" is also
809 recommended.
810
811 Image fuzzer testing
812 --------------------
813
814 An image fuzzer was added to exercise format drivers. Currently only qcow2 is
815 supported. To start the fuzzer, run
816
817 .. code::
818
819 tests/image-fuzzer/runner.py -c '[["qemu-img", "info", "$test_img"]]' /tmp/test qcow2
820
821 Alternatively, some command different from ``qemu-img info`` can be tested, by
822 changing the ``-c`` option.
823
824 Integration tests using the Avocado Framework
825 ---------------------------------------------
826
827 The ``tests/avocado`` directory hosts integration tests. They're usually
828 higher level tests, and may interact with external resources and with
829 various guest operating systems.
830
831 These tests are written using the Avocado Testing Framework (which must
832 be installed separately) in conjunction with a the ``avocado_qemu.Test``
833 class, implemented at ``tests/avocado/avocado_qemu``.
834
835 Tests based on ``avocado_qemu.Test`` can easily:
836
837 * Customize the command line arguments given to the convenience
838 ``self.vm`` attribute (a QEMUMachine instance)
839
840 * Interact with the QEMU monitor, send QMP commands and check
841 their results
842
843 * Interact with the guest OS, using the convenience console device
844 (which may be useful to assert the effectiveness and correctness of
845 command line arguments or QMP commands)
846
847 * Interact with external data files that accompany the test itself
848 (see ``self.get_data()``)
849
850 * Download (and cache) remote data files, such as firmware and kernel
851 images
852
853 * Have access to a library of guest OS images (by means of the
854 ``avocado.utils.vmimage`` library)
855
856 * Make use of various other test related utilities available at the
857 test class itself and at the utility library:
858
859 - http://avocado-framework.readthedocs.io/en/latest/api/test/avocado.html#avocado.Test
860 - http://avocado-framework.readthedocs.io/en/latest/api/utils/avocado.utils.html
861
862 Running tests
863 ~~~~~~~~~~~~~
864
865 You can run the avocado tests simply by executing:
866
867 .. code::
868
869 make check-avocado
870
871 This involves the automatic creation of Python virtual environment
872 within the build tree (at ``tests/venv``) which will have all the
873 right dependencies, and will save tests results also within the
874 build tree (at ``tests/results``).
875
876 Note: the build environment must be using a Python 3 stack, and have
877 the ``venv`` and ``pip`` packages installed. If necessary, make sure
878 ``configure`` is called with ``--python=`` and that those modules are
879 available. On Debian and Ubuntu based systems, depending on the
880 specific version, they may be on packages named ``python3-venv`` and
881 ``python3-pip``.
882
883 It is also possible to run tests based on tags using the
884 ``make check-avocado`` command and the ``AVOCADO_TAGS`` environment
885 variable:
886
887 .. code::
888
889 make check-avocado AVOCADO_TAGS=quick
890
891 Note that tags separated with commas have an AND behavior, while tags
892 separated by spaces have an OR behavior. For more information on Avocado
893 tags, see:
894
895 https://avocado-framework.readthedocs.io/en/latest/guides/user/chapters/tags.html
896
897 To run a single test file, a couple of them, or a test within a file
898 using the ``make check-avocado`` command, set the ``AVOCADO_TESTS``
899 environment variable with the test files or test names. To run all
900 tests from a single file, use:
901
902 .. code::
903
904 make check-avocado AVOCADO_TESTS=$FILEPATH
905
906 The same is valid to run tests from multiple test files:
907
908 .. code::
909
910 make check-avocado AVOCADO_TESTS='$FILEPATH1 $FILEPATH2'
911
912 To run a single test within a file, use:
913
914 .. code::
915
916 make check-avocado AVOCADO_TESTS=$FILEPATH:$TESTCLASS.$TESTNAME
917
918 The same is valid to run single tests from multiple test files:
919
920 .. code::
921
922 make check-avocado AVOCADO_TESTS='$FILEPATH1:$TESTCLASS1.$TESTNAME1 $FILEPATH2:$TESTCLASS2.$TESTNAME2'
923
924 The scripts installed inside the virtual environment may be used
925 without an "activation". For instance, the Avocado test runner
926 may be invoked by running:
927
928 .. code::
929
930 tests/venv/bin/avocado run $OPTION1 $OPTION2 tests/avocado/
931
932 Note that if ``make check-avocado`` was not executed before, it is
933 possible to create the Python virtual environment with the dependencies
934 needed running:
935
936 .. code::
937
938 make check-venv
939
940 It is also possible to run tests from a single file or a single test within
941 a test file. To run tests from a single file within the build tree, use:
942
943 .. code::
944
945 tests/venv/bin/avocado run tests/avocado/$TESTFILE
946
947 To run a single test within a test file, use:
948
949 .. code::
950
951 tests/venv/bin/avocado run tests/avocado/$TESTFILE:$TESTCLASS.$TESTNAME
952
953 Valid test names are visible in the output from any previous execution
954 of Avocado or ``make check-avocado``, and can also be queried using:
955
956 .. code::
957
958 tests/venv/bin/avocado list tests/avocado
959
960 Manual Installation
961 ~~~~~~~~~~~~~~~~~~~
962
963 To manually install Avocado and its dependencies, run:
964
965 .. code::
966
967 pip install --user avocado-framework
968
969 Alternatively, follow the instructions on this link:
970
971 https://avocado-framework.readthedocs.io/en/latest/guides/user/chapters/installing.html
972
973 Overview
974 ~~~~~~~~
975
976 The ``tests/avocado/avocado_qemu`` directory provides the
977 ``avocado_qemu`` Python module, containing the ``avocado_qemu.Test``
978 class. Here's a simple usage example:
979
980 .. code::
981
982 from avocado_qemu import QemuSystemTest
983
984
985 class Version(QemuSystemTest):
986 """
987 :avocado: tags=quick
988 """
989 def test_qmp_human_info_version(self):
990 self.vm.launch()
991 res = self.vm.command('human-monitor-command',
992 command_line='info version')
993 self.assertRegexpMatches(res, r'^(\d+\.\d+\.\d)')
994
995 To execute your test, run:
996
997 .. code::
998
999 avocado run version.py
1000
1001 Tests may be classified according to a convention by using docstring
1002 directives such as ``:avocado: tags=TAG1,TAG2``. To run all tests
1003 in the current directory, tagged as "quick", run:
1004
1005 .. code::
1006
1007 avocado run -t quick .
1008
1009 The ``avocado_qemu.Test`` base test class
1010 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1011
1012 The ``avocado_qemu.Test`` class has a number of characteristics that
1013 are worth being mentioned right away.
1014
1015 First of all, it attempts to give each test a ready to use QEMUMachine
1016 instance, available at ``self.vm``. Because many tests will tweak the
1017 QEMU command line, launching the QEMUMachine (by using ``self.vm.launch()``)
1018 is left to the test writer.
1019
1020 The base test class has also support for tests with more than one
1021 QEMUMachine. The way to get machines is through the ``self.get_vm()``
1022 method which will return a QEMUMachine instance. The ``self.get_vm()``
1023 method accepts arguments that will be passed to the QEMUMachine creation
1024 and also an optional ``name`` attribute so you can identify a specific
1025 machine and get it more than once through the tests methods. A simple
1026 and hypothetical example follows:
1027
1028 .. code::
1029
1030 from avocado_qemu import QemuSystemTest
1031
1032
1033 class MultipleMachines(QemuSystemTest):
1034 def test_multiple_machines(self):
1035 first_machine = self.get_vm()
1036 second_machine = self.get_vm()
1037 self.get_vm(name='third_machine').launch()
1038
1039 first_machine.launch()
1040 second_machine.launch()
1041
1042 first_res = first_machine.command(
1043 'human-monitor-command',
1044 command_line='info version')
1045
1046 second_res = second_machine.command(
1047 'human-monitor-command',
1048 command_line='info version')
1049
1050 third_res = self.get_vm(name='third_machine').command(
1051 'human-monitor-command',
1052 command_line='info version')
1053
1054 self.assertEquals(first_res, second_res, third_res)
1055
1056 At test "tear down", ``avocado_qemu.Test`` handles all the QEMUMachines
1057 shutdown.
1058
1059 The ``avocado_qemu.LinuxTest`` base test class
1060 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1061
1062 The ``avocado_qemu.LinuxTest`` is further specialization of the
1063 ``avocado_qemu.Test`` class, so it contains all the characteristics of
1064 the later plus some extra features.
1065
1066 First of all, this base class is intended for tests that need to
1067 interact with a fully booted and operational Linux guest. At this
1068 time, it uses a Fedora 31 guest image. The most basic example looks
1069 like this:
1070
1071 .. code::
1072
1073 from avocado_qemu import LinuxTest
1074
1075
1076 class SomeTest(LinuxTest):
1077
1078 def test(self):
1079 self.launch_and_wait()
1080 self.ssh_command('some_command_to_be_run_in_the_guest')
1081
1082 Please refer to tests that use ``avocado_qemu.LinuxTest`` under
1083 ``tests/avocado`` for more examples.
1084
1085 QEMUMachine
1086 ~~~~~~~~~~~
1087
1088 The QEMUMachine API is already widely used in the Python iotests,
1089 device-crash-test and other Python scripts. It's a wrapper around the
1090 execution of a QEMU binary, giving its users:
1091
1092 * the ability to set command line arguments to be given to the QEMU
1093 binary
1094
1095 * a ready to use QMP connection and interface, which can be used to
1096 send commands and inspect its results, as well as asynchronous
1097 events
1098
1099 * convenience methods to set commonly used command line arguments in
1100 a more succinct and intuitive way
1101
1102 QEMU binary selection
1103 ^^^^^^^^^^^^^^^^^^^^^
1104
1105 The QEMU binary used for the ``self.vm`` QEMUMachine instance will
1106 primarily depend on the value of the ``qemu_bin`` parameter. If it's
1107 not explicitly set, its default value will be the result of a dynamic
1108 probe in the same source tree. A suitable binary will be one that
1109 targets the architecture matching host machine.
1110
1111 Based on this description, test writers will usually rely on one of
1112 the following approaches:
1113
1114 1) Set ``qemu_bin``, and use the given binary
1115
1116 2) Do not set ``qemu_bin``, and use a QEMU binary named like
1117 "qemu-system-${arch}", either in the current
1118 working directory, or in the current source tree.
1119
1120 The resulting ``qemu_bin`` value will be preserved in the
1121 ``avocado_qemu.Test`` as an attribute with the same name.
1122
1123 Attribute reference
1124 ~~~~~~~~~~~~~~~~~~~
1125
1126 Test
1127 ^^^^
1128
1129 Besides the attributes and methods that are part of the base
1130 ``avocado.Test`` class, the following attributes are available on any
1131 ``avocado_qemu.Test`` instance.
1132
1133 vm
1134 ''
1135
1136 A QEMUMachine instance, initially configured according to the given
1137 ``qemu_bin`` parameter.
1138
1139 arch
1140 ''''
1141
1142 The architecture can be used on different levels of the stack, e.g. by
1143 the framework or by the test itself. At the framework level, it will
1144 currently influence the selection of a QEMU binary (when one is not
1145 explicitly given).
1146
1147 Tests are also free to use this attribute value, for their own needs.
1148 A test may, for instance, use the same value when selecting the
1149 architecture of a kernel or disk image to boot a VM with.
1150
1151 The ``arch`` attribute will be set to the test parameter of the same
1152 name. If one is not given explicitly, it will either be set to
1153 ``None``, or, if the test is tagged with one (and only one)
1154 ``:avocado: tags=arch:VALUE`` tag, it will be set to ``VALUE``.
1155
1156 cpu
1157 '''
1158
1159 The cpu model that will be set to all QEMUMachine instances created
1160 by the test.
1161
1162 The ``cpu`` attribute will be set to the test parameter of the same
1163 name. If one is not given explicitly, it will either be set to
1164 ``None ``, or, if the test is tagged with one (and only one)
1165 ``:avocado: tags=cpu:VALUE`` tag, it will be set to ``VALUE``.
1166
1167 machine
1168 '''''''
1169
1170 The machine type that will be set to all QEMUMachine instances created
1171 by the test.
1172
1173 The ``machine`` attribute will be set to the test parameter of the same
1174 name. If one is not given explicitly, it will either be set to
1175 ``None``, or, if the test is tagged with one (and only one)
1176 ``:avocado: tags=machine:VALUE`` tag, it will be set to ``VALUE``.
1177
1178 qemu_bin
1179 ''''''''
1180
1181 The preserved value of the ``qemu_bin`` parameter or the result of the
1182 dynamic probe for a QEMU binary in the current working directory or
1183 source tree.
1184
1185 LinuxTest
1186 ^^^^^^^^^
1187
1188 Besides the attributes present on the ``avocado_qemu.Test`` base
1189 class, the ``avocado_qemu.LinuxTest`` adds the following attributes:
1190
1191 distro
1192 ''''''
1193
1194 The name of the Linux distribution used as the guest image for the
1195 test. The name should match the **Provider** column on the list
1196 of images supported by the avocado.utils.vmimage library:
1197
1198 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
1199
1200 distro_version
1201 ''''''''''''''
1202
1203 The version of the Linux distribution as the guest image for the
1204 test. The name should match the **Version** column on the list
1205 of images supported by the avocado.utils.vmimage library:
1206
1207 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
1208
1209 distro_checksum
1210 '''''''''''''''
1211
1212 The sha256 hash of the guest image file used for the test.
1213
1214 If this value is not set in the code or by a test parameter (with the
1215 same name), no validation on the integrity of the image will be
1216 performed.
1217
1218 Parameter reference
1219 ~~~~~~~~~~~~~~~~~~~
1220
1221 To understand how Avocado parameters are accessed by tests, and how
1222 they can be passed to tests, please refer to::
1223
1224 https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#accessing-test-parameters
1225
1226 Parameter values can be easily seen in the log files, and will look
1227 like the following:
1228
1229 .. code::
1230
1231 PARAMS (key=qemu_bin, path=*, default=./qemu-system-x86_64) => './qemu-system-x86_64
1232
1233 Test
1234 ^^^^
1235
1236 arch
1237 ''''
1238
1239 The architecture that will influence the selection of a QEMU binary
1240 (when one is not explicitly given).
1241
1242 Tests are also free to use this parameter value, for their own needs.
1243 A test may, for instance, use the same value when selecting the
1244 architecture of a kernel or disk image to boot a VM with.
1245
1246 This parameter has a direct relation with the ``arch`` attribute. If
1247 not given, it will default to None.
1248
1249 cpu
1250 '''
1251
1252 The cpu model that will be set to all QEMUMachine instances created
1253 by the test.
1254
1255 machine
1256 '''''''
1257
1258 The machine type that will be set to all QEMUMachine instances created
1259 by the test.
1260
1261 qemu_bin
1262 ''''''''
1263
1264 The exact QEMU binary to be used on QEMUMachine.
1265
1266 LinuxTest
1267 ^^^^^^^^^
1268
1269 Besides the parameters present on the ``avocado_qemu.Test`` base
1270 class, the ``avocado_qemu.LinuxTest`` adds the following parameters:
1271
1272 distro
1273 ''''''
1274
1275 The name of the Linux distribution used as the guest image for the
1276 test. The name should match the **Provider** column on the list
1277 of images supported by the avocado.utils.vmimage library:
1278
1279 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
1280
1281 distro_version
1282 ''''''''''''''
1283
1284 The version of the Linux distribution as the guest image for the
1285 test. The name should match the **Version** column on the list
1286 of images supported by the avocado.utils.vmimage library:
1287
1288 https://avocado-framework.readthedocs.io/en/latest/guides/writer/libs/vmimage.html#supported-images
1289
1290 distro_checksum
1291 '''''''''''''''
1292
1293 The sha256 hash of the guest image file used for the test.
1294
1295 If this value is not set in the code or by this parameter no
1296 validation on the integrity of the image will be performed.
1297
1298 Skipping tests
1299 ~~~~~~~~~~~~~~
1300
1301 The Avocado framework provides Python decorators which allow for easily skip
1302 tests running under certain conditions. For example, on the lack of a binary
1303 on the test system or when the running environment is a CI system. For further
1304 information about those decorators, please refer to::
1305
1306 https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#skipping-tests
1307
1308 While the conditions for skipping tests are often specifics of each one, there
1309 are recurring scenarios identified by the QEMU developers and the use of
1310 environment variables became a kind of standard way to enable/disable tests.
1311
1312 Here is a list of the most used variables:
1313
1314 AVOCADO_ALLOW_LARGE_STORAGE
1315 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1316 Tests which are going to fetch or produce assets considered *large* are not
1317 going to run unless that ``AVOCADO_ALLOW_LARGE_STORAGE=1`` is exported on
1318 the environment.
1319
1320 The definition of *large* is a bit arbitrary here, but it usually means an
1321 asset which occupies at least 1GB of size on disk when uncompressed.
1322
1323 AVOCADO_ALLOW_UNTRUSTED_CODE
1324 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1325 There are tests which will boot a kernel image or firmware that can be
1326 considered not safe to run on the developer's workstation, thus they are
1327 skipped by default. The definition of *not safe* is also arbitrary but
1328 usually it means a blob which either its source or build process aren't
1329 public available.
1330
1331 You should export ``AVOCADO_ALLOW_UNTRUSTED_CODE=1`` on the environment in
1332 order to allow tests which make use of those kind of assets.
1333
1334 AVOCADO_TIMEOUT_EXPECTED
1335 ^^^^^^^^^^^^^^^^^^^^^^^^
1336 The Avocado framework has a timeout mechanism which interrupts tests to avoid the
1337 test suite of getting stuck. The timeout value can be set via test parameter or
1338 property defined in the test class, for further details::
1339
1340 https://avocado-framework.readthedocs.io/en/latest/guides/writer/chapters/writing.html#setting-a-test-timeout
1341
1342 Even though the timeout can be set by the test developer, there are some tests
1343 that may not have a well-defined limit of time to finish under certain
1344 conditions. For example, tests that take longer to execute when QEMU is
1345 compiled with debug flags. Therefore, the ``AVOCADO_TIMEOUT_EXPECTED`` variable
1346 has been used to determine whether those tests should run or not.
1347
1348 GITLAB_CI
1349 ^^^^^^^^^
1350 A number of tests are flagged to not run on the GitLab CI. Usually because
1351 they proved to the flaky or there are constraints on the CI environment which
1352 would make them fail. If you encounter a similar situation then use that
1353 variable as shown on the code snippet below to skip the test:
1354
1355 .. code::
1356
1357 @skipIf(os.getenv('GITLAB_CI'), 'Running on GitLab')
1358 def test(self):
1359 do_something()
1360
1361 Uninstalling Avocado
1362 ~~~~~~~~~~~~~~~~~~~~
1363
1364 If you've followed the manual installation instructions above, you can
1365 easily uninstall Avocado. Start by listing the packages you have
1366 installed::
1367
1368 pip list --user
1369
1370 And remove any package you want with::
1371
1372 pip uninstall <package_name>
1373
1374 If you've used ``make check-avocado``, the Python virtual environment where
1375 Avocado is installed will be cleaned up as part of ``make check-clean``.
1376
1377 .. _checktcg-ref:
1378
1379 Testing with "make check-tcg"
1380 -----------------------------
1381
1382 The check-tcg tests are intended for simple smoke tests of both
1383 linux-user and softmmu TCG functionality. However to build test
1384 programs for guest targets you need to have cross compilers available.
1385 If your distribution supports cross compilers you can do something as
1386 simple as::
1387
1388 apt install gcc-aarch64-linux-gnu
1389
1390 The configure script will automatically pick up their presence.
1391 Sometimes compilers have slightly odd names so the availability of
1392 them can be prompted by passing in the appropriate configure option
1393 for the architecture in question, for example::
1394
1395 $(configure) --cross-cc-aarch64=aarch64-cc
1396
1397 There is also a ``--cross-cc-cflags-ARCH`` flag in case additional
1398 compiler flags are needed to build for a given target.
1399
1400 If you have the ability to run containers as the user the build system
1401 will automatically use them where no system compiler is available. For
1402 architectures where we also support building QEMU we will generally
1403 use the same container to build tests. However there are a number of
1404 additional containers defined that have a minimal cross-build
1405 environment that is only suitable for building test cases. Sometimes
1406 we may use a bleeding edge distribution for compiler features needed
1407 for test cases that aren't yet in the LTS distros we support for QEMU
1408 itself.
1409
1410 See :ref:`container-ref` for more details.
1411
1412 Running subset of tests
1413 ~~~~~~~~~~~~~~~~~~~~~~~
1414
1415 You can build the tests for one architecture::
1416
1417 make build-tcg-tests-$TARGET
1418
1419 And run with::
1420
1421 make run-tcg-tests-$TARGET
1422
1423 Adding ``V=1`` to the invocation will show the details of how to
1424 invoke QEMU for the test which is useful for debugging tests.
1425
1426 TCG test dependencies
1427 ~~~~~~~~~~~~~~~~~~~~~
1428
1429 The TCG tests are deliberately very light on dependencies and are
1430 either totally bare with minimal gcc lib support (for softmmu tests)
1431 or just glibc (for linux-user tests). This is because getting a cross
1432 compiler to work with additional libraries can be challenging.
1433
1434 Other TCG Tests
1435 ---------------
1436
1437 There are a number of out-of-tree test suites that are used for more
1438 extensive testing of processor features.
1439
1440 KVM Unit Tests
1441 ~~~~~~~~~~~~~~
1442
1443 The KVM unit tests are designed to run as a Guest OS under KVM but
1444 there is no reason why they can't exercise the TCG as well. It
1445 provides a minimal OS kernel with hooks for enabling the MMU as well
1446 as reporting test results via a special device::
1447
1448 https://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
1449
1450 Linux Test Project
1451 ~~~~~~~~~~~~~~~~~~
1452
1453 The LTP is focused on exercising the syscall interface of a Linux
1454 kernel. It checks that syscalls behave as documented and strives to
1455 exercise as many corner cases as possible. It is a useful test suite
1456 to run to exercise QEMU's linux-user code::
1457
1458 https://linux-test-project.github.io/
1459
1460 GCC gcov support
1461 ----------------
1462
1463 ``gcov`` is a GCC tool to analyze the testing coverage by
1464 instrumenting the tested code. To use it, configure QEMU with
1465 ``--enable-gcov`` option and build. Then run the tests as usual.
1466
1467 If you want to gather coverage information on a single test the ``make
1468 clean-gcda`` target can be used to delete any existing coverage
1469 information before running a single test.
1470
1471 You can generate a HTML coverage report by executing ``make
1472 coverage-html`` which will create
1473 ``meson-logs/coveragereport/index.html``.
1474
1475 Further analysis can be conducted by running the ``gcov`` command
1476 directly on the various .gcda output files. Please read the ``gcov``
1477 documentation for more information.