elinuxhenrik | b033eaf | 2020-02-03 16:02:21 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . ../common/test_env.sh |
| 4 | |
| 5 | echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 |
| 6 | |
| 7 | # This is a script that contains all the functions needed for auto test |
| 8 | # Arg: local | remote |
| 9 | |
| 10 | STARTED_POLICY_AGENT="" #Policy agent app names added to this var to keep track of started container in the script |
| 11 | START_ARG=$1 |
| 12 | IMAGE_TAG="1.0.0-SNAPSHOT" |
| 13 | IMAGE_TAG_REMOTE="1.0.0" |
| 14 | |
| 15 | if [ $# -lt 1 ] || [ $# -gt 2 ]; then |
| 16 | echo "Expected arg: local | remote " |
| 17 | exit 1 |
| 18 | elif [ $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 |
| 24 | elif [ $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 |
| 30 | fi |
| 31 | |
| 32 | # Set a description string for the test case |
| 33 | if [ -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 |
| 36 | fi |
| 37 | |
| 38 | ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh) |
| 39 | |
| 40 | |
| 41 | # Create the logs dir if not already created in the current dir |
| 42 | if [ ! -d "logs" ]; then |
| 43 | mkdir logs |
| 44 | fi |
| 45 | |
| 46 | TESTLOGS=$PWD/logs |
| 47 | |
| 48 | mkdir -p $TESTLOGS/$ATC |
| 49 | |
| 50 | TCLOG=$TESTLOGS/$ATC/TC.log |
| 51 | exec &> >(tee ${TCLOG}) |
| 52 | |
| 53 | #Variables for counting tests as well as passed and failed tests |
| 54 | RES_TEST=0 |
| 55 | RES_PASS=0 |
| 56 | RES_FAIL=0 |
| 57 | TCTEST_START=$SECONDS |
| 58 | |
| 59 | echo "-------------------------------------------------------------------------------------------------" |
| 60 | echo "----------------------------------- Test case: "$ATC |
| 61 | echo "----------------------------------- Started: "$(date) |
| 62 | echo "-------------------------------------------------------------------------------------------------" |
| 63 | echo "-- Description: "$TC_ONELINE_DESCR |
| 64 | echo "-------------------------------------------------------------------------------------------------" |
| 65 | echo "----------------------------------- Test case setup -----------------------------------" |
| 66 | |
| 67 | |
| 68 | if [ -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 |
| 77 | elif [ $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 |
| 80 | fi |
| 81 | |
| 82 | echo "" |
| 83 | |
| 84 | if [ $1 != "manual-container" ] && [ $1 != "manual-app" ]; then |
| 85 | #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) |
| 88 | |
| 89 | if [ $1 == "local" ]; then |
| 90 | if [ -z "$tmp_im" ]; then |
| 91 | echo "Local image (non nexus) "$POLICY_AGENT_IMAGE" does not exist in local registry, need to be built" |
| 92 | exit 1 |
| 93 | else |
| 94 | echo -e "Policy agent local image: \033[1m"$tmp_im"\033[0m" |
| 95 | 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." |
| 110 | fi |
| 111 | fi |
| 112 | fi |
| 113 | |
| 114 | echo "" |
| 115 | |
| 116 | echo "Building images for the simulators" |
| 117 | curdir=$PWD |
| 118 | cd $SIM_GROUP |
| 119 | cd ../ric-plt/a1 |
| 120 | docker build -t ric-simulator:latest . &> /dev/null |
| 121 | cd $curdir |
| 122 | |
| 123 | echo "" |
| 124 | |
| 125 | echo "Local registry images for simulators:" |
| 126 | echo "Consul: " $(docker images | grep consul) |
| 127 | echo "CBS: " $(docker images | grep platform.configbinding.app) |
| 128 | echo "RIC: " $(docker images | grep ric-simulator) |
| 129 | echo "" |
| 130 | |
| 131 | |
| 132 | __consul_config() { |
| 133 | |
| 134 | appname=$PA_APP_BASE |
| 135 | |
| 136 | echo "Configuring consul for " $appname " from " $1 |
| 137 | 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 |
| 138 | } |
| 139 | |
| 140 | |
| 141 | consul_config_app() { |
| 142 | |
| 143 | __consul_config $1 |
| 144 | |
| 145 | } |
| 146 | |
| 147 | # Start all simulators in the simulator group |
| 148 | start_simulators() { |
| 149 | |
| 150 | echo "Starting all simulators" |
| 151 | curdir=$PWD |
| 152 | cd $SIM_GROUP |
| 153 | $SIM_GROUP/simulators-start.sh |
| 154 | cd $curdir |
| 155 | echo "" |
| 156 | } |
| 157 | |
| 158 | clean_containers() { |
| 159 | echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'policy_agent'" |
| 160 | docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null |
| 161 | echo "Removing all containers, policy agent app and simulators with name prefix 'policy_agent'" |
| 162 | docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null |
| 163 | echo "Removing unused docker networks with substring 'policy agent' in network name" |
| 164 | docker network rm $(docker network ls -q --filter name=nonrtric) |
| 165 | echo "" |
| 166 | } |
| 167 | |
| 168 | start_policy_agent() { |
| 169 | |
| 170 | appname=$PA_APP_BASE |
| 171 | |
| 172 | if [ $START_ARG == "local" ] || [ $START_ARG == "remote" ]; then |
| 173 | __start_policy_agent_image $appname |
| 174 | fi |
| 175 | } |
| 176 | |
| 177 | __start_policy_agent_image() { |
| 178 | |
| 179 | appname=$1 |
| 180 | localport=$POLICY_AGENT_PORT |
| 181 | |
| 182 | echo "Creating docker network $DOCKER_SIM_NWNAME, if needed" |
| 183 | |
| 184 | docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME |
| 185 | |
| 186 | echo "Starting policy agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME |
| 187 | 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 |
| 188 | #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 |
| 189 | sleep 3 |
| 190 | set +x |
| 191 | pa_started=false |
| 192 | for i in {1..10}; do |
| 193 | if [ $(docker inspect --format '{{ .State.Running }}' $appname) ] |
| 194 | then |
| 195 | echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})" |
| 196 | echo "Policy Agent container ${appname} running" |
| 197 | pa_started=true |
| 198 | break |
| 199 | else |
| 200 | sleep $i |
| 201 | fi |
| 202 | done |
| 203 | if ! [ $pa_started ]; then |
| 204 | echo "Policy Agent container ${appname} could not be started" |
| 205 | exit 1 |
| 206 | fi |
| 207 | |
| 208 | pa_st=false |
| 209 | echo "Waiting for Policy Agent ${appname} service status..." |
| 210 | for i in {1..10}; do |
| 211 | result="$(__do_curl http://127.0.0.1:${localport}/status)" |
| 212 | if [ $? -eq 0 ]; then |
| 213 | echo "Policy Agent ${appname} responds to service status: " $result |
| 214 | echo "Policy Agent is alive." |
| 215 | pa_st=true |
| 216 | break |
| 217 | else |
| 218 | sleep $i |
| 219 | fi |
| 220 | done |
| 221 | |
| 222 | if [ "$pa_st" = "false" ]; then |
| 223 | echo "Policy Agent ${appname} did not respond to service status" |
| 224 | exit 1 |
| 225 | fi |
| 226 | } |
| 227 | |
| 228 | check_policy_agent_logs() { |
| 229 | |
| 230 | appname=$PA_APP_BASE |
| 231 | tmp=$(docker ps | grep $appname) |
| 232 | if ! [ -z "$tmp" ]; then #Only check logs for running policy agent apps |
| 233 | __check_policy_agent_log $appname |
| 234 | fi |
| 235 | |
| 236 | } |
| 237 | |
| 238 | __check_policy_agent_log() { |
| 239 | echo "Checking $1 log $POLICY_AGENT_LOGPATH for WARNINGs and ERRORs" |
| 240 | foundentries=$(docker exec -it $1 grep WARN /var/log/policy-agent/application.log | wc -l) |
| 241 | if [ $? -ne 0 ];then |
| 242 | echo " Problem to search $1 log $POLICY_AGENT_LOGPATH" |
| 243 | else |
| 244 | if [ $foundentries -eq 0 ]; then |
| 245 | echo " No WARN entries found in $1 log $POLICY_AGENT_LOGPATH" |
| 246 | else |
| 247 | echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $POLICY_AGENT_LOGPATH" |
| 248 | fi |
| 249 | fi |
| 250 | foundentries=$(docker exec -it $1 grep ERR $POLICY_AGENT_LOGPATH | wc -l) |
| 251 | if [ $? -ne 0 ];then |
| 252 | echo " Problem to search $1 log $POLICY_AGENT_LOGPATH" |
| 253 | else |
| 254 | if [ $foundentries -eq 0 ]; then |
| 255 | echo " No ERR entries found in $1 log $POLICY_AGENT_LOGPATH" |
| 256 | else |
| 257 | echo -e " Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $POLICY_AGENT_LOGPATH" |
| 258 | fi |
| 259 | fi |
| 260 | } |
| 261 | |
| 262 | store_logs() { |
| 263 | if [ $# != 1 ]; then |
| 264 | __print_err "need one arg, <file-prefix>" |
| 265 | exit 1 |
| 266 | fi |
| 267 | echo "Storing all container logs and policy agent app log using prefix: "$1 |
| 268 | |
| 269 | docker logs polman_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1 |
| 270 | docker logs polman_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1 |
| 271 | } |
| 272 | |
| 273 | __do_curl() { |
| 274 | res=$(curl -skw "%{http_code}" $1) |
| 275 | http_code="${res:${#res}-3}" |
| 276 | if [ ${#res} -eq 3 ]; then |
| 277 | echo "<no-response-from-server>" |
| 278 | return 1 |
| 279 | else |
| 280 | if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then |
| 281 | echo "<not found, resp:${http_code}>" |
| 282 | return 1 |
| 283 | fi |
| 284 | if [ $# -eq 2 ]; then |
| 285 | echo "${res:0:${#res}-3}" | xargs |
| 286 | else |
| 287 | echo "${res:0:${#res}-3}" |
| 288 | fi |
| 289 | |
| 290 | return 0 |
| 291 | fi |
| 292 | } |
| 293 | |