Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2020, Oracle and/or its affiliates.
0004  *    Author: Alan Maguire <alan.maguire@oracle.com>
0005  */
0006 
0007 #include <linux/debugfs.h>
0008 #include <linux/module.h>
0009 
0010 #include <kunit/test.h>
0011 
0012 #include "string-stream.h"
0013 
0014 #define KUNIT_DEBUGFS_ROOT             "kunit"
0015 #define KUNIT_DEBUGFS_RESULTS          "results"
0016 
0017 /*
0018  * Create a debugfs representation of test suites:
0019  *
0020  * Path                     Semantics
0021  * /sys/kernel/debug/kunit/<testsuite>/results  Show results of last run for
0022  *                      testsuite
0023  *
0024  */
0025 
0026 static struct dentry *debugfs_rootdir;
0027 
0028 void kunit_debugfs_cleanup(void)
0029 {
0030     debugfs_remove_recursive(debugfs_rootdir);
0031 }
0032 
0033 void kunit_debugfs_init(void)
0034 {
0035     if (!debugfs_rootdir)
0036         debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
0037 }
0038 
0039 static void debugfs_print_result(struct seq_file *seq,
0040                  struct kunit_suite *suite,
0041                  struct kunit_case *test_case)
0042 {
0043     if (!test_case || !test_case->log)
0044         return;
0045 
0046     seq_printf(seq, "%s", test_case->log);
0047 }
0048 
0049 /*
0050  * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
0051  */
0052 static int debugfs_print_results(struct seq_file *seq, void *v)
0053 {
0054     struct kunit_suite *suite = (struct kunit_suite *)seq->private;
0055     enum kunit_status success = kunit_suite_has_succeeded(suite);
0056     struct kunit_case *test_case;
0057 
0058     if (!suite || !suite->log)
0059         return 0;
0060 
0061     seq_printf(seq, "%s", suite->log);
0062 
0063     kunit_suite_for_each_test_case(suite, test_case)
0064         debugfs_print_result(seq, suite, test_case);
0065 
0066     seq_printf(seq, "%s %d - %s\n",
0067            kunit_status_to_ok_not_ok(success), 1, suite->name);
0068     return 0;
0069 }
0070 
0071 static int debugfs_release(struct inode *inode, struct file *file)
0072 {
0073     return single_release(inode, file);
0074 }
0075 
0076 static int debugfs_results_open(struct inode *inode, struct file *file)
0077 {
0078     struct kunit_suite *suite;
0079 
0080     suite = (struct kunit_suite *)inode->i_private;
0081 
0082     return single_open(file, debugfs_print_results, suite);
0083 }
0084 
0085 static const struct file_operations debugfs_results_fops = {
0086     .open = debugfs_results_open,
0087     .read = seq_read,
0088     .llseek = seq_lseek,
0089     .release = debugfs_release,
0090 };
0091 
0092 void kunit_debugfs_create_suite(struct kunit_suite *suite)
0093 {
0094     struct kunit_case *test_case;
0095 
0096     /* Allocate logs before creating debugfs representation. */
0097     suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
0098     kunit_suite_for_each_test_case(suite, test_case)
0099         test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
0100 
0101     suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
0102 
0103     debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
0104                 suite->debugfs,
0105                 suite, &debugfs_results_fops);
0106 }
0107 
0108 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
0109 {
0110     struct kunit_case *test_case;
0111 
0112     debugfs_remove_recursive(suite->debugfs);
0113     kfree(suite->log);
0114     kunit_suite_for_each_test_case(suite, test_case)
0115         kfree(test_case->log);
0116 }