running_tips.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ============================
  3. Tips For Running KUnit Tests
  4. ============================
  5. Using ``kunit.py run`` ("kunit tool")
  6. =====================================
  7. Running from any directory
  8. --------------------------
  9. It can be handy to create a bash function like:
  10. .. code-block:: bash
  11. function run_kunit() {
  12. ( cd "$(git rev-parse --show-toplevel)" && ./tools/testing/kunit/kunit.py run "$@" )
  13. }
  14. .. note::
  15. Early versions of ``kunit.py`` (before 5.6) didn't work unless run from
  16. the kernel root, hence the use of a subshell and ``cd``.
  17. Running a subset of tests
  18. -------------------------
  19. ``kunit.py run`` accepts an optional glob argument to filter tests. The format
  20. is ``"<suite_glob>[.test_glob]"``.
  21. Say that we wanted to run the sysctl tests, we could do so via:
  22. .. code-block:: bash
  23. $ echo -e 'CONFIG_KUNIT=y\nCONFIG_KUNIT_ALL_TESTS=y' > .kunit/.kunitconfig
  24. $ ./tools/testing/kunit/kunit.py run 'sysctl*'
  25. We can filter down to just the "write" tests via:
  26. .. code-block:: bash
  27. $ echo -e 'CONFIG_KUNIT=y\nCONFIG_KUNIT_ALL_TESTS=y' > .kunit/.kunitconfig
  28. $ ./tools/testing/kunit/kunit.py run 'sysctl*.*write*'
  29. We're paying the cost of building more tests than we need this way, but it's
  30. easier than fiddling with ``.kunitconfig`` files or commenting out
  31. ``kunit_suite``'s.
  32. However, if we wanted to define a set of tests in a less ad hoc way, the next
  33. tip is useful.
  34. Defining a set of tests
  35. -----------------------
  36. ``kunit.py run`` (along with ``build``, and ``config``) supports a
  37. ``--kunitconfig`` flag. So if you have a set of tests that you want to run on a
  38. regular basis (especially if they have other dependencies), you can create a
  39. specific ``.kunitconfig`` for them.
  40. E.g. kunit has one for its tests:
  41. .. code-block:: bash
  42. $ ./tools/testing/kunit/kunit.py run --kunitconfig=lib/kunit/.kunitconfig
  43. Alternatively, if you're following the convention of naming your
  44. file ``.kunitconfig``, you can just pass in the dir, e.g.
  45. .. code-block:: bash
  46. $ ./tools/testing/kunit/kunit.py run --kunitconfig=lib/kunit
  47. .. note::
  48. This is a relatively new feature (5.12+) so we don't have any
  49. conventions yet about on what files should be checked in versus just
  50. kept around locally. It's up to you and your maintainer to decide if a
  51. config is useful enough to submit (and therefore have to maintain).
  52. .. note::
  53. Having ``.kunitconfig`` fragments in a parent and child directory is
  54. iffy. There's discussion about adding an "import" statement in these
  55. files to make it possible to have a top-level config run tests from all
  56. child directories. But that would mean ``.kunitconfig`` files are no
  57. longer just simple .config fragments.
  58. One alternative would be to have kunit tool recursively combine configs
  59. automagically, but tests could theoretically depend on incompatible
  60. options, so handling that would be tricky.
  61. Setting kernel commandline parameters
  62. -------------------------------------
  63. You can use ``--kernel_args`` to pass arbitrary kernel arguments, e.g.
  64. .. code-block:: bash
  65. $ ./tools/testing/kunit/kunit.py run --kernel_args=param=42 --kernel_args=param2=false
  66. Generating code coverage reports under UML
  67. ------------------------------------------
  68. .. note::
  69. TODO(brendanhiggins@google.com): There are various issues with UML and
  70. versions of gcc 7 and up. You're likely to run into missing ``.gcda``
  71. files or compile errors.
  72. This is different from the "normal" way of getting coverage information that is
  73. documented in Documentation/dev-tools/gcov.rst.
  74. Instead of enabling ``CONFIG_GCOV_KERNEL=y``, we can set these options:
  75. .. code-block:: none
  76. CONFIG_DEBUG_KERNEL=y
  77. CONFIG_DEBUG_INFO=y
  78. CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
  79. CONFIG_GCOV=y
  80. Putting it together into a copy-pastable sequence of commands:
  81. .. code-block:: bash
  82. # Append coverage options to the current config
  83. $ ./tools/testing/kunit/kunit.py run --kunitconfig=.kunit/ --kunitconfig=tools/testing/kunit/configs/coverage_uml.config
  84. # Extract the coverage information from the build dir (.kunit/)
  85. $ lcov -t "my_kunit_tests" -o coverage.info -c -d .kunit/
  86. # From here on, it's the same process as with CONFIG_GCOV_KERNEL=y
  87. # E.g. can generate an HTML report in a tmp dir like so:
  88. $ genhtml -o /tmp/coverage_html coverage.info
  89. If your installed version of gcc doesn't work, you can tweak the steps:
  90. .. code-block:: bash
  91. $ ./tools/testing/kunit/kunit.py run --make_options=CC=/usr/bin/gcc-6
  92. $ lcov -t "my_kunit_tests" -o coverage.info -c -d .kunit/ --gcov-tool=/usr/bin/gcov-6
  93. Alternatively, LLVM-based toolchains can also be used:
  94. .. code-block:: bash
  95. # Build with LLVM and append coverage options to the current config
  96. $ ./tools/testing/kunit/kunit.py run --make_options LLVM=1 --kunitconfig=.kunit/ --kunitconfig=tools/testing/kunit/configs/coverage_uml.config
  97. $ llvm-profdata merge -sparse default.profraw -o default.profdata
  98. $ llvm-cov export --format=lcov .kunit/vmlinux -instr-profile default.profdata > coverage.info
  99. # The coverage.info file is in lcov-compatible format and it can be used to e.g. generate HTML report
  100. $ genhtml -o /tmp/coverage_html coverage.info
  101. Running tests manually
  102. ======================
  103. Running tests without using ``kunit.py run`` is also an important use case.
  104. Currently it's your only option if you want to test on architectures other than
  105. UML.
  106. As running the tests under UML is fairly straightforward (configure and compile
  107. the kernel, run the ``./linux`` binary), this section will focus on testing
  108. non-UML architectures.
  109. Running built-in tests
  110. ----------------------
  111. When setting tests to ``=y``, the tests will run as part of boot and print
  112. results to dmesg in TAP format. So you just need to add your tests to your
  113. ``.config``, build and boot your kernel as normal.
  114. So if we compiled our kernel with:
  115. .. code-block:: none
  116. CONFIG_KUNIT=y
  117. CONFIG_KUNIT_EXAMPLE_TEST=y
  118. Then we'd see output like this in dmesg signaling the test ran and passed:
  119. .. code-block:: none
  120. TAP version 14
  121. 1..1
  122. # Subtest: example
  123. 1..1
  124. # example_simple_test: initializing
  125. ok 1 - example_simple_test
  126. ok 1 - example
  127. Running tests as modules
  128. ------------------------
  129. Depending on the tests, you can build them as loadable modules.
  130. For example, we'd change the config options from before to
  131. .. code-block:: none
  132. CONFIG_KUNIT=y
  133. CONFIG_KUNIT_EXAMPLE_TEST=m
  134. Then after booting into our kernel, we can run the test via
  135. .. code-block:: none
  136. $ modprobe kunit-example-test
  137. This will then cause it to print TAP output to stdout.
  138. .. note::
  139. The ``modprobe`` will *not* have a non-zero exit code if any test
  140. failed (as of 5.13). But ``kunit.py parse`` would, see below.
  141. .. note::
  142. You can set ``CONFIG_KUNIT=m`` as well, however, some features will not
  143. work and thus some tests might break. Ideally tests would specify they
  144. depend on ``KUNIT=y`` in their ``Kconfig``'s, but this is an edge case
  145. most test authors won't think about.
  146. As of 5.13, the only difference is that ``current->kunit_test`` will
  147. not exist.
  148. Pretty-printing results
  149. -----------------------
  150. You can use ``kunit.py parse`` to parse dmesg for test output and print out
  151. results in the same familiar format that ``kunit.py run`` does.
  152. .. code-block:: bash
  153. $ ./tools/testing/kunit/kunit.py parse /var/log/dmesg
  154. Retrieving per suite results
  155. ----------------------------
  156. Regardless of how you're running your tests, you can enable
  157. ``CONFIG_KUNIT_DEBUGFS`` to expose per-suite TAP-formatted results:
  158. .. code-block:: none
  159. CONFIG_KUNIT=y
  160. CONFIG_KUNIT_EXAMPLE_TEST=m
  161. CONFIG_KUNIT_DEBUGFS=y
  162. The results for each suite will be exposed under
  163. ``/sys/kernel/debug/kunit/<suite>/results``.
  164. So using our example config:
  165. .. code-block:: bash
  166. $ modprobe kunit-example-test > /dev/null
  167. $ cat /sys/kernel/debug/kunit/example/results
  168. ... <TAP output> ...
  169. # After removing the module, the corresponding files will go away
  170. $ modprobe -r kunit-example-test
  171. $ cat /sys/kernel/debug/kunit/example/results
  172. /sys/kernel/debug/kunit/example/results: No such file or directory
  173. Generating code coverage reports
  174. --------------------------------
  175. See Documentation/dev-tools/gcov.rst for details on how to do this.
  176. The only vaguely KUnit-specific advice here is that you probably want to build
  177. your tests as modules. That way you can isolate the coverage from tests from
  178. other code executed during boot, e.g.
  179. .. code-block:: bash
  180. # Reset coverage counters before running the test.
  181. $ echo 0 > /sys/kernel/debug/gcov/reset
  182. $ modprobe kunit-example-test
  183. Test Attributes and Filtering
  184. =============================
  185. Test suites and cases can be marked with test attributes, such as speed of
  186. test. These attributes will later be printed in test output and can be used to
  187. filter test execution.
  188. Marking Test Attributes
  189. -----------------------
  190. Tests are marked with an attribute by including a ``kunit_attributes`` object
  191. in the test definition.
  192. Test cases can be marked using the ``KUNIT_CASE_ATTR(test_name, attributes)``
  193. macro to define the test case instead of ``KUNIT_CASE(test_name)``.
  194. .. code-block:: c
  195. static const struct kunit_attributes example_attr = {
  196. .speed = KUNIT_VERY_SLOW,
  197. };
  198. static struct kunit_case example_test_cases[] = {
  199. KUNIT_CASE_ATTR(example_test, example_attr),
  200. };
  201. .. note::
  202. To mark a test case as slow, you can also use ``KUNIT_CASE_SLOW(test_name)``.
  203. This is a helpful macro as the slow attribute is the most commonly used.
  204. Test suites can be marked with an attribute by setting the "attr" field in the
  205. suite definition.
  206. .. code-block:: c
  207. static const struct kunit_attributes example_attr = {
  208. .speed = KUNIT_VERY_SLOW,
  209. };
  210. static struct kunit_suite example_test_suite = {
  211. ...,
  212. .attr = example_attr,
  213. };
  214. .. note::
  215. Not all attributes need to be set in a ``kunit_attributes`` object. Unset
  216. attributes will remain uninitialized and act as though the attribute is set
  217. to 0 or NULL. Thus, if an attribute is set to 0, it is treated as unset.
  218. These unset attributes will not be reported and may act as a default value
  219. for filtering purposes.
  220. Reporting Attributes
  221. --------------------
  222. When a user runs tests, attributes will be present in the raw kernel output (in
  223. KTAP format). Note that attributes will be hidden by default in kunit.py output
  224. for all passing tests but the raw kernel output can be accessed using the
  225. ``--raw_output`` flag. This is an example of how test attributes for test cases
  226. will be formatted in kernel output:
  227. .. code-block:: none
  228. # example_test.speed: slow
  229. ok 1 example_test
  230. This is an example of how test attributes for test suites will be formatted in
  231. kernel output:
  232. .. code-block:: none
  233. KTAP version 2
  234. # Subtest: example_suite
  235. # module: kunit_example_test
  236. 1..3
  237. ...
  238. ok 1 example_suite
  239. Additionally, users can output a full attribute report of tests with their
  240. attributes, using the command line flag ``--list_tests_attr``:
  241. .. code-block:: bash
  242. kunit.py run "example" --list_tests_attr
  243. .. note::
  244. This report can be accessed when running KUnit manually by passing in the
  245. module_param ``kunit.action=list_attr``.
  246. Filtering
  247. ---------
  248. Users can filter tests using the ``--filter`` command line flag when running
  249. tests. As an example:
  250. .. code-block:: bash
  251. kunit.py run --filter speed=slow
  252. You can also use the following operations on filters: "<", ">", "<=", ">=",
  253. "!=", and "=". Example:
  254. .. code-block:: bash
  255. kunit.py run --filter "speed>slow"
  256. This example will run all tests with speeds faster than slow. Note that the
  257. characters < and > are often interpreted by the shell, so they may need to be
  258. quoted or escaped, as above.
  259. Additionally, you can use multiple filters at once. Simply separate filters
  260. using commas. Example:
  261. .. code-block:: bash
  262. kunit.py run --filter "speed>slow, module=kunit_example_test"
  263. .. note::
  264. You can use this filtering feature when running KUnit manually by passing
  265. the filter as a module param: ``kunit.filter="speed>slow, speed<=normal"``.
  266. Filtered tests will not run or show up in the test output. You can use the
  267. ``--filter_action=skip`` flag to skip filtered tests instead. These tests will be
  268. shown in the test output in the test but will not run. To use this feature when
  269. running KUnit manually, use the module param ``kunit.filter_action=skip``.
  270. Rules of Filtering Procedure
  271. ----------------------------
  272. Since both suites and test cases can have attributes, there may be conflicts
  273. between attributes during filtering. The process of filtering follows these
  274. rules:
  275. - Filtering always operates at a per-test level.
  276. - If a test has an attribute set, then the test's value is filtered on.
  277. - Otherwise, the value falls back to the suite's value.
  278. - If neither are set, the attribute has a global "default" value, which is used.
  279. List of Current Attributes
  280. --------------------------
  281. ``speed``
  282. This attribute indicates the speed of a test's execution (how slow or fast the
  283. test is).
  284. This attribute is saved as an enum with the following categories: "normal",
  285. "slow", or "very_slow". The assumed default speed for tests is "normal". This
  286. indicates that the test takes a relatively trivial amount of time (less than
  287. 1 second), regardless of the machine it is running on. Any test slower than
  288. this could be marked as "slow" or "very_slow".
  289. The macro ``KUNIT_CASE_SLOW(test_name)`` can be easily used to set the speed
  290. of a test case to "slow".
  291. ``module``
  292. This attribute indicates the name of the module associated with the test.
  293. This attribute is automatically saved as a string and is printed for each suite.
  294. Tests can also be filtered using this attribute.
  295. ``is_init``
  296. This attribute indicates whether the test uses init data or functions.
  297. This attribute is automatically saved as a boolean and tests can also be
  298. filtered using this attribute.