[PATCH v8 0/3] rust: kunit: Support KUnit tests with a user-space like syntax

David Gow posted 3 patches 11 months, 1 week ago
MAINTAINERS          |   1 +
rust/kernel/kunit.rs | 171 +++++++++++++++++++++++++++++++++++++++++++
rust/macros/kunit.rs | 161 ++++++++++++++++++++++++++++++++++++++++
rust/macros/lib.rs   |  29 ++++++++
4 files changed, 362 insertions(+)
create mode 100644 rust/macros/kunit.rs
[PATCH v8 0/3] rust: kunit: Support KUnit tests with a user-space like syntax
Posted by David Gow 11 months, 1 week ago
Hi all,

This is v8 of the Rust/KUnit integration patch. I think all of the
suggestions have at least been responded to (even if there are a few I'm
leaving as either future projects or matters of taste). Hopefully this
is good-to-go for 6.15, so we can start using it concurrently with
making any additional improvements we may wish.

This series was originally written by José Expósito, and has been
modified and updated by Matt Gilbride, Miguel Ojeda, and myself. The
original version can be found here:
https://github.com/Rust-for-Linux/linux/pull/950

Add support for writing KUnit tests in Rust. While Rust doctests are
already converted to KUnit tests and run, they're really better suited
for examples, rather than as first-class unit tests.

This series implements a series of direct Rust bindings for KUnit tests,
as well as a new macro which allows KUnit tests to be written using a
close variant of normal Rust unit test syntax. The only change required
is replacing '#[cfg(test)]' with '#[kunit_tests(kunit_test_suite_name)]'

An example test would look like:
	#[kunit_tests(rust_kernel_hid_driver)]
	mod tests {
	    use super::*;
	    use crate::{c_str, driver, hid, prelude::*};
	    use core::ptr;

	    struct SimpleTestDriver;
	    impl Driver for SimpleTestDriver {
	        type Data = ();
	    }

	    #[test]
	    fn rust_test_hid_driver_adapter() {
	        let mut hid = bindings::hid_driver::default();
	        let name = c_str!("SimpleTestDriver");
	        static MODULE: ThisModule = unsafe { ThisModule::from_ptr(ptr::null_mut()) };

        	let res = unsafe {
	            <hid::Adapter<SimpleTestDriver> as driver::DriverOps>::register(&mut hid, name, &MODULE)
	        };
	        assert_eq!(res, Err(ENODEV)); // The mock returns -19
	    }
	}


Please give this a go, and make sure I haven't broken it! There's almost
certainly a lot of improvements which can be made -- and there's a fair
case to be made for replacing some of this with generated C code which
can use the C macros -- but this is hopefully an adequate implementation
for now, and the interface can (with luck) remain the same even if the
implementation changes.

A few small notable missing features:
- Attributes (like the speed of a test) are hardcoded to the default
  value.
- Similarly, the module name attribute is hardcoded to NULL. In C, we
  use the KBUILD_MODNAME macro, but I couldn't find a way to use this
  from Rust which wasn't more ugly than just disabling it.
- Assertions are not automatically rewritten to use KUnit assertions.

---

Changes since v7:
https://lore.kernel.org/rust-for-linux/20250214074051.1619256-1-davidgow@google.com/
- Reworked the SAFETY comment for addr_of_mut! use with statics in
  kunit_unsafe_test_suite!() (again)
- Removed the second mocking example, which was causing confusion.
  The first example of in_kunit_test() should be clear enough.

Changes since v6:
https://lore.kernel.org/rust-for-linux/20250214074051.1619256-1-davidgow@google.com/
- Fixed an [allow(unused_unsafe)] which ended up in patch 2 instead of
  patch 1. (Thanks, Tamir!)
- Doc comments now have several useful links. (Thanks, Tamir!)
- Fix a potential compile error under macos. (Thanks, Tamir!)
- Several small tidy-ups to limit unsafe usage. (Thanks, Tamir!)

Changes since v5:
https://lore.kernel.org/all/20241213081035.2069066-1-davidgow@google.com/
- Rebased against 6.14-rc1
- Fixed a bunch of warnings / clippy lints introduced in Rust 1.83 and
  1.84.
- No longer needs static_mut_refs / const_mut_refs, and is much cleaned
  up as a result. (Thanks, Miguel)
- Major documentation and example fixes. (Thanks, Miguel)

Changes since v4:
https://lore.kernel.org/linux-kselftest/20241101064505.3820737-1-davidgow@google.com/
- Rebased against 6.13-rc1
- Allowed an unused_unsafe warning after the behaviour of addr_of_mut!()
  changed in Rust 1.82. (Thanks Boqun, Miguel)
- "Expect" that the sample assert_eq!(1+1, 2) produces a clippy warning
  due to a redundant assertion. (Thanks Boqun, Miguel)
- Fix some missing safety comments, and remove some unneeded 'unsafe'
  blocks. (Thanks Boqun)
- Fix a couple of minor rustfmt issues which were triggering checkpatch
  warnings.

Changes since v3:
https://lore.kernel.org/linux-kselftest/20241030045719.3085147-2-davidgow@google.com/T/
- The kunit_unsafe_test_suite!() macro now panic!s if the suite name is
  too long, triggering a compile error. (Thanks, Alice!)
- The #[kunit_tests()] macro now preserves span information, so
  errors can be better reported. (Thanks, Boqun!)
- The example tests have been updated to no longer use assert_eq!() with
  a constant bool argument (which triggered a clippy warning now we
  have the span info).

Changes since v2:
https://lore.kernel.org/linux-kselftest/20241029092422.2884505-1-davidgow@google.com/T/
- Include missing rust/macros/kunit.rs file from v2. (Thanks Boqun!)
- The kunit_unsafe_test_suite!() macro will truncate the name of the
  suite if it is too long. (Thanks Alice!)
