Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kunit: Introduce param_init/exit for parameterized test context management

Add (*param_init) and (*param_exit) function pointers to
`struct kunit_case`. Users will be able to set them via the new
KUNIT_CASE_PARAM_WITH_INIT() macro.

param_init/exit will be invoked by kunit_run_tests() once before and once
after the parameterized test, respectively. They will receive the
`struct kunit` that holds the parameterized test context; facilitating
init and exit for shared state.

This patch also sets param_init/exit to None in rust/kernel/kunit.rs.

Link: https://lore.kernel.org/r/20250826091341.1427123-3-davidgow@google.com
Reviewed-by: Rae Moar <rmoar@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Marie Zhussupova <marievic@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Marie Zhussupova and committed by
Shuah Khan
24142358 4b59300b

+55 -1
+25
include/kunit/test.h
··· 92 92 * @name: the name of the test case. 93 93 * @generate_params: the generator function for parameterized tests. 94 94 * @attr: the attributes associated with the test 95 + * @param_init: The init function to run before a parameterized test. 96 + * @param_exit: The exit function to run after a parameterized test. 95 97 * 96 98 * A test case is a function with the signature, 97 99 * ``void (*)(struct kunit *)`` ··· 130 128 const char *name; 131 129 const void* (*generate_params)(const void *prev, char *desc); 132 130 struct kunit_attributes attr; 131 + int (*param_init)(struct kunit *test); 132 + void (*param_exit)(struct kunit *test); 133 133 134 134 /* private: internal use only. */ 135 135 enum kunit_status status; ··· 221 217 { .run_case = test_name, .name = #test_name, \ 222 218 .generate_params = gen_params, \ 223 219 .attr = attributes, .module_name = KBUILD_MODNAME} 220 + 221 + /** 222 + * KUNIT_CASE_PARAM_WITH_INIT - Define a parameterized KUnit test case with custom 223 + * param_init() and param_exit() functions. 224 + * @test_name: The function implementing the test case. 225 + * @gen_params: The function to generate parameters for the test case. 226 + * @init: A reference to the param_init() function to run before a parameterized test. 227 + * @exit: A reference to the param_exit() function to run after a parameterized test. 228 + * 229 + * Provides the option to register param_init() and param_exit() functions. 230 + * param_init/exit will be passed the parameterized test context and run once 231 + * before and once after the parameterized test. The init function can be used 232 + * to add resources to share between parameter runs, and any other setup logic. 233 + * The exit function can be used to clean up resources that were not managed by 234 + * the parameterized test, and any other teardown logic. 235 + */ 236 + #define KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit) \ 237 + { .run_case = test_name, .name = #test_name, \ 238 + .generate_params = gen_params, \ 239 + .param_init = init, .param_exit = exit, \ 240 + .module_name = KBUILD_MODNAME} 224 241 225 242 /** 226 243 * struct kunit_suite - describes a related collection of &struct kunit_case
+26 -1
lib/kunit/test.c
··· 641 641 total->total += add.total; 642 642 } 643 643 644 + static void kunit_init_parent_param_test(struct kunit_case *test_case, struct kunit *test) 645 + { 646 + if (test_case->param_init) { 647 + int err = test_case->param_init(test); 648 + 649 + if (err) { 650 + kunit_err(test_case, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT 651 + "# failed to initialize parent parameter test (%d)", err); 652 + test->status = KUNIT_FAILURE; 653 + test_case->status = KUNIT_FAILURE; 654 + } 655 + } 656 + } 657 + 644 658 int kunit_run_tests(struct kunit_suite *suite) 645 659 { 646 660 char param_desc[KUNIT_PARAM_DESC_SIZE]; ··· 692 678 kunit_run_case_catch_errors(suite, test_case, &test); 693 679 kunit_update_stats(&param_stats, test.status); 694 680 } else { 681 + kunit_init_parent_param_test(test_case, &test); 682 + if (test_case->status == KUNIT_FAILURE) { 683 + kunit_update_stats(&param_stats, test.status); 684 + goto test_case_end; 685 + } 695 686 /* Get initial param. */ 696 687 param_desc[0] = '\0'; 697 688 /* TODO: Make generate_params try-catch */ ··· 733 714 param_desc[0] = '\0'; 734 715 curr_param = test_case->generate_params(curr_param, param_desc); 735 716 } 717 + /* 718 + * TODO: Put into a try catch. Since we don't need suite->exit 719 + * for it we can't reuse kunit_try_run_cleanup for this yet. 720 + */ 721 + if (test_case->param_exit) 722 + test_case->param_exit(&test); 736 723 /* TODO: Put this kunit_cleanup into a try-catch. */ 737 724 kunit_cleanup(&test); 738 725 } 739 - 726 + test_case_end: 740 727 kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE); 741 728 742 729 kunit_print_test_stats(&test, param_stats);
+4
rust/kernel/kunit.rs
··· 210 210 status: kernel::bindings::kunit_status_KUNIT_SUCCESS, 211 211 module_name: core::ptr::null_mut(), 212 212 log: core::ptr::null_mut(), 213 + param_init: None, 214 + param_exit: None, 213 215 } 214 216 } 215 217 ··· 231 229 status: kernel::bindings::kunit_status_KUNIT_SUCCESS, 232 230 module_name: core::ptr::null_mut(), 233 231 log: core::ptr::null_mut(), 232 + param_init: None, 233 + param_exit: None, 234 234 } 235 235 } 236 236