1# Welcome to kvm-unit-tests 2 3See http://www.linux-kvm.org/page/KVM-unit-tests for a high-level 4description of this project, as well as running tests and adding 5tests HOWTOs. 6 7# Building the tests 8 9This directory contains sources for a KVM test suite. 10 11To create the test images do: 12 13 ./configure 14 make 15 16in this directory. Test images are created in ./ARCH/\*.flat 17 18NOTE: GCC cross-compiler is required for [build on macOS](README.macOS.md). 19 20## Standalone tests 21 22The tests can be built as standalone. To create and use standalone tests do: 23 24 ./configure 25 make standalone 26 (send tests/some-test somewhere) 27 (go to somewhere) 28 ./some-test 29 30`make install` will install all tests in PREFIX/share/kvm-unit-tests/tests, 31each as a standalone test. 32 33 34# Running the tests 35 36Then use the runner script to detect the correct invocation and 37invoke the test: 38 39 ./x86-run ./x86/msr.flat 40or: 41 42 ./run_tests.sh 43 44to run them all. 45 46By default the runner script searches for a suitable QEMU binary in the system. 47To select a specific QEMU binary though, specify the QEMU=path/to/binary 48environment variable: 49 50 QEMU=/tmp/qemu/x86_64-softmmu/qemu-system-x86_64 ./x86-run ./x86/msr.flat 51 52To select an accelerator, for example "kvm", "hvf" or "tcg", specify the 53ACCEL=name environment variable: 54 55 ACCEL=kvm ./x86-run ./x86/msr.flat 56 57For running tests that involve migration from one QEMU instance to another 58you also need to have the "ncat" binary (from the nmap.org project) installed, 59otherwise the related tests will be skipped. 60 61## Running the tests with UEFI 62 63Check [x86/efi/README.md](./x86/efi/README.md). 64 65# Tests configuration file 66 67The test case may need specific runtime configurations, for 68example, extra QEMU parameters and time to execute limited, the 69runner script reads those information from a configuration file found 70at ./ARCH/unittests.cfg. 71 72The configuration file also contain the groups (if any) each test belong 73to. So that a given group can be executed by specifying its name in the 74runner's -g option. 75 76# Unit test inputs 77 78Unit tests use QEMU's '-append args...' parameter for command line 79inputs, i.e. all args will be available as argv strings in main(). 80Additionally a file of the form 81 82 KEY=VAL 83 KEY2=VAL 84 ... 85 86may be passed with '-initrd file' to become the unit test's environ, 87which can then be accessed in the usual ways, e.g. VAL = getenv("KEY"). 88Any key=val strings can be passed, but some have reserved meanings in 89the framework. The list of reserved environment variables is below 90 91 QEMU_ACCEL either kvm, hvf or tcg 92 QEMU_VERSION_STRING string of the form `qemu -h | head -1` 93 KERNEL_VERSION_STRING string of the form `uname -r` 94 95Additionally these self-explanatory variables are reserved 96 97 QEMU_MAJOR, QEMU_MINOR, QEMU_MICRO, KERNEL_VERSION, KERNEL_PATCHLEVEL, 98 KERNEL_SUBLEVEL, KERNEL_EXTRAVERSION 99 100# Guarding unsafe tests 101 102Some tests are not safe to run by default, as they may crash the 103host. kvm-unit-tests provides two ways to handle tests like those. 104 105 1) Adding 'nodefault' to the groups field for the unit test in the 106 unittests.cfg file. When a unit test is in the nodefault group 107 it is only run when invoked 108 109 a) independently, `ARCH-run ARCH/test` 110 111 b) by specifying any other non-nodefault group it is in, 112 groups = nodefault mygroup : `./run_tests.sh -g mygroup` 113 114 c) by specifying all tests should be run, `./run_tests.sh -a` 115 116 2) Making the test conditional on errata in the code, 117 ``` 118 if (ERRATA(abcdef012345)) { 119 do_unsafe_test(); 120 } 121 ``` 122 123 With the errata condition the unsafe unit test is only run 124 when 125 126 a) the ERRATA_abcdef012345 environment variable is provided and 'y' 127 128 b) the ERRATA_FORCE environment variable is provided and 'y' 129 130 c) by specifying all tests should be run, `./run_tests.sh -a` 131 (The -a switch ensures the ERRATA_FORCE is provided and set 132 to 'y'.) 133 134The ./errata.txt file provides a mapping of the commits needed by errata 135conditionals to their respective minimum kernel versions. By default, 136when the user does not provide an environ, then an environ generated 137from the ./errata.txt file and the host's kernel version is provided to 138all unit tests. 139 140# Contributing 141 142## Directory structure 143 144 .: configure script, top-level Makefile, and run_tests.sh 145 ./scripts: general architecture neutral helper scripts for building and running tests 146 ./scripts/<ARCH>: architecture dependent helper scripts for building and running tests 147 ./lib: general architecture neutral services for the tests 148 ./lib/<ARCH>: architecture dependent services for the tests 149 ./<ARCH>: the sources of the tests and the created objects/images 150 151See ./ARCH/README for architecture specific documentation. 152 153## Style 154 155Currently there is a mix of indentation styles so any changes to 156existing files should be consistent with the existing style. For new 157files: 158 159 - C: please use standard linux-with-tabs, see Linux kernel 160 doc Documentation/process/coding-style.rst 161 - Shell: use TABs for indentation 162 163Exceptions: 164 165 - While the kernel standard requires 80 columns, we allow up to 120. 166 167Header guards: 168 169Please try to adhere to the following patterns when adding 170"#ifndef <...> #define <...>" header guards: 171 ./lib: _HEADER_H_ 172 ./lib/<ARCH>: _ARCH_HEADER_H_ 173 ./lib/<ARCH>/asm: _ASMARCH_HEADER_H_ 174 ./<ARCH>: ARCH_HEADER_H 175 176## Patches 177 178Patches are welcome at the KVM mailing list <kvm@vger.kernel.org>. 179 180Please prefix messages with: [kvm-unit-tests PATCH] 181 182You can add the following to .git/config to do this automatically for you: 183 184 [format] 185 subjectprefix = kvm-unit-tests PATCH 186 187Additionally it's helpful to have a common order of file types in patches. 188Our chosen order attempts to place the more declarative files before 189the code files. We also start with common code and finish with unit test 190code. git-diff's orderFile feature allows us to specify the order in a 191file. The orderFile we use is `scripts/git.difforder`; adding the config 192with `git config diff.orderFile scripts/git.difforder` enables it. 193 194We strive to follow the Linux kernels coding style so it's recommended 195to run the kernel's ./scripts/checkpatch.pl on new patches. 196