blob: 17dc7c39cdf145fb239784c32942c3b07d6ee049 [file] [log] [blame]
Klement Sekera799e26d2017-03-13 06:39:08 +00001#!/bin/bash
2
3function usage() {
4 echo "$0" 1>&2
5 echo "" 1>&2
6 echo "Usage: $0 [-p <pre-exec-cmd>] [-m <email>] -- <make test options>" 1>&2
7 echo "" 1>&2
8 echo "Parameters:" 1>&2
9 echo " -p <pre-exec-cmd> - run a command before each test loop (e.g. 'git pull')" 1>&2
10 echo " -m <email> - if set, email is sent to this address on failure" 1>&2
11 echo "" 1>&2
12 echo "Example:" 1>&2
13 echo " $0 -m <somebody@cisco.com> -- test-debug TEST=l2bd"
14 exit 1;
15}
16
17PRE_EXEC_CMD=""
18EMAIL=""
19
20while getopts "p:m:h" o; do
21 case "${o}" in
22 p)
23 PRE_EXEC_CMD=${OPTARG}
24 ;;
25 m)
26 regex="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
27 m=${OPTARG}
28 if [[ ! $m =~ $regex ]]
29 then
30 echo "Invalid -m parameter value: \`$m'" >&2
31 usage
32 fi
33 EMAIL="$m"
34 ;;
35 h)
36 usage
37 ;;
38 ?)
39 usage
40 ;;
41esac
42 done
43shift $((OPTIND-1))
44
45if ! echo $* | grep test >/dev/null
46then
47 echo "Error: command line doesn't look right - should contain \`test' token..." >&2
48 usage
49fi
50
51function finish {
52 NOW=`date +%s`
53 RUNTIME=$((NOW - START))
54 AVG=$(echo "scale=2; $RUNTIME/$COUNT" | bc)
55 OUT="*********************************************************************"
56 OUT="$OUT\n* tail -n 30 $TMP:"
57 OUT="$OUT\n*********************************************************************"
58 OUT="$OUT\n`tail -n 30 $TMP`"
59 OUT="$OUT\n*********************************************************************"
60 OUT="$OUT\n* Total runtime: ${RUNTIME}s"
61 OUT="$OUT\n* Iterations: ${COUNT}"
62 OUT="$OUT\n* Average time: ${AVG}s"
63 OUT="$OUT\n* Log file: ${TMP}"
64 OUT="$OUT\n*********************************************************************"
65 echo -e "$OUT"
66 if [[ "$EMAIL" != "" && "$REASON" != "" ]]
67 then
68 SUBJECT="test loop finished ($REASON)"
69 echo -e "$OUT" | mail -s "$SUBJECT" $EMAIL
70 fi
71}
72
73trap "echo Caught signal, exiting...; REASON=\"received signal\"; finish; exit -1" SIGINT SIGTERM
74
75TMP=`mktemp`
76START=`date +%s`
77COUNT=0
78
79if ! test -f "$TMP"
80then
81 echo "Couldn't create temporary file!"
82 exit -1
83fi
84
85echo "Temporary file is $TMP"
86CMD="make $*"
87echo "Command line is \`$CMD'"
88
89REASON=""
90while true
91do
92 COUNT=$((COUNT+1))
93 BEFORE=`date +%s`
94 if [[ "$PRE_EXEC_CMD" != "" ]]
95 then
96 echo "Executing \`$PRE_EXEC_CMD' before test.."
Klement Sekeraa657e4e2017-04-03 04:21:46 +000097 if ! ($PRE_EXEC_CMD 2>&1 | tee $TMP)
Klement Sekera799e26d2017-03-13 06:39:08 +000098 then
99 echo "\`$PRE_EXEC_CMD' failed!" >&2
100 REASON="$PRE_EXEC_CMD failed"
101 break
102 fi
103 fi
104 echo -n "Running test iteration #$COUNT..."
Klement Sekeraa657e4e2017-04-03 04:21:46 +0000105 if ! ($CMD >$TMP 2>&1)
Klement Sekera799e26d2017-03-13 06:39:08 +0000106 then
107 AFTER=`date +%s`
108 RUNTIME=$((AFTER-BEFORE))
109 echo "FAILED! (after ${RUNTIME}s)"
110 REASON="test failed"
111 break
112 fi
113 AFTER=`date +%s`
114 RUNTIME=$((AFTER-BEFORE))
115 echo "PASSED (after ${RUNTIME}s)"
116done
117
118finish
119exit 1