Commit | Line | Data |
---|---|---|
aac35468 AM |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
b6d5799b | 3 | #include <linux/reboot.h> |
aac35468 | 4 | #include <kunit/test.h> |
5d31f71e DL |
5 | #include <linux/glob.h> |
6 | #include <linux/moduleparam.h> | |
aac35468 AM |
7 | |
8 | /* | |
9 | * These symbols point to the .kunit_test_suites section and are defined in | |
10 | * include/asm-generic/vmlinux.lds.h, and consequently must be extern. | |
11 | */ | |
e5857d39 DL |
12 | extern struct kunit_suite * const __kunit_suites_start[]; |
13 | extern struct kunit_suite * const __kunit_suites_end[]; | |
aac35468 AM |
14 | |
15 | #if IS_BUILTIN(CONFIG_KUNIT) | |
16 | ||
1d71307a | 17 | static char *filter_glob_param; |
9c6b0e1d DL |
18 | static char *action_param; |
19 | ||
1d71307a | 20 | module_param_named(filter_glob, filter_glob_param, charp, 0); |
5d31f71e | 21 | MODULE_PARM_DESC(filter_glob, |
a127b154 | 22 | "Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test"); |
9c6b0e1d DL |
23 | module_param_named(action, action_param, charp, 0); |
24 | MODULE_PARM_DESC(action, | |
25 | "Changes KUnit executor behavior, valid values are:\n" | |
26 | "<none>: run the tests like normal\n" | |
27 | "'list' to list test names instead of running them.\n"); | |
a127b154 DL |
28 | |
29 | /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */ | |
30 | struct kunit_test_filter { | |
31 | char *suite_glob; | |
32 | char *test_glob; | |
33 | }; | |
34 | ||
35 | /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */ | |
36 | static void kunit_parse_filter_glob(struct kunit_test_filter *parsed, | |
37 | const char *filter_glob) | |
38 | { | |
39 | const int len = strlen(filter_glob); | |
40 | const char *period = strchr(filter_glob, '.'); | |
41 | ||
42 | if (!period) { | |
cd94fbc2 | 43 | parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL); |
a127b154 DL |
44 | parsed->test_glob = NULL; |
45 | strcpy(parsed->suite_glob, filter_glob); | |
46 | return; | |
47 | } | |
48 | ||
49 | parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL); | |
50 | parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL); | |
51 | ||
52 | strncpy(parsed->suite_glob, filter_glob, period - filter_glob); | |
53 | strncpy(parsed->test_glob, period + 1, len - (period - filter_glob)); | |
54 | } | |
55 | ||
56 | /* Create a copy of suite with only tests that match test_glob. */ | |
57 | static struct kunit_suite * | |
d2fbdde8 | 58 | kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob) |
a127b154 DL |
59 | { |
60 | int n = 0; | |
61 | struct kunit_case *filtered, *test_case; | |
62 | struct kunit_suite *copy; | |
63 | ||
64 | kunit_suite_for_each_test_case(suite, test_case) { | |
65 | if (!test_glob || glob_match(test_glob, test_case->name)) | |
66 | ++n; | |
67 | } | |
68 | ||
69 | if (n == 0) | |
70 | return NULL; | |
71 | ||
d2fbdde8 | 72 | copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); |
a02353f4 DL |
73 | if (!copy) |
74 | return ERR_PTR(-ENOMEM); | |
a127b154 DL |
75 | |
76 | filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); | |
94681e28 DG |
77 | if (!filtered) { |
78 | kfree(copy); | |
a02353f4 | 79 | return ERR_PTR(-ENOMEM); |
94681e28 | 80 | } |
a127b154 DL |
81 | |
82 | n = 0; | |
83 | kunit_suite_for_each_test_case(suite, test_case) { | |
84 | if (!test_glob || glob_match(test_glob, test_case->name)) | |
85 | filtered[n++] = *test_case; | |
86 | } | |
87 | ||
88 | copy->test_cases = filtered; | |
89 | return copy; | |
90 | } | |
5d31f71e | 91 | |
b6d5799b DG |
92 | static char *kunit_shutdown; |
93 | core_param(kunit_shutdown, kunit_shutdown, charp, 0644); | |
94 | ||
e5857d39 | 95 | /* Stores an array of suites, end points one past the end */ |
5d31f71e | 96 | struct suite_set { |
e5857d39 DL |
97 | struct kunit_suite * const *start; |
98 | struct kunit_suite * const *end; | |
5d31f71e DL |
99 | }; |
100 | ||
a127b154 DL |
101 | static void kunit_free_suite_set(struct suite_set suite_set) |
102 | { | |
e5857d39 | 103 | struct kunit_suite * const *suites; |
a127b154 DL |
104 | |
105 | for (suites = suite_set.start; suites < suite_set.end; suites++) | |
e5857d39 | 106 | kfree(*suites); |
a127b154 DL |
107 | kfree(suite_set.start); |
108 | } | |
109 | ||
1d71307a | 110 | static struct suite_set kunit_filter_suites(const struct suite_set *suite_set, |
a02353f4 DL |
111 | const char *filter_glob, |
112 | int *err) | |
5d31f71e DL |
113 | { |
114 | int i; | |
e5857d39 | 115 | struct kunit_suite **copy, *filtered_suite; |
5d31f71e | 116 | struct suite_set filtered; |
a127b154 | 117 | struct kunit_test_filter filter; |
5d31f71e | 118 | |
1d71307a | 119 | const size_t max = suite_set->end - suite_set->start; |
5d31f71e DL |
120 | |
121 | copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL); | |
122 | filtered.start = copy; | |
123 | if (!copy) { /* won't be able to run anything, return an empty set */ | |
124 | filtered.end = copy; | |
125 | return filtered; | |
126 | } | |
127 | ||
a127b154 DL |
128 | kunit_parse_filter_glob(&filter, filter_glob); |
129 | ||
e5857d39 DL |
130 | for (i = 0; &suite_set->start[i] != suite_set->end; i++) { |
131 | if (!glob_match(filter.suite_glob, suite_set->start[i]->name)) | |
132 | continue; | |
133 | ||
134 | filtered_suite = kunit_filter_tests(suite_set->start[i], filter.test_glob); | |
135 | if (IS_ERR(filtered_suite)) { | |
136 | *err = PTR_ERR(filtered_suite); | |
a02353f4 DL |
137 | return filtered; |
138 | } | |
e5857d39 DL |
139 | if (!filtered_suite) |
140 | continue; | |
141 | ||
142 | *copy++ = filtered_suite; | |
5d31f71e DL |
143 | } |
144 | filtered.end = copy; | |
a127b154 DL |
145 | |
146 | kfree(filter.suite_glob); | |
147 | kfree(filter.test_glob); | |
5d31f71e DL |
148 | return filtered; |
149 | } | |
150 | ||
b6d5799b DG |
151 | static void kunit_handle_shutdown(void) |
152 | { | |
153 | if (!kunit_shutdown) | |
154 | return; | |
155 | ||
156 | if (!strcmp(kunit_shutdown, "poweroff")) | |
157 | kernel_power_off(); | |
158 | else if (!strcmp(kunit_shutdown, "halt")) | |
159 | kernel_halt(); | |
160 | else if (!strcmp(kunit_shutdown, "reboot")) | |
161 | kernel_restart(NULL); | |
162 | ||
163 | } | |
164 | ||
9c6b0e1d | 165 | static void kunit_exec_run_tests(struct suite_set *suite_set) |
aac35468 | 166 | { |
e5857d39 | 167 | size_t num_suites = suite_set->end - suite_set->start; |
9c6b0e1d | 168 | |
6c738b52 | 169 | pr_info("KTAP version 1\n"); |
e5857d39 | 170 | pr_info("1..%zu\n", num_suites); |
9c6b0e1d | 171 | |
e5857d39 | 172 | __kunit_test_suites_init(suite_set->start, num_suites); |
9c6b0e1d DL |
173 | } |
174 | ||
175 | static void kunit_exec_list_tests(struct suite_set *suite_set) | |
176 | { | |
e5857d39 | 177 | struct kunit_suite * const *suites; |
9c6b0e1d DL |
178 | struct kunit_case *test_case; |
179 | ||
6c738b52 RM |
180 | /* Hack: print a ktap header so kunit.py can find the start of KUnit output. */ |
181 | pr_info("KTAP version 1\n"); | |
9c6b0e1d DL |
182 | |
183 | for (suites = suite_set->start; suites < suite_set->end; suites++) | |
e5857d39 DL |
184 | kunit_suite_for_each_test_case((*suites), test_case) { |
185 | pr_info("%s.%s\n", (*suites)->name, test_case->name); | |
9c6b0e1d DL |
186 | } |
187 | } | |
188 | ||
189 | int kunit_run_all_tests(void) | |
190 | { | |
e5857d39 | 191 | struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end}; |
1b11063d | 192 | int err = 0; |
d20a6ba5 JF |
193 | if (!kunit_enabled()) { |
194 | pr_info("kunit: disabled\n"); | |
195 | goto out; | |
196 | } | |
aac35468 | 197 | |
a02353f4 DL |
198 | if (filter_glob_param) { |
199 | suite_set = kunit_filter_suites(&suite_set, filter_glob_param, &err); | |
200 | if (err) { | |
201 | pr_err("kunit executor: error filtering suites: %d\n", err); | |
1b11063d | 202 | goto out; |
a02353f4 DL |
203 | } |
204 | } | |
5d31f71e | 205 | |
9c6b0e1d DL |
206 | if (!action_param) |
207 | kunit_exec_run_tests(&suite_set); | |
208 | else if (strcmp(action_param, "list") == 0) | |
209 | kunit_exec_list_tests(&suite_set); | |
210 | else | |
211 | pr_err("kunit executor: unknown action '%s'\n", action_param); | |
45dcbb6f | 212 | |
e5857d39 | 213 | if (filter_glob_param) { /* a copy was made of each suite */ |
a127b154 | 214 | kunit_free_suite_set(suite_set); |
5d31f71e | 215 | } |
aac35468 | 216 | |
1b11063d DL |
217 | out: |
218 | kunit_handle_shutdown(); | |
219 | return err; | |
aac35468 AM |
220 | } |
221 | ||
1d71307a DL |
222 | #if IS_BUILTIN(CONFIG_KUNIT_TEST) |
223 | #include "executor_test.c" | |
224 | #endif | |
225 | ||
aac35468 | 226 | #endif /* IS_BUILTIN(CONFIG_KUNIT) */ |