Auto test created for policy agent, cbs, consul 39/2039/2
authorYongchaoWu <yongchao.wu@est.tech>
Mon, 16 Dec 2019 21:54:11 +0000 (22:54 +0100)
committeryongchao <yongchao.wu@est.tech>
Tue, 17 Dec 2019 09:17:42 +0000 (10:17 +0100)
-Fix PA dockerfile using java command of adoptopenjdk
-Add initial test for autotest
-dockerize the process of PA reading config from consul/cbs

Issue-ID: NONRTRIC-79
Change-Id: I185f7459fbb59c2be1507b33e44f7d539ab4eeff
Signed-off-by: YongchaoWu <yongchao.wu@est.tech>
12 files changed:
near-rt-ric-simulator/auto-test/.gitignore [new file with mode: 0644]
near-rt-ric-simulator/auto-test/FTC1.sh [new file with mode: 0755]
near-rt-ric-simulator/common/test_env.sh [new file with mode: 0755]
near-rt-ric-simulator/common/testcase_common.sh [new file with mode: 0755]
near-rt-ric-simulator/simulator-group/consul_cbs/config.json [moved from near-rt-ric-simulator/consul_cbs/config.json with 100% similarity]
near-rt-ric-simulator/simulator-group/consul_cbs/consul/cbs_config.hcl [new file with mode: 0644]
near-rt-ric-simulator/simulator-group/consul_cbs/consul/cbs_localhost_config.hcl [moved from near-rt-ric-simulator/consul_cbs/consul/cbs_localhost_config.hcl with 67% similarity]
near-rt-ric-simulator/simulator-group/consul_cbs/docker-compose-template.yml [moved from near-rt-ric-simulator/consul_cbs/docker-compose-template.yml with 100% similarity]
near-rt-ric-simulator/simulator-group/consul_cbs/docker-compose.yml [moved from near-rt-ric-simulator/consul_cbs/docker-compose.yml with 84% similarity]
near-rt-ric-simulator/simulator-group/consul_cbs/start.sh [moved from near-rt-ric-simulator/consul_cbs/start.sh with 100% similarity]
near-rt-ric-simulator/simulator-group/simulators-start.sh [new file with mode: 0755]
policy-agent/Dockerfile

