When writing tests, it'd often be very useful to be able to intercept calls to a function in the code being tested and replace it with a test-specific stub. This has always been an obviously missing piece of KUnit, and the solutions always involve some tradeoffs with cleanliness, performance, or impact on non-test code. See the folowing document for some of the challenges: https://kunit.dev/mocking.html This series introduces a new "static_stub" feature add support for this sort of redirection to KUnit tests. Any function which might want to be intercepted adds a call to a macro which checks if a test has redirected calls to it, and calls the corresponding replacement. Note that at alternate implementation (based on ftrace) was also proposed in an earlier RFC: https://lore.kernel.org/linux-kselftest/20220910212804.670622-3-davidgow@xxxxxxxxxx/ This series only implements "static" stubbing, as it is more compatible across different architectures, and more flexible w/r/t inlined code, but we don't rule out offering the ftrace-based solution as well if the demand is there in the future. This feature was presented at LPC 2022, see: - https://lpc.events/event/16/contributions/1308/ - https://www.youtube.com/watch?v=0Nm06EdXWsE The KUnit 'example' test suite now includes an example of static stubs being used, and the new 'Function Redirection' API documentation provides a step-by-step walkthrough for using the new feature. In addition, an (in-progress) test for the atkbd driver, which provides an example of static stubs being used, can be found here: https://kunit-review.googlesource.com/c/linux/+/5631 Cheers, -- David --- David Gow (1): kunit: Expose 'static stub' API to redirect functions Sadiya Kazi (1): Documentation: Add Function Redirection API docs .../kunit/api/functionredirection.rst | 162 ++++++++++++++++++ Documentation/dev-tools/kunit/api/index.rst | 13 +- include/kunit/static_stub.h | 117 +++++++++++++ lib/kunit/Makefile | 1 + lib/kunit/kunit-example-test.c | 38 ++++ lib/kunit/static_stub.c | 123 +++++++++++++ 6 files changed, 451 insertions(+), 3 deletions(-) create mode 100644 Documentation/dev-tools/kunit/api/functionredirection.rst create mode 100644 include/kunit/static_stub.h create mode 100644 lib/kunit/static_stub.c -- 2.39.0.rc0.267.gcb52ba06e7-goog