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