-# 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
- echo -e $RED" Stopping script..."$ERED
- exit 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...$(($SECONDS-$TSTART)) seconds, 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 in $(($SECONDS-$TSTART)) seconds"$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
- echo -e $RED"Stopping script...."$ERED
- exit 1
- 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
- echo -e $RED"Stopping script...."$ERED
- exit 1
- 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
- fi
-
- echo ""
-
-}
-
-# Function to perpare the consul configuration according to the current simulator configuration
-# args: SDNC|NOSDNC <output-file>
-# (Function for test scripts)
-prepare_consul_config() {
- echo -e $BOLD"Prepare Consul config"$EBOLD
-
- echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
-
- if [ $# != 2 ]; then