Skip to content

Commit 952b5c1

Browse files
committed
rust: support running documentation tests in-kernel
Rust has documentation tests: these are typically examples of usage of any item (e.g. function, struct, module...). They are very convenient because they are just written alongside the documentation. For instance: /// Sums two numbers. /// /// ``` /// assert_eq!(mymod::f(10, 20), 30); /// ``` pub fn f(a: i32, b: i32) -> i32 { a + b } So far, we were compiling and running them in the host as any other Rust documentation test. However, that meant we could not run tests that use kernel APIs (though we were compile-testing them, which was already useful to keep the documentation in sync with the code). Now, the documentation tests for the `kernel` crate are transformed into a KUnit test suite and run within the kernel at boot time, if enabled. This means now we can run tests that use kernel APIs. This has also other side effects, such as now using the in-kernel `alloc`, which does not have infallible allocations (previously, we did use the in-tree `alloc` in the built sysroot, but it was built via `build-std` without the same `cfg` set). This requires fixing a few tests. Another side effect is that Clippy is now run for doctests too, which also requires a few changes in tests. The CI is also changed to enable KUnit, our generated test suite and to ensure they pass. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent a01b701 commit 952b5c1

32 files changed

+414
-28
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ jobs:
266266
! grep -v 'at mm/debug_vm_pgtable.c:' qemu-stdout.log | grep '] WARNING:'
267267
268268
# Check
269+
- run: |
270+
grep '] ok 1 - rust_kernel_doctests$' qemu-stdout.log
271+
269272
- run: |
270273
grep '] rust_minimal: Rust minimal sample (init)$' qemu-stdout.log
271274
grep '] rust_minimal: Am I built-in? false$' qemu-stdout.log

.github/workflows/kernel-arm-debug.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ CONFIG_UNCOMPRESS_INCLUDE="debug/uncompress.h"
18021802
#
18031803
# Kernel Testing and Coverage
18041804
#
1805-
# CONFIG_KUNIT is not set
1805+
CONFIG_KUNIT=y
18061806
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
18071807
# CONFIG_FAULT_INJECTION is not set
18081808
CONFIG_ARCH_HAS_KCOV=y
@@ -1827,6 +1827,7 @@ CONFIG_RUST_OPT_LEVEL_2=y
18271827
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
18281828
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
18291829
CONFIG_RUST_BUILD_ASSERT_DENY=y
1830+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
18301831
# end of Rust hacking
18311832
# end of Kernel hacking
18321833

.github/workflows/kernel-arm-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ CONFIG_UNCOMPRESS_INCLUDE="debug/uncompress.h"
17251725
#
17261726
# Kernel Testing and Coverage
17271727
#
1728-
# CONFIG_KUNIT is not set
1728+
CONFIG_KUNIT=y
17291729
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
17301730
# CONFIG_FAULT_INJECTION is not set
17311731
CONFIG_ARCH_HAS_KCOV=y
@@ -1785,6 +1785,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
17851785
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
17861786
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
17871787
CONFIG_RUST_BUILD_ASSERT_DENY=y
1788+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
17881789
# end of Rust hacking
17891790
# end of Kernel hacking
17901791

.github/workflows/kernel-arm64-debug-thinlto.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ CONFIG_PID_IN_CONTEXTIDR=y
14501450
#
14511451
# Kernel Testing and Coverage
14521452
#
1453-
# CONFIG_KUNIT is not set
1453+
CONFIG_KUNIT=y
14541454
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
14551455
CONFIG_FUNCTION_ERROR_INJECTION=y
14561456
# CONFIG_FAULT_INJECTION is not set
@@ -1476,6 +1476,7 @@ CONFIG_RUST_OPT_LEVEL_1=y
14761476
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14771477
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14781478
CONFIG_RUST_BUILD_ASSERT_DENY=y
1479+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14791480
# end of Rust hacking
14801481
# end of Kernel hacking
14811482

