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