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