kunit: test: create single dispatcher for kunit

Make KUnit launch all tests instead of init system.

Change-Id: If3a5b17f4e674041e16f8a81ada197e524ffeda1
Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
diff --git a/include/test/test.h b/include/test/test.h
index dee210d..2a1765f 100644
--- a/include/test/test.h
+++ b/include/test/test.h
@@ -208,11 +208,8 @@
  * information.
  */
 #define module_test(module) \
-		static int module_test_init##module(void) \
-		{ \
-			return test_run_tests(&module); \
-		} \
-		late_initcall(module_test_init##module)
+		static struct test_module *__test_module_##module __used       \
+	__attribute__((__section__(".test_modules"))) = &module
 
 /**
  * test_alloc_resource() - Allocates a *test managed resource*.
diff --git a/test/Makefile b/test/Makefile
index e27ea5a..07c70a5 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -1,5 +1,5 @@
 obj-$(CONFIG_TEST)		+= test.o mock.o common-mocks.o string-stream.o \
-  test-stream.o
+  test-stream.o test-executor.o
 obj-$(CONFIG_TEST_TEST)		+= \
   test-test.o test-mock.o mock-macro-test.o mock-test.o string-stream-test.o \
   test-stream-test.o
diff --git a/test/test-executor.c b/test/test-executor.c
new file mode 100644
index 0000000..d751fde
--- /dev/null
+++ b/test/test-executor.c
@@ -0,0 +1,29 @@
+#include <linux/init.h>
+#include <linux/printk.h>
+#include <test/test.h>
+
+extern struct test_module *__test_modules_start[];
+extern struct test_module *__test_modules_end[];
+
+static bool test_run_all_tests(void)
+{
+	struct test_module** module;
+	bool has_test_failed = false;
+
+	for (module = __test_modules_start; module < __test_modules_end; ++module) {
+		if (test_run_tests(*module))
+			has_test_failed = true;
+	}
+
+	return !has_test_failed;
+}
+
+static int test_executor_init(void)
+{
+	if (test_run_all_tests())
+		return 0;
+	else
+		return -EFAULT;
+}
+
+late_initcall(test_executor_init);