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