kunit: test: create a single centralized executor for all tests
authorAlan Maguire <alan.maguire@oracle.com>
Tue, 4 Aug 2020 20:47:42 +0000 (13:47 -0700)
committerShuah Khan <skhan@linuxfoundation.org>
Fri, 9 Oct 2020 20:37:34 +0000 (14:37 -0600)
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>
include/kunit/test.h
lib/kunit/Makefile
lib/kunit/executor.c [new file with mode: 0644]
lib/kunit/test.c

index 59f3144..d2d261f 100644 (file)
@@ -233,7 +233,7 @@ size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
 unsigned int kunit_test_case_num(struct kunit_suite *suite,
                                 struct kunit_case *test_case);
 
-int __kunit_test_suites_init(struct kunit_suite **suites);
+int __kunit_test_suites_init(struct kunit_suite * const * const suites);
 
 void __kunit_test_suites_exit(struct kunit_suite **suites);
 
@@ -246,34 +246,57 @@ void __kunit_test_suites_exit(struct kunit_suite **suites);
  * Registers @suites_list with the test framework. See &struct kunit_suite for
  * more information.
  *
- * When builtin, KUnit tests are all run as late_initcalls; this means
- * that they cannot test anything where tests must run at a different init
- * phase. One significant restriction resulting from this is that KUnit
- * cannot reliably test anything that is initialize in the late_init phase;
- * another is that KUnit is useless to test things that need to be run in
- * an earlier init phase.
- *
- * An alternative is to build the tests as a module.  Because modules
- * do not support multiple late_initcall()s, we need to initialize an
- * array of suites for a module.
- *
- * TODO(brendanhiggins@google.com): Don't run all KUnit tests as
- * late_initcalls.  I have some future work planned to dispatch all KUnit
- * tests from the same place, and at the very least to do so after
- * everything else is definitely initialized.
+ * If a test suite is built-in, module_init() gets translated into
+ * an initcall which we don't want as the idea is that for builtins
+ * the executor will manage execution.  So ensure we do not define
+ * module_{init|exit} functions for the builtin case when registering
+ * suites via kunit_test_suites() below.
  */
-#define kunit_test_suites(suites_list...)                              \
-       static struct kunit_suite *suites[] = {suites_list, NULL};      \
-       static int kunit_test_suites_init(void)                         \
+#ifdef MODULE
+#define kunit_test_suites_for_module(__suites)                         \
+       static int __init kunit_test_suites_init(void)                  \
        {                                                               \
-               return __kunit_test_suites_init(suites);                \
+               return __kunit_test_suites_init(__suites);              \
        }                                                               \
-       late_initcall(kunit_test_suites_init);                          \
+       module_init(kunit_test_suites_init);                            \
+                                                                       \
        static void __exit kunit_test_suites_exit(void)                 \
        {                                                               \
-               return __kunit_test_suites_exit(suites);                \
+               return __kunit_test_suites_exit(__suites);              \
        }                                                               \
        module_exit(kunit_test_suites_exit)
+#else
+#define kunit_test_suites_for_module(__suites)
+#endif /* MODULE */
+
+#define __kunit_test_suites(unique_array, unique_suites, ...)                 \
+       static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL };     \
+       kunit_test_suites_for_module(unique_array);                            \
+       static struct kunit_suite **unique_suites                              \
+       __used __section(.kunit_test_suites) = unique_array
+
+/**
+ * kunit_test_suites() - used to register one or more &struct kunit_suite
+ *                      with KUnit.
+ *
+ * @suites: a statically allocated list of &struct kunit_suite.
+ *
+ * Registers @suites with the test framework. See &struct kunit_suite for
+ * more information.
+ *
+ * When builtin,  KUnit tests are all run via executor; this is done
+ * by placing the array of struct kunit_suite * in the .kunit_test_suites
+ * ELF section.
+ *
+ * An alternative is to build the tests as a module.  Because modules do not
+ * support multiple initcall()s, we need to initialize an array of suites for a
+ * module.
+ *
+ */
+#define kunit_test_suites(...)                                         \
+       __kunit_test_suites(__UNIQUE_ID(array),                         \
+                           __UNIQUE_ID(suites),                        \
+                           __VA_ARGS__)
 
 #define kunit_test_suite(suite)        kunit_test_suites(&suite)
 
index 724b943..c49f4ff 100644 (file)
@@ -3,7 +3,8 @@ obj-$(CONFIG_KUNIT) +=                  kunit.o
 kunit-objs +=                          test.o \
                                        string-stream.o \
                                        assert.o \
-                                       try-catch.o
+                                       try-catch.o \
+                                       executor.o
 
 ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
 kunit-objs +=                          debugfs.o
diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
new file mode 100644 (file)
index 0000000..7015e73
--- /dev/null
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <kunit/test.h>
+
+/*
+ * These symbols point to the .kunit_test_suites section and are defined in
+ * include/asm-generic/vmlinux.lds.h, and consequently must be extern.
+ */
+extern struct kunit_suite * const * const __kunit_suites_start[];
+extern struct kunit_suite * const * const __kunit_suites_end[];
+
+#if IS_BUILTIN(CONFIG_KUNIT)
+
+static int kunit_run_all_tests(void)
+{
+       struct kunit_suite * const * const *suites;
+
+       for (suites = __kunit_suites_start;
+            suites < __kunit_suites_end;
+            suites++)
+                       __kunit_test_suites_init(*suites);
+
+       return 0;
+}
+
+late_initcall(kunit_run_all_tests);
+
+#endif /* IS_BUILTIN(CONFIG_KUNIT) */
index c360372..3fd89f9 100644 (file)
@@ -381,7 +381,7 @@ static void kunit_init_suite(struct kunit_suite *suite)
        kunit_debugfs_create_suite(suite);
 }
 
-int __kunit_test_suites_init(struct kunit_suite **suites)
+int __kunit_test_suites_init(struct kunit_suite * const * const suites)
 {
        unsigned int i;