Skip to content

Commit 5f3e062

Browse files
bjh83shuahkh
authored andcommitted
kunit: test: add support for test abort
Add support for aborting/bailing out of test cases, which is needed for implementing assertions. An assertion is like an expectation, but bails out of the test case early if the assertion is not met. The idea with assertions is that you use them to state all the preconditions for your test. Logically speaking, these are the premises of the test case, so if a premise isn't true, there is no point in continuing the test case because there are no conclusions that can be drawn without the premises. Whereas, the expectation is the thing you are trying to prove. 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: Stephen Boyd <sboyd@kernel.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 33adf80 commit 5f3e062

5 files changed

Lines changed: 319 additions & 16 deletions

File tree

‎include/kunit/test.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define _KUNIT_TEST_H
1111

1212
#include <kunit/assert.h>
13+
#include <kunit/try-catch.h>
1314
#include <linux/kernel.h>
1415
#include <linux/slab.h>
1516
#include <linux/types.h>
@@ -172,6 +173,7 @@ struct kunit {
172173

173174
/* private: internal use only. */
174175
const char *name; /* Read only after initialization! */
176+
struct kunit_try_catch try_catch;
175177
/*
176178
* success starts as true, and may only be set to false during a
177179
* test case; thus, it is safe to update this across multiple

‎include/kunit/try-catch.h‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* An API to allow a function, that may fail, to be executed, and recover in a
4+
* controlled manner.
5+
*
6+
* Copyright (C) 2019, Google LLC.
7+
* Author: Brendan Higgins <brendanhiggins@google.com>
8+
*/
9+
10+
#ifndef _KUNIT_TRY_CATCH_H
11+
#define _KUNIT_TRY_CATCH_H
12+
13+
#include <linux/types.h>
14+
15+
typedef void (*kunit_try_catch_func_t)(void *);
16+
17+
struct completion;
18+
struct kunit;
19+
20+
/**
21+
* struct kunit_try_catch - provides a generic way to run code which might fail.
22+
* @test: The test case that is currently being executed.
23+
* @try_completion: Completion that the control thread waits on while test runs.
24+
* @try_result: Contains any errno obtained while running test case.
25+
* @try: The function, the test case, to attempt to run.
26+
* @catch: The function called if @try bails out.
27+
* @context: used to pass user data to the try and catch functions.
28+
*
29+
* kunit_try_catch provides a generic, architecture independent way to execute
30+
* an arbitrary function of type kunit_try_catch_func_t which may bail out by
31+
* calling kunit_try_catch_throw(). If kunit_try_catch_throw() is called, @try
32+
* is stopped at the site of invocation and @catch is called.
33+
*
34+
* struct kunit_try_catch provides a generic interface for the functionality
35+
* needed to implement kunit->abort() which in turn is needed for implementing
36+
* assertions. Assertions allow stating a precondition for a test simplifying
37+
* how test cases are written and presented.
38+
*
39+
* Assertions are like expectations, except they abort (call
40+
* kunit_try_catch_throw()) when the specified condition is not met. This is
41+
* useful when you look at a test case as a logical statement about some piece
42+
* of code, where assertions are the premises for the test case, and the
43+
* conclusion is a set of predicates, rather expectations, that must all be
44+
* true. If your premises are violated, it does not makes sense to continue.
45+
*/
46+
struct kunit_try_catch {
47+
/* private: internal use only. */
48+
struct kunit *test;
49+
struct completion *try_completion;
50+
int try_result;
51+
kunit_try_catch_func_t try;
52+
kunit_try_catch_func_t catch;
53+
void *context;
54+
};
55+
56+
void kunit_try_catch_init(struct kunit_try_catch *try_catch,
57+
struct kunit *test,
58+
kunit_try_catch_func_t try,
59+
kunit_try_catch_func_t catch);
60+
61+
void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context);
62+
63+
void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch);
64+
65+
static inline int kunit_try_catch_get_result(struct kunit_try_catch *try_catch)
66+
{
67+
return try_catch->try_result;
68+
}
69+
70+
/*
71+
* Exposed for testing only.
72+
*/
73+
void kunit_generic_try_catch_init(struct kunit_try_catch *try_catch);
74+
75+
#endif /* _KUNIT_TRY_CATCH_H */

‎lib/kunit/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
obj-$(CONFIG_KUNIT) += test.o \
22
string-stream.o \
3-
assert.o
3+
assert.o \
4+
try-catch.o
45

56
obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o
67

‎lib/kunit/test.c‎

Lines changed: 122 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
*/
88

99
#include <kunit/test.h>
10+
#include <kunit/try-catch.h>
1011
#include <linux/kernel.h>
12+
#include <linux/sched/debug.h>
1113

1214
static void kunit_set_failure(struct kunit *test)
1315
{
@@ -162,6 +164,19 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
162164
WARN_ON(string_stream_destroy(stream));
163165
}
164166

167+
static void __noreturn kunit_abort(struct kunit *test)
168+
{
169+
kunit_try_catch_throw(&test->try_catch); /* Does not return. */
170+
171+
/*
172+
* Throw could not abort from test.
173+
*
174+
* XXX: we should never reach this line! As kunit_try_catch_throw is
175+
* marked __noreturn.
176+
*/
177+
WARN_ONCE(true, "Throw could not abort from test!\n");
178+
}
179+
165180
void kunit_do_assertion(struct kunit *test,
166181
struct kunit_assert *assert,
167182
bool pass,
@@ -180,6 +195,9 @@ void kunit_do_assertion(struct kunit *test,
180195
kunit_fail(test, assert);
181196

182197
va_end(args);
198+
199+
if (assert->type == KUNIT_ASSERTION)
200+
kunit_abort(test);
183201
}
184202

185203
void kunit_init_test(struct kunit *test, const char *name)
@@ -191,33 +209,122 @@ void kunit_init_test(struct kunit *test, const char *name)
191209
}
192210

