Skip to content

Commit aac3546

Browse files
alan-maguireshuahkh
authored andcommitted
kunit: test: create a single centralized executor for all tests
Add a centralized executor to dispatch tests rather than relying on late_initcall to schedule each test suite separately. Centralized execution is for built-in tests only; modules will execute tests when loaded. Signed-off-by: Alan Maguire <alan.maguire@oracle.com> Co-developed-by: Iurii Zaikin <yzaikin@google.com> Signed-off-by: Iurii Zaikin <yzaikin@google.com> Co-developed-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Brendan Higgins <brendanhiggins@google.com> Reviewed-by: Stephen Boyd <sboyd@kernel.org> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 90a025a commit aac3546

4 files changed

Lines changed: 76 additions & 24 deletions

File tree

include/kunit/test.h

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
233233
unsigned int kunit_test_case_num(struct kunit_suite *suite,
234234
struct kunit_case *test_case);
235235

236-
int __kunit_test_suites_init(struct kunit_suite **suites);
236+
int __kunit_test_suites_init(struct kunit_suite * const * const suites);
237237

238238
void __kunit_test_suites_exit(struct kunit_suite **suites);
239239

@@ -246,34 +246,57 @@ void __kunit_test_suites_exit(struct kunit_suite **suites);
246246
* Registers @suites_list with the test framework. See &struct kunit_suite for
247247
* more information.
248248
*
249-
* When builtin, KUnit tests are all run as late_initcalls; this means
250-
* that they cannot test anything where tests must run at a different init
251-
* phase. One significant restriction resulting from this is that KUnit
252-
* cannot reliably test anything that is initialize in the late_init phase;
253-
* another is that KUnit is useless to test things that need to be run in
254-
* an earlier init phase.
255-
*
256-
* An alternative is to build the tests as a module. Because modules
257-
* do not support multiple late_initcall()s, we need to initialize an
258-
* array of suites for a module.
259-
*
260-
* TODO(brendanhiggins@google.com): Don't run all KUnit tests as
261-
* late_initcalls. I have some future work planned to dispatch all KUnit
262-
* tests from the same place, and at the very least to do so after
263-
* everything else is definitely initialized.
249+
* If a test suite is built-in, module_init() gets translated into
250+
* an initcall which we don't want as the idea is that for builtins
251+
* the executor will manage execution. So ensure we do not define
252+
* module_{init|exit} functions for the builtin case when registering
253+
* suites via kunit_test_suites() below.
264254
*/
265-
#define kunit_test_suites(suites_list...) \
266-
static struct kunit_suite *suites[] = {suites_list, NULL}; \
267-
static int kunit_test_suites_init(void) \
255+
#ifdef MODULE
256+
#define kunit_test_suites_for_module(__suites) \
257+
static int __init kunit_test_suites_init(void) \
268258
{ \
269-
return __kunit_test_suites_init(suites); \
259+
return __kunit_test_suites_init(__suites); \
270260
} \
271-
late_initcall(kunit_test_suites_init); \
261+
module_init(kunit_test_suites_init); \
262+
\
272263
static void __exit kunit_test_suites_exit(void) \
273264
{ \
274-
return __kunit_test_suites_exit(suites); \
265+
return __kunit_test_suites_exit(__suites); \
275266
} \
276267
module_exit(kunit_test_suites_exit)
268+
#else
269+
#define kunit_test_suites_for_module(__suites)
270+
#endif /* MODULE */
271+
272+
#define __kunit_test_suites(unique_array, unique_suites, ...) \
273+
static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
274+
kunit_test_suites_for_module(unique_array); \
275+
static struct kunit_suite **unique_suites \
276+
__used __section(.kunit_test_suites) = unique_array
277+
278+
/**
279+
* kunit_test_suites() - used to register one or more &struct kunit_suite
280+
* with KUnit.
281+
*
282+
* @suites: a statically allocated list of &struct kunit_suite.
283+
*
284+
* Registers @suites with the test framework. See &struct kunit_suite for
285+
* more information.
286+
*
287+
* When builtin, KUnit tests are all run via executor; this is done
288+
* by placing the array of struct kunit_suite * in the .kunit_test_suites
289+
* ELF section.
290+
*
291+
* An alternative is to build the tests as a module. Because modules do not
292+
* support multiple initcall()s, we need to initialize an array of suites for a
293+
* module.
294+
*
295+
*/
296+
#define kunit_test_suites(...) \
297+
__kunit_test_suites(__UNIQUE_ID(array), \
298+
__UNIQUE_ID(suites), \
299+
__VA_ARGS__)
277300

278301
#define kunit_test_suite(suite) kunit_test_suites(&suite)
279302

lib/kunit/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ obj-$(CONFIG_KUNIT) += kunit.o
33
kunit-objs += test.o \
44
string-stream.o \
55
assert.o \
6-
try-catch.o
6+
try-catch.o \
7+
executor.o
78

89
ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
910
kunit-objs += debugfs.o

lib/kunit/executor.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <kunit/test.h>
4+
5+
/*
6+
* These symbols point to the .kunit_test_suites section and are defined in
7+
* include/asm-generic/vmlinux.lds.h, and consequently must be extern.
8+
*/
9+
extern struct kunit_suite * const * const __kunit_suites_start[];
10+
extern struct kunit_suite * const * const __kunit_suites_end[];
11+
12+
#if IS_BUILTIN(CONFIG_KUNIT)
13+
14+
static int kunit_run_all_tests(void)
15+
{
16+
struct kunit_suite * const * const *suites;
17+
18+
for (suites = __kunit_suites_start;
19+
suites < __kunit_suites_end;
20+
suites++)
21+
__kunit_test_suites_init(*suites);
22+
23+
return 0;
24+
}
25+
26+
late_initcall(kunit_run_all_tests);
27+
28+
#endif /* IS_BUILTIN(CONFIG_KUNIT) */

lib/kunit/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ static void kunit_init_suite(struct kunit_suite *suite)
381381
kunit_debugfs_create_suite(suite);
382382
}
383383

384-
int __kunit_test_suites_init(struct kunit_suite **suites)
384+
int __kunit_test_suites_init(struct kunit_suite * const * const suites)
385385
{
386386
unsigned int i;
387387

0 commit comments

Comments
 (0)