Skip to content

Commit c475c77

Browse files
alan-maguireshuahkh
authored andcommitted
kunit: allow kunit tests to be loaded as a module
As tests are added to kunit, it will become less feasible to execute all built tests together. By supporting modular tests we provide a simple way to do selective execution on a running system; specifying CONFIG_KUNIT=y CONFIG_KUNIT_EXAMPLE_TEST=m ...means we can simply "insmod example-test.ko" to run the tests. To achieve this we need to do the following: o export the required symbols in kunit o string-stream tests utilize non-exported symbols so for now we skip building them when CONFIG_KUNIT_TEST=m. o drivers/base/power/qos-test.c contains a few unexported interface references, namely freq_qos_read_value() and freq_constraints_init(). Both of these could be potentially defined as static inline functions in include/linux/pm_qos.h, but for now we simply avoid supporting module build for that test suite. o support a new way of declaring test suites. Because a module cannot do multiple late_initcall()s, we provide a kunit_test_suites() macro to declare multiple suites within the same module at once. o some test module names would have been too general ("test-test" and "example-test" for kunit tests, "inode-test" for ext4 tests); rename these as appropriate ("kunit-test", "kunit-example-test" and "ext4-inode-test" respectively). Also define kunit_test_suite() via kunit_test_suites() as callers in other trees may need the old definition. Co-developed-by: Knut Omang <[email protected]> Signed-off-by: Knut Omang <[email protected]> Signed-off-by: Alan Maguire <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Acked-by: Theodore Ts'o <[email protected]> # for ext4 bits Acked-by: David Gow <[email protected]> # For list-test Reported-by: kbuild test robot <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 9bbb11c commit c475c77

File tree

16 files changed

+76
-27
lines changed

16 files changed

+76
-27
lines changed

drivers/base/power/qos-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ static struct kunit_suite pm_qos_test_module = {
114114
.name = "qos-kunit-test",
115115
.test_cases = pm_qos_test_cases,
116116
};
117-
kunit_test_suite(pm_qos_test_module);
117+
kunit_test_suites(&pm_qos_test_module);

fs/ext4/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ config EXT4_DEBUG
108108
echo 1 > /sys/module/ext4/parameters/mballoc_debug
109109

110110
config EXT4_KUNIT_TESTS
111-
bool "KUnit tests for ext4"
111+
tristate "KUnit tests for ext4"
112112
select EXT4_FS
113113
depends on KUNIT
114114
help

fs/ext4/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ ext4-y := balloc.o bitmap.o block_validity.o dir.o ext4_jbd2.o extents.o \
1313

1414
ext4-$(CONFIG_EXT4_FS_POSIX_ACL) += acl.o
1515
ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
16-
ext4-$(CONFIG_EXT4_KUNIT_TESTS) += inode-test.o
16+
ext4-inode-test-objs += inode-test.o
17+
obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-inode-test.o
1718
ext4-$(CONFIG_FS_VERITY) += verity.o

fs/ext4/inode-test.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,6 @@ static struct kunit_suite ext4_inode_test_suite = {
269269
.test_cases = ext4_inode_test_cases,
270270
};
271271

272-
kunit_test_suite(ext4_inode_test_suite);
272+
kunit_test_suites(&ext4_inode_test_suite);
273+
274+
MODULE_LICENSE("GPL v2");

include/kunit/test.h

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <kunit/assert.h>
1313
#include <kunit/try-catch.h>
1414
#include <linux/kernel.h>
15+
#include <linux/module.h>
1516
#include <linux/slab.h>
1617
#include <linux/types.h>
1718

@@ -197,31 +198,47 @@ void kunit_init_test(struct kunit *test, const char *name);
197198
int kunit_run_tests(struct kunit_suite *suite);
198199

199200
/**
200-
* kunit_test_suite() - used to register a &struct kunit_suite with KUnit.
201+
* kunit_test_suites() - used to register one or more &struct kunit_suite
202+
* with KUnit.
201203
*
202-
* @suite: a statically allocated &struct kunit_suite.
204+
* @suites: a statically allocated list of &struct kunit_suite.
203205
*
204-
* Registers @suite with the test framework. See &struct kunit_suite for
206+
* Registers @suites with the test framework. See &struct kunit_suite for
205207
* more information.
206208
*
207-
* NOTE: Currently KUnit tests are all run as late_initcalls; this means
209+
* When builtin, KUnit tests are all run as late_initcalls; this means
208210
* that they cannot test anything where tests must run at a different init
209211
* phase. One significant restriction resulting from this is that KUnit
210212
* cannot reliably test anything that is initialize in the late_init phase;
211213
* another is that KUnit is useless to test things that need to be run in
212214
* an earlier init phase.
213215
*
216+
* An alternative is to build the tests as a module. Because modules
217+
* do not support multiple late_initcall()s, we need to initialize an
218+
* array of suites for a module.
219+
*
214220
* TODO([email protected]): Don't run all KUnit tests as
215221
* late_initcalls. I have some future work planned to dispatch all KUnit
216222
* tests from the same place, and at the very least to do so after
217223
* everything else is definitely initialized.
218224
*/
219-
#define kunit_test_suite(suite) \
220-
static int kunit_suite_init##suite(void) \
221-
{ \
222-
return kunit_run_tests(&suite); \
223-
} \
224-
late_initcall(kunit_suite_init##suite)
225+
#define kunit_test_suites(...) \
226+
static struct kunit_suite *suites[] = { __VA_ARGS__, NULL}; \
227+
static int kunit_test_suites_init(void) \
228+
{ \
229+
unsigned int i; \
230+
for (i = 0; suites[i] != NULL; i++) \
231+
kunit_run_tests(suites[i]); \
232+
return 0; \
233+
} \
234+
late_initcall(kunit_test_suites_init); \
235+
static void __exit kunit_test_suites_exit(void) \
236+
{ \
237+
return; \
238+
} \
239+
module_exit(kunit_test_suites_exit)
240+
241+
#define kunit_test_suite(suite) kunit_test_suites(&suite)
225242

226243
/*
227244
* Like kunit_alloc_resource() below, but returns the struct kunit_resource

kernel/sysctl-test.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,6 @@ static struct kunit_suite sysctl_test_suite = {
389389
.test_cases = sysctl_test_cases,
390390
};
391391

392-
kunit_test_suite(sysctl_test_suite);
392+
kunit_test_suites(&sysctl_test_suite);
393+
394+
MODULE_LICENSE("GPL v2");

lib/Kconfig.debug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ config TEST_SYSCTL
20252025
If unsure, say N.
20262026

20272027
config SYSCTL_KUNIT_TEST
2028-
bool "KUnit test for sysctl"
2028+
tristate "KUnit test for sysctl"
20292029
depends on KUNIT
20302030
help
20312031
This builds the proc sysctl unit test, which runs on boot.
@@ -2036,7 +2036,7 @@ config SYSCTL_KUNIT_TEST
20362036
If unsure, say N.
20372037

20382038
config LIST_KUNIT_TEST
2039-
bool "KUnit Test for Kernel Linked-list structures"
2039+
tristate "KUnit Test for Kernel Linked-list structures"
20402040
depends on KUNIT
20412041
help
20422042
This builds the linked list KUnit test suite.

lib/kunit/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ menuconfig KUNIT
1515
if KUNIT
1616

1717
config KUNIT_TEST
18-
bool "KUnit test for KUnit"
18+
tristate "KUnit test for KUnit"
1919
help
2020
Enables the unit tests for the KUnit test framework. These tests test
2121
the KUnit test framework itself; the tests are both written using
@@ -24,7 +24,7 @@ config KUNIT_TEST
2424
expected.
2525

2626
config KUNIT_EXAMPLE_TEST
27-
bool "Example test for KUnit"
27+
tristate "Example test for KUnit"
2828
help
2929
Enables an example unit test that illustrates some of the basic
3030
features of KUnit. This test only exists to help new users understand

lib/kunit/Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ obj-$(CONFIG_KUNIT) += test.o \
33
assert.o \
44
try-catch.o
55

6-
obj-$(CONFIG_KUNIT_TEST) += test-test.o \
7-
string-stream-test.o
6+
obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
87

9-
obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += example-test.o
8+
# string-stream-test compiles built-in only.
9+
ifeq ($(CONFIG_KUNIT_TEST),y)
10+
obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o
11+
endif
12+
13+
obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o

lib/kunit/assert.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@ void kunit_base_assert_format(const struct kunit_assert *assert,
2626
string_stream_add(stream, "%s FAILED at %s:%d\n",
2727
expect_or_assert, assert->file, assert->line);
2828
}
29+
EXPORT_SYMBOL_GPL(kunit_base_assert_format);
2930

3031
void kunit_assert_print_msg(const struct kunit_assert *assert,
3132
struct string_stream *stream)
3233
{
3334
if (assert->message.fmt)
3435
string_stream_add(stream, "\n%pV", &assert->message);
3536
}
37+
EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
3638

3739
void kunit_fail_assert_format(const struct kunit_assert *assert,
3840
struct string_stream *stream)
3941
{
4042
kunit_base_assert_format(assert, stream);
4143
string_stream_add(stream, "%pV", &assert->message);
4244
}
45+
EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
4346

4447
void kunit_unary_assert_format(const struct kunit_assert *assert,
4548
struct string_stream *stream)
@@ -58,6 +61,7 @@ void kunit_unary_assert_format(const struct kunit_assert *assert,
5861
unary_assert->condition);
5962
kunit_assert_print_msg(assert, stream);
6063
}
64+
EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
6165

6266
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
6367
struct string_stream *stream)
@@ -78,6 +82,7 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
7882
}
7983
kunit_assert_print_msg(assert, stream);
8084
}
85+
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
8186

8287
void kunit_binary_assert_format(const struct kunit_assert *assert,
8388
struct string_stream *stream)
@@ -99,6 +104,7 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
99104
binary_assert->right_value);
100105
kunit_assert_print_msg(assert, stream);
101106
}
107+
EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
102108

103109
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
104110
struct string_stream *stream)
@@ -120,6 +126,7 @@ void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
120126
binary_assert->right_value);
121127
kunit_assert_print_msg(assert, stream);
122128
}
129+
EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
123130

124131
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
125132
struct string_stream *stream)
@@ -141,3 +148,4 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
141148
binary_assert->right_value);
142149
kunit_assert_print_msg(assert, stream);
143150
}
151+
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);

0 commit comments

Comments
 (0)