docs: created select list of tests for report and trending.
[csit.git] / bootstrap.sh
1 #!/bin/bash
2 # Copyright (c) 2018 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 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21 export PYTHONPATH=${SCRIPT_DIR}
22
23 OS_ID=$(grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
24 OS_VERSION_ID=$(grep '^VERSION_ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
25
26 if [ "$OS_ID" == "centos" ]; then
27     DISTRO="CENTOS"
28     PACKAGE="rpm"
29     # TODO: Remove when corresponding part of CSIT-1546 is addressed.
30     sudo yum install -y python-devel python-virtualenv openssh-clients sshpass
31 elif [ "$OS_ID" == "ubuntu" ]; then
32     DISTRO="UBUNTU"
33     PACKAGE="deb"
34     # TODO: Remove when corresponding part of CSIT-1546 is addressed.
35     export DEBIAN_FRONTEND=noninteractive
36     sudo apt-get -y update
37     sudo apt-get -y install libpython2.7-dev python-virtualenv sshpass
38 else
39     echo "$OS_ID is not yet supported."
40     exit 1
41 fi
42
43 # Temporarily download VPP and DPDK packages from nexus.fd.io
44 if [ "${#}" -ne "0" ]; then
45     arr=(${@})
46     echo ${arr[0]}
47     SKIP_PATCH="skip_patchORskip_vpp_patch"
48 else
49     VPP_VERSION=$(< ${SCRIPT_DIR}/VPP_STABLE_VER_${DISTRO})
50     CSIT_DIR=${SCRIPT_DIR}
51     source "${SCRIPT_DIR}/resources/libraries/bash/function/artifacts.sh"
52     download_artifacts
53     # Need to revert -euo as the rest of script is not optimized for this.
54     set +euo pipefail
55 fi
56
57 VIRL_DIR_LOC="/tmp/"
58 VPP_PKGS=(*vpp*.$PACKAGE)
59 VPP_PKGS_FULL=("${VPP_PKGS[@]/#/${VIRL_DIR_LOC}}")
60 echo ${VPP_PKGS[@]}
61
62 VIRL_TOPOLOGY=$(cat ${SCRIPT_DIR}/VIRL_TOPOLOGY_${DISTRO})
63 VIRL_RELEASE=$(cat ${SCRIPT_DIR}/VIRL_RELEASE_${DISTRO})
64 VIRL_SERVERS=("10.30.51.28" "10.30.51.29" "10.30.51.30")
65 IPS_PER_VIRL=( "10.30.51.28:252"
66                "10.30.51.29:252"
67                "10.30.51.30:252" )
68 SIMS_PER_VIRL=( "10.30.51.28:13"
69                "10.30.51.29:13"
70                "10.30.51.30:13" )
71 IPS_PER_SIMULATION=5
72
73 function get_max_ip_nr() {
74     virl_server=$1
75     IP_VALUE="0"
76     for item in "${IPS_PER_VIRL[@]}" ; do
77         if [ "${item%%:*}" == "${virl_server}" ]
78         then
79             IP_VALUE=${item#*:}
80             break
81         fi
82     done
83     echo "$IP_VALUE"
84 }
85
86 function get_max_sim_nr() {
87     virl_server=$1
88     SIM_VALUE="0"
89     for item in "${SIMS_PER_VIRL[@]}" ; do
90         if [ "${item%%:*}" == "${virl_server}" ]
91         then
92             SIM_VALUE=${item#*:}
93             break
94         fi
95     done
96     echo "$SIM_VALUE"
97 }
98
99 VIRL_USERNAME=jenkins-in
100 VIRL_PKEY=priv_key
101 VIRL_SERVER_STATUS_FILE="status"
102 VIRL_SERVER_EXPECTED_STATUS="PRODUCTION"
103
104 SSH_OPTIONS="-i ${VIRL_PKEY} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o LogLevel=error"
105
106 TEST_GROUPS=("ip4_tunnels,l2bd" "ip4,ip6_tunnels,l2xc")
107 SUITE_PATH="tests.vpp.func"
108 SKIP_PATCH="SKIP_PATCH"
109
110 # Create tmp dir
111 mkdir ${SCRIPT_DIR}/tmp
112
113 # Use tmp dir to store log files
114 LOG_PATH="${SCRIPT_DIR}/tmp"
115
116 # Use tmp dir for tarballs
117 export TMPDIR="${SCRIPT_DIR}/tmp"
118
119 function ssh_do() {
120     echo
121     echo "### "  ssh $@
122     ssh ${SSH_OPTIONS} $@
123 }
124
125 rm -f ${VIRL_PKEY}
126 cat > ${VIRL_PKEY} <<EOF
127 -----BEGIN RSA PRIVATE KEY-----
128 MIIEpQIBAAKCAQEA+IHXq87GcqMR1C47rzx6Cbip5Ghq8pKrbqKrP5Nf41HcYrT6
129 GOXl9nFWKsMOzIlIn+8y7Il27eZh7csQGApbg8QLiHMtcYEmWNzKZpkqg4nuAPxX
130 VXwlKgnKX902SrET9Gp9TDayiHtCRWVfrlPPPSA0UEXW6BjLN/uHJ+W/Xzrrab+9
131 asBVa05vT2W6n0KJ66zfCaeDM912mQ6SttscAwFoWDmdHlegiVqrlIG2ABxOvxxz
132 L3dM3iSmlmQlzv9bThjo+nI4KFYh6m5wrZmAo5r/4q9CIJc21HVnTqkGOWJIZz6J
133 73lePJVSq5gYqaoGw3swFEA/MDkOx7baWKSoLQIDAQABAoIBAQCNBeolNp+JWJ76
134 gQ4fwLsknyXSV6sxYyhkDW4PEwwcTU06uqce0AAzXVffxne0fMe48x47+zqBgPbb
135 4huM+Pu8B9nfojUMr5TaYtl9Zbgpk3F8H7dT7LKOa6XrxvZTZrADSRc30+Z26zPN
136 e9zTaf42Gvt0/l0Zs1BHwbaOXqO+XuwJ3/F9Sf3PQYWXD3EOWjpHDP/X/1vAs6lV
137 SLkm6J/9KKE1m6I6LTYjIXuYt4SXybW6N2TSy54hhQtYcDUnIU2hR/PHVWKrGA0J
138 kELgrtTNTdbML27O5gFWU4PLUEYTZ9fN11D6qUZKxLcPOiPPHXkiILMRCCnG5DYI
139 ksBAU/YlAoGBAPxZO9VO18TYc8THV1nLKcvT2+1oSs1UcA2wNQMU55t910ZYinRa
140 MRwUhMOf8Mv5wOeiZaRICQB1PnVWtDVmGECgPpK6jUxqAwn8rgJcnoafLGL5YKMY
141 RVafTe6N5LXgCaOcJrk21wxs6v7ninEbUxxc575urOvZMBkymDw91dwbAoGBAPwa
142 YRhKhrzFKZzdK0RadVjnxKvolUllpoqqg3XuvmeAJHAOAnaOgVWq68NAcp5FZJv0
143 2D2Up7TX8pjf9MofP1SJbcraKBpK4NzfNkA0dSdEi+FhVofAJ9umB2o5LW1n7sab
144 UIrjsdzSJK/9Zb9yTTHPyibYzNEgaJV1HsbxfEFXAoGAYO2RmvRm0phll18OQVJV
145 IpKk9kLKAKZ/R/K32hAsikBC8SVPQTPniyaifFWx81diblalff2hX4ipTf7Yx24I
146 wMIMZuW7Im/R7QMef4+94G3Bad7p7JuE/qnAEHJ2OBnu+eYfxaK35XDsrq6XMazS
147 NqHE7hOq3giVfgg+C12hCKMCgYEAtu9dbYcG5owbehxzfRI2/OCRsjz/t1bv1seM
148 xVMND4XI6xb/apBWAZgZpIFrqrWoIBM3ptfsKipZe91ngBPUnL9s0Dolx452RVAj
149 yctHB8uRxWYgqDkjsxtzXf1HnZBBkBS8CUzYj+hdfuddoeKLaY3invXLCiV+PpXS
150 U4KAK9kCgYEAtSv0m5+Fg74BbAiFB6kCh11FYkW94YI6B/E2D/uVTD5dJhyEUFgZ
151 cWsudXjMki8734WSpMBqBp/J8wG3C9ZS6IpQD+U7UXA+roB7Qr+j4TqtWfM+87Rh
152 maOpG56uAyR0w5Z9BhwzA3VakibVk9KwDgZ29WtKFzuATLFnOtCS46E=
153 -----END RSA PRIVATE KEY-----
154 EOF
155 chmod 600 ${VIRL_PKEY}
156
157 #
158 # The server must be reachable and have a "status" file with
159 # the content "PRODUCTION" to be selected.
160 #
161 # If the server is not reachable or does not have the correct
162 # status remove it from the array and start again.
163 #
164 # Abort if there are no more servers left in the array.
165 #
166 VIRL_PROD_SERVERS=()
167 for index in "${!VIRL_SERVERS[@]}"; do
168     virl_server_status=$(ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVERS[$index]} cat $VIRL_SERVER_STATUS_FILE 2>&1)
169     echo VIRL HOST ${VIRL_SERVERS[$index]} status is \"$virl_server_status\"
170     if [ "$virl_server_status" == "$VIRL_SERVER_EXPECTED_STATUS" ]
171     then
172         # Candidate is in good status. Add to array.
173         VIRL_PROD_SERVERS+=(${VIRL_SERVERS[$index]})
174     fi
175 done
176
177 VIRL_SERVERS=("${VIRL_PROD_SERVERS[@]}")
178 echo "VIRL servers in production: ${VIRL_SERVERS[@]}"
179 num_hosts=${#VIRL_SERVERS[@]}
180 if [ $num_hosts == 0 ]
181 then
182     echo "No more VIRL candidate hosts available, failing."
183     exit 127
184 fi
185
186 # Get the LOAD of each server based on number of active simulations (testcases)
187 VIRL_SERVER_LOAD=()
188 for index in "${!VIRL_SERVERS[@]}"; do
189     VIRL_SERVER_LOAD[${index}]=$(ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVERS[$index]} "list-testcases | grep session | wc -l")
190 done
191
192 # Pick for each TEST_GROUP least loaded server
193 VIRL_SERVER=()
194 for index in "${!TEST_GROUPS[@]}"; do
195     least_load_server_idx=$(echo "${VIRL_SERVER_LOAD[*]}" | tr -s ' ' '\n' | awk '{print($0" "NR)}' | sort -g -k1,1 | head -1 | cut -f2 -d' ')
196     least_load_server=${VIRL_SERVERS[$least_load_server_idx-1]}
197     VIRL_SERVER+=($least_load_server)
198     # Adjusting load as we are not going run simulation immediately
199     VIRL_SERVER_LOAD[$least_load_server_idx-1]=$((VIRL_SERVER_LOAD[$least_load_server_idx-1]+1))
200 done
201
202 echo "Selected VIRL servers: ${VIRL_SERVER[@]}"
203
204 cat ${VIRL_PKEY}
205
206 # Copy the files to VIRL hosts
207 DONE=""
208 for index in "${!VIRL_SERVER[@]}"; do
209     # Do not copy files in case they have already been copied to the VIRL host
210     [[ "${DONE[@]}" =~ "${VIRL_SERVER[${index}]}" ]] && copy=0 || copy=1
211
212     if [ "${copy}" -eq "0" ]; then
213         echo "VPP packages have already been copied to the VIRL host ${VIRL_SERVER[${index}]}"
214     else
215         scp ${SSH_OPTIONS} ${VPP_PKGS[@]} \
216         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:${VIRL_DIR_LOC}
217
218         result=$?
219         if [ "${result}" -ne "0" ]; then
220             echo "Failed to copy VPP packages to VIRL host ${VIRL_SERVER[${index}]}"
221             echo ${result}
222             exit ${result}
223         else
224             echo "VPP packages successfully copied to the VIRL host ${VIRL_SERVER[${index}]}"
225         fi
226         DONE+=(${VIRL_SERVER[${index}]})
227     fi
228 done
229
230 # Start a simulation on VIRL server
231
232 function stop_virl_simulation {
233     for index in "${!VIRL_SERVER[@]}"; do
234         ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\
235             "stop-testcase ${VIRL_SID[${index}]}"
236     done
237 }
238
239 # Upon script exit, cleanup the simulation execution
240 trap stop_virl_simulation EXIT
241
242 for index in "${!VIRL_SERVER[@]}"; do
243     echo "Starting simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}"
244     # Get given VIRL server limits for max. number of VMs and IPs
245     max_ips=$(get_max_ip_nr ${VIRL_SERVER[${index}]})
246     max_ips_from_sims=$(($(get_max_sim_nr ${VIRL_SERVER[${index}]})*IPS_PER_SIMULATION))
247     # Set quota to lower value
248     IP_QUOTA=$([ $max_ips -le $max_ips_from_sims ] && echo "$max_ips" || echo "$max_ips_from_sims")
249     # Start the simulation
250     VIRL_SID[${index}]=$(ssh ${SSH_OPTIONS} ${VIRL_USERNAME}@${VIRL_SERVER[${index}]} \
251         "start-testcase -vv \
252             --quota ${IP_QUOTA} \
253             --copy ${VIRL_TOPOLOGY} \
254             --expiry 180 \
255             --release ${VIRL_RELEASE} \
256             ${VPP_PKGS_FULL[@]}")
257         # TODO: remove param ${VPP_PKGS_FULL[@]} when start-testcase script is
258         # updated on all virl servers
259     retval=$?
260     if [ ${retval} -ne "0" ]; then
261         echo "VIRL simulation start failed on ${VIRL_SERVER[${index}]}"
262         exit ${retval}
263     fi
264     if [[ ! "${VIRL_SID[${index}]}" =~ session-[a-zA-Z0-9_]{6} ]]; then
265         echo "No VIRL session ID reported."
266         exit 127
267     fi
268     echo "VIRL simulation nr. ${index} started on ${VIRL_SERVER[${index}]}"
269
270     ssh_do ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}\
271      cat /scratch/${VIRL_SID[${index}]}/topology.yaml
272
273     # Download the topology file from VIRL session and rename it
274     scp ${SSH_OPTIONS} \
275         ${VIRL_USERNAME}@${VIRL_SERVER[${index}]}:/scratch/${VIRL_SID[${index}]}/topology.yaml \
276         topologies/enabled/topology${index}.yaml
277
278     retval=$?
279     if [ ${retval} -ne "0" ]; then
280         echo "Failed to copy topology file from VIRL simulation nr. ${index} on VIRL server ${VIRL_SERVER[${index}]}"
281         exit ${retval}
282     fi
283 done
284
285 echo ${VIRL_SID[@]}
286
287 virtualenv --system-site-packages env
288 . env/bin/activate
289
290 echo pip install
291 pip install -r ${SCRIPT_DIR}/requirements.txt
292
293 for index in "${!VIRL_SERVER[@]}"; do
294     pykwalify -s ${SCRIPT_DIR}/resources/topology_schemas/3_node_topology.sch.yaml \
295               -s ${SCRIPT_DIR}/resources/topology_schemas/topology.sch.yaml \
296               -d ${SCRIPT_DIR}/topologies/enabled/topology${index}.yaml \
297               -vvv
298     if [ "$?" -ne "0" ]; then
299         echo "Topology${index} schema validation failed."
300         echo "However, the tests will start."
301     fi
302 done
303
304 function run_test_set() {
305     set +x
306     OLDIFS=$IFS
307     IFS=","
308     nr=$(echo $1)
309     rm -f ${LOG_PATH}/test_run${nr}.log
310     exec &> >(while read line; do echo "$(date +'%H:%M:%S') $line" \
311      >> ${LOG_PATH}/test_run${nr}.log; done;)
312     suite_str=""
313     for suite in ${TEST_GROUPS[${nr}]}; do
314         suite_str="${suite_str} --suite ${SUITE_PATH}.${suite}"
315     done
316     IFS=$OLDIFS
317
318     echo "PYTHONPATH=`pwd` pybot -L TRACE -W 136\
319         -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \
320         ${suite_str} \
321         --include vm_envAND3_node_single_link_topo \
322         --include vm_envAND3_node_double_link_topo \
323         --exclude PERFTEST \
324         --exclude SOFTWIRE \
325         --exclude ${SKIP_PATCH} \
326         --exclude SKIP_TEST \
327         --noncritical EXPECTED_FAILING \
328         --output ${LOG_PATH}/log_test_set_run${nr} \
329         tests/"
330
331     PYTHONPATH=`pwd` pybot -L TRACE -W 136\
332         -v TOPOLOGY_PATH:${SCRIPT_DIR}/topologies/enabled/topology${nr}.yaml \
333         ${suite_str} \
334         --include vm_envAND3_node_single_link_topo \
335         --include vm_envAND3_node_double_link_topo \
336         --exclude PERFTEST \
337         --exclude SOFTWIRE \
338         --exclude ${SKIP_PATCH} \
339         --exclude SKIP_TEST \
340         --noncritical EXPECTED_FAILING \
341         --output ${LOG_PATH}/log_test_set_run${nr} \
342         tests/
343
344     local local_run_rc=$?
345     set -x
346     echo ${local_run_rc} > ${LOG_PATH}/rc_test_run${nr}
347 }
348
349 set +x
350 # Send to background an instance of the run_test_set() function for each number,
351 # record the pid.
352 for index in "${!VIRL_SERVER[@]}"; do
353     run_test_set ${index} &
354     pid=$!
355     echo "Sent to background: Test_set${index} (pid=$pid)"
356     pids[$pid]=$index
357 done
358
359 echo
360 echo -n "Waiting..."
361
362 # Watch the stable of background processes.
363 # If a pid goes away, remove it from the array.
364 while [ -n "${pids[*]}" ]; do
365     for i in $(seq 0 9); do
366         sleep 1
367         echo -n "."
368     done
369     for pid in "${!pids[@]}"; do
370         if ! ps "$pid" >/dev/null; then
371             echo -e "\n"
372             echo "Test_set${pids[$pid]} with PID $pid finished."
373             unset pids[$pid]
374         fi
375     done
376     if [ -z "${!pids[*]}" ]; then
377         break
378     fi
379     echo -n -e "\nStill waiting for test set(s): ${pids[*]} ..."
380 done
381
382 echo
383 echo "All test set runs finished."
384 echo
385
386 set -x
387
388 RC=0
389 for index in "${!VIRL_SERVER[@]}"; do
390     echo "Test_set${index} log:"
391     cat ${LOG_PATH}/test_run${index}.log
392     RC_PARTIAL_RUN=$(cat ${LOG_PATH}/rc_test_run${index})
393     if [ -z "$RC_PARTIAL_RUN" ]; then
394         echo "Failed to retrieve return code from test run ${index}"
395         exit 1
396     fi
397     RC=$((RC+RC_PARTIAL_RUN))
398     rm -f ${LOG_PATH}/rc_test_run${index}
399     rm -f ${LOG_PATH}/test_run${index}.log
400     echo
401 done
402
403 # Log the final result
404 if [ "${RC}" -eq "0" ]; then
405     set +x
406     echo
407     echo "========================================================================================================================================"
408     echo "Final result of all test loops:                                                                                                 | PASS |"
409     echo "All critical tests have passed."
410     echo "========================================================================================================================================"
411     echo
412     set -x
413 else
414     if [ "${RC}" -eq "1" ]; then
415         HLP_STR="test has"
416     else
417         HLP_STR="tests have"
418     fi
419     set +x
420     echo
421     echo "========================================================================================================================================"
422     echo "Final result of all test loops:                                                                                                 | FAIL |"
423     echo "${RC} critical ${HLP_STR} failed."
424     echo "========================================================================================================================================"
425     echo
426     set -x
427 fi
428
429 echo Post-processing test data...
430
431 partial_logs=""
432 for index in "${!VIRL_SERVER[@]}"; do
433     partial_logs="${partial_logs} ${LOG_PATH}/log_test_set_run${index}.xml"
434 done
435
436 # Rebot output post-processing
437 rebot --noncritical EXPECTED_FAILING \
438       --output output.xml ${partial_logs}
439
440 # Remove unnecessary log files
441 rm -f ${partial_logs}
442
443 echo Post-processing finished.
444
445 if [ ${RC} -eq 0 ]; then
446     RETURN_STATUS=0
447 else
448     RETURN_STATUS=1
449 fi
450
451 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.