xref: /linux/lib/kunit/executor.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
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 extern struct kunit_suite * const __kunit_init_suites_start[];
16 extern struct kunit_suite * const __kunit_init_suites_end[];
17 
18 static char *action_param;
19 
20 module_param_named(action, action_param, charp, 0400);
21 MODULE_PARM_DESC(action,
22 		 "Changes KUnit executor behavior, valid values are:\n"
23 		 "<none>: run the tests like normal\n"
24 		 "'list' to list test names instead of running them.\n"
25 		 "'list_attr' to list test names and attributes instead of running them.\n");
26 
27 const char *kunit_action(void)
28 {
29 	return action_param;
30 }
31 
32 /*
33  * Run KUnit tests after initialization
34  */
35 #ifdef CONFIG_KUNIT_AUTORUN_ENABLED
36 static bool autorun_param = true;
37 #else
38 static bool autorun_param;
39 #endif
40 module_param_named(autorun, autorun_param, bool, 0);
41 MODULE_PARM_DESC(autorun, "Run KUnit tests after initialization");
42 
43 bool kunit_autorun(void)
44 {
45 	return autorun_param;
46 }
47 
48 static char *filter_glob_param;
49 static char *filter_param;
50 static char *filter_action_param;
51 
52 module_param_named(filter_glob, filter_glob_param, charp, 0600);
53 MODULE_PARM_DESC(filter_glob,
54 		"Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test");
55 module_param_named(filter, filter_param, charp, 0600);
56 MODULE_PARM_DESC(filter,
57 		"Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow");
58 module_param_named(filter_action, filter_action_param, charp, 0600);
59 MODULE_PARM_DESC(filter_action,
60 		"Changes behavior of filtered tests using attributes, valid values are:\n"
61 		"<none>: do not run filtered tests as normal\n"
62 		"'skip': skip all filtered tests instead so tests will appear in output\n");
63 
64 const char *kunit_filter_glob(void)
65 {
66 	return filter_glob_param;
67 }
68 
69 char *kunit_filter(void)
70 {
71 	return filter_param;
72 }
73 
74 char *kunit_filter_action(void)
75 {
76 	return filter_action_param;
77 }
78 
79 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */
80 struct kunit_glob_filter {
81 	char *suite_glob;
82 	char *test_glob;
83 };
84 
85 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */
86 static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed,
87 				    const char *filter_glob)
88 {
89 	const char *period = strchr(filter_glob, '.');
90 
91 	if (!period) {
92 		parsed->suite_glob = kstrdup(filter_glob, GFP_KERNEL);
93 		if (!parsed->suite_glob)
94 			return -ENOMEM;
95 		parsed->test_glob = NULL;
96 		return 0;
97 	}
98 
99 	parsed->suite_glob = kstrndup(filter_glob, period - filter_glob, GFP_KERNEL);
100 	if (!parsed->suite_glob)
101 		return -ENOMEM;
102 
103 	parsed->test_glob = kstrdup(period + 1, GFP_KERNEL);
104 	if (!parsed->test_glob) {
105 		kfree(parsed->suite_glob);
106 		return -ENOMEM;
107 	}
108 
109 	return 0;
110 }
111 
112 /* Create a copy of suite with only tests that match test_glob. */
113 static struct kunit_suite *
114 kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_glob)
115 {
116 	int n = 0;
117 	struct kunit_case *filtered, *test_case;
118 	struct kunit_suite *copy;
119 
120 	kunit_suite_for_each_test_case(suite, test_case) {
121 		if (!test_glob || glob_match(test_glob, test_case->name))
122 			++n;
123 	}
124 
125 	if (n == 0)
126 		return NULL;
127 
128 	copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
129 	if (!copy)
130 		return ERR_PTR(-ENOMEM);
131 
132 	filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
133 	if (!filtered) {
134 		kfree(copy);
135 		return ERR_PTR(-ENOMEM);
136 	}
137 
138 	n = 0;
139 	kunit_suite_for_each_test_case(suite, test_case) {
140 		if (!test_glob || glob_match(test_glob, test_case->name))
141 			filtered[n++] = *test_case;
142 	}
143 
144 	copy->test_cases = filtered;
145 	return copy;
146 }
147 
148 void kunit_free_suite_set(struct kunit_suite_set suite_set)
149 {
150 	struct kunit_suite * const *suites;
151 
152 	for (suites = suite_set.start; suites < suite_set.end; suites++) {
153 		kfree((*suites)->test_cases);
154 		kfree(*suites);
155 	}
156 	kfree(suite_set.start);
157 }
158 
159 /*
160  * Filter and reallocate test suites. Must return the filtered test suites set
161  * allocated at a valid virtual address or NULL in case of error.
162  */
163 struct kunit_suite_set
164 kunit_filter_suites(const struct kunit_suite_set *suite_set,
165 		    const char *filter_glob,
166 		    char *filters,
167 		    char *filter_action,
168 		    int *err)
169 {
170 	int i, j, k;
171 	int filter_count = 0;
172 	struct kunit_suite **copy, **copy_start, *filtered_suite, *new_filtered_suite;
173 	struct kunit_suite_set filtered = {NULL, NULL};
174 	struct kunit_glob_filter parsed_glob;
175 	struct kunit_attr_filter *parsed_filters = NULL;
176 	struct kunit_suite * const *suites;
177 
178 	const size_t max = suite_set->end - suite_set->start;
179 
180 	copy = kcalloc(max, sizeof(*filtered.start), GFP_KERNEL);
181 	if (!copy) { /* won't be able to run anything, return an empty set */
182 		return filtered;
183 	}
184 	copy_start = copy;
185 
186 	if (filter_glob) {
187 		*err = kunit_parse_glob_filter(&parsed_glob, filter_glob);
188 		if (*err)
189 			goto free_copy;
190 	}
191 
192 	/* Parse attribute filters */
193 	if (filters) {
194 		filter_count = kunit_get_filter_count(filters);
195 		parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
196 		if (!parsed_filters) {
197 			*err = -ENOMEM;
198 			goto free_parsed_glob;
199 		}
200 		for (j = 0; j < filter_count; j++)
201 			parsed_filters[j] = kunit_next_attr_filter(&filters, err);
202 		if (*err)
203 			goto free_parsed_filters;
204 	}
205 
206 	for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
207 		filtered_suite = suite_set->start[i];
208 		if (filter_glob) {
209 			if (!glob_match(parsed_glob.suite_glob, filtered_suite->name))
210 				continue;
211 			filtered_suite = kunit_filter_glob_tests(filtered_suite,
212 					parsed_glob.test_glob);
213 			if (IS_ERR(filtered_suite)) {
214 				*err = PTR_ERR(filtered_suite);
215 				goto free_filtered_suite;
216 			}
217 		}
218 		if (filter_count > 0 && parsed_filters != NULL) {
219 			for (k = 0; k < filter_count; k++) {
220 				new_filtered_suite = kunit_filter_attr_tests(filtered_suite,
221 						parsed_filters[k], filter_action, err);
222 
223 				/* Free previous copy of suite */
224 				if (k > 0 || filter_glob) {
225 					kfree(filtered_suite->test_cases);
226 					kfree(filtered_suite);
227 				}
228 
229 				filtered_suite = new_filtered_suite;
230 
231 				if (*err)
232 					goto free_filtered_suite;
233 
234 				if (IS_ERR(filtered_suite)) {
235 					*err = PTR_ERR(filtered_suite);
236 					goto free_filtered_suite;
237 				}
238 				if (!filtered_suite)
239 					break;
240 			}
241 		}
242 
243 		if (!filtered_suite)
244 			continue;
245 
246 		*copy++ = filtered_suite;
247 	}
248 	filtered.start = copy_start;
249 	filtered.end = copy;
250 
251 free_filtered_suite:
252 	if (*err) {
253 		for (suites = copy_start; suites < copy; suites++) {
254 			kfree((*suites)->test_cases);
255 			kfree(*suites);
256 		}
257 	}
258 
259 free_parsed_filters:
260 	if (filter_count)
261 		kfree(parsed_filters);
262 
263 free_parsed_glob:
264 	if (filter_glob) {
265 		kfree(parsed_glob.suite_glob);
266 		kfree(parsed_glob.test_glob);
267 	}
268 
269 free_copy:
270 	if (*err)
271 		kfree(copy_start);
272 
273 	return filtered;
274 }
275 
276 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
277 {
278 	size_t num_suites = suite_set->end - suite_set->start;
279 	bool autorun = kunit_autorun();
280 
281 	if (autorun && (builtin || num_suites)) {
282 		pr_info("KTAP version 1\n");
283 		pr_info("1..%zu\n", num_suites);
284 	}
285 
286 	__kunit_test_suites_init(suite_set->start, num_suites, autorun);
287 }
288 
289 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr)
290 {
291 	struct kunit_suite * const *suites;
292 	struct kunit_case *test_case;
293 
294 	/* Hack: print a ktap header so kunit.py can find the start of KUnit output. */
295 	pr_info("KTAP version 1\n");
296 
297 	for (suites = suite_set->start; suites < suite_set->end; suites++) {
298 		/* Print suite name and suite attributes */
299 		pr_info("%s\n", (*suites)->name);
300 		if (include_attr)
301 			kunit_print_attr((void *)(*suites), false, 0);
302 
303 		/* Print test case name and attributes in suite */
304 		kunit_suite_for_each_test_case((*suites), test_case) {
305 			pr_info("%s.%s\n", (*suites)->name, test_case->name);
306 			if (include_attr)
307 				kunit_print_attr((void *)test_case, true, 0);
308 		}
309 	}
310 }
311 
312 struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,
313 		struct kunit_suite_set suite_set)
314 {
315 	struct kunit_suite_set total_suite_set = {NULL, NULL};
316 	struct kunit_suite **total_suite_start = NULL;
317 	size_t init_num_suites, num_suites, suite_size;
318 	int i = 0;
319 
320 	init_num_suites = init_suite_set.end - init_suite_set.start;
321 	num_suites = suite_set.end - suite_set.start;
322 	suite_size = sizeof(suite_set.start);
323 
324 	/* Allocate memory for array of all kunit suites */
325 	total_suite_start = kmalloc_array(init_num_suites + num_suites, suite_size, GFP_KERNEL);
326 	if (!total_suite_start)
327 		return total_suite_set;
328 
329 	/* Append and mark init suites and then append all other kunit suites */
330 	memcpy(total_suite_start, init_suite_set.start, init_num_suites * suite_size);
331 	for (i = 0; i < init_num_suites; i++)
332 		total_suite_start[i]->is_init = true;
333 
334 	memcpy(total_suite_start + init_num_suites, suite_set.start, num_suites * suite_size);
335 
336 	/* Set kunit suite set start and end */
337 	total_suite_set.start = total_suite_start;
338 	total_suite_set.end = total_suite_start + (init_num_suites + num_suites);
339 
340 	return total_suite_set;
341 }
342 
343 #if IS_BUILTIN(CONFIG_KUNIT)
344 
345 static char *kunit_shutdown;
346 core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
347 
348 static void kunit_handle_shutdown(void)
349 {
350 	if (!kunit_shutdown)
351 		return;
352 
353 	if (!strcmp(kunit_shutdown, "poweroff"))
354 		kernel_power_off();
355 	else if (!strcmp(kunit_shutdown, "halt"))
356 		kernel_halt();
357 	else if (!strcmp(kunit_shutdown, "reboot"))
358 		kernel_restart(NULL);
359 
360 }
361 
362 int kunit_run_all_tests(void)
363 {
364 	struct kunit_suite_set suite_set = {NULL, NULL};
365 	struct kunit_suite_set filtered_suite_set = {NULL, NULL};
366 	struct kunit_suite_set init_suite_set = {
367 		__kunit_init_suites_start, __kunit_init_suites_end,
368 	};
369 	struct kunit_suite_set normal_suite_set = {
370 		__kunit_suites_start, __kunit_suites_end,
371 	};
372 	size_t init_num_suites = init_suite_set.end - init_suite_set.start;
373 	int err = 0;
374 
375 	if (init_num_suites > 0) {
376 		suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set);
377 		if (!suite_set.start)
378 			goto out;
379 	} else
380 		suite_set = normal_suite_set;
381 
382 	if (!kunit_enabled()) {
383 		pr_info("kunit: disabled\n");
384 		goto free_out;
385 	}
386 
387 	if (filter_glob_param || filter_param) {
388 		filtered_suite_set = kunit_filter_suites(&suite_set, filter_glob_param,
389 				filter_param, filter_action_param, &err);
390 
391 		/* Free original suite set before using filtered suite set */
392 		if (init_num_suites > 0)
393 			kfree(suite_set.start);
394 		suite_set = filtered_suite_set;
395 
396 		if (err) {
397 			pr_err("kunit executor: error filtering suites: %d\n", err);
398 			goto free_out;
399 		}
400 	}
401 
402 	if (!action_param)
403 		kunit_exec_run_tests(&suite_set, true);
404 	else if (strcmp(action_param, "list") == 0)
405 		kunit_exec_list_tests(&suite_set, false);
406 	else if (strcmp(action_param, "list_attr") == 0)
407 		kunit_exec_list_tests(&suite_set, true);
408 	else
409 		pr_err("kunit executor: unknown action '%s'\n", action_param);
410 
411 free_out:
412 	if (filter_glob_param || filter_param)
413 		kunit_free_suite_set(suite_set);
414 	else if (init_num_suites > 0)
415 		/* Don't use kunit_free_suite_set because suites aren't individually allocated */
416 		kfree(suite_set.start);
417 
418 out:
419 	kunit_handle_shutdown();
420 	return err;
421 }
422 
423 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
424 #include "executor_test.c"
425 #endif
426 
427 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
428