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