From: Boqun Feng <boqun.feng@gmail.com>
To: David Gow <davidgow@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"José Expósito" <jose.exposito89@gmail.com>,
"Brendan Higgins" <brendan.higgins@linux.dev>,
"Rae Moar" <rmoar@google.com>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Benno Lossin" <benno.lossin@proton.me>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Matt Gilbride" <mattgilbride@google.com>,
kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 3/3] rust: kunit: allow to know if we are in a test
Date: Fri, 1 Nov 2024 14:29:39 -0700 [thread overview]
Message-ID: <ZyVIQxTf1qkts1d_@Boquns-Mac-mini.local> (raw)
In-Reply-To: <20241101064505.3820737-4-davidgow@google.com>
On Fri, Nov 01, 2024 at 02:45:02PM +0800, David Gow wrote:
[...]
> +/// ```
> +/// // Import our mock naming it as the real module.
> +/// #[cfg(CONFIG_KUNIT)]
> +/// use bindings_mock_example as bindings;
> +///
> +/// // This module mocks `bindings`.
> +/// mod bindings_mock_example {
> +/// use kernel::kunit::in_kunit_test;
> +/// use kernel::bindings::u64_;
> +///
> +/// // Make the other binding functions available.
> +/// pub(crate) use kernel::bindings::*;
> +///
> +/// // Mock `ktime_get_boot_fast_ns` to return a well-known value when running a KUnit test.
> +/// pub(crate) unsafe fn ktime_get_boot_fast_ns() -> u64_ {
Clippy complains this `unsafe` pub function doesn't have a "# Safety"
section. Actually this function is not necessarily to be `unsafe`.
> +/// if in_kunit_test() {
> +/// 1234
> +/// } else {
> +/// unsafe { kernel::bindings::ktime_get_boot_fast_ns() }
Need safety comments here,
> +/// }
> +/// }
> +/// }
> +///
> +/// // This is the function we want to test. Since `bindings` has been mocked, we can use its
> +/// // functions seamlessly.
> +/// fn get_boot_ns() -> u64 {
> +/// unsafe { bindings::ktime_get_boot_fast_ns() }
and here. If you make ktime_get_boot_fast_ns() safe, then no unsafe
block is needed here.
Regards,
Boqun
> +/// }
> +///
> +/// let time = get_boot_ns();
> +/// assert_eq!(time, 1234);
> +/// ```
> +pub fn in_kunit_test() -> bool {
> + // SAFETY: kunit_get_current_test() is always safe to call from C (it has fallbacks for
> + // when KUnit is not enabled), and we're only comparing the result to NULL.
> + unsafe { !bindings::kunit_get_current_test().is_null() }
> +}
> +
> #[kunit_tests(rust_kernel_kunit)]
> mod tests {
> + use super::*;
> +
> #[test]
> fn rust_test_kunit_example_test() {
> assert_eq!(1 + 1, 2);
> }
> +
> + #[test]
> + fn rust_test_kunit_in_kunit_test() {
> + let in_kunit = in_kunit_test();
> + assert!(in_kunit);
> + }
> }
> --
> 2.47.0.199.ga7371fff76-goog
>
prev parent reply other threads:[~2024-11-01 21:29 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-01 6:44 [PATCH v4 0/3] rust: kunit: Support KUnit tests with a user-space like syntax David Gow
2024-11-01 6:45 ` [PATCH v4 1/3] rust: kunit: add KUnit case and suite macros David Gow
2024-11-01 17:47 ` Boqun Feng
2024-11-09 8:18 ` David Gow
2024-11-09 20:32 ` Miguel Ojeda
2024-11-01 6:45 ` [PATCH v4 2/3] rust: macros: add macro to easily run KUnit tests David Gow
2024-11-01 21:38 ` Boqun Feng
2024-11-01 22:12 ` Miguel Ojeda
2024-11-02 0:08 ` Boqun Feng
2024-11-03 15:41 ` Miguel Ojeda
2024-11-01 6:45 ` [PATCH v4 3/3] rust: kunit: allow to know if we are in a test David Gow
2024-11-01 21:29 ` Boqun Feng [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZyVIQxTf1qkts1d_@Boquns-Mac-mini.local \
--to=boqun.feng@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=brendan.higgins@linux.dev \
--cc=davidgow@google.com \
--cc=gary@garyguo.net \
--cc=jose.exposito89@gmail.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mattgilbride@google.com \
--cc=ojeda@kernel.org \
--cc=rmoar@google.com \
--cc=rust-for-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).