Merge "move external_network auto-assign to Heat context"
[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    -m      Reporting target (example: -m 213.77.62.197/results)
38
39            Default target is dump to file ($DISPATCHER_FILE_NAME)
40
41 EOF
42 }
43
44 DISPATCHER_TYPE=file
45 DISPATCHER_FILE_NAME="/tmp/yardstick.out"
46 DISPATCHER_HTTP_TARGET="http://testresults.opnfv.org/test/api/v1/results"
47 DISPATCHER_INFLUXDB_TARGET=
48 REPORTING_TARGET="${DISPATCHER_HTTP_TARGET}"
49
50 while getopts "r:i:m:h" OPTION; do
51     case $OPTION in
52         h)
53             usage
54             exit 0
55             ;;
56         r)
57             DISPATCHER_TYPE=http
58             DISPATCHER_HTTP_TARGET=http://${OPTARG}
59             DISPATCHER_FILE_NAME=
60             ;;
61         i)
62             DISPATCHER_TYPE=influxdb
63             DISPATCHER_INFLUXDB_TARGET=http://${OPTARG}
64             DISPATCHER_FILE_NAME=
65             ;;
66         m)
67             REPORTING_TARGET=http://${OPTARG}
68             ;;
69         *)
70             echo "${OPTION} is not a valid argument"
71             exit 1
72             ;;
73     esac
74 done
75
76 shift $[OPTIND - 1]
77 TEST_SUITES=$@
78
79 exitcode=""
80
81 error_exit()
82 {
83     local rc=$?
84
85     if [ -z "$exitcode" ]; then
86         # In case of recursive traps (!?)
87         exitcode=$rc
88     fi
89
90     cleanup
91
92     echo "Exiting with RC=$exitcode"
93
94     exit $exitcode
95 }
96
97 set -o errexit
98 set -o pipefail
99
100 install_yardstick()
101 {
102     echo
103     echo "========== Installing yardstick =========="
104
105     # uninstall previous version
106     pip uninstall -y yardstick || true
107
108     # Install yardstick
109     pip install .
110 }
111
112 install_storperf()
113 {
114     # Install Storper on huawei-pod1
115     if [ "$NODE_NAME" == "huawei-pod1" ]; then
116         echo
117         echo "========== Installing storperf =========="
118
119         if ! yardstick -d plugin install plugin/CI/storperf.yaml; then
120             echo "Install storperf plugin FAILED";
121             exit 1
122         fi
123
124     fi
125 }
126
127 remove_storperf()
128 {
129     # remove Storper from huawei-pod1
130     if [ "$NODE_NAME" == "huawei-pod1" ]; then
131         echo
132         echo "========== Removing storperf =========="
133
134         if ! yardstick -d plugin remove plugin/CI/storperf.yaml; then
135             echo "Remove storperf plugin FAILED";
136             exit 1
137         fi
138
139     fi
140 }
141
142 report(){
143
144     echo
145     echo "========== Reporting Status =========="
146     curl -i -H 'content-type: application/json' -X POST -d \
147         "{\"project_name\": \"yardstick\",
148           \"case_name\": \"scenario_status\",
149           \"pod_name\":\"${NODE_NAME}\",
150           \"installer\":\"${INSTALLER_TYPE}\",
151           \"version\":\"$(basename ${YARDSTICK_BRANCH})\",
152           \"scenario\":\"${DEPLOY_SCENARIO}\",
153           \"description\": \"yardstick ci scenario status\",
154           \"criteria\":\"${1}\",
155           \"start_date\":\"${2}\",
156           \"stop_date\":\"${3}\",
157           \"details\":\"\"}" \
158           "${REPORTING_TARGET}"
159 }
160
161 run_test()
162 {
163     echo
164     echo "========== Running yardstick test suites =========="
165
166     mkdir -p /etc/yardstick
167
168     cat << EOF > /etc/yardstick/yardstick.conf
169 [DEFAULT]
170 debug = False
171 dispatcher = ${DISPATCHER_TYPE}
172
173 [dispatcher_file]
174 file_name = ${DISPATCHER_FILE_NAME}
175
176 [dispatcher_http]
177 timeout = 5
178 target = ${DISPATCHER_HTTP_TARGET}
179
180 [dispatcher_influxdb]
181 timeout = 5
182 target = ${DISPATCHER_INFLUXDB_TARGET}
183 db_name = yardstick
184 username = opnfv
185 password = 0pnfv2015
186 EOF
187
188     local failed=0
189     local start_date
190     local stop_date
191
192     if [ ${#SUITE_FILES[@]} -gt 0 ]; then
193
194         start_date=$(date '+%Y-%m-%d %H:%M:%S')
195         for suite in ${SUITE_FILES[*]}; do
196
197             echo "---------------------------"
198             echo "Running test suite: $suite"
199             echo "---------------------------"
200             if ! yardstick task start --suite $suite; then
201                  echo "test suite $suite FAILED";
202
203                 # Mark the test suite failed but continue
204                 # running the remaining test suites.
205                 (( ++failed ))
206             fi
207             if [ ${DISPATCHER_TYPE} = file ]; then
208                 echo "---------------------------"
209                 echo "Dump test suite $suite result"
210                 echo "---------------------------"
211                 if [ -f ${DISPATCHER_FILE_NAME} ]; then
212                     cat ${DISPATCHER_FILE_NAME}
213                 else
214                     echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
215                 fi
216             fi
217         done
218         stop_date=$(date '+%Y-%m-%d %H:%M:%S')
219
220
221
222         local scenario_status="SUCCESS"
223
224         if [ $failed -gt 0 ]; then
225             scenario_status="FAILED"
226         fi
227
228         report "${scenario_status}" "${start_date}" "${stop_date}"
229
230         if [ $failed -gt 0 ]; then
231             echo "---------------------------"
232             echo "$failed out of ${SUITE_FILES[*]} test suites FAILED"
233             echo "---------------------------"
234             exit 1
235         fi
236
237     else
238
239         echo "---------------------------"
240         echo "Running samples/ping.yaml  "
241         echo "---------------------------"
242
243         if ! yardstick task start samples/ping.yaml; then
244             echo "Yardstick test FAILED"
245             exit 1
246         fi
247
248         if [ ${DISPATCHER_TYPE} = file ]; then
249             echo "---------------------------"
250             echo "Dump samples/ping.yaml test result"
251             echo "---------------------------"
252             if [ -f ${DISPATCHER_FILE_NAME} ]; then
253                 cat ${DISPATCHER_FILE_NAME}
254             else
255                 echo "Test result file ${DISPATCHER_FILE_NAME} is not exist"
256             fi
257         fi
258
259     fi
260
261 }
262
263 main()
264 {
265     GITROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
266
267     cd $GITROOT
268
269     export YARDSTICK_VERSION=$(git rev-parse HEAD)
270
271     SUITE_FILES=()
272
273     # find the test suite files
274     for suite in $TEST_SUITES; do
275         if [ -f $suite ]; then
276             SUITE_FILES+=($suite)
277         else
278             tsdir=$GITROOT/tests/opnfv/test_suites
279             if [ ! -f $tsdir/$suite ]; then
280                 echo "Test suite \"$suite\" does not exist"
281                 exit 1
282             fi
283             SUITE_FILES+=($tsdir/$suite)
284         fi
285     done
286
287     echo
288     echo "========== Running Yardstick CI with following parameters =========="
289     echo "Script options: ${SCRIPT} $SCRIPT_ARGS"
290     echo "Dispatcher: ${DISPATCHER_TYPE} ${DISPATCHER_FILE_NAME}"
291     echo "YARDSTICK_VERSION: ${YARDSTICK_VERSION}"
292     echo "Number of test suites: ${#SUITE_FILES[@]}"
293     for suite in ${SUITE_FILES[*]}; do
294         echo "     $suite"
295     done
296     echo
297
298     # check if some necessary variables is set
299     if [ -z "$OS_AUTH_URL" ]; then
300         echo "OS_AUTH_URL is unset or empty"
301         exit 1
302     fi
303
304     echo "OS_AUTH_URL is $OS_AUTH_URL"
305     echo
306
307     # check OpenStack services
308     echo "Checking OpenStack services:"
309     for cmd in "openstack image list" "openstack server list" "openstack stack list"; do
310         echo "  checking ${cmd} ..."
311         if ! $cmd >/dev/null; then
312             echo "error: command \"$cmd\" failed"
313             exit 1
314         fi
315     done
316
317     echo
318     echo "Checking for External network:"
319     for net in $(openstack network list --external -c Name -f value); do
320         echo "  external network: $net"
321     done
322
323     # install yardstick
324     install_yardstick
325
326     source $YARDSTICK_REPO_DIR/tests/ci/clean_images.sh
327
328     trap "error_exit" EXIT SIGTERM
329
330     source $YARDSTICK_REPO_DIR/tests/ci/load_images.sh
331     install_storperf
332     run_test
333     remove_storperf
334 }
335
336 main