mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Marie Zhussupova <marievic@google.com>
To: rmoar@google.com, davidgow@google.com, shuah@kernel.org,
	 brendan.higgins@linux.dev
Cc: mark.rutland@arm.com, elver@google.com, dvyukov@google.com,
	 lucas.demarchi@intel.com, thomas.hellstrom@linux.intel.com,
	 rodrigo.vivi@intel.com, linux-kselftest@vger.kernel.org,
	 kunit-dev@googlegroups.com, kasan-dev@googlegroups.com,
	 intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
	 linux-kernel@vger.kernel.org,
	Marie Zhussupova <marievic@google.com>
Subject: [PATCH v2 2/7] kunit: Introduce param_init/exit for parameterized test context management
Date: Mon, 11 Aug 2025 22:17:34 +0000	[thread overview]
Message-ID: <20250811221739.2694336-3-marievic@google.com> (raw)
In-Reply-To: <20250811221739.2694336-1-marievic@google.com>

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.

Signed-off-by: Marie Zhussupova <marievic@google.com>
---

Changes in v2:

- param init/exit were set to None
  in rust/kernel/kunit.rs to fix the Rust breakage.
- The name of __kunit_init_parent_test was
  changed to kunit_init_parent_param_test and
  its call was changed to happen only if the
  test is parameterized.
- The param_exit call was also moved inside
  the check for if the test is parameterized.
- KUNIT_CASE_PARAM_WITH_INIT() macro logic was changed
  to not automatically set generate_params() to KUnit's
  built-in generator function. Instead, the test user
  will be asked to provide it themselves.
- The comments and the commit message were changed to
  reflect the parameterized testing terminology. See
  the patch series cover letter change log for the
  definitions.

---
 include/kunit/test.h | 25 +++++++++++++++++++++++++
 lib/kunit/test.c     | 20 ++++++++++++++++++++
 rust/kernel/kunit.rs |  4 ++++
 3 files changed, 49 insertions(+)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index b47b9a3102f3..d2e1b986b161 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -92,6 +92,8 @@ struct kunit_attributes {
  * @name:     the name of the test case.
  * @generate_params: the generator function for parameterized tests.
  * @attr:     the attributes associated with the test
+ * @param_init: The init function to run before a parameterized test.
+ * @param_exit: The exit function to run after a parameterized test.
  *
  * A test case is a function with the signature,
  * ``void (*)(struct kunit *)``
@@ -128,6 +130,8 @@ struct kunit_case {
 	const char *name;
 	const void* (*generate_params)(const void *prev, char *desc);
 	struct kunit_attributes attr;
+	int (*param_init)(struct kunit *test);
+	void (*param_exit)(struct kunit *test);
 
 	/* private: internal use only. */
 	enum kunit_status status;
@@ -218,6 +222,27 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
 		  .generate_params = gen_params,				\
 		  .attr = attributes, .module_name = KBUILD_MODNAME}
 
+/**
+ * KUNIT_CASE_PARAM_WITH_INIT - Define a parameterized KUnit test case with custom
+ * param_init() and param_exit() functions.
+ * @test_name: The function implementing the test case.
+ * @gen_params: The function to generate parameters for the test case.
+ * @init: A reference to the param_init() function to run before a parameterized test.
+ * @exit: A reference to the param_exit() function to run after a parameterized test.
+ *
+ * Provides the option to register param_init() and param_exit() functions.
+ * param_init/exit will be passed the parameterized test context and run once
+ * before and once after the parameterized test. The init function can be used
+ * to add resources to share between parameter runs, and any other setup logic.
+ * The exit function can be used to clean up resources that were not managed by
+ * the parameterized test, and any other teardown logic.
+ */
+#define KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit)		\
+		{ .run_case = test_name, .name = #test_name,			\
+		  .generate_params = gen_params,				\
+		  .param_init = init, .param_exit = exit,			\
+		  .module_name = KBUILD_MODNAME}
+
 /**
  * struct kunit_suite - describes a related collection of &struct kunit_case
  *
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 14a8bd846939..49a5e6c30c86 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -641,6 +641,19 @@ static void kunit_accumulate_stats(struct kunit_result_stats *total,
 	total->total += add.total;
 }
 
+static void kunit_init_parent_param_test(struct kunit_case *test_case, struct kunit *test)
+{
+	if (test_case->param_init) {
+		int err = test_case->param_init(test);
+
+		if (err) {
+			kunit_err(test_case, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
+				"# failed to initialize parent parameter test.");
+			test_case->status = KUNIT_FAILURE;
+		}
+	}
+}
+
 int kunit_run_tests(struct kunit_suite *suite)
 {
 	char param_desc[KUNIT_PARAM_DESC_SIZE];
@@ -678,6 +691,7 @@ int kunit_run_tests(struct kunit_suite *suite)
 			kunit_run_case_catch_errors(suite, test_case, &test);
 			kunit_update_stats(&param_stats, test.status);
 		} else {
+			kunit_init_parent_param_test(test_case, &test);
 			/* Get initial param. */
 			param_desc[0] = '\0';
 			/* TODO: Make generate_params try-catch */
@@ -714,6 +728,12 @@ int kunit_run_tests(struct kunit_suite *suite)
 				param_desc[0] = '\0';
 				curr_param = test_case->generate_params(curr_param, param_desc);
 			}
