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