Release results overview
[yardstick.git] / 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=
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 | 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 build_yardstick_image()
125 {
126     echo
127     echo "========== Build yardstick cloud image =========="
128
129     local cmd="sudo $(which yardstick-img-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
130
131     # Build the image. Retry once if the build fails.
132     $cmd || $cmd
133
134     if [ ! -f $QCOW_IMAGE ]; then
135         echo "Failed building QCOW image"
136         exit 1
137     fi
138 }
139
140 create_nova_flavor()
141 {
142     if ! nova flavor-list | grep -q yardstick-flavor; then
143         echo
144         echo "========== Create nova flavor =========="
145         # Create the nova flavor used by some sample test cases
146         nova flavor-create yardstick-flavor 100 512 3 1
147     fi
148 }
149
150 load_cirros_image()
151 {
152     echo
153     echo "========== Loading cirros cloud image =========="
154
155     local image_file=/home/opnfv/images/cirros-0.3.3-x86_64-disk.img
156
157     output=$(glance image-create \
158         --name  cirros-0.3.3 \
159         --disk-format qcow2 \
160         --container-format bare \
161         --file $image_file)
162     echo "$output"
163
164     CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
165     if [ -z "$CIRROS_IMAGE_ID" ]; then
166         echo 'Failed uploading cirros image to cloud'.
167         exit 1
168     fi
169
170     echo "Cirros image id: $CIRROS_IMAGE_ID"
171 }
172
173 load_yardstick_image()
174 {
175     echo
176     echo "========== Loading yardstick cloud image =========="
177
178     output=$(glance --os-image-api-version 1 image-create \
179         --name yardstick-trusty-server \
180         --is-public true --disk-format qcow2 \
181         --container-format bare \
182         --file $QCOW_IMAGE)
183     echo "$output"
184
185     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
186
187     if [ -z "$GLANCE_IMAGE_ID" ]; then
188         echo 'Failed uploading image to cloud'.
189         exit 1
190     fi
191
192     sudo rm -f $QCOW_IMAGE
193
194     echo "Glance image id: $GLANCE_IMAGE_ID"
195 }
196
197 run_test()
198 {
199     echo
200     echo "========== Running yardstick test suites =========="
201
202     mkdir -p /etc/yardstick
203
204     cat << EOF >> /etc/yardstick/yardstick.conf
205 [DEFAULT]
206 debug = True
207 dispatcher = ${DISPATCHER_TYPE}
208
209 [dispatcher_file]
210 file_name = ${DISPATCHER_FILE_NAME}
211
212 [dispatcher_http]
213 timeout = 5
214 target = ${DISPATCHER_HTTP_TARGET}
215
216 [dispatcher_influxdb]
217 timeout = 5
218 target = ${DISPATCHER_INFLUXDB_TARGET}
219 db_name = yardstick
220 username = opnfv
221 password = 0pnfv2015
222 EOF
223
224     local failed=0
225
226     if [ ${#SUITE_FILES[@]} -gt 0 ]; then
227
228         for suite in ${SUITE_FILES[*]}; do
229
230             echo "---------------------------"
231             echo "Running test suite: $suite"
232             echo "---------------------------"
233
234             if ! yardstick task start --suite $suite; then
235                  echo "test suite $suite FAILED";
236
237                 # Mark the test suite failed but continue
238                 # running the remaining test suites.
239                 (( failed++ ))
240             fi
241             if [ ${DISPATCHER_TYPE} = file ]; then
242                 echo "---------------------------"
243                 echo "Dump test suite $suite result"
244                 echo "---------------------------"
245                 if [ -f ${DISPATCHER_FILE_NAME} ]; then
246                     cat ${DISPATCHER_FILE_NAME}
247                 else
248                     echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
249                 fi
250             fi
251
252         done
253
254         if [ $failed -gt 0 ]; then
255
256             echo "---------------------------"
257             echo "$failed out of ${SUITE_FILES[*]} test suites FAILED"
258             echo "---------------------------"
259             exit 1
260          fi
261
262     else
263
264         echo "---------------------------"
265         echo "Running samples/ping.yaml  "
266         echo "---------------------------"
267
268         if ! yardstick task start samples/ping.yaml; then
269             echo "Yardstick test FAILED"
270             exit 1
271         fi
272
273         if [ ${DISPATCHER_TYPE} = file ]; then
274             echo "---------------------------"
275             echo "Dump samples/ping.yaml test result"
276             echo "---------------------------"
277             if [ -f ${DISPATCHER_FILE_NAME} ]; then
278                 cat ${DISPATCHER_FILE_NAME}
279             else
280                 echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
281             fi
282         fi
283
284     fi
285
286 }
287
288 main()
289 {
290     GITROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
291
292     cd $GITROOT
293
294     export YARDSTICK_VERSION=$(git rev-parse HEAD)
295
296     SUITE_FILES=()
297
298     # find the test suite files
299     for suite in $TEST_SUITES; do
300         if [ -f $suite ]; then
301             SUITE_FILES+=($suite)
302         else
303             tsdir=$GITROOT/tests/opnfv/test_suites
304             if [ ! -f $tsdir/$suite ]; then
305                 echo "Test suite \"$suite\" does not exist"
306                 exit 1
307             fi
308             SUITE_FILES+=($tsdir/$suite)
309         fi
310     done
311
312     echo
313     echo "========== Running Yardstick CI with following parameters =========="
314     echo "Script options: ${SCRIPT} $SCRIPT_ARGS"
315     echo "Dispatcher: ${DISPATCHER_TYPE} ${DISPATCHER_FILE_NAME}"
316     echo "YARDSTICK_VERSION: ${YARDSTICK_VERSION}"
317     echo "Number of test suites: ${#SUITE_FILES[@]}"
318     for suite in ${SUITE_FILES[*]}; do
319         echo "     $suite"
320     done
321     echo
322
323     # check if some necessary variables is set
324     if [ -z "$OS_AUTH_URL" ]; then
325         echo "OS_AUTH_URL is unset or empty"
326         exit 1
327     fi
328
329     echo "OS_AUTH_URL is $OS_AUTH_URL"
330     echo
331
332     # check OpenStack services
333     echo "Checking OpenStack services:"
334     for cmd in "glance image-list" "nova list" "heat stack-list"; do
335         echo "  checking ${cmd/%\ */} ..."
336         if ! $cmd >/dev/null; then
337             echo "error: command \"$cmd\" failed"
338             exit 1
339         fi
340     done
341
342     echo
343     echo "Checking for External network:"
344     for net in $(neutron net-list --router:external -c name -f value); do
345         echo "  external network: $net"
346     done
347
348     # install yardstick
349     install_yardstick
350
351     cleanup
352
353     trap "error_exit" EXIT SIGTERM
354
355     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-trusty-server.img"
356
357     build_yardstick_image
358     load_yardstick_image
359     load_cirros_image
360     create_nova_flavor
361
362     run_test
363 }
364
365 main