CSIT-360: Parallel test sets run
[csit.git] / bootstrap.sh
1 #!/bin/bash
2 # Copyright (c) 2016 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -x
16
17 cat /etc/hostname
18 cat /etc/hosts
19
20 export DEBIAN_FRONTEND=noninteractive
21 sudo apt-get -y update
22 sudo apt-get -y install libpython2.7-dev python-virtualenv
23
24 VIRL_SERVERS=("10.30.51.28" "10.30.51.29" "10.30.51.30")
25
26 VIRL_USERNAME=jenkins-in
27 VIRL_PKEY=priv_key
28 VIRL_SERVER_STATUS_FILE="status"
29 VIRL_SERVER_EXPECTED_STATUS="PRODUCTION"
30
31 SSH_OPTIONS="-i ${VIRL_PKEY} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o LogLevel=error"
32
33 TEST_GROUPS=("bridge_domain,dhcp,gre,honeycomb,l2_xconnect,lisp,softwire" "cop,ipfix,ipsec,ipv6,rpf,tap,vrf" "fds,iacl,ipv4,policer,vlan,vxlan")
34 SUITE_PATH="tests.func"
35
36 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
37 LOG_PATH="${SCRIPT_DIR}"
38
39 # Create temp dir for tarballs
40 mkdir ${SCRIPT_DIR}/../temp
41 export TMPDIR="${SCRIPT_DIR}/../temp"
42
43 SHARED_MEMORY_PATH="/run/shm"
44
45 function ssh_do() {
46     echo
47     echo "### "  ssh $@
48     ssh ${SSH_OPTIONS} $@
49 }
50
51 rm -f ${VIRL_PKEY}
52 cat > ${VIRL_PKEY} <<EOF
53 -----BEGIN RSA PRIVAT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
70 UIrjsdzSJK/9Zb9yTTHPyibYzNEgaJV1HsbxfEFXAoGAYO2RmvRm0phll18OQVJV
71 IpKk9kLKAKZ/R/K32hAsikBC8SVPQTPniyaifFWx81diblalff2hX4ipTf7Yx24I
72 wMIMZuW7Im/R7QMef4+94G3Bad7p7JuE/qnAEHJ2OBnu+eYfxaK35XDsrq6XMazS
73 NqHE7hOq3giVfgg+C12hCKMCgYEAtu9dbYcG5owbehxzfRI2/OCRsjz/t1bv1seM
74 xVMND4XI6xb/apBWAZgZpIFrqrWoIBM3ptfsKipZe91ngBPUnL9s0Dolx452RVAj
75 yctHB8uRxWYgqDkjsxtzXf1HnZBBkBS8CUzYj+hdfuddoeKLaY3invXLCiV+PpXS
76 U4KAK9kCgYEAtSv0m5+Fg74BbAiFB6kCh11FYkW94YI6B/E2D/uVTD5dJhyEUFgZ
77 cWsudXjMki8734WSpMBqBp/J8wG3C9ZS6IpQD+U7UXA+roB7Qr+j4TqtWfM+87Rh
78 maOpG56uAyR0w5Z9BhwzA3VakibVk9KwDgZ29WtKFzuATLFnOtCS46E=
79 -----END RSA PRIVATE KEY-----
80 EOF
81 chmod 600 ${VIRL_PKEY}
82
83 #
84 # Pick a random host from the array of VIRL servers for every test set run
85 # and attempt to reach it and verify it's status.
86 #
87 # The server must be reachable and have a "status" file with
88 # the content "PRODUCTION" to be selected.
89 #
90 # If the server is not reachable or does not have the correct
91 # status remove it from the array and start again.
92 #
93 # Abort if there are no more servers left in the array.
94 #
95 for index in "${!TEST_GROUPS[@]}"; do
96     VIRL_SERVER[${index}]=""
97     while [[ ! "${VIRL_SERVER[${index}]}" ]]; do
98         num_hosts=${#VIRL_SERVERS[@]}
99         if [ $num_hosts == 0 ]
100         then
101             echo "No more VIRL candidate hosts available, failing."
102             exit 127
103         fi
104         element=$[ $RANDOM % $num_hosts ]
105         virl_server_candidate=${VIRL_SERVERS[$element]}
106         virl_server_status=$(ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${virl_server_candidate} cat $VIRL_SERVER_STATUS_FILE 2>&1)
107         echo VIRL HOST $virl_server_candidate status is \"$virl_server_status\"
108         if [ "$virl_server_status" == "$VIRL_SERVER_EXPECTED_STATUS" ]
109         then
110             # Candidate is in good status. Select this server.
111             VIRL_SERVER[${index}]="$virl_server_candidate"
112         else
113             # Candidate is in bad status. Remove from array.
114             VIRL_SERVERS=("${VIRL_SERVERS[@]:0:$element}" "${VIRL_SERVERS[@]:$[$element+1]}")
115         fi
116     done
117 done
118
119 echo "Selected VIRL servers: ${VIRL_SERVER[@]}"
120
121 # Temporarily download VPP packages from nexus.fd.io
122 if [ "${#}" -ne "0" ]; then
123     arr=(${@})
124     echo ${arr[0]}
125 else
126     rm -f *.deb
127     VPP_STABLE_VER=$(cat ${SCRIPT_DIR}/VPP_STABLE_VER)
128     VPP_REPO_URL=$(cat ${SCRIPT_DIR}/VPP_REPO_URL)
129     VPP_CLASSIFIER="-deb"
130     wget -q "${VPP_REPO_URL}/vpp/${VPP_STABLE_VER}/vpp-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
131     wget -q "${VPP_REPO_URL}/vpp-dbg/${VPP_STABLE_VER}/vpp-dbg-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
132     wget -q "${VPP_REPO_URL}/vpp-dev/${VPP_STABLE_VER}/vpp-dev-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
133     wget -q "${VPP_REPO_URL}/vpp-dpdk-dev/${VPP_STABLE_VER}/vpp-dpdk-dev-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
134     wget -q "${VPP_REPO_URL}/vpp-dpdk-dkms/${VPP_STABLE_VER}/vpp-dpdk-dkms-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
135     wget -q "${VPP_REPO_URL}/vpp-lib/${VPP_STABLE_VER}/vpp-lib-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
136     wget -q "${VPP_REPO_URL}/vpp-plugins/${VPP_STABLE_VER}/vpp-plugins-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
137 fi
138
139 VPP_DEBS=(*.deb)
140 echo ${VPP_DEBS[@]}
141 VIRL_DIR_LOC="/tmp"
142 VPP_DEBS_FULL=(${VPP_DEBS[@]})
143
144 # Prepend directory location at remote host to deb file list
145 for index in "${!VPP_DEBS_FULL[@]}"; do
146     VPP_DEBS_FULL[${index}]=${VIRL_DIR_LOC}/${VPP_DEBS_FULL[${index}]}
147 done
148
149 echo "Updated file names: " ${VPP_DEBS_FULL[@]}
150
151 cat ${VIRL_PKEY}
152
153 # Copy the files to VIRL hosts
154 DONE=""
155 for index in "${!VIRL_SERVER[@]}"; do
156     # Do not copy files in case they have already been copied to the VIRL host
157     [[ "${DONE[@]}" =~ "${VIRL_SERVER[${index}]}" ]] && copy=0 || copy=1
158
159     if [ "${copy}" -eq "0" ]; then
160         echo "VPP deb files have already been copied to the VIRL host ${VIRL_SERVER[${index}]}"
161     else
162         scp ${SSH_OPTIONS} *.deb \
163         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:${VIRL_DIR_LOC}/
164
165          result=$?
166         if [ "${result}" -ne "0" ]; then
167             echo "Failed to copy VPP deb files to VIRL host ${VIRL_SERVER[${index}]}"
168             echo ${result}
169             exit ${result}
170         else
171             echo "VPP deb files successfully copied to the VIRL host ${VIRL_SERVER[${index}]}"
172         fi
173         DONE+=(${VIRL_SERVER[${index}]})
174     fi
175 done
176
177 # Start a simulation on VIRL server
178
179 function stop_virl_simulation {
180     for index in "${!VIRL_SERVER[@]}"; do
181         ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\
182             "stop-testcase ${VIRL_SID[${index}]}"
183     done
184 }
185
186 # Upon script exit, cleanup the simulation execution
187 trap stop_virl_simulation EXIT
188
189 for index in "${!VIRL_SERVER[@]}"; do
190     echo "Starting simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}"
191     VIRL_SID[${index}]=$(ssh ${SSH_OPTIONS} \
192         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]} \
193         "start-testcase -c double-ring-nested ${VPP_DEBS_FULL[@]}")
194     retval=$?
195     if [ "$?" -ne "0" ]; then
196         echo "VIRL simulation start failed on ${VIRL_SERVER[${index}]}"
197         exit ${retval}
198     fi
199     if [[ ! "${VIRL_SID[${index}]}" =~ session-[a-zA-Z0-9_]{6} ]]; then
200         echo "No VIRL session ID reported."
201         exit 127
202     fi
203     echo "VIRL simulation started on ${VIRL_SERVER[${index}]}"
204
205     ssh_do ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\
206      cat /scratch/${VIRL_SID[${index}]}/topology.yaml
207
208     # Download the topology file from VIRL session and rename it
209     scp ${SSH_OPTIONS} \
210         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:/scratch/${VIRL_SID[${index}]}/topology.yaml \
211         topologies/enabled/topology${index}.yaml
212
213     retval=$?
214     if [ "$?" -ne "0" ]; then
215         echo "Failed to copy topology file from VIRL simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}"
216         exit ${retval}
217     fi
218 done
219
220 echo ${VIRL_SID[@]}
221
222 virtualenv --system-site-packages env
223 . env/bin/activate
224
225 echo pip install
226 pip install -r ${SCRIPT_DIR}/requirements.txt
227
228 for index in "${!VIRL_SERVER[@]}"; do
229     pykwalify -s ${SCRIPT_DIR}/resources/topology_schemas/3_node_topology.sch.yaml \
230               -s ${SCRIPT_DIR}/resources/topology_schemas/topology.sch.yaml \
231               -d ${SCRIPT_DIR}/topologies/enabled/topology${index}.yaml \
232               -vvv
233     if [ "$?" -ne "0" ]; then
234         echo "Topology${index} schema validation failed."
235         echo "However, the tests will start."
236     fi
237 done
238
239 function run_test_set() {
240     set +x
241     OLDIFS=$IFS
242     IFS=","
243     nr=$(echo $1)
244     rm -f ${LOG_PATH}/test_run${nr}.log
245     exec &> >(while read line; do echo "$(date +'%H:%M:%S') $line" \
246      >> ${LOG_PATH}/test_run${nr}.log; done;)
247     suite_str=""
248     for suite in ${TEST_GROUPS[${nr}]}; do
249         suite_str="${suite_str} --suite ${SUITE_PATH}.${suite}"
250     done
251     IFS=$OLDIFS
252
253     echo "PYTHONPATH=`pwd` pybot -L TRACE -W 136\
254         -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \
255         ${suite_str} \
256         --include vm_envAND3_node_single_link_topo \
257         --include vm_envAND3_node_double_link_topo \
258         --exclude PERFTEST \
259         --exclude SKIP_PATCH \
260         --noncritical EXPECTED_FAILING \
261         --output ${LOG_PATH}/log_test_set_run${nr} \
262         tests/"
263
264     PYTHONPATH=`pwd` pybot -L TRACE -W 136\
265         -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \
266         ${suite_str} \
267         --include vm_envAND3_node_single_link_topo \
268         --include vm_envAND3_node_double_link_topo \
269         --exclude PERFTEST \
270         --exclude SKIP_PATCH \
271         --noncritical EXPECTED_FAILING \
272         --output ${LOG_PATH}/log_test_set_run${nr} \
273         tests/
274
275     local_run_rc=$?
276     echo ${local_run_rc} > ${SHARED_MEMORY_PATH}/rc_test_run${nr}
277     set -x
278 }
279
280 set +x
281 # Send to background an instance of the run_test_set() function for each number,
282 # record the pid.
283 for index in "${!VIRL_SERVER[@]}"; do
284     run_test_set ${index} &
285     pid=$!
286     echo "Sent to background: Test_set${index} (pid=$pid)"
287     pids[$pid]=$index
288 done
289
290 echo
291 echo -n "Waiting..."
292
293 # Watch the stable of background processes.
294 # If a pid goes away, remove it from the array.
295 while [ -n "${pids[*]}" ]; do
296     for i in $(seq 0 9); do
297         sleep 1
298         echo -n "."
299     done
300     for pid in "${!pids[@]}"; do
301         if ! ps "$pid" >/dev/null; then
302             echo -e "\n"
303             echo "Test_set${pids[$pid]} with PID $pid finished."
304             unset pids[$pid]
305         fi
306     done
307     if [ -z "${!pids[*]}" ]; then
308         break
309     fi
310     echo -n -e "\nStill waiting for test set(s): ${pids[*]} ..."
311 done
312
313 echo
314 echo "All test set runs finished."
315 echo
316
317 set -x
318
319 RC=0
320 for index in "${!VIRL_SERVER[@]}"; do
321     echo "Test_set${index} log:"
322     cat ${LOG_PATH}/test_run${index}.log
323     RC_PARTIAL_RUN=$(cat ${SHARED_MEMORY_PATH}/rc_test_run${index})
324     RC=$((RC+RC_PARTIAL_RUN))
325     rm -f ${SHARED_MEMORY_PATH}/rc_test_run${index}
326     rm -f ${LOG_PATH}/test_run${index}.log
327     echo
328 done
329
330 # Log the final result
331 if [ "${RC}" -eq "0" ]; then
332     set +x
333     echo
334     echo "========================================================================================================================================"
335     echo "Final result of all test loops:                                                                                                 | PASS |"
336     echo "All critical tests have passed."
337     echo "========================================================================================================================================"
338     echo
339     set -x
340 else
341     if [ "${RC}" -eq "1" ]; then
342         HLP_STR="test has"
343     else
344         HLP_STR="tests have"
345     fi
346     set +x
347     echo
348     echo "========================================================================================================================================"
349     echo "Final result of all test loops:                                                                                                 | FAIL |"
350     echo "${RC} critical ${HLP_STR} failed."
351     echo "========================================================================================================================================"
352     echo
353     set -x
354 fi
355
356 echo Post-processing test data...
357
358 partial_logs=""
359 for index in "${!VIRL_SERVER[@]}"; do
360     partial_logs="${partial_logs} ${LOG_PATH}/log_test_set_run${index}.xml"
361 done
362
363 # Rebot output post-processing
364 rebot --noncritical EXPECTED_FAILING \
365       --output output.xml ${partial_logs}
366
367 # Remove unnecessary log files
368 rm -f ${partial_logs}
369
370 echo Post-processing finished.
371
372 if [ ${RC} -eq 0 ]; then
373     RETURN_STATUS=0
374 else
375     RETURN_STATUS=1
376 fi
377
378 exit ${RETURN_STATUS}

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.