Add example_params_test_with_init() to illustrate how to manage
shared resources across a parameterized KUnit test. 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 pass a parameter array
to the parameterized test context via kunit_register_params_array()
and leveraging the Resource API for shared resource management.
Signed-off-by: Marie Zhussupova <marievic@google.com>
---
Changes in v2:
- kunit_array_gen_params() is now explicitly passed to
KUNIT_CASE_PARAM_WITH_INIT() to be consistent with
a parameterized test being defined by the existence
of the generate_params() function.
- The comments were edited to be more concise.
- The patch header was changed to reflect that this example
test's intent is more aligned with showcasing using the
Resource API for shared resource management.
- 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.
---
lib/kunit/kunit-example-test.c | 118 +++++++++++++++++++++++++++++++++
1 file changed, 118 insertions(+)
diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c
index 3056d6bc705d..f2819ee58965 100644
--- a/lib/kunit/kunit-example-test.c
+++ b/lib/kunit/kunit-example-test.c
@@ -277,6 +277,122 @@ static void example_slow_test(struct kunit *test)
KUNIT_EXPECT_EQ(test, 1 + 1, 2);
}
+/*
+ * This custom function allocates memory and sets the information we want
+ * stored in the kunit_resource->data field.
+ */
+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.
+ */
+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 certain criteria.
+ */
+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 in a parameterized test.
+ */
+static void example_param_array_get_desc(struct kunit *test, 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);
+}
+
+/*
+ * This function gets passed in the parameterized test context i.e. the
+ * struct kunit belonging to the parameterized test. You can use this function
+ * to add resources you want shared across the whole parameterized test or
+ * for additional setup.
+ */
+static int example_param_init(struct kunit *test)
+{
+ int ctx = 3; /* Data to be stored. */
+ size_t arr_size = ARRAY_SIZE(example_params_array);
+
+ /*
+ * This allocates a struct kunit_resource, sets its data field to
+ * ctx, and adds it to the struct kunit's resources list. Note that
+ * this is parameterized test managed. So, it doesn't need to have
+ * a custom exit function to deallocation as it will get cleaned up at
+ * the end of the parameterized test.
+ */
+ void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free,
+ GFP_KERNEL, &ctx);
+
+ if (!data)
+ return -ENOMEM;
+ /*
+ * Pass the parameter array information to the parameterized test context
+ * struct kunit. Note that you will need to provide kunit_array_gen_params()
+ * as the generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering
+ * a parameter array this route.
+ *
+ * Alternatively, since this is a static array we can also use
+ * KUNIT_CASE_PARAM_ARRAY(,DESC) to create a `*_gen_params()` function
+ * and pass that to KUNIT_CASE_PARAM_WITH_INIT() instead of registering
+ * the parameter array here.
+ */
+ kunit_register_params_array(test, example_params_array, arr_size,
+ example_param_array_get_desc);
+ return 0;
+}
+
+/*
+ * This is an example of a test that uses shared resources available in the
+ * parameterized test context.
+ */
+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 pass test->parent to search for shared resources in the
+ * parameterized test context.
+ */
+ res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL);
+
+ KUNIT_ASSERT_NOT_NULL(test, res);
+
+ /* Since kunit_resource->data 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 +412,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, kunit_array_gen_params,
+ example_param_init, NULL),
KUNIT_CASE_SLOW(example_slow_test),
{}
};
--
2.51.0.rc0.205.g4a044479a3-goog
On Tue, 12 Aug 2025 at 06:18, Marie Zhussupova <marievic@google.com> wrote: > > Add example_params_test_with_init() to illustrate how to manage > shared resources across a parameterized KUnit test. 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 pass a parameter array > to the parameterized test context via kunit_register_params_array() > and leveraging the Resource API for shared resource management. > > Signed-off-by: Marie Zhussupova <marievic@google.com> > --- This looks fine to me. One note below about one of the comments. Otherwise, Reviewed-by: David Gow <davidgow@google.com> Cheers, -- David > > Changes in v2: > > - kunit_array_gen_params() is now explicitly passed to > KUNIT_CASE_PARAM_WITH_INIT() to be consistent with > a parameterized test being defined by the existence > of the generate_params() function. > - The comments were edited to be more concise. > - The patch header was changed to reflect that this example > test's intent is more aligned with showcasing using the > Resource API for shared resource management. > - 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. > > --- > > lib/kunit/kunit-example-test.c | 118 +++++++++++++++++++++++++++++++++ > 1 file changed, 118 insertions(+) > > diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c > index 3056d6bc705d..f2819ee58965 100644 > --- a/lib/kunit/kunit-example-test.c > +++ b/lib/kunit/kunit-example-test.c > @@ -277,6 +277,122 @@ static void example_slow_test(struct kunit *test) > KUNIT_EXPECT_EQ(test, 1 + 1, 2); > } > > +/* > + * This custom function allocates memory and sets the information we want > + * stored in the kunit_resource->data field. > + */ > +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. > + */ > +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 certain criteria. > + */ > +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 in a parameterized test. > + */ > +static void example_param_array_get_desc(struct kunit *test, 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); > +} > + > +/* > + * This function gets passed in the parameterized test context i.e. the > + * struct kunit belonging to the parameterized test. You can use this function > + * to add resources you want shared across the whole parameterized test or > + * for additional setup. > + */ > +static int example_param_init(struct kunit *test) > +{ > + int ctx = 3; /* Data to be stored. */ > + size_t arr_size = ARRAY_SIZE(example_params_array); > + > + /* > + * This allocates a struct kunit_resource, sets its data field to > + * ctx, and adds it to the struct kunit's resources list. Note that > + * this is parameterized test managed. So, it doesn't need to have > + * a custom exit function to deallocation as it will get cleaned up at > + * the end of the parameterized test. > + */ > + void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free, > + GFP_KERNEL, &ctx); > + > + if (!data) > + return -ENOMEM; > + /* > + * Pass the parameter array information to the parameterized test context > + * struct kunit. Note that you will need to provide kunit_array_gen_params() > + * as the generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering > + * a parameter array this route. > + * > + * Alternatively, since this is a static array we can also use > + * KUNIT_CASE_PARAM_ARRAY(,DESC) to create a `*_gen_params()` function > + * and pass that to KUNIT_CASE_PARAM_WITH_INIT() instead of registering > + * the parameter array here. Maybe we should note that KUNIT_CASE_PARAM_ARRAY{,_DESC}() doesn't let us set an init function, so would be less useful here. > + */ > + kunit_register_params_array(test, example_params_array, arr_size, > + example_param_array_get_desc); > + return 0; > +} > + > +/* > + * This is an example of a test that uses shared resources available in the > + * parameterized test context. > + */ > +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 pass test->parent to search for shared resources in the > + * parameterized test context. > + */ > + res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL); > + > + KUNIT_ASSERT_NOT_NULL(test, res); > + > + /* Since kunit_resource->data 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 +412,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, kunit_array_gen_params, > + example_param_init, NULL), > KUNIT_CASE_SLOW(example_slow_test), > {} > }; > -- > 2.51.0.rc0.205.g4a044479a3-goog >
On Mon, Aug 11, 2025 at 6:18 PM Marie Zhussupova <marievic@google.com> wrote: > > Add example_params_test_with_init() to illustrate how to manage > shared resources across a parameterized KUnit test. 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 pass a parameter array > to the parameterized test context via kunit_register_params_array() > and leveraging the Resource API for shared resource management. > > Signed-off-by: Marie Zhussupova <marievic@google.com> > --- > > Changes in v2: > > - kunit_array_gen_params() is now explicitly passed to > KUNIT_CASE_PARAM_WITH_INIT() to be consistent with > a parameterized test being defined by the existence > of the generate_params() function. > - The comments were edited to be more concise. > - The patch header was changed to reflect that this example > test's intent is more aligned with showcasing using the > Resource API for shared resource management. > - 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. Hello! Thank you for adding this example test! As before, this test looks good to me: Reviewed-by: Rae Moar <rmoar@google.com> Thanks! -Rae > > --- > > lib/kunit/kunit-example-test.c | 118 +++++++++++++++++++++++++++++++++ > 1 file changed, 118 insertions(+) > > diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c > index 3056d6bc705d..f2819ee58965 100644 > --- a/lib/kunit/kunit-example-test.c > +++ b/lib/kunit/kunit-example-test.c > @@ -277,6 +277,122 @@ static void example_slow_test(struct kunit *test) > KUNIT_EXPECT_EQ(test, 1 + 1, 2); > } > > +/* > + * This custom function allocates memory and sets the information we want > + * stored in the kunit_resource->data field. > + */ > +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. > + */ > +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 certain criteria. > + */ > +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 in a parameterized test. > + */ > +static void example_param_array_get_desc(struct kunit *test, 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); > +} > + > +/* > + * This function gets passed in the parameterized test context i.e. the > + * struct kunit belonging to the parameterized test. You can use this function > + * to add resources you want shared across the whole parameterized test or > + * for additional setup. > + */ > +static int example_param_init(struct kunit *test) > +{ > + int ctx = 3; /* Data to be stored. */ > + size_t arr_size = ARRAY_SIZE(example_params_array); > + > + /* > + * This allocates a struct kunit_resource, sets its data field to > + * ctx, and adds it to the struct kunit's resources list. Note that > + * this is parameterized test managed. So, it doesn't need to have > + * a custom exit function to deallocation as it will get cleaned up at > + * the end of the parameterized test. > + */ > + void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free, > + GFP_KERNEL, &ctx); > + > + if (!data) > + return -ENOMEM; > + /* > + * Pass the parameter array information to the parameterized test context > + * struct kunit. Note that you will need to provide kunit_array_gen_params() > + * as the generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering > + * a parameter array this route. > + * > + * Alternatively, since this is a static array we can also use > + * KUNIT_CASE_PARAM_ARRAY(,DESC) to create a `*_gen_params()` function > + * and pass that to KUNIT_CASE_PARAM_WITH_INIT() instead of registering > + * the parameter array here. > + */ > + kunit_register_params_array(test, example_params_array, arr_size, > + example_param_array_get_desc); > + return 0; > +} > + > +/* > + * This is an example of a test that uses shared resources available in the > + * parameterized test context. > + */ > +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 pass test->parent to search for shared resources in the > + * parameterized test context. > + */ > + res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL); > + > + KUNIT_ASSERT_NOT_NULL(test, res); > + > + /* Since kunit_resource->data 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 +412,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, kunit_array_gen_params, > + example_param_init, NULL), > KUNIT_CASE_SLOW(example_slow_test), > {} > }; > -- > 2.51.0.rc0.205.g4a044479a3-goog >
© 2016 - 2025 Red Hat, Inc.