xref: /openbmc/linux/lib/kunit/debugfs.c (revision 5ad1ab30)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020, Oracle and/or its affiliates.
4  *    Author: Alan Maguire <alan.maguire@oracle.com>
5  */
6 
7 #include <linux/debugfs.h>
8 #include <linux/module.h>
9 
10 #include <kunit/test.h>
11 
12 #include "string-stream.h"
13 #include "debugfs.h"
14 
15 #define KUNIT_DEBUGFS_ROOT             "kunit"
16 #define KUNIT_DEBUGFS_RESULTS          "results"
17 
18 /*
19  * Create a debugfs representation of test suites:
20  *
21  * Path						Semantics
22  * /sys/kernel/debug/kunit/<testsuite>/results	Show results of last run for
23  *						testsuite
24  *
25  */
26 
27 static struct dentry *debugfs_rootdir;
28 
29 void kunit_debugfs_cleanup(void)
30 {
31 	debugfs_remove_recursive(debugfs_rootdir);
32 }
33 
34 void kunit_debugfs_init(void)
35 {
36 	if (!debugfs_rootdir)
37 		debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
38 }
39 
40 static void debugfs_print_result(struct seq_file *seq,
41 				 struct kunit_suite *suite,
42 				 struct kunit_case *test_case)
43 {
44 	if (!test_case || !test_case->log)
45 		return;
46 
47 	seq_printf(seq, "%s", test_case->log);
48 }
49 
50 /*
51  * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
52  */
53 static int debugfs_print_results(struct seq_file *seq, void *v)
54 {
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;
58 
59 	if (!suite)
60 		return 0;
61 
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");
65 
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));
70 
71 	kunit_suite_for_each_test_case(suite, test_case)
72 		debugfs_print_result(seq, suite, test_case);
73 
74 	if (suite->log)
75 		seq_printf(seq, "%s", suite->log);
76 
77 	seq_printf(seq, "%s %d %s\n",
78 		   kunit_status_to_ok_not_ok(success), 1, suite->name);
79 	return 0;
80 }
81 
82 static int debugfs_release(struct inode *inode, struct file *file)
83 {
84 	return single_release(inode, file);
85 }
86 
87 static int debugfs_results_open(struct inode *inode, struct file *file)
88 {
89 	struct kunit_suite *suite;
90 
91 	suite = (struct kunit_suite *)inode->i_private;
92 
93 	return single_open(file, debugfs_print_results, suite);
94 }
95 
96 static const struct file_operations debugfs_results_fops = {
97 	.open = debugfs_results_open,
98 	.read = seq_read,
99 	.llseek = seq_lseek,
100 	.release = debugfs_release,
101 };
102 
103 void kunit_debugfs_create_suite(struct kunit_suite *suite)
104 {
105 	struct kunit_case *test_case;
106 
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);
111 
112 	suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
113 
114 	debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
115 			    suite->debugfs,
116 			    suite, &debugfs_results_fops);
117 }
118 
119 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
120 {
121 	struct kunit_case *test_case;
122 
123 	debugfs_remove_recursive(suite->debugfs);
124 	kfree(suite->log);
125 	kunit_suite_for_each_test_case(suite, test_case)
126 		kfree(test_case->log);
127 }
128