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