6f2097e9ebebaf723966d06081f5a36650878019
[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
6
7 STARTED_POLICY_AGENT="" #Policy agent app names added to this var to keep track of started container in the script
8 START_ARG=$1
9 IMAGE_TAG="1.0.0-SNAPSHOT"
10
11 if [ $# -lt 1 ] || [ $# -gt 2 ]; then
12         echo "Expected arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app"
13         exit 1
14 elif [ $1 == "local" ]; then
15         if [ -z $POLICY_AGENT_LOCAL_IMAGE ]; then
16                 echo "POLICY_AGENT_LOCAL_IMAGE not set in test_env"
17                 exit 1
18         fi
19         POLICY_AGENT_IMAGE=$POLICY_AGENT_LOCAL_IMAGE":"$IMAGE_TAG
20 fi
21
22 # Set a description string for the test case
23 if [ -z "$TC_ONELINE_DESCR" ]; then
24         TC_ONELINE_DESCR="<no-description>"
25         echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
26 fi
27
28 ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
29
30
31 # Create the logs dir if not already created in the current dir
32 if [ ! -d "logs" ]; then
33     mkdir logs
34 fi
35
36 TESTLOGS=$PWD/logs
37
38 mkdir -p $TESTLOGS/$ATC
39
40 TCLOG=$TESTLOGS/$ATC/TC.log
41 exec &>  >(tee ${TCLOG})
42
43 #Variables for counting tests as well as passed and failed tests
44 RES_TEST=0
45 RES_PASS=0
46 RES_FAIL=0
47 TCTEST_START=$SECONDS
48
49 echo "-------------------------------------------------------------------------------------------------"
50 echo "-----------------------------------      Test case: "$ATC
51 echo "-----------------------------------      Started:   "$(date)
52 echo "-------------------------------------------------------------------------------------------------"
53 echo "-- Description: "$TC_ONELINE_DESCR
54 echo "-------------------------------------------------------------------------------------------------"
55 echo "-----------------------------------      Test case setup      -----------------------------------"
56
57
58 if [ -z "$SIM_GROUP" ]; then
59                 SIM_GROUP=$PWD/../simulator-group
60                 if [ ! -d  $SIM_GROUP ]; then
61                         echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the integration repo, but failed."
62                         echo "Please set the SIM_GROUP manually in the test_env.sh"
63                         exit 1
64                 else
65                         echo "SIM_GROUP auto set to: " $SIM_GROUP
66                 fi
67 elif [ $SIM_GROUP = *simulator_group ]; then
68                         echo "Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the integration repo, check test_env.sh"
69                         exit 1
70 fi
71
72 echo ""
73
74 if [ $1 !=  "manual-container" ] && [ $1 !=  "manual-app" ]; then
75         echo -e "Policy agent image tag set to: \033[1m" $IMAGE_TAG"\033[0m"
76         echo "Configured image for policy agent app(s) (${1}): "$POLICY_AGENT_LOCAL_IMAGE
77         tmp_im=$(docker images ${POLICY_AGENT_LOCAL_IMAGE} | grep -v REPOSITORY)
78
79         if [ $1 == "local" ]; then
80                 if [ -z "$tmp_im" ]; then
81                         echo "Local image (non nexus) "$POLICY_AGENT_LOCAL_IMAGE" does not exist in local registry, need to be built"
82                         exit 1
83                 else
84                         echo -e "Policy agent local image: \033[1m"$tmp_im"\033[0m"
85                         echo "If the policy agen image seem outdated, rebuild the image and run the test again."
86                 fi
87         fi
88 fi
89
90
91
92 __consul_config() {
93
94         appname=$PA_APP_BASE
95
96         echo "Configuring consul for " $appname " from " $1
97         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
98 }
99
100
101 consul_config_app() {
102
103     __consul_config $1
104
105 }
106
107 # Start all simulators in the simulator group
108 start_simulators() {
109
110         echo "Starting all simulators"
111         curdir=$PWD
112         cd $SIM_GROUP
113         $SIM_GROUP/simulators-start.sh
114         cd $curdir
115         echo ""
116 }
117
118 clean_containers() {
119         echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'policy_agent'"
120         docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null
121         echo "Removing all containers, policy agent app and simulators with name prefix 'policy_agent'"
122         docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null
123         echo "Removing unused docker networks with substring 'policy agent' in network name"
124         docker network rm $(docker network ls -q --filter name=nonrtric)
125         echo ""
126 }
127
128 start_policy_agent() {
129
130         appname=$PA_APP_BASE
131
132         if [ $START_ARG == "local" ] ; then
133                 __start_policy_agent_image $appname
134         fi
135 }
136
137 __start_policy_agent_image() {
138
139         appname=$1
140         localport=$POLICY_AGENT_PORT
141
142         echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
143
144         docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
145
146         echo "Starting policy agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME
147         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
148         #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
149         sleep 3
150         set +x
151         pa_started=false
152         for i in {1..10}; do
153                 if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
154                         then
155                                 echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
156                                 echo "Policy Agent container ${appname} running"
157                                 pa_started=true
158                                 break
159                         else
160                                 sleep $i
161                 fi
162         done
163         if ! [ $pa_started  ]; then
164                 echo "Policy Agent container ${appname} could not be started"
165                 exit 1
166         fi
167
168         pa_st=false
169         echo "Waiting for Policy Agent ${appname} service status..."
170         for i in {1..10}; do
171                 result="$(__do_curl http://127.0.0.1:${localport}/status)"
172                 if [ $? -eq 0 ]; then
173                         echo "Policy Agent ${appname} responds to service status: " $result
174                         pa_st=true
175                         break
176                 else
177                         sleep $i
178                 fi
179         done
180
181         if [ "$pa_st" = "false"  ]; then
182                 echo "Policy Agent ${appname} did not respond to service status"
183                 exit 1
184         fi
185 }
186
187 check_policy_agent_logs() {
188
189                 appname=$PA_APP_BASE
190                 tmp=$(docker ps | grep $appname)
191                 if ! [ -z "$tmp" ]; then  #Only check logs for running policy agent apps
192                         __check_policy_agent_log $appname
193                 fi
194
195 }
196
197 __check_policy_agent_log() {
198         echo "Checking $1 log $POLICY_AGENT_LOGPATH for WARNINGs and ERRORs"
199         foundentries=$(docker exec -it $1 grep WARN /var/log/policy-agent/application.log | wc -l)
200         if [ $? -ne  0 ];then
201                 echo "  Problem to search $1 log $POLICY_AGENT_LOGPATH"
202         else
203                 if [ $foundentries -eq 0 ]; then
204                         echo "  No WARN entries found in $1 log $POLICY_AGENT_LOGPATH"
205                 else
206                         echo -e "  Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $POLICY_AGENT_LOGPATH"
207                 fi
208         fi
209         foundentries=$(docker exec -it $1 grep ERR $POLICY_AGENT_LOGPATH | wc -l)
210         if [ $? -ne  0 ];then
211                 echo "  Problem to search $1 log $POLICY_AGENT_LOGPATH"
212         else
213                 if [ $foundentries -eq 0 ]; then
214                         echo "  No ERR entries found in $1 log $POLICY_AGENT_LOGPATH"
215                 else
216                         echo -e "  Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $POLICY_AGENT_LOGPATH"
217                 fi
218         fi
219 }
220
221 store_logs() {
222         if [ $# != 1 ]; then
223         __print_err "need one arg, <file-prefix>"
224                 exit 1
225         fi
226         echo "Storing all container logs and policy agent app log using prefix: "$1
227
228         docker logs polman_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1
229         docker logs polman_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1
230 }
231
232 __do_curl() {
233         res=$(curl -skw "%{http_code}" $1)
234         http_code="${res:${#res}-3}"
235         if [ ${#res} -eq 3 ]; then
236                 echo "<no-response-from-server>"
237                 return 1
238         else
239                 if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then
240                         echo "<not found, resp:${http_code}>"
241                         return 1
242                 fi
243                 if [ $# -eq 2 ]; then
244                         echo "${res:0:${#res}-3}" | xargs
245                 else
246                         echo "${res:0:${#res}-3}"
247                 fi
248
249                 return 0
250         fi
251 }
252