^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2020, Oracle and/or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Alan Maguire <alan.maguire@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <kunit/test.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "string-stream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define KUNIT_DEBUGFS_ROOT "kunit"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define KUNIT_DEBUGFS_RESULTS "results"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Create a debugfs representation of test suites:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Path Semantics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * testsuite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static struct dentry *debugfs_rootdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void kunit_debugfs_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) debugfs_remove_recursive(debugfs_rootdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) void kunit_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!debugfs_rootdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void debugfs_print_result(struct seq_file *seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct kunit_suite *suite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct kunit_case *test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (!test_case || !test_case->log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) seq_printf(seq, "%s", test_case->log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int debugfs_print_results(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct kunit_suite *suite = (struct kunit_suite *)seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) bool success = kunit_suite_has_succeeded(suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct kunit_case *test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!suite || !suite->log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) seq_printf(seq, "%s", suite->log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) kunit_suite_for_each_test_case(suite, test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) debugfs_print_result(seq, suite, test_case);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) seq_printf(seq, "%s %d - %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) kunit_status_to_string(success), 1, suite->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int debugfs_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return single_release(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int debugfs_results_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct kunit_suite *suite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) suite = (struct kunit_suite *)inode->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return single_open(file, debugfs_print_results, suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static const struct file_operations debugfs_results_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .open = debugfs_results_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .llseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .release = debugfs_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void kunit_debugfs_create_suite(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct kunit_case *test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Allocate logs before creating debugfs representation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) kunit_suite_for_each_test_case(suite, test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) suite->debugfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) suite, &debugfs_results_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct kunit_case *test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) debugfs_remove_recursive(suite->debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) kfree(suite->log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) kunit_suite_for_each_test_case(suite, test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kfree(test_case->log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }