Tom Jones | c3b416c | 2024-01-18 14:51:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Klement Sekera | 799e26d | 2017-03-13 06:39:08 +0000 | [diff] [blame] | 2 | |
| 3 | function usage() { |
| 4 | echo "$0" 1>&2 |
| 5 | echo "" 1>&2 |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 6 | echo "Usage: $0 [-p <pre-exec-cmd>] [-m <email>] -- <make test options|verify>" 1>&2 |
Klement Sekera | 799e26d | 2017-03-13 06:39:08 +0000 | [diff] [blame] | 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 |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 12 | 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 Sekera | 799e26d | 2017-03-13 06:39:08 +0000 | [diff] [blame] | 15 | exit 1; |
| 16 | } |
| 17 | |
| 18 | PRE_EXEC_CMD="" |
| 19 | EMAIL="" |
| 20 | |
| 21 | while 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 | ;; |
| 42 | esac |
| 43 | done |
| 44 | shift $((OPTIND-1)) |
| 45 | |
| 46 | if ! echo $* | grep test >/dev/null |
| 47 | then |
Klement Sekera | 8f2a4ea | 2017-05-04 06:15:18 +0200 | [diff] [blame] | 48 | 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 Sekera | 799e26d | 2017-03-13 06:39:08 +0000 | [diff] [blame] | 53 | fi |
| 54 | |
| 55 | function 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 | |
| 77 | trap "echo Caught signal, exiting...; REASON=\"received signal\"; finish; exit -1" SIGINT SIGTERM |
| 78 | |
| 79 | TMP=`mktemp` |
| 80 | START=`date +%s` |
| 81 | COUNT=0 |
| 82 | |
| 83 | if ! test -f "$TMP" |
| 84 | then |
| 85 | echo "Couldn't create temporary file!" |
| 86 | exit -1 |
| 87 | fi |
| 88 | |
| 89 | echo "Temporary file is $TMP" |
| 90 | CMD="make $*" |
| 91 | echo "Command line is \`$CMD'" |
| 92 | |
| 93 | REASON="" |
| 94 | while true |
| 95 | do |
| 96 | COUNT=$((COUNT+1)) |
| 97 | BEFORE=`date +%s` |
| 98 | if [[ "$PRE_EXEC_CMD" != "" ]] |
| 99 | then |
| 100 | echo "Executing \`$PRE_EXEC_CMD' before test.." |
Klement Sekera | a657e4e | 2017-04-03 04:21:46 +0000 | [diff] [blame] | 101 | if ! ($PRE_EXEC_CMD 2>&1 | tee $TMP) |
Klement Sekera | 799e26d | 2017-03-13 06:39:08 +0000 | [diff] [blame] | 102 | 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 Sekera | a657e4e | 2017-04-03 04:21:46 +0000 | [diff] [blame] | 109 | if ! ($CMD >$TMP 2>&1) |
Klement Sekera | 799e26d | 2017-03-13 06:39:08 +0000 | [diff] [blame] | 110 | 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)" |
| 120 | done |
| 121 | |
| 122 | finish |
| 123 | exit 1 |