1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020, Oracle and/or its affiliates.
4 * Author: Alan Maguire <alan.maguire@oracle.com>
7 #include <linux/debugfs.h>
8 #include <linux/module.h>
10 #include <kunit/test.h>
12 #include "string-stream.h"
14 #define KUNIT_DEBUGFS_ROOT "kunit"
15 #define KUNIT_DEBUGFS_RESULTS "results"
18 * Create a debugfs representation of test suites:
21 * /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
26 static struct dentry *debugfs_rootdir;
28 void kunit_debugfs_cleanup(void)
30 debugfs_remove_recursive(debugfs_rootdir);
33 void kunit_debugfs_init(void)
36 debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
39 static void debugfs_print_result(struct seq_file *seq,
40 struct kunit_suite *suite,
41 struct kunit_case *test_case)
43 if (!test_case || !test_case->log)
46 seq_printf(seq, "%s", test_case->log);
50 * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
52 static int debugfs_print_results(struct seq_file *seq, void *v)
54 struct kunit_suite *suite = (struct kunit_suite *)seq->private;
55 enum kunit_status success = kunit_suite_has_succeeded(suite);
56 struct kunit_case *test_case;
61 /* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
62 seq_puts(seq, "KTAP version 1\n");
63 seq_puts(seq, "1..1\n");
65 /* Print suite header because it is not stored in the test logs. */
66 seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
67 seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
68 seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
70 kunit_suite_for_each_test_case(suite, test_case)
71 debugfs_print_result(seq, suite, test_case);
74 seq_printf(seq, "%s", suite->log);
76 seq_printf(seq, "%s %d %s\n",
77 kunit_status_to_ok_not_ok(success), 1, suite->name);
81 static int debugfs_release(struct inode *inode, struct file *file)
83 return single_release(inode, file);
86 static int debugfs_results_open(struct inode *inode, struct file *file)
88 struct kunit_suite *suite;
90 suite = (struct kunit_suite *)inode->i_private;
92 return single_open(file, debugfs_print_results, suite);
95 static const struct file_operations debugfs_results_fops = {
96 .open = debugfs_results_open,
99 .release = debugfs_release,
102 void kunit_debugfs_create_suite(struct kunit_suite *suite)
104 struct kunit_case *test_case;
106 /* Allocate logs before creating debugfs representation. */
107 suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
108 kunit_suite_for_each_test_case(suite, test_case)
109 test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
111 suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
113 debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
115 suite, &debugfs_results_fops);
118 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
120 struct kunit_case *test_case;
122 debugfs_remove_recursive(suite->debugfs);
124 kunit_suite_for_each_test_case(suite, test_case)
125 kfree(test_case->log);