- The proc macro now emits an error if the suite name is too long.
- We no longer needlessly use UnsafeCell<> in
  kunit_unsafe_test_suite!(). (Thanks Alice!)

Changes since v1:
https://lore.kernel.org/lkml/20230720-rustbind-v1-0-c80db349e3b5@google.com/T/
- Rebase on top of the latest rust-next (commit 718c4069896c)
- Make kunit_case a const fn, rather than a macro (Thanks Boqun)
- As a result, the null terminator is now created with
  kernel::kunit::kunit_case_null()
- Use the C kunit_get_current_test() function to implement
  in_kunit_test(), rather than re-implementing it (less efficiently)
  ourselves.

Changes since the GitHub PR:
- Rebased on top of kselftest/kunit
- Add const_mut_refs feature
  This may conflict with https://lore.kernel.org/lkml/20230503090708.2524310-6-nmi@metaspace.dk/
- Add rust/macros/kunit.rs to the KUnit MAINTAINERS entry

---

José Expósito (3):
  rust: kunit: add KUnit case and suite macros
  rust: macros: add macro to easily run KUnit tests
  rust: kunit: allow to know if we are in a test

 MAINTAINERS          |   1 +
 rust/kernel/kunit.rs | 171 +++++++++++++++++++++++++++++++++++++++++++
 rust/macros/kunit.rs | 161 ++++++++++++++++++++++++++++++++++++++++
 rust/macros/lib.rs   |  29 ++++++++
 4 files changed, 362 insertions(+)
 create mode 100644 rust/macros/kunit.rs

-- 
2.49.0.rc0.332.g42c0ae87b1-goog
Re: [PATCH v8 0/3] rust: kunit: Support KUnit tests with a user-space like syntax
Posted by Miguel Ojeda 10 months, 3 weeks ago
On Fri, Mar 7, 2025 at 10:01 AM David Gow <davidgow@google.com> wrote:
>
> This is v8 of the Rust/KUnit integration patch. I think all of the
> suggestions have at least been responded to (even if there are a few I'm
> leaving as either future projects or matters of taste). Hopefully this
> is good-to-go for 6.15, so we can start using it concurrently with
> making any additional improvements we may wish.

Applied to `rust-next` -- thanks everyone!

    [ Applied Markdown in comment. - Miguel ]

    [ Removed spurious (in rendered form) newline in docs. - Miguel ]

(finally! Special thanks to David here.)

Cheers,
Miguel
Re: [PATCH v8 0/3] rust: kunit: Support KUnit tests with a user-space like syntax
Posted by David Gow 10 months, 3 weeks ago
On Thu, 20 Mar 2025 at 19:24, Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Fri, Mar 7, 2025 at 10:01 AM David Gow <davidgow@google.com> wrote:
> >
> > This is v8 of the Rust/KUnit integration patch. I think all of the
> > suggestions have at least been responded to (even if there are a few I'm
> > leaving as either future projects or matters of taste). Hopefully this
> > is good-to-go for 6.15, so we can start using it concurrently with
> > making any additional improvements we may wish.
>
> Applied to `rust-next` -- thanks everyone!
>
>     [ Applied Markdown in comment. - Miguel ]
>
>     [ Removed spurious (in rendered form) newline in docs. - Miguel ]
>
> (finally! Special thanks to David here.)
>

Woohoo! Glad to see this finally at the finish line.

I'll be able to finally have the chocolate I've been saving for when this lands!

Thanks,
-- David
Re: [PATCH v8 0/3] rust: kunit: Support KUnit tests with a user-space like syntax
Posted by David Gow 10 months, 3 weeks ago
On Fri, 7 Mar 2025 at 17:01, David Gow <davidgow@google.com> wrote:
>
> Hi all,
>
> This is v8 of the Rust/KUnit integration patch. I think all of the
> suggestions have at least been responded to (even if there are a few I'm
> leaving as either future projects or matters of taste). Hopefully this
> is good-to-go for 6.15, so we can start using it concurrently with
> making any additional improvements we may wish.
>
> This series was originally written by José Expósito, and has been
> modified and updated by Matt Gilbride, Miguel Ojeda, and myself. The
> original version can be found here:
> https://github.com/Rust-for-Linux/linux/pull/950

Has anyone got any objections to this for 6.15?

I'm happy for it to go in via the rust tree if that's most convenient,
but if you'd rather it go in via kselftest/kunit, that's fine, too.

The only reason for delay I can think of is all of the patches which
adjust clippy warnings, etc., for things like addr_of_mut!() (and
possibly some of the casting). I'm unlikely to have time to re-send
everything out before the merge window with those fixed (I'm going to
be away for the latter half of this week), so if folks are okay with
either accepting this as-is, doing a follow-up fix, or delaying those
warnings, that's ideal on my end.

Cheers,
-- David
Re: [PATCH v8 0/3] rust: kunit: Support KUnit tests with a user-space like syntax
Posted by Miguel Ojeda 10 months, 3 weeks ago
On Tue, Mar 18, 2025 at 9:14 AM David Gow <davidgow@google.com> wrote:
>
> The only reason for delay I can think of is all of the patches which
> adjust clippy warnings, etc., for things like addr_of_mut!() (and
> possibly some of the casting). I'm unlikely to have time to re-send
> everything out before the merge window with those fixed (I'm going to
> be away for the latter half of this week), so if folks are okay with
> either accepting this as-is, doing a follow-up fix, or delaying those
> warnings, that's ideal on my end.

Yeah, those series (Antonio's or Tamir's) are independent, and in my
view they shouldn't delay something (a feature) like this series.

Cheers,
Miguel