diff mbox series

[v3,05/11] of: Add a KUnit test for overlays and test managed APIs

Message ID 20230327222159.3509818-6-sboyd@kernel.org (mailing list archive)
State Superseded, archived
Headers show
Series clk: Add kunit tests for fixed rate and parent data | expand

Commit Message

Stephen Boyd March 27, 2023, 10:21 p.m. UTC
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.

Cc: Rob Herring <robh+dt@kernel.org>
Cc: Frank Rowand <frowand.list@gmail.com>
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
---
 drivers/of/.kunitconfig            |   2 +
 drivers/of/Kconfig                 |  10 +++
 drivers/of/Makefile                |   1 +
 drivers/of/kunit_overlay_test.dtso |   9 +++
 drivers/of/overlay_test.c          | 110 +++++++++++++++++++++++++++++
 5 files changed, 132 insertions(+)
 create mode 100644 drivers/of/kunit_overlay_test.dtso
 create mode 100644 drivers/of/overlay_test.c

Comments

Rob Herring April 6, 2023, 2:59 p.m. UTC | #1
On Mon, Mar 27, 2023 at 03:21:53PM -0700, Stephen Boyd 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.
> 
> Cc: Rob Herring <robh+dt@kernel.org>
> Cc: Frank Rowand <frowand.list@gmail.com>
> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
> ---
>  drivers/of/.kunitconfig            |   2 +
>  drivers/of/Kconfig                 |  10 +++
>  drivers/of/Makefile                |   1 +
>  drivers/of/kunit_overlay_test.dtso |   9 +++
>  drivers/of/overlay_test.c          | 110 +++++++++++++++++++++++++++++
>  5 files changed, 132 insertions(+)
>  create mode 100644 drivers/of/kunit_overlay_test.dtso
>  create mode 100644 drivers/of/overlay_test.c
> 
> diff --git a/drivers/of/.kunitconfig b/drivers/of/.kunitconfig
> index 5a8fee11978c..7d570cb922a1 100644
> --- a/drivers/of/.kunitconfig
> +++ b/drivers/of/.kunitconfig
> @@ -1,3 +1,5 @@
>  CONFIG_KUNIT=y
>  CONFIG_OF=y
>  CONFIG_OF_KUNIT_TEST=y
> +CONFIG_OF_OVERLAY=y
> +CONFIG_OF_OVERLAY_KUNIT_TEST=y
> diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
> index 1b995cecf5be..5bdeba11268d 100644
> --- a/drivers/of/Kconfig
> +++ b/drivers/of/Kconfig
> @@ -113,6 +113,16 @@ config OF_OVERLAY
>  	  While this option is selected automatically when needed, you can
>  	  enable it manually to improve device tree unit test coverage.
>  
> +config OF_OVERLAY_KUNIT_TEST
> +	tristate "Device Tree overlay KUnit tests" if !KUNIT_ALL_TESTS
> +	depends on OF_OVERLAY
> +	depends on KUNIT
> +	default KUNIT_ALL_TESTS
> +	help
> +	  This option builds KUnit unit tests for the device tree overlay code.
> +
> +	  If unsure, say N here, but this option is safe to enable.
> +
>  config OF_NUMA
>  	bool
>  
> diff --git a/drivers/of/Makefile b/drivers/of/Makefile
> index c694f998b9f5..2ad60d5b87ac 100644
> --- a/drivers/of/Makefile
> +++ b/drivers/of/Makefile
> @@ -21,5 +21,6 @@ endif
>  
>  obj-$(CONFIG_KUNIT) += of_kunit.o
>  obj-$(CONFIG_OF_KUNIT_TEST) += of_test.o
> +obj-$(CONFIG_OF_OVERLAY_KUNIT_TEST) += overlay_test.o kunit_overlay_test.dtbo.o
>  
>  obj-$(CONFIG_OF_UNITTEST) += unittest-data/
> diff --git a/drivers/of/kunit_overlay_test.dtso b/drivers/of/kunit_overlay_test.dtso
> new file mode 100644
> index 000000000000..85f20b4b4c16
> --- /dev/null
> +++ b/drivers/of/kunit_overlay_test.dtso
> @@ -0,0 +1,9 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/dts-v1/;
> +/plugin/;
> +
> +&{/} {
> +	kunit-test {
> +		compatible = "test,empty";
> +	};
> +};
> diff --git a/drivers/of/overlay_test.c b/drivers/of/overlay_test.c
> new file mode 100644
> index 000000000000..66b1dceea568
> --- /dev/null
> +++ b/drivers/of/overlay_test.c
> @@ -0,0 +1,110 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * KUnit tests for device tree overlays
> + */
> +#include <linux/device/bus.h>
> +#include <linux/kconfig.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +
> +#include <kunit/of.h>
> +#include <kunit/test.h>
> +
> +static const char * const kunit_node_name = "kunit-test";
> +static const char * const kunit_compatible = "test,empty";
> +
> +/* Test that of_overlay_apply_kunit() adds a node to the live tree */
> +static void of_overlay_apply_kunit_apply(struct kunit *test)
> +{
> +	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);
> +	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);
> +	of_node_put(np);
> +}
> +
> +static int bus_match_np(struct device *dev, const void *data)
> +{
> +	const struct device_node *np = data;
> +
> +	return np == dev->of_node;
> +}
> +
> +/*
> + * Test that of_overlay_apply_kunit() creates platform devices with the
> + * expected device_node
> + */
> +static void of_overlay_apply_kunit_platform_device(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_kunit(test, np);
> +	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
> +
> +	dev = bus_find_device(&platform_bus_type, NULL, np, bus_match_np);

of_find_device_by_node()

> +	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
> +	put_device(dev);
> +}
> +
> +static int of_overlay_bus_match_compatible(struct device *dev, const void *data)
> +{
> +	return of_device_is_compatible(dev->of_node, data);
> +}
> +
> +/* 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);

And here.

> +	put_device(dev); /* Not derefing 'device' after this */
> +	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
> +
> +	/* Remove overlay */
> +	kunit_cleanup(test);

