@@ -12,6 +12,7 @@
#include <kunit/assert.h>
#include <kunit/try-catch.h>
#include <linux/kernel.h>
+#include <linux/module.h>
#include <linux/slab.h>
#include <linux/types.h>
@@ -197,31 +198,46 @@ struct kunit {
int kunit_run_tests(struct kunit_suite *suite);
/**
- * kunit_test_suite() - used to register a &struct kunit_suite with KUnit.
+ * kunit_test_suites() - used to register &struct kunit_suite suites with KUnit.
*
- * @suite: a statically allocated &struct kunit_suite.
+ * @...: a list of statically allocated &struct kunit_suite.
*
- * Registers @suite with the test framework. See &struct kunit_suite for
+ * Registers @... suites with the test framework. See &struct kunit_suite for
* more information.
*
- * NOTE: Currently KUnit tests are all run as late_initcalls; this means
+ * When builtin, KUnit tests are all run as late_initcalls; this means
* that they cannot test anything where tests must run at a different init
* phase. One significant restriction resulting from this is that KUnit
* cannot reliably test anything that is initialize in the late_init phase;
* another is that KUnit is useless to test things that need to be run in
* an earlier init phase.
*
+ * An alternative is to build the tests as a module. Because modules
+ * do not support multiple late_initcall()s, we need to initialize an
+ * array of suites for a module.
+ *
* TODO(brendanhiggins@google.com): Don't run all KUnit tests as
* late_initcalls. I have some future work planned to dispatch all KUnit
* tests from the same place, and at the very least to do so after
* everything else is definitely initialized.
*/
-#define kunit_test_suite(suite) \
- static int kunit_suite_init##suite(void) \
- { \
- return kunit_run_tests(&suite); \
- } \
- late_initcall(kunit_suite_init##suite)
+#define kunit_test_suites(...) \
+ static struct kunit_suite *suites[] = { __VA_ARGS__, NULL}; \
+ static int kunit_test_suites_init(void) \
+ { \
+ unsigned int i; \
+ for (i = 0; suites[i] != NULL; i++) \
+ kunit_run_tests(suites[i]); \
+ return 0; \
+ } \
+ late_initcall(kunit_test_suites_init); \
+ static void __exit kunit_test_suites_exit(void) \
+ { \
+ return; \
+ } \
+ module_exit(kunit_test_suites_exit)
+
+#define kunit_test_suite(suite) kunit_test_suites(suite)
/*
* Like kunit_alloc_resource() below, but returns the struct kunit_resource
@@ -389,4 +389,8 @@ static void sysctl_test_api_dointvec_write_single_greater_int_max(
.test_cases = sysctl_test_cases,
};
-kunit_test_suite(sysctl_test_suite);
+kunit_test_suite(&sysctl_test_suite);
+
+#ifdef MODULE
+MODULE_LICENSE("GPL");
+#endif /* MODULE */
@@ -15,7 +15,7 @@ config KUNIT
Documentation/dev-tools/kunit/.
config KUNIT_TEST
- bool "KUnit test for KUnit"
+ tristate "KUnit test for KUnit"
depends on KUNIT
help
Enables the unit tests for the KUnit test framework. These tests test
@@ -25,7 +25,7 @@ config KUNIT_TEST
expected.
config KUNIT_EXAMPLE_TEST
- bool "Example test for KUnit"
+ tristate "Example test for KUnit"
depends on KUNIT
help
Enables an example unit test that illustrates some of the basic
@@ -24,6 +24,7 @@ void kunit_base_assert_format(const struct kunit_assert *assert,
string_stream_add(stream, "%s FAILED at %s:%d\n",
expect_or_assert, assert->file, assert->line);
}
+EXPORT_SYMBOL_GPL(kunit_base_assert_format);
void kunit_assert_print_msg(const struct kunit_assert *assert,
struct string_stream *stream)
@@ -31,6 +32,7 @@ void kunit_assert_print_msg(const struct kunit_assert *assert,
if (assert->message.fmt)
string_stream_add(stream, "\n%pV", &assert->message);
}
+EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
void kunit_fail_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
@@ -38,6 +40,7 @@ void kunit_fail_assert_format(const struct kunit_assert *assert,
kunit_base_assert_format(assert, stream);
string_stream_add(stream, "%pV", &assert->message);
}
+EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
void kunit_unary_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
@@ -56,6 +59,7 @@ void kunit_unary_assert_format(const struct kunit_assert *assert,
unary_assert->condition);
kunit_assert_print_msg(assert, stream);
}
+EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
@@ -76,6 +80,7 @@ void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
}
kunit_assert_print_msg(assert, stream);
}
+EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
void kunit_binary_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
@@ -97,6 +102,7 @@ void kunit_binary_assert_format(const struct kunit_assert *assert,
binary_assert->right_value);
kunit_assert_print_msg(assert, stream);
}
+EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
@@ -118,6 +124,7 @@ void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
binary_assert->right_value);
kunit_assert_print_msg(assert, stream);
}
+EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
@@ -139,3 +146,4 @@ void kunit_binary_str_assert_format(const struct kunit_assert *assert,
binary_assert->right_value);
kunit_assert_print_msg(assert, stream);
}
+EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
@@ -85,4 +85,8 @@ static int example_test_init(struct kunit *test)
* This registers the above test suite telling KUnit that this is a suite of
* tests that need to be run.
*/
-kunit_test_suite(example_test_suite);
+kunit_test_suite(&example_test_suite);
+
+#ifdef MODULE
+MODULE_LICENSE("GPL");
+#endif /* MODULE */
@@ -45,8 +45,13 @@ static void string_stream_test_get_string(struct kunit *test)
{}
};
-static struct kunit_suite string_stream_test_suite = {
+struct kunit_suite string_stream_test_suite = {
.name = "string-stream-test",
.test_cases = string_stream_test_cases
};
-kunit_test_suite(string_stream_test_suite);
+
+kunit_test_suite(&string_stream_test_suite);
+
+#ifdef MODULE
+MODULE_LICENSE("GPL");
+#endif /* MODULE */
@@ -100,6 +100,7 @@ int string_stream_vadd(struct string_stream *stream,
return 0;
}
+EXPORT_SYMBOL_GPL(string_stream_vadd);
int string_stream_add(struct string_stream *stream, const char *fmt, ...)
{
@@ -112,6 +113,7 @@ int string_stream_add(struct string_stream *stream, const char *fmt, ...)
return result;
}
+EXPORT_SYMBOL_GPL(string_stream_add);
static void string_stream_clear(struct string_stream *stream)
{
@@ -145,6 +147,7 @@ char *string_stream_get_string(struct string_stream *stream)
return buf;
}
+EXPORT_SYMBOL_GPL(string_stream_get_string);
int string_stream_append(struct string_stream *stream,
struct string_stream *other)
@@ -158,11 +161,13 @@ int string_stream_append(struct string_stream *stream,
return string_stream_add(stream, other_content);
}
+EXPORT_SYMBOL_GPL(string_stream_append);
bool string_stream_is_empty(struct string_stream *stream)
{
return list_empty(&stream->fragments);
}
+EXPORT_SYMBOL_GPL(string_stream_is_empty);
struct string_stream_alloc_context {
struct kunit *test;
@@ -207,6 +212,7 @@ struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp)
gfp,
&context);
}
+EXPORT_SYMBOL_GPL(alloc_string_stream);
int string_stream_destroy(struct string_stream *stream)
{
@@ -215,3 +221,4 @@ int string_stream_destroy(struct string_stream *stream)
string_stream_free,
stream);
}
+EXPORT_SYMBOL_GPL(string_stream_destroy);
@@ -100,7 +100,6 @@ static int kunit_try_catch_test_init(struct kunit *test)
.init = kunit_try_catch_test_init,
.test_cases = kunit_try_catch_test_cases,
};
-kunit_test_suite(kunit_try_catch_test_suite);
/*
* Context for testing test managed resources
@@ -328,4 +327,9 @@ static void kunit_resource_test_exit(struct kunit *test)
.exit = kunit_resource_test_exit,
.test_cases = kunit_resource_test_cases,
};
-kunit_test_suite(kunit_resource_test_suite);
+kunit_test_suites(&kunit_resource_test_suite,
+ &kunit_try_catch_test_suite);
+
+#ifdef MODULE
+MODULE_LICENSE("GPL");
+#endif /* MODULE */
@@ -171,6 +171,7 @@ void kunit_do_assertion(struct kunit *test,
if (assert->type == KUNIT_ASSERTION)
kunit_abort(test);
}
+EXPORT_SYMBOL_GPL(kunit_do_assertion);
void kunit_init_test(struct kunit *test, const char *name)
{
@@ -179,6 +180,7 @@ void kunit_init_test(struct kunit *test, const char *name)
test->name = name;
test->success = true;
}
+EXPORT_SYMBOL_GPL(kunit_init_test);
/*
* Initializes and runs test case. Does not clean up or do post validations.
@@ -317,6 +319,7 @@ int kunit_run_tests(struct kunit_suite *suite)
return 0;
}
+EXPORT_SYMBOL_GPL(kunit_run_tests);
struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
kunit_resource_init_t init,
@@ -342,6 +345,7 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
return res;
}
+EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
static void kunit_resource_free(struct kunit *test, struct kunit_resource *res)
{
@@ -400,6 +404,7 @@ int kunit_resource_destroy(struct kunit *test,
kunit_resource_free(test, resource);
return 0;
}
+EXPORT_SYMBOL_GPL(kunit_resource_destroy);
struct kunit_kmalloc_params {
size_t size;
@@ -435,6 +440,7 @@ void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
gfp,
¶ms);
}
+EXPORT_SYMBOL_GPL(kunit_kmalloc);
void kunit_kfree(struct kunit *test, const void *ptr)
{
@@ -447,6 +453,7 @@ void kunit_kfree(struct kunit *test, const void *ptr)
WARN_ON(rc);
}
+EXPORT_SYMBOL_GPL(kunit_kfree);
void kunit_cleanup(struct kunit *test)
{
@@ -476,3 +483,4 @@ void kunit_cleanup(struct kunit *test)
kunit_resource_free(test, resource);
}
}
+EXPORT_SYMBOL_GPL(kunit_cleanup);
@@ -19,6 +19,7 @@ void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
try_catch->try_result = -EFAULT;
complete_and_exit(try_catch->try_completion, -EFAULT);
}
+EXPORT_SYMBOL_GPL(kunit_try_catch_throw);
static int kunit_generic_run_threadfn_adapter(void *data)
{
@@ -50,6 +51,7 @@ static unsigned long kunit_test_timeout(void)
* For more background on this topic, see:
* https://mike-bland.com/2011/11/01/small-medium-large.html
*/
+#ifndef MODULE
if (sysctl_hung_task_timeout_secs) {
/*
* If sysctl_hung_task is active, just set the timeout to some
@@ -60,9 +62,9 @@ static unsigned long kunit_test_timeout(void)
*/
timeout_msecs = (sysctl_hung_task_timeout_secs - 1) *
MSEC_PER_SEC;
- } else {
+ } else
+#endif
timeout_msecs = 300 * MSEC_PER_SEC; /* 5 min */
- }
return timeout_msecs;
}
@@ -106,6 +108,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
try_catch->catch(try_catch->context);
}
+EXPORT_SYMBOL_GPL(kunit_try_catch_run);
void kunit_try_catch_init(struct kunit_try_catch *try_catch,
struct kunit *test,
@@ -116,3 +119,4 @@ void kunit_try_catch_init(struct kunit_try_catch *try_catch,
try_catch->try = try;
try_catch->catch = catch;
}
+EXPORT_SYMBOL_GPL(kunit_try_catch_init);
@@ -1966,10 +1966,10 @@ config TEST_SYSCTL
If unsure, say N.
config SYSCTL_KUNIT_TEST
- bool "KUnit test for sysctl"
+ tristate "KUnit test for sysctl"
depends on KUNIT
help
- This builds the proc sysctl unit test, which runs on boot.
+ This builds the proc sysctl unit test, which runs on boot/module load.
Tests the API contract and implementation correctness of sysctl.
For more information on KUnit and unit tests in general please refer
to the KUnit documentation in Documentation/dev-tools/kunit/.