blob: cdc50d35a53c62e70a01d647951088e602e35f5d [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
Steve McIntyre138ced92015-07-30 17:29:51 +010016if [ "$LOGVERBOSE"x = ""x ] ; then
17 LOGVERBOSE=$(($VERBOSE + 1))
18fi
Steve McIntyrea31981e2015-07-10 16:24:59 +010019if [ "$HOSTS"x = ""x ] ; then
20 HOSTS="panda01 panda02 panda03 arndale01 arndale02 arndale03 arndale04 imx5301 imx5302 imx5303"
21fi
22if [ "$SWITCHES"x = ""x ] ; then
23 SWITCHES="vlandswitch01 vlandswitch02 vlandswitch03 vlandswitch04 vlandswitch05"
24fi
25
Steve McIntyre39db0e12015-07-20 16:22:08 +010026LAST_COMMAND=""
Steve McIntyre4c2c6012015-01-26 16:17:38 +000027
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000028# Topology definitions - how are the test machines connected?
29panda01_SWITCH_PORT="vlandswitch01:Gi1/0/2"
30panda02_SWITCH_PORT="vlandswitch02:fa25"
31panda03_SWITCH_PORT="vlandswitch03:gi25"
32arndale01_SWITCH_PORT="vlandswitch01:Gi1/0/3"
33arndale02_SWITCH_PORT="vlandswitch02:fa2"
34arndale03_SWITCH_PORT="vlandswitch03:gi2"
Steve McIntyrecda3ef92015-07-10 14:43:38 +010035arndale04_SWITCH_PORT="vlandswitch04:Gi1/0/3"
36imx5301_SWITCH_PORT="vlandswitch05:1/0/21"
37imx5302_SWITCH_PORT="vlandswitch05:1/0/22"
38imx5303_SWITCH_PORT="vlandswitch04:Gi1/0/2"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000039
Steve McIntyre138ced92015-07-30 17:29:51 +010040# Function for output; write text to our logfile and (optionally) the
41# terminal, with timestamp for sorting.
42_log () {
Steve McIntyre716519e2015-07-17 15:21:01 +010043 DATE=$(date -u +%F/%H:%M:%S.%N)
Steve McIntyreedf32d22015-07-17 15:19:03 +010044 if [ "${LOGFILE}"x = ""x ] ; then
45 LOGFILE=$0.log
46 fi
Steve McIntyre138ced92015-07-30 17:29:51 +010047 LEVEL=$1
48 shift
49 if [ $VERBOSE -gt $LEVEL ] ; then
Steve McIntyre20ca8ae2015-07-29 17:14:49 +010050 echo "${DATE}: $@" >&2 # Use stderr so we don't confuse
51 # anybody reading our output
52 fi
Steve McIntyre138ced92015-07-30 17:29:51 +010053 if [ $LOGVERBOSE -gt $LEVEL ] ; then
54 echo " ${DATE}: $@" >> ${LOGFILE}
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000055 fi
56}
Steve McIntyre4c2c6012015-01-26 16:17:38 +000057
Steve McIntyre138ced92015-07-30 17:29:51 +010058log () {
59 _log 0 $@
60}
61
62vlog () {
63 _log 1 $@
64}
65
66vvlog () {
67 _log 2 $@
Steve McIntyreedf32d22015-07-17 15:19:03 +010068}
69
70# Run a vland admin command, and optionally log both the full text of
71# the command and its output
72run_admin_command () {
Steve McIntyre53787d22018-02-02 17:08:04 +000073 ADMIN="python $TOPDIR/vland-admin"
Steve McIntyre138ced92015-07-30 17:29:51 +010074 vlog "Running \"$ADMIN $@\""
Steve McIntyre39db0e12015-07-20 16:22:08 +010075 LAST_COMMAND="$@"
Steve McIntyreedf32d22015-07-17 15:19:03 +010076 RESULT=$($ADMIN $@)
Steve McIntyre138ced92015-07-30 17:29:51 +010077 vlog " Result is \"$RESULT\""
Steve McIntyreedf32d22015-07-17 15:19:03 +010078 echo $RESULT
79}
80
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000081verify_host_is_base () {
82 HOST=$1
83 PORT_ID_VAR=${HOST}_PORT_ID
84 PORT_ID=${!PORT_ID_VAR}
Steve McIntyre244a8f42016-03-12 12:36:47 +000085 CURRENT_VLAN_ID=$(run_admin_command get_port_current_vlan --port_id $PORT_ID)
86 CURRENT_VLAN_TAG=$(run_admin_command show_vlan_tag --vlan_id $CURRENT_VLAN_ID)
87 BASE_VLAN_ID=$(run_admin_command get_port_base_vlan --port_id $PORT_ID)
88 BASE_VLAN_TAG=$(run_admin_command show_vlan_tag --vlan_id $BASE_VLAN_ID)
Steve McIntyre138ced92015-07-30 17:29:51 +010089 vlog "$HOST"
90 vlog " is on port ID $PORT_ID"
91 vlog " which is on VLAN ID $CURRENT_VLAN_ID, tag $CURRENT_VLAN_TAG"
92 vlog " and should be on base VLAN ID $BASE_VLAN_ID, tag $BASE_VLAN_TAG"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000093 if [ $CURRENT_VLAN_ID == $BASE_VLAN_ID ] ; then
94 return 0
95 else
96 return 1
97 fi
98}
99
100verify_all_hosts_are_base () {
101 # Check that all the machines are correctly on their base VLANs
102 for host in $HOSTS; do
103 verify_host_is_base $host
104 if [ $? -ne 0 ] ; then
105 return 1
106 fi
107 done
108}
109
110all_hosts () {
111 COMMAND="$@"
112 for HOST in ${HOSTS}; do
Steve McIntyre138ced92015-07-30 17:29:51 +0100113 vvlog "Running on ${HOST}:: ${COMMAND}"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000114 ssh linaro@${HOST} "${COMMAND}"
115 done
116}
117
118start_logging () {
Steve McIntyre138ced92015-07-30 17:29:51 +0100119 log "Starting logging on hosts"
Steve McIntyree2848942015-07-10 16:51:27 +0100120 all_hosts "echo HOSTS=\\\"$HOSTS\\\" > test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000121}
122
123stop_logging () {
Steve McIntyre138ced92015-07-30 17:29:51 +0100124 log "Stopping logging on hosts"
Steve McIntyrea31981e2015-07-10 16:24:59 +0100125 all_hosts "rm -f test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000126}
127
128grab_logs () {
Steve McIntyre138ced92015-07-30 17:29:51 +0100129 log "Grabbing logs"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000130 all_hosts "cat /tmp/ping-log*"
131}
132
133clear_logs () {
Steve McIntyre138ced92015-07-30 17:29:51 +0100134 log "Clearing old logs on hosts"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000135 all_hosts "rm -f /tmp/ping-log*"
136}
Steve McIntyre39db0e12015-07-20 16:22:08 +0100137
Steve McIntyred0d693b2015-07-20 16:45:01 +0100138pause () {
Steve McIntyre138ced92015-07-30 17:29:51 +0100139 log "Pausing $1 seconds for systems to settle and/or log results"
Steve McIntyred0d693b2015-07-20 16:45:01 +0100140 sleep $1
141}
142
Steve McIntyre39db0e12015-07-20 16:22:08 +0100143cleanup () {
144 error=$?
145 if [ $error -ne 0 ] ; then
Steve McIntyre138ced92015-07-30 17:29:51 +0100146 _log -1 "Test script aborted with error $error - check logs for the failure"
147 _log -1 " Last VLANd command appears to be \"$LAST_COMMAND\""
Steve McIntyre39db0e12015-07-20 16:22:08 +0100148 fi
149}
150
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100151list_test_steps () {
152 sort -u ${LOGFILE} | awk '
153 /CHECK.*START/ {
154 gsub("^.*CHECK ","")
155 gsub(" START.*$","")
156 start[$0]=1
157 }
158 /CHECK.*END/ {
159 gsub("^.*CHECK ","")
160 gsub(" END.*$","")
161 if(start[$0] == 1) {
162 print $0
163 start[$0] = 0
164 }
165 }'
166}
167
168check_test_steps () {
Steve McIntyreb6b48b82015-07-30 17:27:49 +0100169 OK=1
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100170 for STEP in $(list_test_steps); do
Steve McIntyreb6b48b82015-07-30 17:27:49 +0100171 log "Checking connectivity results for ${STEP}: "
172 RESULT=$(check_test_step ${STEP})
173 log " $RESULT"
174 case "$RESULT" in
175 PASS*)
176 ;; # all good, do nothing
177 FAIL*)
178 echo " $STEP failed: $RESULT"
179 log " $STEP failed: $RESULT"
180 OK=0
181 ;;
182 esac
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100183 done
Steve McIntyreb6b48b82015-07-30 17:27:49 +0100184 if [ $OK -eq 1 ] ; then
185 echo " PASS"
186 log "PASS"
187 else
188 echo " FAIL"
189 log "FAIL"
190 false
191 fi
192
193 # Now clean up the log file
194 sort -u ${LOGFILE} > ${LOGFILE}.1
195 mv -f ${LOGFILE}.1 ${LOGFILE}
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100196}
197
198check_test_step () {
199 STEP=$1
200 sort -u ${LOGFILE} | awk -v STEP=$1 "
Steve McIntyre10962ad2015-07-30 17:26:53 +0100201 BEGIN { fails = \"\" }
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100202 /CHECK $STEP START/ { running=1 }
203 /CHECK $STEP END/ { running=0 }
204 /CHECK $STEP CHECK/ {
205 for( i = 4; i <= NF; i++) {
206 num_mach = split(\$i, mach_list, \":\")
207 for (j = 2; j <= num_mach; j++) {
208 test[mach_list[1]][mach_list[j]] = 1
209 }
210 }
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100211 }
212 /UP/ {
213 if(running) {
214 OK=0
215 for (group in test) {
216 if (test[group][\$2] && test[group][\$4]) {
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100217 OK=1
218 success++
219 }
220 }
221 if (OK == 0) {
Steve McIntyre0b7cce12015-07-31 14:25:10 +0100222 fails = sprintf(\"%s{BAD UP: %s %s to %s} \",fails,\$1,\$2,\$4)
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100223 fail++
224 }
225 }
226 }
227 /DOWN/ {
228 if(running) {
229 OK=1
230 for (group in test) {
231 if (test[group][\$2] && test[group][\$4]) {
Steve McIntyre0b7cce12015-07-31 14:25:10 +0100232 fails = sprintf(\"%s{BAD DOWN: %s %s to %s (%s)} \",fails,\$1,\$2,\$4,group)
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100233 OK=0
234 fail++
235 }
236 }
237 if (OK == 1) {
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100238 success++
239 }
240 }
241 }
Steve McIntyre5263e792015-07-30 14:13:31 +0100242 END {
Steve McIntyre10962ad2015-07-30 17:26:53 +0100243 if (fail > 0) {
244 printf(\"FAIL: success %d, fail %d (%s)\n\", success, fail, fails)
245 } else {
246 printf(\"PASS: success %d, fail %d\n\", success, fail)
247 }
Steve McIntyre5263e792015-07-30 14:13:31 +0100248 }
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100249"
250}
251
Steve McIntyre39db0e12015-07-20 16:22:08 +0100252trap cleanup 0
Steve McIntyree28b84f2015-07-29 16:58:51 +0100253
254#####################
255#
256# MAIN CODE STARTS HERE
257#
258#####################
259
Steve McIntyre0006ee22015-07-29 17:52:52 +0100260echo "Running test $NAME:"
Steve McIntyre138ced92015-07-30 17:29:51 +0100261echo " ($DESCRIPTION)"
262echo " Logging to ${LOGFILE}"
Steve McIntyre0006ee22015-07-29 17:52:52 +0100263
Steve McIntyree28b84f2015-07-29 16:58:51 +0100264# Startup check
Steve McIntyre244a8f42016-03-12 12:36:47 +0000265STATUS=$(run_admin_command status)
Steve McIntyre0006ee22015-07-29 17:52:52 +0100266log $STATUS
Steve McIntyree28b84f2015-07-29 16:58:51 +0100267
268# Preload some data - what are the switch IDs and port IDs of the
269# various things in our test setup?
270for SWITCH in $SWITCHES; do
Steve McIntyre244a8f42016-03-12 12:36:47 +0000271 export ${SWITCH}_ID=$(run_admin_command lookup_switch_by_name --name $SWITCH)
Steve McIntyree28b84f2015-07-29 16:58:51 +0100272done
273
274# Generate more detailed data for the hosts by asking VLANd what the
275# IDs are for each port, etc.
276for host in $HOSTS; do
277 SWITCH_PORT_VAR=${host}_SWITCH_PORT
278 SW=${!SWITCH_PORT_VAR%%:*}
279 SW_VAR=${SW}_ID
280 SW_ID=${!SW_VAR}
281 PORT_NAME=${!SWITCH_PORT_VAR##*:}
Steve McIntyre244a8f42016-03-12 12:36:47 +0000282 PORT_ID=$(run_admin_command lookup_port_by_switch_and_name --switch_id $SW_ID --name $PORT_NAME)
Steve McIntyree28b84f2015-07-29 16:58:51 +0100283 export ${host}_PORT_ID=${PORT_ID}
Steve McIntyre244a8f42016-03-12 12:36:47 +0000284 CURRENT_VLAN_ID=$(run_admin_command get_port_current_vlan --port_id $PORT_ID)
Steve McIntyree28b84f2015-07-29 16:58:51 +0100285 export ${host}_CURRENT_VLANID=$CURRENT_VLAN_ID
Steve McIntyre244a8f42016-03-12 12:36:47 +0000286 CURRENT_VLAN_TAG=$(run_admin_command show_vlan_tag --vlan_id $CURRENT_VLAN_ID)
Steve McIntyree28b84f2015-07-29 16:58:51 +0100287 export ${host}_CURRENT_VLAN_TAG=$CURRENT_VLAN_TAG
288done