I guess this is testing the cleanup. It's not that obvious given the 
overlay is removed by kunit_cleanup(). Not sure if anything can be done 
though...

> +
> +	np = of_find_node_by_name(NULL, kunit_node_name);
> +	KUNIT_EXPECT_PTR_EQ(test, NULL, np);
> +	of_node_put(np);
> +
> +	dev = bus_find_device(&platform_bus_type, NULL, kunit_compatible,
> +			      of_overlay_bus_match_compatible);
> +	KUNIT_EXPECT_PTR_EQ(test, NULL, dev);
> +	put_device(dev);
> +}
> +
> +static struct kunit_case of_overlay_apply_kunit_test_cases[] = {
> +	KUNIT_CASE(of_overlay_apply_kunit_apply),
> +	KUNIT_CASE(of_overlay_apply_kunit_platform_device),
> +	KUNIT_CASE(of_overlay_apply_kunit_cleanup),
> +	{}
> +};
> +
> +/*
> + * Test suite for test managed device tree overlays.
> + */
> +static struct kunit_suite of_overlay_apply_kunit_suite = {
> +	.name = "of_overlay_apply_kunit",
> +	.test_cases = of_overlay_apply_kunit_test_cases,
> +};
> +
> +kunit_test_suites(
> +	&of_overlay_apply_kunit_suite,
> +);
> +MODULE_LICENSE("GPL");
> -- 
> https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git/
> https://git.kernel.org/pub/scm/linux/kernel/git/sboyd/spmi.git
>
Daniel Latypov April 13, 2023, 12:04 a.m. UTC | #2
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@google.com/
[2] https://lore.kernel.org/linux-kselftest/20230403201930.2019419-1-rmoar@google.com/

Daniel
diff mbox series

Patch

diff --git a/drivers/of/.kunitconfig b/drivers/of/.kunitconfig
index 5a8fee11978c..7d570cb922a1 100644
--- a/drivers/of/.kunitconfig
+++ b/drivers/of/.kunitconfig
@@ -1,3 +1,5 @@ 
 CONFIG_KUNIT=y
 CONFIG_OF=y
 CONFIG_OF_KUNIT_TEST=y
