Skip to content

Commit 08a3ef8

Browse files
committed
Merge tag 'linux-kselftest-5.6-rc1-kunit' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull Kselftest kunit updates from Shuah Khan: "This kunit update consists of: - Support for building kunit as a module from Alan Maguire - AppArmor KUnit tests for policy unpack from Mike Salvatore" * tag 'linux-kselftest-5.6-rc1-kunit' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: building kunit as a module breaks allmodconfig kunit: update documentation to describe module-based build kunit: allow kunit to be loaded as a module kunit: remove timeout dependence on sysctl_hung_task_timeout_seconds kunit: allow kunit tests to be loaded as a module kunit: hide unexported try-catch interface in try-catch-impl.h kunit: move string-stream.h to lib/kunit apparmor: add AppArmor KUnit tests for policy unpack
2 parents ce7ae9d + 35c57fc commit 08a3ef8

28 files changed

+788
-74
lines changed

Documentation/dev-tools/kunit/faq.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Yes, well, mostly.
2929

3030
For the most part, the KUnit core framework (what you use to write the tests)
3131
can compile to any architecture; it compiles like just another part of the
32-
kernel and runs when the kernel boots. However, there is some infrastructure,
32+
kernel and runs when the kernel boots, or when built as a module, when the
33+
module is loaded. However, there is some infrastructure,
3334
like the KUnit Wrapper (``tools/testing/kunit/kunit.py``) that does not support
3435
other architectures.
3536

Documentation/dev-tools/kunit/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ to a standalone program that can be run like any other program directly inside
4949
of a host operating system; to be clear, it does not require any virtualization
5050
support; it is just a regular program.
5151

52+
Alternatively, kunit and kunit tests can be built as modules and tests will
53+
run when the test module is loaded.
54+
5255
KUnit is fast. Excluding build time, from invocation to completion KUnit can run
5356
several dozen tests in only 10 to 20 seconds; this might not sound like a big
5457
deal to some people, but having such fast and easy to run tests fundamentally

Documentation/dev-tools/kunit/usage.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,22 @@ Interspersed in the kernel logs you might see the following:
539539
540540
Congratulations, you just ran a KUnit test on the x86 architecture!
541541

542+
In a similar manner, kunit and kunit tests can also be built as modules,
543+
so if you wanted to run tests in this way you might add the following config
544+
options to your ``.config``:
545+
546+
.. code-block:: none
547+
548+
CONFIG_KUNIT=m
549+
CONFIG_KUNIT_EXAMPLE_TEST=m
550+
551+
Once the kernel is built and installed, a simple
552+
553+
.. code-block:: bash
554+
modprobe example-test
555+
556+
...will run the tests.
557+
542558
Writing new tests for other architectures
543559
-----------------------------------------
544560

drivers/base/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ config DEBUG_TEST_DRIVER_REMOVE
150150

151151
config PM_QOS_KUNIT_TEST
152152
bool "KUnit Test for PM QoS features"
153-
depends on KUNIT
153+
depends on KUNIT=y
154154

155155
config HMEM_REPORTING
156156
bool

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
@@ -109,7 +109,7 @@ config EXT4_DEBUG
109109
echo 1 > /sys/module/ext4/parameters/mballoc_debug
110110

111111
config EXT4_KUNIT_TESTS
112-
bool "KUnit tests for ext4"
112+
tristate "KUnit tests for ext4"
113113
select EXT4_FS
114114
depends on KUNIT
115115
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/assert.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
#ifndef _KUNIT_ASSERT_H
1010
#define _KUNIT_ASSERT_H
1111

12-
#include <kunit/string-stream.h>
1312
#include <linux/err.h>
13+
#include <linux/kernel.h>
1414

1515
struct kunit;
16+
struct string_stream;
1617

1718
/**
1819
* enum kunit_assert_type - Type of expectation/assertion.

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

0 commit comments

Comments
 (0)