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