.github/workflows/kernel-arm64-debug.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ CONFIG_PID_IN_CONTEXTIDR=y
14451445
#
14461446
# Kernel Testing and Coverage
14471447
#
1448-
# CONFIG_KUNIT is not set
1448+
CONFIG_KUNIT=y
14491449
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
14501450
CONFIG_FUNCTION_ERROR_INJECTION=y
14511451
# CONFIG_FAULT_INJECTION is not set
@@ -1471,6 +1471,7 @@ CONFIG_RUST_OPT_LEVEL_1=y
14711471
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14721472
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14731473
CONFIG_RUST_BUILD_ASSERT_DENY=y
1474+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14741475
# end of Rust hacking
14751476
# end of Kernel hacking
14761477

.github/workflows/kernel-arm64-release-thinlto.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ CONFIG_SAMPLE_RUST_HOSTPROGS=y
13681368
#
13691369
# Kernel Testing and Coverage
13701370
#
1371-
# CONFIG_KUNIT is not set
1371+
CONFIG_KUNIT=y
13721372
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
13731373
# CONFIG_FAULT_INJECTION is not set
13741374
CONFIG_ARCH_HAS_KCOV=y
@@ -1393,6 +1393,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
13931393
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
13941394
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
13951395
CONFIG_RUST_BUILD_ASSERT_DENY=y
1396+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
13961397
# end of Rust hacking
13971398
# end of Kernel hacking
13981399

.github/workflows/kernel-arm64-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ CONFIG_SAMPLE_RUST_HOSTPROGS=y
13631363
#
13641364
# Kernel Testing and Coverage
13651365
#
1366-
# CONFIG_KUNIT is not set
1366+
CONFIG_KUNIT=y
13671367
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
13681368
# CONFIG_FAULT_INJECTION is not set
13691369
CONFIG_ARCH_HAS_KCOV=y
@@ -1388,6 +1388,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
13881388
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
13891389
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
13901390
CONFIG_RUST_BUILD_ASSERT_DENY=y
1391+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
13911392
# end of Rust hacking
13921393
# end of Kernel hacking
13931394

.github/workflows/kernel-ppc64le-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,7 @@ CONFIG_RUST_OPT_LEVEL_0=y
15921592
# CONFIG_RUST_OPT_LEVEL_Z is not set
15931593
CONFIG_RUST_BUILD_ASSERT_ALLOW=y
15941594
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
1595+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
15951596
# end of Rust hacking
15961597
# end of Kernel hacking
15971598

.github/workflows/kernel-ppc64le-release.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ CONFIG_PRINT_STACK_DEPTH=64
14731473
#
14741474
# Kernel Testing and Coverage
14751475
#
1476-
# CONFIG_KUNIT is not set
1476+
CONFIG_KUNIT=y
14771477
CONFIG_ARCH_HAS_KCOV=y
14781478
CONFIG_CC_HAS_SANCOV_TRACE_PC=y
14791479
# CONFIG_KCOV is not set
@@ -1496,6 +1496,7 @@ CONFIG_RUST_OPT_LEVEL_SIMILAR_AS_CHOSEN_FOR_C=y
14961496
# CONFIG_RUST_BUILD_ASSERT_ALLOW is not set
14971497
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
14981498
CONFIG_RUST_BUILD_ASSERT_DENY=y
1499+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
14991500
# end of Rust hacking
15001501
# end of Kernel hacking
15011502

.github/workflows/kernel-riscv64-debug.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ CONFIG_SAMPLE_RUST_HOSTPROGS=y
12961296
#
12971297
# Kernel Testing and Coverage
12981298
#
1299-
# CONFIG_KUNIT is not set
1299+
CONFIG_KUNIT=y
13001300
# CONFIG_NOTIFIER_ERROR_INJECTION is not set
13011301
# CONFIG_FAULT_INJECTION is not set
13021302
CONFIG_ARCH_HAS_KCOV=y
@@ -1320,6 +1320,7 @@ CONFIG_RUST_OPT_LEVEL_0=y
13201320
# CONFIG_RUST_OPT_LEVEL_Z is not set
13211321
CONFIG_RUST_BUILD_ASSERT_ALLOW=y
13221322
# CONFIG_RUST_BUILD_ASSERT_WARN is not set
1323+
CONFIG_RUST_KERNEL_KUNIT_TEST=y
13231324
# end of Rust hacking
13241325
# end of Kernel hacking
13251326

0 commit comments

Comments
 (0)