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