blob: 4aa4941f1d7c47f7e58b5ebb7fc573b083740aad [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
16if [ "$HOSTS"x = ""x ] ; then
17 HOSTS="panda01 panda02 panda03 arndale01 arndale02 arndale03 arndale04 imx5301 imx5302 imx5303"
18fi
19if [ "$SWITCHES"x = ""x ] ; then
20 SWITCHES="vlandswitch01 vlandswitch02 vlandswitch03 vlandswitch04 vlandswitch05"
21fi
22
Steve McIntyre39db0e12015-07-20 16:22:08 +010023LAST_COMMAND=""
Steve McIntyre4c2c6012015-01-26 16:17:38 +000024
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000025# Topology definitions - how are the test machines connected?
26panda01_SWITCH_PORT="vlandswitch01:Gi1/0/2"
27panda02_SWITCH_PORT="vlandswitch02:fa25"
28panda03_SWITCH_PORT="vlandswitch03:gi25"
29arndale01_SWITCH_PORT="vlandswitch01:Gi1/0/3"
30arndale02_SWITCH_PORT="vlandswitch02:fa2"
31arndale03_SWITCH_PORT="vlandswitch03:gi2"
Steve McIntyrecda3ef92015-07-10 14:43:38 +010032arndale04_SWITCH_PORT="vlandswitch04:Gi1/0/3"
33imx5301_SWITCH_PORT="vlandswitch05:1/0/21"
34imx5302_SWITCH_PORT="vlandswitch05:1/0/22"
35imx5303_SWITCH_PORT="vlandswitch04:Gi1/0/2"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000036
Steve McIntyreedf32d22015-07-17 15:19:03 +010037# Function for output; write text to terminal and to our logfile, with
38# timestamp for sorting.
39log () {
Steve McIntyre716519e2015-07-17 15:21:01 +010040 DATE=$(date -u +%F/%H:%M:%S.%N)
Steve McIntyreedf32d22015-07-17 15:19:03 +010041 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 McIntyrebbe4ae32015-02-12 08:57:28 +000049vecho () {
50 if [ $VERBOSE -gt 0 ] ; then
Steve McIntyreedf32d22015-07-17 15:19:03 +010051 log "$@"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000052 fi
53}
Steve McIntyre4c2c6012015-01-26 16:17:38 +000054
Steve McIntyreedf32d22015-07-17 15:19:03 +010055vvecho () {
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
63run_admin_command () {
64 ADMIN="python $TOPDIR/admin.py"
65 vvecho "Running \"$ADMIN $@\""
Steve McIntyre39db0e12015-07-20 16:22:08 +010066 LAST_COMMAND="$@"
Steve McIntyreedf32d22015-07-17 15:19:03 +010067 RESULT=$($ADMIN $@)
68 vvecho " Result is \"$RESULT\""
69 echo $RESULT
70}
71
Steve McIntyre4c2c6012015-01-26 16:17:38 +000072# Startup check
Steve McIntyreedf32d22015-07-17 15:19:03 +010073run_admin_command --status
Steve McIntyre4c2c6012015-01-26 16:17:38 +000074
75# Preload some data - what are the switch IDs and port IDs of the
76# various things in our test setup?
Steve McIntyrea31981e2015-07-10 16:24:59 +010077for SWITCH in $SWITCHES; do
Steve McIntyreedf32d22015-07-17 15:19:03 +010078 export ${SWITCH}_ID=$(run_admin_command --lookup_switch_by_name $SWITCH)
Steve McIntyrea31981e2015-07-10 16:24:59 +010079done
Steve McIntyre4c2c6012015-01-26 16:17:38 +000080
Steve McIntyrebbe4ae32015-02-12 08:57:28 +000081# Generate more detailed data for the hosts by asking VLANd what the
82# IDs are for each port, etc.
83for 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 McIntyreedf32d22015-07-17 15:19:03 +010088 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 McIntyrebbe4ae32015-02-12 08:57:28 +000094 export ${host}_CURRENT_VLAN_TAG=$CURRENT_VLAN_TAG
95done
96
97verify_host_is_base () {
98 HOST=$1
99 PORT_ID_VAR=${HOST}_PORT_ID
100 PORT_ID=${!PORT_ID_VAR}
Steve McIntyreedf32d22015-07-17 15:19:03 +0100101 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 McIntyrebbe4ae32015-02-12 08:57:28 +0000105 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
116verify_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
126all_hosts () {
127 COMMAND="$@"
128 for HOST in ${HOSTS}; do
Steve McIntyreedf32d22015-07-17 15:19:03 +0100129 vvecho "Running on ${HOST}:: ${COMMAND}"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000130 ssh linaro@${HOST} "${COMMAND}"
131 done
132}
133
134start_logging () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100135 vecho "Starting logging on hosts"
Steve McIntyree2848942015-07-10 16:51:27 +0100136 all_hosts "echo HOSTS=\\\"$HOSTS\\\" > test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000137}
138
139stop_logging () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100140 vecho "Stopping logging on hosts"
Steve McIntyrea31981e2015-07-10 16:24:59 +0100141 all_hosts "rm -f test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000142}
143
144grab_logs () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100145 vecho "Grabbing logs"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000146 all_hosts "cat /tmp/ping-log*"
147}
148
149clear_logs () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100150 vecho "Clearing old logs on hosts"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000151 all_hosts "rm -f /tmp/ping-log*"
152}
Steve McIntyre39db0e12015-07-20 16:22:08 +0100153
Steve McIntyred0d693b2015-07-20 16:45:01 +0100154pause () {
Steve McIntyreb066a5a2015-07-28 18:39:09 +0100155 vecho "Pausing $1 seconds for systems to settle and/or log results"
Steve McIntyred0d693b2015-07-20 16:45:01 +0100156 sleep $1
157}
158
Steve McIntyre39db0e12015-07-20 16:22:08 +0100159cleanup () {
160 error=$?
161 if [ $error -ne 0 ] ; then
162 echo "Test script aborted with error $error - check logs for the failure"
Steve McIntyreb066a5a2015-07-28 18:39:09 +0100163 echo " Last VLANd command appears to be \"$LAST_COMMAND\""
Steve McIntyre39db0e12015-07-20 16:22:08 +0100164 fi
165}
166
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100167list_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
184check_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
191check_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 McIntyre39db0e12015-07-20 16:22:08 +0100249trap cleanup 0