xref: /linux-6.15/lib/kunit/executor.c (revision 39e92cb1)
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 
21 module_param_named(filter_glob, filter_glob_param, charp, 0);
22 MODULE_PARM_DESC(filter_glob,
23 		"Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test");
24 module_param_named(action, action_param, charp, 0);
25 MODULE_PARM_DESC(action,
26 		 "Changes KUnit executor behavior, valid values are:\n"
27 		 "<none>: run the tests like normal\n"
28 		 "'list' to list test names instead of running them.\n"
29 		 "'list_attr' to list test names and attributes instead of running them.\n");
30 
31 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */
32 struct kunit_test_filter {
33 	char *suite_glob;
34 	char *test_glob;
35 };
36 
37 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */
38 static void kunit_parse_filter_glob(struct kunit_test_filter *parsed,
39 				    const char *filter_glob)
40 {
41 	const int len = strlen(filter_glob);
42 	const char *period = strchr(filter_glob, '.');
43 
44 	if (!period) {
45 		parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
46 		parsed->test_glob = NULL;
47 		strcpy(parsed->suite_glob, filter_glob);
48 		return;
49 	}
50 
51 	parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
52 	parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
53 
54 	strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
55 	strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
56 }
57 
58 /* Create a copy of suite with only tests that match test_glob. */
59 static struct kunit_suite *
60 kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)
61 {
62 	int n = 0;
63 	struct kunit_case *filtered, *test_case;
64 	struct kunit_suite *copy;
65 
66 	kunit_suite_for_each_test_case(suite, test_case) {
67 		if (!test_glob || glob_match(test_glob, test_case->name))
68 			++n;
69 	}
70 
71 	if (n == 0)
72 		return NULL;
73 
74 	copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
75 	if (!copy)
76 		return ERR_PTR(-ENOMEM);
77 
78 	filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
79 	if (!filtered) {
80 		kfree(copy);
81 		return ERR_PTR(-ENOMEM);
82 	}
83 
84 	n = 0;
85 	kunit_suite_for_each_test_case(suite, test_case) {
86 		if (!test_glob || glob_match(test_glob, test_case->name))
87 			filtered[n++] = *test_case;
88 	}
89 
90 	copy->test_cases = filtered;
91 	return copy;
92 }
93 
94 static char *kunit_shutdown;
95 core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
96 
97 /* Stores an array of suites, end points one past the end */
98 struct suite_set {
99 	struct kunit_suite * const *start;
100 	struct kunit_suite * const *end;
101 };
102 
103 static void kunit_free_suite_set(struct suite_set suite_set)
104 {
105 	struct kunit_suite * const *suites;
106 
107 	for (suites = suite_set.start; suites < suite_set.end; suites++)
108 		kfree(*suites);
109 	kfree(suite_set.start);
110 }
111 
112 static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
113 					    const char *filter_glob,
114 					    int *err)
115 {
116 	int i;
117 	struct kunit_suite **copy, *filtered_suite;
118 	struct suite_set filtered;
119 	struct kunit_test_filter filter;
120 
121 	const size_t max = suite_set->end - suite_set->start;
122 
123 	copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
124 	filtered.start = copy;
125 	if (!copy) { /* won't be able to run anything, return an empty set */
126 		filtered.end = copy;
127 		return filtered;
128 	}
129 
130 	kunit_parse_filter_glob(&filter, filter_glob);
131 
132 	for (i = 0; &suite_set->start[i] != suite_set->end; i++) {
133 		if (!glob_match(filter.suite_glob, suite_set->start[i]->name))
134 			continue;
135 
136 		filtered_suite = kunit_filter_tests(suite_set->start[i], filter.test_glob);
137 		if (IS_ERR(filtered_suite)) {
138 			*err = PTR_ERR(filtered_suite);
139 			return filtered;
140 		}
141 		if (!filtered_suite)
142 			continue;
143 
144 		*copy++ = filtered_suite;
145 	}
146 	filtered.end = copy;
147 
148 	kfree(filter.suite_glob);
149 	kfree(filter.test_glob);
150 	return filtered;
151 }
152 
153 static void kunit_handle_shutdown(void)
154 {
155 	if (!kunit_shutdown)
156 		return;
157 
158 	if (!strcmp(kunit_shutdown, "poweroff"))
159 		kernel_power_off();
160 	else if (!strcmp(kunit_shutdown, "halt"))
161 		kernel_halt();
162 	else if (!strcmp(kunit_shutdown, "reboot"))
163 		kernel_restart(NULL);
164 
165 }
166 
167 static void kunit_exec_run_tests(struct suite_set *suite_set)
168 {
169 	size_t num_suites = suite_set->end - suite_set->start;
170 
171 	pr_info("KTAP version 1\n");
172 	pr_info("1..%zu\n", num_suites);
173 
174 	__kunit_test_suites_init(suite_set->start, num_suites);
175 }
176 
177 static void kunit_exec_list_tests(struct suite_set *suite_set, bool include_attr)
178 {
179 	struct kunit_suite * const *suites;
180 	struct kunit_case *test_case;
181 
182 	/* Hack: print a ktap header so kunit.py can find the start of KUnit output. */
183 	pr_info("KTAP version 1\n");
184 
185 	for (suites = suite_set->start; suites < suite_set->end; suites++) {
186 		/* Print suite name and suite attributes */
187 		pr_info("%s\n", (*suites)->name);
188 		if (include_attr)
189 			kunit_print_attr((void *)(*suites), false, 0);
190 
191 		/* Print test case name and attributes in suite */
192 		kunit_suite_for_each_test_case((*suites), test_case) {
193 			pr_info("%s.%s\n", (*suites)->name, test_case->name);
194 			if (include_attr)
195 				kunit_print_attr((void *)test_case, true, 0);
196 		}
197 	}
198 }
199 
200 int kunit_run_all_tests(void)
201 {
202 	struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end};
203 	int err = 0;
204 	if (!kunit_enabled()) {
205 		pr_info("kunit: disabled\n");
206 		goto out;
207 	}
208 
209 	if (filter_glob_param) {
210 		suite_set = kunit_filter_suites(&suite_set, filter_glob_param, &err);
211 		if (err) {
212 			pr_err("kunit executor: error filtering suites: %d\n", err);
213 			goto out;
214 		}
215 	}
216 
217 	if (!action_param)
218 		kunit_exec_run_tests(&suite_set);
219 	else if (strcmp(action_param, "list") == 0)
220 		kunit_exec_list_tests(&suite_set, false);
221 	else if (strcmp(action_param, "list_attr") == 0)
222 		kunit_exec_list_tests(&suite_set, true);
223 	else
224 		pr_err("kunit executor: unknown action '%s'\n", action_param);
225 
226 	if (filter_glob_param) { /* a copy was made of each suite */
227 		kunit_free_suite_set(suite_set);
228 	}
229 
230 out:
231 	kunit_handle_shutdown();
232 	return err;
233 }
234 
235 #if IS_BUILTIN(CONFIG_KUNIT_TEST)
236 #include "executor_test.c"
237 #endif
238 
239 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
240