Skip to content

Commit 5d31f71

Browse files
dlatypovshuahkh
authored andcommitted
kunit: add kunit.filter_glob cmdline option to filter suites
E.g. specifying this would run suites with "list" in their name. kunit.filter_glob=list* Note: the executor prints out a TAP header that includes the number of suites we intend to run. So unless we want to report empty results for filtered-out suites, we need to do the filtering here in the executor. It's also probably better in the executor since we most likely don't want any filtering to apply to tests built as modules. This code does add a CONFIG_GLOB=y dependency for CONFIG_KUNIT=y. But the code seems light enough that it shouldn't be an issue. For now, we only filter on suite names so we don't have to create copies of the suites themselves, just the array (of arrays) holding them. The name is rather generic since in the future, we could consider extending it to a syntax like: kunit.filter_glob=<suite_glob>.<test_glob> E.g. to run all the del list tests kunit.filter_glob=list-kunit-test.*del* But at the moment, it's far easier to manually comment out test cases in test files as opposed to messing with sets of Kconfig entries to select specific suites. So even just doing this makes using kunit far less annoying. Signed-off-by: Daniel Latypov <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 65af9b9 commit 5d31f71

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

lib/kunit/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
menuconfig KUNIT
66
tristate "KUnit - Enable support for unit tests"
7+
select GLOB if KUNIT=y
78
help
89
Enables support for kernel unit tests (KUnit), a lightweight unit
910
testing and mocking framework for the Linux kernel. These tests are

lib/kunit/executor.c

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#include <kunit/test.h>
4+
#include <linux/glob.h>
5+
#include <linux/moduleparam.h>
46

57
/*
68
* These symbols point to the .kunit_test_suites section and are defined in
@@ -11,14 +13,81 @@ extern struct kunit_suite * const * const __kunit_suites_end[];
1113

1214
#if IS_BUILTIN(CONFIG_KUNIT)
1315

14-
static void kunit_print_tap_header(void)
16+
static char *filter_glob;
17+
module_param(filter_glob, charp, 0);
18+
MODULE_PARM_DESC(filter_glob,
19+
"Filter which KUnit test suites run at boot-time, e.g. list*");
20+
21+
static struct kunit_suite * const *
22+
kunit_filter_subsuite(struct kunit_suite * const * const subsuite)
23+
{
24+
int i, n = 0;
25+
struct kunit_suite **filtered;
26+
27+
n = 0;
28+
for (i = 0; subsuite[i] != NULL; ++i) {
29+
if (glob_match(filter_glob, subsuite[i]->name))
30+
++n;
31+
}
32+
33+
if (n == 0)
34+
return NULL;
35+
36+
filtered = kmalloc_array(n + 1, sizeof(*filtered), GFP_KERNEL);
37+
if (!filtered)
38+
return NULL;
39+
40+
n = 0;
41+
for (i = 0; subsuite[i] != NULL; ++i) {
42+
if (glob_match(filter_glob, subsuite[i]->name))
43+
filtered[n++] = subsuite[i];
44+
}
45+
filtered[n] = NULL;
46+
47+
return filtered;
48+
}
49+
50+
struct suite_set {
51+
struct kunit_suite * const * const *start;
52+
struct kunit_suite * const * const *end;
53+
};
54+
55+
static struct suite_set kunit_filter_suites(void)
56+
{
57+
int i;
58+
struct kunit_suite * const **copy, * const *filtered_subsuite;
59+
struct suite_set filtered;
60+
61+
const size_t max = __kunit_suites_end - __kunit_suites_start;
62+
63+
if (!filter_glob) {
64+
filtered.start = __kunit_suites_start;
65+
filtered.end = __kunit_suites_end;
66+
return filtered;
67+
}
68+
69+
copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
70+
filtered.start = copy;
71+
if (!copy) { /* won't be able to run anything, return an empty set */
72+
filtered.end = copy;
73+
return filtered;
74+
}
75+
76+
for (i = 0; i < max; ++i) {
77+
filtered_subsuite = kunit_filter_subsuite(__kunit_suites_start[i]);
78+
if (filtered_subsuite)
79+
*copy++ = filtered_subsuite;
80+
}
81+
filtered.end = copy;
82+
return filtered;
83+
}
84+
85+
static void kunit_print_tap_header(struct suite_set *suite_set)
1586
{
1687
struct kunit_suite * const * const *suites, * const *subsuite;
1788
int num_of_suites = 0;
1889

19-
for (suites = __kunit_suites_start;
20-
suites < __kunit_suites_end;
21-
suites++)
90+
for (suites = suite_set->start; suites < suite_set->end; suites++)
2291
for (subsuite = *suites; *subsuite != NULL; subsuite++)
2392
num_of_suites++;
2493

@@ -30,12 +99,18 @@ int kunit_run_all_tests(void)
3099
{
31100
struct kunit_suite * const * const *suites;
32101

33-
kunit_print_tap_header();
102+
struct suite_set suite_set = kunit_filter_suites();
103+
104+
kunit_print_tap_header(&suite_set);
105+
106+
for (suites = suite_set.start; suites < suite_set.end; suites++)
107+
__kunit_test_suites_init(*suites);
34108

35-
for (suites = __kunit_suites_start;
36-
suites < __kunit_suites_end;
37-
suites++)
38-
__kunit_test_suites_init(*suites);
109+
if (filter_glob) { /* a copy was made of each array */
110+
for (suites = suite_set.start; suites < suite_set.end; suites++)
111+
kfree(*suites);
112+
kfree(suite_set.start);
113+
}
39114

40115
return 0;
41116
}

0 commit comments

Comments
 (0)