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