blob: 1671fe64ff3d0312ace809f61df3f2ad556bbfd0 [file] [log] [blame]
Steve McIntyre4c2c6012015-01-26 16:17:38 +00001#
2# test-common
3#
4# Common set of definitions and functions to help with driving VLANd
5# in testing
Steve McIntyre4cbd5222015-07-28 18:38:50 +01006#
7# This specifically depends on *gawk* for the 2d arrays used in
8# check_test_step() at the bottom
Steve McIntyre4c2c6012015-01-26 16:17:38 +00009
Steve McIntyre4c2c6012015-01-26 16:17:38 +000010TOPDIR=$(dirname $0)/..
Steve McIntyrea31981e2015-07-10 16:24:59 +010011
12# Default settings
13if [ "$VERBOSE"x = ""x ] ; then
14 VERBOSE=0
15fi
16if [ "$HOSTS"x = ""x ] ; then
17 HOSTS="panda01 panda02 panda03 arndale01 arndale02 arndale03 arndale04 imx5301 imx5302 imx5303"
18fi
19if [ "$SWITCHES"x = ""x ] ; then
20 SWITCHES="vlandswitch01 vlandswitch02 vlandswitch03 vlandswitch04 vlandswitch05"
21fi
22
Steve McIntyre39db0e12015-07-20 16:22:08 +010023LAST_COMMAND=""
Steve McIntyre4c2c6012015-01-26 16:17:38 +000024
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000025# Topology definitions - how are the test machines connected?
26panda01_SWITCH_PORT="vlandswitch01:Gi1/0/2"
27panda02_SWITCH_PORT="vlandswitch02:fa25"
28panda03_SWITCH_PORT="vlandswitch03:gi25"
29arndale01_SWITCH_PORT="vlandswitch01:Gi1/0/3"
30arndale02_SWITCH_PORT="vlandswitch02:fa2"
31arndale03_SWITCH_PORT="vlandswitch03:gi2"
Steve McIntyrecda3ef92015-07-10 14:43:38 +010032arndale04_SWITCH_PORT="vlandswitch04:Gi1/0/3"
33imx5301_SWITCH_PORT="vlandswitch05:1/0/21"
34imx5302_SWITCH_PORT="vlandswitch05:1/0/22"
35imx5303_SWITCH_PORT="vlandswitch04:Gi1/0/2"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000036
Steve McIntyreedf32d22015-07-17 15:19:03 +010037# Function for output; write text to terminal and to our logfile, with
38# timestamp for sorting.
39log () {
Steve McIntyre716519e2015-07-17 15:21:01 +010040 DATE=$(date -u +%F/%H:%M:%S.%N)
Steve McIntyreedf32d22015-07-17 15:19:03 +010041 if [ "${LOGFILE}"x = ""x ] ; then
42 LOGFILE=$0.log
43 fi
Steve McIntyre20ca8ae2015-07-29 17:14:49 +010044 if [ $VERBOSE -gt 0 ] ; then
45 echo "${DATE}: $@" >&2 # Use stderr so we don't confuse
46 # anybody reading our output
47 fi
Steve McIntyreedf32d22015-07-17 15:19:03 +010048 echo " ${DATE}: $@" >> ${LOGFILE}
49}
50
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000051vecho () {
52 if [ $VERBOSE -gt 0 ] ; then
Steve McIntyreedf32d22015-07-17 15:19:03 +010053 log "$@"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000054 fi
55}
Steve McIntyre4c2c6012015-01-26 16:17:38 +000056
Steve McIntyreedf32d22015-07-17 15:19:03 +010057vvecho () {
58 if [ $VERBOSE -gt 1 ] ; then
59 log "$@"
60 fi
61}
62
63# Run a vland admin command, and optionally log both the full text of
64# the command and its output
65run_admin_command () {
66 ADMIN="python $TOPDIR/admin.py"
67 vvecho "Running \"$ADMIN $@\""
Steve McIntyre39db0e12015-07-20 16:22:08 +010068 LAST_COMMAND="$@"
Steve McIntyreedf32d22015-07-17 15:19:03 +010069 RESULT=$($ADMIN $@)
70 vvecho " Result is \"$RESULT\""
71 echo $RESULT
72}
73
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000074verify_host_is_base () {
75 HOST=$1
76 PORT_ID_VAR=${HOST}_PORT_ID
77 PORT_ID=${!PORT_ID_VAR}
Steve McIntyreedf32d22015-07-17 15:19:03 +010078 CURRENT_VLAN_ID=$(run_admin_command --get_port_current_vlan $PORT_ID)
79 CURRENT_VLAN_TAG=$(run_admin_command --show_vlan_tag $CURRENT_VLAN_ID)
80 BASE_VLAN_ID=$(run_admin_command --get_port_base_vlan $PORT_ID)
81 BASE_VLAN_TAG=$(run_admin_command --show_vlan_tag $BASE_VLAN_ID)
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000082 vecho "$HOST"
83 vecho " is on port ID $PORT_ID"
84 vecho " which is on VLAN ID $CURRENT_VLAN_ID, tag $CURRENT_VLAN_TAG"
85 vecho " and should be on base VLAN ID $BASE_VLAN_ID, tag $BASE_VLAN_TAG"
86 if [ $CURRENT_VLAN_ID == $BASE_VLAN_ID ] ; then
87 return 0
88 else
89 return 1
90 fi
91}
92
93verify_all_hosts_are_base () {
94 # Check that all the machines are correctly on their base VLANs
95 for host in $HOSTS; do
96 verify_host_is_base $host
97 if [ $? -ne 0 ] ; then
98 return 1
99 fi
100 done
101}
102
103all_hosts () {
104 COMMAND="$@"
105 for HOST in ${HOSTS}; do
Steve McIntyreedf32d22015-07-17 15:19:03 +0100106 vvecho "Running on ${HOST}:: ${COMMAND}"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000107 ssh linaro@${HOST} "${COMMAND}"
108 done
109}
110
111start_logging () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100112 vecho "Starting logging on hosts"
Steve McIntyree2848942015-07-10 16:51:27 +0100113 all_hosts "echo HOSTS=\\\"$HOSTS\\\" > test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000114}
115
116stop_logging () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100117 vecho "Stopping logging on hosts"
Steve McIntyrea31981e2015-07-10 16:24:59 +0100118 all_hosts "rm -f test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000119}
120
121grab_logs () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100122 vecho "Grabbing logs"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000123 all_hosts "cat /tmp/ping-log*"
124}
125
126clear_logs () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100127 vecho "Clearing old logs on hosts"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000128 all_hosts "rm -f /tmp/ping-log*"
129}
Steve McIntyre39db0e12015-07-20 16:22:08 +0100130
Steve McIntyred0d693b2015-07-20 16:45:01 +0100131pause () {
Steve McIntyreb066a5a2015-07-28 18:39:09 +0100132 vecho "Pausing $1 seconds for systems to settle and/or log results"
Steve McIntyred0d693b2015-07-20 16:45:01 +0100133 sleep $1
134}
135
Steve McIntyre39db0e12015-07-20 16:22:08 +0100136cleanup () {
137 error=$?
138 if [ $error -ne 0 ] ; then
139 echo "Test script aborted with error $error - check logs for the failure"
Steve McIntyreb066a5a2015-07-28 18:39:09 +0100140 echo " Last VLANd command appears to be \"$LAST_COMMAND\""
Steve McIntyre39db0e12015-07-20 16:22:08 +0100141 fi
142}
143
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100144list_test_steps () {
145 sort -u ${LOGFILE} | awk '
146 /CHECK.*START/ {
147 gsub("^.*CHECK ","")
148 gsub(" START.*$","")
149 start[$0]=1
150 }
151 /CHECK.*END/ {
152 gsub("^.*CHECK ","")
153 gsub(" END.*$","")
154 if(start[$0] == 1) {
155 print $0
156 start[$0] = 0
157 }
158 }'
159}
160
161check_test_steps () {
162 for STEP in $(list_test_steps); do
Steve McIntyre20ca8ae2015-07-29 17:14:49 +0100163 echo " Checking connectivity results for ${STEP}: "
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100164 check_test_step ${STEP}
165 done
166}
167
168check_test_step () {
169 STEP=$1
170 sort -u ${LOGFILE} | awk -v STEP=$1 "
171 /CHECK $STEP START/ { running=1 }
172 /CHECK $STEP END/ { running=0 }
173 /CHECK $STEP CHECK/ {
174 for( i = 4; i <= NF; i++) {
175 num_mach = split(\$i, mach_list, \":\")
176 for (j = 2; j <= num_mach; j++) {
177 test[mach_list[1]][mach_list[j]] = 1
178 }
179 }
180 if (0 == 1) {
181 for (group in test) {
182 printf(\" $STEP test group %s contains: \", group)
183 for (host in test[group]) {
184 printf(\"%s \", host)
185 }
186 printf(\"\n\")
187 }
188 }
189 }
190 /UP/ {
191 if(running) {
192 OK=0
193 for (group in test) {
194 if (test[group][\$2] && test[group][\$4]) {
195 #printf(\"GOOD UP: %s to %s (%s)\n\",\$2,\$4,group)
196 OK=1
197 success++
198 }
199 }
200 if (OK == 0) {
201 printf(\" BAD UP: %s to %s\n\",\$2,\$4)
202 fail++
203 }
204 }
205 }
206 /DOWN/ {
207 if(running) {
208 OK=1
209 for (group in test) {
210 if (test[group][\$2] && test[group][\$4]) {
211 printf(\" BAD DOWN: %s to %s (%s)\n\",\$2,\$4,group)
212 OK=0
213 fail++
214 }
215 }
216 if (OK == 1) {
217 # printf(\"GOOD DOWN: %s to %s\n\",\$2,\$4)
218 success++
219 }
220 }
221 }
Steve McIntyre20ca8ae2015-07-29 17:14:49 +0100222 END { printf(\" success: %d, fail: %d\n\", success, fail) }
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100223"
224}
225
Steve McIntyre39db0e12015-07-20 16:22:08 +0100226trap cleanup 0
Steve McIntyree28b84f2015-07-29 16:58:51 +0100227
228#####################
229#
230# MAIN CODE STARTS HERE
231#
232#####################
233
Steve McIntyre0006ee22015-07-29 17:52:52 +0100234echo "Running test $NAME:"
235echo "($DESCRIPTION)"
236
Steve McIntyree28b84f2015-07-29 16:58:51 +0100237# Startup check
Steve McIntyre0006ee22015-07-29 17:52:52 +0100238STATUS=$(run_admin_command --status)
239log $STATUS
Steve McIntyree28b84f2015-07-29 16:58:51 +0100240
241# Preload some data - what are the switch IDs and port IDs of the
242# various things in our test setup?
243for SWITCH in $SWITCHES; do
244 export ${SWITCH}_ID=$(run_admin_command --lookup_switch_by_name $SWITCH)
245done
246
247# Generate more detailed data for the hosts by asking VLANd what the
248# IDs are for each port, etc.
249for host in $HOSTS; do
250 SWITCH_PORT_VAR=${host}_SWITCH_PORT
251 SW=${!SWITCH_PORT_VAR%%:*}
252 SW_VAR=${SW}_ID
253 SW_ID=${!SW_VAR}
254 PORT_NAME=${!SWITCH_PORT_VAR##*:}
255 PORT_ID=$(run_admin_command --lookup_port_by_switch_and_name $SW_ID $PORT_NAME)
256 export ${host}_PORT_ID=${PORT_ID}
257 CURRENT_VLAN_ID=$(run_admin_command --get_port_current_vlan $PORT_ID)
258 export ${host}_CURRENT_VLANID=$CURRENT_VLAN_ID
259 CURRENT_VLAN_TAG=$(run_admin_command --show_vlan_tag $CURRENT_VLAN_ID)
260 export ${host}_CURRENT_VLAN_TAG=$CURRENT_VLAN_TAG
261done