kunit: add kunit.enable to enable/disable KUnit test
authorJoe Fradley <joefradley@google.com>
Tue, 23 Aug 2022 14:24:54 +0000 (07:24 -0700)
committerShuah Khan <skhan@linuxfoundation.org>
Fri, 30 Sep 2022 19:17:39 +0000 (13:17 -0600)
This patch adds the kunit.enable module parameter that will need to be
set to true in addition to KUNIT being enabled for KUnit tests to run.
The default value is true giving backwards compatibility. However, for
the production+testing use case the new config option
KUNIT_DEFAULT_ENABLED can be set to N requiring the tester to opt-in
by passing kunit.enable=1 to the kernel.

Signed-off-by: Joe Fradley <joefradley@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Documentation/admin-guide/kernel-parameters.txt
include/kunit/test.h
lib/kunit/Kconfig
lib/kunit/executor.c
lib/kunit/test.c
tools/testing/kunit/kunit_kernel.py

index 426fa892d311a308e38131edf21de630db0654aa..9f15ee63c4f4e02688fadddee010173e53d4294b 100644 (file)
                        0: force disabled
                        1: force enabled
 
+       kunit.enable=   [KUNIT] Enable executing KUnit tests. Requires
+                       CONFIG_KUNIT to be set to be fully enabled. The
+                       default value can be overridden via
+                       KUNIT_DEFAULT_ENABLED.
+                       Default is 1 (enabled)
+
        kvm.ignore_msrs=[KVM] Ignore guest accesses to unhandled MSRs.
                        Default is 0 (don't ignore, but inject #GP)
 
index 840a2c375065b941a0043a660298102061bf18e7..a6d6892b7957639bb31ad0bb3dac5c83dde96c8e 100644 (file)
@@ -228,6 +228,8 @@ static inline void kunit_set_failure(struct kunit *test)
        WRITE_ONCE(test->status, KUNIT_FAILURE);
 }
 
+bool kunit_enabled(void);
+
 void kunit_init_test(struct kunit *test, const char *name, char *log);
 
 int kunit_run_tests(struct kunit_suite *suite);
index 0b5dfb001bacc1b173f6fb8b2b3f944d6d891b01..626719b95badd59a761082d78676734624fb65b6 100644 (file)
@@ -59,4 +59,15 @@ config KUNIT_ALL_TESTS
 
          If unsure, say N.
 
+config KUNIT_DEFAULT_ENABLED
+       bool "Default value of kunit.enable"
+       default y
+       help
+         Sets the default value of kunit.enable. If set to N then KUnit
+         tests will not execute unless kunit.enable=1 is passed to the
+         kernel command line.
+
+         In most cases this should be left as Y. Only if additional opt-in
+         behavior is needed should this be set to N.
+
 endif # KUNIT
index 5e223327196a6d8493c921c040446c76cd313999..9bbc422c284bfb921823d0b45c6639883f394478 100644 (file)
@@ -190,6 +190,10 @@ int kunit_run_all_tests(void)
 {
        struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end};
        int err = 0;
+       if (!kunit_enabled()) {
+               pr_info("kunit: disabled\n");
+               goto out;
+       }
 
        if (filter_glob_param) {
                suite_set = kunit_filter_suites(&suite_set, filter_glob_param, &err);
index b73d5bb5c47381e232861a43d28f9e16d727cc13..1e54373309a411c90f70f0330fe18dc3d605d497 100644 (file)
@@ -54,6 +54,17 @@ void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
 EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
 #endif
 
+/*
+ * Enable KUnit tests to run.
+ */
+#ifdef CONFIG_KUNIT_DEFAULT_ENABLED
+static bool enable_param = true;
+#else
+static bool enable_param;
+#endif
+module_param_named(enable, enable_param, bool, 0);
+MODULE_PARM_DESC(enable, "Enable KUnit tests");
+
 /*
  * KUnit statistic mode:
  * 0 - disabled
@@ -586,10 +597,20 @@ static void kunit_init_suite(struct kunit_suite *suite)
        suite->suite_init_err = 0;
 }
 
+bool kunit_enabled(void)
+{
+       return enable_param;
+}
+
 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
 {
        unsigned int i;
 
+       if (!kunit_enabled() && num_suites > 0) {
+               pr_info("kunit: disabled\n");
+               return 0;
+       }
+
        for (i = 0; i < num_suites; i++) {
                kunit_init_suite(suites[i]);
                kunit_run_tests(suites[i]);
@@ -607,6 +628,9 @@ void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
 {
        unsigned int i;
 
+       if (!kunit_enabled())
+               return;
+
        for (i = 0; i < num_suites; i++)
                kunit_exit_suite(suites[i]);
 
index f5c26ea8971486cd599d5b2a5d918aa5c05add69..ef794da420d797aaccd1662ba40c9c7c63f3b13d 100644 (file)
@@ -359,6 +359,7 @@ class LinuxSourceTree:
                        args = []
                if filter_glob:
                        args.append('kunit.filter_glob='+filter_glob)
+               args.append('kunit.enable=1')
 
                process = self._ops.start(args, build_dir)
                assert process.stdout is not None  # tell mypy it's set