Add more debug to image build
[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             ;;
58         i)
59             DISPATCHER_TYPE=influxdb
60             DISPATCHER_INFLUXDB_TARGET=http://${OPTARG}
61             ;;
62         *)
63             echo "${OPTION} is not a valid argument"
64             exit 1
65             ;;
66     esac
67 done
68
69 shift $[OPTIND - 1]
70 TEST_SUITES=$@
71
72 cleanup()
73 {
74     echo
75     echo "========== Cleanup =========="
76
77     if ! glance image-list; then
78         return
79     fi
80
81     for image in $(glance image-list | grep -e cirros-0.3.3 -e yardstick-trusty-server | awk '{print $2}'); do
82         echo "Deleting image $image..."
83         glance image-delete $image || true
84     done
85
86     nova flavor-delete yardstick-flavor &> /dev/null || true
87 }
88
89 exitcode=""
90
91 error_exit()
92 {
93     local rc=$?
94
95     if [ -z "$exitcode" ]; then
96         # In case of recursive traps (!?)
97         exitcode=$rc
98     fi
99
100     cleanup
101
102     echo "Exiting with RC=$exitcode"
103
104     exit $exitcode
105 }
106
107 set -o errexit
108 set -o pipefail
109
110 install_yardstick()
111 {
112     echo
113     echo "========== Installing yardstick =========="
114
115     if ! sudo python setup.py install; then
116         echo 'Yardstick installation failed!'
117         exit 1
118     fi
119 }
120
121 build_yardstick_image()
122 {
123     echo
124     echo "========== Build yardstick cloud image =========="
125
126     local cmd="sudo $(which yardstick-img-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
127
128     # Build the image. Retry once if the build fails.
129     $cmd || $cmd
130
131     if [ ! -f $QCOW_IMAGE ]; then
132         echo "Failed building QCOW image"
133         exit 1
134     fi
135 }
136
137 create_nova_flavor()
138 {
139     if ! nova flavor-list | grep -q yardstick-flavor; then
140         echo
141         echo "========== Create nova flavor =========="
142         # Create the nova flavor used by some sample test cases
143         nova flavor-create yardstick-flavor 100 512 3 1
144     fi
145 }
146
147 load_cirros_image()
148 {
149     echo
150     echo "========== Loading cirros cloud image =========="
151
152     local image_file=/home/opnfv/images/cirros-0.3.3-x86_64-disk.img
153
154     output=$(glance image-create \
155         --name  cirros-0.3.3 \
156         --disk-format qcow2 \
157         --container-format bare \
158         --file $image_file)
159     echo "$output"
160
161     CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
162     if [ -z "$CIRROS_IMAGE_ID" ]; then
163         echo 'Failed uploading cirros image to cloud'.
164         exit 1
165     fi
166
167     echo "Cirros image id: $CIRROS_IMAGE_ID"
168 }
169
170 load_yardstick_image()
171 {
172     echo
173     echo "========== Loading yardstick cloud image =========="
174
175     output=$(glance --os-image-api-version 1 image-create \
176         --name yardstick-trusty-server \
177         --is-public true --disk-format qcow2 \
178         --container-format bare \
179         --file $QCOW_IMAGE)
180     echo "$output"
181
182     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
183
184     if [ -z "$GLANCE_IMAGE_ID" ]; then
185         echo 'Failed uploading image to cloud'.
186         exit 1
187     fi
188
189     sudo rm -f $QCOW_IMAGE
190
191     echo "Glance image id: $GLANCE_IMAGE_ID"
192 }
193
194 run_test()
195 {
196     echo
197     echo "========== Running yardstick test suites =========="
198
199     mkdir -p /etc/yardstick
200
201     cat << EOF >> /etc/yardstick/yardstick.conf
202 [DEFAULT]
203 debug = True
204 dispatcher = ${DISPATCHER_TYPE}
205
206 [dispatcher_file]
207 file_name = ${DISPATCHER_FILE_NAME}
208
209 [dispatcher_http]
210 timeout = 5
211 target = ${DISPATCHER_HTTP_TARGET}
212
213 [dispatcher_influxdb]
214 timeout = 5
215 target = ${DISPATCHER_INFLUXDB_TARGET}
216 db_name = yardstick
217 username = opnfv
218 password = 0pnfv2015
219 EOF
220
221     local failed=0
222
223     if [ ${#SUITE_FILES[@]} -gt 0 ]; then
224
225         for suite in ${SUITE_FILES[*]}; do
226
227             echo "---------------------------"
228             echo "Running test suite: $suite"
229             echo "---------------------------"
230
231             if ! yardstick task start --suite $suite; then
232                  echo "test suite $suite FAILED";
233
234                 # Mark the test suite failed but continue
235                 # running the remaining test suites.
236                 (( failed++ ))
237             fi
238             if [ ${DISPATCHER_TYPE} = file ]; then
239                 echo "---------------------------"
240                 echo "Dump test suite $suite result"
241                 echo "---------------------------"
242                 if [ -f ${DISPATCHER_FILE_NAME} ]; then
243                     cat ${DISPATCHER_FILE_NAME}
244                 else
245                     echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
246                 fi
247             fi
248
249         done
250
251         if [ $failed -gt 0 ]; then
252
253             echo "---------------------------"
254             echo "$failed out of ${SUITE_FILES[*]} test suites FAILED"
255             echo "---------------------------"
256             exit 1
257          fi
258
259     else
260
261         echo "---------------------------"
262         echo "Running samples/ping.yaml  "
263         echo "---------------------------"
264
265         if ! yardstick task start samples/ping.yaml; then
266             echo "Yardstick test FAILED"
267             exit 1
268         fi
269
270         if [ ${DISPATCHER_TYPE} = file ]; then
271             echo "---------------------------"
272             echo "Dump samples/ping.yaml test result"
273             echo "---------------------------"
274             if [ -f ${DISPATCHER_FILE_NAME} ]; then
275                 cat ${DISPATCHER_FILE_NAME}
276             else
277                 echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
278             fi
279         fi
280
281     fi
282
283 }
284
285 main()
286 {
287     GITROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
288
289     cd $GITROOT
290
291     export YARDSTICK_VERSION=$(git rev-parse HEAD)
292
293     SUITE_FILES=()
294
295     # find the test suite files
296     for suite in $TEST_SUITES; do
297         if [ -f $suite ]; then
298             SUITE_FILES+=($suite)
299         else
300             tsdir=$GITROOT/tests/opnfv/test_suites
301             if [ ! -f $tsdir/$suite ]; then
302                 echo "Test suite \"$suite\" does not exist"
303                 exit 1
304             fi
305             SUITE_FILES+=($tsdir/$suite)
306         fi
307     done
308
309     echo
310     echo "========== Running Yardstick CI with following parameters =========="
311     echo "Script options: ${SCRIPT} $SCRIPT_ARGS"
312     echo "Result API: ${DISPATCHER_HTTP_TARGET:-$DISPATCHER_FILE_NAME}"
313     echo "YARDSTICK_VERSION: ${YARDSTICK_VERSION}"
314     echo "Number of test suites: ${#SUITE_FILES[@]}"
315     for suite in ${SUITE_FILES[*]}; do
316         echo "     $suite"
317     done
318
319     # install yardstick
320     install_yardstick
321
322     # check if some necessary variables is set
323     if [ -z "$OS_AUTH_URL" ]; then
324         echo "OS_AUTH_URL is unset or empty"
325         exit 1
326     fi
327
328     # check if the api is up
329     echo "Checking if OS API is working..."
330     if ! glance image-list > /dev/null; then
331         echo "OS API is down"
332         exit 1
333     fi
334
335     cleanup
336
337     trap "error_exit" EXIT SIGTERM
338
339     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-trusty-server.img"
340
341     build_yardstick_image
342     load_yardstick_image
343     load_cirros_image
344     create_nova_flavor
345
346     run_test
347 }
348
349 main