Skip to content

Commit 5039a33

Browse files
committed
leds: Provide skeleton KUnit testing for the LEDs framework
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>
1 parent d1d3205 commit 5039a33

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

drivers/leds/.kunitconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_KUNIT=y
2+
CONFIG_NEW_LEDS=y
3+
CONFIG_LEDS_CLASS=y
4+
CONFIG_LEDS_KUNIT_TEST=y

drivers/leds/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ config LEDS_BRIGHTNESS_HW_CHANGED
5555

5656
See Documentation/ABI/testing/sysfs-class-led for details.
5757

58+
config LEDS_KUNIT_TEST
59+
tristate "KUnit tests for LEDs"
60+
depends on KUNIT && LEDS_CLASS
61+
default KUNIT_ALL_TESTS
62+
help
63+
Say Y here to enable KUnit testing for the LEDs framework.
64+
5865
comment "LED drivers"
5966

6067
config LEDS_88PM860X

drivers/leds/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ obj-$(CONFIG_LEDS_CLASS) += led-class.o
66
obj-$(CONFIG_LEDS_CLASS_FLASH) += led-class-flash.o
77
obj-$(CONFIG_LEDS_CLASS_MULTICOLOR) += led-class-multicolor.o
88
obj-$(CONFIG_LEDS_TRIGGERS) += led-triggers.o
9+
obj-$(CONFIG_LEDS_KUNIT_TEST) += led-test.o
910

1011
# LED Platform Drivers (keep this sorted, M-| sort)
1112
obj-$(CONFIG_LEDS_88PM860X) += leds-88pm860x.o

drivers/leds/led-test.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2025 Google LLC
4+
*
5+
* Author: Lee Jones <lee@kernel.org>
6+
*/
7+
8+
#include <kunit/device.h>
9+
#include <kunit/test.h>
10+
#include <linux/device.h>
11+
#include <linux/leds.h>
12+
13+
struct led_test_ddata {
14+
struct led_classdev cdev;
15+
struct device *dev;
16+
};
17+
18+
static void led_test_class_register(struct kunit *test)
19+
{
20+
struct led_test_ddata *ddata = test->priv;
21+
struct led_classdev *cdev = &ddata->cdev;
22+
struct device *dev = ddata->dev;
23+
int ret;
24+
25+
cdev->name = "led-test";
26+
27+
ret = devm_led_classdev_register(dev, cdev);
28+
KUNIT_ASSERT_EQ(test, ret, 0);
29+
if (ret)
30+
return;
31+
}
32+
33+
static struct kunit_case led_test_cases[] = {
34+
KUNIT_CASE(led_test_class_register),
35+
{ }
36+
};
37+
38+
static int led_test_init(struct kunit *test)
39+
{
40+
struct led_test_ddata *ddata;
41+
struct device *dev;
42+
43+
ddata = kunit_kzalloc(test, sizeof(*ddata), GFP_KERNEL);
44+
if (!ddata)
45+
return -ENOMEM;
46+
47+
test->priv = ddata;
48+
49+
dev = kunit_device_register(test, "led_test");
50+
if (IS_ERR(dev))
51+
return PTR_ERR(dev);
52+
53+
ddata->dev = get_device(dev);
54+
55+
return 0;
56+
}
57+
58+
static void led_test_exit(struct kunit *test)
59+
{
60+
struct led_test_ddata *ddata = test->priv;
61+
62+
if (ddata && ddata->dev)
63+
put_device(ddata->dev);
64+
}
65+
66+
static struct kunit_suite led_test_suite = {
67+
.name = "led",
68+
.init = led_test_init,
69+
.exit = led_test_exit,
70+
.test_cases = led_test_cases,
71+
};
72+
kunit_test_suite(led_test_suite);
73+
74+
MODULE_AUTHOR("Lee Jones <lee@kernel.org>");
75+
MODULE_DESCRIPTION("KUnit tests for the LED framework");
76+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)