Skip to content

Commit 914cc63

Browse files
bjh83shuahkh
authored andcommitted
kunit: test: add KUnit test runner core
Add core facilities for defining unit tests; this provides a common way to define test cases, functions that execute code which is under test and determine whether the code under test behaves as expected; this also provides a way to group together related test cases in test suites (here we call them test_modules). Just define test cases and how to execute them for now; setting expectations on code will be defined later. Signed-off-by: Brendan Higgins <brendanhiggins@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Logan Gunthorpe <logang@deltatee.com> Reviewed-by: Luis Chamberlain <mcgrof@kernel.org> Reviewed-by: Stephen Boyd <sboyd@kernel.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 54ecb8f commit 914cc63

4 files changed

Lines changed: 393 additions & 0 deletions

File tree

‎include/kunit/test.h‎

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Base unit test (KUnit) API.
4+
*
5+
* Copyright (C) 2019, Google LLC.
6+
* Author: Brendan Higgins <brendanhiggins@google.com>
7+
*/
8+
9+
#ifndef _KUNIT_TEST_H
10+
#define _KUNIT_TEST_H
11+
12+
#include <linux/types.h>
13+
14+
struct kunit;
15+
16+
/**
17+
* struct kunit_case - represents an individual test case.
18+
*
19+
* @run_case: the function representing the actual test case.
20+
* @name: the name of the test case.
21+
*
22+
* A test case is a function with the signature,
23+
* ``void (*)(struct kunit *)`` that makes expectations (see
24+
* KUNIT_EXPECT_TRUE()) about code under test. Each test case is associated
25+
* with a &struct kunit_suite and will be run after the suite's init
26+
* function and followed by the suite's exit function.
27+
*
28+
* A test case should be static and should only be created with the
29+
* KUNIT_CASE() macro; additionally, every array of test cases should be
30+
* terminated with an empty test case.
31+
*
32+
* Example:
33+
*
34+
* .. code-block:: c
35+
*
36+
* void add_test_basic(struct kunit *test)
37+
* {
38+
* KUNIT_EXPECT_EQ(test, 1, add(1, 0));
39+
* KUNIT_EXPECT_EQ(test, 2, add(1, 1));
40+
* KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
41+
* KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
42+
* KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
43+
* }
44+
*
45+
* static struct kunit_case example_test_cases[] = {
46+
* KUNIT_CASE(add_test_basic),
47+
* {}
48+
* };
49+
*
50+
*/
51+
struct kunit_case {
52+
void (*run_case)(struct kunit *test);
53+
const char *name;
54+
55+
/* private: internal use only. */
56+
bool success;
57+
};
58+
59+
/**
60+
* KUNIT_CASE - A helper for creating a &struct kunit_case
61+
*
62+
* @test_name: a reference to a test case function.
63+
*
64+
* Takes a symbol for a function representing a test case and creates a
65+
* &struct kunit_case object from it. See the documentation for
66+
* &struct kunit_case for an example on how to use it.
67+
*/
68+
#define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
69+
70+
/**
71+
* struct kunit_suite - describes a related collection of &struct kunit_case
72+
*
73+
* @name: the name of the test. Purely informational.
74+
* @init: called before every test case.
75+
* @exit: called after every test case.
76+
* @test_cases: a null terminated array of test cases.
77+
*
78+
* A kunit_suite is a collection of related &struct kunit_case s, such that
79+
* @init is called before every test case and @exit is called after every
80+
* test case, similar to the notion of a *test fixture* or a *test class*
81+
* in other unit testing frameworks like JUnit or Googletest.
82+
*
83+
* Every &struct kunit_case must be associated with a kunit_suite for KUnit
84+
* to run it.
85+
*/
86+
struct kunit_suite {
87+
const char name[256];
88+
int (*init)(struct kunit *test);
89+
void (*exit)(struct kunit *test);
90+
struct kunit_case *test_cases;
91+
};
92+
93+
/**
94+
* struct kunit - represents a running instance of a test.
95+
*
96+
* @priv: for user to store arbitrary data. Commonly used to pass data
97+
* created in the init function (see &struct kunit_suite).
98+
*
99+
* Used to store information about the current context under which the test
100+
* is running. Most of this data is private and should only be accessed
101+
* indirectly via public functions; the one exception is @priv which can be
102+
* used by the test writer to store arbitrary data.
103+
*/
104+
struct kunit {
105+
void *priv;
106+
107+
/* private: internal use only. */
108+
const char *name; /* Read only after initialization! */
109+
/*
110+
* success starts as true, and may only be set to false during a
111+
* test case; thus, it is safe to update this across multiple
112+
* threads using WRITE_ONCE; however, as a consequence, it may only
113+
* be read after the test case finishes once all threads associated
114+
* with the test case have terminated.
115+
*/
116+
bool success; /* Read only after test_case finishes! */
117+
};
118+
119+
void kunit_init_test(struct kunit *test, const char *name);
120+
121+
int kunit_run_tests(struct kunit_suite *suite);
122+
123+
/**
124+
* kunit_test_suite() - used to register a &struct kunit_suite with KUnit.
125+
*
126+
* @suite: a statically allocated &struct kunit_suite.
127+
*
128+
* Registers @suite with the test framework. See &struct kunit_suite for
129+
* more information.
130+
*
131+
* NOTE: Currently KUnit tests are all run as late_initcalls; this means
132+
* that they cannot test anything where tests must run at a different init
133+
* phase. One significant restriction resulting from this is that KUnit
134+
* cannot reliably test anything that is initialize in the late_init phase;
135+
* another is that KUnit is useless to test things that need to be run in
136+
* an earlier init phase.
137+
*
138+
* TODO(brendanhiggins@google.com): Don't run all KUnit tests as
139+
* late_initcalls. I have some future work planned to dispatch all KUnit
140+
* tests from the same place, and at the very least to do so after
141+
* everything else is definitely initialized.
142+
*/
143+
#define kunit_test_suite(suite) \
144+
static int kunit_suite_init##suite(void) \
145+
{ \
146+
return kunit_run_tests(&suite); \
147+
} \
148+
late_initcall(kunit_suite_init##suite)
149+
150+
void __printf(3, 4) kunit_printk(const char *level,
151+
const struct kunit *test,
152+
const char *fmt, ...);
153+
154+
/**
155+
* kunit_info() - Prints an INFO level message associated with @test.
156+
*
157+
* @test: The test context object.
158+
* @fmt: A printk() style format string.
159+
*
160+
* Prints an info level message associated with the test suite being run.
161+
* Takes a variable number of format parameters just like printk().
162+
*/
163+
#define kunit_info(test, fmt, ...) \
164+
kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
165+
166+
/**
167+
* kunit_warn() - Prints a WARN level message associated with @test.
168+
*
169+
* @test: The test context object.
170+
* @fmt: A printk() style format string.
171+
*
172+
* Prints a warning level message.
173+
*/
174+
#define kunit_warn(test, fmt, ...) \
175+
kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
176+
177+
/**
178+
* kunit_err() - Prints an ERROR level message associated with @test.
179+
*
180+
* @test: The test context object.
181+
* @fmt: A printk() style format string.
182+
*
183+
* Prints an error level message.
184+
*/
185+
#define kunit_err(test, fmt, ...) \
186+
kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
187+
188+
#endif /* _KUNIT_TEST_H */

‎lib/kunit/Kconfig‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# KUnit base configuration
3+
#
4+
5+
menuconfig KUNIT
6+
bool "KUnit - Enable support for unit tests"
7+
help
8+
Enables support for kernel unit tests (KUnit), a lightweight unit
9+
testing and mocking framework for the Linux kernel. These tests are
10+
able to be run locally on a developer's workstation without a VM or
11+
special hardware when using UML. Can also be used on most other
12+
architectures. For more information, please see
13+
Documentation/dev-tools/kunit/.

‎lib/kunit/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obj-$(CONFIG_KUNIT) += test.o

‎lib/kunit/test.c‎

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Base unit test (KUnit) API.
4+
*
5+
* Copyright (C) 2019, Google LLC.
6+
* Author: Brendan Higgins <brendanhiggins@google.com>
7+
*/
8+
9+
#include <kunit/test.h>
10+
#include <linux/kernel.h>
11+
12+
static void kunit_set_failure(struct kunit *test)
13+
{
14+
WRITE_ONCE(test->success, false);
15+
}
16+
17+
static int kunit_vprintk_emit(int level, const char *fmt, va_list args)
18+
{
19+
return vprintk_emit(0, level, NULL, 0, fmt, args);
20+
}
21+
22+
static int kunit_printk_emit(int level, const char *fmt, ...)
23+
{
24+
va_list args;
25+
int ret;
26+
27+
va_start(args, fmt);
28+
ret = kunit_vprintk_emit(level, fmt, args);
29+
va_end(args);
30+
31+
return ret;
32+
}
33+
34+
static void kunit_vprintk(const struct kunit *test,
35+
const char *level,
36+
struct va_format *vaf)
37+
{
38+
kunit_printk_emit(level[1] - '0', "\t# %s: %pV", test->name, vaf);
39+
}
40+
41+
static void kunit_print_tap_version(void)
42+
{
43+
static bool kunit_has_printed_tap_version;
44+
45+
if (!kunit_has_printed_tap_version) {
46+
kunit_printk_emit(LOGLEVEL_INFO, "TAP version 14\n");
47+
kunit_has_printed_tap_version = true;
48+
}
49+
}
50+
51+
static size_t kunit_test_cases_len(struct kunit_case *test_cases)
52+
{
53+
struct kunit_case *test_case;
54+
size_t len = 0;
55+
56+
for (test_case = test_cases; test_case->run_case; test_case++)
57+
len++;
58+
59+
return len;
60+
}
61+
62+
static void kunit_print_subtest_start(struct kunit_suite *suite)
63+
{
64+
kunit_print_tap_version();
65+
kunit_printk_emit(LOGLEVEL_INFO, "\t# Subtest: %s\n", suite->name);
66+
kunit_printk_emit(LOGLEVEL_INFO,
67+
"\t1..%zd\n",
68+
kunit_test_cases_len(suite->test_cases));
69+
}
70+
71+
static void kunit_print_ok_not_ok(bool should_indent,
72+
bool is_ok,
73+
size_t test_number,
74+
const char *description)
75+
{
76+
const char *indent, *ok_not_ok;
77+
78+
if (should_indent)
79+
indent = "\t";
80+
else
81+
indent = "";
82+
83+
if (is_ok)
84+
ok_not_ok = "ok";
85+
else
86+
ok_not_ok = "not ok";
87+
88+
kunit_printk_emit(LOGLEVEL_INFO,
89+
"%s%s %zd - %s\n",
90+
indent, ok_not_ok, test_number, description);
91+
}
92+
93+
static bool kunit_suite_has_succeeded(struct kunit_suite *suite)
94+
{
95+
const struct kunit_case *test_case;
96+
97+
for (test_case = suite->test_cases; test_case->run_case; test_case++)
98+
if (!test_case->success)
99+
return false;
100+
101+
return true;
102+
}
103+
104+
static void kunit_print_subtest_end(struct kunit_suite *suite)
105+
{
106+
static size_t kunit_suite_counter = 1;
107+
108+
kunit_print_ok_not_ok(false,
109+
kunit_suite_has_succeeded(suite),
110+
kunit_suite_counter++,
111+
suite->name);
112+
}
113+
114+
static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case,
115+
size_t test_number)
116+
{
117+
kunit_print_ok_not_ok(true,
118+
test_case->success,
119+
test_number,
120+
test_case->name);
121+
}
122+
123+
void kunit_init_test(struct kunit *test, const char *name)
124+
{
125+
test->name = name;
126+
test->success = true;
127+
}
128+
129+
/*
130+
* Performs all logic to run a test case.
131+
*/
132+
static void kunit_run_case(struct kunit_suite *suite,
133+
struct kunit_case *test_case)
134+
{
135+
struct kunit test;
136+
137+
kunit_init_test(&test, test_case->name);
138+
139+
if (suite->init) {
140+
int ret;
141+
142+
ret = suite->init(&test);
143+
if (ret) {
144+
kunit_err(&test, "failed to initialize: %d\n", ret);
145+
kunit_set_failure(&test);
146+
test_case->success = test.success;
147+
return;
148+
}
149+
}
150+
151+
test_case->run_case(&test);
152+
153+
if (suite->exit)
154+
suite->exit(&test);
155+
156+
test_case->success = test.success;
157+
}
158+
159+
int kunit_run_tests(struct kunit_suite *suite)
160+
{
161+
struct kunit_case *test_case;
162+
size_t test_case_count = 1;
163+
164+
kunit_print_subtest_start(suite);
165+
166+
for (test_case = suite->test_cases; test_case->run_case; test_case++) {
167+
kunit_run_case(suite, test_case);
168+
kunit_print_test_case_ok_not_ok(test_case, test_case_count++);
169+
}
170+
171+
kunit_print_subtest_end(suite);
172+
173+
return 0;
174+
}
175+
176+
void kunit_printk(const char *level,
177+
const struct kunit *test,
178+
const char *fmt, ...)
179+
{
180+
struct va_format vaf;
181+
va_list args;
182+
183+
va_start(args, fmt);
184+
185+
vaf.fmt = fmt;
186+
vaf.va = &args;
187+
188+
kunit_vprintk(test, level, &vaf);
189+
190+
va_end(args);
191+
}

0 commit comments

Comments
 (0)