xref: /linux-6.15/lib/kunit/executor.c (revision abbf7381)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/reboot.h>
4 #include <kunit/test.h>
5 #include <kunit/attributes.h>
6 #include <linux/glob.h>
7 #include <linux/moduleparam.h>
8 
9 /*
10  * These symbols point to the .kunit_test_suites section and are defined in
11  * include/asm-generic/vmlinux.lds.h, and consequently must be extern.
12  */
13 extern struct kunit_suite * const __kunit_suites_start[];
14 extern struct kunit_suite * const __kunit_suites_end[];
15 
16 #if IS_BUILTIN(CONFIG_KUNIT)
17 
18 static char *filter_glob_param;
19 static char *action_param;
20 static char *filter_param;
21 static char *filter_action_param;
22 
23 module_param_named(filter_glob, filter_glob_param, charp, 0);
24 MODULE_PARM_DESC(filter_glob,
25 		"Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test");
26 module_param_named(action, action_param, charp, 0);
27 MODULE_PARM_DESC(action,
28 		 "Changes KUnit executor behavior, valid values are:\n"
29 		 "<none>: run the tests like normal\n"
30 		 "'list' to list test names instead of running them.\n"
31 		 "'list_attr' to list test names and attributes instead of running them.\n");
32 module_param_named(filter, filter_param, charp, 0);
33 MODULE_PARM_DESC(filter,
34 		"Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow");
35 module_param_named(filter_action, filter_action_param, charp, 0);
36 MODULE_PARM_DESC(filter_action,
37 		"Changes behavior of filtered tests using attributes, valid values are:\n"
38 		"<none>: do not run filtered tests as normal\n"
39 		"'skip': skip all filtered tests instead so tests will appear in output\n");
40 
41 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */
42 struct kunit_glob_filter {
43 	char *suite_glob;
44 	char *test_glob;
45 };
46 
47 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */
48 static void kunit_parse_glob_filter(struct kunit_glob_filter *parsed,
49 				    const char *filter_glob)
50 {
51 	const int len = strlen(filter_glob);
52 	const char *period = strchr(filter_glob, '.');
53 
54 	if (!period) {
55 		parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
56 		parsed->test_glob = NULL;
57 		strcpy(parsed->suite_glob, filter_glob);
58 		return;
59 	}
60 
61 	parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
62 	parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
63 
64 	strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
65 	strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
66 }
67 
68 /* Create a copy of suite with only tests that match test_glob. */
69 static struct kunit_suite *
70 kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_glob)
71 {
72 	int n = 0;
73 	struct kunit_case *filtered, *test_case;
74 	struct kunit_suite *copy;
75 
76 	kunit_suite_for_each_test_case(suite, test_case) {
77 		if (!test_glob || glob_match(test_glob, test_case->name))
78 			++n;
79 	}
80 
81 	if (n == 0)
82 		return NULL;
83 
84 	copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
85 	if (!copy)
86 		return ERR_PTR(-ENOMEM);
87 
88 	filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
89 	if (!filtered) {
90 		kfree(copy);
91 		return ERR_PTR(-ENOMEM);
92 	}
93 
94 	n = 0;
95 	kunit_suite_for_each_test_case(suite, test_case) {
96 		if (!test_glob || glob_match(test_glob, test_case->name))
97 			filtered[n++] = *test_case;
98 	}
99 
100 	copy->test_cases = filtered;
101 	return copy;
102 }
103 
104 static char *kunit_shutdown;
105 core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
106 
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)
114 {
115 	struct kunit_suite * const *suites;
116 
117 	for (suites = suite_set.start; suites < suite_set.end; suites++)
118 		kfree(*suites);
119 	kfree(suite_set.start);
120 }
121 
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)
127 {
128 	int i, j, k;
129 	int filter_count = 0;
130 	struct kunit_suite **copy, *filtered_suite, *new_filtered_suite;
131 	struct suite_set filtered;
132 	struct kunit_glob_filter parsed_glob;
133 	struct kunit_attr_filter *parsed_filters;
134 
135 	const size_t max = suite_set->end - suite_set->start;
136 
137 	copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
138 	filtered.start = copy;
139 	if (!copy) { /* won't be able to run anything, return an empty set */
140 		filtered.end = copy;
141 		return filtered;
142 	}
143 
144 	if (filter_glob)
145 		kunit_parse_glob_filter(&parsed_glob, filter_glob);
146 
147 	/* Parse attribute filters */
148 	if (filters) {
149 		filter_count = kunit_get_filter_count(filters);
150 		parsed_filters = kcalloc(filter_count + 1, sizeof(*parsed_filters), GFP_KERNEL);
151 		for (j = 0; j < filter_count; j++)
152 			parsed_filters[j] = kunit_next_attr_filter(&filters, err);
153 		if (*err)
154 			goto err;
155 	}
156 
157 	for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
158 		filtered_suite = suite_set->start[i];
159 		if (filter_glob) {
160 			if (!glob_match(parsed_glob.suite_glob, filtered_suite->name))
161 				continue;
162 			filtered_suite = kunit_filter_glob_tests(filtered_suite,
163 					parsed_glob.test_glob);
164 			if (IS_ERR(filtered_suite)) {
165 				*err = PTR_ERR(filtered_suite);
166 				goto err;
167 			}
168 		}
169 		if (filter_count) {
170 			for (k = 0; k < filter_count; k++) {
171 				new_filtered_suite = kunit_filter_attr_tests(filtered_suite,
172 						parsed_filters[k], filter_action, err);
173 
174 				/* Free previous copy of suite */
175 				if (k > 0 || filter_glob) {
176 					kfree(filtered_suite->test_cases);
177 					kfree(filtered_suite);
178 				}
179 
180 				filtered_suite = new_filtered_suite;
181 
182 				if (*err)
183 					goto err;
184 				if (IS_ERR(filtered_suite)) {
185 					*err = PTR_ERR(filtered_suite);
186 					goto err;
187 				}
188 				if (!filtered_suite)
189 					break;
190 			}
191 		}
192 
193 		if (!filtered_suite)
194 			continue;
195 
196 		*copy++ = filtered_suite;
197 	}
198 	filtered.end = copy;
199 
200 err:
201 	if (*err)
202 		kfree(copy);
203 
204 	if (filter_glob) {
205 		kfree(parsed_glob.suite_glob);
206 		kfree(parsed_glob.test_glob);
207 	}
208 
209 	if (filter_count)
210 		kfree(parsed_filters);
211 
212 	return filtered;
213 }
214 
215 static void kunit_handle_shutdown(void)
216 {
217 	if (!kunit_shutdown)
218 		return;
219 
220 	if (!strcmp(kunit_shutdown, "poweroff"))
221 		kernel_power_off();
222 	else if (!strcmp(kunit_shutdown, "halt"))
223 		kernel_halt();
224 	else if (!strcmp(kunit_shutdown, "reboot"))
225 		kernel_restart(NULL);
226 
227 }
228 
229 static void kunit_exec_run_tests(struct suite_set *suite_set)
230 {
231 	size_t num_suites = suite_set->end - suite_set->start;
232 
233 	pr_info("KTAP version 1\n");
234 	pr_info("1..%zu\n", num_suites);
235 
236 	__kunit_test_suites_init(suite_set->start, num_suites);
237 }
238 
239 static void kunit_exec_list_tests(struct suite_set *suite_set, bool include_attr)
240 {
241 	struct kunit_suite * const *suites;
242 	struct kunit_case *test_case;
243 
244 	/* Hack: print a ktap header so kunit.py can find the start of KUnit output. */
245 	pr_info("KTAP version 1\n");
246 
247 	for (suites = suite_set->start; suites < suite_set->end; suites++) {
248 		/* Print suite name and suite attributes */
249 		pr_info("%s\n", (*suites)->name);
250 		if (include_attr)
251 			kunit_print_attr((void *)(*suites), false, 0);
252 
253 		/* Print test case name and attributes in suite */
254 		kunit_suite_for_each_test_case((*suites), test_case) {
255 			pr_info("%s.%s\n", (*suites)->name, test_case->name);
256 			if (include_attr)
257 				kunit_print_attr((void *)test_case, true, 0);
258 		}
259 	}
260 }
261 
262 int kunit_run_all_tests(void)
263 {
264 	struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end};
265 	int err = 0;
266 	if (!kunit_enabled()) {
267 		pr_info("kunit: disabled\n");
268 		goto out;
269 	}
270 
271 	if (filter_glob_param || filter_param) {
272 		suite_set = kunit_filter_suites(&suite_set, filter_glob_param,
273 				filter_param, filter_action_param, &err);
274 		if (err) {
275 			pr_err("kunit executor: error filtering suites: %d\n", err);
276 			goto out;
277 		}
278 	}
279 
280 	if (!action_param)
281 		kunit_exec_run_tests(&suite_set);
282 	else if (strcmp(action_param, "list") == 0)
283 		kunit_exec_list_tests(&suite_set, false);
284 	else if (strcmp(action_param, "list_attr") == 0)
285 		kunit_exec_list_tests(&suite_set, true);
286 	else
287 		pr_err("kunit executor: unknown action '%s'\n", action_param);
288 
289 	if (filter_glob_param || filter_param) { /* a copy was made of each suite */
290 		kunit_free_suite_set(suite_set);
291 	}
292 
293 out:
294 	kunit_handle_shutdown();
295 	return err;
296 }
297 
298 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
299 #include "executor_test.c"
300 #endif
301 
302 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
303