Skip to content

Commit a00a727

Browse files
rmr167shuahkh
authored andcommitted
kunit: Add module attribute
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. Reviewed-by: David Gow <[email protected]> Signed-off-by: Rae Moar <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 02c2d0c commit a00a727

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

include/kunit/test.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ struct kunit_case {
131131

132132
/* private: internal use only. */
133133
enum kunit_status status;
134+
char *module_name;
134135
char *log;
135136
};
136137

@@ -155,7 +156,9 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
155156
* &struct kunit_case object from it. See the documentation for
156157
* &struct kunit_case for an example on how to use it.
157158
*/
158-
#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
159+
#define KUNIT_CASE(test_name) \
160+
{ .run_case = test_name, .name = #test_name, \
161+
.module_name = KBUILD_MODNAME}
159162

160163
/**
161164
* KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
@@ -167,7 +170,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
167170
*/
168171
#define KUNIT_CASE_ATTR(test_name, attributes) \
169172
{ .run_case = test_name, .name = #test_name, \
170-
.attr = attributes }
173+
.attr = attributes, .module_name = KBUILD_MODNAME}
171174

172175
/**
173176
* KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
@@ -178,7 +181,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
178181

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

183186
/**
184187
* KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
@@ -199,7 +202,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
199202
*/
200203
#define KUNIT_CASE_PARAM(test_name, gen_params) \
201204
{ .run_case = test_name, .name = #test_name, \
202-
.generate_params = gen_params }
205+
.generate_params = gen_params, .module_name = KBUILD_MODNAME}
203206

204207
/**
205208
* KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
@@ -213,7 +216,7 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
213216
#define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \
214217
{ .run_case = test_name, .name = #test_name, \
215218
.generate_params = gen_params, \
216-
.attr = attributes }
219+
.attr = attributes, .module_name = KBUILD_MODNAME}
217220

218221
/**
219222
* struct kunit_suite - describes a related collection of &struct kunit_case

lib/kunit/attributes.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ static const char *attr_speed_to_string(void *attr, bool *to_free)
6161
return attr_enum_to_string(attr, speed_str_list, to_free);
6262
}
6363

64+
static const char *attr_string_to_string(void *attr, bool *to_free)
65+
{
66+
*to_free = false;
67+
return (char *) attr;
68+
}
69+
6470
/* Get Attribute Methods */
6571

6672
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)
7480
return ((void *) suite->attr.speed);
7581
}
7682

83+
static void *attr_module_get(void *test_or_suite, bool is_test)
84+
{
85+
struct kunit_suite *suite = is_test ? NULL : test_or_suite;
86+
struct kunit_case *test = is_test ? test_or_suite : NULL;
87+
88+
// Suites get their module attribute from their first test_case
89+
if (test)
90+
return ((void *) test->module_name);
91+
else
92+
return ((void *) suite->test_cases[0].module_name);
93+
}
94+
7795
/* List of all Test Attributes */
7896

7997
static struct kunit_attr kunit_attr_list[] = {
@@ -84,6 +102,13 @@ static struct kunit_attr kunit_attr_list[] = {
84102
.attr_default = (void *)KUNIT_SPEED_NORMAL,
85103
.print = PRINT_ALWAYS,
86104
},
105+
{
106+
.name = "module",
107+
.get_attr = attr_module_get,
108+
.to_string = attr_string_to_string,
109+
.attr_default = (void *)"",
110+
.print = PRINT_SUITE,
111+
}
87112
};
88113

89114
/* Helper Functions to Access Attributes */

0 commit comments

Comments
 (0)