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