blob: 65e739b0c97a2919e30f8c96597a536a7b30ef4b [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 McIntyrebbe4ae32015-02-12 08:57:28 +000072verify_host_is_base () {
73 HOST=$1
74 PORT_ID_VAR=${HOST}_PORT_ID
75 PORT_ID=${!PORT_ID_VAR}
Steve McIntyreedf32d22015-07-17 15:19:03 +010076 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 McIntyrebbe4ae32015-02-12 08:57:28 +000080 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
91verify_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
101all_hosts () {
102 COMMAND="$@"
103 for HOST in ${HOSTS}; do
Steve McIntyreedf32d22015-07-17 15:19:03 +0100104 vvecho "Running on ${HOST}:: ${COMMAND}"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000105 ssh linaro@${HOST} "${COMMAND}"
106 done
107}
108
109start_logging () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100110 vecho "Starting logging on hosts"
Steve McIntyree2848942015-07-10 16:51:27 +0100111 all_hosts "echo HOSTS=\\\"$HOSTS\\\" > test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000112}
113
114stop_logging () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100115 vecho "Stopping logging on hosts"
Steve McIntyrea31981e2015-07-10 16:24:59 +0100116 all_hosts "rm -f test-config"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000117}
118
119grab_logs () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100120 vecho "Grabbing logs"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000121 all_hosts "cat /tmp/ping-log*"
122}
123
124clear_logs () {
Steve McIntyred0d693b2015-07-20 16:45:01 +0100125 vecho "Clearing old logs on hosts"
Steve McIntyrebbe4ae32015-02-12 08:57:28 +0000126 all_hosts "rm -f /tmp/ping-log*"
127}
Steve McIntyre39db0e12015-07-20 16:22:08 +0100128
Steve McIntyred0d693b2015-07-20 16:45:01 +0100129pause () {
Steve McIntyreb066a5a2015-07-28 18:39:09 +0100130 vecho "Pausing $1 seconds for systems to settle and/or log results"
Steve McIntyred0d693b2015-07-20 16:45:01 +0100131 sleep $1
132}
133
Steve McIntyre39db0e12015-07-20 16:22:08 +0100134cleanup () {
135 error=$?
136 if [ $error -ne 0 ] ; then
137 echo "Test script aborted with error $error - check logs for the failure"
Steve McIntyreb066a5a2015-07-28 18:39:09 +0100138 echo " Last VLANd command appears to be \"$LAST_COMMAND\""
Steve McIntyre39db0e12015-07-20 16:22:08 +0100139 fi
140}
141
Steve McIntyre4cbd5222015-07-28 18:38:50 +0100142list_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
159check_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
166check_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 McIntyre39db0e12015-07-20 16:22:08 +0100224trap cleanup 0
Steve McIntyree28b84f2015-07-29 16:58:51 +0100225
226#####################
227#
228# MAIN CODE STARTS HERE
229#
230#####################
231
232# Startup check
233run_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?
237for SWITCH in $SWITCHES; do
238 export ${SWITCH}_ID=$(run_admin_command --lookup_switch_by_name $SWITCH)
239done
240
241# Generate more detailed data for the hosts by asking VLANd what the
242# IDs are for each port, etc.
243for 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
255done
256
257log "Running test $NAME"
258log "$DESCRIPTION"