summaryrefslogtreecommitdiff
path: root/include/kunit
diff options
context:
space:
mode:
authorAlan Maguire <alan.maguire@oracle.com>2020-05-29 22:46:21 +0100
committerShuah Khan <skhan@linuxfoundation.org>2020-06-26 14:12:00 -0600
commit725aca9585956676687c4cb803e88f770b0df2b2 (patch)
treed12f4d1586c07f91dec8596e07b17f22c30dad14 /include/kunit
parentd4cdd146d0db900b2eb6c2d28cba719b3bf0a928 (diff)
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 <alan.maguire@oracle.com> Reviewed-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Diffstat (limited to 'include/kunit')
-rw-r--r--include/kunit/test.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/include/kunit/test.h b/include/kunit/test.h
index f9b914ebfd75..59f3144f009a 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -72,9 +72,15 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);
* return kunit_alloc_resource(test, kunit_kmalloc_init,
* kunit_kmalloc_free, &params);
* }
+ *
+ * Resources can also be named, with lookup/removal done on a name
+ * basis also. kunit_add_named_resource(), kunit_find_named_resource()
+ * and kunit_destroy_named_resource(). Resource names must be
+ * unique within the test instance.
*/
struct kunit_resource {
void *data;
+ const char *name; /* optional name */
/* private: internal use only. */
kunit_resource_free_t free;
@@ -344,6 +350,21 @@ int kunit_add_resource(struct kunit *test,
kunit_resource_free_t free,
struct kunit_resource *res,
void *data);
+
+/**
+ * kunit_add_named_resource() - Add a named *test managed resource*.
+ * @test: The test context object.
+ * @init: a user-supplied function to initialize the resource data, if needed.
+ * @free: a user-supplied function to free the resource data, if needed.
+ * @name_data: name and data to be set for resource.
+ */
+int kunit_add_named_resource(struct kunit *test,
+ kunit_resource_init_t init,
+ kunit_resource_free_t free,
+ struct kunit_resource *res,
+ const char *name,
+ void *data);
+
/**
* kunit_alloc_resource() - Allocates a *test managed resource*.
* @test: The test context object.
@@ -399,6 +420,19 @@ static inline bool kunit_resource_instance_match(struct kunit *test,
}
/**
+ * kunit_resource_name_match() - Match a resource with the same name.
+ * @test: Test case to which the resource belongs.
+ * @res: The resource.
+ * @match_name: The name to match against.
+ */
+static inline bool kunit_resource_name_match(struct kunit *test,
+ struct kunit_resource *res,
+ void *match_name)
+{
+ return res->name && strcmp(res->name, match_name) == 0;
+}
+
+/**
* kunit_find_resource() - Find a resource using match function/data.
* @test: Test case to which the resource belongs.
* @match: match function to be applied to resources/match data.
@@ -427,6 +461,19 @@ kunit_find_resource(struct kunit *test,
}
/**
+ * kunit_find_named_resource() - Find a resource using match name.
+ * @test: Test case to which the resource belongs.
+ * @name: match name.
+ */
+static inline struct kunit_resource *
+kunit_find_named_resource(struct kunit *test,
+ const char *name)
+{
+ return kunit_find_resource(test, kunit_resource_name_match,
+ (void *)name);
+}
+
+/**
* kunit_destroy_resource() - Find a kunit_resource and destroy it.
* @test: Test case to which the resource belongs.
* @match: Match function. Returns whether a given resource matches @match_data.
@@ -439,6 +486,13 @@ int kunit_destroy_resource(struct kunit *test,
kunit_resource_match_t match,
void *match_data);
+static inline int kunit_destroy_named_resource(struct kunit *test,
+ const char *name)
+{
+ return kunit_destroy_resource(test, kunit_resource_name_match,
+ (void *)name);
+}
+
/**
* kunit_remove_resource: remove resource from resource list associated with
* test.