blob: d9a012ea66e7cc0fdf1d985b9fa083a5601bb926 [file] [log] [blame]
YongchaoWu9a84f512019-12-16 22:54:11 +01001#!/usr/bin/env bash
2
3. ../common/test_env.sh
4
YongchaoWu4e489b02020-02-24 09:18:16 +01005echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 $2
6echo "Numbers of ric simulator started" $2
YongchaoWu9a84f512019-12-16 22:54:11 +01007
YongchaoWuffde6eb2020-01-17 13:58:51 +01008# This is a script that contains all the functions needed for auto test
YongchaoWu4e489b02020-02-24 09:18:16 +01009# Arg: local | remote (1, 2, 3, 4....)
YongchaoWuffde6eb2020-01-17 13:58:51 +010010
YongchaoWu9a84f512019-12-16 22:54:11 +010011STARTED_POLICY_AGENT="" #Policy agent app names added to this var to keep track of started container in the script
12START_ARG=$1
13IMAGE_TAG="1.0.0-SNAPSHOT"
YongchaoWua3c3e362020-02-10 11:50:04 +010014IMAGE_TAG_REMOTE="latest"
YongchaoWu4e489b02020-02-24 09:18:16 +010015RIC_NUMBER=$2
YongchaoWu9a84f512019-12-16 22:54:11 +010016
17if [ $# -lt 1 ] || [ $# -gt 2 ]; then
YongchaoWu4e489b02020-02-24 09:18:16 +010018 echo "Expected arg: local | remote and numbers of the rics "
YongchaoWu9a84f512019-12-16 22:54:11 +010019 exit 1
20elif [ $1 == "local" ]; then
21 if [ -z $POLICY_AGENT_LOCAL_IMAGE ]; then
22 echo "POLICY_AGENT_LOCAL_IMAGE not set in test_env"
23 exit 1
24 fi
25 POLICY_AGENT_IMAGE=$POLICY_AGENT_LOCAL_IMAGE":"$IMAGE_TAG
YongchaoWuffde6eb2020-01-17 13:58:51 +010026elif [ $1 == "remote" ]; then
27 if [ -z $POLICY_AGENT_REMOTE_IMAGE ]; then
28 echo "POLICY_AGENT_REMOTE_IMAGE not set in test_env"
29 exit 1
30 fi
31 POLICY_AGENT_IMAGE=$POLICY_AGENT_REMOTE_IMAGE":"$IMAGE_TAG_REMOTE
YongchaoWu9a84f512019-12-16 22:54:11 +010032fi
33
34# Set a description string for the test case
35if [ -z "$TC_ONELINE_DESCR" ]; then
36 TC_ONELINE_DESCR="<no-description>"
37 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
38fi
39
40ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
41
42
43# Create the logs dir if not already created in the current dir
44if [ ! -d "logs" ]; then
45 mkdir logs
46fi
47
48TESTLOGS=$PWD/logs
49
50mkdir -p $TESTLOGS/$ATC
51
52TCLOG=$TESTLOGS/$ATC/TC.log
53exec &> >(tee ${TCLOG})
54
55#Variables for counting tests as well as passed and failed tests
56RES_TEST=0
57RES_PASS=0
58RES_FAIL=0
59TCTEST_START=$SECONDS
60
61echo "-------------------------------------------------------------------------------------------------"
62echo "----------------------------------- Test case: "$ATC
63echo "----------------------------------- Started: "$(date)
64echo "-------------------------------------------------------------------------------------------------"
65echo "-- Description: "$TC_ONELINE_DESCR
66echo "-------------------------------------------------------------------------------------------------"
67echo "----------------------------------- Test case setup -----------------------------------"
68
69
70if [ -z "$SIM_GROUP" ]; then
71 SIM_GROUP=$PWD/../simulator-group
72 if [ ! -d $SIM_GROUP ]; then
73 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the integration repo, but failed."
74 echo "Please set the SIM_GROUP manually in the test_env.sh"
75 exit 1
76 else
77 echo "SIM_GROUP auto set to: " $SIM_GROUP
78 fi
79elif [ $SIM_GROUP = *simulator_group ]; then
80 echo "Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the integration repo, check test_env.sh"
81 exit 1
82fi
83
84echo ""
85
86if [ $1 != "manual-container" ] && [ $1 != "manual-app" ]; then
YongchaoWuffde6eb2020-01-17 13:58:51 +010087 #echo -e "Policy agent image tag set to: \033[1m" $IMAGE_TAG"\033[0m"
88 echo "Configured image for policy agent app(s) (${1}): "$POLICY_AGENT_IMAGE
89 tmp_im=$(docker images ${POLICY_AGENT_IMAGE} | grep -v REPOSITORY)
YongchaoWu9a84f512019-12-16 22:54:11 +010090
91 if [ $1 == "local" ]; then
92 if [ -z "$tmp_im" ]; then
YongchaoWuffde6eb2020-01-17 13:58:51 +010093 echo "Local image (non nexus) "$POLICY_AGENT_IMAGE" does not exist in local registry, need to be built"
YongchaoWu9a84f512019-12-16 22:54:11 +010094 exit 1
95 else
96 echo -e "Policy agent local image: \033[1m"$tmp_im"\033[0m"
YongchaoWuffde6eb2020-01-17 13:58:51 +010097 echo "If the policy agent image seem outdated, rebuild the image and run the test again."
98 fi
99 elif [ $1 == "remote" ]; then
100 if [ -z "$tmp_im" ]; then
101 echo "Pulling policy agent image from nexus: "$POLICY_AGENT_IMAGE
102 docker pull $POLICY_AGENT_IMAGE > /dev/null
103 tmp_im=$(docker images ${POLICY_AGENT_IMAGE} | grep -v REPOSITORY)
104 if [ -z "$tmp_im" ]; then
105 echo "Image could not be pulled"
106 exit 1
107 fi
108 echo -e "Policy Agent image: \033[1m"$tmp_im"\033[0m"
109 else
110 echo -e "Policy Agent image: \033[1m"$tmp_im"\033[0m"
111 echo "!! If the Policy agent image seem outdated, consider removing it from your docker registry and run the test again."
YongchaoWu9a84f512019-12-16 22:54:11 +0100112 fi
113 fi
114fi
115
YongchaoWuf309b1b2020-01-22 13:24:48 +0100116echo ""
117
118echo "Building images for the simulators"
119curdir=$PWD
120cd $SIM_GROUP
121cd ../ric-plt/a1
122docker build -t ric-simulator:latest . &> /dev/null
123cd $curdir
124
125echo ""
126
127echo "Local registry images for simulators:"
128echo "Consul: " $(docker images | grep consul)
129echo "CBS: " $(docker images | grep platform.configbinding.app)
130echo "RIC: " $(docker images | grep ric-simulator)
131echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100132
133
134__consul_config() {
135
136 appname=$PA_APP_BASE
137
138 echo "Configuring consul for " $appname " from " $1
139 curl -s http://127.0.0.1:${CONSUL_PORT}/v1/kv/${appname}?dc=dc1 -X PUT -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'X-Requested-With: XMLHttpRequest' --data-binary "@"$1 >/dev/null
140}
141
142
143consul_config_app() {
144
145 __consul_config $1
146
147}
148
YongchaoWu4e489b02020-02-24 09:18:16 +0100149
150
151start_ric_simulator() {
152
153 DOCKER_SIM_NWNAME="nonrtric-docker-net"
154 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
155 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
156
157 echo "start ric simulator"
158 curdir=$PWD
159 cd $SIM_GROUP
160 cd ric/
161
162 docker-compose up --scale ric-simulator=$RIC_NUMBER -d
163
164 cd $curdir
165 echo ""
166}
167
168prepare_consul_config() {
169 echo "prepare consul config"
170 curdir=$PWD
171 cd $SIM_GROUP
172 cd ric/
173
174 python3 cleanConsul.py
175 python3 prepareConsul.py
176
177 cd $curdir
178 echo ""
179
180
181}
182
183
YongchaoWu9a84f512019-12-16 22:54:11 +0100184# Start all simulators in the simulator group
185start_simulators() {
186
187 echo "Starting all simulators"
188 curdir=$PWD
189 cd $SIM_GROUP
YongchaoWu4e489b02020-02-24 09:18:16 +0100190
YongchaoWu9a84f512019-12-16 22:54:11 +0100191 $SIM_GROUP/simulators-start.sh
192 cd $curdir
193 echo ""
194}
195
196clean_containers() {
197 echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'policy_agent'"
198 docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null
199 echo "Removing all containers, policy agent app and simulators with name prefix 'policy_agent'"
200 docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null
YongchaoWu4e489b02020-02-24 09:18:16 +0100201 echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'ric-simulator'"
202 docker stop $(docker ps -q --filter name=ric-simulator) &> /dev/null
203 echo "Removing all containers, policy agent app and simulators with name prefix 'ric-simulator'"
204 docker rm $(docker ps -a -q --filter name=ric-simulator) &> /dev/null
YongchaoWu9a84f512019-12-16 22:54:11 +0100205 echo "Removing unused docker networks with substring 'policy agent' in network name"
206 docker network rm $(docker network ls -q --filter name=nonrtric)
207 echo ""
208}
209
210start_policy_agent() {
211
212 appname=$PA_APP_BASE
213
YongchaoWuffde6eb2020-01-17 13:58:51 +0100214 if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +0100215 __start_policy_agent_image $appname
216 fi
217}
218
219__start_policy_agent_image() {
220
221 appname=$1
222 localport=$POLICY_AGENT_PORT
223
224 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
225
226 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
227
228 echo "Starting policy agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME
229 docker run -d -p $localport":8081" --network=$DOCKER_SIM_NWNAME -e CONSUL_HOST=$CONSUL_HOST -e CONSUL_PORT=$CONSUL_PORT -e CONFIG_BINDING_SERVICE=$CONFIG_BINDING_SERVICE -e HOSTNAME=$appname --name $appname $POLICY_AGENT_IMAGE
230 #docker run -d -p 8081:8081 --network=nonrtric-docker-net -e CONSUL_HOST=CONSUL_HOST=$CONSUL_HOST -e CONSUL_PORT=$CONSUL_PORT -e CONFIG_BINDING_SERVICE=$CONFIG_BINDING_SERVICE -e HOSTNAME=policy-agent
231 sleep 3
232 set +x
233 pa_started=false
234 for i in {1..10}; do
235 if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
236 then
237 echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
238 echo "Policy Agent container ${appname} running"
239 pa_started=true
240 break
241 else
242 sleep $i
243 fi
244 done
245 if ! [ $pa_started ]; then
246 echo "Policy Agent container ${appname} could not be started"
247 exit 1
248 fi
249
250 pa_st=false
251 echo "Waiting for Policy Agent ${appname} service status..."
252 for i in {1..10}; do
253 result="$(__do_curl http://127.0.0.1:${localport}/status)"
254 if [ $? -eq 0 ]; then
255 echo "Policy Agent ${appname} responds to service status: " $result
YongchaoWuffde6eb2020-01-17 13:58:51 +0100256 echo "Policy Agent is alive."
YongchaoWu9a84f512019-12-16 22:54:11 +0100257 pa_st=true
258 break
259 else
260 sleep $i
261 fi
262 done
263
264 if [ "$pa_st" = "false" ]; then
265 echo "Policy Agent ${appname} did not respond to service status"
266 exit 1
267 fi
268}
269
270check_policy_agent_logs() {
271
272 appname=$PA_APP_BASE
273 tmp=$(docker ps | grep $appname)
274 if ! [ -z "$tmp" ]; then #Only check logs for running policy agent apps
275 __check_policy_agent_log $appname
276 fi
277
278}
279
280__check_policy_agent_log() {
281 echo "Checking $1 log $POLICY_AGENT_LOGPATH for WARNINGs and ERRORs"
282 foundentries=$(docker exec -it $1 grep WARN /var/log/policy-agent/application.log | wc -l)
283 if [ $? -ne 0 ];then
284 echo " Problem to search $1 log $POLICY_AGENT_LOGPATH"
285 else
286 if [ $foundentries -eq 0 ]; then
287 echo " No WARN entries found in $1 log $POLICY_AGENT_LOGPATH"
288 else
289 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $POLICY_AGENT_LOGPATH"
290 fi
291 fi
292 foundentries=$(docker exec -it $1 grep ERR $POLICY_AGENT_LOGPATH | wc -l)
293 if [ $? -ne 0 ];then
294 echo " Problem to search $1 log $POLICY_AGENT_LOGPATH"
295 else
296 if [ $foundentries -eq 0 ]; then
297 echo " No ERR entries found in $1 log $POLICY_AGENT_LOGPATH"
298 else
299 echo -e " Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $POLICY_AGENT_LOGPATH"
300 fi
301 fi
302}
303
304store_logs() {
305 if [ $# != 1 ]; then
306 __print_err "need one arg, <file-prefix>"
307 exit 1
308 fi
309 echo "Storing all container logs and policy agent app log using prefix: "$1
310
311 docker logs polman_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1
312 docker logs polman_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1
313}
314
315__do_curl() {
316 res=$(curl -skw "%{http_code}" $1)
317 http_code="${res:${#res}-3}"
318 if [ ${#res} -eq 3 ]; then
319 echo "<no-response-from-server>"
320 return 1
321 else
322 if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then
323 echo "<not found, resp:${http_code}>"
324 return 1
325 fi
326 if [ $# -eq 2 ]; then
327 echo "${res:0:${#res}-3}" | xargs
328 else
329 echo "${res:0:${#res}-3}"
330 fi
331
332 return 0
333 fi
334}
335