Skip to content

Commit 76066f9

Browse files
rmr167shuahkh
authored andcommitted
kunit: add tests for filtering attributes
Add four tests to executor_test.c to test behavior of filtering attributes. - parse_filter_attr_test - to test the parsing of inputted filters - filter_attr_test - to test the filtering procedure on attributes - filter_attr_empty_test - to test the behavior when all tests are filtered out - filter_attr_skip_test - to test the configurable filter_action=skip option Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Rae Moar <rmoar@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent a547c4c commit 76066f9

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

lib/kunit/executor_test.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <kunit/test.h>
10+
#include <kunit/attributes.h>
1011

1112
static void kfree_at_end(struct kunit *test, const void *to_free);
1213
static struct kunit_suite *alloc_fake_suite(struct kunit *test,
@@ -108,11 +109,126 @@ static void filter_suites_to_empty_test(struct kunit *test)
108109
"should be empty to indicate no match");
109110
}
110111

112+
static void parse_filter_attr_test(struct kunit *test)
113+
{
114+
int j, filter_count;
115+
struct kunit_attr_filter *parsed_filters;
116+
char *filters = "speed>slow, module!=example";
117+
int err = 0;
118+
119+
filter_count = kunit_get_filter_count(filters);
120+
KUNIT_EXPECT_EQ(test, filter_count, 2);
121+
122+
parsed_filters = kunit_kcalloc(test, filter_count + 1, sizeof(*parsed_filters),
123+
GFP_KERNEL);
124+
for (j = 0; j < filter_count; j++) {
125+
parsed_filters[j] = kunit_next_attr_filter(&filters, &err);
126+
KUNIT_ASSERT_EQ_MSG(test, err, 0, "failed to parse filter '%s'", filters[j]);
127+
}
128+
129+
KUNIT_EXPECT_STREQ(test, kunit_attr_filter_name(parsed_filters[0]), "speed");
130+
KUNIT_EXPECT_STREQ(test, parsed_filters[0].input, ">slow");
131+
132+
KUNIT_EXPECT_STREQ(test, kunit_attr_filter_name(parsed_filters[1]), "module");
133+
KUNIT_EXPECT_STREQ(test, parsed_filters[1].input, "!=example");
134+
}
135+
136+
static struct kunit_case dummy_attr_test_cases[] = {
137+
/* .run_case is not important, just needs to be non-NULL */
138+
{ .name = "slow", .run_case = dummy_test, .module_name = "dummy",
139+
.attr.speed = KUNIT_SPEED_SLOW },
140+
{ .name = "normal", .run_case = dummy_test, .module_name = "dummy" },
141+
{},
142+
};
143+
144+
static void filter_attr_test(struct kunit *test)
145+
{
146+
struct kunit_suite *subsuite[3] = {NULL, NULL};
147+
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
148+
struct suite_set got;
149+
int err = 0;
150+
151+
subsuite[0] = alloc_fake_suite(test, "normal_suite", dummy_attr_test_cases);
152+
subsuite[1] = alloc_fake_suite(test, "slow_suite", dummy_attr_test_cases);
153+
subsuite[1]->attr.speed = KUNIT_SPEED_SLOW; // Set suite attribute
154+
155+
/*
156+
* Want: normal_suite(slow, normal), slow_suite(slow, normal),
157+
* NULL -> normal_suite(normal), NULL
158+
*
159+
* The normal test in slow_suite is filtered out because the speed
160+
* attribute is unset and thus, the filtering is based on the parent attribute
161+
* of slow.
162+
*/
163+
got = kunit_filter_suites(&suite_set, NULL, "speed>slow", NULL, &err);
164+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start);
165+
KUNIT_ASSERT_EQ(test, err, 0);
166+
kfree_at_end(test, got.start);
167+
168+
/* Validate we just have normal_suite */
169+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]);
170+
KUNIT_EXPECT_STREQ(test, got.start[0]->name, "normal_suite");
171+
KUNIT_ASSERT_EQ(test, got.end - got.start, 1);
172+
173+
/* Now validate we just have normal test case */
174+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]->test_cases);
175+
KUNIT_EXPECT_STREQ(test, got.start[0]->test_cases[0].name, "normal");
176+
KUNIT_EXPECT_FALSE(test, got.start[0]->test_cases[1].name);
177+
}
178+
179+
static void filter_attr_empty_test(struct kunit *test)
180+
{
181+
struct kunit_suite *subsuite[3] = {NULL, NULL};
182+
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[2]};
183+
struct suite_set got;
184+
int err = 0;
185+
186+
subsuite[0] = alloc_fake_suite(test, "suite1", dummy_attr_test_cases);
187+
subsuite[1] = alloc_fake_suite(test, "suite2", dummy_attr_test_cases);
188+
189+
got = kunit_filter_suites(&suite_set, NULL, "module!=dummy", NULL, &err);
190+
KUNIT_ASSERT_EQ(test, err, 0);
191+
kfree_at_end(test, got.start); /* just in case */
192+
193+
KUNIT_EXPECT_PTR_EQ_MSG(test, got.start, got.end,
194+
"should be empty to indicate no match");
195+
}
196+
197+
static void filter_attr_skip_test(struct kunit *test)
198+
{
199+
struct kunit_suite *subsuite[2] = {NULL};
200+
struct suite_set suite_set = {.start = subsuite, .end = &subsuite[1]};
201+
struct suite_set got;
202+
int err = 0;
203+
204+
subsuite[0] = alloc_fake_suite(test, "suite", dummy_attr_test_cases);
205+
206+
/* Want: suite(slow, normal), NULL -> suite(slow with SKIP, normal), NULL */
207+
got = kunit_filter_suites(&suite_set, NULL, "speed>slow", "skip", &err);
208+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start);
209+
KUNIT_ASSERT_EQ(test, err, 0);
210+
kfree_at_end(test, got.start);
211+
212+
/* Validate we have both the slow and normal test */
213+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, got.start[0]->test_cases);
214+
KUNIT_ASSERT_EQ(test, kunit_suite_num_test_cases(got.start[0]), 2);
215+
KUNIT_EXPECT_STREQ(test, got.start[0]->test_cases[0].name, "slow");
216+
KUNIT_EXPECT_STREQ(test, got.start[0]->test_cases[1].name, "normal");
217+
218+
/* Now ensure slow is skipped and normal is not */
219+
KUNIT_EXPECT_EQ(test, got.start[0]->test_cases[0].status, KUNIT_SKIPPED);
220+
KUNIT_EXPECT_FALSE(test, got.start[0]->test_cases[1].status);
221+
}
222+
111223
static struct kunit_case executor_test_cases[] = {
112224
KUNIT_CASE(parse_filter_test),
113225
KUNIT_CASE(filter_suites_test),
114226
KUNIT_CASE(filter_suites_test_glob_test),
115227
KUNIT_CASE(filter_suites_to_empty_test),
228+
KUNIT_CASE(parse_filter_attr_test),
229+
KUNIT_CASE(filter_attr_test),
230+
KUNIT_CASE(filter_attr_empty_test),
231+
KUNIT_CASE(filter_attr_skip_test),
116232
{}
117233
};
118234

0 commit comments

Comments
 (0)