Re: [PATCH 1/4] kunit: Add APIs for managing devices

From: Amadeusz Sławiński
Date: Tue Dec 05 2023 - 04:02:20 EST


On 12/5/2023 8:31 AM, davidgow@xxxxxxxxxx wrote:
Tests for drivers often require a struct device to pass to other
functions. While it's possible to create these with
root_device_register(), or to use something like a platform device, this
is both a misuse of those APIs, and can be difficult to clean up after,
for example, a failed assertion.

Add some KUnit-specific functions for registering and unregistering a
struct device:
- kunit_device_register()
- kunit_device_register_with_driver()
- kunit_device_unregister()

These helpers allocate a on a 'kunit' bus which will either probe the
driver passed in (kunit_device_register_with_driver), or will create a
stub driver (kunit_device_register) which is cleaned up on test shutdown.

Devices are automatically unregistered on test shutdown, but can be
manually unregistered earlier with kunit_device_unregister() in order
to, for example, test device release code.

Signed-off-by: David Gow <davidgow@xxxxxxxxxx>
---
Documentation/dev-tools/kunit/usage.rst | 49 +++++++++
include/kunit/device.h | 76 ++++++++++++++
lib/kunit/Makefile | 3 +-
lib/kunit/device.c | 176 ++++++++++++++++++++++++++++++++
lib/kunit/kunit-test.c | 68 +++++++++++-
lib/kunit/test.c | 3 +
6 files changed, 373 insertions(+), 2 deletions(-)

diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst
index 9db12e91668e..a222a98edceb 100644
--- a/Documentation/dev-tools/kunit/usage.rst
+++ b/Documentation/dev-tools/kunit/usage.rst
@@ -797,3 +797,52 @@ structures as shown below:
KUnit is not enabled, or if no test is running in the current task, it will do
nothing. This compiles down to either a no-op or a static key check, so will
have a negligible performance impact when no test is running.
+
+Managing Fake Devcices and Drivers
+----------------------------------

Typo: Devices