diff options
Diffstat (limited to 'tools/testing/selftests/ftrace/ftracetest')
-rwxr-xr-x | tools/testing/selftests/ftrace/ftracetest | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/tools/testing/selftests/ftrace/ftracetest b/tools/testing/selftests/ftrace/ftracetest new file mode 100755 index 000000000000..a8f81c782856 --- /dev/null +++ b/tools/testing/selftests/ftrace/ftracetest | |||
@@ -0,0 +1,253 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | # ftracetest - Ftrace test shell scripts | ||
4 | # | ||
5 | # Copyright (C) Hitachi Ltd., 2014 | ||
6 | # Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> | ||
7 | # | ||
8 | # Released under the terms of the GPL v2. | ||
9 | |||
10 | usage() { # errno [message] | ||
11 | [ "$2" ] && echo $2 | ||
12 | echo "Usage: ftracetest [options] [testcase(s)] [testcase-directory(s)]" | ||
13 | echo " Options:" | ||
14 | echo " -h|--help Show help message" | ||
15 | echo " -k|--keep Keep passed test logs" | ||
16 | echo " -d|--debug Debug mode (trace all shell commands)" | ||
17 | exit $1 | ||
18 | } | ||
19 | |||
20 | errexit() { # message | ||
21 | echo "Error: $1" 1>&2 | ||
22 | exit 1 | ||
23 | } | ||
24 | |||
25 | # Ensuring user privilege | ||
26 | if [ `id -u` -ne 0 ]; then | ||
27 | errexit "this must be run by root user" | ||
28 | fi | ||
29 | |||
30 | # Utilities | ||
31 | absdir() { # file_path | ||
32 | (cd `dirname $1`; pwd) | ||
33 | } | ||
34 | |||
35 | abspath() { | ||
36 | echo `absdir $1`/`basename $1` | ||
37 | } | ||
38 | |||
39 | find_testcases() { #directory | ||
40 | echo `find $1 -name \*.tc` | ||
41 | } | ||
42 | |||
43 | parse_opts() { # opts | ||
44 | local OPT_TEST_CASES= | ||
45 | local OPT_TEST_DIR= | ||
46 | |||
47 | while [ "$1" ]; do | ||
48 | case "$1" in | ||
49 | --help|-h) | ||
50 | usage 0 | ||
51 | ;; | ||
52 | --keep|-k) | ||
53 | KEEP_LOG=1 | ||
54 | shift 1 | ||
55 | ;; | ||
56 | --debug|-d) | ||
57 | DEBUG=1 | ||
58 | shift 1 | ||
59 | ;; | ||
60 | *.tc) | ||
61 | if [ -f "$1" ]; then | ||
62 | OPT_TEST_CASES="$OPT_TEST_CASES `abspath $1`" | ||
63 | shift 1 | ||
64 | else | ||
65 | usage 1 "$1 is not a testcase" | ||
66 | fi | ||
67 | ;; | ||
68 | *) | ||
69 | if [ -d "$1" ]; then | ||
70 | OPT_TEST_DIR=`abspath $1` | ||
71 | OPT_TEST_CASES="$OPT_TEST_CASES `find_testcases $OPT_TEST_DIR`" | ||
72 | shift 1 | ||
73 | else | ||
74 | usage 1 "Invalid option ($1)" | ||
75 | fi | ||
76 | ;; | ||
77 | esac | ||
78 | done | ||
79 | if [ "$OPT_TEST_CASES" ]; then | ||
80 | TEST_CASES=$OPT_TEST_CASES | ||
81 | fi | ||
82 | } | ||
83 | |||
84 | # Parameters | ||
85 | DEBUGFS_DIR=`grep debugfs /proc/mounts | cut -f2 -d' '` | ||
86 | TRACING_DIR=$DEBUGFS_DIR/tracing | ||
87 | TOP_DIR=`absdir $0` | ||
88 | TEST_DIR=$TOP_DIR/test.d | ||
89 | TEST_CASES=`find_testcases $TEST_DIR` | ||
90 | LOG_DIR=$TOP_DIR/logs/`date +%Y%m%d-%H%M%S`/ | ||
91 | KEEP_LOG=0 | ||
92 | DEBUG=0 | ||
93 | # Parse command-line options | ||
94 | parse_opts $* | ||
95 | |||
96 | [ $DEBUG -ne 0 ] && set -x | ||
97 | |||
98 | # Verify parameters | ||
99 | if [ -z "$DEBUGFS_DIR" -o ! -d "$TRACING_DIR" ]; then | ||
100 | errexit "No ftrace directory found" | ||
101 | fi | ||
102 | |||
103 | # Preparing logs | ||
104 | LOG_FILE=$LOG_DIR/ftracetest.log | ||
105 | mkdir -p $LOG_DIR || errexit "Failed to make a log directory: $LOG_DIR" | ||
106 | date > $LOG_FILE | ||
107 | prlog() { # messages | ||
108 | echo "$@" | tee -a $LOG_FILE | ||
109 | } | ||
110 | catlog() { #file | ||
111 | cat $1 | tee -a $LOG_FILE | ||
112 | } | ||
113 | prlog "=== Ftrace unit tests ===" | ||
114 | |||
115 | |||
116 | # Testcase management | ||
117 | # Test result codes - Dejagnu extended code | ||
118 | PASS=0 # The test succeeded. | ||
119 | FAIL=1 # The test failed, but was expected to succeed. | ||
120 | UNRESOLVED=2 # The test produced indeterminate results. (e.g. interrupted) | ||
121 | UNTESTED=3 # The test was not run, currently just a placeholder. | ||
122 | UNSUPPORTED=4 # The test failed because of lack of feature. | ||
123 | XFAIL=5 # The test failed, and was expected to fail. | ||
124 | |||
125 | # Accumulations | ||
126 | PASSED_CASES= | ||
127 | FAILED_CASES= | ||
128 | UNRESOLVED_CASES= | ||
129 | UNTESTED_CASES= | ||
130 | UNSUPPORTED_CASES= | ||
131 | XFAILED_CASES= | ||
132 | UNDEFINED_CASES= | ||
133 | TOTAL_RESULT=0 | ||
134 | |||
135 | CASENO=0 | ||
136 | testcase() { # testfile | ||
137 | CASENO=$((CASENO+1)) | ||
138 | prlog -n "[$CASENO]"`grep "^#[ \t]*description:" $1 | cut -f2 -d:` | ||
139 | } | ||
140 | |||
141 | eval_result() { # retval sigval | ||
142 | local retval=$2 | ||
143 | if [ $2 -eq 0 ]; then | ||
144 | test $1 -ne 0 && retval=$FAIL | ||
145 | fi | ||
146 | case $retval in | ||
147 | $PASS) | ||
148 | prlog " [PASS]" | ||
149 | PASSED_CASES="$PASSED_CASES $CASENO" | ||
150 | return 0 | ||
151 | ;; | ||
152 | $FAIL) | ||
153 | prlog " [FAIL]" | ||
154 | FAILED_CASES="$FAILED_CASES $CASENO" | ||
155 | return 1 # this is a bug. | ||
156 | ;; | ||
157 | $UNRESOLVED) | ||
158 | prlog " [UNRESOLVED]" | ||
159 | UNRESOLVED_CASES="$UNRESOLVED_CASES $CASENO" | ||
160 | return 1 # this is a kind of bug.. something happened. | ||
161 | ;; | ||
162 | $UNTESTED) | ||
163 | prlog " [UNTESTED]" | ||
164 | UNTESTED_CASES="$UNTESTED_CASES $CASENO" | ||
165 | return 0 | ||
166 | ;; | ||
167 | $UNSUPPORTED) | ||
168 | prlog " [UNSUPPORTED]" | ||
169 | UNSUPPORTED_CASES="$UNSUPPORTED_CASES $CASENO" | ||
170 | return 1 # this is not a bug, but the result should be reported. | ||
171 | ;; | ||
172 | $XFAIL) | ||
173 | prlog " [XFAIL]" | ||
174 | XFAILED_CASES="$XFAILED_CASES $CASENO" | ||
175 | return 0 | ||
176 | ;; | ||
177 | *) | ||
178 | prlog " [UNDEFINED]" | ||
179 | UNDEFINED_CASES="$UNDEFINED_CASES $CASENO" | ||
180 | return 1 # this must be a test bug | ||
181 | ;; | ||
182 | esac | ||
183 | } | ||
184 | |||
185 | # Signal handling for result codes | ||
186 | SIG_RESULT= | ||
187 | SIG_BASE=36 # Use realtime signals | ||
188 | SIG_PID=$$ | ||
189 | |||
190 | SIG_UNRESOLVED=$((SIG_BASE + UNRESOLVED)) | ||
191 | exit_unresolved () { | ||
192 | kill -s $SIG_UNRESOLVED $SIG_PID | ||
193 | exit 0 | ||
194 | } | ||
195 | trap 'SIG_RESULT=$UNRESOLVED' $SIG_UNRESOLVED | ||
196 | |||
197 | SIG_UNTESTED=$((SIG_BASE + UNTESTED)) | ||
198 | exit_untested () { | ||
199 | kill -s $SIG_UNTESTED $SIG_PID | ||
200 | exit 0 | ||
201 | } | ||
202 | trap 'SIG_RESULT=$UNTESTED' $SIG_UNTESTED | ||
203 | |||
204 | SIG_UNSUPPORTED=$((SIG_BASE + UNSUPPORTED)) | ||
205 | exit_unsupported () { | ||
206 | kill -s $SIG_UNSUPPORTED $SIG_PID | ||
207 | exit 0 | ||
208 | } | ||
209 | trap 'SIG_RESULT=$UNSUPPORTED' $SIG_UNSUPPORTED | ||
210 | |||
211 | SIG_XFAIL=$((SIG_BASE + XFAIL)) | ||
212 | exit_xfail () { | ||
213 | kill -s $SIG_XFAIL $SIG_PID | ||
214 | exit 0 | ||
215 | } | ||
216 | trap 'SIG_RESULT=$XFAIL' $SIG_XFAIL | ||
217 | |||
218 | # Run one test case | ||
219 | run_test() { # testfile | ||
220 | local testname=`basename $1` | ||
221 | local testlog=`mktemp --tmpdir=$LOG_DIR ${testname}-XXXXXX.log` | ||
222 | testcase $1 | ||
223 | echo "execute: "$1 > $testlog | ||
224 | SIG_RESULT=0 | ||
225 | # setup PID and PPID, $$ is not updated. | ||
226 | (cd $TRACING_DIR; read PID _ < /proc/self/stat ; | ||
227 | set -e; set -x; . $1) >> $testlog 2>&1 | ||
228 | eval_result $? $SIG_RESULT | ||
229 | if [ $? -eq 0 ]; then | ||
230 | # Remove test log if the test was done as it was expected. | ||
231 | [ $KEEP_LOG -eq 0 ] && rm $testlog | ||
232 | else | ||
233 | catlog $testlog | ||
234 | TOTAL_RESULT=1 | ||
235 | fi | ||
236 | } | ||
237 | |||
238 | # Main loop | ||
239 | for t in $TEST_CASES; do | ||
240 | run_test $t | ||
241 | done | ||
242 | |||
243 | prlog "" | ||
244 | prlog "# of passed: " `echo $PASSED_CASES | wc -w` | ||
245 | prlog "# of failed: " `echo $FAILED_CASES | wc -w` | ||
246 | prlog "# of unresolved: " `echo $UNRESOLVED_CASES | wc -w` | ||
247 | prlog "# of untested: " `echo $UNTESTED_CASES | wc -w` | ||
248 | prlog "# of unsupported: " `echo $UNSUPPORTED_CASES | wc -w` | ||
249 | prlog "# of xfailed: " `echo $XFAILED_CASES | wc -w` | ||
250 | prlog "# of undefined(test bug): " `echo $UNDEFINED_CASES | wc -w` | ||
251 | |||
252 | # if no error, return 0 | ||
253 | exit $TOTAL_RESULT | ||