Skip to content

Commit 1d71307

Browse files
dlatypovshuahkh
authored andcommitted
kunit: add unit test for filtering suites by names
This adds unit tests for kunit_filter_subsuite() and kunit_filter_suites(). Note: what the executor means by "subsuite" is the array of suites corresponding to each test file. This patch lightly refactors executor.c to avoid the use of global variables to make it testable. It also includes a clever `kfree_at_end()` helper that makes this test easier to write than it otherwise would have been. Tested by running just the new tests using itself $ ./tools/testing/kunit/kunit.py run '*exec*' Signed-off-by: Daniel Latypov <[email protected]> Reviewed-by: David Gow <[email protected]> Acked-by: Brendan Higgins <[email protected]> Tested-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 40eb5cf commit 1d71307

File tree

2 files changed

+152
-14
lines changed

2 files changed

+152
-14
lines changed

lib/kunit/executor.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ extern struct kunit_suite * const * const __kunit_suites_end[];
1414

1515
#if IS_BUILTIN(CONFIG_KUNIT)
1616

17-
static char *filter_glob;
18-
module_param(filter_glob, charp, 0);
17+
static char *filter_glob_param;
18+
module_param_named(filter_glob, filter_glob_param, charp, 0);
1919
MODULE_PARM_DESC(filter_glob,
2020
"Filter which KUnit test suites run at boot-time, e.g. list*");
2121

2222
static char *kunit_shutdown;
2323
core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
2424

2525
static struct kunit_suite * const *
26-
kunit_filter_subsuite(struct kunit_suite * const * const subsuite)
26+
kunit_filter_subsuite(struct kunit_suite * const * const subsuite,
27+
const char *filter_glob)
2728
{
2829
int i, n = 0;
2930
struct kunit_suite **filtered;
@@ -56,19 +57,14 @@ struct suite_set {
5657
struct kunit_suite * const * const *end;
5758
};
5859

59-
static struct suite_set kunit_filter_suites(void)
60+
static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
61+
const char *filter_glob)
6062
{
6163
int i;
6264
struct kunit_suite * const **copy, * const *filtered_subsuite;
6365
struct suite_set filtered;
6466

65-
const size_t max = __kunit_suites_end - __kunit_suites_start;
66-
67-
if (!filter_glob) {
68-
filtered.start = __kunit_suites_start;
69-
filtered.end = __kunit_suites_end;
70-
return filtered;
71-
}
67+
const size_t max = suite_set->end - suite_set->start;
7268

7369
copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
7470
filtered.start = copy;
@@ -78,7 +74,7 @@ static struct suite_set kunit_filter_suites(void)
7874
}
7975

