xref: /linux/Documentation/dev-tools/kselftest.rst (revision 8ea8814fcdcb32cc667d090455649893a362c658)
1======================
2Linux Kernel Selftests
3======================
4
5The kernel contains a set of "self tests" under the tools/testing/selftests/
6directory. These are intended to be small tests to exercise individual code
7paths in the kernel. Tests are intended to be run after building, installing
8and booting a kernel.
9
10You can find additional information on Kselftest framework, how to
11write new tests using the framework on Kselftest wiki:
12
13https://kselftest.wiki.kernel.org/
14
15On some systems, hot-plug tests could hang forever waiting for cpu and
16memory to be ready to be offlined. A special hot-plug target is created
17to run the full range of hot-plug tests. In default mode, hot-plug tests run
18in safe mode with a limited scope. In limited mode, cpu-hotplug test is
19run on a single cpu as opposed to all hotplug capable cpus, and memory
20hotplug test is run on 2% of hotplug capable memory instead of 10%.
21
22Running the selftests (hotplug tests are run in limited mode)
23=============================================================
24
25To build the tests::
26
27  $ make -C tools/testing/selftests
28
29To run the tests::
30
31  $ make -C tools/testing/selftests run_tests
32
33To build and run the tests with a single command, use::
34
35  $ make kselftest
36
37Note that some tests will require root privileges.
38
39Kselftest supports saving output files in a separate directory and then
40running tests. To locate output files in a separate directory two syntaxes
41are supported. In both cases the working directory must be the root of the
42kernel src. This is applicable to "Running a subset of selftests" section
43below.
44
45To build, save output files in a separate directory with O= ::
46
47  $ make O=/tmp/kselftest kselftest
48
49To build, save output files in a separate directory with KBUILD_OUTPUT ::
50
51  $ export KBUILD_OUTPUT=/tmp/kselftest; make kselftest
52
53The O= assignment takes precedence over the KBUILD_OUTPUT environment
54variable.
55
56The above commands by default run the tests and print full pass/fail report.
57Kselftest supports "summary" option to make it easier to understand the test
58results. Please find the detailed individual test results for each test in
59/tmp/testname file(s) when summary option is specified. This is applicable
60to "Running a subset of selftests" section below.
61
62To run kselftest with summary option enabled ::
63
64  $ make summary=1 kselftest
65
66Running a subset of selftests
67=============================
68
69You can use the "TARGETS" variable on the make command line to specify
70single test to run, or a list of tests to run.
71
72To run only tests targeted for a single subsystem::
73
74  $ make -C tools/testing/selftests TARGETS=ptrace run_tests
75
76You can specify multiple tests to build and run::
77
78  $  make TARGETS="size timers" kselftest
79
80To build, save output files in a separate directory with O= ::
81
82  $ make O=/tmp/kselftest TARGETS="size timers" kselftest
83
84To build, save output files in a separate directory with KBUILD_OUTPUT ::
85
86  $ export KBUILD_OUTPUT=/tmp/kselftest; make TARGETS="size timers" kselftest
87
88See the top-level tools/testing/selftests/Makefile for the list of all
89possible targets.
90
91Running the full range hotplug selftests
92========================================
93
94To build the hotplug tests::
95
96  $ make -C tools/testing/selftests hotplug
97
98To run the hotplug tests::
99
100  $ make -C tools/testing/selftests run_hotplug
101
102Note that some tests will require root privileges.
103
104
105Install selftests
106=================
107
108You can use the kselftest_install.sh tool to install selftests in the
109default location, which is tools/testing/selftests/kselftest, or in a
110user specified location.
111
112To install selftests in default location::
113
114   $ cd tools/testing/selftests
115   $ ./kselftest_install.sh
116
117To install selftests in a user specified location::
118
119   $ cd tools/testing/selftests
120   $ ./kselftest_install.sh install_dir
121
122Running installed selftests
123===========================
124
125Kselftest install as well as the Kselftest tarball provide a script
126named "run_kselftest.sh" to run the tests.
127
128You can simply do the following to run the installed Kselftests. Please
129note some tests will require root privileges::
130
131   $ cd kselftest
132   $ ./run_kselftest.sh
133
134Contributing new tests
135======================
136
137In general, the rules for selftests are
138
139 * Do as much as you can if you're not root;
140
141 * Don't take too long;
142
143 * Don't break the build on any architecture, and
144
145 * Don't cause the top-level "make run_tests" to fail if your feature is
146   unconfigured.
147
148Contributing new tests (details)
149================================
150
151 * Use TEST_GEN_XXX if such binaries or files are generated during
152   compiling.
153
154   TEST_PROGS, TEST_GEN_PROGS mean it is the executable tested by
155   default.
156
157   TEST_CUSTOM_PROGS should be used by tests that require custom build
158   rules and prevent common build rule use.
159
160   TEST_PROGS are for test shell scripts. Please ensure shell script has
161   its exec bit set. Otherwise, lib.mk run_tests will generate a warning.
162
163   TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests.
164
165   TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
166   executable which is not tested by default.
167   TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
168   test.
169
170 * First use the headers inside the kernel source and/or git repo, and then the
171   system headers.  Headers for the kernel release as opposed to headers
172   installed by the distro on the system should be the primary focus to be able
173   to find regressions.
174
175 * If a test needs specific kernel config options enabled, add a config file in
176   the test directory to enable them.
177
178   e.g: tools/testing/selftests/android/config
179
180Test Harness
181============
182
183The kselftest_harness.h file contains useful helpers to build tests.  The tests
184from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as example.
185
186Example
187-------
188
189.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
190    :doc: example
191
192
193Helpers
194-------
195
196.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
197    :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
198                FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN
199
200Operators
201---------
202
203.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
204    :doc: operators
205
206.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
207    :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
208                ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
209                ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
210                EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
211                EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE
212