Moving the near-rt-ric simulator
[nonrtric.git] / near-rt-ric-simulator / common / testcase_common.sh
1 #!/usr/bin/env bash
2
3 . ../common/test_env.sh
4
5 echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 $2 $3
6 echo "Numbers of ric simulator started" $2
7
8 # This is a script that contains all the functions needed for auto test
9 # Arg: local | remote (1, 2, 3, 4....)
10
11 STARTED_POLICY_AGENT="" #Policy agent app names added to this var to keep track of started container in the script
12 START_ARG=$1
13 IMAGE_TAG="1.0.0-SNAPSHOT"
14 IMAGE_TAG_REMOTE="latest"
15 RIC_NUMBER=$2
16 SDNC=$3
17
18 if [ $# -lt 1 ] || [ $# -gt 4 ]; then
19         echo "Expected arg: local  | remote  and numbers of the rics and SDNC "
20         exit 1
21 elif [ $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
27 elif [ $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
33 fi
34
35 # Set a description string for the test case
36 if [ -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
39 fi
40
41 ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
42
43
44 # Create the logs dir if not already created in the current dir
45 if [ ! -d "logs" ]; then
46     mkdir logs
47 fi
48
49 TESTLOGS=$PWD/logs
50
51 mkdir -p $TESTLOGS/$ATC
52
53 TCLOG=$TESTLOGS/$ATC/TC.log
54 exec &>  >(tee ${TCLOG})
55
56 #Variables for counting tests as well as passed and failed tests
57 RES_TEST=0
58 RES_PASS=0
59 RES_FAIL=0
60 TCTEST_START=$SECONDS
61
62 echo "-------------------------------------------------------------------------------------------------"
63 echo "-----------------------------------      Test case: "$ATC
64 echo "-----------------------------------      Started:   "$(date)
65 echo "-------------------------------------------------------------------------------------------------"
66 echo "-- Description: "$TC_ONELINE_DESCR
67 echo "-------------------------------------------------------------------------------------------------"
68 echo "-----------------------------------      Test case setup      -----------------------------------"
69
70
71 if [ -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
80 elif [ $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
83 fi
84
85 echo ""
86
87 if [ $1 !=  "manual-container" ] && [ $1 !=  "manual-app" ]; then
88         #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)
91
92         if [ $1 == "local" ]; then
93                 if [ -z "$tmp_im" ]; then
94                         echo "Local image (non nexus) "$POLICY_AGENT_IMAGE" does not exist in local registry, need to be built"
95                         exit 1
96                 else
97                         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."
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."
113                 fi
114         fi
115 fi
116
117 echo ""
118
119 echo "Building images for the simulators"
120 if [ -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."
124                         echo "It might be that you did not download the repository of the near-rt-ric simulator.In that case, run the command:"
125                         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
131 fi
132 cd $SIM_DIR
133 docker build -t ric-simulator:latest . &> /dev/null
134 cd $curdir
135
136 echo ""
137
138 echo "Local registry images for simulators:"
139 echo "Consul:             " $(docker images | grep consul)
140 echo "CBS:                " $(docker images | grep platform.configbinding.app)
141 echo "RIC:                " $(docker images | grep ric-simulator)
142 echo ""
143
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
154 consul_config_app() {
155
156     __consul_config $1
157
158 }
159
160
161
162 start_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
179 start_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
185   echo "start dashboard"
186   curdir=$PWD
187   cd $SIM_GROUP
188   cd dashboard/
189
190   docker-compose up -d
191
192   cd $curdir
193         echo ""
194 }
195
196 start_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
215 prepare_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
231 # Start all simulators in the simulator group
232 start_simulators() {
233
234         echo "Starting all simulators"
235         curdir=$PWD
236         cd $SIM_GROUP
237
238         $SIM_GROUP/simulators-start.sh
239         cd $curdir
240         echo ""
241 }
242
243
244
245 clean_containers() {
246         echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'policy_agent'"
247         docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null
248         echo "Removing all containers, policy agent app and simulators with name prefix 'policy_agent'"
249         docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null
250         echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'ric-simulator'"
251         docker stop $(docker ps -q --filter name=ric-simulator) &> /dev/null
252         echo "Removing all containers, policy agent app and simulators with name prefix 'ric-simulator'"
253         docker rm $(docker ps -a -q --filter name=ric-simulator) &> /dev/null
254         echo "Removing all containers, policy agent app and simulators with name prefix 'dashboard'"
255         docker rm $(docker ps -a -q --filter name=dashboard) &> /dev/null
256         echo "Removing all containers, policy agent app and simulators with name prefix 'a1-controller'"
257         docker rm $(docker ps -a -q --filter name=a1-controller) &> /dev/null
258         echo "Removing all containers, policy agent app and simulators with name prefix 'sdnc_db_container'"
259         docker rm $(docker ps -a -q --filter name=sdnc_db_container) &> /dev/null
260         echo "Removing all containers, policy agent app and simulators with name prefix 'cbs'"
261         docker rm $(docker ps -a -q --filter name=polman_cbs) &> /dev/null
262         echo "Removing all containers, policy agent app and simulators with name prefix 'consul'"
263         docker rm $(docker ps -a -q --filter name=polman_consul) &> /dev/null
264         echo "Removing unused docker networks with substring 'policy agent' in network name"
265         docker network rm $(docker network ls -q --filter name=nonrtric)
266         echo ""
267 }
268
269 start_policy_agent() {
270
271         appname=$PA_APP_BASE
272
273         if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ]; then
274                 __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
287         echo "Starting policy agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME
288         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
315                         echo "Policy Agent is alive."
316                         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
329 check_policy_agent_logs() {
330
331                 appname=$PA_APP_BASE
332                 tmp=$(docker ps | grep $appname)
333                 if ! [ -z "$tmp" ]; then  #Only check logs for running policy agent apps
334                         __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
363 store_logs() {
364         if [ $# != 1 ]; then
365         __print_err "need one arg, <file-prefix>"
366                 exit 1
367         fi
368         echo "Storing all container logs and policy agent app log using prefix: "$1
369
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