Skip to content

Commit 851da97

Browse files
committed
of/unittest: Remove test devices after adding them
The of_platform_populate() test cases don't remove the test devices after they are added. Fix this by adding tests for of_platform_depopulate(). At the same time rework the selftest() macro to return the test result value. This makes it easy to use the macro inside an if() condition. Signed-off-by: Grant Likely <[email protected]>
1 parent 5063e25 commit 851da97

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

drivers/of/selftest.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ static struct device_node *nodes[NO_OF_NODES];
3030
static int last_node_index;
3131
static bool selftest_live_tree;
3232

33-
#define selftest(result, fmt, ...) { \
34-
if (!(result)) { \
33+
#define selftest(result, fmt, ...) ({ \
34+
bool failed = !(result); \
35+
if (failed) { \
3536
selftest_results.failed++; \
3637
pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
3738
} else { \
3839
selftest_results.passed++; \
3940
pr_debug("pass %s():%i\n", __func__, __LINE__); \
4041
} \
41-
}
42+
failed; \
43+
})
4244

4345
static void __init of_selftest_find_node_by_name(void)
4446
{
@@ -694,10 +696,13 @@ static void __init of_selftest_match_node(void)
694696
}
695697
}
696698

699+
struct device test_bus = {
700+
.init_name = "unittest-bus",
701+
};
697702
static void __init of_selftest_platform_populate(void)
698703
{
699-
int irq;
700-
struct device_node *np, *child;
704+
int irq, rc;
705+
struct device_node *np, *child, *grandchild;
701706
struct platform_device *pdev;
702707
struct of_device_id match[] = {
703708
{ .compatible = "test-device", },
@@ -722,20 +727,32 @@ static void __init of_selftest_platform_populate(void)
722727
irq = platform_get_irq(pdev, 0);
723728
selftest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq);
724729

725-
np = of_find_node_by_path("/testcase-data/platform-tests");
726-
if (!np) {
727-
pr_err("No testcase data in device tree\n");
730+
if (selftest(np = of_find_node_by_path("/testcase-data/platform-tests"),
731+
"No testcase data in device tree\n"));
732+
return;
733+
734+
if (selftest(!(rc = device_register(&test_bus)),
735+
"testbus registration failed; rc=%i\n", rc));
728736
return;
729-
}
730737

731738
for_each_child_of_node(np, child) {
732-
struct device_node *grandchild;
733-
of_platform_populate(child, match, NULL, NULL);
739+
of_platform_populate(child, match, NULL, &test_bus);
734740
for_each_child_of_node(child, grandchild)
735741
selftest(of_find_device_by_node(grandchild),
736742
"Could not create device for node '%s'\n",
737743
grandchild->name);
738744
}
745+
746+
of_platform_depopulate(&test_bus);
747+
for_each_child_of_node(np, child) {
748+
for_each_child_of_node(child, grandchild)
749+
selftest(!of_find_device_by_node(grandchild),
750+
"device didn't get destroyed '%s'\n",
751+
grandchild->name);
752+
}
753+
754+
device_unregister(&test_bus);
755+
of_node_put(np);
739756
}
740757

741758
/**

0 commit comments

Comments
 (0)