Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Brendan Higgins and committed by
Shuah Khan
5f3e0620 33adf80f

+319 -16
+2
include/kunit/test.h
··· 10 10 #define _KUNIT_TEST_H 11 11 12 12 #include <kunit/assert.h> 13 + #include <kunit/try-catch.h> 13 14 #include <linux/kernel.h> 14 15 #include <linux/slab.h> 15 16 #include <linux/types.h> ··· 173 172 174 173 /* private: internal use only. */ 175 174 const char *name; /* Read only after initialization! */ 175 + struct kunit_try_catch try_catch; 176 176 /* 177 177 * success starts as true, and may only be set to false during a 178 178 * test case; thus, it is safe to update this across multiple
+75
include/kunit/try-catch.h
··· 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 */
+2 -1
lib/kunit/Makefile
··· 1 1 obj-$(CONFIG_KUNIT) += test.o \ 2 2 string-stream.o \ 3 - assert.o 3 + assert.o \ 4 + try-catch.o 4 5 5 6 obj-$(CONFIG_KUNIT_TEST) += string-stream-test.o 6 7
+122 -15
lib/kunit/test.c
··· 7 7 */ 8 8 9 9 #include <kunit/test.h> 10 + #include <kunit/try-catch.h> 10 11 #include <linux/kernel.h> 12 + #include <linux/sched/debug.h> 11 13 12 14 static void kunit_set_failure(struct kunit *test) 13 15 { ··· 164 162 WARN_ON(string_stream_destroy(stream)); 165 163 } 166 164 165 + static void __noreturn kunit_abort(struct kunit *test) 166 + { 167 + kunit_try_catch_throw(&test->try_catch); /* Does not return. */ 168 + 169 + /* 170 + * Throw could not abort from test. 171 + * 172 + * XXX: we should never reach this line! As kunit_try_catch_throw is 173 + * marked __noreturn. 174 + */ 175 + WARN_ONCE(true, "Throw could not abort from test!\n"); 176 + } 177 + 167 178 void kunit_do_assertion(struct kunit *test, 168 179 struct kunit_assert *assert, 169 180 bool pass, ··· 195 180 kunit_fail(test, assert); 196 181 197 182 va_end(args); 183 + 184 + if (assert->type == KUNIT_ASSERTION) 185 + kunit_abort(test); 198 186 } 199 187 200 188 void kunit_init_test(struct kunit *test, const char *name) ··· 209 191 } 210 192 211 193 /* 212 - * Performs all logic to run a test case. 194 + * Initializes and runs test case. Does not clean up or do post validations. 213 195 */ 214 - static void kunit_run_case(struct kunit_suite *suite, 215 - struct kunit_case *test_case) 196 + static void kunit_run_case_internal(struct kunit *test, 197 + struct kunit_suite *suite, 198 + struct kunit_case *test_case) 216 199 { 217 - struct kunit test; 218 - 219 - kunit_init_test(&test, test_case->name); 220 - 221 200 if (suite->init) { 222 201 int ret; 223 202 224 - ret = suite->init(&test); 203 + ret = suite->init(test); 225 204 if (ret) { 226 - kunit_err(&test, "failed to initialize: %d\n", ret); 227 - kunit_set_failure(&test); 228 - test_case->success = test.success; 205 + kunit_err(test, "failed to initialize: %d\n", ret); 206 + kunit_set_failure(test); 229 207 return; 230 208 } 231 209 } 232 210 233 - test_case->run_case(&test); 211 + test_case->run_case(test); 212 + } 234 213 214 + static void kunit_case_internal_cleanup(struct kunit *test) 215 + { 216 + kunit_cleanup(test); 217 + } 218 + 219 + /* 220 + * Performs post validations and cleanup after a test case was run. 221 + * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal! 222 + */ 223 + static void kunit_run_case_cleanup(struct kunit *test, 224 + struct kunit_suite *suite) 225 + { 235 226 if (suite->exit) 236 - suite->exit(&test); 227 + suite->exit(test); 237 228 238 - kunit_cleanup(&test); 229 + kunit_case_internal_cleanup(test); 230 + } 231 + 232 + struct kunit_try_catch_context { 233 + struct kunit *test; 234 + struct kunit_suite *suite; 235 + struct kunit_case *test_case; 236 + }; 237 + 238 + static void kunit_try_run_case(void *data) 239 + { 240 + struct kunit_try_catch_context *ctx = data; 241 + struct kunit *test = ctx->test; 242 + struct kunit_suite *suite = ctx->suite; 243 + struct kunit_case *test_case = ctx->test_case; 244 + 245 + /* 246 + * kunit_run_case_internal may encounter a fatal error; if it does, 247 + * abort will be called, this thread will exit, and finally the parent 248 + * thread will resume control and handle any necessary clean up. 249 + */ 250 + kunit_run_case_internal(test, suite, test_case); 251 + /* This line may never be reached. */ 252 + kunit_run_case_cleanup(test, suite); 253 + } 254 + 255 + static void kunit_catch_run_case(void *data) 256 + { 257 + struct kunit_try_catch_context *ctx = data; 258 + struct kunit *test = ctx->test; 259 + struct kunit_suite *suite = ctx->suite; 260 + int try_exit_code = kunit_try_catch_get_result(&test->try_catch); 261 + 262 + if (try_exit_code) { 263 + kunit_set_failure(test); 264 + /* 265 + * Test case could not finish, we have no idea what state it is 266 + * in, so don't do clean up. 267 + */ 268 + if (try_exit_code == -ETIMEDOUT) { 269 + kunit_err(test, "test case timed out\n"); 270 + /* 271 + * Unknown internal error occurred preventing test case from 272 + * running, so there is nothing to clean up. 273 + */ 274 + } else { 275 + kunit_err(test, "internal error occurred preventing test case from running: %d\n", 276 + try_exit_code); 277 + } 278 + return; 279 + } 280 + 281 + /* 282 + * Test case was run, but aborted. It is the test case's business as to 283 + * whether it failed or not, we just need to clean up. 284 + */ 285 + kunit_run_case_cleanup(test, suite); 286 + } 287 + 288 + /* 289 + * Performs all logic to run a test case. It also catches most errors that 290 + * occur in a test case and reports them as failures. 291 + */ 292 + static void kunit_run_case_catch_errors(struct kunit_suite *suite, 293 + struct kunit_case *test_case) 294 + { 295 + struct kunit_try_catch_context context; 296 + struct kunit_try_catch *try_catch; 297 + struct kunit test; 298 + 299 + kunit_init_test(&test, test_case->name); 300 + try_catch = &test.try_catch; 301 + 302 + kunit_try_catch_init(try_catch, 303 + &test, 304 + kunit_try_run_case, 305 + kunit_catch_run_case); 306 + context.test = &test; 307 + context.suite = suite; 308 + context.test_case = test_case; 309 + kunit_try_catch_run(try_catch, &context); 239 310 240 311 test_case->success = test.success; 241 312 } ··· 337 230 kunit_print_subtest_start(suite); 338 231 339 232 for (test_case = suite->test_cases; test_case->run_case; test_case++) { 340 - kunit_run_case(suite, test_case); 233 + kunit_run_case_catch_errors(suite, test_case); 341 234 kunit_print_test_case_ok_not_ok(test_case, test_case_count++); 342 235 } 343 236
+118
lib/kunit/try-catch.c
··· 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 + #include <kunit/test.h> 11 + #include <kunit/try-catch.h> 12 + #include <linux/completion.h> 13 + #include <linux/kernel.h> 14 + #include <linux/kthread.h> 15 + #include <linux/sched/sysctl.h> 16 + 17 + void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch) 18 + { 19 + try_catch->try_result = -EFAULT; 20 + complete_and_exit(try_catch->try_completion, -EFAULT); 21 + } 22 + 23 + static int kunit_generic_run_threadfn_adapter(void *data) 24 + { 25 + struct kunit_try_catch *try_catch = data; 26 + 27 + try_catch->try(try_catch->context); 28 + 29 + complete_and_exit(try_catch->try_completion, 0); 30 + } 31 + 32 + static unsigned long kunit_test_timeout(void) 33 + { 34 + unsigned long timeout_msecs; 35 + 36 + /* 37 + * TODO(brendanhiggins@google.com): We should probably have some type of 38 + * variable timeout here. The only question is what that timeout value 39 + * should be. 40 + * 41 + * The intention has always been, at some point, to be able to label 42 + * tests with some type of size bucket (unit/small, integration/medium, 43 + * large/system/end-to-end, etc), where each size bucket would get a 44 + * default timeout value kind of like what Bazel does: 45 + * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size 46 + * There is still some debate to be had on exactly how we do this. (For 47 + * one, we probably want to have some sort of test runner level 48 + * timeout.) 49 + * 50 + * For more background on this topic, see: 51 + * https://mike-bland.com/2011/11/01/small-medium-large.html 52 + */ 53 + if (sysctl_hung_task_timeout_secs) { 54 + /* 55 + * If sysctl_hung_task is active, just set the timeout to some 56 + * value less than that. 57 + * 58 + * In regards to the above TODO, if we decide on variable 59 + * timeouts, this logic will likely need to change. 60 + */ 61 + timeout_msecs = (sysctl_hung_task_timeout_secs - 1) * 62 + MSEC_PER_SEC; 63 + } else { 64 + timeout_msecs = 300 * MSEC_PER_SEC; /* 5 min */ 65 + } 66 + 67 + return timeout_msecs; 68 + } 69 + 70 + void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) 71 + { 72 + DECLARE_COMPLETION_ONSTACK(try_completion); 73 + struct kunit *test = try_catch->test; 74 + struct task_struct *task_struct; 75 + int exit_code, time_remaining; 76 + 77 + try_catch->context = context; 78 + try_catch->try_completion = &try_completion; 79 + try_catch->try_result = 0; 80 + task_struct = kthread_run(kunit_generic_run_threadfn_adapter, 81 + try_catch, 82 + "kunit_try_catch_thread"); 83 + if (IS_ERR(task_struct)) { 84 + try_catch->catch(try_catch->context); 85 + return; 86 + } 87 + 88 + time_remaining = wait_for_completion_timeout(&try_completion, 89 + kunit_test_timeout()); 90 + if (time_remaining == 0) { 91 + kunit_err(test, "try timed out\n"); 92 + try_catch->try_result = -ETIMEDOUT; 93 + } 94 + 95 + exit_code = try_catch->try_result; 96 + 97 + if (!exit_code) 98 + return; 99 + 100 + if (exit_code == -EFAULT) 101 + try_catch->try_result = 0; 102 + else if (exit_code == -EINTR) 103 + kunit_err(test, "wake_up_process() was never called\n"); 104 + else if (exit_code) 105 + kunit_err(test, "Unknown error: %d\n", exit_code); 106 + 107 + try_catch->catch(try_catch->context); 108 + } 109 + 110 + void kunit_try_catch_init(struct kunit_try_catch *try_catch, 111 + struct kunit *test, 112 + kunit_try_catch_func_t try, 113 + kunit_try_catch_func_t catch) 114 + { 115 + try_catch->test = test; 116 + try_catch->try = try; 117 + try_catch->catch = catch; 118 + }