Skip to content

Commit a03e3ca

Browse files
Marie Zhussupovashuahkh
authored andcommitted
kunit: Add example parameterized test with shared resource management using the Resource API
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. Link: https://lore.kernel.org/r/20250826091341.1427123-6-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>
1 parent b820b90 commit a03e3ca

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

lib/kunit/kunit-example-test.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,117 @@ static void example_slow_test(struct kunit *test)
277277
KUNIT_EXPECT_EQ(test, 1 + 1, 2);
278278
}
279279

280+
/*
281+
* This custom function allocates memory and sets the information we want
282+
* stored in the kunit_resource->data field.
283+
*/
284+
static int example_resource_init(struct kunit_resource *res, void *context)
285+
{
286+
int *info = kmalloc(sizeof(*info), GFP_KERNEL);
287+
288+
if (!info)
289+
return -ENOMEM;
290+
*info = *(int *)context;
291+
res->data = info;
292+
return 0;
293+
}
294+
295+
/*
296+
* This function deallocates memory for the kunit_resource->data field.
297+
*/
298+
static void example_resource_free(struct kunit_resource *res)
299+
{
300+
kfree(res->data);
301+
}
302+
303+
/*
304+
* This match function is invoked by kunit_find_resource() to locate
305+
* a test resource based on certain criteria.
306+
*/
307+
static bool example_resource_alloc_match(struct kunit *test,
308+
struct kunit_resource *res,
309+
void *match_data)
310+
{
311+
return res->data && res->free == example_resource_free;
312+
}
313+
314+
/*
315+
* This is an example of a function that provides a description for each of the
316+
* parameters in a parameterized test.
317+
*/
318+
static void example_param_array_get_desc(struct kunit *test, const void *p, char *desc)
319+
{
320+
const struct example_param *param = p;
321+
322+
snprintf(desc, KUNIT_PARAM_DESC_SIZE,
323+
"example check if %d is less than or equal to 3", param->value);
324+
}
325+
326+
/*
327+
* This function gets passed in the parameterized test context i.e. the
328+
* struct kunit belonging to the parameterized test. You can use this function
329+
* to add resources you want shared across the whole parameterized test or
330+
* for additional setup.
331+
*/
332+
static int example_param_init(struct kunit *test)
333+
{
334+
int ctx = 3; /* Data to be stored. */
335+
size_t arr_size = ARRAY_SIZE(example_params_array);
336+
337+
/*
338+
* This allocates a struct kunit_resource, sets its data field to
339+
* ctx, and adds it to the struct kunit's resources list. Note that
340+
* this is parameterized test managed. So, it doesn't need to have
341+
* a custom exit function to deallocation as it will get cleaned up at
342+
* the end of the parameterized test.
343+
*/
344+
void *data = kunit_alloc_resource(test, example_resource_init, example_resource_free,
345+
GFP_KERNEL, &ctx);
346+
347+
if (!data)
348+
return -ENOMEM;
349+
/*
350+
* Pass the parameter array information to the parameterized test context
351+
* struct kunit. Note that you will need to provide kunit_array_gen_params()
352+
* as the generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering
353+
* a parameter array this route.
354+
*/
355+
kunit_register_params_array(test, example_params_array, arr_size,
356+
example_param_array_get_desc);
357+
return 0;
358+
}
359+
360+
/*
361+
* This is an example of a test that uses shared resources available in the
362+
* parameterized test context.
363+
*/
364+
static void example_params_test_with_init(struct kunit *test)
365+
{
366+
int threshold;
367+
struct kunit_resource *res;
368+
const struct example_param *param = test->param_value;
369+
370+
/* By design, param pointer will not be NULL. */
371+
KUNIT_ASSERT_NOT_NULL(test, param);
372+
373+
/*
374+
* Here we pass test->parent to search for shared resources in the
375+
* parameterized test context.
376+
*/
377+
res = kunit_find_resource(test->parent, example_resource_alloc_match, NULL);
378+
379+
KUNIT_ASSERT_NOT_NULL(test, res);
380+
381+
/* Since kunit_resource->data is a void pointer we need to typecast it. */
382+
threshold = *((int *)res->data);
383+
384+
/* Assert that the parameter is less than or equal to a certain threshold. */
385+
KUNIT_ASSERT_LE(test, param->value, threshold);
386+
387+
/* This decreases the reference count after calling kunit_find_resource(). */
388+
kunit_put_resource(res);
389+
}
390+
280391
/*
281392
* Here we make a list of all the test cases we want to add to the test suite
282393
* below.
@@ -296,6 +407,8 @@ static struct kunit_case example_test_cases[] = {
296407
KUNIT_CASE(example_static_stub_using_fn_ptr_test),
297408
KUNIT_CASE(example_priv_test),
298409
KUNIT_CASE_PARAM(example_params_test, example_gen_params),
410+
KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params,
411+
example_param_init, NULL),
299412
KUNIT_CASE_SLOW(example_slow_test),
300413
{}
301414
};

0 commit comments

Comments
 (0)