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 |
| 44 | echo "${DATE}: $@" >&2 # Use stderr so we don't confuse anybody |
| 45 | # reading our output |
| 46 | echo " ${DATE}: $@" >> ${LOGFILE} |
| 47 | } |
| 48 | |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 49 | vecho () { |
| 50 | if [ $VERBOSE -gt 0 ] ; then |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 51 | log "$@" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 52 | fi |
| 53 | } |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 54 | |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 55 | vvecho () { |
| 56 | if [ $VERBOSE -gt 1 ] ; then |
| 57 | log "$@" |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | # Run a vland admin command, and optionally log both the full text of |
| 62 | # the command and its output |
| 63 | run_admin_command () { |
| 64 | ADMIN="python $TOPDIR/admin.py" |
| 65 | vvecho "Running \"$ADMIN $@\"" |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 66 | LAST_COMMAND="$@" |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 67 | RESULT=$($ADMIN $@) |
| 68 | vvecho " Result is \"$RESULT\"" |
| 69 | echo $RESULT |
| 70 | } |
| 71 | |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 72 | # Startup check |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 73 | run_admin_command --status |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 74 | |
| 75 | # Preload some data - what are the switch IDs and port IDs of the |
| 76 | # various things in our test setup? |
Steve McIntyre | a31981e | 2015-07-10 16:24:59 +0100 | [diff] [blame] | 77 | for SWITCH in $SWITCHES; do |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 78 | export ${SWITCH}_ID=$(run_admin_command --lookup_switch_by_name $SWITCH) |
Steve McIntyre | a31981e | 2015-07-10 16:24:59 +0100 | [diff] [blame] | 79 | done |
Steve McIntyre | 4c2c601 | 2015-01-26 16:17:38 +0000 | [diff] [blame] | 80 | |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 81 | # Generate more detailed data for the hosts by asking VLANd what the |
| 82 | # IDs are for each port, etc. |
| 83 | for host in $HOSTS; do |
| 84 | SWITCH_PORT_VAR=${host}_SWITCH_PORT |
| 85 | SW=${!SWITCH_PORT_VAR%%:*} |
| 86 | SW_VAR=${SW}_ID |
| 87 | SW_ID=${!SW_VAR} |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 88 | PORT_NAME=${!SWITCH_PORT_VAR##*:} |
| 89 | PORT_ID=$(run_admin_command --lookup_port_by_switch_and_name $SW_ID $PORT_NAME) |
| 90 | export ${host}_PORT_ID=${PORT_ID} |
| 91 | CURRENT_VLAN_ID=$(run_admin_command --get_port_current_vlan $PORT_ID) |
| 92 | export ${host}_CURRENT_VLANID=$CURRENT_VLAN_ID |
| 93 | CURRENT_VLAN_TAG=$(run_admin_command --show_vlan_tag $CURRENT_VLAN_ID) |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 94 | export ${host}_CURRENT_VLAN_TAG=$CURRENT_VLAN_TAG |
| 95 | done |
| 96 | |
| 97 | verify_host_is_base () { |
| 98 | HOST=$1 |
| 99 | PORT_ID_VAR=${HOST}_PORT_ID |
| 100 | PORT_ID=${!PORT_ID_VAR} |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 101 | CURRENT_VLAN_ID=$(run_admin_command --get_port_current_vlan $PORT_ID) |
| 102 | CURRENT_VLAN_TAG=$(run_admin_command --show_vlan_tag $CURRENT_VLAN_ID) |
| 103 | BASE_VLAN_ID=$(run_admin_command --get_port_base_vlan $PORT_ID) |
| 104 | BASE_VLAN_TAG=$(run_admin_command --show_vlan_tag $BASE_VLAN_ID) |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 105 | vecho "$HOST" |
| 106 | vecho " is on port ID $PORT_ID" |
| 107 | vecho " which is on VLAN ID $CURRENT_VLAN_ID, tag $CURRENT_VLAN_TAG" |
| 108 | vecho " and should be on base VLAN ID $BASE_VLAN_ID, tag $BASE_VLAN_TAG" |
| 109 | if [ $CURRENT_VLAN_ID == $BASE_VLAN_ID ] ; then |
| 110 | return 0 |
| 111 | else |
| 112 | return 1 |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | verify_all_hosts_are_base () { |
| 117 | # Check that all the machines are correctly on their base VLANs |
| 118 | for host in $HOSTS; do |
| 119 | verify_host_is_base $host |
| 120 | if [ $? -ne 0 ] ; then |
| 121 | return 1 |
| 122 | fi |
| 123 | done |
| 124 | } |
| 125 | |
| 126 | all_hosts () { |
| 127 | COMMAND="$@" |
| 128 | for HOST in ${HOSTS}; do |
Steve McIntyre | edf32d2 | 2015-07-17 15:19:03 +0100 | [diff] [blame] | 129 | vvecho "Running on ${HOST}:: ${COMMAND}" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 130 | ssh linaro@${HOST} "${COMMAND}" |
| 131 | done |
| 132 | } |
| 133 | |
| 134 | start_logging () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 135 | vecho "Starting logging on hosts" |
Steve McIntyre | e284894 | 2015-07-10 16:51:27 +0100 | [diff] [blame] | 136 | all_hosts "echo HOSTS=\\\"$HOSTS\\\" > test-config" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | stop_logging () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 140 | vecho "Stopping logging on hosts" |
Steve McIntyre | a31981e | 2015-07-10 16:24:59 +0100 | [diff] [blame] | 141 | all_hosts "rm -f test-config" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | grab_logs () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 145 | vecho "Grabbing logs" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 146 | all_hosts "cat /tmp/ping-log*" |
| 147 | } |
| 148 | |
| 149 | clear_logs () { |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 150 | vecho "Clearing old logs on hosts" |
Steve McIntyre | bbe4ae3 | 2015-02-12 08:57:28 +0000 | [diff] [blame] | 151 | all_hosts "rm -f /tmp/ping-log*" |
| 152 | } |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 153 | |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 154 | pause () { |
Steve McIntyre | b066a5a | 2015-07-28 18:39:09 +0100 | [diff] [blame] | 155 | vecho "Pausing $1 seconds for systems to settle and/or log results" |
Steve McIntyre | d0d693b | 2015-07-20 16:45:01 +0100 | [diff] [blame] | 156 | sleep $1 |
| 157 | } |
| 158 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 159 | cleanup () { |
| 160 | error=$? |
| 161 | if [ $error -ne 0 ] ; then |
| 162 | echo "Test script aborted with error $error - check logs for the failure" |
Steve McIntyre | b066a5a | 2015-07-28 18:39:09 +0100 | [diff] [blame] | 163 | echo " Last VLANd command appears to be \"$LAST_COMMAND\"" |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 164 | fi |
| 165 | } |
| 166 | |
Steve McIntyre | 4cbd522 | 2015-07-28 18:38:50 +0100 | [diff] [blame] | 167 | list_test_steps () { |
| 168 | sort -u ${LOGFILE} | awk ' |
| 169 | /CHECK.*START/ { |
| 170 | gsub("^.*CHECK ","") |
| 171 | gsub(" START.*$","") |
| 172 | start[$0]=1 |
| 173 | } |
| 174 | /CHECK.*END/ { |
| 175 | gsub("^.*CHECK ","") |
| 176 | gsub(" END.*$","") |
| 177 | if(start[$0] == 1) { |
| 178 | print $0 |
| 179 | start[$0] = 0 |
| 180 | } |
| 181 | }' |
| 182 | } |
| 183 | |
| 184 | check_test_steps () { |
| 185 | for STEP in $(list_test_steps); do |
| 186 | echo "Checking connectivity results for ${STEP}: " |
| 187 | check_test_step ${STEP} |
| 188 | done |
| 189 | } |
| 190 | |
| 191 | check_test_step () { |
| 192 | STEP=$1 |
| 193 | sort -u ${LOGFILE} | awk -v STEP=$1 " |
| 194 | /CHECK $STEP START/ { running=1 } |
| 195 | /CHECK $STEP END/ { running=0 } |
| 196 | /CHECK $STEP CHECK/ { |
| 197 | for( i = 4; i <= NF; i++) { |
| 198 | num_mach = split(\$i, mach_list, \":\") |
| 199 | for (j = 2; j <= num_mach; j++) { |
| 200 | test[mach_list[1]][mach_list[j]] = 1 |
| 201 | } |
| 202 | } |
| 203 | if (0 == 1) { |
| 204 | for (group in test) { |
| 205 | printf(\" $STEP test group %s contains: \", group) |
| 206 | for (host in test[group]) { |
| 207 | printf(\"%s \", host) |
| 208 | } |
| 209 | printf(\"\n\") |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | /UP/ { |
| 214 | if(running) { |
| 215 | OK=0 |
| 216 | for (group in test) { |
| 217 | if (test[group][\$2] && test[group][\$4]) { |
| 218 | #printf(\"GOOD UP: %s to %s (%s)\n\",\$2,\$4,group) |
| 219 | OK=1 |
| 220 | success++ |
| 221 | } |
| 222 | } |
| 223 | if (OK == 0) { |
| 224 | printf(\" BAD UP: %s to %s\n\",\$2,\$4) |
| 225 | fail++ |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | /DOWN/ { |
| 230 | if(running) { |
| 231 | OK=1 |
| 232 | for (group in test) { |
| 233 | if (test[group][\$2] && test[group][\$4]) { |
| 234 | printf(\" BAD DOWN: %s to %s (%s)\n\",\$2,\$4,group) |
| 235 | OK=0 |
| 236 | fail++ |
| 237 | } |
| 238 | } |
| 239 | if (OK == 1) { |
| 240 | # printf(\"GOOD DOWN: %s to %s\n\",\$2,\$4) |
| 241 | success++ |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | END { printf(\" success: %d, fail: %d\n\", success, fail) } |
| 246 | " |
| 247 | } |
| 248 | |
Steve McIntyre | 39db0e1 | 2015-07-20 16:22:08 +0100 | [diff] [blame] | 249 | trap cleanup 0 |