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