xref: /linux-6.15/lib/kunit/executor.c (revision c95e7c05)
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 static void kunit_free_suite_set(struct kunit_suite_set suite_set)
108 {
109 	struct kunit_suite * const *suites;
110 
111 	for (suites = suite_set.start; suites < suite_set.end; suites++)
112 		kfree(*suites);
113 	kfree(suite_set.start);
114 }
115 
116 static struct kunit_suite_set
117 kunit_filter_suites(const struct kunit_suite_set *suite_set,
118 		    const char *filter_glob,
119 		    char *filters,
120 		    char *filter_action,
121 		    int *err)
122 {
123 	int i, j, k;
124 	int filter_count = 0;
125 	struct kunit_suite **copy, **copy_start, *filtered_suite, *new_filtered_suite;
126 	struct kunit_suite_set filtered = {NULL, NULL};
127 	struct kunit_glob_filter parsed_glob;
128 	struct kunit_attr_filter *parsed_filters = NULL;
129 
130 	const size_t max = suite_set->end - suite_set->start;
131 
132 	copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
133 	if (!copy) { /* won't be able to run anything, return an empty set */
134 		return filtered;
135 	}
136 	copy_start = copy;
137 
138 	if (filter_glob)
139 		kunit_parse_glob_filter(&parsed_glob, filter_glob);
140 
141 	/* Parse attribute filters */
142 	if (filters) {
143 		filter_count = kunit_get_filter_count(filters);
144 		parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
145 		if (!parsed_filters) {
146 			kfree(copy);
147 			return filtered;
148 		}
149 		for (j = 0; j < filter_count; j++)
150 			parsed_filters[j] = kunit_next_attr_filter(&filters, err);
151 		if (*err)
152 			goto err;
153 	}
154 
155 	for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
156 		filtered_suite = suite_set->start[i];
157 		if (filter_glob) {
158 			if (!glob_match(parsed_glob.suite_glob, filtered_suite->name))
159 				continue;
160 			filtered_suite = kunit_filter_glob_tests(filtered_suite,
161 					parsed_glob.test_glob);
162 			if (IS_ERR(filtered_suite)) {
163 				*err = PTR_ERR(filtered_suite);
164 				goto err;
165 			}
166 		}
167 		if (filter_count > 0 && parsed_filters != NULL) {
168 			for (k = 0; k < filter_count; k++) {
169 				new_filtered_suite = kunit_filter_attr_tests(filtered_suite,
170 						parsed_filters[k], filter_action, err);
171 
172 				/* Free previous copy of suite */
173 				if (k > 0 || filter_glob) {
174 					kfree(filtered_suite->test_cases);
175 					kfree(filtered_suite);
176 				}
177 
178 				filtered_suite = new_filtered_suite;
179 
180 				if (*err)
181 					goto err;
182 				if (IS_ERR(filtered_suite)) {
183 					*err = PTR_ERR(filtered_suite);
184 					goto err;
185 				}
186 				if (!filtered_suite)
187 					break;
188 			}
189 		}
190 
191 		if (!filtered_suite)
192 			continue;
193 
194 		*copy++ = filtered_suite;
195 	}
196 	filtered.start = copy_start;
197 	filtered.end = copy;
198 
199 err:
200 	if (*err)
201 		kfree(copy);
202 
203 	if (filter_glob) {
204 		kfree(parsed_glob.suite_glob);
205 		kfree(parsed_glob.test_glob);
206 	}
207 
208 	if (filter_count)
209 		kfree(parsed_filters);
210 
211 	return filtered;
212 }
213 
214 static void kunit_handle_shutdown(void)
215 {
216 	if (!kunit_shutdown)
217 		return;
218 
219 	if (!strcmp(kunit_shutdown, "poweroff"))
220 		kernel_power_off();
221 	else if (!strcmp(kunit_shutdown, "halt"))
222 		kernel_halt();
223 	else if (!strcmp(kunit_shutdown, "reboot"))
224 		kernel_restart(NULL);
225 
226 }
227 
228 #endif
229 
230 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
231 {
232 	size_t num_suites = suite_set->end - suite_set->start;
233 
234 	if (builtin || num_suites) {
235 		pr_info("KTAP version 1\n");
236 		pr_info("1..%zu\n", num_suites);
237 	}
238 
239 	__kunit_test_suites_init(suite_set->start, num_suites);
240 }
241 
242 #if IS_BUILTIN(CONFIG_KUNIT)
243 
244 static void kunit_exec_list_tests(struct kunit_suite_set *suite_set,
245 				  bool include_attr)
246 {
247 	struct kunit_suite * const *suites;
248 	struct kunit_case *test_case;
249 
250 	/* Hack: print a ktap header so kunit.py can find the start of KUnit output. */
251 	pr_info("KTAP version 1\n");
252 
253 	for (suites = suite_set->start; suites < suite_set->end; suites++) {
254 		/* Print suite name and suite attributes */
255 		pr_info("%s\n", (*suites)->name);
256 		if (include_attr)
257 			kunit_print_attr((void *)(*suites), false, 0);
258 
259 		/* Print test case name and attributes in suite */
260 		kunit_suite_for_each_test_case((*suites), test_case) {
261 			pr_info("%s.%s\n", (*suites)->name, test_case->name);
262 			if (include_attr)
263 				kunit_print_attr((void *)test_case, true, 0);
264 		}
265 	}
266 }
267 
268 int kunit_run_all_tests(void)
269 {
270 	struct kunit_suite_set suite_set = {
271 		__kunit_suites_start, __kunit_suites_end,
272 	};
273 	int err = 0;
274 	if (!kunit_enabled()) {
275 		pr_info("kunit: disabled\n");
276 		goto out;
277 	}
278 
279 	if (filter_glob_param || filter_param) {
280 		suite_set = kunit_filter_suites(&suite_set, filter_glob_param,
281 				filter_param, filter_action_param, &err);
282 		if (err) {
283 			pr_err("kunit executor: error filtering suites: %d\n", err);
284 			goto out;
285 		}
286 	}
287 
288 	if (!action_param)
289 		kunit_exec_run_tests(&suite_set, true);
290 	else if (strcmp(action_param, "list") == 0)
291 		kunit_exec_list_tests(&suite_set, false);
292 	else if (strcmp(action_param, "list_attr") == 0)
293 		kunit_exec_list_tests(&suite_set, true);
294 	else
295 		pr_err("kunit executor: unknown action '%s'\n", action_param);
296 
297 	if (filter_glob_param || filter_param) { /* a copy was made of each suite */
298 		kunit_free_suite_set(suite_set);
299 	}
300 
301 out:
302 	kunit_handle_shutdown();
303 	return err;
304 }
305 
306 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
307 #include "executor_test.c"
308 #endif
309 
310 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
311