Add new fields to config file in slice-analysis and remove unused files
[integration/csit.git] / scripts / common_functions.sh
1 #!/bin/bash
2
3 # Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 function memory_details(){
18     #General memory details
19     echo "> top -bn1 | head -3"
20     top -bn1 | head -3
21     echo
22
23     echo "> free -h"
24     free -h
25     echo
26
27     #Memory details per Docker
28     echo "> docker ps"
29     docker ps
30     echo
31
32     echo "> docker stats --no-stream"
33     docker stats --no-stream
34     echo
35 }
36 function fibonacci_number(){
37     set +x
38     if [ $1 -le 1 ]
39     then
40         echo "1"
41     elif [ $1 -le 10 ]
42     then
43         Num=$1
44         f1=0
45         f2=1
46         fn=-1
47         for i in `eval echo {1..$Num}`;do
48             fn=$((f1+f2))
49             f1=$f2
50             f2=$fn
51         done
52         echo $fn
53     else
54         echo "30"
55     fi
56 }
57 function wait_curl_driver(){
58     #Parameters:
59     #CURL_COMMAND - the URL on which the curl command will be executed
60     #GREP_STRING - Desired string to be found inside the body response of the
61     #              previous curl command
62     #EXCLUDE_STRING - If the filtered string (GREP_STRING) must not exist in
63     #                 the body response of the curl
64     #WAIT_MESSAGE - the message to be displayed for logging purpose. (optional)
65     #REPEAT_NUMBER - the maximum number of tries before abandoning the curl
66     #                command (optional, by default = 15)
67     #MAX_TIME - Maximum time allowed for the transfer (in seconds)
68     #STATUS_CODE - A HTTP status code desired to be found by getting the link
69     #  /!\ IMPORTANT NOTICE: the usage of STATUS_CODE option turn GREP_STRING/
70     #  /!\ EXCLUDE_STRING/and the MAX_TIME options becomes obsolete with no
71     #  /!\ execution impact
72     #MEMORY_USAGE - If Parameters exists shows the memory usage after curl
73     #               execution(s)
74
75     repeat_max=15
76     parameters="$@"
77
78     #WAIT_MESSAGE
79     if [[ $parameters == *"WAIT_MESSAGE"* ]]
80     then
81         wait_message=`echo $parameters | sed -e "s/.*WAIT_MESSAGE=//g"`
82         wait_message=`echo $wait_message | sed -e "s/ .*//g"`
83     else
84         wait_message="wait ..."
85     fi
86
87     #REPEAT_NUMBER
88     if [[ $parameters == *"REPEAT_NUMBER"* ]]
89     then
90         repeat_max=`echo $parameters | sed -e "s/.*REPEAT_NUMBER=//g"`
91         repeat_max=`echo $repeat_max | sed -e "s/ .*//g"`
92     fi
93
94     #CURL_COMMAND
95     if [[ $parameters == *"CURL_COMMAND"* ]]
96     then
97         curl_command=`echo $parameters | sed -e 's/.*CURL_COMMAND=//g'`
98         curl_command=`echo $curl_command | sed -e 's/ .*//g'`
99     else
100         echo "-Curl is empty-"  # Or no parameterseter passed.
101         return 0
102     fi
103
104     #MAX_TIME
105     if [[ $parameters == *"MAX_TIME"* ]]
106     then
107         max_time=`echo $parameters | sed -e 's/.*MAX_TIME=//g'`
108         max_time=`echo $max_time | sed -e 's/ .*//g'`
109     else
110         max_time="5"
111     fi
112
113     exclude_string=""
114     #EXCLUDE_STRING
115     if [[ $parameters == *"EXCLUDE_STRING"* ]]
116     then
117         exclude_string="-v"
118     fi
119
120     status_code=""
121     #STATUS_CODE
122     if [[ $parameters == *"STATUS_CODE"* ]]
123     then
124         status_code=`echo $parameters | sed -e 's/.*STATUS_CODE=//g'`
125         status_code=`echo $status_code | sed -e 's/ .*//g'`
126     fi
127
128     for i in `eval echo {1..$repeat_max}`; do
129         response_code=`curl -o /dev/null --silent --head --write-out '%{http_code}' $curl_command`
130         echo "..."
131         if [[ ! -z $status_code ]] ; then
132             if [ "$status_code" -eq "$response_code" ]
133             then
134                 echo "SUCCESS:Actual Status code <$response_code> match the expected code <$status_code>"
135                 return 0
136             else
137                 echo "WARNING:Expected <$status_code> but Actual <$response_code>"
138             fi
139         else
140             #GREP_STRING
141             if [[ $parameters == *"GREP_STRING"* ]]
142             then
143                 grep_command=`echo $parameters | sed -e 's/.*GREP_STRING=//g'`
144                 grep_command=`echo $grep_command | sed -e 's/ REPEAT_NUMBER=.*//g' | sed -e 's/ CURL_COMMAND=.*//g' | sed -e 's/ WAIT_MESSAGE=.*//g' | sed -e 's/ MAX_TIME=.*//g' | sed -e 's/ EXCLUDE_STRING.*//g'`
145             else
146                 echo "-Grep_command is empty-"  # Or no parameters passed.
147                 return 0
148             fi
149
150             str=`curl -sS -m$max_time $curl_command | grep "$grep_command"`
151             echo "BODY::$str"
152             if [[ ! -z $exclude_string ]]
153             then
154                 if [[ -z $str ]]
155                 then
156                     echo "SUCCESS: body response does not contains '$grep_command'";
157                     break;
158                 else
159                     echo "Fall_Short: Body response still contains '$grep_command'"
160                 fi
161             else
162                 if [[ ! -z $str ]]
163                 then
164                     echo "SUCCESS: body response contains '$grep_command'";
165                 break;
166                 else
167                     echo "Fall_Short: Element '$grep_command' not found yet # "$i""
168                 fi
169             fi
170
171             if [ "$?" = "7" ]; then
172                 echo 'Connection refused or can not connect to server/proxy';
173                 str=''
174             fi
175         fi
176         seconds2sleep=`fibonacci_number $i`
177         echo $wait_message
178         echo "Iteration::$i out of $repeat_max "
179         echo "Quiet time for $seconds2sleep seconds ..."
180         sleep $seconds2sleep
181
182         # if waiting for a long time, log system load
183         if [ $i -gt 45 ]
184         then
185             memory_details
186         fi
187     done
188     #MEMORY_USAGE
189     if [[ $parameters == *"MEMORY_USAGE"* ]]
190     then
191         echo "==========================MEMORY USAGE=================================="
192         memory_details
193         echo "========================================================================"
194     fi
195     return 0
196 }
197
198 function run_simulator ()
199 {
200    run_robottestlib
201    run_simulator_docker $1
202 }
203
204 function run_robottestlib ()
205 {
206     #Start the robottest REST library if not started
207     if ! pgrep -f robottest > /dev/null
208     then
209         #Download the latest robottest jar
210         wget -q -O  ${SCRIPTS}/integration/mockserver/org.openo.robottest.jar  "https://nexus.open-o.org/service/local/artifact/maven/redirect?r=snapshots&g=org.openo.integration&a=org.openo.robottest&e=jar&v=LATEST"
211         chmod +x  ${SCRIPTS}/integration/mockserver/org.openo.robottest.jar
212         eval `java -cp ${SCRIPTS}/integration/mockserver/org.openo.robottest.jar  org.openo.robot.test.robottest.MyRemoteLibrary` &
213     fi
214 }
215
216 function run_simulator_docker ()
217 {
218     #Start the simulator docker if not started
219     SIMULATOR_IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' simulator`
220     if [[ -z $SIMULATOR_IP ]]
221     then
222         echo "Starting simulator docker..."
223         SIMULATOR_JSON=$1
224         if [[ -z $SIMULATOR_JSON ]]
225         then
226             SIMULATOR_JSON=main.json
227         fi
228         docker run -d -i -t --name simulator -e SIMULATOR_JSON=$SIMULATOR_JSON -p 18009:18009 -p 18008:18008  openoint/simulate-test-docker
229         SIMULATOR_IP=`docker inspect --format '{{ .NetworkSettings.IPAddress }}' simulator`
230     fi
231
232     #Set the simulator IP in robot variables
233     ROBOT_VARIABLES=${ROBOT_VARIABLES}" -v SIMULATOR_IP:${SIMULATOR_IP}  -v SCRIPTS:${SCRIPTS}"
234     echo ${ROBOT_VARIABLES}
235 }
236
237 function get_docker_compose_service ()
238 {
239     local service=$1
240     local compose_file=${2:-docker-compose.yml}
241
242     echo $(docker-compose --file ./${compose_file} ps | grep $service |  cut -d " " -f1 )
243 }
244
245 function bypass_ip_adress ()
246 {
247     local ip_address=$1
248
249     if [[ $no_proxy && $no_proxy != *$ip_address* ]]; then
250         export no_proxy=$no_proxy,$ip_address
251     fi
252 }
253
254 function wait_for_service_init ()
255 {
256     local service_url=$1
257
258     for delay in {1..50}; do
259         curl -sS ${service_url} && break
260         echo "$delay - Waiting for $service_url..."
261         sleep $delay
262     done
263 }