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: 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 7/9] kunit: Add example parameterized test with shared resources and direct static parameter array setup
Date: Tue, 29 Jul 2025 19:36:45 +0000	[thread overview]
Message-ID: <20250729193647.3410634-8-marievic@google.com> (raw)
In-Reply-To: <20250729193647.3410634-1-marievic@google.com>

Add `example_params_test_with_init` to illustrate how to manage
shared resources across parameterized KUnit tests. This example
showcases the use of the new `param_init` function and its registration
to a test using the `KUNIT_CASE_PARAM_WITH_INIT` macro.

Additionally, the test demonstrates:
- How to directly assign a static parameter array to a test via
  `kunit_register_params_array`.
- Leveraging the Resource API for test resource management.

Signed-off-by: Marie Zhussupova <marievic@google.com>
---
 lib/kunit/kunit-example-test.c | 112 +++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)

diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c
index 3056d6bc705d..5bf559e243f6 100644
--- a/lib/kunit/kunit-example-test.c
+++ b/lib/kunit/kunit-example-test.c
@@ -277,6 +277,116 @@ static void example_slow_test(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, 1 + 1, 2);
 }
 
+/*
+ * This custom function allocates memory for the kunit_resource data field.
+ * The function is passed to kunit_alloc_resource() and executed once
+ * by the internal helper __kunit_add_resource().
+ */
+static int example_resource_init(struct kunit_resource *res, void *context)
+{
+	int *info = kmalloc(sizeof(*info), GFP_KERNEL);
+
+	if (!info)
+		return -ENOMEM;
+	*info = *(int *)context;
+	res->data = info;
+	return 0;
+}
+
+/*
+ * This function deallocates memory for the 'kunit_resource' data field.
+ * The function is passed to kunit_alloc_resource() and automatically
+ * executes within kunit_release_resource() when the resource's reference
+ * count, via kunit_put_resource(), drops to zero. KUnit uses reference
+ * counting to ensure that resources are not freed prematurely.
+ */
+static void example_resource_free(struct kunit_resource *res)
+{
+	kfree(res->data);
+}
+
+/*
+ * This match function is invoked by kunit_find_resource() to locate
+ * a test resource based on defined criteria. The current example
+ * uniquely identifies the resource by its free function; however,
+ * alternative custom criteria can be implemented. Refer to
+ * lib/kunit/platform.c and lib/kunit/static_stub.c for further examples.
+ */
+static bool example_resource_alloc_match(struct kunit *test,
+					 struct kunit_resource *res,
+					 void *match_data)
+{
+	return res->data && res->free == example_resource_free;
+}
+
+/*
+ * This is an example of a function that provides a description for each of the
+ * parameters.
+ */
+static void example_param_array_get_desc(const void *p, char *desc)
+{
+	const struct example_param *param = p;
+
+	snprintf(desc, KUNIT_PARAM_DESC_SIZE,
+		 "example check if %d is less than or equal to 3", param->value);
+}
+
+/*
+ * Initializes the parent kunit struct for parameterized KUnit tests.
+ * This function enables sharing resources across all parameterized
+ * tests by adding them to the `parent` kunit test struct. It also supports
+ * registering either static or dynamic arrays of test parameters.
+ */
+static int example_param_init(struct kunit *test)
+{
+	int ctx = 3; /* Data to be stored. */
+	int arr_size = ARRAY_SIZE(example_params_array);
+
+	/*
+	 * This allocates a struct kunit_resource, sets its data field to
+	 * ctx, and adds it to the kunit struct's resources list. Note that
+	 * this is test managed so we don't need to have a custom exit function
+	 * to free it.
+	 */
+	void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free,
+					  GFP_KERNEL, &ctx);
+
+	if (!data)
+		return -ENOMEM;
+	/* Pass the static param array information to the parent struct kunit. */
+	kunit_register_params_array(test, example_params_array, arr_size,
+				    example_param_array_get_desc);
+	return 0;
+}
+
+/*
+ * This is an example of a parameterized test that uses shared resources
+ * available from the struct kunit parent field of the kunit struct.
+ */
+static void example_params_test_with_init(struct kunit *test)
+{
+	int threshold;
+	struct kunit_resource *res;
+	const struct example_param *param = test->param_value;
+
+	/* By design, param pointer will not be NULL. */
+	KUNIT_ASSERT_NOT_NULL(test, param);
+
+	/* Here we access the parent pointer of the test to find the shared resource. */
+	res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL);
+
+	KUNIT_ASSERT_NOT_NULL(test, res);
+
+	/* Since the data field in kunit_resource is a void pointer we need to typecast it. */
+	threshold = *((int *)res->data);
+
+	/* Assert that the parameter is less than or equal to a certain threshold. */
+	KUNIT_ASSERT_LE(test, param->value, threshold);
+
+	/* This decreases the reference count after calling kunit_find_resource(). */
+	kunit_put_resource(res);
+}
+
 /*
  * Here we make a list of all the test cases we want to add to the test suite
  * below.
@@ -296,6 +406,8 @@ static struct kunit_case example_test_cases[] = {
 	KUNIT_CASE(example_static_stub_using_fn_ptr_test),
 	KUNIT_CASE(example_priv_test),
 	KUNIT_CASE_PARAM(example_params_test, example_gen_params),
+	KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, NULL,
+				   example_param_init, NULL),
 	KUNIT_CASE_SLOW(example_slow_test),
 	{}
 };
-- 
2.50.1.552.g942d659e1b-goog


  parent reply	other threads:[~2025-07-29 19:37 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-29 19:36 [PATCH 0/9] kunit: Refactor and extend KUnit's Marie Zhussupova
2025-07-29 19:36 ` [PATCH 1/9] kunit: Add parent kunit for parameterized test context Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-05 15:17   ` Rae Moar
2025-08-08 12:20     ` Marie Zhussupova
2025-07-29 19:36 ` [PATCH 2/9] kunit: Introduce param_init/exit for parameterized test shared context management Marie Zhussupova
2025-07-30 13:50   ` kernel test robot
2025-08-02  9:44   ` David Gow
2025-08-05 15:17   ` Rae Moar
2025-08-08 13:01     ` Marie Zhussupova
2025-07-29 19:36 ` [PATCH 3/9] kunit: Pass additional context to generate_params for parameterized testing Marie Zhussupova
2025-07-30 14:32   ` kernel test robot
2025-08-02  9:44   ` David Gow
2025-08-05 15:18   ` Rae Moar
2025-07-29 19:36 ` [PATCH 4/9] kcsan: test: Update parameter generator to new signature Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-07 11:11     ` Marco Elver
2025-07-29 19:36 ` [PATCH 5/9] drm/xe: " Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-07 10:59     ` Lucas De Marchi
2025-07-29 19:36 ` [PATCH 6/9] kunit: Enable direct registration of parameter arrays to a KUnit test Marie Zhussupova
2025-07-29 20:14   ` Rae Moar
2025-07-29 20:26     ` Rae Moar
2025-07-31 15:58   ` Dan Carpenter
2025-07-31 20:01     ` Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-05 15:18   ` Rae Moar
2025-08-08  1:22     ` Marie Zhussupova
2025-07-29 19:36 ` Marie Zhussupova [this message]
2025-07-30 12:25   ` [PATCH 7/9] kunit: Add example parameterized test with shared resources and direct static parameter array setup kernel test robot
2025-08-02  9:44   ` David Gow
2025-08-06 17:50     ` Marie Zhussupova
2025-08-05 15:19   ` Rae Moar
2025-07-29 19:36 ` [PATCH 8/9] kunit: Add example parameterized test with direct dynamic " Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-05 15:19   ` Rae Moar
2025-07-29 19:36 ` [PATCH 9/9] Documentation: kunit: Document new parameterized test features Marie Zhussupova
2025-08-02  9:45   ` David Gow
2025-08-08 13:02     ` Marie Zhussupova
2025-08-05 15:19   ` Rae Moar
2025-08-08 13:05     ` Marie Zhussupova

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=20250729193647.3410634-8-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=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®