Klement Sekera | 799e26d | 2017-03-13 06:39:08 +0000 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | function 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 | |
| 17 | PRE_EXEC_CMD="" |
| 18 | EMAIL="" |
| 19 | |
| 20 | while 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 | ;; |
| 41 | esac |
| 42 | done |
| 43 | shift $((OPTIND-1)) |
| 44 | |
| 45 | if ! echo $* | grep test >/dev/null |
| 46 | then |
| 47 | echo "Error: command line doesn't look right - should contain \`test' token..." >&2 |
| 48 | usage |
| 49 | fi |
| 50 | |
| 51 | function 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 | |
| 73 | trap "echo Caught signal, exiting...; REASON=\"received signal\"; finish; exit -1" SIGINT SIGTERM |
| 74 | |
| 75 | TMP=`mktemp` |
| 76 | START=`date +%s` |
| 77 | COUNT=0 |
| 78 | |
| 79 | if ! test -f "$TMP" |
| 80 | then |
| 81 | echo "Couldn't create temporary file!" |
| 82 | exit -1 |
| 83 | fi |
| 84 | |
| 85 | echo "Temporary file is $TMP" |
| 86 | CMD="make $*" |
| 87 | echo "Command line is \`$CMD'" |
| 88 | |
| 89 | REASON="" |
| 90 | while true |
| 91 | do |
| 92 | COUNT=$((COUNT+1)) |
| 93 | BEFORE=`date +%s` |
| 94 | if [[ "$PRE_EXEC_CMD" != "" ]] |
| 95 | then |
| 96 | echo "Executing \`$PRE_EXEC_CMD' before test.." |
| 97 | if ! ($PRE_EXEC_CMD 2>$TMP 1>$TMP) |
| 98 | 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..." |
| 105 | if ! ($CMD 2>$TMP 1>$TMP) |
| 106 | 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)" |
| 116 | done |
| 117 | |
| 118 | finish |
| 119 | exit 1 |