+			/*
+			 * TODO: Put into a try catch. Since we don't need suite->exit
+			 * for it we can't reuse kunit_try_run_cleanup for this yet.
+			 */
+			if (test_case->param_exit)
+				test_case->param_exit(&test);
 			/* TODO: Put this kunit_cleanup into a try-catch. */
 			kunit_cleanup(&test);
 		}
diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs
index 4b8cdcb21e77..cda64574b44d 100644
--- a/rust/kernel/kunit.rs
+++ b/rust/kernel/kunit.rs
@@ -207,6 +207,8 @@ pub const fn kunit_case(
         status: kernel::bindings::kunit_status_KUNIT_SUCCESS,
         module_name: core::ptr::null_mut(),
         log: core::ptr::null_mut(),
+        param_init: None,
+        param_exit: None,
     }
 }
 
@@ -226,6 +228,8 @@ pub const fn kunit_case_null() -> kernel::bindings::kunit_case {
         status: kernel::bindings::kunit_status_KUNIT_SUCCESS,
         module_name: core::ptr::null_mut(),
         log: core::ptr::null_mut(),
+        param_init: None,
+        param_exit: None,
     }
 }
 
-- 
2.51.0.rc0.205.g4a044479a3-goog


  parent reply	other threads:[~2025-08-11 22:17 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-11 22:17 [PATCH v2 0/7] kunit: Refactor and extend KUnit's parameterized testing framework Marie Zhussupova
2025-08-11 22:17 ` [PATCH v2 1/7] kunit: Add parent kunit for parameterized test context Marie Zhussupova
2025-08-12 22:22   ` Rae Moar
2025-08-13  9:17   ` David Gow
2025-08-11 22:17 ` Marie Zhussupova [this message]
2025-08-12 22:22   ` [PATCH v2 2/7] kunit: Introduce param_init/exit for parameterized test context management Rae Moar
2025-08-13  9:17   ` David Gow
2025-08-11 22:17 ` [PATCH v2 3/7] kunit: Pass parameterized test context to generate_params() Marie Zhussupova
2025-08-12 13:54   ` Rodrigo Vivi
2025-08-12 22:22   ` Rae Moar
2025-08-13  7:43   ` Marco Elver
2025-08-13  9:18   ` David Gow
2025-08-11 22:17 ` [PATCH v2 4/7] kunit: Enable direct registration of parameter arrays to a KUnit test Marie Zhussupova
2025-08-12 22:23   ` Rae Moar
2025-08-13  9:17   ` David Gow
2025-08-11 22:17 ` [PATCH v2 5/7] kunit: Add example parameterized test with shared resource management using the Resource API Marie Zhussupova
2025-08-12 22:23   ` Rae Moar
2025-08-13  9:17   ` David Gow
2025-08-11 22:17 ` [PATCH v2 6/7] kunit: Add example parameterized test with direct dynamic parameter array setup Marie Zhussupova
2025-08-12 22:23   ` Rae Moar
2025-08-13  9:18   ` David Gow
2025-08-11 22:17 ` [PATCH v2 7/7] Documentation: kunit: Document new parameterized test features Marie Zhussupova
2025-08-12 22:23   ` Rae Moar
2025-08-13  9:18   ` David Gow

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=20250811221739.2694336-3-marievic@google.com \
    --to=marievic@google.com \
    --cc=brendan.higgins@linux.dev \
    --cc=davidgow@google.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=kasan-dev@googlegroups.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=mark.rutland@arm.com \
    --cc=rmoar@google.com \
    --cc=rodrigo.vivi@intel.com \
    --cc=shuah@kernel.org \
    --cc=thomas.hellstrom@linux.intel.com \
    /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

all inboxes | Powered by JetHome®