Blame view

Documentation/dev-tools/kselftest.rst 4.27 KB
cef04cdcb   Mickaël Salaün   Documentation/dev...
1
  ======================
80d034285   Jeremy Kerr   selftests: add a ...
2
  Linux Kernel Selftests
cef04cdcb   Mickaël Salaün   Documentation/dev...
3
  ======================
80d034285   Jeremy Kerr   selftests: add a ...
4
5
  
  The kernel contains a set of "self tests" under the tools/testing/selftests/
3409f9ab7   Aleksander Alekseev   kselftest: kselft...
6
7
8
  directory. These are intended to be small tests to exercise individual code
  paths in the kernel. Tests are intended to be run after building, installing
  and booting a kernel.
80d034285   Jeremy Kerr   selftests: add a ...
9

ddddda9bc   Shuah Khan   tools: selftests ...
10
11
12
13
14
15
16
17
18
  On some systems, hot-plug tests could hang forever waiting for cpu and
  memory to be ready to be offlined. A special hot-plug target is created
  to run full range of hot-plug tests. In default mode, hot-plug tests run
  in safe mode with a limited scope. In limited mode, cpu-hotplug test is
  run on a single cpu as opposed to all hotplug capable cpus, and memory
  hotplug test is run on 2% of hotplug capable memory instead of 10%.
  
  Running the selftests (hotplug tests are run in limited mode)
  =============================================================
80d034285   Jeremy Kerr   selftests: add a ...
19

cef04cdcb   Mickaël Salaün   Documentation/dev...
20
  To build the tests::
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
21
    $ make -C tools/testing/selftests
cef04cdcb   Mickaël Salaün   Documentation/dev...
22
23
  
  To run the tests::
80d034285   Jeremy Kerr   selftests: add a ...
24

fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
25
    $ make -C tools/testing/selftests run_tests
80d034285   Jeremy Kerr   selftests: add a ...
26

cef04cdcb   Mickaël Salaün   Documentation/dev...
27
  To build and run the tests with a single command, use::
80d034285   Jeremy Kerr   selftests: add a ...
28

fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
29
    $ make kselftest
3c415707b   Tim Bird   kselftest: Move t...
30

cef04cdcb   Mickaël Salaün   Documentation/dev...
31
  Note that some tests will require root privileges.
80d034285   Jeremy Kerr   selftests: add a ...
32

80d034285   Jeremy Kerr   selftests: add a ...
33

3c415707b   Tim Bird   kselftest: Move t...
34
  Running a subset of selftests
cef04cdcb   Mickaël Salaün   Documentation/dev...
35
  =============================
3c415707b   Tim Bird   kselftest: Move t...
36
37
  You can use the "TARGETS" variable on the make command line to specify
  single test to run, or a list of tests to run.
cef04cdcb   Mickaël Salaün   Documentation/dev...
38
  To run only tests targeted for a single subsystem::
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
39
    $ make -C tools/testing/selftests TARGETS=ptrace run_tests
3c415707b   Tim Bird   kselftest: Move t...
40

cef04cdcb   Mickaël Salaün   Documentation/dev...
41
  You can specify multiple tests to build and run::
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
42
    $  make TARGETS="size timers" kselftest
3c415707b   Tim Bird   kselftest: Move t...
43
44
45
  
  See the top-level tools/testing/selftests/Makefile for the list of all
  possible targets.
80d034285   Jeremy Kerr   selftests: add a ...
46

80d034285   Jeremy Kerr   selftests: add a ...
47

ddddda9bc   Shuah Khan   tools: selftests ...
48
49
  Running the full range hotplug selftests
  ========================================
cef04cdcb   Mickaël Salaün   Documentation/dev...
50
  To build the hotplug tests::
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
51
    $ make -C tools/testing/selftests hotplug
cef04cdcb   Mickaël Salaün   Documentation/dev...
52
53
  
  To run the hotplug tests::
ddddda9bc   Shuah Khan   tools: selftests ...
54

fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
55
    $ make -C tools/testing/selftests run_hotplug
ddddda9bc   Shuah Khan   tools: selftests ...
56

cef04cdcb   Mickaël Salaün   Documentation/dev...
57
  Note that some tests will require root privileges.
80d034285   Jeremy Kerr   selftests: add a ...
58

3c415707b   Tim Bird   kselftest: Move t...
59

76caa6ca9   Wang Long   Documentation: Up...
60
61
62
63
  Install selftests
  =================
  
  You can use kselftest_install.sh tool installs selftests in default
9332ef9db   Masahiro Yamada   scripts/spelling....
64
  location which is tools/testing/selftests/kselftest or a user specified
76caa6ca9   Wang Long   Documentation: Up...
65
  location.
cef04cdcb   Mickaël Salaün   Documentation/dev...
66
  To install selftests in default location::
76caa6ca9   Wang Long   Documentation: Up...
67

fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
68
69
     $ cd tools/testing/selftests
     $ ./kselftest_install.sh
cef04cdcb   Mickaël Salaün   Documentation/dev...
70
71
  
  To install selftests in a user specified location::
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
72
73
     $ cd tools/testing/selftests
     $ ./kselftest_install.sh install_dir
76caa6ca9   Wang Long   Documentation: Up...
74

78566cf13   Shuah Khan   Doc: update kself...
75
76
77
78
79
80
81
  Running installed selftests
  ===========================
  
  Kselftest install as well as the Kselftest tarball provide a script
  named "run_kselftest.sh" to run the tests.
  
  You can simply do the following to run the installed Kselftests. Please
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
82
  note some tests will require root privileges::
78566cf13   Shuah Khan   Doc: update kself...
83

fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
84
85
     $ cd kselftest
     $ ./run_kselftest.sh
76caa6ca9   Wang Long   Documentation: Up...
86

80d034285   Jeremy Kerr   selftests: add a ...
87
88
  Contributing new tests
  ======================
4db1fa664   Zhiyi Sun   Documentation: ks...
89
  In general, the rules for selftests are
80d034285   Jeremy Kerr   selftests: add a ...
90
91
92
93
94
95
96
97
98
  
   * Do as much as you can if you're not root;
  
   * Don't take too long;
  
   * Don't break the build on any architecture, and
  
   * Don't cause the top-level "make run_tests" to fail if your feature is
     unconfigured.
88baa78d1   bamvor.zhangjian@huawei.com   selftests: remove...
99

cef04cdcb   Mickaël Salaün   Documentation/dev...
100
101
  Contributing new tests (details)
  ================================
88baa78d1   bamvor.zhangjian@huawei.com   selftests: remove...
102
103
104
  
   * Use TEST_GEN_XXX if such binaries or files are generated during
     compiling.
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
105

88baa78d1   bamvor.zhangjian@huawei.com   selftests: remove...
106
107
     TEST_PROGS, TEST_GEN_PROGS mean it is the excutable tested by
     default.
fde662d9c   Mauro Carvalho Chehab   kselftest.rst: do...
108

88baa78d1   bamvor.zhangjian@huawei.com   selftests: remove...
109
110
111
112
     TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
     executable which is not tested by default.
     TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
     test.
7e6a32abd   Mickaël Salaün   Documentation/dev...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  
  Test Harness
  ============
  
  The kselftest_harness.h file contains useful helpers to build tests.  The tests
  from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as example.
  
  Example
  -------
  
  .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
      :doc: example
  
  
  Helpers
  -------
  
  .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
      :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
                  FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN
  
  Operators
  ---------
  
  .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
      :doc: operators
  
  .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
      :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
                  ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
                  ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
                  EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
                  EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE