On Mon, Mar 27, 2023 at 3:22 PM Stephen Boyd sboyd@kernel.org wrote:
Test the KUnit test managed overlay APIs. Confirm that platform devices are created and destroyed properly. This provides us confidence that the test managed work correctly and can be relied upon to provide tests with fake platform devices and device nodes via overlays compiled into the kernel image.
The discussion around kunit_cleanup() caught my eye below, so one small comment about that.
<snip>
+/* Test that of_overlay_apply_kunit() cleans up after the test is finished */ +static void of_overlay_apply_kunit_cleanup(struct kunit *test) +{
struct device *dev;
struct device_node *np;
KUNIT_ASSERT_EQ(test, 0,
of_overlay_apply_kunit(test, kunit_overlay_test));
np = of_find_node_by_name(NULL, kunit_node_name);
of_node_put(np); /* Not derefing 'np' after this */
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
dev = bus_find_device(&platform_bus_type, NULL, np, bus_match_np);
put_device(dev); /* Not derefing 'device' after this */
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
/* Remove overlay */
kunit_cleanup(test);
Note: this cleans up *all* resources associated with `test`. Right now, it's probably fine, but this probably isn't the safest approach.
Notably, two of the new/upcoming API changes rely on resource, kunit/static_stub.h and kunit_add_action() [1]. Calling kunit_cleanup() undoes all the stubs and immediately triggers all the actions.
Perhaps you can create your own local `struct kunit` like in [2] E.g.
struct kunit subtest; // not sure what to call this...
kunit_init_test(&subtest, "fake test", NULL); /* use subtest */ kunit_cleanup(&subtest);
There's also already-submitted code doing similar things in lib/kunit/kunit-test.c you can look at, but it's in init/exit funcs. See kunit_resource_test_init() and kunit_resource_test_exit().
[1] https://lore.kernel.org/linux-kselftest/20230331080411.981038-2-davidgow@goo... [2] https://lore.kernel.org/linux-kselftest/20230403201930.2019419-1-rmoar@googl...
Daniel