[RFC v2 3/9] kunit: Add module attribute

From: Rae Moar
Date: Fri Jul 07 2023 - 17:10:08 EST


Add module attribute to the test attribute API. This attribute stores the
module name associated with the test using KBUILD_MODNAME.

The name of a test suite and the module name often do not match. A
reference to the module name associated with the suite could be extremely
helpful in running tests as modules without needing to check the codebase.

This attribute will be printed for each suite.

Signed-off-by: Rae Moar <rmoar@xxxxxxxxxx>
---

Changes: since v1:
- This is a new patch.

include/kunit/test.h | 13 ++++++++-----
lib/kunit/attributes.c | 28 +++++++++++++++++++++++++++-
2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index c255c98a58f7..cdfc3f42e899 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -129,6 +129,7 @@ struct kunit_case {

/* private: internal use only. */
enum kunit_status status;
+ char *module_name;
char *log;
};

@@ -153,7 +154,9 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
* &struct kunit_case object from it. See the documentation for
* &struct kunit_case for an example on how to use it.
*/
-#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
+#define KUNIT_CASE(test_name) \
+ { .run_case = test_name, .name = #test_name, \
+ .module_name = KBUILD_MODNAME}

/**
* KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
@@ -165,7 +168,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
*/
#define KUNIT_CASE_ATTR(test_name, attributes) \
{ .run_case = test_name, .name = #test_name, \
- .attr = attributes }
+ .attr = attributes, .module_name = KBUILD_MODNAME}

/**
* KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
@@ -176,7 +179,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)

#define KUNIT_CASE_SLOW(test_name) \
{ .run_case = test_name, .name = #test_name, \
- .attr.speed = KUNIT_SPEED_SLOW }
+ .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}

/**
* KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
@@ -197,7 +200,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
*/
#define KUNIT_CASE_PARAM(test_name, gen_params) \
{ .run_case = test_name, .name = #test_name, \
- .generate_params = gen_params }
+ .generate_params = gen_params, .module_name = KBUILD_MODNAME}

/**
* KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
@@ -211,7 +214,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
#define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \
{ .run_case = test_name, .name = #test_name, \
.generate_params = gen_params, \
- .attr = attributes }
+ .attr = attributes, .module_name = KBUILD_MODNAME}

/**
* struct kunit_suite - describes a related collection of &struct kunit_case
diff --git a/lib/kunit/attributes.c b/lib/kunit/attributes.c
index e97096dbb3b1..43dcb5de8b8f 100644
--- a/lib/kunit/attributes.c
+++ b/lib/kunit/attributes.c
@@ -61,6 +61,12 @@ static const char *attr_speed_to_string(void *attr, bool *to_free)
return attr_enum_to_string(attr, speed_str_list, to_free);
}

+static const char *attr_string_to_string(void *attr, bool *to_free)
+{
+ *to_free = false;
+ return (char *) attr;
+}
+
/* Get Attribute Methods */

static void *attr_speed_get(void *test_or_suite, bool is_test)
@@ -74,6 +80,18 @@ static void *attr_speed_get(void *test_or_suite, bool is_test)
return ((void *) suite->attr.speed);
}

+static void *attr_module_get(void *test_or_suite, bool is_test)
+{
+ struct kunit_suite *suite = is_test ? NULL : test_or_suite;
+ struct kunit_case *test = is_test ? test_or_suite : NULL;
+
+ // Suites get their module attribute from their first test_case
+ if (test)
+ return ((void *) test->module_name);
+ else
+ return ((void *) suite->test_cases[0].module_name);
+}
+
/* Attribute Struct Definitions */

static const struct kunit_attr speed_attr = {
@@ -84,9 +102,17 @@ static const struct kunit_attr speed_attr = {
.print = PRINT_ALWAYS,
};

+static const struct kunit_attr module_attr = {
+ .name = "module",
+ .get_attr = attr_module_get,
+ .to_string = attr_string_to_string,
+ .attr_default = (void *)"",
+ .print = PRINT_SUITE,
+};
+
/* List of all Test Attributes */

-static struct kunit_attr kunit_attr_list[] = {speed_attr};
+static struct kunit_attr kunit_attr_list[] = {speed_attr, module_attr};

/* Helper Functions to Access Attributes */

--
2.41.0.255.g8b1d071c50-goog