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