Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 1 | ====================== |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 2 | Linux Kernel Selftests |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 3 | ====================== |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 4 | |
| 5 | The kernel contains a set of "self tests" under the tools/testing/selftests/ |
Aleksander Alekseev | 3409f9a | 2016-08-18 12:36:53 +0300 | [diff] [blame] | 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. |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 9 | |
Shuah Khan | ddddda9 | 2014-07-02 09:51:38 -0600 | [diff] [blame] | 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 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 | Running the selftests (hotplug tests are run in limited mode) |
| 18 | ============================================================= |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 19 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 20 | To build the tests:: |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 21 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 22 | make -C tools/testing/selftests |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 23 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 24 | To run the tests:: |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 25 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 26 | make -C tools/testing/selftests run_tests |
Tim Bird | 3c41570 | 2014-11-19 16:16:16 -0800 | [diff] [blame] | 27 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 28 | To build and run the tests with a single command, use:: |
| 29 | |
| 30 | make kselftest |
| 31 | |
| 32 | Note that some tests will require root privileges. |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 33 | |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 34 | |
Tim Bird | 3c41570 | 2014-11-19 16:16:16 -0800 | [diff] [blame] | 35 | Running a subset of selftests |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 36 | ============================= |
| 37 | |
Tim Bird | 3c41570 | 2014-11-19 16:16:16 -0800 | [diff] [blame] | 38 | You can use the "TARGETS" variable on the make command line to specify |
| 39 | single test to run, or a list of tests to run. |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 40 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 41 | To run only tests targeted for a single subsystem:: |
Tim Bird | 3c41570 | 2014-11-19 16:16:16 -0800 | [diff] [blame] | 42 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 43 | make -C tools/testing/selftests TARGETS=ptrace run_tests |
| 44 | |
| 45 | You can specify multiple tests to build and run:: |
| 46 | |
| 47 | make TARGETS="size timers" kselftest |
Tim Bird | 3c41570 | 2014-11-19 16:16:16 -0800 | [diff] [blame] | 48 | |
| 49 | See the top-level tools/testing/selftests/Makefile for the list of all |
| 50 | possible targets. |
| 51 | |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 52 | |
Shuah Khan | ddddda9 | 2014-07-02 09:51:38 -0600 | [diff] [blame] | 53 | Running the full range hotplug selftests |
| 54 | ======================================== |
| 55 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 56 | To build the hotplug tests:: |
Shuah Khan | ddddda9 | 2014-07-02 09:51:38 -0600 | [diff] [blame] | 57 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 58 | make -C tools/testing/selftests hotplug |
Shuah Khan | ddddda9 | 2014-07-02 09:51:38 -0600 | [diff] [blame] | 59 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 60 | To run the hotplug tests:: |
| 61 | |
| 62 | make -C tools/testing/selftests run_hotplug |
| 63 | |
| 64 | Note that some tests will require root privileges. |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 65 | |
Tim Bird | 3c41570 | 2014-11-19 16:16:16 -0800 | [diff] [blame] | 66 | |
Wang Long | 76caa6c | 2015-09-23 09:35:25 +0000 | [diff] [blame] | 67 | Install selftests |
| 68 | ================= |
| 69 | |
| 70 | You can use kselftest_install.sh tool installs selftests in default |
Masahiro Yamada | 9332ef9 | 2017-02-27 14:28:47 -0800 | [diff] [blame] | 71 | location which is tools/testing/selftests/kselftest or a user specified |
Wang Long | 76caa6c | 2015-09-23 09:35:25 +0000 | [diff] [blame] | 72 | location. |
| 73 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 74 | To install selftests in default location:: |
Wang Long | 76caa6c | 2015-09-23 09:35:25 +0000 | [diff] [blame] | 75 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 76 | cd tools/testing/selftests |
| 77 | ./kselftest_install.sh |
| 78 | |
| 79 | To install selftests in a user specified location:: |
| 80 | |
| 81 | cd tools/testing/selftests |
| 82 | ./kselftest_install.sh install_dir |
Wang Long | 76caa6c | 2015-09-23 09:35:25 +0000 | [diff] [blame] | 83 | |
Shuah Khan | 78566cf | 2016-11-07 13:24:14 -0700 | [diff] [blame] | 84 | Running installed selftests |
| 85 | =========================== |
| 86 | |
| 87 | Kselftest install as well as the Kselftest tarball provide a script |
| 88 | named "run_kselftest.sh" to run the tests. |
| 89 | |
| 90 | You can simply do the following to run the installed Kselftests. Please |
| 91 | note some tests will require root privileges. |
| 92 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 93 | :: |
| 94 | |
| 95 | cd kselftest |
| 96 | ./run_kselftest.sh |
Wang Long | 76caa6c | 2015-09-23 09:35:25 +0000 | [diff] [blame] | 97 | |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 98 | Contributing new tests |
| 99 | ====================== |
| 100 | |
Zhiyi Sun | 4db1fa6 | 2016-02-24 00:08:11 +0800 | [diff] [blame] | 101 | In general, the rules for selftests are |
Jeremy Kerr | 80d0342 | 2013-02-27 17:05:57 -0800 | [diff] [blame] | 102 | |
| 103 | * Do as much as you can if you're not root; |
| 104 | |
| 105 | * Don't take too long; |
| 106 | |
| 107 | * Don't break the build on any architecture, and |
| 108 | |
| 109 | * Don't cause the top-level "make run_tests" to fail if your feature is |
| 110 | unconfigured. |
bamvor.zhangjian@huawei.com | 88baa78 | 2016-11-29 19:55:47 +0800 | [diff] [blame] | 111 | |
Mickaël Salaün | cef04cd | 2017-05-26 20:44:00 +0200 | [diff] [blame] | 112 | Contributing new tests (details) |
| 113 | ================================ |
bamvor.zhangjian@huawei.com | 88baa78 | 2016-11-29 19:55:47 +0800 | [diff] [blame] | 114 | |
| 115 | * Use TEST_GEN_XXX if such binaries or files are generated during |
| 116 | compiling. |
| 117 | TEST_PROGS, TEST_GEN_PROGS mean it is the excutable tested by |
| 118 | default. |
| 119 | TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the |
| 120 | executable which is not tested by default. |
| 121 | TEST_FILES, TEST_GEN_FILES mean it is the file which is used by |
| 122 | test. |
Mickaël Salaün | 7e6a32a | 2017-06-05 20:37:17 +0200 | [diff] [blame^] | 123 | |
| 124 | Test Harness |
| 125 | ============ |
| 126 | |
| 127 | The kselftest_harness.h file contains useful helpers to build tests. The tests |
| 128 | from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as example. |
| 129 | |
| 130 | Example |
| 131 | ------- |
| 132 | |
| 133 | .. kernel-doc:: tools/testing/selftests/kselftest_harness.h |
| 134 | :doc: example |
| 135 | |
| 136 | |
| 137 | Helpers |
| 138 | ------- |
| 139 | |
| 140 | .. kernel-doc:: tools/testing/selftests/kselftest_harness.h |
| 141 | :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP |
| 142 | FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN |
| 143 | |
| 144 | Operators |
| 145 | --------- |
| 146 | |
| 147 | .. kernel-doc:: tools/testing/selftests/kselftest_harness.h |
| 148 | :doc: operators |
| 149 | |
| 150 | .. kernel-doc:: tools/testing/selftests/kselftest_harness.h |
| 151 | :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE |
| 152 | ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE |
| 153 | ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT |
| 154 | EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE |
| 155 | EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE |