Merge tag 'regulator-v5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[sfrench/cifs-2.6.git] / Documentation / dev-tools / kselftest.rst
1 ======================
2 Linux Kernel Selftests
3 ======================
4
5 The kernel contains a set of "self tests" under the tools/testing/selftests/
6 directory. These are intended to be small tests to exercise individual code
7 paths in the kernel. Tests are intended to be run after building, installing
8 and booting a kernel.
9
10 On some systems, hot-plug tests could hang forever waiting for cpu and
11 memory to be ready to be offlined. A special hot-plug target is created
12 to run the full range of hot-plug tests. In default mode, hot-plug tests run
13 in safe mode with a limited scope. In limited mode, cpu-hotplug test is
14 run on a single cpu as opposed to all hotplug capable cpus, and memory
15 hotplug test is run on 2% of hotplug capable memory instead of 10%.
16
17 kselftest runs as a userspace process.  Tests that can be written/run in
18 userspace may wish to use the `Test Harness`_.  Tests that need to be
19 run in kernel space may wish to use a `Test Module`_.
20
21 Running the selftests (hotplug tests are run in limited mode)
22 =============================================================
23
24 To build the tests::
25
26   $ make -C tools/testing/selftests
27
28 To run the tests::
29
30   $ make -C tools/testing/selftests run_tests
31
32 To build and run the tests with a single command, use::
33
34   $ make kselftest
35
36 Note that some tests will require root privileges.
37
38 Build and run from user specific object directory (make O=dir)::
39
40   $ make O=/tmp/kselftest kselftest
41
42 Build and run KBUILD_OUTPUT directory (make KBUILD_OUTPUT=)::
43
44   $ make KBUILD_OUTPUT=/tmp/kselftest kselftest
45
46 The above commands run the tests and print pass/fail summary to make it
47 easier to understand the test results. Please find the detailed individual
48 test results for each test in /tmp/testname file(s).
49
50 Running a subset of selftests
51 =============================
52
53 You can use the "TARGETS" variable on the make command line to specify
54 single test to run, or a list of tests to run.
55
56 To run only tests targeted for a single subsystem::
57
58   $ make -C tools/testing/selftests TARGETS=ptrace run_tests
59
60 You can specify multiple tests to build and run::
61
62   $  make TARGETS="size timers" kselftest
63
64 Build and run from user specific object directory (make O=dir)::
65
66   $ make O=/tmp/kselftest TARGETS="size timers" kselftest
67
68 Build and run KBUILD_OUTPUT directory (make KBUILD_OUTPUT=)::
69
70   $ make KBUILD_OUTPUT=/tmp/kselftest TARGETS="size timers" kselftest
71
72 The above commands run the tests and print pass/fail summary to make it
73 easier to understand the test results. Please find the detailed individual
74 test results for each test in /tmp/testname file(s).
75
76 See the top-level tools/testing/selftests/Makefile for the list of all
77 possible targets.
78
79 Running the full range hotplug selftests
80 ========================================
81
82 To build the hotplug tests::
83
84   $ make -C tools/testing/selftests hotplug
85
86 To run the hotplug tests::
87
88   $ make -C tools/testing/selftests run_hotplug
89
90 Note that some tests will require root privileges.
91
92
93 Install selftests
94 =================
95
96 You can use the kselftest_install.sh tool to install selftests in the
97 default location, which is tools/testing/selftests/kselftest, or in a
98 user specified location.
99
100 To install selftests in default location::
101
102    $ cd tools/testing/selftests
103    $ ./kselftest_install.sh
104
105 To install selftests in a user specified location::
106
107    $ cd tools/testing/selftests
108    $ ./kselftest_install.sh install_dir
109
110 Running installed selftests
111 ===========================
112
113 Kselftest install as well as the Kselftest tarball provide a script
114 named "run_kselftest.sh" to run the tests.
115
116 You can simply do the following to run the installed Kselftests. Please
117 note some tests will require root privileges::
118
119    $ cd kselftest
120    $ ./run_kselftest.sh
121
122 Contributing new tests
123 ======================
124
125 In general, the rules for selftests are
126
127  * Do as much as you can if you're not root;
128
129  * Don't take too long;
130
131  * Don't break the build on any architecture, and
132
133  * Don't cause the top-level "make run_tests" to fail if your feature is
134    unconfigured.
135
136 Contributing new tests (details)
137 ================================
138
139  * Use TEST_GEN_XXX if such binaries or files are generated during
140    compiling.
141
142    TEST_PROGS, TEST_GEN_PROGS mean it is the executable tested by
143    default.
144
145    TEST_CUSTOM_PROGS should be used by tests that require custom build
146    rules and prevent common build rule use.
147
148    TEST_PROGS are for test shell scripts. Please ensure shell script has
149    its exec bit set. Otherwise, lib.mk run_tests will generate a warning.
150
151    TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests.
152
153    TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
154    executable which is not tested by default.
155    TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
156    test.
157
158  * First use the headers inside the kernel source and/or git repo, and then the
159    system headers.  Headers for the kernel release as opposed to headers
160    installed by the distro on the system should be the primary focus to be able
161    to find regressions.
162
163  * If a test needs specific kernel config options enabled, add a config file in
164    the test directory to enable them.
165
166    e.g: tools/testing/selftests/android/config
167
168 Test Module
169 ===========
170
171 Kselftest tests the kernel from userspace.  Sometimes things need
172 testing from within the kernel, one method of doing this is to create a
173 test module.  We can tie the module into the kselftest framework by
174 using a shell script test runner.  ``kselftest_module.sh`` is designed
175 to facilitate this process.  There is also a header file provided to
176 assist writing kernel modules that are for use with kselftest:
177
178 - ``tools/testing/kselftest/kselftest_module.h``
179 - ``tools/testing/kselftest/kselftest_module.sh``
180
181 How to use
182 ----------
183
184 Here we show the typical steps to create a test module and tie it into
185 kselftest.  We use kselftests for lib/ as an example.
186
187 1. Create the test module
188
189 2. Create the test script that will run (load/unload) the module
190    e.g. ``tools/testing/selftests/lib/printf.sh``
191
192 3. Add line to config file e.g. ``tools/testing/selftests/lib/config``
193
194 4. Add test script to makefile  e.g. ``tools/testing/selftests/lib/Makefile``
195
196 5. Verify it works:
197
198 .. code-block:: sh
199
200    # Assumes you have booted a fresh build of this kernel tree
201    cd /path/to/linux/tree
202    make kselftest-merge
203    make modules
204    sudo make modules_install
205    make TARGETS=lib kselftest
206
207 Example Module
208 --------------
209
210 A bare bones test module might look like this:
211
212 .. code-block:: c
213
214    // SPDX-License-Identifier: GPL-2.0+
215
216    #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
217
218    #include "../tools/testing/selftests/kselftest_module.h"
219
220    KSTM_MODULE_GLOBALS();
221
222    /*
223     * Kernel module for testing the foobinator
224     */
225
226    static int __init test_function()
227    {
228            ...
229    }
230
231    static void __init selftest(void)
232    {
233            KSTM_CHECK_ZERO(do_test_case("", 0));
234    }
235
236    KSTM_MODULE_LOADERS(test_foo);
237    MODULE_AUTHOR("John Developer <jd@fooman.org>");
238    MODULE_LICENSE("GPL");
239
240 Example test script
241 -------------------
242
243 .. code-block:: sh
244
245     #!/bin/bash
246     # SPDX-License-Identifier: GPL-2.0+
247     $(dirname $0)/../kselftest_module.sh "foo" test_foo
248
249
250 Test Harness
251 ============
252
253 The kselftest_harness.h file contains useful helpers to build tests.  The
254 test harness is for userspace testing, for kernel space testing see `Test
255 Module`_ above.
256
257 The tests from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as
258 example.
259
260 Example
261 -------
262
263 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
264     :doc: example
265
266
267 Helpers
268 -------
269
270 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
271     :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
272                 FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN
273
274 Operators
275 ---------
276
277 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
278     :doc: operators
279
280 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
281     :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
282                 ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
283                 ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
284                 EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
285                 EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE