Fix start-testcase script on VIRL
[csit.git] / bootstrap-ubuntu.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 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
32
33 VIRL_TOPOLOGY=$(cat ${SCRIPT_DIR}/VIRL_TOPOLOGY_UBUNTU)
34 VIRL_RELEASE=$(cat ${SCRIPT_DIR}/VIRL_RELEASE_UBUNTU)
35
36 SSH_OPTIONS="-i ${VIRL_PKEY} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o LogLevel=error"
37
38 TEST_GROUPS=("gre,ipv6,lisp,policer,rpf,softwire" "dhcp,ipsec,l2bd,l2xc,telemetry,vrf,vxlan" "cop,fds,honeycomb,iacl,ipv4,tap,vhost,vlan")
39 SUITE_PATH="tests.func"
40 SKIP_PATCH="SKIP_PATCH"
41
42 # Create tmp dir
43 mkdir ${SCRIPT_DIR}/tmp
44
45 # Use tmp dir to store log files
46 LOG_PATH="${SCRIPT_DIR}/tmp"
47
48 # Use tmp dir for tarballs
49 export TMPDIR="${SCRIPT_DIR}/tmp"
50
51 SHARED_MEMORY_PATH="/run/shm"
52
53 function ssh_do() {
54     echo
55     echo "### "  ssh $@
56     ssh ${SSH_OPTIONS} $@
57 }
58
59 rm -f ${VIRL_PKEY}
60 cat > ${VIRL_PKEY} <<EOF
61 -----BEGIN RSA PRIVATE KEY-----
62 MIIEpQIBAAKCAQEA+IHXq87GcqMR1C47rzx6Cbip5Ghq8pKrbqKrP5Nf41HcYrT6
63 GOXl9nFWKsMOzIlIn+8y7Il27eZh7csQGApbg8QLiHMtcYEmWNzKZpkqg4nuAPxX
64 VXwlKgnKX902SrET9Gp9TDayiHtCRWVfrlPPPSA0UEXW6BjLN/uHJ+W/Xzrrab+9
65 asBVa05vT2W6n0KJ66zfCaeDM912mQ6SttscAwFoWDmdHlegiVqrlIG2ABxOvxxz
66 L3dM3iSmlmQlzv9bThjo+nI4KFYh6m5wrZmAo5r/4q9CIJc21HVnTqkGOWJIZz6J
67 73lePJVSq5gYqaoGw3swFEA/MDkOx7baWKSoLQIDAQABAoIBAQCNBeolNp+JWJ76
68 gQ4fwLsknyXSV6sxYyhkDW4PEwwcTU06uqce0AAzXVffxne0fMe48x47+zqBgPbb
69 4huM+Pu8B9nfojUMr5TaYtl9Zbgpk3F8H7dT7LKOa6XrxvZTZrADSRc30+Z26zPN
70 e9zTaf42Gvt0/l0Zs1BHwbaOXqO+XuwJ3/F9Sf3PQYWXD3EOWjpHDP/X/1vAs6lV
71 SLkm6J/9KKE1m6I6LTYjIXuYt4SXybW6N2TSy54hhQtYcDUnIU2hR/PHVWKrGA0J
72 kELgrtTNTdbML27O5gFWU4PLUEYTZ9fN11D6qUZKxLcPOiPPHXkiILMRCCnG5DYI
73 ksBAU/YlAoGBAPxZO9VO18TYc8THV1nLKcvT2+1oSs1UcA2wNQMU55t910ZYinRa
74 MRwUhMOf8Mv5wOeiZaRICQB1PnVWtDVmGECgPpK6jUxqAwn8rgJcnoafLGL5YKMY
75 RVafTe6N5LXgCaOcJrk21wxs6v7ninEbUxxc575urOvZMBkymDw91dwbAoGBAPwa
76 YRhKhrzFKZzdK0RadVjnxKvolUllpoqqg3XuvmeAJHAOAnaOgVWq68NAcp5FZJv0
77 2D2Up7TX8pjf9MofP1SJbcraKBpK4NzfNkA0dSdEi+FhVofAJ9umB2o5LW1n7sab
78 UIrjsdzSJK/9Zb9yTTHPyibYzNEgaJV1HsbxfEFXAoGAYO2RmvRm0phll18OQVJV
79 IpKk9kLKAKZ/R/K32hAsikBC8SVPQTPniyaifFWx81diblalff2hX4ipTf7Yx24I
80 wMIMZuW7Im/R7QMef4+94G3Bad7p7JuE/qnAEHJ2OBnu+eYfxaK35XDsrq6XMazS
81 NqHE7hOq3giVfgg+C12hCKMCgYEAtu9dbYcG5owbehxzfRI2/OCRsjz/t1bv1seM
82 xVMND4XI6xb/apBWAZgZpIFrqrWoIBM3ptfsKipZe91ngBPUnL9s0Dolx452RVAj
83 yctHB8uRxWYgqDkjsxtzXf1HnZBBkBS8CUzYj+hdfuddoeKLaY3invXLCiV+PpXS
84 U4KAK9kCgYEAtSv0m5+Fg74BbAiFB6kCh11FYkW94YI6B/E2D/uVTD5dJhyEUFgZ
85 cWsudXjMki8734WSpMBqBp/J8wG3C9ZS6IpQD+U7UXA+roB7Qr+j4TqtWfM+87Rh
86 maOpG56uAyR0w5Z9BhwzA3VakibVk9KwDgZ29WtKFzuATLFnOtCS46E=
87 -----END RSA PRIVATE KEY-----
88 EOF
89 chmod 600 ${VIRL_PKEY}
90
91 #
92 # Pick a random host from the array of VIRL servers for every test set run
93 # and attempt to reach it and verify it's status.
94 #
95 # The server must be reachable and have a "status" file with
96 # the content "PRODUCTION" to be selected.
97 #
98 # If the server is not reachable or does not have the correct
99 # status remove it from the array and start again.
100 #
101 # Abort if there are no more servers left in the array.
102 #
103 for index in "${!TEST_GROUPS[@]}"; do
104     VIRL_SERVER[${index}]=""
105     while [[ ! "${VIRL_SERVER[${index}]}" ]]; do
106         num_hosts=${#VIRL_SERVERS[@]}
107         if [ $num_hosts == 0 ]
108         then
109             echo "No more VIRL candidate hosts available, failing."
110             exit 127
111         fi
112         element=$[ $RANDOM % $num_hosts ]
113         virl_server_candidate=${VIRL_SERVERS[$element]}
114         virl_server_status=$(ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${virl_server_candidate} cat $VIRL_SERVER_STATUS_FILE 2>&1)
115         echo VIRL HOST $virl_server_candidate status is \"$virl_server_status\"
116         if [ "$virl_server_status" == "$VIRL_SERVER_EXPECTED_STATUS" ]
117         then
118             # Candidate is in good status. Select this server.
119             VIRL_SERVER[${index}]="$virl_server_candidate"
120         else
121             # Candidate is in bad status. Remove from array.
122             VIRL_SERVERS=("${VIRL_SERVERS[@]:0:$element}" "${VIRL_SERVERS[@]:$[$element+1]}")
123         fi
124     done
125 done
126
127 echo "Selected VIRL servers: ${VIRL_SERVER[@]}"
128
129 # Temporarily download VPP and DPDK packages from nexus.fd.io
130 DPDK_STABLE_VER=$(cat ${SCRIPT_DIR}/DPDK_STABLE_VER)_amd64
131 VPP_REPO_URL=$(cat ${SCRIPT_DIR}/VPP_REPO_URL_UBUNTU)
132 VPP_CLASSIFIER="-deb"
133 if [ "${#}" -ne "0" ]; then
134     arr=(${@})
135     echo ${arr[0]}
136     SKIP_PATCH="skip_patchORskip_vpp_patch"
137     # Download DPDK parts not included in dpdk plugin of vpp build
138     wget -q "${VPP_REPO_URL}/vpp-dpdk-dkms/${DPDK_STABLE_VER}/vpp-dpdk-dkms-${DPDK_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
139 else
140     rm -f *.deb
141     VPP_STABLE_VER=$(cat ${SCRIPT_DIR}/VPP_STABLE_VER_UBUNTU)
142     wget -q "${VPP_REPO_URL}/vpp/${VPP_STABLE_VER}/vpp-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
143     wget -q "${VPP_REPO_URL}/vpp-dbg/${VPP_STABLE_VER}/vpp-dbg-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
144     wget -q "${VPP_REPO_URL}/vpp-dev/${VPP_STABLE_VER}/vpp-dev-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
145     wget -q "${VPP_REPO_URL}/vpp-dpdk-dkms/${DPDK_STABLE_VER}/vpp-dpdk-dkms-${DPDK_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
146     wget -q "${VPP_REPO_URL}/vpp-lib/${VPP_STABLE_VER}/vpp-lib-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
147     wget -q "${VPP_REPO_URL}/vpp-plugins/${VPP_STABLE_VER}/vpp-plugins-${VPP_STABLE_VER}${VPP_CLASSIFIER}.deb" || exit
148 fi
149
150 VPP_DEBS=(*.deb)
151 echo ${VPP_DEBS[@]}
152 VIRL_DIR_LOC="/tmp"
153 VPP_DEBS_FULL=(${VPP_DEBS[@]})
154
155 # Prepend directory location at remote host to deb file list
156 for index in "${!VPP_DEBS_FULL[@]}"; do
157     VPP_DEBS_FULL[${index}]=${VIRL_DIR_LOC}/${VPP_DEBS_FULL[${index}]}
158 done
159
160 echo "Updated file names: " ${VPP_DEBS_FULL[@]}
161
162 cat ${VIRL_PKEY}
163
164 # Copy the files to VIRL hosts
165 DONE=""
166 for index in "${!VIRL_SERVER[@]}"; do
167     # Do not copy files in case they have already been copied to the VIRL host
168     [[ "${DONE[@]}" =~ "${VIRL_SERVER[${index}]}" ]] && copy=0 || copy=1
169
170     if [ "${copy}" -eq "0" ]; then
171         echo "VPP deb files have already been copied to the VIRL host ${VIRL_SERVER[${index}]}"
172     else
173         scp ${SSH_OPTIONS} *.deb \
174         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:${VIRL_DIR_LOC}/
175
176         result=$?
177         if [ "${result}" -ne "0" ]; then
178             echo "Failed to copy VPP deb files to VIRL host ${VIRL_SERVER[${index}]}"
179             echo ${result}
180             exit ${result}
181         else
182             echo "VPP deb files successfully copied to the VIRL host ${VIRL_SERVER[${index}]}"
183         fi
184         DONE+=(${VIRL_SERVER[${index}]})
185     fi
186 done
187
188 # Start a simulation on VIRL server
189
190 function stop_virl_simulation {
191     for index in "${!VIRL_SERVER[@]}"; do
192         ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\
193             "stop-testcase ${VIRL_SID[${index}]}"
194     done
195 }
196
197 # Upon script exit, cleanup the simulation execution
198 trap stop_virl_simulation EXIT
199
200 for index in "${!VIRL_SERVER[@]}"; do
201     echo "Starting simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}"
202     VIRL_SID[${index}]=$(ssh ${SSH_OPTIONS} \
203         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]} \
204         "start-testcase -vv --copy ${VIRL_TOPOLOGY} \
205         --release ${VIRL_RELEASE} ${VPP_DEBS_FULL[@]}")
206     retval=$?
207     if [ ${retval} -ne "0" ]; then
208         echo "VIRL simulation start failed on ${VIRL_SERVER[${index}]}"
209         exit ${retval}
210     fi
211     if [[ ! "${VIRL_SID[${index}]}" =~ session-[a-zA-Z0-9_]{6} ]]; then
212         echo "No VIRL session ID reported."
213         exit 127
214     fi
215     echo "VIRL simulation started on ${VIRL_SERVER[${index}]}"
216
217     ssh_do ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\
218      cat /scratch/${VIRL_SID[${index}]}/topology.yaml
219
220     # Download the topology file from VIRL session and rename it
221     scp ${SSH_OPTIONS} \
222         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:/scratch/${VIRL_SID[${index}]}/topology.yaml \
223         topologies/enabled/topology${index}.yaml
224
225     retval=$?
226     if [ ${retval} -ne "0" ]; then
227         echo "Failed to copy topology file from VIRL simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}"
228         exit ${retval}
229     fi
230 done
231
232 echo ${VIRL_SID[@]}
233
234 virtualenv --system-site-packages env
235 . env/bin/activate
236
237 echo pip install
238 pip install -r ${SCRIPT_DIR}/requirements.txt
239
240 for index in "${!VIRL_SERVER[@]}"; do
241     pykwalify -s ${SCRIPT_DIR}/resources/topology_schemas/3_node_topology.sch.yaml \
242               -s ${SCRIPT_DIR}/resources/topology_schemas/topology.sch.yaml \
243               -d ${SCRIPT_DIR}/topologies/enabled/topology${index}.yaml \
244               -vvv
245     if [ "$?" -ne "0" ]; then
246         echo "Topology${index} schema validation failed."
247         echo "However, the tests will start."
248     fi
249 done
250
251 function run_test_set() {
252     set +x
253     OLDIFS=$IFS
254     IFS=","
255     nr=$(echo $1)
256     rm -f ${LOG_PATH}/test_run${nr}.log
257     exec &> >(while read line; do echo "$(date +'%H:%M:%S') $line" \
258      >> ${LOG_PATH}/test_run${nr}.log; done;)
259     suite_str=""
260     for suite in ${TEST_GROUPS[${nr}]}; do
261         suite_str="${suite_str} --suite ${SUITE_PATH}.${suite}"
262     done
263     IFS=$OLDIFS
264
265     echo "PYTHONPATH=`pwd` pybot -L TRACE -W 136\
266         -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \
267         ${suite_str} \
268         --include vm_envAND3_node_single_link_topo \
269         --include vm_envAND3_node_double_link_topo \
270         --exclude PERFTEST \
271         --exclude ${SKIP_PATCH} \
272         --noncritical EXPECTED_FAILING \
273         --output ${LOG_PATH}/log_test_set_run${nr} \
274         tests/"
275
276     PYTHONPATH=`pwd` pybot -L TRACE -W 136\
277         -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \
278         ${suite_str} \
279         --include vm_envAND3_node_single_link_topo \
280         --include vm_envAND3_node_double_link_topo \
281         --exclude PERFTEST \
282         --exclude ${SKIP_PATCH} \
283         --noncritical EXPECTED_FAILING \
284         --output ${LOG_PATH}/log_test_set_run${nr} \
285         tests/
286
287     local_run_rc=$?
288     echo ${local_run_rc} > ${SHARED_MEMORY_PATH}/rc_test_run${nr}
289     set -x
290 }
291
292 set +x
293 # Send to background an instance of the run_test_set() function for each number,
294 # record the pid.
295 for index in "${!VIRL_SERVER[@]}"; do
296     run_test_set ${index} &
297     pid=$!
298     echo "Sent to background: Test_set${index} (pid=$pid)"
299     pids[$pid]=$index
300 done
301
302 echo
303 echo -n "Waiting..."
304
305 # Watch the stable of background processes.
306 # If a pid goes away, remove it from the array.
307 while [ -n "${pids[*]}" ]; do
308     for i in $(seq 0 9); do
309         sleep 1
310         echo -n "."
311     done
312     for pid in "${!pids[@]}"; do
313         if ! ps "$pid" >/dev/null; then
314             echo -e "\n"
315             echo "Test_set${pids[$pid]} with PID $pid finished."
316             unset pids[$pid]
317         fi
318     done
319     if [ -z "${!pids[*]}" ]; then
320         break
321     fi
322     echo -n -e "\nStill waiting for test set(s): ${pids[*]} ..."
323 done
324
325 echo
326 echo "All test set runs finished."
327 echo
328
329 set -x
330
331 RC=0
332 for index in "${!VIRL_SERVER[@]}"; do
333     echo "Test_set${index} log:"
334     cat ${LOG_PATH}/test_run${index}.log
335     RC_PARTIAL_RUN=$(cat ${SHARED_MEMORY_PATH}/rc_test_run${index})
336     RC=$((RC+RC_PARTIAL_RUN))
337     rm -f ${SHARED_MEMORY_PATH}/rc_test_run${index}
338     rm -f ${LOG_PATH}/test_run${index}.log
339     echo
340 done
341
342 # Log the final result
343 if [ "${RC}" -eq "0" ]; then
344     set +x
345     echo
346     echo "========================================================================================================================================"
347     echo "Final result of all test loops:                                                                                                 | PASS |"
348     echo "All critical tests have passed."
349     echo "========================================================================================================================================"
350     echo
351     set -x
352 else
353     if [ "${RC}" -eq "1" ]; then
354         HLP_STR="test has"
355     else
356         HLP_STR="tests have"
357     fi
358     set +x
359     echo
360     echo "========================================================================================================================================"
361     echo "Final result of all test loops:                                                                                                 | FAIL |"
362     echo "${RC} critical ${HLP_STR} failed."
363     echo "========================================================================================================================================"
364     echo
365     set -x
366 fi
367
368 echo Post-processing test data...
369
370 partial_logs=""
371 for index in "${!VIRL_SERVER[@]}"; do
372     partial_logs="${partial_logs} ${LOG_PATH}/log_test_set_run${index}.xml"
373 done
374
375 # Rebot output post-processing
376 rebot --noncritical EXPECTED_FAILING \
377       --output output.xml ${partial_logs}
378
379 # Remove unnecessary log files
380 rm -f ${partial_logs}
381
382 echo Post-processing finished.
383
384 if [ ${RC} -eq 0 ]; then
385     RETURN_STATUS=0
386 else
387     RETURN_STATUS=1
388 fi
389
390 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.