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