Skip to content

Commit 3169191

Browse files
Stanislav Kinsburskiishuahkh
authored andcommitted
kunit: Introduce autorun option
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>
1 parent 220374e commit 3169191

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

include/kunit/test.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ static inline void kunit_set_failure(struct kunit *test)
312312
}
313313

314314
bool kunit_enabled(void);
315+
bool kunit_autorun(void);
315316
const char *kunit_action(void);
316317
const char *kunit_filter_glob(void);
317318
char *kunit_filter(void);
@@ -334,7 +335,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
334335
int *err);
335336
void kunit_free_suite_set(struct kunit_suite_set suite_set);
336337

337-
int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
338+
int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
339+
bool run_tests);
338340

339341
void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
340342

lib/kunit/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,16 @@ config KUNIT_DEFAULT_ENABLED
8181
In most cases this should be left as Y. Only if additional opt-in
8282
behavior is needed should this be set to N.
8383

84+
config KUNIT_AUTORUN_ENABLED
85+
bool "Default value of kunit.autorun"
86+
default y
87+
help
88+
Sets the default value of kunit.autorun. If set to N then KUnit
89+
tests will not run after initialization unless kunit.autorun=1 is
90+
passed to the kernel command line. The test can still be run manually
91+
via debugfs interface.
92+
93+
In most cases this should be left as Y. Only if additional opt-in
94+
behavior is needed should this be set to N.
95+
8496
endif # KUNIT

lib/kunit/debugfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static ssize_t debugfs_run(struct file *file,
145145
struct inode *f_inode = file->f_inode;
146146
struct kunit_suite *suite = (struct kunit_suite *) f_inode->i_private;
147147

148-
__kunit_test_suites_init(&suite, 1);
148+
__kunit_test_suites_init(&suite, 1, true);
149149

150150
return count;
151151
}

lib/kunit/executor.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ const char *kunit_action(void)
2929
return action_param;
3030
}
3131

32+
/*
33+
* Run KUnit tests after initialization
34+
*/
35+
#ifdef CONFIG_KUNIT_AUTORUN_ENABLED
36+
static bool autorun_param = true;
37+
#else
38+
static bool autorun_param;
39+
#endif
40+
module_param_named(autorun, autorun_param, bool, 0);
41+
MODULE_PARM_DESC(autorun, "Run KUnit tests after initialization");
42+
43+
bool kunit_autorun(void)
44+
{
45+
return autorun_param;
46+
}
47+
3248
static char *filter_glob_param;
3349
static char *filter_param;
3450
static char *filter_action_param;
@@ -260,13 +276,14 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
260276
void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
261277
{
262278
size_t num_suites = suite_set->end - suite_set->start;
279+
bool autorun = kunit_autorun();
263280

264-
if (builtin || num_suites) {
281+
if (autorun && (builtin || num_suites)) {
265282
pr_info("KTAP version 1\n");
266283
pr_info("1..%zu\n", num_suites);
267284
}
268285

269-
__kunit_test_suites_init(suite_set->start, num_suites);
286+
__kunit_test_suites_init(suite_set->start, num_suites, autorun);
270287
}
271288

272289
void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr)

lib/kunit/test.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ bool kunit_enabled(void)
708708
return enable_param;
709709
}
710710

711-
int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
711+
int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
712+
bool run_tests)
712713
{
713714
unsigned int i;
714715

@@ -731,7 +732,8 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
731732

732733
for (i = 0; i < num_suites; i++) {
733734
kunit_init_suite(suites[i]);
734-
kunit_run_tests(suites[i]);
735+
if (run_tests)
736+
kunit_run_tests(suites[i]);
735737
}
736738

737739
static_branch_dec(&kunit_running);

0 commit comments

Comments
 (0)