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