blob: b4ceb0d004492be98c778267c08fdd161973c43e [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
maximessonf217fff2020-04-09 15:16:04 +020011STARTED_POLICY_AGENT="" #Policy Agent app names added to this var to keep track of started container in the script
YongchaoWu9a84f512019-12-16 22:54:11 +010012START_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
maximessonf217fff2020-04-09 15:16:04 +020088 #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
YongchaoWuffde6eb2020-01-17 13:58:51 +010090 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
maximessonf217fff2020-04-09 15:16:04 +020097 echo -e "Policy Agent local image: \033[1m"$tmp_im"\033[0m"
98 echo "If the Policy Agent image seem outdated, rebuild the image and run the test again."
YongchaoWuffde6eb2020-01-17 13:58:51 +010099 fi
100 elif [ $1 == "remote" ]; then
101 if [ -z "$tmp_im" ]; then
maximessonf217fff2020-04-09 15:16:04 +0200102 echo "Pulling Policy Agent image from nexus: "$POLICY_AGENT_IMAGE
YongchaoWuffde6eb2020-01-17 13:58:51 +0100103 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"
maximessonf217fff2020-04-09 15:16:04 +0200112 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"
maximesson28ee8a52020-03-17 09:32:03 +0100120if [ -z "$SIM_DIR" ]; then
121 SIM_DIR=$(find . -type d -path "*a1-interface/near-rt-ric-simulator/scripts*" 2>/dev/null -print -quit)
122 if [ ! -d $SIM_DIR ]; then
123 echo "Trying to set env var SIM_DIR to dir 'a1-interface/near-rt-ric-simulator/scripts' in the sim repo, but failed."
maximessonf217fff2020-04-09 15:16:04 +0200124 echo "It might be that you did not download the repository of the Near-rt-ric simulator.In that case, run the command:"
maximesson28ee8a52020-03-17 09:32:03 +0100125 echo "git clone 'https://gerrit.o-ran-sc.org/oransc/sim/a1-interface'"
126 echo "Otherwise, please set the SIM_DIR manually in the test_env.sh"
127 exit 1
128 else
129 echo "SIM_DIR auto set to: " $SIM_DIR
130 fi
131fi
132cd $SIM_DIR
YongchaoWuf309b1b2020-01-22 13:24:48 +0100133docker build -t ric-simulator:latest . &> /dev/null
134cd $curdir
135
136echo ""
137
138echo "Local registry images for simulators:"
139echo "Consul: " $(docker images | grep consul)
140echo "CBS: " $(docker images | grep platform.configbinding.app)
141echo "RIC: " $(docker images | grep ric-simulator)
142echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100143
144
145__consul_config() {
146
147 appname=$PA_APP_BASE
148
149 echo "Configuring consul for " $appname " from " $1
150 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
151}
152
153
154consul_config_app() {
155
156 __consul_config $1
157
158}
159
YongchaoWu4e489b02020-02-24 09:18:16 +0100160
161
162start_ric_simulator() {
163
164 DOCKER_SIM_NWNAME="nonrtric-docker-net"
165 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
166 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
167
168 echo "start ric simulator"
169 curdir=$PWD
170 cd $SIM_GROUP
171 cd ric/
172
173 docker-compose up --scale ric-simulator=$RIC_NUMBER -d
174
175 cd $curdir
176 echo ""
177}
178
YongchaoWu21f17bb2020-03-05 12:44:08 +0100179start_dashboard() {
180
181 DOCKER_SIM_NWNAME="nonrtric-docker-net"
182 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
183 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
184
maximessonf217fff2020-04-09 15:16:04 +0200185 echo "start Control Panel"
YongchaoWu21f17bb2020-03-05 12:44:08 +0100186 curdir=$PWD
187 cd $SIM_GROUP
188 cd dashboard/
189
190 docker-compose up -d
191
192 cd $curdir
193 echo ""
194}
195
196start_sdnc() {
197
198 if [ $SDNC == "sdnc" ]; then
199 DOCKER_SIM_NWNAME="nonrtric-docker-net"
200 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
201 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
202
203 echo "start sdnc"
204 curdir=$PWD
205 cd $SIM_GROUP
206 cd sdnc/
207
208 docker-compose up -d a1-controller
209
210 cd $curdir
211 echo ""
212 fi
213}
214
YongchaoWu4e489b02020-02-24 09:18:16 +0100215prepare_consul_config() {
216 echo "prepare consul config"
217 curdir=$PWD
218 cd $SIM_GROUP
219 cd ric/
220
221 python3 cleanConsul.py
222 python3 prepareConsul.py
223
224 cd $curdir
225 echo ""
226
227
228}
229
230
YongchaoWu9a84f512019-12-16 22:54:11 +0100231# Start all simulators in the simulator group
232start_simulators() {
233
234 echo "Starting all simulators"
235 curdir=$PWD
236 cd $SIM_GROUP
YongchaoWu4e489b02020-02-24 09:18:16 +0100237
YongchaoWu9a84f512019-12-16 22:54:11 +0100238 $SIM_GROUP/simulators-start.sh
239 cd $curdir
240 echo ""
241}
242
YongchaoWu21f17bb2020-03-05 12:44:08 +0100243
244
YongchaoWu9a84f512019-12-16 22:54:11 +0100245clean_containers() {
maximessonf217fff2020-04-09 15:16:04 +0200246 echo "Stopping all containers, Policy Agent app(s) and simulators with name prefix 'policy-agent'"
YongchaoWu9a84f512019-12-16 22:54:11 +0100247 docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200248 echo "Removing all containers, Policy Agent app and simulators with name prefix 'policy-agent'"
YongchaoWu9a84f512019-12-16 22:54:11 +0100249 docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200250 echo "Stopping all containers, Policy Agent app(s) and simulators with name prefix 'ric-simulator'"
YongchaoWu4e489b02020-02-24 09:18:16 +0100251 docker stop $(docker ps -q --filter name=ric-simulator) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200252 echo "Removing all containers, Policy Agent app and simulators with name prefix 'ric-simulator'"
YongchaoWu4e489b02020-02-24 09:18:16 +0100253 docker rm $(docker ps -a -q --filter name=ric-simulator) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200254 echo "Removing all containers, Policy Agent app and simulators with name prefix 'dashboard'"
YongchaoWu21f17bb2020-03-05 12:44:08 +0100255 docker rm $(docker ps -a -q --filter name=dashboard) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200256 echo "Removing all containers, Policy Agent app and simulators with name prefix 'a1-controller'"
YongchaoWu21f17bb2020-03-05 12:44:08 +0100257 docker rm $(docker ps -a -q --filter name=a1-controller) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200258 echo "Removing all containers, Policy Agent app and simulators with name prefix 'sdnc_db_container'"
YongchaoWu21f17bb2020-03-05 12:44:08 +0100259 docker rm $(docker ps -a -q --filter name=sdnc_db_container) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200260 echo "Removing all containers, Policy Agent app and simulators with name prefix 'cbs'"
YongchaoWu21f17bb2020-03-05 12:44:08 +0100261 docker rm $(docker ps -a -q --filter name=polman_cbs) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200262 echo "Removing all containers, Policy Agent app and simulators with name prefix 'consul'"
YongchaoWu21f17bb2020-03-05 12:44:08 +0100263 docker rm $(docker ps -a -q --filter name=polman_consul) &> /dev/null
maximessonf217fff2020-04-09 15:16:04 +0200264 echo "Removing unused docker networks with substring 'nonrtric' in network name"
YongchaoWu9a84f512019-12-16 22:54:11 +0100265 docker network rm $(docker network ls -q --filter name=nonrtric)
266 echo ""
267}
268
269start_policy_agent() {
270
271 appname=$PA_APP_BASE
272
YongchaoWuffde6eb2020-01-17 13:58:51 +0100273 if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +0100274 __start_policy_agent_image $appname
275 fi
276}
277
278__start_policy_agent_image() {
279
280 appname=$1
281 localport=$POLICY_AGENT_PORT
282
283 echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
284
285 docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
286
maximessonf217fff2020-04-09 15:16:04 +0200287 echo "Starting Policy Agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME
YongchaoWu9a84f512019-12-16 22:54:11 +0100288 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
289 #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
290 sleep 3
291 set +x
292 pa_started=false
293 for i in {1..10}; do
294 if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
295 then
296 echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
297 echo "Policy Agent container ${appname} running"
298 pa_started=true
299 break
300 else
301 sleep $i
302 fi
303 done
304 if ! [ $pa_started ]; then
305 echo "Policy Agent container ${appname} could not be started"
306 exit 1
307 fi
308
309 pa_st=false
310 echo "Waiting for Policy Agent ${appname} service status..."
311 for i in {1..10}; do
312 result="$(__do_curl http://127.0.0.1:${localport}/status)"
313 if [ $? -eq 0 ]; then
314 echo "Policy Agent ${appname} responds to service status: " $result
YongchaoWuffde6eb2020-01-17 13:58:51 +0100315 echo "Policy Agent is alive."
YongchaoWu9a84f512019-12-16 22:54:11 +0100316 pa_st=true
317 break
318 else
319 sleep $i
320 fi
321 done
322
323 if [ "$pa_st" = "false" ]; then
324 echo "Policy Agent ${appname} did not respond to service status"
325 exit 1
326 fi
327}
328
329check_policy_agent_logs() {
330
331 appname=$PA_APP_BASE
332 tmp=$(docker ps | grep $appname)
maximessonf217fff2020-04-09 15:16:04 +0200333 if ! [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
YongchaoWu9a84f512019-12-16 22:54:11 +0100334 __check_policy_agent_log $appname
335 fi
336
337}
338
339__check_policy_agent_log() {
340 echo "Checking $1 log $POLICY_AGENT_LOGPATH for WARNINGs and ERRORs"
341 foundentries=$(docker exec -it $1 grep WARN /var/log/policy-agent/application.log | 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 WARN entries found in $1 log $POLICY_AGENT_LOGPATH"
347 else
348 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $POLICY_AGENT_LOGPATH"
349 fi
350 fi
351 foundentries=$(docker exec -it $1 grep ERR $POLICY_AGENT_LOGPATH | wc -l)
352 if [ $? -ne 0 ];then
353 echo " Problem to search $1 log $POLICY_AGENT_LOGPATH"
354 else
355 if [ $foundentries -eq 0 ]; then
356 echo " No ERR entries found in $1 log $POLICY_AGENT_LOGPATH"
357 else
358 echo -e " Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $POLICY_AGENT_LOGPATH"
359 fi
360 fi
361}
362
363store_logs() {
364 if [ $# != 1 ]; then
365 __print_err "need one arg, <file-prefix>"
366 exit 1
367 fi
maximessonf217fff2020-04-09 15:16:04 +0200368 echo "Storing all container logs and Policy Agent app log using prefix: "$1
YongchaoWu9a84f512019-12-16 22:54:11 +0100369
370 docker logs polman_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1
371 docker logs polman_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1
372}
373
374__do_curl() {
375 res=$(curl -skw "%{http_code}" $1)
376 http_code="${res:${#res}-3}"
377 if [ ${#res} -eq 3 ]; then
378 echo "<no-response-from-server>"
379 return 1
380 else
381 if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then
382 echo "<not found, resp:${http_code}>"
383 return 1
384 fi
385 if [ $# -eq 2 ]; then
386 echo "${res:0:${#res}-3}" | xargs
387 else
388 echo "${res:0:${#res}-3}"
389 fi
390
391 return 0
392 fi
393}
394