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