8f67b9ffb02e894f15da6967803897d7eedaeff1
[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="http://testresults.opnfv.org/test/api/v1/results"
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 -e Ubuntu-14.04 | 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 install_storperf()
125 {
126     # Install Storper on huawei-pod1
127     if [ "$NODE_NAME" == "huawei-pod1" ]; then
128         echo
129         echo "========== Installing storperf =========="
130
131         if ! yardstick -d plugin install plugin/CI/storperf.yaml; then
132             echo "Install storperf plugin FAILED";
133             exit 1
134         fi
135
136         echo
137         echo "========== Installed storperf container =========="
138     fi
139 }
140
141 build_yardstick_image()
142 {
143     echo
144     echo "========== Build yardstick cloud image =========="
145
146     local cmd="sudo $(which yardstick-img-modify) $(pwd)/tools/ubuntu-server-cloudimg-modify.sh"
147
148     # Build the image. Retry once if the build fails.
149     $cmd || $cmd
150
151     if [ ! -f $QCOW_IMAGE ]; then
152         echo "Failed building QCOW image"
153         exit 1
154     fi
155 }
156
157 create_nova_flavor()
158 {
159     if ! nova flavor-list | grep -q yardstick-flavor; then
160         echo
161         echo "========== Create nova flavor =========="
162         # Create the nova flavor used by some sample test cases
163         nova flavor-create yardstick-flavor 100 512 3 1
164         # DPDK-enabled OVS requires guest memory to be backed by large pages
165         if [[ "$DEPLOY_SCENARIO" == *"-ovs-"* ]]; then
166             nova flavor-key yardstick-flavor set hw:mem_page_size=large
167         fi
168     fi
169 }
170
171 load_cirros_image()
172 {
173     echo
174     echo "========== Loading cirros cloud image =========="
175
176     local image_file=/home/opnfv/images/cirros-0.3.3-x86_64-disk.img
177
178     output=$(glance image-create \
179         --name  cirros-0.3.3 \
180         --disk-format qcow2 \
181         --container-format bare \
182         --file $image_file)
183     echo "$output"
184
185     CIRROS_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
186     if [ -z "$CIRROS_IMAGE_ID" ]; then
187         echo 'Failed uploading cirros image to cloud'.
188         exit 1
189     fi
190
191     echo "Cirros image id: $CIRROS_IMAGE_ID"
192 }
193
194 load_ubuntu_image()
195 {
196     echo
197     echo "========== Loading ubuntu cloud image =========="
198
199     local ubuntu_image_file=/home/opnfv/images/trusty-server-cloudimg-amd64-disk1.img
200
201     output=$(glance image-create \
202         --name Ubuntu-14.04 \
203         --disk-format qcow2 \
204         --container-format bare \
205         --file $ubuntu_image_file)
206     echo "$output"
207
208     UBUNTU_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
209
210     if [ -z "$UBUNTU_IMAGE_ID" ]; then
211         echo 'Failed uploading UBUNTU image to cloud'.
212         exit 1
213     fi
214
215     echo "Ubuntu image id: $UBUNTU_IMAGE_ID"
216 }
217
218 load_yardstick_image()
219 {
220     echo
221     echo "========== Loading yardstick cloud image =========="
222
223     output=$(glance --os-image-api-version 1 image-create \
224         --name yardstick-trusty-server \
225         --is-public true --disk-format qcow2 \
226         --container-format bare \
227         --file $QCOW_IMAGE)
228     echo "$output"
229
230     GLANCE_IMAGE_ID=$(echo "$output" | grep " id " | awk '{print $(NF-1)}')
231
232     if [ -z "$GLANCE_IMAGE_ID" ]; then
233         echo 'Failed uploading image to cloud'.
234         exit 1
235     fi
236
237     sudo rm -f $QCOW_IMAGE
238
239     echo "Glance image id: $GLANCE_IMAGE_ID"
240 }
241
242 run_test()
243 {
244     echo
245     echo "========== Running yardstick test suites =========="
246
247     mkdir -p /etc/yardstick
248
249     cat << EOF >> /etc/yardstick/yardstick.conf
250 [DEFAULT]
251 debug = True
252 dispatcher = ${DISPATCHER_TYPE}
253
254 [dispatcher_file]
255 file_name = ${DISPATCHER_FILE_NAME}
256
257 [dispatcher_http]
258 timeout = 5
259 target = ${DISPATCHER_HTTP_TARGET}
260
261 [dispatcher_influxdb]
262 timeout = 5
263 target = ${DISPATCHER_INFLUXDB_TARGET}
264 db_name = yardstick
265 username = opnfv
266 password = 0pnfv2015
267 EOF
268
269     local failed=0
270     local start_date
271     local stop_date
272
273     if [ ${#SUITE_FILES[@]} -gt 0 ]; then
274
275         start_date=$(date '+%Y-%m-%d %H:%M:%S')
276         for suite in ${SUITE_FILES[*]}; do
277
278             echo "---------------------------"
279             echo "Running test suite: $suite"
280             echo "---------------------------"
281             if ! yardstick task start --suite $suite; then
282                  echo "test suite $suite FAILED";
283
284                 # Mark the test suite failed but continue
285                 # running the remaining test suites.
286                 (( failed++ ))
287             fi
288             if [ ${DISPATCHER_TYPE} = file ]; then
289                 echo "---------------------------"
290                 echo "Dump test suite $suite result"
291                 echo "---------------------------"
292                 if [ -f ${DISPATCHER_FILE_NAME} ]; then
293                     cat ${DISPATCHER_FILE_NAME}
294                 else
295                     echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
296                 fi
297             fi
298         done
299         stop_date=$(date '+%Y-%m-%d %H:%M:%S')
300
301
302
303         local sceanrio_status="SUCCESS"
304
305         if [ $failed -gt 0 ]; then
306             scenario_status="FAILED"
307         fi
308         curl -i -H 'content-type: application/json' -X POST -d \
309             "{\"project_name\": \"yardstick\",
310               \"case_name\": \"scenario_status\",
311               \"pod_name\":\"${NODE_NAME}\",
312               \"installer\":\"${INSTALLER_TYPE}\",
313               \"version\":\"${YARDSTICK_BRANCH}\",
314               \"scenario\":\"${DEPLOY_SCENARIO}\",
315               \"description\": \"yardstick ci scenario status\",
316               \"start_date\":\"${start_date}\",
317               \"stop_date\":\"${stop_date}\",
318               \"details\":\"${sceanrio_status}\"}" \
319               ${DISPATCHER_HTTP_TARGET}
320
321         if [ $failed -gt 0 ]; then
322             echo "---------------------------"
323             echo "$failed out of ${SUITE_FILES[*]} test suites FAILED"
324             echo "---------------------------"
325             exit 1
326         fi
327
328     else
329
330         echo "---------------------------"
331         echo "Running samples/ping.yaml  "
332         echo "---------------------------"
333
334         if ! yardstick task start samples/ping.yaml; then
335             echo "Yardstick test FAILED"
336             exit 1
337         fi
338
339         if [ ${DISPATCHER_TYPE} = file ]; then
340             echo "---------------------------"
341             echo "Dump samples/ping.yaml test result"
342             echo "---------------------------"
343             if [ -f ${DISPATCHER_FILE_NAME} ]; then
344                 cat ${DISPATCHER_FILE_NAME}
345             else
346                 echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
347             fi
348         fi
349
350     fi
351
352 }
353
354 main()
355 {
356     GITROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
357
358     cd $GITROOT
359
360     export YARDSTICK_VERSION=$(git rev-parse HEAD)
361
362     SUITE_FILES=()
363
364     # find the test suite files
365     for suite in $TEST_SUITES; do
366         if [ -f $suite ]; then
367             SUITE_FILES+=($suite)
368         else
369             tsdir=$GITROOT/tests/opnfv/test_suites
370             if [ ! -f $tsdir/$suite ]; then
371                 echo "Test suite \"$suite\" does not exist"
372                 exit 1
373             fi
374             SUITE_FILES+=($tsdir/$suite)
375         fi
376     done
377
378     echo
379     echo "========== Running Yardstick CI with following parameters =========="
380     echo "Script options: ${SCRIPT} $SCRIPT_ARGS"
381     echo "Dispatcher: ${DISPATCHER_TYPE} ${DISPATCHER_FILE_NAME}"
382     echo "YARDSTICK_VERSION: ${YARDSTICK_VERSION}"
383     echo "Number of test suites: ${#SUITE_FILES[@]}"
384     for suite in ${SUITE_FILES[*]}; do
385         echo "     $suite"
386     done
387     echo
388
389     # check if some necessary variables is set
390     if [ -z "$OS_AUTH_URL" ]; then
391         echo "OS_AUTH_URL is unset or empty"
392         exit 1
393     fi
394
395     echo "OS_AUTH_URL is $OS_AUTH_URL"
396     echo
397
398     # check OpenStack services
399     echo "Checking OpenStack services:"
400     for cmd in "glance image-list" "nova list" "heat stack-list"; do
401         echo "  checking ${cmd/%\ */} ..."
402         if ! $cmd >/dev/null; then
403             echo "error: command \"$cmd\" failed"
404             exit 1
405         fi
406     done
407
408     echo
409     echo "Checking for External network:"
410     for net in $(neutron net-list --router:external True -c name -f value); do
411         echo "  external network: $net"
412     done
413
414     # install yardstick
415     install_yardstick
416
417     cleanup
418
419     trap "error_exit" EXIT SIGTERM
420
421     QCOW_IMAGE="/tmp/workspace/yardstick/yardstick-trusty-server.img"
422
423     build_yardstick_image
424     load_yardstick_image
425     load_cirros_image
426     load_ubuntu_image
427     create_nova_flavor
428
429     install_storperf
430     run_test
431 }
432
433 main