Skip to content

Commit 725aca9

Browse files
alan-maguireshuahkh
authored andcommitted
kunit: add support for named resources
The kunit resources API allows for custom initialization and cleanup code (init/fini); here a new resource add function sets the "struct kunit_resource" "name" field, and calls the standard add function. Having a simple way to name resources is useful in cases such as multithreaded tests where a set of resources are shared among threads; a pointer to the "struct kunit *" test state then is all that is needed to retrieve and use named resources. Support is provided to add, find and destroy named resources; the latter two are simply wrappers that use a "match-by-name" callback. If an attempt to add a resource with a name that already exists is made kunit_add_named_resource() will return -EEXIST. Signed-off-by: Alan Maguire <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent d4cdd14 commit 725aca9

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

include/kunit/test.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);
7272
* return kunit_alloc_resource(test, kunit_kmalloc_init,
7373
* kunit_kmalloc_free, &params);
7474
* }
75+
*
76+
* Resources can also be named, with lookup/removal done on a name
77+
* basis also. kunit_add_named_resource(), kunit_find_named_resource()
78+
* and kunit_destroy_named_resource(). Resource names must be
79+
* unique within the test instance.
7580
*/
7681
struct kunit_resource {
7782
void *data;
83+
const char *name; /* optional name */
7884

7985
/* private: internal use only. */
8086
kunit_resource_free_t free;
@@ -344,6 +350,21 @@ int kunit_add_resource(struct kunit *test,
344350
kunit_resource_free_t free,
345351
struct kunit_resource *res,
346352
void *data);
353+
354+
/**
355+
* kunit_add_named_resource() - Add a named *test managed resource*.
356+
* @test: The test context object.
357+
* @init: a user-supplied function to initialize the resource data, if needed.
358+
* @free: a user-supplied function to free the resource data, if needed.
359+
* @name_data: name and data to be set for resource.
360+
*/
361+
int kunit_add_named_resource(struct kunit *test,
362+
kunit_resource_init_t init,
363+
kunit_resource_free_t free,
364+
struct kunit_resource *res,
365+
const char *name,
366+
void *data);
367+
347368
/**
348369
* kunit_alloc_resource() - Allocates a *test managed resource*.
349370
* @test: The test context object.
@@ -398,6 +419,19 @@ static inline bool kunit_resource_instance_match(struct kunit *test,
398419
return res->data == match_data;
399420
}
400421

422+
/**
423+
* kunit_resource_name_match() - Match a resource with the same name.
424+
* @test: Test case to which the resource belongs.
425+
* @res: The resource.
426+
* @match_name: The name to match against.
427+
*/
428+
static inline bool kunit_resource_name_match(struct kunit *test,
429+
struct kunit_resource *res,
430+
void *match_name)
431+
{
432+
return res->name && strcmp(res->name, match_name) == 0;
433+
}
434+
401435
/**
402436
* kunit_find_resource() - Find a resource using match function/data.
403437
* @test: Test case to which the resource belongs.
@@ -426,6 +460,19 @@ kunit_find_resource(struct kunit *test,
426460
return found;
427461
}
428462

463+
/**
464+
* kunit_find_named_resource() - Find a resource using match name.
465+
* @test: Test case to which the resource belongs.
466+
* @name: match name.
467+
*/
468+
static inline struct kunit_resource *
469+
kunit_find_named_resource(struct kunit *test,
470+
const char *name)
471+
{
472+
return kunit_find_resource(test, kunit_resource_name_match,
473+
(void *)name);
474+
}
475+
429476
/**
430477
* kunit_destroy_resource() - Find a kunit_resource and destroy it.
431478
* @test: Test case to which the resource belongs.
@@ -439,6 +486,13 @@ int kunit_destroy_resource(struct kunit *test,
439486
kunit_resource_match_t match,
440487
void *match_data);
441488

489+
static inline int kunit_destroy_named_resource(struct kunit *test,
490+
const char *name)
491+
{
492+
return kunit_destroy_resource(test, kunit_resource_name_match,
493+
(void *)name);
494+
}
495+
442496
/**
443497
* kunit_remove_resource: remove resource from resource list associated with
444498
* test.

lib/kunit/kunit-test.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,42 @@ static void kunit_resource_test_static(struct kunit *test)
325325
KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
326326
}
327327

328+
static void kunit_resource_test_named(struct kunit *test)
329+
{
330+
struct kunit_resource res1, res2, *found = NULL;
331+
struct kunit_test_resource_context ctx;
332+
333+
KUNIT_EXPECT_EQ(test,
334+
kunit_add_named_resource(test, NULL, NULL, &res1,
335+
"resource_1", &ctx),
336+
0);
337+
KUNIT_EXPECT_PTR_EQ(test, res1.data, (void *)&ctx);
338+
339+
KUNIT_EXPECT_EQ(test,
340+
kunit_add_named_resource(test, NULL, NULL, &res1,
341+
"resource_1", &ctx),
342+
-EEXIST);
343+
344+
KUNIT_EXPECT_EQ(test,
345+
kunit_add_named_resource(test, NULL, NULL, &res2,
346+
"resource_2", &ctx),
347+
0);
348+
349+
found = kunit_find_named_resource(test, "resource_1");
350+
351+
KUNIT_EXPECT_PTR_EQ(test, found, &res1);
352+
353+
if (found)
354+
kunit_put_resource(&res1);
355+
356+
KUNIT_EXPECT_EQ(test, kunit_destroy_named_resource(test, "resource_2"),
357+
0);
358+
359+
kunit_cleanup(test);
360+
361+
KUNIT_EXPECT_TRUE(test, list_empty(&test->resources));
362+
}
363+
328364
static int kunit_resource_test_init(struct kunit *test)
329365
{
330366
struct kunit_test_resource_context *ctx =
@@ -354,6 +390,7 @@ static struct kunit_case kunit_resource_test_cases[] = {
354390
KUNIT_CASE(kunit_resource_test_cleanup_resources),
355391
KUNIT_CASE(kunit_resource_test_proper_free_ordering),
356392
KUNIT_CASE(kunit_resource_test_static),
393+
KUNIT_CASE(kunit_resource_test_named),
357394
{}
358395
};
359396

lib/kunit/test.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,30 @@ int kunit_add_resource(struct kunit *test,
441441
}
442442
EXPORT_SYMBOL_GPL(kunit_add_resource);
443443

444+
int kunit_add_named_resource(struct kunit *test,
445+
kunit_resource_init_t init,
446+
kunit_resource_free_t free,
447+
struct kunit_resource *res,
448+
const char *name,
449+
void *data)
450+
{
451+
struct kunit_resource *existing;
452+
453+
if (!name)
454+
return -EINVAL;
455+
456+
existing = kunit_find_named_resource(test, name);
457+
if (existing) {
458+
kunit_put_resource(existing);
459+
return -EEXIST;
460+
}
461+
462+
res->name = name;
463+
464+
return kunit_add_resource(test, init, free, res, data);
465+
}
466+
EXPORT_SYMBOL_GPL(kunit_add_named_resource);
467+
444468
struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
445469
kunit_resource_init_t init,
446470
kunit_resource_free_t free,

0 commit comments

Comments
 (0)