summaryrefslogtreecommitdiffstats
path: root/test/scripts/test-loop.sh
blob: 17dc7c39cdf145fb239784c32942c3b07d6ee049 (plain)
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
#!/bin/bash

function usage() {
 echo "$0" 1>&2
 echo "" 1>&2
 echo "Usage: $0 [-p <pre-exec-cmd>] [-m <email>] -- <make test options>" 1>&2
 echo "" 1>&2
 echo "Parameters:" 1>&2
 echo "    -p <pre-exec-cmd> - run a command before each test loop (e.g. 'git pull')" 1>&2
 echo "    -m <email>        - if set, email is sent to this address on failure" 1>&2
 echo "" 1>&2
 echo "Example:" 1>&2
 echo "    $0 -m <somebody@cisco.com> -- test-debug TEST=l2bd"
 exit 1;
}

PRE_EXEC_CMD=""
EMAIL=""

while getopts "p:m:h" o; do
	case "${o}" in
	p)
		PRE_EXEC_CMD=${OPTARG}
		;;
	m)
		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])?\$"
		m=${OPTARG}
		if [[ ! $m =~ $regex ]]
		then
			echo "Invalid -m parameter value: \`$m'" >&2
			usage
		fi
		EMAIL="$m"
		;;
	h)
		usage
		;;
	?)
		usage
		;;
esac
	done
shift $((OPTIND-1))

if ! echo $* | grep test >/dev/null
then
        echo "Error: command line doesn't look right - should contain \`test' token..." >&2
	usage
fi

function finish {
	NOW=`date +%s`
	RUNTIME=$((NOW - START))
	AVG=$(echo "scale=2; $RUNTIME/$COUNT" | bc)
	OUT="*********************************************************************"
	OUT="$OUT\n* tail -n 30 $TMP:"
	OUT="$OUT\n*********************************************************************"
	OUT="$OUT\n`tail -n 30 $TMP`"
	OUT="$OUT\n*********************************************************************"
	OUT="$OUT\n* Total runtime: ${RUNTIME}s"
	OUT="$OUT\n* Iterations:    ${COUNT}"
	OUT="$OUT\n* Average time:  ${AVG}s"
	OUT="$OUT\n* Log file:      ${TMP}"
	OUT="$OUT\n*********************************************************************"
	echo -e "$OUT"
	if [[ "$EMAIL" != "" && "$REASON" != "" ]]
	then
		SUBJECT="test loop finished ($REASON)"
		echo -e "$OUT" | mail -s "$SUBJECT" $EMAIL
	fi
}

trap "echo Caught signal, exiting...; REASON=\"received signal\"; finish; exit -1" SIGINT SIGTERM

TMP=`mktemp`
START=`date +%s`
COUNT=0

if ! test -f "$TMP"
then
	echo "Couldn't create temporary file!"
	exit -1
fi

echo "Temporary file is $TMP"
CMD="make $*"
echo "Command line is \`$CMD'"

REASON=""
while true
do
	COUNT=$((COUNT+1))
	BEFORE=`date +%s`
	if [[ "$PRE_EXEC_CMD" != "" ]]
	then
		echo "Executing \`$PRE_EXEC_CMD' before test.."
		if ! ($PRE_EXEC_CMD 2>&1 | tee $TMP)
		then
			echo "\`$PRE_EXEC_CMD' failed!" >&2
			REASON="$PRE_EXEC_CMD failed"
			break
		fi
	fi
	echo -n "Running test iteration #$COUNT..."
	if ! ($CMD >$TMP 2>&1)
	then
		AFTER=`date +%s`
		RUNTIME=$((AFTER-BEFORE))
		echo "FAILED! (after ${RUNTIME}s)"
		REASON="test failed"
		break
	fi
	AFTER=`date +%s`
	RUNTIME=$((AFTER-BEFORE))
	echo "PASSED (after ${RUNTIME}s)"
done

finish
exit 1
> data, n_data_bytes); else s = format (s, "%U", format_hex_bytes, data, n_data_bytes); return s; } /* Enable/on => 1; disable/off => 0. */ uword unformat_vlib_enable_disable (unformat_input_t * input, va_list * args) { int *result = va_arg (*args, int *); int enable; if (unformat (input, "enable") || unformat (input, "on")) enable = 1; else if (unformat (input, "disable") || unformat (input, "off")) enable = 0; else return 0; *result = enable; return 1; } /* rx/tx => VLIB_RX/VLIB_TX. */ uword unformat_vlib_rx_tx (unformat_input_t * input, va_list * args) { int *result = va_arg (*args, int *); if (unformat (input, "rx")) *result = VLIB_RX; else if (unformat (input, "tx")) *result = VLIB_TX; else return 0; return 1; } /* Parse an int either %d or 0x%x. */ uword unformat_vlib_number (unformat_input_t * input, va_list * args) { int *result = va_arg (*args, int *); return (unformat (input, "0x%x", result) || unformat (input, "%d", result)); } /* Parse a-zA-Z0-9_ token and hash to value. */ uword unformat_vlib_number_by_name (unformat_input_t * input, va_list * args) { uword *hash = va_arg (*args, uword *); int *result = va_arg (*args, int *); uword *p; u8 *token; int i; if (!unformat_user (input, unformat_token, "a-zA-Z0-9_", &token)) return 0; /* Null terminate. */ if (vec_len (token) > 0 && token[vec_len (token) - 1] != 0) vec_add1 (token, 0); /* Check for exact match. */ p = hash_get_mem (hash, token); if (p) goto done; /* Convert to upper case & try match. */ for (i = 0; i < vec_len (token); i++) if (token[i] >= 'a' && token[i] <= 'z') token[i] = 'A' + token[i] - 'a'; p = hash_get_mem (hash, token); done: vec_free (token); if (p) *result = p[0]; return p != 0; } /* Parse a filename to dump debug info */ uword unformat_vlib_tmpfile (unformat_input_t * input, va_list * args) { u8 **chroot_filename = va_arg (*args, u8 **); u8 *filename; if (!unformat (input, "%s", &filename)) return 0; /* Brain-police user path input */ if (strstr ((char *) filename, "..") || index ((char *) filename, '/')) { vec_free (filename); return 0; } *chroot_filename = format (0, "/tmp/%s%c", filename, 0); vec_free (filename); return 1; } u8 * format_vlib_thread_name (u8 * s, va_list * args) { u32 thread_index = va_arg (*args, u32); if (thread_index == 0) return format (s, "main"); if (thread_index < vec_len (vlib_worker_threads)) return format (s, "%s", vlib_worker_threads[thread_index].name); return s; } u8 * format_vlib_thread_name_and_index (u8 * s, va_list * args) { u32 thread_index = va_arg (*args, u32); return format (s, "%U (%u)", format_vlib_thread_name, thread_index, thread_index); } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */