testing.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Testing
  3. =======
  4. This document contains useful information how to test the Rust code in the
  5. kernel.
  6. There are three sorts of tests:
  7. - The KUnit tests.
  8. - The ``#[test]`` tests.
  9. - The Kselftests.
  10. The KUnit tests
  11. ---------------
  12. These are the tests that come from the examples in the Rust documentation. They
  13. get transformed into KUnit tests.
  14. Usage
  15. *****
  16. These tests can be run via KUnit. For example via ``kunit_tool`` (``kunit.py``)
  17. on the command line::
  18. ./tools/testing/kunit/kunit.py run --make_options LLVM=1 --arch x86_64 --kconfig_add CONFIG_RUST=y
  19. Alternatively, KUnit can run them as kernel built-in at boot. Refer to
  20. Documentation/dev-tools/kunit/index.rst for the general KUnit documentation
  21. and Documentation/dev-tools/kunit/architecture.rst for the details of kernel
  22. built-in vs. command line testing.
  23. To use these KUnit doctests, the following must be enabled::
  24. CONFIG_KUNIT
  25. Kernel hacking -> Kernel Testing and Coverage -> KUnit - Enable support for unit tests
  26. CONFIG_RUST_KERNEL_DOCTESTS
  27. Kernel hacking -> Rust hacking -> Doctests for the `kernel` crate
  28. in the kernel config system.
  29. KUnit tests are documentation tests
  30. ***********************************
  31. These documentation tests are typically examples of usage of any item (e.g.
  32. function, struct, module...).
  33. They are very convenient because they are just written alongside the
  34. documentation. For instance:
  35. .. code-block:: rust
  36. /// Sums two numbers.
  37. ///
  38. /// ```
  39. /// assert_eq!(mymod::f(10, 20), 30);
  40. /// ```
  41. pub fn f(a: i32, b: i32) -> i32 {
  42. a + b
  43. }
  44. In userspace, the tests are collected and run via ``rustdoc``. Using the tool
  45. as-is would be useful already, since it allows verifying that examples compile
  46. (thus enforcing they are kept in sync with the code they document) and as well
  47. as running those that do not depend on in-kernel APIs.
  48. For the kernel, however, these tests get transformed into KUnit test suites.
  49. This means that doctests get compiled as Rust kernel objects, allowing them to
  50. run against a built kernel.
  51. A benefit of this KUnit integration is that Rust doctests get to reuse existing
  52. testing facilities. For instance, the kernel log would look like::
  53. KTAP version 1
  54. 1..1
  55. KTAP version 1
  56. # Subtest: rust_doctests_kernel
  57. 1..59
  58. # rust_doctest_kernel_build_assert_rs_0.location: rust/kernel/build_assert.rs:13
  59. ok 1 rust_doctest_kernel_build_assert_rs_0
  60. # rust_doctest_kernel_build_assert_rs_1.location: rust/kernel/build_assert.rs:56
  61. ok 2 rust_doctest_kernel_build_assert_rs_1
  62. # rust_doctest_kernel_init_rs_0.location: rust/kernel/init.rs:122
  63. ok 3 rust_doctest_kernel_init_rs_0
  64. ...
  65. # rust_doctest_kernel_types_rs_2.location: rust/kernel/types.rs:150
  66. ok 59 rust_doctest_kernel_types_rs_2
  67. # rust_doctests_kernel: pass:59 fail:0 skip:0 total:59
  68. # Totals: pass:59 fail:0 skip:0 total:59
  69. ok 1 rust_doctests_kernel
  70. Tests using the `? <https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator>`_
  71. operator are also supported as usual, e.g.:
  72. .. code-block:: rust
  73. /// ```
  74. /// # use kernel::{spawn_work_item, workqueue};
  75. /// spawn_work_item!(workqueue::system(), || pr_info!("x"))?;
  76. /// # Ok::<(), Error>(())
  77. /// ```
  78. The tests are also compiled with Clippy under ``CLIPPY=1``, just like normal
  79. code, thus also benefitting from extra linting.
  80. In order for developers to easily see which line of doctest code caused a
  81. failure, a KTAP diagnostic line is printed to the log. This contains the
  82. location (file and line) of the original test (i.e. instead of the location in
  83. the generated Rust file)::
  84. # rust_doctest_kernel_types_rs_2.location: rust/kernel/types.rs:150
  85. Rust tests appear to assert using the usual ``assert!`` and ``assert_eq!``
  86. macros from the Rust standard library (``core``). We provide a custom version
  87. that forwards the call to KUnit instead. Importantly, these macros do not
  88. require passing context, unlike those for KUnit testing (i.e.
  89. ``struct kunit *``). This makes them easier to use, and readers of the
  90. documentation do not need to care about which testing framework is used. In
  91. addition, it may allow us to test third-party code more easily in the future.
  92. A current limitation is that KUnit does not support assertions in other tasks.
  93. Thus, we presently simply print an error to the kernel log if an assertion
  94. actually failed. Additionally, doctests are not run for nonpublic functions.
  95. The ``#[test]`` tests
  96. ---------------------
  97. Additionally, there are the ``#[test]`` tests. These can be run using the
  98. ``rusttest`` Make target::
  99. make LLVM=1 rusttest
  100. This requires the kernel ``.config``. It runs the ``#[test]`` tests on the host
  101. (currently) and thus is fairly limited in what these tests can test.
  102. The Kselftests
  103. --------------
  104. Kselftests are also available in the ``tools/testing/selftests/rust`` folder.
  105. The kernel config options required for the tests are listed in the
  106. ``tools/testing/selftests/rust/config`` file and can be included with the aid
  107. of the ``merge_config.sh`` script::
  108. ./scripts/kconfig/merge_config.sh .config tools/testing/selftests/rust/config
  109. The kselftests are built within the kernel source tree and are intended to
  110. be executed on a system that is running the same kernel.
  111. Once a kernel matching the source tree has been installed and booted, the
  112. tests can be compiled and executed using the following command::
  113. make TARGETS="rust" kselftest
  114. Refer to Documentation/dev-tools/kselftest.rst for the general Kselftest
  115. documentation.