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 |
| 16 | if [ "$HOSTS"x = ""x ] ; then |
| 17 | HOSTS="panda01 panda02 panda03 arndale01 arndale02 arndale03 arndale04 imx5301 imx5302 imx5303" |
| 18 | fi |
| 19 | if [ "$SWITCHES"x = ""x ] ; then |
| 20 | SWITCHES="vlandswitch01 vlandswitch02 vlandswitch03 vlandswitch04 vlandswitch05" |
| 21 | fi |
| 22 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 23 | LAST_COMMAND="" |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 24 | |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 25 | # Topology definitions - how are the test machines connected? |
| 26 | panda01_SWITCH_PORT="vlandswitch01:Gi1/0/2" |
| 27 | panda02_SWITCH_PORT="vlandswitch02:fa25" |
| 28 | panda03_SWITCH_PORT="vlandswitch03:gi25" |
| 29 | arndale01_SWITCH_PORT="vlandswitch01:Gi1/0/3" |
| 30 | arndale02_SWITCH_PORT="vlandswitch02:fa2" |
| 31 | arndale03_SWITCH_PORT="vlandswitch03:gi2" |
Steve McIntyre | cda3ef9 | 2015-07-10 14:43:38 +0100 | [diff] [blame] | 32 | arndale04_SWITCH_PORT="vlandswitch04:Gi1/0/3" |
| 33 | imx5301_SWITCH_PORT="vlandswitch05:1/0/21" |
| 34 | imx5302_SWITCH_PORT="vlandswitch05:1/0/22" |
| 35 | imx5303_SWITCH_PORT="vlandswitch04:Gi1/0/2" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 36 | |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 37 | # Function for output; write text to terminal and to our logfile, with |
| 38 | # timestamp for sorting. |
| 39 | log () { |
Steve McIntyre | 716519e | 2015-07-17 15:21:01 +0100 | [diff] [blame] | 40 | DATE=$(date -u +%F/%H:%M:%S.%N) |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 41 | if [ "${LOGFILE}"x = ""x ] ; then |
| 42 | LOGFILE=$0.log |
| 43 | fi |
Steve McIntyre | 20ca8ae | 2015-07-29 17:14:49 +0100 | [diff] [blame] | 44 | if [ $VERBOSE -gt 0 ] ; then |
| 45 | echo "${DATE}: $@" >&2 # Use stderr so we don't confuse |
| 46 | # anybody reading our output |
| 47 | fi |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 48 | echo " ${DATE}: $@" >> ${LOGFILE} |
| 49 | } |
| 50 | |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 51 | vecho () { |
| 52 | if [ $VERBOSE -gt 0 ] ; then |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 53 | log "$@" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 54 | fi |
| 55 | } |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 56 | |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 57 | vvecho () { |
| 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 |
| 65 | run_admin_command () { |
| 66 | ADMIN="python $TOPDIR/admin.py" |
| 67 | vvecho "Running \"$ADMIN $@\"" |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 68 | LAST_COMMAND="$@" |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 69 | RESULT=$($ADMIN $@) |
| 70 | vvecho " Result is \"$RESULT\"" |
| 71 | echo $RESULT |
| 72 | } |
| 73 | |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 74 | verify_host_is_base () { |
| 75 | HOST=$1 |
| 76 | PORT_ID_VAR=${HOST}_PORT_ID |
| 77 | PORT_ID=${!PORT_ID_VAR} |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 78 | 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 McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 82 | 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 | |
| 93 | verify_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 | |
| 103 | all_hosts () { |
| 104 | COMMAND="$@" |
| 105 | for HOST in ${HOSTS}; do |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 106 | vvecho "Running on ${HOST}:: ${COMMAND}" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 107 | ssh linaro@${HOST} "${COMMAND}" |
| 108 | done |
| 109 | } |
| 110 | |
| 111 | start_logging () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 112 | vecho "Starting logging on hosts" |
Steve McIntyre | e284894 | 2015-07-10 16:51:27 +0100 | [diff] [blame] | 113 | all_hosts "echo HOSTS=\\\"$HOSTS\\\" > test-config" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | stop_logging () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 117 | vecho "Stopping logging on hosts" |
Steve McIntyre | a31981e | 2015-07-10 16:24:59 +0100 | [diff] [blame] | 118 | all_hosts "rm -f test-config" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | grab_logs () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 122 | vecho "Grabbing logs" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 123 | all_hosts "cat /tmp/ping-log*" |
| 124 | } |
| 125 | |
| 126 | clear_logs () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 127 | vecho "Clearing old logs on hosts" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 128 | all_hosts "rm -f /tmp/ping-log*" |
| 129 | } |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 130 | |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 131 | pause () { |
Steve McIntyre | b066a5a | 2015-07-28 18:39:09 +0100 | [diff] [blame] | 132 | vecho "Pausing $1 seconds for systems to settle and/or log results" |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 133 | sleep $1 |
| 134 | } |
| 135 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 136 | cleanup () { |
| 137 | error=$? |
| 138 | if [ $error -ne 0 ] ; then |
| 139 | echo "Test script aborted with error $error - check logs for the failure" |
Steve McIntyre | b066a5a | 2015-07-28 18:39:09 +0100 | [diff] [blame] | 140 | echo " Last VLANd command appears to be \"$LAST_COMMAND\"" |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 141 | fi |
| 142 | } |
| 143 | |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 144 | list_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 | |
| 161 | check_test_steps () { |
| 162 | for STEP in $(list_test_steps); do |
Steve McIntyre | 20ca8ae | 2015-07-29 17:14:49 +0100 | [diff] [blame] | 163 | echo " Checking connectivity results for ${STEP}: " |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 164 | check_test_step ${STEP} |
| 165 | done |
| 166 | } |
| 167 | |
| 168 | check_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 McIntyre | 20ca8ae | 2015-07-29 17:14:49 +0100 | [diff] [blame] | 222 | END { printf(\" success: %d, fail: %d\n\", success, fail) } |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 223 | " |
| 224 | } |
| 225 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 226 | trap cleanup 0 |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 227 | |
| 228 | ##################### |
| 229 | # |
| 230 | # MAIN CODE STARTS HERE |
| 231 | # |
| 232 | ##################### |
| 233 | |
Steve McIntyre | 0006ee2 | 2015-07-29 17:52:52 +0100 | [diff] [blame^] | 234 | echo "Running test $NAME:" |
| 235 | echo "($DESCRIPTION)" |
| 236 | |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 237 | # Startup check |
Steve McIntyre | 0006ee2 | 2015-07-29 17:52:52 +0100 | [diff] [blame^] | 238 | STATUS=$(run_admin_command --status) |
| 239 | log $STATUS |
Steve McIntyre | e28b84f | 2015-07-29 16:58:51 +0100 | [diff] [blame] | 240 | |
| 241 | # Preload some data - what are the switch IDs and port IDs of the |
| 242 | # various things in our test setup? |
| 243 | for SWITCH in $SWITCHES; do |
| 244 | export ${SWITCH}_ID=$(run_admin_command --lookup_switch_by_name $SWITCH) |
| 245 | done |
| 246 | |
| 247 | # Generate more detailed data for the hosts by asking VLANd what the |
| 248 | # IDs are for each port, etc. |
| 249 | for 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 |
| 261 | done |