1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020, Oracle and/or its affiliates.
7 #include <linux/debugfs.h>
8 #include <linux/module.h>
10 #include <kunit/test.h>
12 #include "string-stream.h"
15 #define KUNIT_DEBUGFS_ROOT "kunit"
16 #define KUNIT_DEBUGFS_RESULTS "results"
19 * Create a debugfs representation of test suites:
22 * /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
27 static struct dentry *debugfs_rootdir;
29 void kunit_debugfs_cleanup(void)
31 debugfs_remove_recursive(debugfs_rootdir);
34 void kunit_debugfs_init(void)
37 debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
40 static void debugfs_print_result(struct seq_file *seq,
41 struct kunit_suite *suite,
42 struct kunit_case *test_case)
44 if (!test_case || !test_case->log)
47 seq_printf(seq, "%s", test_case->log);
51 * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
53 static int debugfs_print_results(struct seq_file *seq, void *v)
55 struct kunit_suite *suite = (struct kunit_suite *)seq->private;
56 enum kunit_status success = kunit_suite_has_succeeded(suite);
57 struct kunit_case *test_case;
62 /* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
63 seq_puts(seq, "KTAP version 1\n");
64 seq_puts(seq, "1..1\n");
66 /* Print suite header because it is not stored in the test logs. */
67 seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
68 seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
69 seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
71 kunit_suite_for_each_test_case(suite, test_case)
72 debugfs_print_result(seq, suite, test_case);
75 seq_printf(seq, "%s", suite->log);
77 seq_printf(seq, "%s %d %s\n",
78 kunit_status_to_ok_not_ok(success), 1, suite->name);
82 static int debugfs_release(struct inode *inode, struct file *file)
84 return single_release(inode, file);
87 static int debugfs_results_open(struct inode *inode, struct file *file)
89 struct kunit_suite *suite;
91 suite = (struct kunit_suite *)inode->i_private;
93 return single_open(file, debugfs_print_results, suite);
96 static const struct file_operations debugfs_results_fops = {
97 .open = debugfs_results_open,
100 .release = debugfs_release,
103 void kunit_debugfs_create_suite(struct kunit_suite *suite)
105 struct kunit_case *test_case;
107 /* Allocate logs before creating debugfs representation. */
108 suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
109 kunit_suite_for_each_test_case(suite, test_case)
110 test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
112 suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
114 debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
116 suite, &debugfs_results_fops);
119 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
121 struct kunit_case *test_case;
123 debugfs_remove_recursive(suite->debugfs);
125 kunit_suite_for_each_test_case(suite, test_case)
126 kfree(test_case->log);