- exit 1
- fi
- ((RES_DEVIATION++))
- echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
- echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
- echo ""
-}
-
-# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
-__check_stop_at_error() {
- if [ $STOP_AT_ERROR -eq 1 ]; then
- echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
- store_logs "STOP_AT_ERROR"
- exit 1
- fi
- return 0
-}
-
-# Check if app name var is set. If so return the app name otherwise return "NOTSET"
-__check_app_name() {
- if [ $# -eq 1 ]; then
- echo $1
- else
- echo "NOTSET"
- fi
-}
-
-# Stop and remove all containers
-# args: -
-# (Function for test scripts)
-clean_containers() {
-
- echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
-
- CONTAINTER_NAMES=("Policy Agent " $(__check_app_name $POLICY_AGENT_APP_NAME)\
- "ECS " $(__check_app_name $ECS_APP_NAME)\
- "Non-RT RIC Simulator(s)" $(__check_app_name $RIC_SIM_PREFIX)\
- "Message Router " $(__check_app_name $MR_APP_NAME)\
- "Callback Receiver " $(__check_app_name $CR_APP_NAME)\
- "Producer stub " $(__check_app_name $PROD_STUB_APP_NAME)\
- "Control Panel " $(__check_app_name $CONTROL_PANEL_APP_NAME)\
- "SDNC A1 Controller " $(__check_app_name $SDNC_APP_NAME)\
- "SDNC DB " $(__check_app_name $SDNC_DB_APP_NAME)\
- "CBS " $(__check_app_name $CBS_APP_NAME)\
- "Consul " $(__check_app_name $CONSUL_APP_NAME))
-
- nw=0 # Calc max width of container name, to make a nice table
- for (( i=1; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
-
- if [ ${#CONTAINTER_NAMES[i]} -gt $nw ]; then
- nw=${#CONTAINTER_NAMES[i]}
- fi
- done
-
- for (( i=0; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
- APP="${CONTAINTER_NAMES[i]}"
- CONTR="${CONTAINTER_NAMES[i+1]}"
- if [ $CONTR != "NOTSET" ]; then
- for((w=${#CONTR}; w<$nw; w=w+1)); do
- CONTR="$CONTR "
- done
- echo -ne " $APP: $CONTR - ${GREEN}stopping${EGREEN}${SAMELINE}"
- docker stop $(docker ps -qa --filter name=${CONTR}) &> /dev/null
- echo -ne " $APP: $CONTR - ${GREEN}stopped${EGREEN}${SAMELINE}"
- docker rm --force $(docker ps -qa --filter name=${CONTR}) &> /dev/null
- echo -e " $APP: $CONTR - ${GREEN}stopped removed${EGREEN}"
- fi
- done
-
- echo ""
-
- echo -e $BOLD" Removing docker network"$EBOLD
- TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
- if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
- docker network rm $DOCKER_SIM_NWNAME | indent2
- if [ $? -ne 0 ]; then
- echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
- exit 1
- fi
- fi
- echo -e "$GREEN Done$EGREEN"
-
- echo -e $BOLD" Removing all unused docker neworks"$EBOLD
- docker network prune --force | indent2
- echo -e "$GREEN Done$EGREEN"
-
- echo -e $BOLD" Removing all unused docker volumes"$EBOLD
- docker volume prune --force | indent2
- echo -e "$GREEN Done$EGREEN"
-
- echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
- docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
- echo -e "$GREEN Done$EGREEN"
- echo ""
-
- CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
- if [ $? -eq 0 ]; then
- if [ $CONTRS -ne 0 ]; then
- echo -e $RED"Containers running, may cause distubance to the test case"$ERED
- docker ps -a
- fi
- fi
-}
-
-# Function stop and remove all container in the end of the test script, if the arg 'auto-clean' is given at test script start
-# args: -
-# (Function for test scripts)
-auto_clean_containers() {
- echo
- if [ "$AUTO_CLEAN" == "auto" ]; then
- echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
- clean_containers
- fi
-}
-
-# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
-# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
-# (Function for test scripts)
-sleep_wait() {
-
- echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
- if [ $# -lt 1 ]; then
- ((RES_CONF_FAIL++))
- __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
- exit 1
- fi
- #echo "---- Sleep for " $1 " seconds ---- "$2
- start=$SECONDS
- duration=$((SECONDS-start))
- while [ $duration -lt $1 ]; do
- echo -ne " Slept for ${duration} seconds${SAMELINE}"
- sleep 1
- duration=$((SECONDS-start))
- done
- echo -ne " Slept for ${duration} seconds${SAMELINE}"
- echo ""
-}
-
-# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
-# Not to be called from the test script itself.
-__print_err() {
- echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
- if [ $# -gt 1 ]; then
- echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
- fi
-}
-
-
-# Helper function to get a the port of a specific ric simulatpor
-# args: <ric-id>
-# (Not for test scripts)
-__find_sim_port() {
- name=$1" " #Space appended to prevent matching 10 if 1 is desired....
- cmdstr="docker inspect --format='{{(index (index .NetworkSettings.Ports \"$RIC_SIM_PORT/tcp\") 0).HostPort}}' ${name}"
- res=$(eval $cmdstr)
- if [[ "$res" =~ ^[0-9]+$ ]]; then
- echo $res
- else
- echo "0"
- fi
-}
-
-# Function to create the docker network for the test
-# Not to be called from the test script itself.
-__create_docker_network() {
- tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
- if [ $? -ne 0 ]; then
- echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
- return 1
- fi
- if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
- echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
- docker network create $DOCKER_SIM_NWNAME | indent2
- if [ $? -ne 0 ]; then
- echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
- return 1
- else
- echo -e "$GREEN Done$EGREEN"
- fi
- else
- echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
- fi
-}
-
-# Check if container is started by calling url on localhost using a port, expects response code 2XX
-# args: <container-name> <port> <url> https|https
-# Not to be called from the test script itself.
-__check_container_start() {
- paramError=0
- if [ $# -ne 4 ]; then
- paramError=1
- elif [ $4 != "http" ] && [ $4 != "https" ]; then
- paramError=1
- fi
- if [ $paramError -ne 0 ]; then
- ((RES_CONF_FAIL++))
- __print_err "need 3 args, <container-name> <port> <url> https|https" $@
- return 1
- fi
- echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
- appname=$1
- localport=$2
- url=$3
- if [[ $appname != "STANDALONE_"* ]] ; then
- app_started=0
- for i in {1..10}; do
- if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
- echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
- app_started=1
- break
- else
- sleep $i
- fi
- done
- if [ $app_started -eq 0 ]; then
- ((RES_CONF_FAIL++))
- echo ""
- echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
- return 1
- fi
- if [ $localport -eq 0 ]; then
- while [ $localport -eq 0 ]; do
- echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
- localport=$(__find_sim_port $appname)
- sleep 1
- echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
- done
- echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
- echo ""
- fi
- fi
-
- pa_st=false
- echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
- TSTART=$SECONDS
- for i in {1..50}; do
- if [ $4 == "https" ]; then
- result="$(__do_curl "-k https://localhost:"${localport}${url})"
- else
- result="$(__do_curl $LOCALHOST${localport}${url})"
- fi
- if [ $? -eq 0 ]; then
- if [ ${#result} -gt 15 ]; then
- #If response is too long, truncate
- result="...response text too long, omitted"
- fi
- echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
- echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN after $(($SECONDS-$TSTART)) seconds"
- pa_st=true
- break
- else
- TS_TMP=$SECONDS
- while [ $(($TS_TMP+$i)) -gt $SECONDS ]; do
- echo -ne " Waiting for container ${appname} service status...retrying in $(($TS_TMP+$i-$SECONDS)) seconds ${SAMELINE}"
- sleep 1
- done
- fi
- done
-
- if [ "$pa_st" = "false" ]; then
- ((RES_CONF_FAIL++))
- echo -e $RED" Container ${appname} did not respond to service status"$ERED
- return 0
- fi
-
- echo ""
- return 0
-}
-
-
-# Function to start a container and wait until it responds on the given port and url.
-#args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
-__start_container() {
-
- variableArgCount=$(($#-2))
- if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
- ((RES_CONF_FAIL++))
- __print_err "need 6 or more args, <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> http|https [<app-name> <port-number> <alive-url> http|https ]*" $@
- exit 1
- fi
-
- __create_docker_network
-
- curdir=$PWD
- cd $SIM_GROUP
- cd $1
-
- if [ "$2" == "NODOCKERARGS" ]; then
- docker-compose up -d &> .dockererr
- if [ $? -ne 0 ]; then
- echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
- cat .dockererr
- fi
- elif [ "$2" == "STANDALONE" ]; then
- echo "Skipping docker-compose"
- else
- docker-compose up -d $2 &> .dockererr
- if [ $? -ne 0 ]; then
- echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
- cat .dockererr
- fi
- fi
- app_prefix=""
- if [ "$2" == "STANDALONE" ]; then
- app_prefix="STANDALONE_"
- fi
- shift; shift;
- cntr=0
- while [ $cntr -lt $variableArgCount ]; do
- app=$app_prefix$1; shift;
- port=$1; shift;
- url=$1; shift;
- httpx=$1; shift;
- let cntr=cntr+4
-
- __check_container_start "$app" "$port" "$url" $httpx
- done
-
- cd $curdir
- echo ""
- return 0
-}
-
-# Generate a UUID to use as prefix for policy ids
-generate_uuid() {
- UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)')
- #Reduce length to make space for serial id, us 'a' as marker where the serial id is added
- UUID=${UUID:0:${#UUID}-4}"a"
-}
-
-####################
-### Consul functions
-####################
-
-# Function to load config from a file into consul for the Policy Agent
-# arg: <json-config-file>
-# (Function for test scripts)
-consul_config_app() {
-
- echo -e $BOLD"Configuring Consul"$EBOLD
-
- if [ $# -ne 1 ]; then
- ((RES_CONF_FAIL++))
- __print_err "need one arg, <json-config-file>" $@
- exit 1
- fi
-
- echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
-
- curlString="$LOCALHOST${CONSUL_EXTERNAL_PORT}/v1/kv/${POLICY_AGENT_APP_NAME}?dc=dc1 -X PUT -H Accept:application/json -H Content-Type:application/json -H X-Requested-With:XMLHttpRequest --data-binary @"$1
- result=$(__do_curl "$curlString")
- if [ $? -ne 0 ]; then
- echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
- ((RES_CONF_FAIL++))
- return 1
- fi
- body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
- echo $body > "./tmp/.output"$1
-
- if [ $? -ne 0 ]; then
- echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
- ((RES_CONF_FAIL++))
- return 1
- else
- targetJson=$(< $1)
- targetJson="{\"config\":"$targetJson"}"
- echo "TARGET JSON: $targetJson" >> $HTTPLOG
- res=$(python3 ../common/compare_json.py "$targetJson" "$body")
- if [ $res -ne 0 ]; then
- echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
- ((RES_CONF_FAIL++))
- return 1
- else
- echo -e $GREEN" Config loaded ok to consul"$EGREEN
- fi