Skip to content

Commit c95e7c0

Browse files
jkrzyszt-intelshuahkh
authored andcommitted
kunit: Report the count of test suites in a module
According to KTAP specification[1], results should always start from a header that provides a TAP protocol version, followed by a test plan with a count of items to be executed. That pattern should be followed at each nesting level. In the current implementation of the top-most, i.e., test suite level, those rules apply only for test suites built into the kernel, executed and reported on boot. Results submitted to dmesg from kunit test modules loaded later are missing those top-level headers. As a consequence, if a kunit test module provides more than one test suite then, without the top level test plan, external tools that are parsing dmesg for kunit test output are not able to tell how many test suites should be expected and whether to continue parsing after complete output from the first test suite is collected. Submit the top-level headers also from the kunit test module notifier initialization callback. v3: Fix new name of a structure moved to kunit namespace not updated in executor_test functions ([email protected]). v2: Use kunit_exec_run_tests() (Mauro, Rae), but prevent it from emitting the headers when called on load of non-test modules. [1] https://docs.kernel.org/dev-tools/ktap.html# Signed-off-by: Janusz Krzysztofik <[email protected]> Cc: Mauro Carvalho Chehab <[email protected]> Cc: Rae Moar <[email protected]> Reviewed-by: Rae Moar <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 1c9fd08 commit c95e7c0

File tree

4 files changed

+60
-32
lines changed

4 files changed

+60
-32
lines changed

include/kunit/test.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ struct kunit_suite {
256256
int suite_init_err;
257257
};
258258

259+
/* Stores an array of suites, end points one past the end */
260+
struct kunit_suite_set {
261+
struct kunit_suite * const *start;
262+
struct kunit_suite * const *end;
263+
};
264+
259265
/**
260266
* struct kunit - represents a running instance of a test.
261267
*
@@ -317,6 +323,8 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
317323

318324
void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
319325

326+
void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
327+
320328
#if IS_BUILTIN(CONFIG_KUNIT)
321329
int kunit_run_all_tests(void);
322330
#else

lib/kunit/executor.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,7 @@ kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_
104104
static char *kunit_shutdown;
105105
core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
106106

107-
/* Stores an array of suites, end points one past the end */
108-
struct suite_set {
109-
struct kunit_suite * const *start;
110-
struct kunit_suite * const *end;
111-
};
112-
113-
static void kunit_free_suite_set(struct suite_set suite_set)
107+
static void kunit_free_suite_set(struct kunit_suite_set suite_set)
114108
{
115109
struct kunit_suite * const *suites;
116110

@@ -119,16 +113,17 @@ static void kunit_free_suite_set(struct suite_set suite_set)
119113
kfree(suite_set.start);
120114
}
121115

122-
static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
123-
const char *filter_glob,
124-
char *filters,
125-
char *filter_action,
126-
int *err)
116+
static struct kunit_suite_set
117+
kunit_filter_suites(const struct kunit_suite_set *suite_set,
118+
const char *filter_glob,
119+
char *filters,
120+
char *filter_action,
121+
int *err)
127122
{
128123
int i, j, k;
129124
int filter_count = 0;
130125
struct kunit_suite **copy, **copy_start, *filtered_suite, *new_filtered_suite;
131-
struct suite_set filtered = {NULL, NULL};
126+
struct kunit_suite_set filtered = {NULL, NULL};
132127
struct kunit_glob_filter parsed_glob;
133128
struct kunit_attr_filter *parsed_filters = NULL;
134129

@@ -230,17 +225,24 @@ static void kunit_handle_shutdown(void)
230225

231226
}
232227

233-
static void kunit_exec_run_tests(struct suite_set *suite_set)
228+
#endif
229+
230+
void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
234231
{
235232
size_t num_suites = suite_set->end - suite_set->start;
236233

237-
pr_info("KTAP version 1\n");
238-
pr_info("1..%zu\n", num_suites);
234+
if (builtin || num_suites) {
235+
pr_info("KTAP version 1\n");
236+
pr_info("1..%zu\n", num_suites);
237+
}
239238

240239
__kunit_test_suites_init(suite_set->start, num_suites);
241240
}
242241

