From db8b5261cacf7dfafed182a9f4e94fcfa5d37aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Exp=C3=B3sito?= Date: Mon, 5 Dec 2022 17:34:11 +0100 Subject: [PATCH 1/2] rust: add rust/.kunitconfig MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a .kunitconfig file with the required configuration options to allow to easily run the KUnit tests without adding them manually: $ ./tools/testing/kunit/kunit.py run --kunitconfig=rust \ --make_options LLVM=1 --arch=x86_64 Note that "CONFIG_UML" is set to "n" because UML is not working at the moment [1]. [1] https://github.com/Rust-for-Linux/linux/pull/881 Reviewed-by: David Gow Signed-off-by: José Expósito --- rust/.kunitconfig | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 rust/.kunitconfig diff --git a/rust/.kunitconfig b/rust/.kunitconfig new file mode 100644 index 00000000000000..e670ffb08578bf --- /dev/null +++ b/rust/.kunitconfig @@ -0,0 +1,6 @@ +CONFIG_KUNIT=y +CONFIG_RUST=y +CONFIG_RUST_KERNEL_KUNIT_TEST=y + +# UML is not supported at the moment +CONFIG_UML=n From 81938fa74c46b1f7f45e10a6da16822f070868d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Exp=C3=B3sito?= Date: Mon, 5 Dec 2022 17:47:27 +0100 Subject: [PATCH 2/2] docs: rust: document automated testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Explain how to run unit tests and documentation tests. Note that the documentation uses "--arch=x86_64" to run KUnit tests because UML is not working at the moment [1]. [1] https://github.com/Rust-for-Linux/linux/pull/881 Reviewed-by: David Gow Signed-off-by: José Expósito --- Documentation/rust/index.rst | 1 + Documentation/rust/testing.rst | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Documentation/rust/testing.rst diff --git a/Documentation/rust/index.rst b/Documentation/rust/index.rst index 4ae8c66b94faf9..2e3a24cea6b2a2 100644 --- a/Documentation/rust/index.rst +++ b/Documentation/rust/index.rst @@ -13,6 +13,7 @@ in the kernel, please read the quick-start.rst guide. general-information coding-guidelines arch-support + testing .. only:: subproject and html diff --git a/Documentation/rust/testing.rst b/Documentation/rust/testing.rst new file mode 100644 index 00000000000000..da0c713e68d51e --- /dev/null +++ b/Documentation/rust/testing.rst @@ -0,0 +1,52 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Testing +======= + +Like in any other Rust project it is possible to write and run unit tests and +documentation tests in the kernel. + +Running Unit Tests +------------------ + +Unit tests in the kernel are identical to user-space Rust tests: + +.. code-block:: rust + + #[cfg(test)] + mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } + } + +And can be run using the ``rusttest`` Make target: + +.. code-block:: bash + + $ make LLVM=1 rusttest + +Running Documentation Tests +--------------------------- + +Like in user-space, it is possible to write documentation tests: + +.. code-block:: rust + + /// ``` + /// let result = 2 + 2; + /// assert_eq!(result, 4); + /// ``` + +Documentation tests use KUnit and it is possible to run them either on boot or +using the ``kunit.py`` tool: + +.. code-block:: bash + + $ ./tools/testing/kunit/kunit.py run --kunitconfig=rust \ + --make_options LLVM=1 --arch=x86_64 + +For general information about KUnit and `kunit.py``, please refer to +Documentation/dev-tools/kunit/start.rst.