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"
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, struct string_stream *log)
42 struct string_stream_fragment *frag_container;
48 * Walk the fragments so we don't need to allocate a temporary
49 * buffer to hold the entire string.
51 spin_lock(&log->lock);
52 list_for_each_entry(frag_container, &log->fragments, node)
53 seq_printf(seq, "%s", frag_container->fragment);
54 spin_unlock(&log->lock);
58 * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
60 static int debugfs_print_results(struct seq_file *seq, void *v)
62 struct kunit_suite *suite = (struct kunit_suite *)seq->private;
63 enum kunit_status success = kunit_suite_has_succeeded(suite);
64 struct kunit_case *test_case;
69 /* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
70 seq_puts(seq, "KTAP version 1\n");
71 seq_puts(seq, "1..1\n");
73 /* Print suite header because it is not stored in the test logs. */
74 seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
75 seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
76 seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
78 kunit_suite_for_each_test_case(suite, test_case)
79 debugfs_print_result(seq, test_case->log);
81 debugfs_print_result(seq, suite->log);
83 seq_printf(seq, "%s %d %s\n",
84 kunit_status_to_ok_not_ok(success), 1, suite->name);
88 static int debugfs_release(struct inode *inode, struct file *file)
90 return single_release(inode, file);
93 static int debugfs_results_open(struct inode *inode, struct file *file)
95 struct kunit_suite *suite;
97 suite = (struct kunit_suite *)inode->i_private;
99 return single_open(file, debugfs_print_results, suite);
102 static const struct file_operations debugfs_results_fops = {
103 .open = debugfs_results_open,
106 .release = debugfs_release,
109 void kunit_debugfs_create_suite(struct kunit_suite *suite)
111 struct kunit_case *test_case;
113 /* Allocate logs before creating debugfs representation. */
114 suite->log = alloc_string_stream(GFP_KERNEL);
115 string_stream_set_append_newlines(suite->log, true);
117 kunit_suite_for_each_test_case(suite, test_case) {
118 test_case->log = alloc_string_stream(GFP_KERNEL);
119 string_stream_set_append_newlines(test_case->log, true);
122 suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
124 debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
126 suite, &debugfs_results_fops);
129 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
131 struct kunit_case *test_case;
133 debugfs_remove_recursive(suite->debugfs);
134 string_stream_destroy(suite->log);
135 kunit_suite_for_each_test_case(suite, test_case)
136 string_stream_destroy(test_case->log);