Add test name check function and cleanup
[bottlenecks.git] / run_tests.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd 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 usage="Script to run the tests in Bottlenecks.
12
13 usage:
14     bash $(basename "$0") [-h|--help] [-s <test suite>] [-c <test case>] [--report] [--cleanup]
15
16 where:
17     -h|--help         show the help text
18     -s|--teststory    run specific test story
19       <test story>        one of the following:
20                               (rubbos, vstf, posca_factor_test)
21                       user can also define their own test story and pass as var to this file,
22                       please refer to testsuites/posca/testsuite_story/ for details
23     -c|--testcase     run specific test case
24       <test case>         one of the following:
25                               (posca_factor_system_bandwidth, posca_factor_ping)
26     --cleanup         cleanup test dockers runing when test is done (false by default)
27     --report          push results to DB (false by default)
28
29 examples:
30     $(basename "$0")
31     $(basename "$0") -s posca_factor_test"
32
33 # Define global variables
34 Bottlenecks_key_dir="/home/opnfv/bottlenecks/utils/infra_setup"
35 POSCA_SUITE="/home/opnfv/bottlenecks/testsuites/posca"
36 POSCA_TESTCASE="/home/opnfv/bottlenecks/testsuites/posca/testcase_cfg"
37 POSCA_TESTSTORY="/home/opnfv/bottlenecks/testsuites/posca/testsuite_story"
38 BASEDIR=`dirname $0`
39
40 report=false
41 cleanup=false
42
43
44 # Define alias for log printing
45 info () {
46     logger -s -t "bottlenecks.info" "$*"
47 }
48
49 error () {
50     logger -s -t "bottlenecks.error" "$*"
51     exit 1
52 }
53
54 # Define check_test function for test case/story list check
55 function check_test(){
56
57     TEST_LEVEL="$1"
58     TEST_NAME="$2"
59
60     if [[ "${TEST_LEVEL}" == "testcase" ]]; then
61         TEST_CONFIG="${POSCA_TESTCASE}"
62     else
63         if [[ "${TEST_LEVEL}" == "teststory" ]]; then
64             TEST_CONFIG="${POSCA_TESTSTORY}"
65         else
66             info "Invalid name for test level: testcase or teststory"
67         fi
68     fi
69
70     # Find all the test case yaml files first
71     find $TEST_CONFIG -name "*yaml" > /tmp/all_tests.yaml
72     all_tests_insuite=`cat /tmp/all_tests.yaml | awk -F '/' '{print $NF}' | awk -F '.' '{print $1}'`
73     all_tests=(${all_tests_insuite})
74
75     if [ "${TEST_NAME}" != "" ]; then
76        TEST_EXEC=(${TEST_NAME// /})
77        for i in "${TEST_EXEC[@]}"; do
78            if [[ " ${all_tests[*]} " != *" $i "* ]]; then
79                error "Unknown $TEST_LEVEL: $i. Available $TEST_LEVEL are: ${all_tests[@]}"
80            fi
81        done
82        info "Tests to execute: ${TEST_NAME}."
83     else
84        error "Lack of $TEST_LEVEL name"
85     fi
86 }
87
88 # Define run test function
89 function run_test(){
90
91     test_exec=$1
92
93     case $test_exec in
94         "rubbos")
95             info "After OPNFV Colorado release, Rubbos testsuite is not updating anymore.
96                   This entrance for running Rubbos within Bottlenecks is no longer supported.
97                   This testsuite is also not in the release plan with Bottlenecks since then.
98                   If you want to run Rubbos, please refer to earlier releases."
99         ;;
100         "vstf")
101             info "After OPNFV Colorado release, VSTF testsuite is not updating anymore.
102                   This entrance for running VSTF within Bottlenecks is no longer supported.
103                   This testsuite is also not in the release plan with Bottlenecks since then.
104                   If you want to run VSTF, please refer to earlier releases."
105         ;;
106         *)
107             info "Composing up dockers"
108             docker-compose -f /home/opnfv/bottlenecks/docker/bottleneck-compose/docker-compose.yml up -d
109             info "Pulling tutum/influxdb for yardstick"
110             docker pull tutum/influxdb:0.13
111             sleep 5
112             info "Running posca test story: $test_exec"
113             docker exec bottleneckcompose_bottlenecks_1 python ${POSCA_SUITE}/run_posca.py $test_level $test_exec
114         ;;
115     esac
116 }
117
118 # Process input variables
119 while [[ $# > 0 ]]
120     do
121     key="$1"
122     case $key in
123         -h|--help)
124             echo "$usage"
125             exit 0
126             shift
127         ;;
128         -s|--teststory)
129             teststory="$2"
130             shift
131         ;;
132         -c|--testcase)
133             testcase="$2"
134             shift
135         ;;
136         --report)
137             report=true
138         ;;
139         --cleanup)
140             cleanup=true
141         ;;
142         *)
143             echo "unkown option $1 $2"
144             exit 1
145         ;;
146      esac
147      shift
148 done
149
150 # Clean up related docker images
151 bash ${BASEDIR}/docker/docker_cleanup.sh -d bottlenecks --debug
152 bash ${BASEDIR}/docker/docker_cleanup.sh -d yardstick --debug
153 bash ${BASEDIR}/docker/docker_cleanup.sh -d kibana --debug
154 bash ${BASEDIR}/docker/docker_cleanup.sh -d elasticsearch --debug
155 bash ${BASEDIR}/docker/docker_cleanup.sh -d influxdb --debug
156
157 # Run tests
158 if [ "${teststory}" != "" ]; then
159     test_level="teststory"
160     teststory_exec=(${teststory//,/ })
161     check_test $test_level $teststory
162     for i in "${teststory_exec[@]}"; do
163         info "Start to run test story $i"
164         run_test $i
165     done
166 fi
167
168 if [ "${testcase}" != "" ]; then
169     test_level="testcase"
170     testcase_exec=(${testcase//,/ })
171     check_test $test_level $testcase
172     for i in "${testcase_exec[@]}"; do
173         info "Start to run test case $i"
174         run_test $i
175     done
176 fi
177
178 # Clean up testing dockers
179 if [[ ${cleanup} == true ]]; then
180     info "Cleaning up docker-compose images and dockers"
181     docker-compose -f $BASEDIR/docker/bottleneck-compose/docker-compose.yml down --rmi all
182     bash ${BASEDIR}/docker/docker_cleanup.sh -d influxdb --debug
183     bash ${BASEDIR}/docker/docker_cleanup.sh -d bottlenecks --debug
184 fi