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