Skip to content

Commit 5c9dd72

Browse files
committed
of: Add a KUnit test for overlays and test managed APIs
Test the KUnit test managed overlay APIs. Confirm that platform devices are created and destroyed properly. This provides us confidence that the test managed APIs work correctly and can be relied upon to provide tests with fake platform devices and device nodes via overlays compiled into the kernel image. Cc: Rob Herring <[email protected]> Cc: Saravana Kannan <[email protected]> Cc: Daniel Latypov <[email protected]> Cc: Brendan Higgins <[email protected]> Reviewed-by: David Gow <[email protected]> Cc: Rae Moar <[email protected]> Reviewed-by: Rob Herring (Arm) <[email protected]> Signed-off-by: Stephen Boyd <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 7fc616c commit 5c9dd72

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

drivers/of/.kunitconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
CONFIG_KUNIT=y
22
CONFIG_OF=y
33
CONFIG_OF_KUNIT_TEST=y
4+
CONFIG_OF_OVERLAY_KUNIT_TEST=y

drivers/of/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ config OF_OVERLAY
107107
While this option is selected automatically when needed, you can
108108
enable it manually to improve device tree unit test coverage.
109109

110+
config OF_OVERLAY_KUNIT_TEST
111+
tristate "Device Tree overlay KUnit tests" if !KUNIT_ALL_TESTS
112+
depends on KUNIT
113+
default KUNIT_ALL_TESTS
114+
select OF_OVERLAY
115+
help
116+
This option builds KUnit unit tests for the device tree overlay code.
117+
118+
If unsure, say N here, but this option is safe to enable.
119+
110120
config OF_NUMA
111121
bool
112122

drivers/of/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ endif
2121

2222
obj-$(CONFIG_KUNIT) += of_kunit_helpers.o
2323
obj-$(CONFIG_OF_KUNIT_TEST) += of_test.o
24+
obj-$(CONFIG_OF_OVERLAY_KUNIT_TEST) += overlay-test.o
25+
overlay-test-y := overlay_test.o kunit_overlay_test.dtbo.o
2426

2527
obj-$(CONFIG_OF_UNITTEST) += unittest-data/

drivers/of/kunit_overlay_test.dtso

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/dts-v1/;
3+
/plugin/;
4+
5+
&{/} {
6+
kunit-test {
7+
compatible = "test,empty";
8+
};
9+
};

drivers/of/overlay_test.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* KUnit tests for device tree overlays
4+
*/
5+
#include <linux/device/bus.h>
6+
#include <linux/kconfig.h>
7+
#include <linux/of.h>
8+
#include <linux/of_platform.h>
9+
#include <linux/platform_device.h>
10+
11+
#include <kunit/of.h>
12+
#include <kunit/test.h>
13+
14+
static const char * const kunit_node_name = "kunit-test";
15+
static const char * const kunit_compatible = "test,empty";
16+
17+
/* Test that of_overlay_apply_kunit() adds a node to the live tree */
18+
static void of_overlay_apply_kunit_apply(struct kunit *test)
19+
{
20+
struct device_node *np;
21+
22+
KUNIT_ASSERT_EQ(test, 0,
23+
of_overlay_apply_kunit(test, kunit_overlay_test));
24+
25+
np = of_find_node_by_name(NULL, kunit_node_name);
26+
KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);
27+
of_node_put(np);
28+
}
29+
30+
/*
31+
* Test that of_overlay_apply_kunit() creates platform devices with the
32+
* expected device_node
33+
*/
34+
static void of_overlay_apply_kunit_platform_device(struct kunit *test)
35+
{
36+
struct platform_device *pdev;
37+
struct device_node *np;
38+
39+
KUNIT_ASSERT_EQ(test, 0,
40+
of_overlay_apply_kunit(test, kunit_overlay_test));
41+
42+
np = of_find_node_by_name(NULL, kunit_node_name);
43+
of_node_put_kunit(test, np);
44+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
45+
46+
pdev = of_find_device_by_node(np);
47+
KUNIT_EXPECT_NOT_ERR_OR_NULL(test, pdev);
48+
if (pdev)
49+
put_device(&pdev->dev);
50+
}
51+
52+
static int of_overlay_bus_match_compatible(struct device *dev, const void *data)
53+
{
54+
return of_device_is_compatible(dev->of_node, data);
55+
}
56+
57+
/* Test that of_overlay_apply_kunit() cleans up after the test is finished */
58+
static void of_overlay_apply_kunit_cleanup(struct kunit *test)
59+
{
60+
struct kunit fake;
61+
struct platform_device *pdev;
62+
struct device *dev;
63+
struct device_node *np;
64+
65+
if (!IS_ENABLED(CONFIG_OF_EARLY_FLATTREE))
66+
kunit_skip(test, "requires CONFIG_OF_EARLY_FLATTREE for root node");
67+
68+
kunit_init_test(&fake, "fake test", NULL);
69+
KUNIT_ASSERT_EQ(test, fake.status, KUNIT_SUCCESS);
70+
71+
KUNIT_ASSERT_EQ(test, 0,
72+
of_overlay_apply_kunit(&fake, kunit_overlay_test));
73+
74+
np = of_find_node_by_name(NULL, kunit_node_name);
75+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
76+
of_node_put_kunit(test, np);
77+
78+
pdev = of_find_device_by_node(np);
79+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
80+
put_device(&pdev->dev); /* Not derefing 'pdev' after this */
81+
82+
/* Remove overlay */
83+
kunit_cleanup(&fake);
84+
85+
/* The node and device should be removed */
86+
np = of_find_node_by_name(NULL, kunit_node_name);
87+
KUNIT_EXPECT_PTR_EQ(test, NULL, np);
88+
of_node_put(np);
89+
90+
dev = bus_find_device(&platform_bus_type, NULL, kunit_compatible,
91+
of_overlay_bus_match_compatible);
92+
KUNIT_EXPECT_PTR_EQ(test, NULL, dev);
93+
put_device(dev);
94+
}
95+
96+
static struct kunit_case of_overlay_apply_kunit_test_cases[] = {
97+
KUNIT_CASE(of_overlay_apply_kunit_apply),
98+
KUNIT_CASE(of_overlay_apply_kunit_platform_device),
99+
KUNIT_CASE(of_overlay_apply_kunit_cleanup),
100+
{}
101+
};
102+
103+
/*
104+
* Test suite for test managed device tree overlays.
105+
*/
106+
static struct kunit_suite of_overlay_apply_kunit_suite = {
107+
.name = "of_overlay_apply_kunit",
108+
.test_cases = of_overlay_apply_kunit_test_cases,
109+
};
110+
111+
kunit_test_suites(
112+
&of_overlay_apply_kunit_suite,
113+
);
114+
MODULE_LICENSE("GPL");
115+
MODULE_DESCRIPTION("KUnit tests for device tree overlays");

0 commit comments

Comments
 (0)