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