blob: 51f5d5cef58e05c276dc6bb67c7d802374d426e4 [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
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02006 echo "Usage: $0 [-p <pre-exec-cmd>] [-m <email>] -- <make test options|verify>" 1>&2
Klement Sekera799e26d2017-03-13 06:39:08 +00007 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
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020012 echo "Examples:" 1>&2
13 echo " $0 -m <somebody@cisco.com> -- test-debug TEST=l2bd" 1>&2
14 echo " $0 -m <somebody@cisco.com> -- verify" 1>&2
Klement Sekera799e26d2017-03-13 06:39:08 +000015 exit 1;
16}
17
18PRE_EXEC_CMD=""
19EMAIL=""
20
21while getopts "p:m:h" o; do
22 case "${o}" in
23 p)
24 PRE_EXEC_CMD=${OPTARG}
25 ;;
26 m)
27 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])?\$"
28 m=${OPTARG}
29 if [[ ! $m =~ $regex ]]
30 then
31 echo "Invalid -m parameter value: \`$m'" >&2
32 usage
33 fi
34 EMAIL="$m"
35 ;;
36 h)
37 usage
38 ;;
39 ?)
40 usage
41 ;;
42esac
43 done
44shift $((OPTIND-1))
45
46if ! echo $* | grep test >/dev/null
47then
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020048 if ! echo $* | grep verify >/dev/null
49 then
50 echo "Error: command line doesn't look right - should contain \`test' or \`verify' token..." >&2
51 usage
52 fi
Klement Sekera799e26d2017-03-13 06:39:08 +000053fi
54
55function finish {
56 NOW=`date +%s`
57 RUNTIME=$((NOW - START))
58 AVG=$(echo "scale=2; $RUNTIME/$COUNT" | bc)
59 OUT="*********************************************************************"
60 OUT="$OUT\n* tail -n 30 $TMP:"
61 OUT="$OUT\n*********************************************************************"
62 OUT="$OUT\n`tail -n 30 $TMP`"
63 OUT="$OUT\n*********************************************************************"
64 OUT="$OUT\n* Total runtime: ${RUNTIME}s"
65 OUT="$OUT\n* Iterations: ${COUNT}"
66 OUT="$OUT\n* Average time: ${AVG}s"
67 OUT="$OUT\n* Log file: ${TMP}"
68 OUT="$OUT\n*********************************************************************"
69 echo -e "$OUT"
70 if [[ "$EMAIL" != "" && "$REASON" != "" ]]
71 then
72 SUBJECT="test loop finished ($REASON)"
73 echo -e "$OUT" | mail -s "$SUBJECT" $EMAIL
74 fi
75}
76
77trap "echo Caught signal, exiting...; REASON=\"received signal\"; finish; exit -1" SIGINT SIGTERM
78
79TMP=`mktemp`
80START=`date +%s`
81COUNT=0
82
83if ! test -f "$TMP"
84then
85 echo "Couldn't create temporary file!"
86 exit -1
87fi
88
89echo "Temporary file is $TMP"
90CMD="make $*"
91echo "Command line is \`$CMD'"
92
93REASON=""
94while true
95do
96 COUNT=$((COUNT+1))
97 BEFORE=`date +%s`
98 if [[ "$PRE_EXEC_CMD" != "" ]]
99 then
100 echo "Executing \`$PRE_EXEC_CMD' before test.."
Klement Sekeraa657e4e2017-04-03 04:21:46 +0000101 if ! ($PRE_EXEC_CMD 2>&1 | tee $TMP)
Klement Sekera799e26d2017-03-13 06:39:08 +0000102 then
103 echo "\`$PRE_EXEC_CMD' failed!" >&2
104 REASON="$PRE_EXEC_CMD failed"
105 break
106 fi
107 fi
108 echo -n "Running test iteration #$COUNT..."
Klement Sekeraa657e4e2017-04-03 04:21:46 +0000109 if ! ($CMD >$TMP 2>&1)
Klement Sekera799e26d2017-03-13 06:39:08 +0000110 then
111 AFTER=`date +%s`
112 RUNTIME=$((AFTER-BEFORE))
113 echo "FAILED! (after ${RUNTIME}s)"
114 REASON="test failed"
115 break
116 fi
117 AFTER=`date +%s`
118 RUNTIME=$((AFTER-BEFORE))
119 echo "PASSED (after ${RUNTIME}s)"
120done
121
122finish
123exit 1