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, struct string_stream *log) 41 { 42 struct string_stream_fragment *frag_container; 43 44 if (!log) 45 return; 46 47 /* 48 * Walk the fragments so we don't need to allocate a temporary 49 * buffer to hold the entire string. 50 */ 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); 55 } 56 57 /* 58 * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite. 59 */ 60 static int debugfs_print_results(struct seq_file *seq, void *v) 61 { 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; 65 66 if (!suite) 67 return 0; 68 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"); 72 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)); 77 78 kunit_suite_for_each_test_case(suite, test_case) 79 debugfs_print_result(seq, test_case->log); 80 81 debugfs_print_result(seq, suite->log); 82 83 seq_printf(seq, "%s %d %s\n", 84 kunit_status_to_ok_not_ok(success), 1, suite->name); 85 return 0; 86 } 87 88 static int debugfs_release(struct inode *inode, struct file *file) 89 { 90 return single_release(inode, file); 91 } 92 93 static int debugfs_results_open(struct inode *inode, struct file *file) 94 { 95 struct kunit_suite *suite; 96 97 suite = (struct kunit_suite *)inode->i_private; 98 99 return single_open(file, debugfs_print_results, suite); 100 } 101 102 static const struct file_operations debugfs_results_fops = { 103 .open = debugfs_results_open, 104 .read = seq_read, 105 .llseek = seq_lseek, 106 .release = debugfs_release, 107 }; 108 109 void kunit_debugfs_create_suite(struct kunit_suite *suite) 110 { 111 struct kunit_case *test_case; 112 113 /* Allocate logs before creating debugfs representation. */ 114 suite->log = alloc_string_stream(GFP_KERNEL); 115 string_stream_set_append_newlines(suite->log, true); 116 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); 120 } 121 122 suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir); 123 124 debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444, 125 suite->debugfs, 126 suite, &debugfs_results_fops); 127 } 128 129 void kunit_debugfs_destroy_suite(struct kunit_suite *suite) 130 { 131 struct kunit_case *test_case; 132 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); 137 } 138