+# Create the image var used during the test
+# arg: <image-tag-suffix> (selects staging, snapshot, release etc)
+# <image-tag-suffix> is present only for images with staging, snapshot,release tags
+__ECS_imagesetup() {
+ __check_and_create_image_var ECS "ECS_IMAGE" "ECS_IMAGE_BASE" "ECS_IMAGE_TAG" $1 "$ECS_DISPLAY_NAME"
+}
+
+# Pull image from remote repo or use locally built image
+# arg: <pull-policy-override> <pull-policy-original>
+# <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
+# <pull-policy-original> Shall be used for images that does not allow overriding
+# Both var may contain: 'remote', 'remote-remove' or 'local'
+__ECS_imagepull() {
+ __check_and_pull_image $1 "$ECS_DISPLAY_NAME" $ECS_APP_NAME ECS_IMAGE
+}
+
+# Build image (only for simulator or interfaces stubs owned by the test environment)
+# arg: <image-tag-suffix> (selects staging, snapshot, release etc)
+# <image-tag-suffix> is present only for images with staging, snapshot,release tags
+__ECS_imagebuild() {
+ echo -e $RED" Image for app ECS shall never be built"$ERED
+}
+
+# Generate a string for each included image using the app display name and a docker images format string
+# If a custom image repo is used then also the source image from the local repo is listed
+# arg: <docker-images-format-string> <file-to-append>
+__ECS_image_data() {
+ echo -e "$ECS_DISPLAY_NAME\t$(docker images --format $1 $ECS_IMAGE)" >> $2
+ if [ ! -z "$ECS_IMAGE_SOURCE" ]; then
+ echo -e "-- source image --\t$(docker images --format $1 $ECS_IMAGE_SOURCE)" >> $2
+ fi
+}
+
+# Scale kubernetes resources to zero
+# All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
+# This function is called for apps fully managed by the test script
+__ECS_kube_scale_zero() {
+ __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ECS
+}
+
+# Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
+# This function is called for prestarted apps not managed by the test script.
+__ECS_kube_scale_zero_and_wait() {
+ __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app nonrtric-enrichmentservice
+}
+
+# Delete all kube resouces for the app
+# This function is called for apps managed by the test script.
+__ECS_kube_delete_all() {
+ __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ECS
+}
+
+# Store docker logs
+# This function is called for apps managed by the test script.
+# args: <log-dir> <file-prexix>
+__ECS_store_docker_logs() {
+ docker logs $ECS_APP_NAME > $1$2_ecs.log 2>&1
+}
+#######################################################
+
+
+## Access to ECS
+# Host name may be changed if app started by kube
+# Direct access
+ECS_HTTPX="http"
+ECS_HOST_NAME=$LOCALHOST_NAME
+ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
+
+# ECS_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
+ECS_ADAPTER_TYPE="REST"
+ECS_ADAPTER=$ECS_PATH
+
+# Make curl retries towards ECS for http response codes set in this env var, space separated list of codes
+ECS_RETRY_CODES=""
+
+#Save first worker node the pod is started on
+__ECS_WORKER_NODE=""
+
+###########################
+### ECS functions
+###########################
+
+# All calls to ECS will be directed to the ECS REST interface from now on
+# args: -
+# (Function for test scripts)
+use_ecs_rest_http() {
+ echo -e $BOLD"ECS protocol setting"$EBOLD
+ echo -e " Using $BOLD http $EBOLD and $BOLD REST $EBOLD towards ECS"
+ ECS_HTTPX="http"
+ ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
+
+ ECS_ADAPTER_TYPE="REST"
+ ECS_ADAPTER=$ECS_PATH
+ echo ""
+}
+
+# All calls to ECS will be directed to the ECS REST interface from now on
+# args: -
+# (Function for test scripts)
+use_ecs_rest_https() {
+ echo -e $BOLD"ECS protocol setting"$EBOLD
+ echo -e " Using $BOLD https $EBOLD and $BOLD REST $EBOLD towards ECS"
+ ECS_HTTPX="https"
+ ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_SECURE_PORT
+
+ ECS_ADAPTER_TYPE="REST"
+ ECS_ADAPTER=$ECS_PATH
+ echo ""
+}
+
+# All calls to ECS will be directed to the ECS dmaap interface over http from now on
+# args: -
+# (Function for test scripts)
+use_ecs_dmaap_http() {
+ echo -e $BOLD"ECS dmaap protocol setting"$EBOLD
+ echo -e $RED" - NOT SUPPORTED - "$ERED
+ echo -e " Using $BOLD http $EBOLD and $BOLD DMAAP $EBOLD towards ECS"
+ ECS_ADAPTER_TYPE="MR-HTTP"
+ echo ""
+}
+
+# All calls to ECS will be directed to the ECS dmaap interface over https from now on
+# args: -
+# (Function for test scripts)
+use_ecs_dmaap_https() {
+ echo -e $BOLD"RICSIM protocol setting"$EBOLD
+ echo -e $RED" - NOT SUPPORTED - "$ERED
+ echo -e " Using $BOLD https $EBOLD and $BOLD REST $EBOLD towards ECS"
+ ECS_ADAPTER_TYPE="MR-HTTPS"
+ echo ""
+}
+
+# Start the ECS
+# args: PROXY|NOPROXY <config-file>
+# (Function for test scripts)
+start_ecs() {
+
+ echo -e $BOLD"Starting $ECS_DISPLAY_NAME"$EBOLD
+
+ if [ $RUNMODE == "KUBE" ]; then
+
+ # Check if app shall be fully managed by the test script
+ __check_included_image "ECS"
+ retcode_i=$?
+
+ # Check if app shall only be used by the testscipt
+ __check_prestarted_image "ECS"
+ retcode_p=$?
+
+ if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
+ echo -e $RED"The $ECS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
+ echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
+ exit
+ fi
+ if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
+ echo -e $RED"The $ECS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
+ echo -e $RED"The $ECS_APP_NAME will not be started"$ERED
+ exit
+ fi
+
+
+ if [ $retcode_p -eq 0 ]; then
+ echo -e " Using existing $ECS_APP_NAME deployment and service"
+ echo " Setting ECS replicas=1"
+ res_type=$(__kube_get_resource_type $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
+ __kube_scale $res_type $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
+ fi
+
+ # Check if app shall be fully managed by the test script
+ if [ $retcode_i -eq 0 ]; then
+ echo -e " Creating $ECS_APP_NAME app and expose service"
+
+ #Check if nonrtric namespace exists, if not create it
+ __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
+
+ export ECS_APP_NAME
+ export KUBE_NONRTRIC_NAMESPACE
+ export ECS_IMAGE
+ export ECS_INTERNAL_PORT
+ export ECS_INTERNAL_SECURE_PORT
+ export ECS_EXTERNAL_PORT
+ export ECS_EXTERNAL_SECURE_PORT
+ export ECS_CONFIG_MOUNT_PATH
+ export ECS_CONFIG_CONFIGMAP_NAME=$ECS_APP_NAME"-config"
+ export ECS_DATA_CONFIGMAP_NAME=$ECS_APP_NAME"-data"
+ export ECS_CONTAINER_MNT_DIR
+
+ export ECS_DATA_PV_NAME=$ECS_APP_NAME"-pv"
+ export ECS_DATA_PVC_NAME=$ECS_APP_NAME"-pvc"
+ #Create a unique path for the pv each time to prevent a previous volume to be reused
+ export ECS_PV_PATH="ecsdata-"$(date +%s)
+
+ if [ $1 == "PROXY" ]; then
+ ECS_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT #Set if proxy is started
+ ECS_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
+ if [ $ECS_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$ECS_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
+ echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
+ else
+ echo " Configured with http proxy"
+ fi
+ else
+ ECS_HTTP_PROXY_CONFIG_PORT=0
+ ECS_HTTP_PROXY_CONFIG_HOST_NAME=""
+ echo " Configured without http proxy"
+ fi
+ export ECS_HTTP_PROXY_CONFIG_PORT
+ export ECS_HTTP_PROXY_CONFIG_HOST_NAME
+
+ # Create config map for config
+ datafile=$PWD/tmp/$ECS_CONFIG_FILE
+ cp $2 $datafile
+ output_yaml=$PWD/tmp/ecs_cfc.yaml
+ __kube_create_configmap $ECS_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest ECS $datafile $output_yaml
+
+ # Create pv
+ input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"pv.yaml
+ output_yaml=$PWD/tmp/ecs_pv.yaml
+ __kube_create_instance pv $ECS_APP_NAME $input_yaml $output_yaml
+
+ # Create pvc
+ input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"pvc.yaml
+ output_yaml=$PWD/tmp/ecs_pvc.yaml
+ __kube_create_instance pvc $ECS_APP_NAME $input_yaml $output_yaml
+
+ # Create service
+ input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"svc.yaml
+ output_yaml=$PWD/tmp/ecs_svc.yaml
+ __kube_create_instance service $ECS_APP_NAME $input_yaml $output_yaml
+
+ # Create app
+ input_yaml=$SIM_GROUP"/"$ECS_COMPOSE_DIR"/"app.yaml
+ output_yaml=$PWD/tmp/ecs_app.yaml
+ __kube_create_instance app $ECS_APP_NAME $input_yaml $output_yaml
+ fi
+
+ # Tie the ECS to a worker node so that ECS will always be scheduled to the same worker node if the ECS pod is restarted
+ # A PVC of type hostPath is mounted to ECS, for persistent storage, so the ECS must always be on the node which mounted the volume
+
+ # Keep the initial worker node in case the pod need to be "restarted" - must be made to the same node due to a volume mounted on the host
+ if [ $retcode_i -eq 0 ]; then
+ __ECS_WORKER_NODE=$(kubectl get pod -l "autotest=ECS" -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[*].spec.nodeName}')
+ if [ -z "$__ECS_WORKER_NODE" ]; then
+ echo -e $YELLOW" Cannot find worker node for pod for $ECS_APP_NAME, persistency may not work"$EYELLOW
+ fi
+ else
+ echo -e $YELLOW" Persistency may not work for app $ECS_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
+ fi
+
+ echo " Retrieving host and ports for service..."
+ ECS_HOST_NAME=$(__kube_get_service_host $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
+ ECS_EXTERNAL_PORT=$(__kube_get_service_port $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
+ ECS_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
+
+ echo " Host IP, http port, https port: $ECS_HOST_NAME $ECS_EXTERNAL_PORT $ECS_EXTERNAL_SECURE_PORT"
+
+ if [ $ECS_HTTPX == "http" ]; then
+ ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_PORT
+ else
+ ECS_PATH=$ECS_HTTPX"://"$ECS_HOST_NAME":"$ECS_EXTERNAL_SECURE_PORT
+ fi
+
+ __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
+
+ if [ $ECS_ADAPTER_TYPE == "REST" ]; then
+ ECS_ADAPTER=$ECS_PATH
+ fi
+ else
+ __check_included_image 'ECS'
+ if [ $? -eq 1 ]; then
+ echo -e $RED"The ECS app is not included in this test script"$ERED
+ echo -e $RED"ECS will not be started"$ERED
+ exit 1
+ fi
+
+ curdir=$PWD
+ cd $SIM_GROUP
+ cd ecs
+ cd $ECS_HOST_MNT_DIR
+ #cd ..
+ if [ -d db ]; then
+ if [ "$(ls -A $DIR)" ]; then
+ echo -e $BOLD" Cleaning files in mounted dir: $PWD/db"$EBOLD
+ rm -rf db/* &> /dev/null
+ if [ $? -ne 0 ]; then
+ echo -e $RED" Cannot remove database files in: $PWD"$ERED
+ exit 1
+ fi
+ fi
+ else
+ echo " No files in mounted dir or dir does not exists"
+ fi
+ cd $curdir
+
+ export ECS_APP_NAME
+ export ECS_APP_NAME_ALIAS
+ export ECS_HOST_MNT_DIR
+ export ECS_CONTAINER_MNT_DIR
+ export ECS_CONFIG_MOUNT_PATH
+ export ECS_CONFIG_FILE
+ export ECS_INTERNAL_PORT
+ export ECS_EXTERNAL_PORT
+ export ECS_INTERNAL_SECURE_PORT
+ export ECS_EXTERNAL_SECURE_PORT
+ export DOCKER_SIM_NWNAME
+ export ECS_DISPLAY_NAME
+
+ if [ $1 == "PROXY" ]; then
+ ECS_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT #Set if proxy is started
+ ECS_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
+ if [ $ECS_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$ECS_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
+ echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
+ else
+ echo " Configured with http proxy"
+ fi
+ else
+ ECS_HTTP_PROXY_CONFIG_PORT=0
+ ECS_HTTP_PROXY_CONFIG_HOST_NAME=""
+ echo " Configured without http proxy"
+ fi
+ export ECS_HTTP_PROXY_CONFIG_PORT
+ export ECS_HTTP_PROXY_CONFIG_HOST_NAME
+
+ dest_file=$SIM_GROUP/$ECS_COMPOSE_DIR/$ECS_HOST_MNT_DIR/$ECS_CONFIG_FILE
+
+ envsubst < $2 > $dest_file
+
+ __start_container $ECS_COMPOSE_DIR "" NODOCKERARGS 1 $ECS_APP_NAME
+
+ __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
+ fi
+ echo ""
+ return 0
+}
+
+# Stop the ecs
+# args: -
+# args: -
+# (Function for test scripts)
+stop_ecs() {
+ echo -e $BOLD"Stopping $ECS_DISPLAY_NAME"$EBOLD
+
+ if [ $RUNMODE == "KUBE" ]; then
+
+ __check_prestarted_image "ECS"
+ if [ $? -eq 0 ]; then
+ echo -e $YELLOW" Persistency may not work for app $ECS_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
+ res_type=$(__kube_get_resource_type $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
+ __kube_scale $res_type $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 0
+ return 0
+ fi
+
+ __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest ECS
+ echo " Deleting the replica set - a new will be started when the app is started"
+ tmp=$(kubectl delete rs -n $KUBE_NONRTRIC_NAMESPACE -l "autotest=ECS")
+ if [ $? -ne 0 ]; then
+ echo -e $RED" Could not delete replica set "$RED
+ ((RES_CONF_FAIL++))
+ return 1
+ fi
+ else
+ docker stop $ECS_APP_NAME &> ./tmp/.dockererr
+ if [ $? -ne 0 ]; then
+ __print_err "Could not stop $ECS_APP_NAME" $@
+ cat ./tmp/.dockererr
+ ((RES_CONF_FAIL++))
+ return 1
+ fi
+ fi
+ echo -e $BOLD$GREEN"Stopped"$EGREEN$EBOLD
+ echo ""
+ return 0
+}
+
+# Start a previously stopped ecs
+# args: -
+# (Function for test scripts)
+start_stopped_ecs() {
+ echo -e $BOLD"Starting (the previously stopped) $ECS_DISPLAY_NAME"$EBOLD
+
+ if [ $RUNMODE == "KUBE" ]; then
+
+ __check_prestarted_image "ECS"
+ if [ $? -eq 0 ]; then
+ echo -e $YELLOW" Persistency may not work for app $ECS_APP_NAME in multi-worker node config when running it as a prestarted app"$EYELLOW
+ res_type=$(__kube_get_resource_type $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
+ __kube_scale $res_type $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
+ __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
+ return 0
+ fi
+
+ # Tie the PMS to the same worker node it was initially started on
+ # A PVC of type hostPath is mounted to PMS, for persistent storage, so the PMS must always be on the node which mounted the volume
+ if [ -z "$__ECS_WORKER_NODE" ]; then
+ echo -e $RED" No initial worker node found for pod "$RED
+ ((RES_CONF_FAIL++))
+ return 1
+ else
+ echo -e $BOLD" Setting nodeSelector kubernetes.io/hostname=$__ECS_WORKER_NODE to deployment for $ECS_APP_NAME. Pod will always run on this worker node: $__PA_WORKER_NODE"$BOLD
+ echo -e $BOLD" The mounted volume is mounted as hostPath and only available on that worker node."$BOLD
+ tmp=$(kubectl patch deployment $ECS_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE --patch '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "'$__ECS_WORKER_NODE'"}}}}}')
+ if [ $? -ne 0 ]; then
+ echo -e $YELLOW" Cannot set nodeSelector to deployment for $ECS_APP_NAME, persistency may not work"$EYELLOW
+ fi
+ __kube_scale deployment $ECS_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
+ fi
+ else
+ docker start $ECS_APP_NAME &> ./tmp/.dockererr
+ if [ $? -ne 0 ]; then
+ __print_err "Could not start (the stopped) $ECS_APP_NAME" $@
+ cat ./tmp/.dockererr
+ ((RES_CONF_FAIL++))
+ return 1
+ fi
+ fi
+ __check_service_start $ECS_APP_NAME $ECS_PATH$ECS_ALIVE_URL
+ if [ $? -ne 0 ]; then
+ return 1
+ fi
+ echo ""
+ return 0
+}
+
+# Turn on debug level tracing in ECS
+# args: -
+# (Function for test scripts)
+set_ecs_debug() {
+ echo -e $BOLD"Setting ecs debug logging"$EBOLD
+ curlString="$ECS_PATH$ECS_ACTUATOR -X POST -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
+ result=$(__do_curl "$curlString")
+ if [ $? -ne 0 ]; then
+ __print_err "Could not set debug mode" $@
+ ((RES_CONF_FAIL++))
+ return 1
+ fi
+ echo ""
+ return 0
+}
+
+# Turn on trace level tracing in ECS
+# args: -
+# (Function for test scripts)
+set_ecs_trace() {
+ echo -e $BOLD"Setting ecs trace logging"$EBOLD
+ curlString="$ECS_PATH/actuator/loggers/org.oransc.enrichment -X POST -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
+ result=$(__do_curl "$curlString")
+ if [ $? -ne 0 ]; then
+ __print_err "Could not set trace mode" $@
+ ((RES_CONF_FAIL++))
+ return 1
+ fi
+ echo ""
+ return 0
+}
+
+# Perform curl retries when making direct call to ECS for the specified http response codes
+# Speace separated list of http response codes
+# args: [<response-code>]*
+use_ecs_retries() {
+ echo -e $BOLD"Do curl retries to the ECS REST inteface for these response codes:$@"$EBOLD
+ ECS_RETRY_CODES=$@
+ echo ""
+ return 0
+}
+
+# Check the ecs logs for WARNINGs and ERRORs
+# args: -
+# (Function for test scripts)
+check_ecs_logs() {
+ __check_container_logs "ECS" $ECS_APP_NAME $ECS_LOGPATH WARN ERR
+}
+
+
+# Tests if a variable value in the ECS is equal to a target value and and optional timeout.
+# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
+# equal to the target or not.
+# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
+# before setting pass or fail depending on if the variable value becomes equal to the target
+# value or not.
+# (Function for test scripts)
+ecs_equal() {
+ if [ $# -eq 2 ] || [ $# -eq 3 ]; then
+ __var_test ECS "$ECS_PATH/" $1 "=" $2 $3
+ else
+ __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
+ fi
+}