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