+CONFIG_OF_OVERLAY=y
+CONFIG_OF_OVERLAY_KUNIT_TEST=y
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index 1b995cecf5be..5bdeba11268d 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -113,6 +113,16 @@  config OF_OVERLAY
 	  While this option is selected automatically when needed, you can
 	  enable it manually to improve device tree unit test coverage.
 
+config OF_OVERLAY_KUNIT_TEST
+	tristate "Device Tree overlay KUnit tests" if !KUNIT_ALL_TESTS
+	depends on OF_OVERLAY
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This option builds KUnit unit tests for the device tree overlay code.
+
+	  If unsure, say N here, but this option is safe to enable.
+
 config OF_NUMA
 	bool
 
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index c694f998b9f5..2ad60d5b87ac 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -21,5 +21,6 @@  endif
 
 obj-$(CONFIG_KUNIT) += of_kunit.o
 obj-$(CONFIG_OF_KUNIT_TEST) += of_test.o
+obj-$(CONFIG_OF_OVERLAY_KUNIT_TEST) += overlay_test.o kunit_overlay_test.dtbo.o
 
 obj-$(CONFIG_OF_UNITTEST) += unittest-data/
diff --git a/drivers/of/kunit_overlay_test.dtso b/drivers/of/kunit_overlay_test.dtso
new file mode 100644
index 000000000000..85f20b4b4c16
--- /dev/null
+++ b/drivers/of/kunit_overlay_test.dtso
@@ -0,0 +1,9 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/dts-v1/;
+/plugin/;
+
+&{/} {
+	kunit-test {
+		compatible = "test,empty";
+	};
+};
diff --git a/drivers/of/overlay_test.c b/drivers/of/overlay_test.c
new file mode 100644
index 000000000000..66b1dceea568
--- /dev/null
+++ b/drivers/of/overlay_test.c
@@ -0,0 +1,110 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for device tree overlays
+ */
+#include <linux/device/bus.h>
+#include <linux/kconfig.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+
+#include <kunit/of.h>
+#include <kunit/test.h>
+
+static const char * const kunit_node_name = "kunit-test";
+static const char * const kunit_compatible = "test,empty";
+
+/* Test that of_overlay_apply_kunit() adds a node to the live tree */
+static void of_overlay_apply_kunit_apply(struct kunit *test)
+{
+	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);
+	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, np);
+	of_node_put(np);
+}
+
+static int bus_match_np(struct device *dev, const void *data)
+{
+	const struct device_node *np = data;
+
+	return np == dev->of_node;
+}
+
+/*
+ * Test that of_overlay_apply_kunit() creates platform devices with the
+ * expected device_node
+ */
+static void of_overlay_apply_kunit_platform_device(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_kunit(test, np);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, np);
+
+	dev = bus_find_device(&platform_bus_type, NULL, np, bus_match_np);
+	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, dev);
+	put_device(dev);
+}
+
+static int of_overlay_bus_match_compatible(struct device *dev, const void *data)
+{
+	return of_device_is_compatible(dev->of_node, data);
+}
+
+/* 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);
+
+	np = of_find_node_by_name(NULL, kunit_node_name);
+	KUNIT_EXPECT_PTR_EQ(test, NULL, np);
+	of_node_put(np);
+
+	dev = bus_find_device(&platform_bus_type, NULL, kunit_compatible,
+			      of_overlay_bus_match_compatible);
+	KUNIT_EXPECT_PTR_EQ(test, NULL, dev);
+	put_device(dev);
+}
+
+static struct kunit_case of_overlay_apply_kunit_test_cases[] = {
+	KUNIT_CASE(of_overlay_apply_kunit_apply),
+	KUNIT_CASE(of_overlay_apply_kunit_platform_device),
+	KUNIT_CASE(of_overlay_apply_kunit_cleanup),
+	{}
+};
+
+/*
+ * Test suite for test managed device tree overlays.
+ */
+static struct kunit_suite of_overlay_apply_kunit_suite = {
+	.name = "of_overlay_apply_kunit",
+	.test_cases = of_overlay_apply_kunit_test_cases,
+};
+
+kunit_test_suites(
+	&of_overlay_apply_kunit_suite,
+);
+MODULE_LICENSE("GPL");