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