History

Merge tag 'linux_kselftest-kunit-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest

Pull kunit updates from Shuah Khan: - New parameterized test features KUnit parameterized tests supported two primary methods for getting parameters: - Defining custom logic within a generate_params() function. - Using the KUNIT_ARRAY_PARAM() and KUNIT_ARRAY_PARAM_DESC() macros with a pre-defined static array and passing the created *_gen_params() to KUNIT_CASE_PARAM(). These methods present limitations when dealing with dynamically generated parameter arrays, or in scenarios where populating parameters sequentially via generate_params() is inefficient or overly complex. These limitations are fixed with a parameterized test method - Fix issues in kunit build artifacts cleanup - Fix parsing skipped test problem in kselftest framework - Enable PCI on UML without triggering WARN() - a few other fixes and adds support for new configs such as MIPS * tag 'linux_kselftest-kunit-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: Extend kconfig help text for KUNIT_UML_PCI rust: kunit: allow `cfg` on `test`s kunit: qemu_configs: Add MIPS configurations kunit: Enable PCI on UML without triggering WARN() Documentation: kunit: Document new parameterized test features kunit: Add example parameterized test with direct dynamic parameter array setup kunit: Add example parameterized test with shared resource management using the Resource API kunit: Enable direct registration of parameter arrays to a KUnit test kunit: Pass parameterized test context to generate_params() kunit: Introduce param_init/exit for parameterized test context management kunit: Add parent kunit for parameterized test context kunit: tool: Accept --raw_output=full as an alias of 'all' kunit: tool: Parse skipped tests from kselftest.h kunit: Always descend into kunit directory during build

Linus Torvalds committed 1 year ago

Commit 30bbcb42 parents d2b2fea 285cae520 files · +880 −63Browse files

Showing the changes to rust (2 of 20 files). Show all

Unified · Split

2 files
  1. rust/kernel/kunit.rs +11 −0
  2. rust/macros/kunit.rs +36 −12
rust/kernel/kunit.rs+11−0
@@ -210,6 +210,8 @@ pub const fn kunit_case( · 209 unmodified lines
210210 status: kernel::bindings::kunit_status_KUNIT_SUCCESS,
211211 module_name: core::ptr::null_mut(),
212212 log: core::ptr::null_mut(),
213+ param_init: None,
214+ param_exit: None,
213215 }
214216 }
215217
@@ -229,6 +231,8 @@ pub const fn kunit_case_null() -> kernel::bindings::kunit_case { · 13 unmodified lines
229231 status: kernel::bindings::kunit_status_KUNIT_SUCCESS,
230232 module_name: core::ptr::null_mut(),
231233 log: core::ptr::null_mut(),
234+ param_init: None,
235+ param_exit: None,
232236 }
233237 }
234238
@@ -357,4 +361,11 @@ mod tests { · 122 unmodified lines
357361 fn rust_test_kunit_in_kunit_test() {
358362 assert!(in_kunit_test());
359363 }
364+
365+ #[test]
366+ #[cfg(not(all()))]
367+ fn rust_test_kunit_always_disabled_test() {
368+ // This test should never run because of the `cfg`.
369+ assert!(false);
370+ }
360371 }
rust/macros/kunit.rs+36−12
@@ -5,6 +5,7 @@ · 4 unmodified lines
55 //! Copyright (c) 2023 José Expósito <jose.exposito89@gmail.com>
66
77 use proc_macro::{Delimiter, Group, TokenStream, TokenTree};
8+use std::collections::HashMap;
89 use std::fmt::Write;
910
1011 pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream {
@@ -41,20 +42,32 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream { · 30 unmodified lines
4142 // Get the functions set as tests. Search for `[test]` -> `fn`.
4243 let mut body_it = body.stream().into_iter();
4344 let mut tests = Vec::new();
45+ let mut attributes: HashMap<String, TokenStream> = HashMap::new();
4446 while let Some(token) = body_it.next() {
4547 match token {
46− TokenTree::Group(ident) if ident.to_string() == "[test]" => match body_it.next() {
47− Some(TokenTree::Ident(ident)) if ident.to_string() == "fn" => {
48− let test_name = match body_it.next() {
49− Some(TokenTree::Ident(ident)) => ident.to_string(),
50− _ => continue,
51− };
52− tests.push(test_name);
48+ TokenTree::Punct(ref p) if p.as_char() == '#' => match body_it.next() {
49+ Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Bracket => {
50+ if let Some(TokenTree::Ident(name)) = g.stream().into_iter().next() {
51+ // Collect attributes because we need to find which are tests. We also
52+ // need to copy `cfg` attributes so tests can be conditionally enabled.
53+ attributes
54+ .entry(name.to_string())
55+ .or_default()
56+ .extend([token, TokenTree::Group(g)]);
57+ }
58+ continue;
5359 }
54− _ => continue,
60+ _ => (),
5561 },
62+ TokenTree::Ident(i) if i.to_string() == "fn" && attributes.contains_key("test") => {
63+ if let Some(TokenTree::Ident(test_name)) = body_it.next() {
64+ tests.push((test_name, attributes.remove("cfg").unwrap_or_default()))
65+ }
66+ }
67+
5668 _ => (),
5769 }
70+ attributes.clear();
5871 }
5972
6073 // Add `#[cfg(CONFIG_KUNIT="y")]` before the module declaration.
@@ -100,11 +113,22 @@ pub(crate) fn kunit_tests(attr: TokenStream, ts: TokenStream) -> TokenStream { · 39 unmodified lines
100113 let mut test_cases = "".to_owned();
101114 let mut assert_macros = "".to_owned();
102115 let path = crate::helpers::file();
103− for test in &tests {
116+ let num_tests = tests.len();
117+ for (test, cfg_attr) in tests {
104118 let kunit_wrapper_fn_name = format!("kunit_rust_wrapper_{test}");
105− // An extra `use` is used here to reduce the length of the message.
119+ // Append any `cfg` attributes the user might have written on their tests so we don't
120+ // attempt to call them when they are `cfg`'d out. An extra `use` is used here to reduce
121+ // the length of the assert message.
106122 let kunit_wrapper = format!(
107− "unsafe extern \"C\" fn {kunit_wrapper_fn_name}(_test: *mut ::kernel::bindings::kunit) {{ use ::kernel::kunit::is_test_result_ok; assert!(is_test_result_ok({test}())); }}",
123+ r#"unsafe extern "C" fn {kunit_wrapper_fn_name}(_test: *mut ::kernel::bindings::kunit)
124+ {{
125+ (*_test).status = ::kernel::bindings::kunit_status_KUNIT_SKIPPED;
126+ {cfg_attr} {{
127+ (*_test).status = ::kernel::bindings::kunit_status_KUNIT_SUCCESS;
128+ use ::kernel::kunit::is_test_result_ok;
129+ assert!(is_test_result_ok({test}()));
130+ }}
131+ }}"#,
108132 );
109133 writeln!(kunit_macros, "{kunit_wrapper}").unwrap();
110134 writeln!(
@@ -139,7 +163,7 @@ macro_rules! assert_eq {{ · 28 unmodified lines
139163 writeln!(
140164 kunit_macros,
141165 "static mut TEST_CASES: [::kernel::bindings::kunit_case; {}] = [\n{test_cases} ::kernel::kunit::kunit_case_null(),\n];",
142− tests.len() + 1
166+ num_tests + 1
143167 )
144168 .unwrap();
145169

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t