243-
static void kunit_exec_list_tests(struct suite_set *suite_set, bool include_attr)
242+
#if IS_BUILTIN(CONFIG_KUNIT)
243+
244+
static void kunit_exec_list_tests(struct kunit_suite_set *suite_set,
245+
bool include_attr)
244246
{
245247
struct kunit_suite * const *suites;
246248
struct kunit_case *test_case;
@@ -265,7 +267,9 @@ static void kunit_exec_list_tests(struct suite_set *suite_set, bool include_attr
265267

266268
int kunit_run_all_tests(void)
267269
{
268-
struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end};
270+
struct kunit_suite_set suite_set = {
271+
__kunit_suites_start, __kunit_suites_end,
272+
};
269273
int err = 0;
270274
if (!kunit_enabled()) {
271275
pr_info("kunit: disabled\n");
@@ -282,7 +286,7 @@ int kunit_run_all_tests(void)
282286
}
283287

284288
if (!action_param)
285-
kunit_exec_run_tests(&suite_set);
289+
kunit_exec_run_tests(&suite_set, true);
286290
else if (strcmp(action_param, "list") == 0)
287291
kunit_exec_list_tests(&suite_set, false);
288292
else if (strcmp(action_param, "list_attr") == 0)

lib/kunit/executor_test.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ static void parse_filter_test(struct kunit *test)
4343
static void filter_suites_test(struct kunit *test)
4444
{
4545
struct kunit_suite *subsuite[3] = {NULL, NULL};
46-
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
47-
struct suite_set got;
46+
struct kunit_suite_set suite_set = {
47+
.start = subsuite, .end = &subsuite[2],
48+
};
49+
struct kunit_suite_set got;
4850
int err = 0;
4951

5052
subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases);
@@ -67,8 +69,10 @@ static void filter_suites_test(struct kunit *test)
6769
static void filter_suites_test_glob_test(struct kunit *test)
6870
{
6971
struct kunit_suite *subsuite[3] = {NULL, NULL};
70-
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
71-
struct suite_set got;
72+
struct kunit_suite_set suite_set = {
73+
.start = subsuite, .end = &subsuite[2],
74+
};
75+
struct kunit_suite_set got;
7276
int err = 0;
7377

7478
subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases);
@@ -94,8 +98,10 @@ static void filter_suites_test_glob_test(struct kunit *test)
9498
static void filter_suites_to_empty_test(struct kunit *test)
9599
{
96100
struct kunit_suite *subsuite[3] = {NULL, NULL};
97-
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
98-
struct suite_set got;
101+
struct kunit_suite_set suite_set = {
102+
.start = subsuite, .end = &subsuite[2],
103+
};
104+
struct kunit_suite_set got;
99105
int err = 0;
100106

101107
subsuite[0] = alloc_fake_suite(test, "suite1", dummy_test_cases);
@@ -144,8 +150,10 @@ static struct kunit_case dummy_attr_test_cases[] = {
144150
static void filter_attr_test(struct kunit *test)
145151
{
146152
struct kunit_suite *subsuite[3] = {NULL, NULL};
147-
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
148-
struct suite_set got;
153+
struct kunit_suite_set suite_set = {
154+
.start = subsuite, .end = &subsuite[2],
155+
};
156+
struct kunit_suite_set got;
149157
int err = 0;
150158

151159
subsuite[0] = alloc_fake_suite(test, "normal_suite", dummy_attr_test_cases);
@@ -179,8 +187,10 @@ static void filter_attr_test(struct kunit *test)
179187
static void filter_attr_empty_test(struct kunit *test)
180188
{
181189
struct kunit_suite *subsuite[3] = {NULL, NULL};
182-
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
183-
struct suite_set got;
190+
struct kunit_suite_set suite_set = {
191+
.start = subsuite, .end = &subsuite[2],
192+
};
193+
struct kunit_suite_set got;
184194
int err = 0;
185195

186196
subsuite[0] = alloc_fake_suite(test, "suite1", dummy_attr_test_cases);
@@ -197,8 +207,10 @@ static void filter_attr_empty_test(struct kunit *test)
197207
static void filter_attr_skip_test(struct kunit *test)
198208
{
199209
struct kunit_suite *subsuite[2] = {NULL};
200-
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[1]};
201-
struct suite_set got;
210+
struct kunit_suite_set suite_set = {
211+
.start = subsuite, .end = &subsuite[1],
212+
};
213+
struct kunit_suite_set got;
202214
int err = 0;
203215

204216
subsuite[0] = alloc_fake_suite(test, "suite", dummy_attr_test_cases);

lib/kunit/test.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,11 @@ EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
736736
#ifdef CONFIG_MODULES
737737
static void kunit_module_init(struct module *mod)
738738
{
739-
__kunit_test_suites_init(mod->kunit_suites, mod->num_kunit_suites);
739+
struct kunit_suite_set suite_set = {
740+
mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
741+
};
742+
743+
kunit_exec_run_tests(&suite_set, false);
740744
}
741745

742746
static void kunit_module_exit(struct module *mod)

0 commit comments

Comments
 (0)