]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
leds: Provide skeleton KUnit testing for the LEDs framework
authorLee Jones <lee@kernel.org>
Thu, 24 Apr 2025 14:45:38 +0000 (15:45 +0100)
committerLee Jones <lee@kernel.org>
Wed, 14 May 2025 08:25:02 +0000 (09:25 +0100)
Apply a very basic implementation of KUnit LED testing.

More tests / use-cases will be added steadily over time.

CMD:
  tools/testing/kunit/kunit.py run --kunitconfig drivers/leds

OUTPUT:
  [15:34:19] Configuring KUnit Kernel ...
  [15:34:19] Building KUnit Kernel ...
  Populating config with:
  $ make ARCH=um O=.kunit olddefconfig
  Building with:
  $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=20
  [15:34:22] Starting KUnit Kernel (1/1)...
  [15:34:22] ============================================================
  Running tests with:
  $ .kunit/linux kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
  [15:34:23] ===================== led (1 subtest) ======================
  [15:34:23] [PASSED] led_test_class_register
  [15:34:23] ======================= [PASSED] led =======================
  [15:34:23] ============================================================
  [15:34:23] Testing complete. Ran 1 tests: passed: 1
  [15:34:23] Elapsed time: 4.268s total, 0.001s configuring, 3.048s building, 1.214s running

Link: https://lore.kernel.org/r/20250424144544.1438584-1-lee@kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
drivers/leds/.kunitconfig [new file with mode: 0644]
drivers/leds/Kconfig
drivers/leds/Makefile
drivers/leds/led-test.c [new file with mode: 0644]

diff --git a/drivers/leds/.kunitconfig b/drivers/leds/.kunitconfig
new file mode 100644 (file)
index 0000000..5180f77
--- /dev/null
@@ -0,0 +1,4 @@
+CONFIG_KUNIT=y
+CONFIG_NEW_LEDS=y
+CONFIG_LEDS_CLASS=y
+CONFIG_LEDS_KUNIT_TEST=y
index b107dbe1fa90476f8fadee9c5ea14b8a4db81859..6e3dce7e35a490df050cb4cd8e98611028c8dce1 100644 (file)
@@ -55,6 +55,13 @@ config LEDS_BRIGHTNESS_HW_CHANGED
 
          See Documentation/ABI/testing/sysfs-class-led for details.
 
+config LEDS_KUNIT_TEST
+       tristate "KUnit tests for LEDs"
+       depends on KUNIT && LEDS_CLASS
+       default KUNIT_ALL_TESTS
+       help
+         Say Y here to enable KUnit testing for the LEDs framework.
+
 comment "LED drivers"
 
 config LEDS_88PM860X
index 2f170d69dcbf517cf7df98f25c49e3b445da3470..9a0333ec1a861e4f543e7525a4a6d519b2e2a1e8 100644 (file)
@@ -6,6 +6,7 @@ obj-$(CONFIG_LEDS_CLASS)                += led-class.o
 obj-$(CONFIG_LEDS_CLASS_FLASH)         += led-class-flash.o
 obj-$(CONFIG_LEDS_CLASS_MULTICOLOR)    += led-class-multicolor.o
 obj-$(CONFIG_LEDS_TRIGGERS)            += led-triggers.o
+obj-$(CONFIG_LEDS_KUNIT_TEST)          += led-test.o
 
 # LED Platform Drivers (keep this sorted, M-| sort)
 obj-$(CONFIG_LEDS_88PM860X)            += leds-88pm860x.o
diff --git a/drivers/leds/led-test.c b/drivers/leds/led-test.c
new file mode 100644 (file)
index 0000000..068c9d0
--- /dev/null
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Google LLC
+ *
+ * Author: Lee Jones <lee@kernel.org>
+ */
+
+#include <kunit/device.h>
+#include <kunit/test.h>
+#include <linux/device.h>
+#include <linux/leds.h>
+
+struct led_test_ddata {
+       struct led_classdev cdev;
+       struct device *dev;
+};
+
+static void led_test_class_register(struct kunit *test)
+{
+       struct led_test_ddata *ddata = test->priv;
+       struct led_classdev *cdev = &ddata->cdev;
+       struct device *dev = ddata->dev;
+       int ret;
+
+       cdev->name = "led-test";
+
+       ret = devm_led_classdev_register(dev, cdev);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+       if (ret)
+               return;
+}
+
+static struct kunit_case led_test_cases[] = {
+       KUNIT_CASE(led_test_class_register),
+       { }
+};
+
+static int led_test_init(struct kunit *test)
+{
+       struct led_test_ddata *ddata;
+       struct device *dev;
+
+       ddata = kunit_kzalloc(test, sizeof(*ddata), GFP_KERNEL);
+       if (!ddata)
+               return -ENOMEM;
+
+       test->priv = ddata;
+
+       dev = kunit_device_register(test, "led_test");
+       if (IS_ERR(dev))
+               return PTR_ERR(dev);
+
+       ddata->dev = get_device(dev);
+
+       return 0;
+}
+
+static void led_test_exit(struct kunit *test)
+{
+       struct led_test_ddata *ddata = test->priv;
+
+       if (ddata && ddata->dev)
+               put_device(ddata->dev);
+}
+
+static struct kunit_suite led_test_suite = {
+       .name = "led",
+       .init = led_test_init,
+       .exit = led_test_exit,
+       .test_cases = led_test_cases,
+};
+kunit_test_suite(led_test_suite);
+
+MODULE_AUTHOR("Lee Jones <lee@kernel.org>");
+MODULE_DESCRIPTION("KUnit tests for the LED framework");
+MODULE_LICENSE("GPL");