b3cae11885d417e454fd6e28099bb62b66581158
[yardstick.git] / tests / ci / yardstick-verify
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 #
12 # Set up the environment and run yardstick test suites.
13 #
14 # Example invocation: yardstick-verify -r 10.4.4.4 suite1.yaml suite2.yaml
15 #
16 # Openstack credentials must be set and the script must be run from its
17 # original location in the yardstick repo.
18 #
19 # This script is intended to be used by the CI pipeline but it may also
20 # be invoked manually.
21 #
22
23 SCRIPT=$0
24 SCRIPT_ARGS=$@
25
26 usage()
27 {
28     cat << EOF
29 usage: $0 options [TEST_SUITE ...]
30
31 If no test suites are given ping.yaml is run.
32
33 OPTIONS:
34    -h      Show this message
35    -r      Http target (example: -r 213.77.62.197/results)
36    -i      Influxdb target (example: -i 127.0.0.1:8086)
37
38            Default target is dump to file ($DISPATCHER_FILE_NAME)
39
40 EOF
41 }
42
43 DISPATCHER_TYPE=file
44 DISPATCHER_FILE_NAME="/tmp/yardstick.out"
45 DISPATCHER_HTTP_TARGET="http://testresults.opnfv.org/test/api/v1/results"
46 DISPATCHER_INFLUXDB_TARGET=
47
48 while getopts "r:i:h" OPTION; do
49     case $OPTION in
50         h)
51             usage
52             exit 0
53             ;;
54         r)
55             DISPATCHER_TYPE=http
56             DISPATCHER_HTTP_TARGET=http://${OPTARG}
57             DISPATCHER_FILE_NAME=
58             ;;
59         i)
60             DISPATCHER_TYPE=influxdb
61             DISPATCHER_INFLUXDB_TARGET=http://${OPTARG}
62             DISPATCHER_FILE_NAME=
63             ;;
64         *)
65             echo "${OPTION} is not a valid argument"
66             exit 1
67             ;;
68     esac
69 done
70
71 shift $[OPTIND - 1]
72 TEST_SUITES=$@
73
74 cleanup()
75 {
76     echo
77     echo "========== Cleanup =========="
78
79     if ! glance image-list; then
80         return
81     fi
82
83     for image in $(glance image-list | grep -e cirros-0.3.3 -e yardstick-trusty-server -e Ubuntu-14.04 | awk '{print $2}'); do
84         echo "Deleting image $image..."
85         glance image-delete $image || true
86     done
87
88     nova flavor-delete yardstick-flavor &> /dev/null || true
89 }
90
91 exitcode=""
92
93 error_exit()
94 {
95     local rc=$?
96
97     if [ -z "$exitcode" ]; then
98         # In case of recursive traps (!?)
99         exitcode=$rc
100     fi
101
102     cleanup
103
104     echo "Exiting with RC=$exitcode"
105
106     exit $exitcode
107 }
108
109 set -o errexit
110 set -o pipefail
111
112 install_yardstick()
113 {
114     echo
115     echo "========== Installing yardstick =========="
116
117     # uninstall previous version
118     pip uninstall -y yardstick || true
119
120     # Install yardstick
121     pip install .
122 }
123
124 install_storperf()
125 {
126     # Install Storper on huawei-pod1
127     if [ "$NODE_NAME" == "huawei-pod1" ]; then
128         echo
129         echo "========== Installing storperf =========="
130
131         if ! yardstick plugin install plugin/CI/storperf.yaml; then
132             echo "Install storperf plugin FAILED";
133             exit 1
134         fi
135     fi
136 }
137
138 build_yardstick_image()
139 {
140     echo
141     echo "========== Build yardstick cloud image =========="
142
143     local cmd="sudo $(which yardstick-img-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
144
145     # Build the image. Retry once if the build fails.
146     $cmd || $cmd
147
148     if [ ! -f $QCOW_IMAGE ]; then
149         echo "Failed building QCOW image"
150         exit 1
151     fi
152 }
153
154 create_nova_flavor()
155 {
156     if ! nova flavor-list | grep -q yardstick-flavor; then
157         echo
158         echo "========== Create nova flavor =========="
159         # Create the nova flavor used by some sample test cases
160         nova flavor-create yardstick-flavor 100 512 3 1
161         # DPDK-enabled OVS requires guest memory to be backed by large pages
162         if [[ "$DEPLOY_SCENARIO" == *"-ovs-"* ]]; then
163             nova flavor-key yardstick-flavor set hw:mem_page_size=large
164         fi
165     fi
166 }
167
168 load_cirros_image()
169 {
170     echo
171     echo "========== Loading cirros cloud image =========="
172
173     local image_file=/home/opnfv/images/cirros-0.3.3-x86_64-disk.img
174
175     output=$(glance image-create \
176         --name  cirros-0.3.3 \
177         --disk-format qcow2 \
178         --container-format bare \
179         --file $image_file)
180     echo "$output"
181
182     CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
183     if [ -z "$CIRROS_IMAGE_ID" ]; then
184         echo 'Failed uploading cirros image to cloud'.
185         exit 1
186     fi
187
188     echo "Cirros image id: $CIRROS_IMAGE_ID"
189 }
190
191 load_ubuntu_image()
192 {
193     echo
194     echo "========== Loading ubuntu cloud image =========="
195
196     local ubuntu_image_file=/home/opnfv/images/trusty-server-cloudimg-amd64-disk1.img
197
198     output=$(glance image-create \
199         --name Ubuntu-14.04 \
200         --disk-format qcow2 \
201         --container-format bare \
202         --file $ubuntu_image_file)
203     echo "$output"
204
205     UBUNTU_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
206
207     if [ -z "$UBUNTU_IMAGE_ID" ]; then
208         echo 'Failed uploading UBUNTU image to cloud'.
209         exit 1
210     fi
211
212     echo "Ubuntu image id: $UBUNTU_IMAGE_ID"
213 }
214
215 load_yardstick_image()
216 {
217     echo
218     echo "========== Loading yardstick cloud image =========="
219
220     output=$(glance --os-image-api-version 1 image-create \
221         --name yardstick-trusty-server \
222         --is-public true --disk-format qcow2 \
223         --container-format bare \
224         --file $QCOW_IMAGE)
225     echo "$output"
226
227     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
228
229     if [ -z "$GLANCE_IMAGE_ID" ]; then
230         echo 'Failed uploading image to cloud'.
231         exit 1
232     fi
233
234     sudo rm -f $QCOW_IMAGE
235
236     echo "Glance image id: $GLANCE_IMAGE_ID"
237 }
238
239 run_test()
240 {
241     echo
242     echo "========== Running yardstick test suites =========="
243
244     mkdir -p /etc/yardstick
245
246     cat << EOF >> /etc/yardstick/yardstick.conf
247 [DEFAULT]
248 debug = True
249 dispatcher = ${DISPATCHER_TYPE}
250
251 [dispatcher_file]
252 file_name = ${DISPATCHER_FILE_NAME}
253
254 [dispatcher_http]
255 timeout = 5
256 target = ${DISPATCHER_HTTP_TARGET}
257
258 [dispatcher_influxdb]
259 timeout = 5
260 target = ${DISPATCHER_INFLUXDB_TARGET}
261 db_name = yardstick
262 username = opnfv
263 password = 0pnfv2015
264 EOF
265
266     local failed=0
267
268     if [ ${#SUITE_FILES[@]} -gt 0 ]; then
269
270         for suite in ${SUITE_FILES[*]}; do
271
272             echo "---------------------------"
273             echo "Running test suite: $suite"
274             echo "---------------------------"
275
276             if ! yardstick task start --suite $suite; then
277                  echo "test suite $suite FAILED";
278
279                 # Mark the test suite failed but continue
280                 # running the remaining test suites.
281                 (( failed++ ))
282             fi
283             if [ ${DISPATCHER_TYPE} = file ]; then
284                 echo "---------------------------"
285                 echo "Dump test suite $suite result"
286                 echo "---------------------------"
287                 if [ -f ${DISPATCHER_FILE_NAME} ]; then
288                     cat ${DISPATCHER_FILE_NAME}
289                 else
290                     echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
291                 fi
292             fi
293
294         done
295
296         local sceanrio_status="SUCCESS"
297
298         if [ $failed -gt 0 ]; then
299             scenario_status="FAILED"
300         fi
301         curl -i -H 'content-type: application/json' -X POST -d \
302             "{\"project_name\": \"yardstick\",
303               \"pod_name\":\"${NODE_NAME}\",
304               \"installer\":\"${INSTALLER_TYPE}\",
305               \"description\": \"yardstick ci scenario status\",
306               \"case_name\": \"scenario_status\",
307               \"version\":\"${YARDSTICK_BRANCH}\",
308               \"scenario\":\"${DEPLOY_SCENARIO}\",
309               \"details\":\"${sceanrio_status}\"}" \
310               ${DISPATCHER_HTTP_TARGET}
311
312         if [ $failed -gt 0 ]; then
313             echo "---------------------------"
314             echo "$failed out of ${SUITE_FILES[*]} test suites FAILED"
315             echo "---------------------------"
316             exit 1
317         fi
318
319     else
320
321         echo "---------------------------"
322         echo "Running samples/ping.yaml  "
323         echo "---------------------------"
324
325         if ! yardstick task start samples/ping.yaml; then
326             echo "Yardstick test FAILED"
327             exit 1
328         fi
329
330         if [ ${DISPATCHER_TYPE} = file ]; then
331             echo "---------------------------"
332             echo "Dump samples/ping.yaml test result"
333             echo "---------------------------"
334             if [ -f ${DISPATCHER_FILE_NAME} ]; then
335                 cat ${DISPATCHER_FILE_NAME}
336             else
337                 echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
338             fi
339         fi
340
341     fi
342
343 }
344
345 main()
346 {
347     GITROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
348
349     cd $GITROOT
350
351     export YARDSTICK_VERSION=$(git rev-parse HEAD)
352
353     SUITE_FILES=()
354
355     # find the test suite files
356     for suite in $TEST_SUITES; do
357         if [ -f $suite ]; then
358             SUITE_FILES+=($suite)
359         else
360             tsdir=$GITROOT/tests/opnfv/test_suites
361             if [ ! -f $tsdir/$suite ]; then
362                 echo "Test suite \"$suite\" does not exist"
363                 exit 1
364             fi
365             SUITE_FILES+=($tsdir/$suite)
366         fi
367     done
368
369     echo
370     echo "========== Running Yardstick CI with following parameters =========="
371     echo "Script options: ${SCRIPT} $SCRIPT_ARGS"
372     echo "Dispatcher: ${DISPATCHER_TYPE} ${DISPATCHER_FILE_NAME}"
373     echo "YARDSTICK_VERSION: ${YARDSTICK_VERSION}"
374     echo "Number of test suites: ${#SUITE_FILES[@]}"
375     for suite in ${SUITE_FILES[*]}; do
376         echo "     $suite"
377     done
378     echo
379
380     # check if some necessary variables is set
381     if [ -z "$OS_AUTH_URL" ]; then
382         echo "OS_AUTH_URL is unset or empty"
383         exit 1
384     fi
385
386     echo "OS_AUTH_URL is $OS_AUTH_URL"
387     echo
388
389     # check OpenStack services
390     echo "Checking OpenStack services:"
391     for cmd in "glance image-list" "nova list" "heat stack-list"; do
392         echo "  checking ${cmd/%\ */} ..."
393         if ! $cmd >/dev/null; then
394             echo "error: command \"$cmd\" failed"
395             exit 1
396         fi
397     done
398
399     echo
400     echo "Checking for External network:"
401     for net in $(neutron net-list --router:external True -c name -f value); do
402         echo "  external network: $net"
403     done
404
405     # install yardstick
406     install_yardstick
407
408     cleanup
409
410     trap "error_exit" EXIT SIGTERM
411
412     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-trusty-server.img"
413
414     build_yardstick_image
415     load_yardstick_image
416     load_cirros_image
417     load_ubuntu_image
418     create_nova_flavor
419
420     install_storperf
421     run_test
422 }
423
424 main