8076
for (i = 0; i < max; ++i) {
81-
filtered_subsuite = kunit_filter_subsuite(__kunit_suites_start[i]);
77+
filtered_subsuite = kunit_filter_subsuite(suite_set->start[i], filter_glob);
8278
if (filtered_subsuite)
8379
*copy++ = filtered_subsuite;
8480
}
@@ -116,15 +112,20 @@ static void kunit_print_tap_header(struct suite_set *suite_set)
116112
int kunit_run_all_tests(void)
117113
{
118114
struct kunit_suite * const * const *suites;
115+
struct suite_set suite_set = {
116+
.start = __kunit_suites_start,
117+
.end = __kunit_suites_end,
118+
};
119119

120-
struct suite_set suite_set = kunit_filter_suites();
120+
if (filter_glob_param)
121+
suite_set = kunit_filter_suites(&suite_set, filter_glob_param);
121122

122123
kunit_print_tap_header(&suite_set);
123124

124125
for (suites = suite_set.start; suites < suite_set.end; suites++)
125126
__kunit_test_suites_init(*suites);
126127

127-
if (filter_glob) { /* a copy was made of each array */
128+
if (filter_glob_param) { /* a copy was made of each array */
128129
for (suites = suite_set.start; suites < suite_set.end; suites++)
129130
kfree(*suites);
130131
kfree(suite_set.start);
@@ -135,4 +136,8 @@ int kunit_run_all_tests(void)
135136
return 0;
136137
}
137138

139+
#if IS_BUILTIN(CONFIG_KUNIT_TEST)
140+
#include "executor_test.c"
141+
#endif
142+
138143
#endif /* IS_BUILTIN(CONFIG_KUNIT) */

lib/kunit/executor_test.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* KUnit test for the KUnit executor.
4+
*
5+
* Copyright (C) 2021, Google LLC.
6+
* Author: Daniel Latypov <[email protected]>
7+
*/
8+
9+
#include <kunit/test.h>
10+
11+
static void kfree_at_end(struct kunit *test, const void *to_free);
12+
static struct kunit_suite *alloc_fake_suite(struct kunit *test,
13+
const char *suite_name);
14+
15+
static void filter_subsuite_test(struct kunit *test)
16+
{
17+
struct kunit_suite *subsuite[3] = {NULL, NULL, NULL};
18+
struct kunit_suite * const *filtered;
19+
20+
subsuite[0] = alloc_fake_suite(test, "suite1");
21+
subsuite[1] = alloc_fake_suite(test, "suite2");
22+
23+
/* Want: suite1, suite2, NULL -> suite2, NULL */
24+
filtered = kunit_filter_subsuite(subsuite, "suite2*");
25+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered);
26+
kfree_at_end(test, filtered);
27+
28+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered[0]);
29+
KUNIT_EXPECT_STREQ(test, (const char *)filtered[0]->name, "suite2");
30+
31+
KUNIT_EXPECT_FALSE(test, filtered[1]);
32+
}
33+
34+
static void filter_subsuite_to_empty_test(struct kunit *test)
35+
{
36+
struct kunit_suite *subsuite[3] = {NULL, NULL, NULL};
37+
struct kunit_suite * const *filtered;
38+
39+
subsuite[0] = alloc_fake_suite(test, "suite1");
40+
subsuite[1] = alloc_fake_suite(test, "suite2");
41+
42+
filtered = kunit_filter_subsuite(subsuite, "not_found");
43+
kfree_at_end(test, filtered); /* just in case */
44+
45+
KUNIT_EXPECT_FALSE_MSG(test, filtered,
46+
"should be NULL to indicate no match");
47+
}
48+
49+
static void kfree_subsuites_at_end(struct kunit *test, struct suite_set *suite_set)
50+
{
51+
struct kunit_suite * const * const *suites;
52+
53+
kfree_at_end(test, suite_set->start);
54+
for (suites = suite_set->start; suites < suite_set->end; suites++)
55+
kfree_at_end(test, *suites);
56+
}
57+
58+
static void filter_suites_test(struct kunit *test)
59+
{
60+
/* Suites per-file are stored as a NULL terminated array */
61+
struct kunit_suite *subsuites[2][2] = {
62+
{NULL, NULL},
63+
{NULL, NULL},
64+
};
65+
/* Match the memory layout of suite_set */
66+
struct kunit_suite * const * const suites[2] = {
67+
subsuites[0], subsuites[1],
68+
};
69+
70+
const struct suite_set suite_set = {
71+
.start = suites,
72+
.end = suites + 2,
73+
};
74+
struct suite_set filtered = {.start = NULL, .end = NULL};
75+
76+
/* Emulate two files, each having one suite */
77+
subsuites[0][0] = alloc_fake_suite(test, "suite0");
78+
subsuites[1][0] = alloc_fake_suite(test, "suite1");
79+
80+
/* Filter out suite1 */
81+
filtered = kunit_filter_suites(&suite_set, "suite0");
82+
kfree_subsuites_at_end(test, &filtered); /* let us use ASSERTs without leaking */
83+
KUNIT_ASSERT_EQ(test, filtered.end - filtered.start, (ptrdiff_t)1);
84+
85+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered.start);
86+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, filtered.start[0]);
87+
KUNIT_EXPECT_STREQ(test, (const char *)filtered.start[0][0]->name, "suite0");
88+
}
89+
90+
static struct kunit_case executor_test_cases[] = {
91+
KUNIT_CASE(filter_subsuite_test),
92+
KUNIT_CASE(filter_subsuite_to_empty_test),
93+
KUNIT_CASE(filter_suites_test),
94+
{}
95+
};
96+
97+
static struct kunit_suite executor_test_suite = {
98+
.name = "kunit_executor_test",
99+
.test_cases = executor_test_cases,
100+
};
101+
102+
kunit_test_suites(&executor_test_suite);
103+
104+
/* Test helpers */
105+
106+
static void kfree_res_free(struct kunit_resource *res)
107+
{
108+
kfree(res->data);
109+
}
110+
111+
/* Use the resource API to register a call to kfree(to_free).
112+
* Since we never actually use the resource, it's safe to use on const data.
113+
*/
114+
static void kfree_at_end(struct kunit *test, const void *to_free)
115+
{
116+
/* kfree() handles NULL already, but avoid allocating a no-op cleanup. */
117+
if (IS_ERR_OR_NULL(to_free))
118+
return;
119+
kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
120+
(void *)to_free);
121+
}
122+
123+
static struct kunit_suite *alloc_fake_suite(struct kunit *test,
124+
const char *suite_name)
125+
{
126+
struct kunit_suite *suite;
127+
128+
/* We normally never expect to allocate suites, hence the non-const cast. */
129+
suite = kunit_kzalloc(test, sizeof(*suite), GFP_KERNEL);
130+
strncpy((char *)suite->name, suite_name, sizeof(suite->name) - 1);
131+
132+
return suite;
133+
}

0 commit comments

Comments
 (0)