xref: /openbmc/linux/lib/kunit/debugfs.c (revision 5e990887)
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 
kunit_debugfs_cleanup(void)29 void kunit_debugfs_cleanup(void)
30 {
31 	debugfs_remove_recursive(debugfs_rootdir);
32 }
33 
kunit_debugfs_init(void)34 void kunit_debugfs_init(void)
35 {
36 	if (!debugfs_rootdir)
37 		debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
38 }
39 
debugfs_print_result(struct seq_file * seq,struct kunit_suite * suite,struct kunit_case * test_case)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  */
debugfs_print_results(struct seq_file * seq,void * v)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;
57 	struct kunit_case *test_case;
58 
59 	if (!suite)
60 		return 0;
61 
62 	success = kunit_suite_has_succeeded(suite);
63 
64 	/* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
65 	seq_puts(seq, "KTAP version 1\n");
66 	seq_puts(seq, "1..1\n");
67 
68 	/* Print suite header because it is not stored in the test logs. */
69 	seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
70 	seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
71 	seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
72 
73 	kunit_suite_for_each_test_case(suite, test_case)
74 		debugfs_print_result(seq, suite, test_case);
75 
76 	if (suite->log)
77 		seq_printf(seq, "%s", suite->log);
78 
79 	seq_printf(seq, "%s %d %s\n",
80 		   kunit_status_to_ok_not_ok(success), 1, suite->name);
81 	return 0;
82 }
83 
debugfs_release(struct inode * inode,struct file * file)84 static int debugfs_release(struct inode *inode, struct file *file)
85 {
86 	return single_release(inode, file);
87 }
88 
debugfs_results_open(struct inode * inode,struct file * file)89 static int debugfs_results_open(struct inode *inode, struct file *file)
90 {
91 	struct kunit_suite *suite;
92 
93 	suite = (struct kunit_suite *)inode->i_private;
94 
95 	return single_open(file, debugfs_print_results, suite);
96 }
97 
98 static const struct file_operations debugfs_results_fops = {
99 	.open = debugfs_results_open,
100 	.read = seq_read,
101 	.llseek = seq_lseek,
102 	.release = debugfs_release,
103 };
104 
kunit_debugfs_create_suite(struct kunit_suite * suite)105 void kunit_debugfs_create_suite(struct kunit_suite *suite)
106 {
107 	struct kunit_case *test_case;
108 
109 	/* Allocate logs before creating debugfs representation. */
110 	suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
111 	kunit_suite_for_each_test_case(suite, test_case)
112 		test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
113 
114 	suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
115 
116 	debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
117 			    suite->debugfs,
118 			    suite, &debugfs_results_fops);
119 }
120 
kunit_debugfs_destroy_suite(struct kunit_suite * suite)121 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
122 {
123 	struct kunit_case *test_case;
124 
125 	debugfs_remove_recursive(suite->debugfs);
126 	kfree(suite->log);
127 	kunit_suite_for_each_test_case(suite, test_case)
128 		kfree(test_case->log);
129 }
130