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