193211
/*
194-
* Performs all logic to run a test case.
212+
* Initializes and runs test case. Does not clean up or do post validations.
195213
*/
196-
static void kunit_run_case(struct kunit_suite *suite,
197-
struct kunit_case *test_case)
214+
static void kunit_run_case_internal(struct kunit *test,
215+
struct kunit_suite *suite,
216+
struct kunit_case *test_case)
198217
{
199-
struct kunit test;
200-
201-
kunit_init_test(&test, test_case->name);
202-
203218
if (suite->init) {
204219
int ret;
205220

206-
ret = suite->init(&test);
221+
ret = suite->init(test);
207222
if (ret) {
208-
kunit_err(&test, "failed to initialize: %d\n", ret);
209-
kunit_set_failure(&test);
210-
test_case->success = test.success;
223+
kunit_err(test, "failed to initialize: %d\n", ret);
224+
kunit_set_failure(test);
211225
return;
212226
}
213227
}
214228

215-
test_case->run_case(&test);
229+
test_case->run_case(test);
230+
}
216231

232+
static void kunit_case_internal_cleanup(struct kunit *test)
233+
{
234+
kunit_cleanup(test);
235+
}
236+
237+
/*
238+
* Performs post validations and cleanup after a test case was run.
239+
* XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
240+
*/
241+
static void kunit_run_case_cleanup(struct kunit *test,
242+
struct kunit_suite *suite)
243+
{
217244
if (suite->exit)
218-
suite->exit(&test);
245+
suite->exit(test);
246+
247+
kunit_case_internal_cleanup(test);
248+
}
249+
250+
struct kunit_try_catch_context {
251+
struct kunit *test;
252+
struct kunit_suite *suite;
253+
struct kunit_case *test_case;
254+
};
255+
256+
static void kunit_try_run_case(void *data)
257+
{
258+
struct kunit_try_catch_context *ctx = data;
259+
struct kunit *test = ctx->test;
260+
struct kunit_suite *suite = ctx->suite;
261+
struct kunit_case *test_case = ctx->test_case;
262+
263+
/*
264+
* kunit_run_case_internal may encounter a fatal error; if it does,
265+
* abort will be called, this thread will exit, and finally the parent
266+
* thread will resume control and handle any necessary clean up.
267+
*/
268+
kunit_run_case_internal(test, suite, test_case);
269+
/* This line may never be reached. */
270+
kunit_run_case_cleanup(test, suite);
271+
}
272+
273+
static void kunit_catch_run_case(void *data)
274+
{
275+
struct kunit_try_catch_context *ctx = data;
276+
struct kunit *test = ctx->test;
277+
struct kunit_suite *suite = ctx->suite;
278+
int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
279+
280+
if (try_exit_code) {
281+
kunit_set_failure(test);
282+
/*
283+
* Test case could not finish, we have no idea what state it is
284+
* in, so don't do clean up.
285+
*/
286+
if (try_exit_code == -ETIMEDOUT) {
287+
kunit_err(test, "test case timed out\n");
288+
/*
289+
* Unknown internal error occurred preventing test case from
290+
* running, so there is nothing to clean up.
291+
*/
292+
} else {
293+
kunit_err(test, "internal error occurred preventing test case from running: %d\n",
294+
try_exit_code);
295+
}
296+
return;
297+
}
298+
299+
/*
300+
* Test case was run, but aborted. It is the test case's business as to
301+
* whether it failed or not, we just need to clean up.
302+
*/
303+
kunit_run_case_cleanup(test, suite);
304+
}
305+
306+
/*
307+
* Performs all logic to run a test case. It also catches most errors that
308+
* occur in a test case and reports them as failures.
309+
*/
310+
static void kunit_run_case_catch_errors(struct kunit_suite *suite,
311+
struct kunit_case *test_case)
312+
{
313+
struct kunit_try_catch_context context;
314+
struct kunit_try_catch *try_catch;
315+
struct kunit test;
316+
317+
kunit_init_test(&test, test_case->name);
318+
try_catch = &test.try_catch;
219319

220-
kunit_cleanup(&test);
320+
kunit_try_catch_init(try_catch,
321+
&test,
322+
kunit_try_run_case,
323+
kunit_catch_run_case);
324+
context.test = &test;
325+
context.suite = suite;
326+
context.test_case = test_case;
327+
kunit_try_catch_run(try_catch, &context);
221328

222329
test_case->success = test.success;
223330
}
@@ -230,7 +337,7 @@ int kunit_run_tests(struct kunit_suite *suite)
230337
kunit_print_subtest_start(suite);
231338

232339
for (test_case = suite->test_cases; test_case->run_case; test_case++) {
233-
kunit_run_case(suite, test_case);
340+
kunit_run_case_catch_errors(suite, test_case);
234341
kunit_print_test_case_ok_not_ok(test_case, test_case_count++);
235342
}
236343

0 commit comments

Comments
 (0)