Docker container for Yardstick CI part 2
[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      IP address for Result API.
36            Default is to store the results to file ($DISPATCHER_FILE_NAME)
37            if this option is not present.
38
39 EOF
40 }
41
42 DISPATCHER_TYPE=file
43 DISPATCHER_FILE_NAME="/tmp/yardstick.out"
44 DISPATCHER_HTTP_TARGET=
45
46 while getopts "r:h" OPTION; do
47     case $OPTION in
48         h)
49             usage
50             exit 0
51             ;;
52         r)
53             DISPATCHER_TYPE=http
54             DISPATCHER_HTTP_TARGET=http://${OPTARG}/results
55             ;;
56         *)
57             echo "${OPTION} is not a valid argument"
58             exit 1
59             ;;
60     esac
61 done
62
63 shift $[OPTIND - 1]
64 TEST_SUITES=$@
65
66 cleanup()
67 {
68     echo
69     echo "========== Cleanup =========="
70
71     if ! glance image-list; then
72         return
73     fi
74
75     for image in $(glance image-list | grep -e cirros-0.3.3 -e yardstick-trusty-server | awk '{print $2}'); do
76         echo "Deleting image $image..."
77         glance image-delete $image || true
78     done
79 }
80
81 exitcode=""
82
83 error_exit()
84 {
85     local rc=$?
86
87     if [ -z "$exitcode" ]; then
88         # In case of recursive traps (!?)
89         exitcode=$rc
90     fi
91
92     cleanup
93
94     echo "Exiting with RC=$exitcode"
95
96     exit $exitcode
97 }
98
99 set -o errexit
100 set -o pipefail
101
102 install_yardstick()
103 {
104     echo
105     echo "========== Installing yardstick =========="
106
107     if ! sudo python setup.py install; then
108         echo 'Yardstick installation failed!'
109         exit 1
110     fi
111 }
112
113 build_yardstick_image()
114 {
115     echo
116     echo "========== Build yardstick cloud image =========="
117
118     sudo $(which yardstick-img-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh
119     if [ ! -f $QCOW_IMAGE ]; then
120         echo "Failed building QCOW image"
121         exit 1
122     fi
123 }
124
125 load_cirros_image()
126 {
127     echo
128     echo "========== Loading cirros cloud image =========="
129
130     local image_file=
131
132     wget http://download.cirros-cloud.net/0.3.3/cirros-0.3.3-x86_64-disk.img -O /tmp/cirros.img
133
134     output=$(glance image-create \
135         --name  cirros-0.3.3 \
136         --disk-format qcow2 \
137         --container-format bare \
138         --file /tmp/cirros.img)
139     echo "$output"
140
141     CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
142     if [ -z "$CIRROS_IMAGE_ID" ]; then
143         echo 'Failed uploading cirros image to cloud'.
144         exit 1
145     fi
146
147     echo "Cirros image id: $CIRROS_IMAGE_ID"
148 }
149
150 load_yardstick_image()
151 {
152     echo
153     echo "========== Loading yardstick cloud image =========="
154
155     output=$(glance --os-image-api-version 1 image-create \
156         --name yardstick-trusty-server \
157         --is-public true --disk-format qcow2 \
158         --container-format bare \
159         --file $QCOW_IMAGE)
160     echo "$output"
161
162     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
163
164     if [ -z "$GLANCE_IMAGE_ID" ]; then
165         echo 'Failed uploading image to cloud'.
166         exit 1
167     fi
168
169     sudo rm -f $QCOW_IMAGE
170
171     echo "Glance image id: $GLANCE_IMAGE_ID"
172 }
173
174 run_test()
175 {
176     echo
177     echo "========== Running yardstick test suites =========="
178
179     mkdir -p /etc/yardstick
180
181     cat << EOF >> /etc/yardstick/yardstick.conf
182 [DEFAULT]
183 debug = True
184 dispatcher = ${DISPATCHER_TYPE}
185
186 [dispatcher_file]
187 file_name = ${DISPATCHER_FILE_NAME}
188
189 [dispatcher_http]
190 timeout = 5
191 target = ${DISPATCHER_HTTP_TARGET}
192 EOF
193
194     local failed=0
195
196     if [ ${#SUITE_FILES[@]} -gt 0 ]; then
197
198         for suite in ${SUITE_FILES[*]}; do
199
200             echo "---------------------------"
201             echo "Running test suite: $suite"
202             echo "---------------------------"
203
204              if ! yardstick task start --suite $suite; then
205                  echo "test suite $suite FAILED";
206
207                  # Mark the test suite failed but continue
208                  # running the remaining test suites.
209                  (( failed++ ))
210              fi
211
212          done
213
214          if [ $failed -gt 0 ]; then
215
216              echo "---------------------------"
217              echo "$failed out of ${SUITE_FILES[*]} test suites FAILED"
218              echo "---------------------------"
219              exit 1
220          fi
221
222     else
223
224         echo "---------------------------"
225         echo "Running samples/ping.yaml  "
226         echo "---------------------------"
227
228         if ! yardstick task start samples/ping.yaml; then
229             echo "Yardstick test FAILED"
230             exit 1
231         fi
232
233     fi
234
235 }
236
237 main()
238 {
239     GITROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
240
241     cd $GITROOT
242
243     export YARDSTICK_VERSION=$(git rev-parse HEAD)
244
245     SUITE_FILES=()
246
247     # find the test suite files
248     for suite in $TEST_SUITES; do
249         if [ -f $suite ]; then
250             SUITE_FILES+=($suite)
251         else
252             tsdir=$GITROOT/tests/opnfv/test_suites
253             if [ ! -f $tsdir/$suite ]; then
254                 echo "Test suite \"$suite\" does not exist"
255                 exit 1
256             fi
257             SUITE_FILES+=($tsdir/$suite)
258         fi
259     done
260
261     echo
262     echo "========== Running Yardstick CI with following parameters =========="
263     echo "Script options: ${SCRIPT} $SCRIPT_ARGS"
264     echo "Result API: ${DISPATCHER_HTTP_TARGET:-$DISPATCHER_FILE_NAME}"
265     echo "YARDSTICK_VERSION: ${YARDSTICK_VERSION}"
266     echo "Number of test suites: ${#SUITE_FILES[@]}"
267     for suite in ${SUITE_FILES[*]}; do
268         echo "     $suite"
269     done
270
271     # install yardstick
272     install_yardstick
273
274     # check if some necessary variables is set
275     if [ -z "$OS_AUTH_URL" ]; then
276         echo "OS_AUTH_URL is unset or empty"
277         exit 1
278     fi
279
280     # check if the api is up
281     echo "Checking if OS API is working..."
282     if ! glance image-list > /dev/null; then
283         echo "OS API is down"
284         exit 1
285     fi
286
287     cleanup
288
289     trap "error_exit" EXIT SIGTERM
290
291     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-trusty-server.img"
292
293     build_yardstick_image
294     load_yardstick_image
295     load_cirros_image
296
297     run_test
298 }
299
300 main