Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 1 | # |
| 2 | # test-common |
| 3 | # |
| 4 | # Common set of definitions and functions to help with driving VLANd |
| 5 | # in testing |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 6 | # |
| 7 | # This specifically depends on *gawk* for the 2d arrays used in |
| 8 | # check_test_step() at the bottom |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 9 | |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 10 | TOPDIR=$(dirname $0)/.. |
Steve McIntyre | a31981e | 2015-07-10 16:24:59 +0100 | [diff] [blame] | 11 | |
| 12 | # Default settings |
| 13 | if [ "$VERBOSE"x = ""x ] ; then |
| 14 | VERBOSE=0 |
| 15 | fi |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 16 | if [ "$LOGVERBOSE"x = ""x ] ; then |
| 17 | LOGVERBOSE=$(($VERBOSE + 1)) |
| 18 | fi |
Steve McIntyre | a31981e | 2015-07-10 16:24:59 +0100 | [diff] [blame] | 19 | if [ "$HOSTS"x = ""x ] ; then |
| 20 | HOSTS="panda01 panda02 panda03 arndale01 arndale02 arndale03 arndale04 imx5301 imx5302 imx5303" |
| 21 | fi |
| 22 | if [ "$SWITCHES"x = ""x ] ; then |
| 23 | SWITCHES="vlandswitch01 vlandswitch02 vlandswitch03 vlandswitch04 vlandswitch05" |
| 24 | fi |
| 25 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 26 | LAST_COMMAND="" |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 27 | |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 28 | # Topology definitions - how are the test machines connected? |
| 29 | panda01_SWITCH_PORT="vlandswitch01:Gi1/0/2" |
| 30 | panda02_SWITCH_PORT="vlandswitch02:fa25" |
| 31 | panda03_SWITCH_PORT="vlandswitch03:gi25" |
| 32 | arndale01_SWITCH_PORT="vlandswitch01:Gi1/0/3" |
| 33 | arndale02_SWITCH_PORT="vlandswitch02:fa2" |
| 34 | arndale03_SWITCH_PORT="vlandswitch03:gi2" |
Steve McIntyre | cda3ef9 | 2015-07-10 14:43:38 +0100 | [diff] [blame] | 35 | arndale04_SWITCH_PORT="vlandswitch04:Gi1/0/3" |
| 36 | imx5301_SWITCH_PORT="vlandswitch05:1/0/21" |
| 37 | imx5302_SWITCH_PORT="vlandswitch05:1/0/22" |
| 38 | imx5303_SWITCH_PORT="vlandswitch04:Gi1/0/2" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 39 | |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 40 | # Function for output; write text to our logfile and (optionally) the |
| 41 | # terminal, with timestamp for sorting. |
| 42 | _log () { |
Steve McIntyre | 716519e | 2015-07-17 15:21:01 +0100 | [diff] [blame] | 43 | DATE=$(date -u +%F/%H:%M:%S.%N) |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 44 | if [ "${LOGFILE}"x = ""x ] ; then |
| 45 | LOGFILE=$0.log |
| 46 | fi |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 47 | LEVEL=$1 |
| 48 | shift |
| 49 | if [ $VERBOSE -gt $LEVEL ] ; then |
Steve McIntyre | 20ca8ae | 2015-07-29 17:14:49 +0100 | [diff] [blame] | 50 | echo "${DATE}: $@" >&2 # Use stderr so we don't confuse |
| 51 | # anybody reading our output |
| 52 | fi |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 53 | if [ $LOGVERBOSE -gt $LEVEL ] ; then |
| 54 | echo " ${DATE}: $@" >> ${LOGFILE} |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 55 | fi |
| 56 | } |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 57 | |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 58 | log () { |
| 59 | _log 0 $@ |
| 60 | } |
| 61 | |
| 62 | vlog () { |
| 63 | _log 1 $@ |
| 64 | } |
| 65 | |
| 66 | vvlog () { |
| 67 | _log 2 $@ |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | # Run a vland admin command, and optionally log both the full text of |
| 71 | # the command and its output |
| 72 | run_admin_command () { |
Steve McIntyre | 53787d2 | 2018-02-02 17:08:04 +0000 | [diff] [blame^] | 73 | ADMIN="python $TOPDIR/vland-admin" |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 74 | vlog "Running \"$ADMIN $@\"" |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 75 | LAST_COMMAND="$@" |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 76 | RESULT=$($ADMIN $@) |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 77 | vlog " Result is \"$RESULT\"" |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 78 | echo $RESULT |
| 79 | } |
| 80 | |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 81 | verify_host_is_base () { |
| 82 | HOST=$1 |
| 83 | PORT_ID_VAR=${HOST}_PORT_ID |
| 84 | PORT_ID=${!PORT_ID_VAR} |
Steve McIntyre | 244a8f4 | 2016-03-12 12:36:47 +0000 | [diff] [blame] | 85 | 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 McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 89 | 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 McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 93 | if [ $CURRENT_VLAN_ID == $BASE_VLAN_ID ] ; then |
| 94 | return 0 |
| 95 | else |
| 96 | return 1 |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | verify_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 | |
| 110 | all_hosts () { |
| 111 | COMMAND="$@" |
| 112 | for HOST in ${HOSTS}; do |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 113 | vvlog "Running on ${HOST}:: ${COMMAND}" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 114 | ssh linaro@${HOST} "${COMMAND}" |
| 115 | done |
| 116 | } |
| 117 | |
| 118 | start_logging () { |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 119 | log "Starting logging on hosts" |
Steve McIntyre | e284894 | 2015-07-10 16:51:27 +0100 | [diff] [blame] | 120 | all_hosts "echo HOSTS=\\\"$HOSTS\\\" > test-config" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | stop_logging () { |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 124 | log "Stopping logging on hosts" |
Steve McIntyre | a31981e | 2015-07-10 16:24:59 +0100 | [diff] [blame] | 125 | all_hosts "rm -f test-config" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | grab_logs () { |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 129 | log "Grabbing logs" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 130 | all_hosts "cat /tmp/ping-log*" |
| 131 | } |
| 132 | |
| 133 | clear_logs () { |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 134 | log "Clearing old logs on hosts" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 135 | all_hosts "rm -f /tmp/ping-log*" |
| 136 | } |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 137 | |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 138 | pause () { |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 139 | log "Pausing $1 seconds for systems to settle and/or log results" |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 140 | sleep $1 |
| 141 | } |
| 142 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 143 | cleanup () { |
| 144 | error=$? |
| 145 | if [ $error -ne 0 ] ; then |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 146 | _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 McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 148 | fi |
| 149 | } |
| 150 | |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 151 | list_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 | |
| 168 | check_test_steps () { |
Steve McIntyre | b6b48b8 | 2015-07-30 17:27:49 +0100 | [diff] [blame] | 169 | OK=1 |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 170 | for STEP in $(list_test_steps); do |
Steve McIntyre | b6b48b8 | 2015-07-30 17:27:49 +0100 | [diff] [blame] | 171 | 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 McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 183 | done |
Steve McIntyre | b6b48b8 | 2015-07-30 17:27:49 +0100 | [diff] [blame] | 184 | 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 McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | check_test_step () { |
| 199 | STEP=$1 |
| 200 | sort -u ${LOGFILE} | awk -v STEP=$1 " |
Steve McIntyre | 10962ad | 2015-07-30 17:26:53 +0100 | [diff] [blame] | 201 | BEGIN { fails = \"\" } |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 202 | /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 McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 211 | } |
| 212 | /UP/ { |
| 213 | if(running) { |
| 214 | OK=0 |
| 215 | for (group in test) { |
| 216 | if (test[group][\$2] && test[group][\$4]) { |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 217 | OK=1 |
| 218 | success++ |
| 219 | } |
| 220 | } |
| 221 | if (OK == 0) { |
Steve McIntyre | 0b7cce1 | 2015-07-31 14:25:10 +0100 | [diff] [blame] | 222 | fails = sprintf(\"%s{BAD UP: %s %s to %s} \",fails,\$1,\$2,\$4) |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 223 | 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 McIntyre | 0b7cce1 | 2015-07-31 14:25:10 +0100 | [diff] [blame] | 232 | fails = sprintf(\"%s{BAD DOWN: %s %s to %s (%s)} \",fails,\$1,\$2,\$4,group) |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 233 | OK=0 |
| 234 | fail++ |
| 235 | } |
| 236 | } |
| 237 | if (OK == 1) { |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 238 | success++ |
| 239 | } |
| 240 | } |
| 241 | } |
Steve McIntyre | 5263e79 | 2015-07-30 14:13:31 +0100 | [diff] [blame] | 242 | END { |
Steve McIntyre | 10962ad | 2015-07-30 17:26:53 +0100 | [diff] [blame] | 243 | 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 McIntyre | 5263e79 | 2015-07-30 14:13:31 +0100 | [diff] [blame] | 248 | } |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 249 | " |
| 250 | } |
| 251 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 252 | trap cleanup 0 |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 253 | |
| 254 | ##################### |
| 255 | # |
| 256 | # MAIN CODE STARTS HERE |
| 257 | # |
| 258 | ##################### |
| 259 | |
Steve McIntyre | 0006ee2 | 2015-07-29 17:52:52 +0100 | [diff] [blame] | 260 | echo "Running test $NAME:" |
Steve McIntyre | 138ced9 | 2015-07-30 17:29:51 +0100 | [diff] [blame] | 261 | echo " ($DESCRIPTION)" |
| 262 | echo " Logging to ${LOGFILE}" |
Steve McIntyre | 0006ee2 | 2015-07-29 17:52:52 +0100 | [diff] [blame] | 263 | |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 264 | # Startup check |
Steve McIntyre | 244a8f4 | 2016-03-12 12:36:47 +0000 | [diff] [blame] | 265 | STATUS=$(run_admin_command status) |
Steve McIntyre | 0006ee2 | 2015-07-29 17:52:52 +0100 | [diff] [blame] | 266 | log $STATUS |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 267 | |
| 268 | # Preload some data - what are the switch IDs and port IDs of the |
| 269 | # various things in our test setup? |
| 270 | for SWITCH in $SWITCHES; do |
Steve McIntyre | 244a8f4 | 2016-03-12 12:36:47 +0000 | [diff] [blame] | 271 | export ${SWITCH}_ID=$(run_admin_command lookup_switch_by_name --name $SWITCH) |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 272 | done |
| 273 | |
| 274 | # Generate more detailed data for the hosts by asking VLANd what the |
| 275 | # IDs are for each port, etc. |
| 276 | for 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 McIntyre | 244a8f4 | 2016-03-12 12:36:47 +0000 | [diff] [blame] | 282 | PORT_ID=$(run_admin_command lookup_port_by_switch_and_name --switch_id $SW_ID --name $PORT_NAME) |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 283 | export ${host}_PORT_ID=${PORT_ID} |
Steve McIntyre | 244a8f4 | 2016-03-12 12:36:47 +0000 | [diff] [blame] | 284 | CURRENT_VLAN_ID=$(run_admin_command get_port_current_vlan --port_id $PORT_ID) |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 285 | export ${host}_CURRENT_VLANID=$CURRENT_VLAN_ID |
Steve McIntyre | 244a8f4 | 2016-03-12 12:36:47 +0000 | [diff] [blame] | 286 | CURRENT_VLAN_TAG=$(run_admin_command show_vlan_tag --vlan_id $CURRENT_VLAN_ID) |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 287 | export ${host}_CURRENT_VLAN_TAG=$CURRENT_VLAN_TAG |
| 288 | done |