diff --git a/near-rt-ric-simulator/auto-test/.gitignore b/near-rt-ric-simulator/auto-test/.gitignore
new file mode 100644 (file)
index 0000000..1c5ae4e
--- /dev/null
@@ -0,0 +1,2 @@
+logs
+.tmp_*
\ No newline at end of file
diff --git a/near-rt-ric-simulator/auto-test/FTC1.sh b/near-rt-ric-simulator/auto-test/FTC1.sh
new file mode 100755 (executable)
index 0000000..180682b
--- /dev/null
@@ -0,0 +1,20 @@
+#!/usr/bin/env bash
+
+TC_ONELINE_DESCR="Auto test for policy agent with simulator"
+
+. ../common/testcase_common.sh $1 $2
+
+clean_containers
+
+start_simulators
+
+consul_config_app         "../simulator-group/consul_cbs/config.json"
+
+start_policy_agent
+
+check_policy_agent_logs
+
+#### TEST COMPLETE ####
+
+store_logs          END
+
diff --git a/near-rt-ric-simulator/common/test_env.sh b/near-rt-ric-simulator/common/test_env.sh
new file mode 100755 (executable)
index 0000000..7861940
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+
+# Local image and tag, shall point to locally built image (non-nexus path)
+export POLICY_AGENT_LOCAL_IMAGE=oransc/policy-agent
+
+
+# Common env var for auto-test.
+
+POLICY_AGENT_PORT=8081
+POLICY_AGENT_LOGPATH="/var/log/policy-agent/application.log"  #Path the application log in the policy agent container
+DOCKER_SIM_NWNAME="nonrtric-docker-net"             #Name of docker private network
+CONSUL_HOST="consul-server"            #Host name of consul
+CONSUL_PORT=8500                       #Port number of consul
+CONFIG_BINDING_SERVICE="config-binding-service"  #Host name of CBS
+PA_APP_BASE="policy-agent"
diff --git a/near-rt-ric-simulator/common/testcase_common.sh b/near-rt-ric-simulator/common/testcase_common.sh
new file mode 100755 (executable)
index 0000000..6f2097e
--- /dev/null
@@ -0,0 +1,252 @@
+#!/usr/bin/env bash
+
+. ../common/test_env.sh
+
+echo "Test case started as: ${BASH_SOURCE[$i+1]} "$1 $2
+
+STARTED_POLICY_AGENT="" #Policy agent app names added to this var to keep track of started container in the script
+START_ARG=$1
+IMAGE_TAG="1.0.0-SNAPSHOT"
+
+if [ $# -lt 1 ] || [ $# -gt 2 ]; then
+       echo "Expected arg: local [<image-tag>] ]| remote [<image-tag>] ]| remote-remove [<image-tag>]] | manual-container | manual-app"
+       exit 1
+elif [ $1 == "local" ]; then
+       if [ -z $POLICY_AGENT_LOCAL_IMAGE ]; then
+               echo "POLICY_AGENT_LOCAL_IMAGE not set in test_env"
+               exit 1
+       fi
+       POLICY_AGENT_IMAGE=$POLICY_AGENT_LOCAL_IMAGE":"$IMAGE_TAG
+fi
+
+# Set a description string for the test case
+if [ -z "$TC_ONELINE_DESCR" ]; then
+       TC_ONELINE_DESCR="<no-description>"
+       echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
+fi
+
+ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
+
+
+# Create the logs dir if not already created in the current dir
+if [ ! -d "logs" ]; then
+    mkdir logs
+fi
+
+TESTLOGS=$PWD/logs
+
+mkdir -p $TESTLOGS/$ATC
+
+TCLOG=$TESTLOGS/$ATC/TC.log
+exec &>  >(tee ${TCLOG})
+
+#Variables for counting tests as well as passed and failed tests
+RES_TEST=0
+RES_PASS=0
+RES_FAIL=0
+TCTEST_START=$SECONDS
+
+echo "-------------------------------------------------------------------------------------------------"
+echo "-----------------------------------      Test case: "$ATC
+echo "-----------------------------------      Started:   "$(date)
+echo "-------------------------------------------------------------------------------------------------"
+echo "-- Description: "$TC_ONELINE_DESCR
+echo "-------------------------------------------------------------------------------------------------"
+echo "-----------------------------------      Test case setup      -----------------------------------"
+
+
+if [ -z "$SIM_GROUP" ]; then
+               SIM_GROUP=$PWD/../simulator-group
+               if [ ! -d  $SIM_GROUP ]; then
+                       echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the integration repo, but failed."
+                       echo "Please set the SIM_GROUP manually in the test_env.sh"
+                       exit 1
+               else
+                       echo "SIM_GROUP auto set to: " $SIM_GROUP
+               fi
+elif [ $SIM_GROUP = *simulator_group ]; then
+                       echo "Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the integration repo, check test_env.sh"
+                       exit 1
+fi
+
+echo ""
+
+if [ $1 !=  "manual-container" ] && [ $1 !=  "manual-app" ]; then
+       echo -e "Policy agent image tag set to: \033[1m" $IMAGE_TAG"\033[0m"
+       echo "Configured image for policy agent app(s) (${1}): "$POLICY_AGENT_LOCAL_IMAGE
+       tmp_im=$(docker images ${POLICY_AGENT_LOCAL_IMAGE} | grep -v REPOSITORY)
+
+       if [ $1 == "local" ]; then
+               if [ -z "$tmp_im" ]; then
+                       echo "Local image (non nexus) "$POLICY_AGENT_LOCAL_IMAGE" does not exist in local registry, need to be built"
+                       exit 1
+               else
+                       echo -e "Policy agent local image: \033[1m"$tmp_im"\033[0m"
+                       echo "If the policy agen image seem outdated, rebuild the image and run the test again."
+               fi
+       fi
+fi
+
+
+
+__consul_config() {
+
+       appname=$PA_APP_BASE
+
+       echo "Configuring consul for " $appname " from " $1
+       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
+}
+
+
+consul_config_app() {
+
+    __consul_config $1
+
+}
+
+# Start all simulators in the simulator group
+start_simulators() {
+
+       echo "Starting all simulators"
+       curdir=$PWD
+       cd $SIM_GROUP
+       $SIM_GROUP/simulators-start.sh
+       cd $curdir
+       echo ""
+}
+
+clean_containers() {
+       echo "Stopping all containers, policy agent app(s) and simulators with name prefix 'policy_agent'"
+       docker stop $(docker ps -q --filter name=/policy-agent) &> /dev/null
+       echo "Removing all containers, policy agent app and simulators with name prefix 'policy_agent'"
+       docker rm $(docker ps -a -q --filter name=/policy-agent) &> /dev/null
+       echo "Removing unused docker networks with substring 'policy agent' in network name"
+       docker network rm $(docker network ls -q --filter name=nonrtric)
+       echo ""
+}
+
+start_policy_agent() {
+
+       appname=$PA_APP_BASE
+
+       if [ $START_ARG == "local" ] ; then
+               __start_policy_agent_image $appname
+       fi
+}
+
+__start_policy_agent_image() {
+
+       appname=$1
+       localport=$POLICY_AGENT_PORT
+
+       echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
+
+       docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
+
+       echo "Starting policy agent: " $appname " with ports mapped to " $localport " in docker network "$DOCKER_SIM_NWNAME
+       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
+       #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
+       sleep 3
+       set +x
+       pa_started=false
+       for i in {1..10}; do
+               if [ $(docker inspect --format '{{ .State.Running }}' $appname) ]
+                       then
+                               echo " Image: $(docker inspect --format '{{ .Config.Image }}' ${appname})"
+                               echo "Policy Agent container ${appname} running"
+                               pa_started=true
+                               break
+                       else
+                               sleep $i
+               fi
+       done
+       if ! [ $pa_started  ]; then
+               echo "Policy Agent container ${appname} could not be started"
+               exit 1
+       fi
+
+       pa_st=false
+       echo "Waiting for Policy Agent ${appname} service status..."
+       for i in {1..10}; do
+               result="$(__do_curl http://127.0.0.1:${localport}/status)"
+               if [ $? -eq 0 ]; then
+                       echo "Policy Agent ${appname} responds to service status: " $result
+                       pa_st=true
+                       break
+               else
+                       sleep $i
+               fi
+       done
+
+       if [ "$pa_st" = "false"  ]; then
+               echo "Policy Agent ${appname} did not respond to service status"
+               exit 1
+       fi
+}
+
+check_policy_agent_logs() {
+
+               appname=$PA_APP_BASE
+               tmp=$(docker ps | grep $appname)
+               if ! [ -z "$tmp" ]; then  #Only check logs for running policy agent apps
+                       __check_policy_agent_log $appname
+               fi
+
+}
+
+__check_policy_agent_log() {
+       echo "Checking $1 log $POLICY_AGENT_LOGPATH for WARNINGs and ERRORs"
+       foundentries=$(docker exec -it $1 grep WARN /var/log/policy-agent/application.log | wc -l)
+       if [ $? -ne  0 ];then
+               echo "  Problem to search $1 log $POLICY_AGENT_LOGPATH"
+       else
+               if [ $foundentries -eq 0 ]; then
+                       echo "  No WARN entries found in $1 log $POLICY_AGENT_LOGPATH"
+               else
+                       echo -e "  Found \033[1m"$foundentries"\033[0m WARN entries in $1 log $POLICY_AGENT_LOGPATH"
+               fi
+       fi
+       foundentries=$(docker exec -it $1 grep ERR $POLICY_AGENT_LOGPATH | wc -l)
+       if [ $? -ne  0 ];then
+               echo "  Problem to search $1 log $POLICY_AGENT_LOGPATH"
+       else
+               if [ $foundentries -eq 0 ]; then
+                       echo "  No ERR entries found in $1 log $POLICY_AGENT_LOGPATH"
+               else
+                       echo -e "  Found \033[1m"$foundentries"\033[0m ERR entries in $1 log $POLICY_AGENT_LOGPATH"
+               fi
+       fi
+}
+
+store_logs() {
+       if [ $# != 1 ]; then
+       __print_err "need one arg, <file-prefix>"
+               exit 1
+       fi
+       echo "Storing all container logs and policy agent app log using prefix: "$1
+
+       docker logs polman_consul > $TESTLOGS/$ATC/$1_consul.log 2>&1
+       docker logs polman_cbs > $TESTLOGS/$ATC/$1_cbs.log 2>&1
+}
+
+__do_curl() {
+       res=$(curl -skw "%{http_code}" $1)
+       http_code="${res:${#res}-3}"
+       if [ ${#res} -eq 3 ]; then
+               echo "<no-response-from-server>"
+               return 1
+       else
+               if [ $http_code -lt 200 ] && [ $http_code -gt 299]; then
+                       echo "<not found, resp:${http_code}>"
+                       return 1
+               fi
+               if [ $# -eq 2 ]; then
+                       echo "${res:0:${#res}-3}" | xargs
+               else
+                       echo "${res:0:${#res}-3}"
+               fi
+
+               return 0
+       fi
+}
+
diff --git a/near-rt-ric-simulator/simulator-group/consul_cbs/consul/cbs_config.hcl b/near-rt-ric-simulator/simulator-group/consul_cbs/consul/cbs_config.hcl
new file mode 100644 (file)
index 0000000..9a9c0c4
--- /dev/null
@@ -0,0 +1,13 @@
+#server = true
+#bootstrap = true
+#client_addr = "0.0.0.0"
+
+service  {
+  # Name for CBS in consul, env var CONFIG_BINDING_SERVICE
+  # should be passed to policy agent app with this value
+  Name = "config-binding-service"
+  # Host name where CBS is running
+  Address = "config-binding-service"
+  # Port number where CBS is running
+  Port = 10000
+}
\ No newline at end of file
@@ -1,8 +1,8 @@
 service {
   # Name for CBS in consul, env var CONFIG_BINDING_SERVICE
-  # should be passed to dfc app with this value
+  # should be passed to policy agent app with this value
   # This is only to be used when contacting cbs via local host
-  # (typicall when dfc is executed as an application without a container)
+  # (typicall when policy agent is executed as an application without a container)
   Name = "config-binding-service-localhost"
   # Host name where CBS is running
   Address = "localhost"
@@ -22,6 +22,6 @@ services:
     ports:
     - 8500:8500/tcp
     volumes:
-    - /Users/yonwu/ORAN/nonrtric/near-rt-ric-simulator/consul_cbs/consul:/consul/config:rw
+    - /Users/yonwu/ORAN/nonrtric/near-rt-ric-simulator/simulator-group/consul_cbs/consul:/consul/config:rw
 version: '3.0'
 
diff --git a/near-rt-ric-simulator/simulator-group/simulators-start.sh b/near-rt-ric-simulator/simulator-group/simulators-start.sh
new file mode 100755 (executable)
index 0000000..7f83397
--- /dev/null
@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+
+DOCKER_SIM_NWNAME="nonrtric-docker-net"
+echo "Creating docker network $DOCKER_SIM_NWNAME, if needed"
+docker network ls| grep $DOCKER_SIM_NWNAME > /dev/null || docker network create $DOCKER_SIM_NWNAME
+
+docker-compose -f consul_cbs/docker-compose-template.yml config > docker-compose.yml
+
+docker-compose up -d
\ No newline at end of file
index 7119f07..51a4dd3 100644 (file)
@@ -23,6 +23,7 @@ WORKDIR /opt/app/policy-agent
 RUN mkdir -p /var/log/policy-agent
 RUN mkdir -p /opt/app/policy-agent/etc/cert/
 
+EXPOSE 8081
 
 ADD /config/application.yaml /opt/app/policy-agent/config/
 ADD /target/policy-agent-1.0.0-SNAPSHOT.jar /opt/app/policy-agent/
@@ -30,6 +31,8 @@ ADD /target/policy-agent-1.0.0-SNAPSHOT.jar /opt/app/policy-agent/
 
 RUN chmod -R 777 /opt/app/policy-agent/config/
 
-ENTRYPOINT ["/usr/bin/java", "-jar", "/opt/app/policy-agent/policy-agent-1.0.0-SNAPSHOT.jar"]
+CMD ["java", "-jar", "/opt/app/policy-agent/policy-agent-1.0.0-SNAPSHOT.jar"]
+
+