Debian Codegraph @

tests/bin/test_run

raw
1 #!/usr/bin/env bash
2 #
3 # Copyright IBM Corp. 2017
4 #
5 # Usage: test_run <testname> <cmdline>
6 #
7 # Announce a test case, run it, and record the resulting output in the
8 # test log file. Must be run after testsuite_init.
9 #
10
11 trap 'echo ; exit 1' SIGINT
12
13 [[ -z "$TOPDIR" ]] && TOPDIR=$(readlink -f $(dirname $0)/..) && test -d "$TOPDIR"
14
15 source "$TOPDIR/bin/common"
16 #echo $@
17
18 EXCERPTLEN=10
19 TESTNAME="$1"
20 shift
21 SCRIPT=$1
22 shift
23
24 OPTS=''
25 while [ $# -gt 0 ] ; do
26
27 OPT=$1
28 shift
29 case $OPT in
30
31 --script-args )
32 OPTS="$OPTS $1"
33 shift
34 ;;
35
36 --coverage )
37 COVER_DB=$1
38 export PERL_COVER_ARGS="-MDevel::Cover=-db,$(COVER_DB),-coverage,statement,branch,condition,subroutine,-silent,1"
39 shift
40 OPTS="$OPTS $OPT $COVER_DB --keep-going"
41 ;;
42
43 * )
44 echo "Error: test_run: unexpected option '$OPT'"
45 exit 1
46 ;;
47
48 esac
49 done
50
51 if [[ "$SCRIPT" =~ '.pl' ]] ; then
52 INVOKE_COVER="perl ${PERL_COVER_ARGS}"
53 fi
54
55 TIME=$(type -P time 2>/dev/null)
56 if [ ! -z "$TIME" ] ; then
57 TIME="$TIME -v -o $TIMEFILE"
58 if ! $TIME true 2>/dev/null ; then
59 TIME=""
60 fi
61 fi
62
63 t_announce "$TESTNAME"
64
65 case "$OSTYPE" in
66 linux*)
67 let POS=$(stat -c %s "$LOGFILE")+1
68 ;;
69 *)
70 let POS=$(stat -f %z "$LOGFILE")+1
71 ;;
72 esac
73
74 t_detail "COMMAND" "\"$SCRIPT $OPTS*\"" >>"$LOGFILE"
75 t_detail "OUTPUT" "" >>"$LOGFILE"
76
77 # Run command
78 $TIME bash -c "$INVOKE_COVER $SCRIPT $OPTS" 2>&1 | t_indent >>"$LOGFILE"
79 RC=${PIPESTATUS[0]}
80
81 # Evaluate output of time command
82 ELAPSED=
83 RESIDENT=
84 SIGNAL=
85 if [ ! -z "$TIME" ] ; then
86 while read LINE ; do
87 case "$LINE" in
88 "Command terminated by signal"*) SIGNAL=${LINE##* } ;;
89 "Elapsed"*) ELAPSED=$(elapsed_to_ms ${LINE##* }) ;;
90 "Maximum resident"*) RESIDENT=${LINE##* } ;;
91 "Exit status"*) RC=${LINE##* } ;;
92 esac
93 done < "$TIMEFILE"
94 rm -f "$TIMEFILE"
95 fi
96
97 if [ 0 == $RC ] ; then
98 for str in uninitialized ; do
99 grep $str $LOGFILE
100 if [ 0 == $? ] ; then
101 echo "unexpected '$str' in '$LOGFILE' for $TESTNAME"
102 RC=1
103 fi
104 done
105 fi
106
107 # Save last output line as reason in case of skip result
108 LAST=$(tail -n 1 "$LOGFILE" | sed -e 's/^ //g')
109
110 t_detail "EXITCODE" "$RC" >>"$LOGFILE"
111
112 # Show result
113 if [ $RC -eq 0 -a -z "$SIGNAL" ] ; then
114 RESULT="pass"
115 t_pass "$TESTNAME"
116 else
117 if [ $RC -eq 2 ] ; then
118 RESULT="skip"
119 t_skip "$TESTNAME"
120 else
121 if [ -z "$SIGNAL" ] ; then
122 RESULT="fail"
123 t_fail "$TESTNAME"
124 else
125 RESULT="kill"
126 t_kill "$TESTNAME"
127 fi
128 fi
129 fi
130
131 if [ ! -z "$SIGNAL" ] ; then
132 t_detail "SIGNAL" "$SIGNAL" >>"$LOGFILE"
133 fi
134
135 if [ ! -z "$ELAPSED" ] ; then
136 echo -n " (time $(($ELAPSED/1000)).$(($ELAPSED%1000/100))s, "
137 echo "elapsed $TESTNAME $ELAPSED" >> "$COUNTFILE"
138 fi
139
140 if [ ! -z "$RESIDENT" ] ; then
141 echo -n "mem $(($RESIDENT/1024)).$((($RESIDENT%1024)/100))MB)"
142 echo "resident $TESTNAME $RESIDENT" >> "$COUNTFILE"
143 fi
144
145 echo
146
147 # Show skip reason
148 if [ $RC -eq 2 ] ; then
149 t_detail "REASON" "$LAST" | t_indent
150 t_detail "REASON" "$LAST" >>"$LOGFILE"
151 fi
152
153 # Show log excerpt on failure or if requested
154 if [ $RC -ne 0 -a $RC -ne 2 -o "$V" == "1" ] ; then
155 LEN=$(tail -c "+$POS" "$LOGFILE" | wc -l)
156 if [ "$LEN" -gt "$EXCERPTLEN" -a "$V" != "1" ] ; then
157 echo " Skipping $LEN previous lines (see $LOGFILE)"
158 echo " ..."
159 tail -c "+$POS" "$LOGFILE" | tail -n $EXCERPTLEN | t_indent
160 let LEN=$LEN-$EXCERPTLEN
161 else
162 tail -c "+$POS" "$LOGFILE" | t_indent
163 fi
164 fi
165
166 # Log more details
167 [ ! -z "$ELAPSED" ] && t_detail "TIME" "${ELAPSED}ms" >>"$LOGFILE"
168 [ ! -z "$RESIDENT" ] && t_detail "MEM" "${RESIDENT}kB" >>"$LOGFILE"
169 t_detail "RESULT" "$RESULT" >> "$LOGFILE"
170
171 exit $RC