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>
This commit is contained in:
Lee Jones 2025-04-24 15:45:38 +01:00
parent d1d3205730
commit 5039a33fed
4 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,4 @@
CONFIG_KUNIT=y
CONFIG_NEW_LEDS=y
CONFIG_LEDS_CLASS=y
CONFIG_LEDS_KUNIT_TEST=y

View 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

View 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

76
drivers/leds/led-test.c Normal file
View File

@ -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");