kselftest.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ======================
  2. Linux Kernel Selftests
  3. ======================
  4. The kernel contains a set of "self tests" under the tools/testing/selftests/
  5. directory. These are intended to be small tests to exercise individual code
  6. paths in the kernel. Tests are intended to be run after building, installing
  7. and booting a kernel.
  8. On some systems, hot-plug tests could hang forever waiting for cpu and
  9. memory to be ready to be offlined. A special hot-plug target is created
  10. to run full range of hot-plug tests. In default mode, hot-plug tests run
  11. in safe mode with a limited scope. In limited mode, cpu-hotplug test is
  12. run on a single cpu as opposed to all hotplug capable cpus, and memory
  13. hotplug test is run on 2% of hotplug capable memory instead of 10%.
  14. Running the selftests (hotplug tests are run in limited mode)
  15. =============================================================
  16. To build the tests::
  17. $ make -C tools/testing/selftests
  18. To run the tests::
  19. $ make -C tools/testing/selftests run_tests
  20. To build and run the tests with a single command, use::
  21. $ make kselftest
  22. Note that some tests will require root privileges.
  23. Build and run from user specific object directory (make O=dir)::
  24. $ make O=/tmp/kselftest kselftest
  25. Build and run KBUILD_OUTPUT directory (make KBUILD_OUTPUT=)::
  26. $ make KBUILD_OUTPUT=/tmp/kselftest kselftest
  27. The above commands run the tests and print pass/fail summary to make it
  28. easier to understand the test results. Please find the detailed individual
  29. test results for each test in /tmp/testname file(s).
  30. Running a subset of selftests
  31. =============================
  32. You can use the "TARGETS" variable on the make command line to specify
  33. single test to run, or a list of tests to run.
  34. To run only tests targeted for a single subsystem::
  35. $ make -C tools/testing/selftests TARGETS=ptrace run_tests
  36. You can specify multiple tests to build and run::
  37. $ make TARGETS="size timers" kselftest
  38. Build and run from user specific object directory (make O=dir)::
  39. $ make O=/tmp/kselftest TARGETS="size timers" kselftest
  40. Build and run KBUILD_OUTPUT directory (make KBUILD_OUTPUT=)::
  41. $ make KBUILD_OUTPUT=/tmp/kselftest TARGETS="size timers" kselftest
  42. The above commands run the tests and print pass/fail summary to make it
  43. easier to understand the test results. Please find the detailed individual
  44. test results for each test in /tmp/testname file(s).
  45. See the top-level tools/testing/selftests/Makefile for the list of all
  46. possible targets.
  47. Running the full range hotplug selftests
  48. ========================================
  49. To build the hotplug tests::
  50. $ make -C tools/testing/selftests hotplug
  51. To run the hotplug tests::
  52. $ make -C tools/testing/selftests run_hotplug
  53. Note that some tests will require root privileges.
  54. Install selftests
  55. =================
  56. You can use kselftest_install.sh tool installs selftests in default
  57. location which is tools/testing/selftests/kselftest or a user specified
  58. location.
  59. To install selftests in default location::
  60. $ cd tools/testing/selftests
  61. $ ./kselftest_install.sh
  62. To install selftests in a user specified location::
  63. $ cd tools/testing/selftests
  64. $ ./kselftest_install.sh install_dir
  65. Running installed selftests
  66. ===========================
  67. Kselftest install as well as the Kselftest tarball provide a script
  68. named "run_kselftest.sh" to run the tests.
  69. You can simply do the following to run the installed Kselftests. Please
  70. note some tests will require root privileges::
  71. $ cd kselftest
  72. $ ./run_kselftest.sh
  73. Contributing new tests
  74. ======================
  75. In general, the rules for selftests are
  76. * Do as much as you can if you're not root;
  77. * Don't take too long;
  78. * Don't break the build on any architecture, and
  79. * Don't cause the top-level "make run_tests" to fail if your feature is
  80. unconfigured.
  81. Contributing new tests (details)
  82. ================================
  83. * Use TEST_GEN_XXX if such binaries or files are generated during
  84. compiling.
  85. TEST_PROGS, TEST_GEN_PROGS mean it is the executable tested by
  86. default.
  87. TEST_CUSTOM_PROGS should be used by tests that require custom build
  88. rule and prevent common build rule use.
  89. TEST_PROGS are for test shell scripts. Please ensure shell script has
  90. its exec bit set. Otherwise, lib.mk run_tests will generate a warning.
  91. TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests.
  92. TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
  93. executable which is not tested by default.
  94. TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
  95. test.
  96. * First use the headers inside the kernel source and/or git repo, and then the
  97. system headers. Headers for the kernel release as opposed to headers
  98. installed by the distro on the system should be the primary focus to be able
  99. to find regressions.
  100. * If a test needs specific kernel config options enabled, add a config file in
  101. the test directory to enable them.
  102. e.g: tools/testing/selftests/android/ion/config
  103. Test Harness
  104. ============
  105. The kselftest_harness.h file contains useful helpers to build tests. The tests
  106. from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as example.
  107. Example
  108. -------
  109. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  110. :doc: example
  111. Helpers
  112. -------
  113. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  114. :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
  115. FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN
  116. Operators
  117. ---------
  118. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  119. :doc: operators
  120. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  121. :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
  122. ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
  123. ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
  124. EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
  125. EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE