• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
kunit_debugfs_cleanup(void)28 void kunit_debugfs_cleanup(void)
29 {
30 	debugfs_remove_recursive(debugfs_rootdir);
31 }
32 
kunit_debugfs_init(void)33 void kunit_debugfs_init(void)
34 {
35 	if (!debugfs_rootdir)
36 		debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
37 }
38 
debugfs_print_result(struct seq_file * seq,struct kunit_suite * suite,struct kunit_case * test_case)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  */
debugfs_print_results(struct seq_file * seq,void * v)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;
56 	struct kunit_case *test_case;
57 
58 	if (!suite || !suite->log)
59 		return 0;
60 
61 	success = kunit_suite_has_succeeded(suite);
62 
63 	seq_printf(seq, "%s", suite->log);
64 
65 	kunit_suite_for_each_test_case(suite, test_case)
66 		debugfs_print_result(seq, suite, test_case);
67 
68 	seq_printf(seq, "%s %d - %s\n",
69 		   kunit_status_to_ok_not_ok(success), 1, suite->name);
70 	return 0;
71 }
72 
debugfs_release(struct inode * inode,struct file * file)73 static int debugfs_release(struct inode *inode, struct file *file)
74 {
75 	return single_release(inode, file);
76 }
77 
debugfs_results_open(struct inode * inode,struct file * file)78 static int debugfs_results_open(struct inode *inode, struct file *file)
79 {
80 	struct kunit_suite *suite;
81 
82 	suite = (struct kunit_suite *)inode->i_private;
83 
84 	return single_open(file, debugfs_print_results, suite);
85 }
86 
87 static const struct file_operations debugfs_results_fops = {
88 	.open = debugfs_results_open,
89 	.read = seq_read,
90 	.llseek = seq_lseek,
91 	.release = debugfs_release,
92 };
93 
kunit_debugfs_create_suite(struct kunit_suite * suite)94 void kunit_debugfs_create_suite(struct kunit_suite *suite)
95 {
96 	struct kunit_case *test_case;
97 
98 	/* Allocate logs before creating debugfs representation. */
99 	suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
100 	kunit_suite_for_each_test_case(suite, test_case)
101 		test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
102 
103 	suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
104 
105 	debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
106 			    suite->debugfs,
107 			    suite, &debugfs_results_fops);
108 }
109 
kunit_debugfs_destroy_suite(struct kunit_suite * suite)110 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
111 {
112 	struct kunit_case *test_case;
113 
114 	debugfs_remove_recursive(suite->debugfs);
115 	kfree(suite->log);
116 	kunit_suite_for_each_test_case(suite, test_case)
117 		kfree(test_case->log);
118 }
119