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