xref: /openbmc/qemu/docs/devel/testing/main.rst (revision dfbc72a7)
1ff41da50SThomas Huth.. _testing:
2ff41da50SThomas Huth
3ff41da50SThomas HuthTesting in QEMU
4ff41da50SThomas Huth===============
5ff41da50SThomas Huth
6ff41da50SThomas HuthQEMU's testing infrastructure is fairly complex as it covers
7ff41da50SThomas Hutheverything from unit testing and exercising specific sub-systems all
8ff41da50SThomas Huththe way to full blown acceptance tests. To get an overview of the
9ff41da50SThomas Huthtests you can run ``make check-help`` from either the source or build
10ff41da50SThomas Huthtree.
11ff41da50SThomas Huth
12ff41da50SThomas HuthMost (but not all) tests are also integrated into the meson build
13ff41da50SThomas Huthsystem so can be run directly from the build tree, for example:
14ff41da50SThomas Huth
15ff41da50SThomas Huth.. code::
16ff41da50SThomas Huth
17ff41da50SThomas Huth  [./pyvenv/bin/]meson test --suite qemu:softfloat
18ff41da50SThomas Huth
19ff41da50SThomas Huthwill run just the softfloat tests.
20ff41da50SThomas Huth
21ff41da50SThomas HuthThe rest of this document will cover the details for specific test
22ff41da50SThomas Huthgroups.
23ff41da50SThomas Huth
24ff41da50SThomas HuthTesting with "make check"
25ff41da50SThomas Huth-------------------------
26ff41da50SThomas Huth
27ff41da50SThomas HuthThe "make check" testing family includes most of the C based tests in QEMU.
28ff41da50SThomas Huth
29ff41da50SThomas HuthThe usual way to run these tests is:
30ff41da50SThomas Huth
31ff41da50SThomas Huth.. code::
32ff41da50SThomas Huth
33ff41da50SThomas Huth  make check
34ff41da50SThomas Huth
35ff41da50SThomas Huthwhich includes QAPI schema tests, unit tests, QTests and some iotests.
36ff41da50SThomas HuthDifferent sub-types of "make check" tests will be explained below.
37ff41da50SThomas Huth
38ff41da50SThomas HuthBefore running tests, it is best to build QEMU programs first. Some tests
39ff41da50SThomas Huthexpect the executables to exist and will fail with obscure messages if they
40ff41da50SThomas Huthcannot find them.
41ff41da50SThomas Huth
42ff41da50SThomas HuthUnit tests
43ff41da50SThomas Huth~~~~~~~~~~
44ff41da50SThomas Huth
45ff41da50SThomas HuthUnit tests, which can be invoked with ``make check-unit``, are simple C tests
46ff41da50SThomas Huththat typically link to individual QEMU object files and exercise them by
47ff41da50SThomas Huthcalling exported functions.
48ff41da50SThomas Huth
49ff41da50SThomas HuthIf you are writing new code in QEMU, consider adding a unit test, especially
50ff41da50SThomas Huthfor utility modules that are relatively stateless or have few dependencies. To
51ff41da50SThomas Huthadd a new unit test:
52ff41da50SThomas Huth
53ff41da50SThomas Huth1. Create a new source file. For example, ``tests/unit/foo-test.c``.
54ff41da50SThomas Huth
55ff41da50SThomas Huth2. Write the test. Normally you would include the header file which exports
56ff41da50SThomas Huth   the module API, then verify the interface behaves as expected from your
57ff41da50SThomas Huth   test. The test code should be organized with the glib testing framework.
58ff41da50SThomas Huth   Copying and modifying an existing test is usually a good idea.
59ff41da50SThomas Huth
60ff41da50SThomas Huth3. Add the test to ``tests/unit/meson.build``. The unit tests are listed in a
61ff41da50SThomas Huth   dictionary called ``tests``.  The values are any additional sources and
62ff41da50SThomas Huth   dependencies to be linked with the test.  For a simple test whose source
63ff41da50SThomas Huth   is in ``tests/unit/foo-test.c``, it is enough to add an entry like::
64ff41da50SThomas Huth
65ff41da50SThomas Huth     {
66ff41da50SThomas Huth       ...
67ff41da50SThomas Huth       'foo-test': [],
68ff41da50SThomas Huth       ...
69ff41da50SThomas Huth     }
70ff41da50SThomas Huth
71ff41da50SThomas HuthSince unit tests don't require environment variables, the simplest way to debug
72ff41da50SThomas Hutha unit test failure is often directly invoking it or even running it under
73ff41da50SThomas Huth``gdb``. However there can still be differences in behavior between ``make``
74ff41da50SThomas Huthinvocations and your manual run, due to ``$MALLOC_PERTURB_`` environment
75ff41da50SThomas Huthvariable (which affects memory reclamation and catches invalid pointers better)
76ff41da50SThomas Huthand gtester options. If necessary, you can run
77ff41da50SThomas Huth
78ff41da50SThomas Huth.. code::
79ff41da50SThomas Huth
80ff41da50SThomas Huth  make check-unit V=1
81ff41da50SThomas Huth
82ff41da50SThomas Huthand copy the actual command line which executes the unit test, then run
83ff41da50SThomas Huthit from the command line.
84ff41da50SThomas Huth
85ff41da50SThomas HuthQTest
86ff41da50SThomas Huth~~~~~
87ff41da50SThomas Huth
88ff41da50SThomas HuthQTest is a device emulation testing framework.  It can be very useful to test
89ff41da50SThomas Huthdevice models; it could also control certain aspects of QEMU (such as virtual
90ff41da50SThomas Huthclock stepping), with a special purpose "qtest" protocol.  Refer to
91ff41da50SThomas Huth:doc:`qtest` for more details.
92ff41da50SThomas Huth
93ff41da50SThomas HuthQTest cases can be executed with
94ff41da50SThomas Huth
95ff41da50SThomas Huth.. code::
96ff41da50SThomas Huth
97ff41da50SThomas Huth   make check-qtest
98ff41da50SThomas Huth
99ff41da50SThomas HuthWriting portable test cases
100ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~~~~~
101ff41da50SThomas HuthBoth unit tests and qtests can run on POSIX hosts as well as Windows hosts.
102ff41da50SThomas HuthCare must be taken when writing portable test cases that can be built and run
103ff41da50SThomas Huthsuccessfully on various hosts. The following list shows some best practices:
104ff41da50SThomas Huth
105ff41da50SThomas Huth* Use portable APIs from glib whenever necessary, e.g.: g_setenv(),
106ff41da50SThomas Huth  g_mkdtemp(), g_mkdir().
107ff41da50SThomas Huth* Avoid using hardcoded /tmp for temporary file directory.
108ff41da50SThomas Huth  Use g_get_tmp_dir() instead.
109ff41da50SThomas Huth* Bear in mind that Windows has different special string representation for
110ff41da50SThomas Huth  stdin/stdout/stderr and null devices. For example if your test case uses
111ff41da50SThomas Huth  "/dev/fd/2" and "/dev/null" on Linux, remember to use "2" and "nul" on
112ff41da50SThomas Huth  Windows instead. Also IO redirection does not work on Windows, so avoid
113ff41da50SThomas Huth  using "2>nul" whenever necessary.
114ff41da50SThomas Huth* If your test cases uses the blkdebug feature, use relative path to pass
115ff41da50SThomas Huth  the config and image file paths in the command line as Windows absolute
116ff41da50SThomas Huth  path contains the delimiter ":" which will confuse the blkdebug parser.
117ff41da50SThomas Huth* Use double quotes in your extra QEMU command line in your test cases
118ff41da50SThomas Huth  instead of single quotes, as Windows does not drop single quotes when
119ff41da50SThomas Huth  passing the command line to QEMU.
120ff41da50SThomas Huth* Windows opens a file in text mode by default, while a POSIX compliant
121ff41da50SThomas Huth  implementation treats text files and binary files the same. So if your
122ff41da50SThomas Huth  test cases opens a file to write some data and later wants to compare the
123ff41da50SThomas Huth  written data with the original one, be sure to pass the letter 'b' as
124ff41da50SThomas Huth  part of the mode string to fopen(), or O_BINARY flag for the open() call.
125ff41da50SThomas Huth* If a certain test case can only run on POSIX or Linux hosts, use a proper
126ff41da50SThomas Huth  #ifdef in the codes. If the whole test suite cannot run on Windows, disable
127ff41da50SThomas Huth  the build in the meson.build file.
128ff41da50SThomas Huth
129ff41da50SThomas HuthQAPI schema tests
130ff41da50SThomas Huth~~~~~~~~~~~~~~~~~
131ff41da50SThomas Huth
132ff41da50SThomas HuthThe QAPI schema tests validate the QAPI parser used by QMP, by feeding
133ff41da50SThomas Huthpredefined input to the parser and comparing the result with the reference
134ff41da50SThomas Huthoutput.
135ff41da50SThomas Huth
136ff41da50SThomas HuthThe input/output data is managed under the ``tests/qapi-schema`` directory.
137ff41da50SThomas HuthEach test case includes four files that have a common base name:
138ff41da50SThomas Huth
139ff41da50SThomas Huth  * ``${casename}.json`` - the file contains the JSON input for feeding the
140ff41da50SThomas Huth    parser
141ff41da50SThomas Huth  * ``${casename}.out`` - the file contains the expected stdout from the parser
142ff41da50SThomas Huth  * ``${casename}.err`` - the file contains the expected stderr from the parser
143ff41da50SThomas Huth  * ``${casename}.exit`` - the expected error code
144ff41da50SThomas Huth
145ff41da50SThomas HuthConsider adding a new QAPI schema test when you are making a change on the QAPI
146ff41da50SThomas Huthparser (either fixing a bug or extending/modifying the syntax). To do this:
147ff41da50SThomas Huth
148ff41da50SThomas Huth1. Add four files for the new case as explained above. For example:
149ff41da50SThomas Huth
150ff41da50SThomas Huth  ``$EDITOR tests/qapi-schema/foo.{json,out,err,exit}``.
151ff41da50SThomas Huth
152ff41da50SThomas Huth2. Add the new test in ``tests/Makefile.include``. For example:
153ff41da50SThomas Huth
154ff41da50SThomas Huth  ``qapi-schema += foo.json``
155ff41da50SThomas Huth
156ff41da50SThomas Huthcheck-block
157ff41da50SThomas Huth~~~~~~~~~~~
158ff41da50SThomas Huth
159ff41da50SThomas Huth``make check-block`` runs a subset of the block layer iotests (the tests that
160ff41da50SThomas Huthare in the "auto" group).
161ff41da50SThomas HuthSee the "QEMU iotests" section below for more information.
162ff41da50SThomas Huth
163ff41da50SThomas HuthQEMU iotests
164ff41da50SThomas Huth------------
165ff41da50SThomas Huth
166ff41da50SThomas HuthQEMU iotests, under the directory ``tests/qemu-iotests``, is the testing
167ff41da50SThomas Huthframework widely used to test block layer related features. It is higher level
168ff41da50SThomas Huththan "make check" tests and 99% of the code is written in bash or Python
169ff41da50SThomas Huthscripts.  The testing success criteria is golden output comparison, and the
170ff41da50SThomas Huthtest files are named with numbers.
171ff41da50SThomas Huth
172ff41da50SThomas HuthTo run iotests, make sure QEMU is built successfully, then switch to the
173ff41da50SThomas Huth``tests/qemu-iotests`` directory under the build directory, and run ``./check``
174ff41da50SThomas Huthwith desired arguments from there.
175ff41da50SThomas Huth
176ff41da50SThomas HuthBy default, "raw" format and "file" protocol is used; all tests will be
177ff41da50SThomas Huthexecuted, except the unsupported ones. You can override the format and protocol
178ff41da50SThomas Huthwith arguments:
179ff41da50SThomas Huth
180ff41da50SThomas Huth.. code::
181ff41da50SThomas Huth
182ff41da50SThomas Huth  # test with qcow2 format
183ff41da50SThomas Huth  ./check -qcow2
184ff41da50SThomas Huth  # or test a different protocol
185ff41da50SThomas Huth  ./check -nbd
186ff41da50SThomas Huth
187ff41da50SThomas HuthIt's also possible to list test numbers explicitly:
188ff41da50SThomas Huth
189ff41da50SThomas Huth.. code::
190ff41da50SThomas Huth
191ff41da50SThomas Huth  # run selected cases with qcow2 format
192ff41da50SThomas Huth  ./check -qcow2 001 030 153
193ff41da50SThomas Huth
194ff41da50SThomas HuthCache mode can be selected with the "-c" option, which may help reveal bugs
195ff41da50SThomas Huththat are specific to certain cache mode.
196ff41da50SThomas Huth
197ff41da50SThomas HuthMore options are supported by the ``./check`` script, run ``./check -h`` for
198ff41da50SThomas Huthhelp.
199ff41da50SThomas Huth
200ff41da50SThomas HuthWriting a new test case
201ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~
202ff41da50SThomas Huth
203ff41da50SThomas HuthConsider writing a tests case when you are making any changes to the block
204ff41da50SThomas Huthlayer. An iotest case is usually the choice for that. There are already many
205ff41da50SThomas Huthtest cases, so it is possible that extending one of them may achieve the goal
206ff41da50SThomas Huthand save the boilerplate to create one.  (Unfortunately, there isn't a 100%
207ff41da50SThomas Huthreliable way to find a related one out of hundreds of tests.  One approach is
208ff41da50SThomas Huthusing ``git grep``.)
209ff41da50SThomas Huth
210ff41da50SThomas HuthUsually an iotest case consists of two files. One is an executable that
211ff41da50SThomas Huthproduces output to stdout and stderr, the other is the expected reference
212ff41da50SThomas Huthoutput. They are given the same number in file names. E.g. Test script ``055``
213ff41da50SThomas Huthand reference output ``055.out``.
214ff41da50SThomas Huth
215ff41da50SThomas HuthIn rare cases, when outputs differ between cache mode ``none`` and others, a
216ff41da50SThomas Huth``.out.nocache`` file is added. In other cases, when outputs differ between
217ff41da50SThomas Huthimage formats, more than one ``.out`` files are created ending with the
218ff41da50SThomas Huthrespective format names, e.g. ``178.out.qcow2`` and ``178.out.raw``.
219ff41da50SThomas Huth
220ff41da50SThomas HuthThere isn't a hard rule about how to write a test script, but a new test is
221ff41da50SThomas Huthusually a (copy and) modification of an existing case.  There are a few
222ff41da50SThomas Huthcommonly used ways to create a test:
223ff41da50SThomas Huth
224ff41da50SThomas Huth* A Bash script. It will make use of several environmental variables related
225ff41da50SThomas Huth  to the testing procedure, and could source a group of ``common.*`` libraries
226ff41da50SThomas Huth  for some common helper routines.
227ff41da50SThomas Huth
228ff41da50SThomas Huth* A Python unittest script. Import ``iotests`` and create a subclass of
229ff41da50SThomas Huth  ``iotests.QMPTestCase``, then call ``iotests.main`` method. The downside of
230ff41da50SThomas Huth  this approach is that the output is too scarce, and the script is considered
231ff41da50SThomas Huth  harder to debug.
232ff41da50SThomas Huth
233ff41da50SThomas Huth* A simple Python script without using unittest module. This could also import
234ff41da50SThomas Huth  ``iotests`` for launching QEMU and utilities etc, but it doesn't inherit
235ff41da50SThomas Huth  from ``iotests.QMPTestCase`` therefore doesn't use the Python unittest
236ff41da50SThomas Huth  execution. This is a combination of 1 and 2.
237ff41da50SThomas Huth
238ff41da50SThomas HuthPick the language per your preference since both Bash and Python have
239ff41da50SThomas Huthcomparable library support for invoking and interacting with QEMU programs. If
240ff41da50SThomas Huthyou opt for Python, it is strongly recommended to write Python 3 compatible
241ff41da50SThomas Huthcode.
242ff41da50SThomas Huth
243ff41da50SThomas HuthBoth Python and Bash frameworks in iotests provide helpers to manage test
244ff41da50SThomas Huthimages. They can be used to create and clean up images under the test
245ff41da50SThomas Huthdirectory. If no I/O or any protocol specific feature is needed, it is often
246ff41da50SThomas Huthmore convenient to use the pseudo block driver, ``null-co://``, as the test
247ff41da50SThomas Huthimage, which doesn't require image creation or cleaning up. Avoid system-wide
248ff41da50SThomas Huthdevices or files whenever possible, such as ``/dev/null`` or ``/dev/zero``.
249ff41da50SThomas HuthOtherwise, image locking implications have to be considered.  For example,
250ff41da50SThomas Huthanother application on the host may have locked the file, possibly leading to a
251ff41da50SThomas Huthtest failure.  If using such devices are explicitly desired, consider adding
252ff41da50SThomas Huth``locking=off`` option to disable image locking.
253ff41da50SThomas Huth
254ff41da50SThomas HuthDebugging a test case
255ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~
256ff41da50SThomas Huth
257ff41da50SThomas HuthThe following options to the ``check`` script can be useful when debugging
258ff41da50SThomas Hutha failing test:
259ff41da50SThomas Huth
260ff41da50SThomas Huth* ``-gdb`` wraps every QEMU invocation in a ``gdbserver``, which waits for a
261ff41da50SThomas Huth  connection from a gdb client.  The options given to ``gdbserver`` (e.g. the
262ff41da50SThomas Huth  address on which to listen for connections) are taken from the ``$GDB_OPTIONS``
263ff41da50SThomas Huth  environment variable.  By default (if ``$GDB_OPTIONS`` is empty), it listens on
264ff41da50SThomas Huth  ``localhost:12345``.
265ff41da50SThomas Huth  It is possible to connect to it for example with
266ff41da50SThomas Huth  ``gdb -iex "target remote $addr"``, where ``$addr`` is the address
267ff41da50SThomas Huth  ``gdbserver`` listens on.
268ff41da50SThomas Huth  If the ``-gdb`` option is not used, ``$GDB_OPTIONS`` is ignored,
269ff41da50SThomas Huth  regardless of whether it is set or not.
270ff41da50SThomas Huth
271ff41da50SThomas Huth* ``-valgrind`` attaches a valgrind instance to QEMU. If it detects
272ff41da50SThomas Huth  warnings, it will print and save the log in
273ff41da50SThomas Huth  ``$TEST_DIR/<valgrind_pid>.valgrind``.
274ff41da50SThomas Huth  The final command line will be ``valgrind --log-file=$TEST_DIR/
275ff41da50SThomas Huth  <valgrind_pid>.valgrind --error-exitcode=99 $QEMU ...``
276ff41da50SThomas Huth
277ff41da50SThomas Huth* ``-d`` (debug) just increases the logging verbosity, showing
278ff41da50SThomas Huth  for example the QMP commands and answers.
279ff41da50SThomas Huth
280ff41da50SThomas Huth* ``-p`` (print) redirects QEMU’s stdout and stderr to the test output,
281ff41da50SThomas Huth  instead of saving it into a log file in
282ff41da50SThomas Huth  ``$TEST_DIR/qemu-machine-<random_string>``.
283ff41da50SThomas Huth
284ff41da50SThomas HuthTest case groups
285ff41da50SThomas Huth~~~~~~~~~~~~~~~~
286ff41da50SThomas Huth
287ff41da50SThomas Huth"Tests may belong to one or more test groups, which are defined in the form
288ff41da50SThomas Huthof a comment in the test source file. By convention, test groups are listed
289ff41da50SThomas Huthin the second line of the test file, after the "#!/..." line, like this:
290ff41da50SThomas Huth
291ff41da50SThomas Huth.. code::
292ff41da50SThomas Huth
293ff41da50SThomas Huth  #!/usr/bin/env python3
294ff41da50SThomas Huth  # group: auto quick
295ff41da50SThomas Huth  #
296ff41da50SThomas Huth  ...
297ff41da50SThomas Huth
298ff41da50SThomas HuthAnother way of defining groups is creating the tests/qemu-iotests/group.local
299ff41da50SThomas Huthfile. This should be used only for downstream (this file should never appear
300ff41da50SThomas Huthin upstream). This file may be used for defining some downstream test groups
301ff41da50SThomas Huthor for temporarily disabling tests, like this:
302ff41da50SThomas Huth
303ff41da50SThomas Huth.. code::
304ff41da50SThomas Huth
305ff41da50SThomas Huth  # groups for some company downstream process
306ff41da50SThomas Huth  #
307ff41da50SThomas Huth  # ci - tests to run on build
308ff41da50SThomas Huth  # down - our downstream tests, not for upstream
309ff41da50SThomas Huth  #
310ff41da50SThomas Huth  # Format of each line is:
311ff41da50SThomas Huth  # TEST_NAME TEST_GROUP [TEST_GROUP ]...
312ff41da50SThomas Huth
313ff41da50SThomas Huth  013 ci
314ff41da50SThomas Huth  210 disabled
315ff41da50SThomas Huth  215 disabled
316ff41da50SThomas Huth  our-ugly-workaround-test down ci
317ff41da50SThomas Huth
318ff41da50SThomas HuthNote that the following group names have a special meaning:
319ff41da50SThomas Huth
320ff41da50SThomas Huth- quick: Tests in this group should finish within a few seconds.
321ff41da50SThomas Huth
322ff41da50SThomas Huth- auto: Tests in this group are used during "make check" and should be
323ff41da50SThomas Huth  runnable in any case. That means they should run with every QEMU binary
324ff41da50SThomas Huth  (also non-x86), with every QEMU configuration (i.e. must not fail if
325ff41da50SThomas Huth  an optional feature is not compiled in - but reporting a "skip" is ok),
326ff41da50SThomas Huth  work at least with the qcow2 file format, work with all kind of host
327ff41da50SThomas Huth  filesystems and users (e.g. "nobody" or "root") and must not take too
328ff41da50SThomas Huth  much memory and disk space (since CI pipelines tend to fail otherwise).
329ff41da50SThomas Huth
330ff41da50SThomas Huth- disabled: Tests in this group are disabled and ignored by check.
331ff41da50SThomas Huth
332ff41da50SThomas Huth.. _container-ref:
333ff41da50SThomas Huth
334ff41da50SThomas HuthContainer based tests
335ff41da50SThomas Huth---------------------
336ff41da50SThomas Huth
337ff41da50SThomas HuthIntroduction
338ff41da50SThomas Huth~~~~~~~~~~~~
339ff41da50SThomas Huth
340ff41da50SThomas HuthThe container testing framework in QEMU utilizes public images to
341ff41da50SThomas Huthbuild and test QEMU in predefined and widely accessible Linux
342ff41da50SThomas Huthenvironments. This makes it possible to expand the test coverage
343ff41da50SThomas Huthacross distros, toolchain flavors and library versions. The support
344ff41da50SThomas Huthwas originally written for Docker although we also support Podman as
345ff41da50SThomas Huthan alternative container runtime. Although many of the target
346ff41da50SThomas Huthnames and scripts are prefixed with "docker" the system will
347ff41da50SThomas Huthautomatically run on whichever is configured.
348ff41da50SThomas Huth
349ff41da50SThomas HuthThe container images are also used to augment the generation of tests
350ff41da50SThomas Huthfor testing TCG. See :ref:`checktcg-ref` for more details.
351ff41da50SThomas Huth
352ff41da50SThomas HuthDocker Prerequisites
353ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~
354ff41da50SThomas Huth
355ff41da50SThomas HuthInstall "docker" with the system package manager and start the Docker service
356ff41da50SThomas Huthon your development machine, then make sure you have the privilege to run
357ff41da50SThomas HuthDocker commands. Typically it means setting up passwordless ``sudo docker``
358ff41da50SThomas Huthcommand or login as root. For example:
359ff41da50SThomas Huth
360ff41da50SThomas Huth.. code::
361ff41da50SThomas Huth
362ff41da50SThomas Huth  $ sudo yum install docker
363ff41da50SThomas Huth  $ # or `apt-get install docker` for Ubuntu, etc.
364ff41da50SThomas Huth  $ sudo systemctl start docker
365ff41da50SThomas Huth  $ sudo docker ps
366ff41da50SThomas Huth
367ff41da50SThomas HuthThe last command should print an empty table, to verify the system is ready.
368ff41da50SThomas Huth
369ff41da50SThomas HuthAn alternative method to set up permissions is by adding the current user to
370ff41da50SThomas Huth"docker" group and making the docker daemon socket file (by default
371ff41da50SThomas Huth``/var/run/docker.sock``) accessible to the group:
372ff41da50SThomas Huth
373ff41da50SThomas Huth.. code::
374ff41da50SThomas Huth
375ff41da50SThomas Huth  $ sudo groupadd docker
376ff41da50SThomas Huth  $ sudo usermod $USER -a -G docker
377ff41da50SThomas Huth  $ sudo chown :docker /var/run/docker.sock
378ff41da50SThomas Huth
379ff41da50SThomas HuthNote that any one of above configurations makes it possible for the user to
380ff41da50SThomas Huthexploit the whole host with Docker bind mounting or other privileged
381ff41da50SThomas Huthoperations.  So only do it on development machines.
382ff41da50SThomas Huth
383ff41da50SThomas HuthPodman Prerequisites
384ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~
385ff41da50SThomas Huth
386ff41da50SThomas HuthInstall "podman" with the system package manager.
387ff41da50SThomas Huth
388ff41da50SThomas Huth.. code::
389ff41da50SThomas Huth
390ff41da50SThomas Huth  $ sudo dnf install podman
391ff41da50SThomas Huth  $ podman ps
392ff41da50SThomas Huth
393ff41da50SThomas HuthThe last command should print an empty table, to verify the system is ready.
394ff41da50SThomas Huth
395ff41da50SThomas HuthQuickstart
396ff41da50SThomas Huth~~~~~~~~~~
397ff41da50SThomas Huth
398ff41da50SThomas HuthFrom source tree, type ``make docker-help`` to see the help. Testing
399ff41da50SThomas Huthcan be started without configuring or building QEMU (``configure`` and
400ff41da50SThomas Huth``make`` are done in the container, with parameters defined by the
401ff41da50SThomas Huthmake target):
402ff41da50SThomas Huth
403ff41da50SThomas Huth.. code::
404ff41da50SThomas Huth
405ff41da50SThomas Huth  make docker-test-build@debian
406ff41da50SThomas Huth
407ff41da50SThomas HuthThis will create a container instance using the ``debian`` image (the image
408ff41da50SThomas Huthis downloaded and initialized automatically), in which the ``test-build`` job
409ff41da50SThomas Huthis executed.
410ff41da50SThomas Huth
411ff41da50SThomas HuthRegistry
412ff41da50SThomas Huth~~~~~~~~
413ff41da50SThomas Huth
414ff41da50SThomas HuthThe QEMU project has a container registry hosted by GitLab at
415ff41da50SThomas Huth``registry.gitlab.com/qemu-project/qemu`` which will automatically be
416ff41da50SThomas Huthused to pull in pre-built layers. This avoids unnecessary strain on
417ff41da50SThomas Huththe distro archives created by multiple developers running the same
418ff41da50SThomas Huthcontainer build steps over and over again. This can be overridden
419ff41da50SThomas Huthlocally by using the ``NOCACHE`` build option:
420ff41da50SThomas Huth
421ff41da50SThomas Huth.. code::
422ff41da50SThomas Huth
423ff41da50SThomas Huth   make docker-image-debian-arm64-cross NOCACHE=1
424ff41da50SThomas Huth
425ff41da50SThomas HuthImages
426ff41da50SThomas Huth~~~~~~
427ff41da50SThomas Huth
428ff41da50SThomas HuthAlong with many other images, the ``debian`` image is defined in a Dockerfile
429ff41da50SThomas Huthin ``tests/docker/dockerfiles/``, called ``debian.docker``. ``make docker-help``
430ff41da50SThomas Huthcommand will list all the available images.
431ff41da50SThomas Huth
432ff41da50SThomas HuthA ``.pre`` script can be added beside the ``.docker`` file, which will be
433ff41da50SThomas Huthexecuted before building the image under the build context directory. This is
434ff41da50SThomas Huthmainly used to do necessary host side setup. One such setup is ``binfmt_misc``,
435ff41da50SThomas Huthfor example, to make qemu-user powered cross build containers work.
436ff41da50SThomas Huth
437ff41da50SThomas HuthMost of the existing Dockerfiles were written by hand, simply by creating a
438ff41da50SThomas Hutha new ``.docker`` file under the ``tests/docker/dockerfiles/`` directory.
439ff41da50SThomas HuthThis has led to an inconsistent set of packages being present across the
440ff41da50SThomas Huthdifferent containers.
441ff41da50SThomas Huth
442ff41da50SThomas HuthThus going forward, QEMU is aiming to automatically generate the Dockerfiles
443ff41da50SThomas Huthusing the ``lcitool`` program provided by the ``libvirt-ci`` project:
444ff41da50SThomas Huth
445ff41da50SThomas Huth  https://gitlab.com/libvirt/libvirt-ci
446ff41da50SThomas Huth
447ff41da50SThomas Huth``libvirt-ci`` contains an ``lcitool`` program as well as a list of
448ff41da50SThomas Huthmappings to distribution package names for a wide variety of third
449ff41da50SThomas Huthparty projects.  ``lcitool`` applies the mappings to a list of build
450ff41da50SThomas Huthpre-requisites in ``tests/lcitool/projects/qemu.yml``, determines the
451ff41da50SThomas Huthlist of native packages to install on each distribution, and uses them
452ff41da50SThomas Huthto generate build environments (dockerfiles and Cirrus CI variable files)
453ff41da50SThomas Huththat are consistent across OS distribution.
454ff41da50SThomas Huth
455ff41da50SThomas Huth
456ff41da50SThomas HuthAdding new build pre-requisites
457ff41da50SThomas Huth^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
458ff41da50SThomas Huth
459ff41da50SThomas HuthWhen preparing a patch series that adds a new build
460ff41da50SThomas Huthpre-requisite to QEMU, the prerequisites should to be added to
461ff41da50SThomas Huth``tests/lcitool/projects/qemu.yml`` in order to make the dependency
462ff41da50SThomas Huthavailable in the CI build environments.
463ff41da50SThomas Huth
464ff41da50SThomas HuthIn the simple case where the pre-requisite is already known to ``libvirt-ci``
465ff41da50SThomas Huththe following steps are needed:
466ff41da50SThomas Huth
467ff41da50SThomas Huth * Edit ``tests/lcitool/projects/qemu.yml`` and add the pre-requisite
468ff41da50SThomas Huth
469ff41da50SThomas Huth * Run ``make lcitool-refresh`` to re-generate all relevant build environment
470ff41da50SThomas Huth   manifests
471ff41da50SThomas Huth
472ff41da50SThomas HuthIt may be that ``libvirt-ci`` does not know about the new pre-requisite.
473ff41da50SThomas HuthIf that is the case, some extra preparation steps will be required
474ff41da50SThomas Huthfirst to contribute the mapping to the ``libvirt-ci`` project:
475ff41da50SThomas Huth
476ff41da50SThomas Huth * Fork the ``libvirt-ci`` project on gitlab
477ff41da50SThomas Huth
478ff41da50SThomas Huth * Add an entry for the new build prerequisite to
479ff41da50SThomas Huth   ``lcitool/facts/mappings.yml``, listing its native package name on as
480ff41da50SThomas Huth   many OS distros as practical.  Run ``python -m pytest --regenerate-output``
481ff41da50SThomas Huth   and check that the changes are correct.
482ff41da50SThomas Huth
483ff41da50SThomas Huth * Commit the ``mappings.yml`` change together with the regenerated test
484ff41da50SThomas Huth   files, and submit a merge request to the ``libvirt-ci`` project.
485ff41da50SThomas Huth   Please note in the description that this is a new build pre-requisite
486ff41da50SThomas Huth   desired for use with QEMU.
487ff41da50SThomas Huth
488ff41da50SThomas Huth * CI pipeline will run to validate that the changes to ``mappings.yml``
489ff41da50SThomas Huth   are correct, by attempting to install the newly listed package on
490ff41da50SThomas Huth   all OS distributions supported by ``libvirt-ci``.
491ff41da50SThomas Huth
492ff41da50SThomas Huth * Once the merge request is accepted, go back to QEMU and update
493ff41da50SThomas Huth   the ``tests/lcitool/libvirt-ci`` submodule to point to a commit that
494ff41da50SThomas Huth   contains the ``mappings.yml`` update.  Then add the prerequisite and
495ff41da50SThomas Huth   run ``make lcitool-refresh``.
496ff41da50SThomas Huth
497ff41da50SThomas Huth * Please also trigger gitlab container generation pipelines on your change
498ff41da50SThomas Huth   for as many OS distros as practical to make sure that there are no
499ff41da50SThomas Huth   obvious breakages when adding the new pre-requisite. Please see
500ff41da50SThomas Huth   `CI <https://www.qemu.org/docs/master/devel/ci.html>`__ documentation
501ff41da50SThomas Huth   page on how to trigger gitlab CI pipelines on your change.
502ff41da50SThomas Huth
503ff41da50SThomas HuthFor enterprise distros that default to old, end-of-life versions of the
504ff41da50SThomas HuthPython runtime, QEMU uses a separate set of mappings that work with more
505ff41da50SThomas Huthrecent versions.  These can be found in ``tests/lcitool/mappings.yml``.
506ff41da50SThomas HuthModifying this file should not be necessary unless the new pre-requisite
507ff41da50SThomas Huthis a Python library or tool.
508ff41da50SThomas Huth
509ff41da50SThomas Huth
510ff41da50SThomas HuthAdding new OS distros
511ff41da50SThomas Huth^^^^^^^^^^^^^^^^^^^^^
512ff41da50SThomas Huth
513ff41da50SThomas HuthIn some cases ``libvirt-ci`` will not know about the OS distro that is
514ff41da50SThomas Huthdesired to be tested. Before adding a new OS distro, discuss the proposed
515ff41da50SThomas Huthaddition:
516ff41da50SThomas Huth
517ff41da50SThomas Huth * Send a mail to qemu-devel, copying people listed in the
518ff41da50SThomas Huth   MAINTAINERS file for ``Build and test automation``.
519ff41da50SThomas Huth
520ff41da50SThomas Huth   There are limited CI compute resources available to QEMU, so the
521ff41da50SThomas Huth   cost/benefit tradeoff of adding new OS distros needs to be considered.
522ff41da50SThomas Huth
523ff41da50SThomas Huth * File an issue at https://gitlab.com/libvirt/libvirt-ci/-/issues
524ff41da50SThomas Huth   pointing to the qemu-devel mail thread in the archives.
525ff41da50SThomas Huth
526ff41da50SThomas Huth   This alerts other people who might be interested in the work
527ff41da50SThomas Huth   to avoid duplication, as well as to get feedback from libvirt-ci
528ff41da50SThomas Huth   maintainers on any tips to ease the addition
529ff41da50SThomas Huth
530ff41da50SThomas HuthAssuming there is agreement to add a new OS distro then
531ff41da50SThomas Huth
532ff41da50SThomas Huth * Fork the ``libvirt-ci`` project on gitlab
533ff41da50SThomas Huth
534ff41da50SThomas Huth * Add metadata under ``lcitool/facts/targets/`` for the new OS
535ff41da50SThomas Huth   distro. There might be code changes required if the OS distro
536ff41da50SThomas Huth   uses a package format not currently known. The ``libvirt-ci``
537ff41da50SThomas Huth   maintainers can advise on this when the issue is filed.
538ff41da50SThomas Huth
539ff41da50SThomas Huth * Edit the ``lcitool/facts/mappings.yml`` change to add entries for
540ff41da50SThomas Huth   the new OS, listing the native package names for as many packages
541ff41da50SThomas Huth   as practical.  Run ``python -m pytest --regenerate-output`` and
542ff41da50SThomas Huth   check that the changes are correct.
543ff41da50SThomas Huth
544ff41da50SThomas Huth * Commit the changes to ``lcitool/facts`` and the regenerated test
545ff41da50SThomas Huth   files, and submit a merge request to the ``libvirt-ci`` project.
546ff41da50SThomas Huth   Please note in the description that this is a new build pre-requisite
547ff41da50SThomas Huth   desired for use with QEMU
548ff41da50SThomas Huth
549ff41da50SThomas Huth * CI pipeline will run to validate that the changes to ``mappings.yml``
550ff41da50SThomas Huth   are correct, by attempting to install the newly listed package on
551ff41da50SThomas Huth   all OS distributions supported by ``libvirt-ci``.
552ff41da50SThomas Huth
553ff41da50SThomas Huth * Once the merge request is accepted, go back to QEMU and update
554ff41da50SThomas Huth   the ``libvirt-ci`` submodule to point to a commit that contains
555ff41da50SThomas Huth   the ``mappings.yml`` update.
556ff41da50SThomas Huth
557ff41da50SThomas Huth
558ff41da50SThomas HuthTests
559ff41da50SThomas Huth~~~~~
560ff41da50SThomas Huth
561ff41da50SThomas HuthDifferent tests are added to cover various configurations to build and test
562ff41da50SThomas HuthQEMU.  Docker tests are the executables under ``tests/docker`` named
563ff41da50SThomas Huth``test-*``. They are typically shell scripts and are built on top of a shell
564ff41da50SThomas Huthlibrary, ``tests/docker/common.rc``, which provides helpers to find the QEMU
565ff41da50SThomas Huthsource and build it.
566ff41da50SThomas Huth
567ff41da50SThomas HuthThe full list of tests is printed in the ``make docker-help`` help.
568ff41da50SThomas Huth
569ff41da50SThomas HuthDebugging a Docker test failure
570ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
571ff41da50SThomas Huth
572ff41da50SThomas HuthWhen CI tasks, maintainers or yourself report a Docker test failure, follow the
573ff41da50SThomas Huthbelow steps to debug it:
574ff41da50SThomas Huth
575ff41da50SThomas Huth1. Locally reproduce the failure with the reported command line. E.g. run
576ff41da50SThomas Huth   ``make docker-test-mingw@fedora-win64-cross J=8``.
577ff41da50SThomas Huth2. Add "V=1" to the command line, try again, to see the verbose output.
578ff41da50SThomas Huth3. Further add "DEBUG=1" to the command line. This will pause in a shell prompt
579ff41da50SThomas Huth   in the container right before testing starts. You could either manually
580ff41da50SThomas Huth   build QEMU and run tests from there, or press Ctrl-D to let the Docker
581ff41da50SThomas Huth   testing continue.
582ff41da50SThomas Huth4. If you press Ctrl-D, the same building and testing procedure will begin, and
583ff41da50SThomas Huth   will hopefully run into the error again. After that, you will be dropped to
584ff41da50SThomas Huth   the prompt for debug.
585ff41da50SThomas Huth
586ff41da50SThomas HuthOptions
587ff41da50SThomas Huth~~~~~~~
588ff41da50SThomas Huth
589ff41da50SThomas HuthVarious options can be used to affect how Docker tests are done. The full
590ff41da50SThomas Huthlist is in the ``make docker`` help text. The frequently used ones are:
591ff41da50SThomas Huth
592ff41da50SThomas Huth* ``V=1``: the same as in top level ``make``. It will be propagated to the
593ff41da50SThomas Huth  container and enable verbose output.
594ff41da50SThomas Huth* ``J=$N``: the number of parallel tasks in make commands in the container,
595ff41da50SThomas Huth  similar to the ``-j $N`` option in top level ``make``. (The ``-j`` option in
596ff41da50SThomas Huth  top level ``make`` will not be propagated into the container.)
597ff41da50SThomas Huth* ``DEBUG=1``: enables debug. See the previous "Debugging a Docker test
598ff41da50SThomas Huth  failure" section.
599ff41da50SThomas Huth
600ff41da50SThomas HuthThread Sanitizer
601ff41da50SThomas Huth----------------
602ff41da50SThomas Huth
603ff41da50SThomas HuthThread Sanitizer (TSan) is a tool which can detect data races.  QEMU supports
604ff41da50SThomas Huthbuilding and testing with this tool.
605ff41da50SThomas Huth
606ff41da50SThomas HuthFor more information on TSan:
607ff41da50SThomas Huth
608ff41da50SThomas Huthhttps://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual
609ff41da50SThomas Huth
610ff41da50SThomas HuthThread Sanitizer in Docker
611ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~~~~
612ff41da50SThomas HuthTSan is currently supported in the ubuntu2204 docker.
613ff41da50SThomas Huth
614ff41da50SThomas HuthThe test-tsan test will build using TSan and then run make check.
615ff41da50SThomas Huth
616ff41da50SThomas Huth.. code::
617ff41da50SThomas Huth
618ff41da50SThomas Huth  make docker-test-tsan@ubuntu2204
619ff41da50SThomas Huth
620ff41da50SThomas HuthTSan warnings under docker are placed in files located at build/tsan/.
621ff41da50SThomas Huth
622ff41da50SThomas HuthWe recommend using DEBUG=1 to allow launching the test from inside the docker,
623ff41da50SThomas Huthand to allow review of the warnings generated by TSan.
624ff41da50SThomas Huth
625ff41da50SThomas HuthBuilding and Testing with TSan
626ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
627ff41da50SThomas Huth
628ff41da50SThomas HuthIt is possible to build and test with TSan, with a few additional steps.
629ff41da50SThomas HuthThese steps are normally done automatically in the docker.
630ff41da50SThomas Huth
631*dfbc72a7SPierrick BouvierTSan is supported for clang and gcc.
632*dfbc72a7SPierrick BouvierOne particularity of sanitizers is that all the code, including shared objects
633*dfbc72a7SPierrick Bouvierdependencies, should be built with it.
634*dfbc72a7SPierrick BouvierIn the case of TSan, any synchronization primitive from glib (GMutex for
635*dfbc72a7SPierrick Bouvierinstance) will not be recognized, and will lead to false positives.
636*dfbc72a7SPierrick Bouvier
637*dfbc72a7SPierrick BouvierTo build a tsan version of glib:
638ff41da50SThomas Huth
639ff41da50SThomas Huth.. code::
640ff41da50SThomas Huth
641*dfbc72a7SPierrick Bouvier   $ git clone --depth=1 --branch=2.81.0 https://github.com/GNOME/glib.git
642*dfbc72a7SPierrick Bouvier   $ cd glib
643*dfbc72a7SPierrick Bouvier   $ CFLAGS="-O2 -g -fsanitize=thread" meson build
644*dfbc72a7SPierrick Bouvier   $ ninja -C build
645ff41da50SThomas Huth
646ff41da50SThomas HuthTo configure the build for TSan:
647ff41da50SThomas Huth
648ff41da50SThomas Huth.. code::
649ff41da50SThomas Huth
650*dfbc72a7SPierrick Bouvier  ../configure --enable-tsan \
651ff41da50SThomas Huth               --disable-werror --extra-cflags="-O0"
652ff41da50SThomas Huth
653*dfbc72a7SPierrick BouvierWhen executing qemu, don't forget to point to tsan glib:
654*dfbc72a7SPierrick Bouvier
655*dfbc72a7SPierrick Bouvier.. code::
656*dfbc72a7SPierrick Bouvier
657*dfbc72a7SPierrick Bouvier   $ glib_dir=/path/to/glib
658*dfbc72a7SPierrick Bouvier   $ export LD_LIBRARY_PATH=$glib_dir/build/gio:$glib_dir/build/glib:$glib_dir/build/gmodule:$glib_dir/build/gobject:$glib_dir/build/gthread
659*dfbc72a7SPierrick Bouvier   # check correct version is used
660*dfbc72a7SPierrick Bouvier   $ ldd build/qemu-x86_64 | grep glib
661*dfbc72a7SPierrick Bouvier   $ qemu-system-x86_64 ...
662*dfbc72a7SPierrick Bouvier
663ff41da50SThomas HuthThe runtime behavior of TSAN is controlled by the TSAN_OPTIONS environment
664ff41da50SThomas Huthvariable.
665ff41da50SThomas Huth
666ff41da50SThomas HuthMore information on the TSAN_OPTIONS can be found here:
667ff41da50SThomas Huth
668ff41da50SThomas Huthhttps://github.com/google/sanitizers/wiki/ThreadSanitizerFlags
669ff41da50SThomas Huth
670ff41da50SThomas HuthFor example:
671ff41da50SThomas Huth
672ff41da50SThomas Huth.. code::
673ff41da50SThomas Huth
674ff41da50SThomas Huth  export TSAN_OPTIONS=suppressions=<path to qemu>/tests/tsan/suppressions.tsan \
675ff41da50SThomas Huth                      detect_deadlocks=false history_size=7 exitcode=0 \
676ff41da50SThomas Huth                      log_path=<build path>/tsan/tsan_warning
677ff41da50SThomas Huth
678ff41da50SThomas HuthThe above exitcode=0 has TSan continue without error if any warnings are found.
679ff41da50SThomas HuthThis allows for running the test and then checking the warnings afterwards.
680ff41da50SThomas HuthIf you want TSan to stop and exit with error on warnings, use exitcode=66.
681ff41da50SThomas Huth
682ff41da50SThomas HuthTSan Suppressions
683ff41da50SThomas Huth~~~~~~~~~~~~~~~~~
684ff41da50SThomas HuthKeep in mind that for any data race warning, although there might be a data race
685ff41da50SThomas Huthdetected by TSan, there might be no actual bug here.  TSan provides several
686ff41da50SThomas Huthdifferent mechanisms for suppressing warnings.  In general it is recommended
687ff41da50SThomas Huthto fix the code if possible to eliminate the data race rather than suppress
688ff41da50SThomas Huththe warning.
689ff41da50SThomas Huth
690ff41da50SThomas HuthA few important files for suppressing warnings are:
691ff41da50SThomas Huth
692ff41da50SThomas Huthtests/tsan/suppressions.tsan - Has TSan warnings we wish to suppress at runtime.
693ff41da50SThomas HuthThe comment on each suppression will typically indicate why we are
694ff41da50SThomas Huthsuppressing it.  More information on the file format can be found here:
695ff41da50SThomas Huth
696ff41da50SThomas Huthhttps://github.com/google/sanitizers/wiki/ThreadSanitizerSuppressions
697ff41da50SThomas Huth
698ff41da50SThomas Huthtests/tsan/ignore.tsan - Has TSan warnings we wish to disable
699ff41da50SThomas Huthat compile time for test or debug.
700ff41da50SThomas HuthAdd flags to configure to enable:
701ff41da50SThomas Huth
702ff41da50SThomas Huth"--extra-cflags=-fsanitize-blacklist=<src path>/tests/tsan/ignore.tsan"
703ff41da50SThomas Huth
704ff41da50SThomas HuthMore information on the file format can be found here under "Blacklist Format":
705ff41da50SThomas Huth
706ff41da50SThomas Huthhttps://github.com/google/sanitizers/wiki/ThreadSanitizerFlags
707ff41da50SThomas Huth
708ff41da50SThomas HuthTSan Annotations
709ff41da50SThomas Huth~~~~~~~~~~~~~~~~
710ff41da50SThomas Huthinclude/qemu/tsan.h defines annotations.  See this file for more descriptions
711ff41da50SThomas Huthof the annotations themselves.  Annotations can be used to suppress
712ff41da50SThomas HuthTSan warnings or give TSan more information so that it can detect proper
713ff41da50SThomas Huthrelationships between accesses of data.
714ff41da50SThomas Huth
715ff41da50SThomas HuthAnnotation examples can be found here:
716ff41da50SThomas Huth
717ff41da50SThomas Huthhttps://github.com/llvm/llvm-project/tree/master/compiler-rt/test/tsan/
718ff41da50SThomas Huth
719ff41da50SThomas HuthGood files to start with are: annotate_happens_before.cpp and ignore_race.cpp
720ff41da50SThomas Huth
721ff41da50SThomas HuthThe full set of annotations can be found here:
722ff41da50SThomas Huth
723ff41da50SThomas Huthhttps://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/tsan/rtl/tsan_interface_ann.cpp
724ff41da50SThomas Huth
725ff41da50SThomas Huthdocker-binfmt-image-debian-% targets
726ff41da50SThomas Huth------------------------------------
727ff41da50SThomas Huth
728ff41da50SThomas HuthIt is possible to combine Debian's bootstrap scripts with a configured
729ff41da50SThomas Huth``binfmt_misc`` to bootstrap a number of Debian's distros including
730ff41da50SThomas Huthexperimental ports not yet supported by a released OS. This can
731ff41da50SThomas Huthsimplify setting up a rootfs by using docker to contain the foreign
732ff41da50SThomas Huthrootfs rather than manually invoking chroot.
733ff41da50SThomas Huth
734ff41da50SThomas HuthSetting up ``binfmt_misc``
735ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~~~~
736ff41da50SThomas Huth
737ff41da50SThomas HuthYou can use the script ``qemu-binfmt-conf.sh`` to configure a QEMU
738ff41da50SThomas Huthuser binary to automatically run binaries for the foreign
739ff41da50SThomas Hutharchitecture. While the scripts will try their best to work with
740ff41da50SThomas Huthdynamically linked QEMU's a statically linked one will present less
741ff41da50SThomas Huthpotential complications when copying into the docker image. Modern
742ff41da50SThomas Huthkernels support the ``F`` (fix binary) flag which will open the QEMU
743ff41da50SThomas Huthexecutable on setup and avoids the need to find and re-open in the
744ff41da50SThomas Huthchroot environment. This is triggered with the ``--persistent`` flag.
745ff41da50SThomas Huth
746ff41da50SThomas HuthExample invocation
747ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~
748ff41da50SThomas Huth
749ff41da50SThomas HuthFor example to setup the HPPA ports builds of Debian::
750ff41da50SThomas Huth
751ff41da50SThomas Huth  make docker-binfmt-image-debian-sid-hppa \
752ff41da50SThomas Huth    DEB_TYPE=sid DEB_ARCH=hppa \
753ff41da50SThomas Huth    DEB_URL=http://ftp.ports.debian.org/debian-ports/ \
754ff41da50SThomas Huth    DEB_KEYRING=/usr/share/keyrings/debian-ports-archive-keyring.gpg \
755ff41da50SThomas Huth    EXECUTABLE=(pwd)/qemu-hppa V=1
756ff41da50SThomas Huth
757ff41da50SThomas HuthThe ``DEB_`` variables are substitutions used by
758ff41da50SThomas Huth``debian-bootstrap.pre`` which is called to do the initial debootstrap
759ff41da50SThomas Huthof the rootfs before it is copied into the container. The second stage
760ff41da50SThomas Huthis run as part of the build. The final image will be tagged as
761ff41da50SThomas Huth``qemu/debian-sid-hppa``.
762ff41da50SThomas Huth
763ff41da50SThomas HuthVM testing
764ff41da50SThomas Huth----------
765ff41da50SThomas Huth
766ff41da50SThomas HuthThis test suite contains scripts that bootstrap various guest images that have
767ff41da50SThomas Huthnecessary packages to build QEMU. The basic usage is documented in ``Makefile``
768ff41da50SThomas Huthhelp which is displayed with ``make vm-help``.
769ff41da50SThomas Huth
770ff41da50SThomas HuthQuickstart
771ff41da50SThomas Huth~~~~~~~~~~
772ff41da50SThomas Huth
773ff41da50SThomas HuthRun ``make vm-help`` to list available make targets. Invoke a specific make
774ff41da50SThomas Huthcommand to run build test in an image. For example, ``make vm-build-freebsd``
775ff41da50SThomas Huthwill build the source tree in the FreeBSD image. The command can be executed
776ff41da50SThomas Huthfrom either the source tree or the build dir; if the former, ``./configure`` is
777ff41da50SThomas Huthnot needed. The command will then generate the test image in ``./tests/vm/``
778ff41da50SThomas Huthunder the working directory.
779ff41da50SThomas Huth
780ff41da50SThomas HuthNote: images created by the scripts accept a well-known RSA key pair for SSH
781ff41da50SThomas Huthaccess, so they SHOULD NOT be exposed to external interfaces if you are
782ff41da50SThomas Huthconcerned about attackers taking control of the guest and potentially
783ff41da50SThomas Huthexploiting a QEMU security bug to compromise the host.
784ff41da50SThomas Huth
785ff41da50SThomas HuthQEMU binaries
786ff41da50SThomas Huth~~~~~~~~~~~~~
787ff41da50SThomas Huth
788ff41da50SThomas HuthBy default, ``qemu-system-x86_64`` is searched in $PATH to run the guest. If
789ff41da50SThomas Huththere isn't one, or if it is older than 2.10, the test won't work. In this case,
790ff41da50SThomas Huthprovide the QEMU binary in env var: ``QEMU=/path/to/qemu-2.10+``.
791ff41da50SThomas Huth
792ff41da50SThomas HuthLikewise the path to ``qemu-img`` can be set in QEMU_IMG environment variable.
793ff41da50SThomas Huth
794ff41da50SThomas HuthMake jobs
795ff41da50SThomas Huth~~~~~~~~~
796ff41da50SThomas Huth
797ff41da50SThomas HuthThe ``-j$X`` option in the make command line is not propagated into the VM,
798ff41da50SThomas Huthspecify ``J=$X`` to control the make jobs in the guest.
799ff41da50SThomas Huth
800ff41da50SThomas HuthDebugging
801ff41da50SThomas Huth~~~~~~~~~
802ff41da50SThomas Huth
803ff41da50SThomas HuthAdd ``DEBUG=1`` and/or ``V=1`` to the make command to allow interactive
804ff41da50SThomas Huthdebugging and verbose output. If this is not enough, see the next section.
805ff41da50SThomas Huth``V=1`` will be propagated down into the make jobs in the guest.
806ff41da50SThomas Huth
807ff41da50SThomas HuthManual invocation
808ff41da50SThomas Huth~~~~~~~~~~~~~~~~~
809ff41da50SThomas Huth
810ff41da50SThomas HuthEach guest script is an executable script with the same command line options.
811ff41da50SThomas HuthFor example to work with the netbsd guest, use ``$QEMU_SRC/tests/vm/netbsd``:
812ff41da50SThomas Huth
813ff41da50SThomas Huth.. code::
814ff41da50SThomas Huth
815ff41da50SThomas Huth    $ cd $QEMU_SRC/tests/vm
816ff41da50SThomas Huth
817ff41da50SThomas Huth    # To bootstrap the image
818ff41da50SThomas Huth    $ ./netbsd --build-image --image /var/tmp/netbsd.img
819ff41da50SThomas Huth    <...>
820ff41da50SThomas Huth
821ff41da50SThomas Huth    # To run an arbitrary command in guest (the output will not be echoed unless
822ff41da50SThomas Huth    # --debug is added)
823ff41da50SThomas Huth    $ ./netbsd --debug --image /var/tmp/netbsd.img uname -a
824ff41da50SThomas Huth
825ff41da50SThomas Huth    # To build QEMU in guest
826ff41da50SThomas Huth    $ ./netbsd --debug --image /var/tmp/netbsd.img --build-qemu $QEMU_SRC
827ff41da50SThomas Huth
828ff41da50SThomas Huth    # To get to an interactive shell
829ff41da50SThomas Huth    $ ./netbsd --interactive --image /var/tmp/netbsd.img sh
830ff41da50SThomas Huth
831ff41da50SThomas HuthAdding new guests
832ff41da50SThomas Huth~~~~~~~~~~~~~~~~~
833ff41da50SThomas Huth
834ff41da50SThomas HuthPlease look at existing guest scripts for how to add new guests.
835ff41da50SThomas Huth
836ff41da50SThomas HuthMost importantly, create a subclass of BaseVM and implement ``build_image()``
837ff41da50SThomas Huthmethod and define ``BUILD_SCRIPT``, then finally call ``basevm.main()`` from
838ff41da50SThomas Huththe script's ``main()``.
839ff41da50SThomas Huth
840ff41da50SThomas Huth* Usually in ``build_image()``, a template image is downloaded from a
841ff41da50SThomas Huth  predefined URL. ``BaseVM._download_with_cache()`` takes care of the cache and
842ff41da50SThomas Huth  the checksum, so consider using it.
843ff41da50SThomas Huth
844ff41da50SThomas Huth* Once the image is downloaded, users, SSH server and QEMU build deps should
845ff41da50SThomas Huth  be set up:
846ff41da50SThomas Huth
847ff41da50SThomas Huth  - Root password set to ``BaseVM.ROOT_PASS``
848ff41da50SThomas Huth  - User ``BaseVM.GUEST_USER`` is created, and password set to
849ff41da50SThomas Huth    ``BaseVM.GUEST_PASS``
850ff41da50SThomas Huth  - SSH service is enabled and started on boot,
851ff41da50SThomas Huth    ``$QEMU_SRC/tests/keys/id_rsa.pub`` is added to ssh's ``authorized_keys``
852ff41da50SThomas Huth    file of both root and the normal user
853ff41da50SThomas Huth  - DHCP client service is enabled and started on boot, so that it can
854ff41da50SThomas Huth    automatically configure the virtio-net-pci NIC and communicate with QEMU
855ff41da50SThomas Huth    user net (10.0.2.2)
856ff41da50SThomas Huth  - Necessary packages are installed to untar the source tarball and build
857ff41da50SThomas Huth    QEMU
858ff41da50SThomas Huth
859ff41da50SThomas Huth* Write a proper ``BUILD_SCRIPT`` template, which should be a shell script that
860ff41da50SThomas Huth  untars a raw virtio-blk block device, which is the tarball data blob of the
861ff41da50SThomas Huth  QEMU source tree, then configure/build it. Running "make check" is also
862ff41da50SThomas Huth  recommended.
863ff41da50SThomas Huth
864ff41da50SThomas HuthImage fuzzer testing
865ff41da50SThomas Huth--------------------
866ff41da50SThomas Huth
867ff41da50SThomas HuthAn image fuzzer was added to exercise format drivers. Currently only qcow2 is
868ff41da50SThomas Huthsupported. To start the fuzzer, run
869ff41da50SThomas Huth
870ff41da50SThomas Huth.. code::
871ff41da50SThomas Huth
872ff41da50SThomas Huth  tests/image-fuzzer/runner.py -c '[["qemu-img", "info", "$test_img"]]' /tmp/test qcow2
873ff41da50SThomas Huth
874ff41da50SThomas HuthAlternatively, some command different from ``qemu-img info`` can be tested, by
875ff41da50SThomas Huthchanging the ``-c`` option.
876ff41da50SThomas Huth
877c3e24cffSThomas HuthFunctional tests using Python
878c3e24cffSThomas Huth-----------------------------
879c3e24cffSThomas Huth
880c3e24cffSThomas HuthThe ``tests/functional`` directory hosts functional tests written in
881c3e24cffSThomas HuthPython. You can run the functional tests simply by executing:
882c3e24cffSThomas Huth
883c3e24cffSThomas Huth.. code::
884c3e24cffSThomas Huth
885c3e24cffSThomas Huth  make check-functional
886c3e24cffSThomas Huth
887c3e24cffSThomas HuthSee :ref:`checkfunctional-ref` for more details.
888c3e24cffSThomas Huth
889ff41da50SThomas HuthIntegration tests using the Avocado Framework
890ff41da50SThomas Huth---------------------------------------------
891ff41da50SThomas Huth
892ff41da50SThomas HuthThe ``tests/avocado`` directory hosts integration tests. They're usually
893ff41da50SThomas Huthhigher level tests, and may interact with external resources and with
894ff41da50SThomas Huthvarious guest operating systems.
895ff41da50SThomas Huth
896ff41da50SThomas HuthYou can run the avocado tests simply by executing:
897ff41da50SThomas Huth
898ff41da50SThomas Huth.. code::
899ff41da50SThomas Huth
900ff41da50SThomas Huth  make check-avocado
901ff41da50SThomas Huth
9022133c2abSThomas HuthSee :ref:`checkavocado-ref` for more details.
903ff41da50SThomas Huth
904ff41da50SThomas Huth
905ff41da50SThomas Huth.. _checktcg-ref:
906ff41da50SThomas Huth
907ff41da50SThomas HuthTesting with "make check-tcg"
908ff41da50SThomas Huth-----------------------------
909ff41da50SThomas Huth
910ff41da50SThomas HuthThe check-tcg tests are intended for simple smoke tests of both
911ff41da50SThomas Huthlinux-user and softmmu TCG functionality. However to build test
912ff41da50SThomas Huthprograms for guest targets you need to have cross compilers available.
913ff41da50SThomas HuthIf your distribution supports cross compilers you can do something as
914ff41da50SThomas Huthsimple as::
915ff41da50SThomas Huth
916ff41da50SThomas Huth  apt install gcc-aarch64-linux-gnu
917ff41da50SThomas Huth
918ff41da50SThomas HuthThe configure script will automatically pick up their presence.
919ff41da50SThomas HuthSometimes compilers have slightly odd names so the availability of
920ff41da50SThomas Huththem can be prompted by passing in the appropriate configure option
921ff41da50SThomas Huthfor the architecture in question, for example::
922ff41da50SThomas Huth
923ff41da50SThomas Huth  $(configure) --cross-cc-aarch64=aarch64-cc
924ff41da50SThomas Huth
925ff41da50SThomas HuthThere is also a ``--cross-cc-cflags-ARCH`` flag in case additional
926ff41da50SThomas Huthcompiler flags are needed to build for a given target.
927ff41da50SThomas Huth
928ff41da50SThomas HuthIf you have the ability to run containers as the user the build system
929ff41da50SThomas Huthwill automatically use them where no system compiler is available. For
930ff41da50SThomas Hutharchitectures where we also support building QEMU we will generally
931ff41da50SThomas Huthuse the same container to build tests. However there are a number of
932ff41da50SThomas Huthadditional containers defined that have a minimal cross-build
933ff41da50SThomas Huthenvironment that is only suitable for building test cases. Sometimes
934ff41da50SThomas Huthwe may use a bleeding edge distribution for compiler features needed
935ff41da50SThomas Huthfor test cases that aren't yet in the LTS distros we support for QEMU
936ff41da50SThomas Huthitself.
937ff41da50SThomas Huth
938ff41da50SThomas HuthSee :ref:`container-ref` for more details.
939ff41da50SThomas Huth
940ff41da50SThomas HuthRunning subset of tests
941ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~
942ff41da50SThomas Huth
943ff41da50SThomas HuthYou can build the tests for one architecture::
944ff41da50SThomas Huth
945ff41da50SThomas Huth  make build-tcg-tests-$TARGET
946ff41da50SThomas Huth
947ff41da50SThomas HuthAnd run with::
948ff41da50SThomas Huth
949ff41da50SThomas Huth  make run-tcg-tests-$TARGET
950ff41da50SThomas Huth
951ff41da50SThomas HuthAdding ``V=1`` to the invocation will show the details of how to
952ff41da50SThomas Huthinvoke QEMU for the test which is useful for debugging tests.
953ff41da50SThomas Huth
954ff41da50SThomas HuthRunning individual tests
955ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~~~~
956ff41da50SThomas Huth
957ff41da50SThomas HuthTests can also be run directly from the test build directory. If you
958ff41da50SThomas Huthrun ``make help`` from the test build directory you will get a list of
959ff41da50SThomas Huthall the tests that can be run. Please note that same binaries are used
960ff41da50SThomas Huthin multiple tests, for example::
961ff41da50SThomas Huth
962ff41da50SThomas Huth  make run-plugin-test-mmap-with-libinline.so
963ff41da50SThomas Huth
964ff41da50SThomas Huthwill run the mmap test with the ``libinline.so`` TCG plugin. The
965ff41da50SThomas Huthgdbstub tests also re-use the test binaries but while exercising gdb.
966ff41da50SThomas Huth
967ff41da50SThomas HuthTCG test dependencies
968ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~~~~
969ff41da50SThomas Huth
970ff41da50SThomas HuthThe TCG tests are deliberately very light on dependencies and are
971ff41da50SThomas Hutheither totally bare with minimal gcc lib support (for system-mode tests)
972ff41da50SThomas Huthor just glibc (for linux-user tests). This is because getting a cross
973ff41da50SThomas Huthcompiler to work with additional libraries can be challenging.
974ff41da50SThomas Huth
975ff41da50SThomas HuthOther TCG Tests
976ff41da50SThomas Huth---------------
977ff41da50SThomas Huth
978ff41da50SThomas HuthThere are a number of out-of-tree test suites that are used for more
979ff41da50SThomas Huthextensive testing of processor features.
980ff41da50SThomas Huth
981ff41da50SThomas HuthKVM Unit Tests
982ff41da50SThomas Huth~~~~~~~~~~~~~~
983ff41da50SThomas Huth
984ff41da50SThomas HuthThe KVM unit tests are designed to run as a Guest OS under KVM but
985ff41da50SThomas Huththere is no reason why they can't exercise the TCG as well. It
986ff41da50SThomas Huthprovides a minimal OS kernel with hooks for enabling the MMU as well
987ff41da50SThomas Huthas reporting test results via a special device::
988ff41da50SThomas Huth
989ff41da50SThomas Huth  https://git.kernel.org/pub/scm/virt/kvm/kvm-unit-tests.git
990ff41da50SThomas Huth
991ff41da50SThomas HuthLinux Test Project
992ff41da50SThomas Huth~~~~~~~~~~~~~~~~~~
993ff41da50SThomas Huth
994ff41da50SThomas HuthThe LTP is focused on exercising the syscall interface of a Linux
995ff41da50SThomas Huthkernel. It checks that syscalls behave as documented and strives to
996ff41da50SThomas Huthexercise as many corner cases as possible. It is a useful test suite
997ff41da50SThomas Huthto run to exercise QEMU's linux-user code::
998ff41da50SThomas Huth
999ff41da50SThomas Huth  https://linux-test-project.github.io/
1000ff41da50SThomas Huth
1001ff41da50SThomas HuthGCC gcov support
1002ff41da50SThomas Huth----------------
1003ff41da50SThomas Huth
1004ff41da50SThomas Huth``gcov`` is a GCC tool to analyze the testing coverage by
1005ff41da50SThomas Huthinstrumenting the tested code. To use it, configure QEMU with
1006ff41da50SThomas Huth``--enable-gcov`` option and build. Then run the tests as usual.
1007ff41da50SThomas Huth
1008ff41da50SThomas HuthIf you want to gather coverage information on a single test the ``make
1009ff41da50SThomas Huthclean-gcda`` target can be used to delete any existing coverage
1010ff41da50SThomas Huthinformation before running a single test.
1011ff41da50SThomas Huth
1012ff41da50SThomas HuthYou can generate a HTML coverage report by executing ``make
1013ff41da50SThomas Huthcoverage-html`` which will create
1014ff41da50SThomas Huth``meson-logs/coveragereport/index.html``.
1015ff41da50SThomas Huth
1016ff41da50SThomas HuthFurther analysis can be conducted by running the ``gcov`` command
1017ff41da50SThomas Huthdirectly on the various .gcda output files. Please read the ``gcov``
1018ff41da50SThomas Huthdocumentation for more information.
1019