blob: 7d55b156c7bd3150bb64e831b91ce1515081e1f5 [file] [log] [blame]
YongchaoWu9a84f512019-12-16 22:54:11 +01001#!/usr/bin/env bash
2
3. ../common/test_env.sh
4
YongchaoWu21f17bb2020-03-05 12:44:08 +01005echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 $2 $3
YongchaoWu4e489b02020-02-24 09:18:16 +01006echo "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
YongchaoWu21f17bb2020-03-05 12:44:08 +010016SDNC=$3
YongchaoWu9a84f512019-12-16 22:54:11 +010017
YongchaoWu21f17bb2020-03-05 12:44:08 +010018if [ $# -lt 1 ] || [ $# -gt 4 ]; then
19 echo "Expected arg: local | remote and numbers of the rics and SDNC "
YongchaoWu9a84f512019-12-16 22:54:11 +010020 exit 1
21elif [ $1 == "local" ]; then
22 if [ -z $POLICY_AGENT_LOCAL_IMAGE ]; then
23 echo "POLICY_AGENT_LOCAL_IMAGE not set in test_env"
24 exit 1
25 fi
26 POLICY_AGENT_IMAGE=$POLICY_AGENT_LOCAL_IMAGE":"$IMAGE_TAG
YongchaoWuffde6eb2020-01-17 13:58:51 +010027elif [ $1 == "remote" ]; then
28 if [ -z $POLICY_AGENT_REMOTE_IMAGE ]; then
29 echo "POLICY_AGENT_REMOTE_IMAGE not set in test_env"
30 exit 1
31 fi
32 POLICY_AGENT_IMAGE=$POLICY_AGENT_REMOTE_IMAGE":"$IMAGE_TAG_REMOTE
YongchaoWu9a84f512019-12-16 22:54:11 +010033fi
34
35# Set a description string for the test case
36if [ -z "$TC_ONELINE_DESCR" ]; then
37 TC_ONELINE_DESCR="<no-description>"
38 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
39fi
40
41ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
42
43
44# Create the logs dir if not already created in the current dir
45if [ ! -d "logs" ]; then
46 mkdir logs
47fi
48
49TESTLOGS=$PWD/logs
50
51mkdir -p $TESTLOGS/$ATC
52
53TCLOG=$TESTLOGS/$ATC/TC.log
54exec &> >(tee ${TCLOG})
55
56#Variables for counting tests as well as passed and failed tests
57RES_TEST=0
58RES_PASS=0
59RES_FAIL=0
60TCTEST_START=$SECONDS
61
62echo "-------------------------------------------------------------------------------------------------"
63echo "----------------------------------- Test case: "$ATC
64echo "----------------------------------- Started: "$(date)
65echo "-------------------------------------------------------------------------------------------------"
66echo "-- Description: "$TC_ONELINE_DESCR
67echo "-------------------------------------------------------------------------------------------------"
68echo "----------------------------------- Test case setup -----------------------------------"
69
70
71if [ -z "$SIM_GROUP" ]; then
72 SIM_GROUP=$PWD/../simulator-group
73 if [ ! -d $SIM_GROUP ]; then
74 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the integration repo, but failed."
75 echo "Please set the SIM_GROUP manually in the test_env.sh"
76 exit 1
77 else
78 echo "SIM_GROUP auto set to: " $SIM_GROUP
79 fi
80elif [ $SIM_GROUP = *simulator_group ]; then
81 echo "Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the integration repo, check test_env.sh"
82 exit 1
83fi
84
85echo ""
86
87if [ $1 != "manual-container" ] && [ $1 != "manual-app" ]; then
YongchaoWuffde6eb2020-01-17 13:58:51 +010088 #echo -e "Policy agent image tag set to: \033[1m" $IMAGE_TAG"\033[0m"
89 echo "Configured image for policy agent app(s) (${1}): "$POLICY_AGENT_IMAGE
90 tmp_im=$(docker images ${POLICY_AGENT_IMAGE} | grep -v REPOSITORY)
YongchaoWu9a84f512019-12-16 22:54:11 +010091
92 if [ $1 == "local" ]; then
93 if [ -z "$tmp_im" ]; then
YongchaoWuffde6eb2020-01-17 13:58:51 +010094 echo "Local image (non nexus) "$POLICY_AGENT_IMAGE" does not exist in local registry, need to be built"
YongchaoWu9a84f512019-12-16 22:54:11 +010095 exit 1
96 else
97 echo -e "Policy agent local image: \033[1m"$tmp_im"\033[0m"
YongchaoWuffde6eb2020-01-17 13:58:51 +010098 echo "If the policy agent image seem outdated, rebuild the image and run the test again."
99 fi
100 elif [ $1 == "remote" ]; then
101 if [ -z "$tmp_im" ]; then
102 echo "Pulling policy agent image from nexus: "$POLICY_AGENT_IMAGE
103 docker pull $POLICY_AGENT_IMAGE > /dev/null
104 tmp_im=$(docker images ${POLICY_AGENT_IMAGE} | grep -v REPOSITORY)
105 if [ -z "$tmp_im" ]; then
106 echo "Image could not be pulled"
107 exit 1
108 fi
109 echo -e "Policy Agent image: \033[1m"$tmp_im"\033[0m"
110 else
111 echo -e "Policy Agent image: \033[1m"$tmp_im"\033[0m"
112 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 +0100113 fi
114 fi
115fi
116
YongchaoWuf309b1b2020-01-22 13:24:48 +0100117echo ""
118
119echo "Building images for the simulators"
120curdir=$PWD
121cd $SIM_GROUP
122cd ../ric-plt/a1
123docker build -t ric-simulator:latest . &> /dev/null
124cd $curdir
125
126echo ""
127
128echo "Local registry images for simulators:"
129echo "Consul: " $(docker images | grep consul)
130echo "CBS: " $(docker images | grep platform.configbinding.app)
131echo "RIC: " $(docker images | grep ric-simulator)
132echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100133
134
135__consul_config() {
136
137 appname=$PA_APP_BASE
138
139 echo "Configuring consul for " $appname " from " $1
140 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
141}
142
143
144consul_config_app() {
145
146 __consul_config $1
147
148}
149
YongchaoWu4e489b02020-02-24 09:18:16 +0100150
151
152start_ric_simulator() {
153
154 DOCKER_SIM_NWNAME="nonrtric-docker-net"
155 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
156 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
157
158 echo "start ric simulator"
159 curdir=$PWD
160 cd $SIM_GROUP
161 cd ric/
162
163 docker-compose up --scale ric-simulator=$RIC_NUMBER -d
164
165 cd $curdir
166 echo ""
167}
168
YongchaoWu21f17bb2020-03-05 12:44:08 +0100169start_dashboard() {
170
171 DOCKER_SIM_NWNAME="nonrtric-docker-net"
172 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
173 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
174
175 echo "start dashboard"
176 curdir=$PWD
177 cd $SIM_GROUP
178 cd dashboard/
179
180 docker-compose up -d
181
182 cd $curdir
183 echo ""
184}
185
186start_sdnc() {
187
188 if [ $SDNC == "sdnc" ]; then
189 DOCKER_SIM_NWNAME="nonrtric-docker-net"
190 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
191 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
192
193 echo "start sdnc"
194 curdir=$PWD
195 cd $SIM_GROUP
196 cd sdnc/
197
198 docker-compose up -d a1-controller
199
200 cd $curdir
201 echo ""
202 fi
203}
204
YongchaoWu4e489b02020-02-24 09:18:16 +0100205prepare_consul_config() {
206 echo "prepare consul config"
207 curdir=$PWD
208 cd $SIM_GROUP
209 cd ric/
210
211 python3 cleanConsul.py
212 python3 prepareConsul.py
213
214 cd $curdir
215 echo ""
216
217
218}
219
220
YongchaoWu9a84f512019-12-16 22:54:11 +0100221# Start all simulators in the simulator group
222start_simulators() {
223
224 echo "Starting all simulators"
225 curdir=$PWD
226 cd $SIM_GROUP
YongchaoWu4e489b02020-02-24 09:18:16 +0100227
YongchaoWu9a84f512019-12-16 22:54:11 +0100228 $SIM_GROUP/simulators-start.sh
229 cd $curdir
230 echo ""
231}
232
YongchaoWu21f17bb2020-03-05 12:44:08 +0100233
234
YongchaoWu9a84f512019-12-16 22:54:11 +0100235clean_containers() {
236 echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'policy_agent'"
237 docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null
238 echo "Removing all containers, policy agent app and simulators with name prefix 'policy_agent'"
239 docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null
YongchaoWu4e489b02020-02-24 09:18:16 +0100240 echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'ric-simulator'"
241 docker stop $(docker ps -q --filter name=ric-simulator) &> /dev/null
242 echo "Removing all containers, policy agent app and simulators with name prefix 'ric-simulator'"
243 docker rm $(docker ps -a -q --filter name=ric-simulator) &> /dev/null
YongchaoWu21f17bb2020-03-05 12:44:08 +0100244 echo "Removing all containers, policy agent app and simulators with name prefix 'dashboard'"
245 docker rm $(docker ps -a -q --filter name=dashboard) &> /dev/null
246 echo "Removing all containers, policy agent app and simulators with name prefix 'a1-controller'"
247 docker rm $(docker ps -a -q --filter name=a1-controller) &> /dev/null
248 echo "Removing all containers, policy agent app and simulators with name prefix 'sdnc_db_container'"
249 docker rm $(docker ps -a -q --filter name=sdnc_db_container) &> /dev/null
250 echo "Removing all containers, policy agent app and simulators with name prefix 'cbs'"
251 docker rm $(docker ps -a -q --filter name=polman_cbs) &> /dev/null
252 echo "Removing all containers, policy agent app and simulators with name prefix 'consul'"
253 docker rm $(docker ps -a -q --filter name=polman_consul) &> /dev/null
YongchaoWu9a84f512019-12-16 22:54:11 +0100254 echo "Removing unused docker networks with substring 'policy agent' in network name"
255 docker network rm $(docker network ls -q --filter name=nonrtric)
256 echo ""
257}
258
259start_policy_agent() {
260
261 appname=$PA_APP_BASE
262
YongchaoWuffde6eb2020-01-17 13:58:51 +0100263 if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +0100264 __start_policy_agent_image $appname
265 fi
266}
267
268__start_policy_agent_image() {
269
270 appname=$1
271 localport=$POLICY_AGENT_PORT
272
273 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
274
275 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
276
277 echo "Starting policy agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME
278 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
279 #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
280 sleep 3
281 set +x
282 pa_started=false
283 for i in {1..10}; do
284 if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
285 then
286 echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
287 echo "Policy Agent container ${appname} running"
288 pa_started=true
289 break
290 else
291 sleep $i
292 fi
293 done
294 if ! [ $pa_started ]; then
295 echo "Policy Agent container ${appname} could not be started"
296 exit 1
297 fi
298
299 pa_st=false
300 echo "Waiting for Policy Agent ${appname} service status..."
301 for i in {1..10}; do
302 result="$(__do_curl http://127.0.0.1:${localport}/status)"
303 if [ $? -eq 0 ]; then
304 echo "Policy Agent ${appname} responds to service status: " $result
YongchaoWuffde6eb2020-01-17 13:58:51 +0100305 echo "Policy Agent is alive."
YongchaoWu9a84f512019-12-16 22:54:11 +0100306 pa_st=true
307 break
308 else
309 sleep $i
310 fi
311 done
312
313 if [ "$pa_st" = "false" ]; then
314 echo "Policy Agent ${appname} did not respond to service status"
315 exit 1
316 fi
317}
318
319check_policy_agent_logs() {
320
321 appname=$PA_APP_BASE
322 tmp=$(docker ps | grep $appname)
323 if ! [ -z "$tmp" ]; then #Only check logs for running policy agent apps
324 __check_policy_agent_log $appname
325 fi
326
327}
328
329__check_policy_agent_log() {
330 echo "Checking $1 log $POLICY_AGENT_LOGPATH for WARNINGs and ERRORs"
331 foundentries=$(docker exec -it $1 grep WARN /var/log/policy-agent/application.log | wc -l)
332 if [ $? -ne 0 ];then
333 echo " Problem to search $1 log $POLICY_AGENT_LOGPATH"
334 else
335 if [ $foundentries -eq 0 ]; then
336 echo " No WARN entries found in $1 log $POLICY_AGENT_LOGPATH"
337 else
338 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $POLICY_AGENT_LOGPATH"
339 fi
340 fi
341 foundentries=$(docker exec -it $1 grep ERR $POLICY_AGENT_LOGPATH | wc -l)
342 if [ $? -ne 0 ];then
343 echo " Problem to search $1 log $POLICY_AGENT_LOGPATH"
344 else
345 if [ $foundentries -eq 0 ]; then
346 echo " No ERR entries found in $1 log $POLICY_AGENT_LOGPATH"
347 else
348 echo -e " Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $POLICY_AGENT_LOGPATH"
349 fi
350 fi
351}
352
353store_logs() {
354 if [ $# != 1 ]; then
355 __print_err "need one arg, <file-prefix>"
356 exit 1
357 fi
358 echo "Storing all container logs and policy agent app log using prefix: "$1
359
360 docker logs polman_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1
361 docker logs polman_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1
362}
363
364__do_curl() {
365 res=$(curl -skw "%{http_code}" $1)
366 http_code="${res:${#res}-3}"
367 if [ ${#res} -eq 3 ]; then
368 echo "<no-response-from-server>"
369 return 1
370 else
371 if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then
372 echo "<not found, resp:${http_code}>"
373 return 1
374 fi
375 if [ $# -eq 2 ]; then
376 echo "${res:0:${#res}-3}" | xargs
377 else
378 echo "${res:0:${#res}-3}"
379 fi
380
381 return 0
382 fi
383}
384