kunit: Introduce autorun option
authorStanislav Kinsburskii <skinsburskii@linux.microsoft.com>
Mon, 28 Oct 2024 21:54:52 +0000 (21:54 +0000)
committerShuah Khan <skhan@linuxfoundation.org>
Wed, 15 Jan 2025 16:04:06 +0000 (09:04 -0700)
The new option controls tests run on boot or module load. With the new
debugfs "run" dentry allowing to run tests on demand, an ability to disable
automatic tests run becomes a useful option in case of intrusive tests.

The option is set to true by default to preserve the existent behavior. It
can be overridden by either the corresponding module option or by the
corresponding config build option.

Link: https://lore.kernel.org/r/173015245931.4747.16419517391658830640.stgit@skinsburskii-cloud-desktop.internal.cloudapp.net
Signed-off-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
Reviewed-by: Rae Moar <rmoar@google.com>
Acked-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
include/kunit/test.h
lib/kunit/Kconfig
lib/kunit/debugfs.c
lib/kunit/executor.c
lib/kunit/test.c

index 34b71e4..58dbab6 100644 (file)
@@ -312,6 +312,7 @@ static inline void kunit_set_failure(struct kunit *test)
 }
 
 bool kunit_enabled(void);
+bool kunit_autorun(void);
 const char *kunit_action(void);
 const char *kunit_filter_glob(void);
 char *kunit_filter(void);
@@ -334,7 +335,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
                    int *err);
 void kunit_free_suite_set(struct kunit_suite_set suite_set);
 
-int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
+int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
+                            bool run_tests);
 
 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
 
index 34d7242..a97897e 100644 (file)
@@ -81,4 +81,16 @@ config KUNIT_DEFAULT_ENABLED
          In most cases this should be left as Y. Only if additional opt-in
          behavior is needed should this be set to N.
 
+config KUNIT_AUTORUN_ENABLED
+       bool "Default value of kunit.autorun"
+       default y
+       help
+         Sets the default value of kunit.autorun. If set to N then KUnit
+         tests will not run after initialization unless kunit.autorun=1 is
+         passed to the kernel command line. The test can still be run manually
+         via debugfs interface.
+
+         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 af71911..9c326f1 100644 (file)
@@ -145,7 +145,7 @@ static ssize_t debugfs_run(struct file *file,
        struct inode *f_inode = file->f_inode;
        struct kunit_suite *suite = (struct kunit_suite *) f_inode->i_private;
 
-       __kunit_test_suites_init(&suite, 1);
+       __kunit_test_suites_init(&suite, 1, true);
 
        return count;
 }
index 34b7b68..3f39955 100644 (file)
@@ -29,6 +29,22 @@ const char *kunit_action(void)
        return action_param;
 }
 
+/*
+ * Run KUnit tests after initialization
+ */
+#ifdef CONFIG_KUNIT_AUTORUN_ENABLED
+static bool autorun_param = true;
+#else
+static bool autorun_param;
+#endif
+module_param_named(autorun, autorun_param, bool, 0);
+MODULE_PARM_DESC(autorun, "Run KUnit tests after initialization");
+
+bool kunit_autorun(void)
+{
+       return autorun_param;
+}
+
 static char *filter_glob_param;
 static char *filter_param;
 static char *filter_action_param;
@@ -260,13 +276,14 @@ free_copy:
 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
 {
        size_t num_suites = suite_set->end - suite_set->start;
+       bool autorun = kunit_autorun();
 
-       if (builtin || num_suites) {
+       if (autorun && (builtin || num_suites)) {
                pr_info("KTAP version 1\n");
                pr_info("1..%zu\n", num_suites);
        }
 
-       __kunit_test_suites_init(suite_set->start, num_suites);
+       __kunit_test_suites_init(suite_set->start, num_suites, autorun);
 }
 
 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr)
index 089c832..146d1b4 100644 (file)
@@ -708,7 +708,8 @@ bool kunit_enabled(void)
        return enable_param;
 }
 
-int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
+int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
+                            bool run_tests)
 {
        unsigned int i;
 
@@ -731,7 +732,8 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
 
        for (i = 0; i < num_suites; i++) {
                kunit_init_suite(suites[i]);
-               kunit_run_tests(suites[i]);
+               if (run_tests)
+                       kunit_run_tests(suites[i]);
        }
 
        static_branch_dec(&kunit_running);