+################ Test engine functions ################
+
+# 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
+__PA_imagesetup() {
+ __check_and_create_image_var PA "POLICY_AGENT_IMAGE" "POLICY_AGENT_IMAGE_BASE" "POLICY_AGENT_IMAGE_TAG" $1 "$POLICY_AGENT_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'
+__PA_imagepull() {
+ __check_and_pull_image $1 "$POLICY_AGENT_DISPLAY_NAME" $POLICY_AGENT_APP_NAME POLICY_AGENT_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
+__PA_imagebuild() {
+ echo -e $RED" Image for app PA 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>
+__PA_image_data() {
+ echo -e "$POLICY_AGENT_DISPLAY_NAME\t$(docker images --format $1 $POLICY_AGENT_IMAGE)" >> $2
+ if [ ! -z "$POLICY_AGENT_IMAGE_SOURCE" ]; then
+ echo -e "-- source image --\t$(docker images --format $1 $POLICY_AGENT_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
+__PA_kube_scale_zero() {
+ __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest PA
+}
+
+# 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.
+__PA_kube_scale_zero_and_wait() {
+ __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app nonrtric-policymanagementservice
+}
+
+# Delete all kube resouces for the app
+# This function is called for apps managed by the test script.
+__PA_kube_delete_all() {
+ __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest PA
+}
+
+# Store docker logs
+# This function is called for apps managed by the test script.
+# args: <log-dir> <file-prexix>
+__PA_store_docker_logs() {
+ docker logs $POLICY_AGENT_APP_NAME > $1$2_policy-agent.log 2>&1
+}
+
+#######################################################
+
+## Access to Policy agent
+# Host name may be changed if app started by kube
+# Direct access from script
+PA_HTTPX="http"
+PA_HOST_NAME=$LOCALHOST_NAME
+PA_PATH=$PA_HTTPX"://"$PA_HOST_NAME":"$POLICY_AGENT_EXTERNAL_PORT
+
+# PA_ADAPTER used for switch between REST and DMAAP
+PA_ADAPTER_TYPE="REST"
+PA_ADAPTER=$PA_PATH
+
+# Make curl retries towards the agent for http response codes set in this env var, space separated list of codes
+AGENT_RETRY_CODES=""
+
+#Save first worker node the pod is started on
+__PA_WORKER_NODE=""
+
+###########################
+### Policy Agents functions
+###########################
+
+# All calls to the agent will be directed to the agent REST interface from now on
+# args: -
+# (Function for test scripts)
+use_agent_rest_http() {
+ echo -e $BOLD"Agent protocol setting"$EBOLD
+ echo -e " Using $BOLD http $EBOLD and $BOLD REST $EBOLD towards the agent"
+ PA_HTTPX="http"
+ PA_PATH=$PA_HTTPX"://"$PA_HOST_NAME":"$POLICY_AGENT_EXTERNAL_PORT
+
+ PA_ADAPTER_TYPE="REST"
+ PA_ADAPTER=$PA_PATH
+ echo ""
+}
+
+# All calls to the agent will be directed to the agent REST interface from now on
+# args: -
+# (Function for test scripts)
+use_agent_rest_https() {
+ echo -e $BOLD"Agent protocol setting"$EBOLD
+ echo -e " Using $BOLD https $EBOLD and $BOLD REST $EBOLD towards the agent"
+ PA_HTTPX="https"
+ PA_PATH=$PA_HTTPX"://"$PA_HOST_NAME":"$POLICY_AGENT_EXTERNAL_SECURE_PORT
+
+ PA_ADAPTER_TYPE="REST"
+ PA_ADAPTER=$PA_PATH
+ echo ""
+}
+
+# All calls to the agent will be directed to the agent dmaap interface over http from now on
+# args: -
+# (Function for test scripts)
+use_agent_dmaap_http() {
+ echo -e $BOLD"Agent dmaap protocol setting"$EBOLD
+ echo -e " Using $BOLD http $EBOLD and $BOLD DMAAP $EBOLD towards the agent"
+ PA_ADAPTER_TYPE="MR-HTTP"
+ echo ""
+}
+
+# All calls to the agent will be directed to the agent dmaap interface over https from now on
+# args: -
+# (Function for test scripts)
+use_agent_dmaap_https() {
+ echo -e $BOLD"Agent dmaap protocol setting"$EBOLD
+ echo -e " Using $BOLD https $EBOLD and $BOLD DMAAP $EBOLD towards the agent"
+ echo -e $YELLOW" Setting http instead of https - MR only uses http"$EYELLOW
+ PA_ADAPTER_TYPE="MR-HTTPS"
+ echo ""
+}
+
+# Start the policy agent
+# args: (docker) PROXY|NOPROXY <config-file>
+# args: (kube) PROXY|NOPROXY <config-file> [ <data-file>]
+# (Function for test scripts)
+start_policy_agent() {
+ echo -e $BOLD"Starting $POLICY_AGENT_DISPLAY_NAME"$EBOLD
+
+ if [ $RUNMODE == "KUBE" ]; then
+
+ # Check if app shall be fully managed by the test script
+ __check_included_image "PA"
+ retcode_i=$?
+
+ # Check if app shall only be used by the testscipt
+ __check_prestarted_image "PA"
+ retcode_p=$?
+
+ if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
+ echo -e $RED"The $POLICY_AGENT_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
+ echo -e $RED"The $POLICY_AGENT_APP_NAME will not be started"$ERED
+ exit
+ fi
+ if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
+ echo -e $RED"The $POLICY_AGENT_APP_NAME app is included both as managed and prestarted in this test script"$ERED
+ echo -e $RED"The $POLICY_AGENT_APP_NAME will not be started"$ERED
+ exit
+ fi
+
+ if [ $retcode_p -eq 0 ]; then
+ echo -e " Using existing $POLICY_AGENT_APP_NAME deployment and service"
+ echo " Setting $POLICY_AGENT_APP_NAME replicas=1"
+ __kube_scale deployment $POLICY_AGENT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
+ fi
+
+ if [ $retcode_i -eq 0 ]; then
+
+ echo -e " Creating $POLICY_AGENT_APP_NAME app and expose service"
+
+ #Check if nonrtric namespace exists, if not create it
+ __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
+
+ #Export all vars needed for service and deployment
+ export POLICY_AGENT_APP_NAME
+ export KUBE_NONRTRIC_NAMESPACE
+ export POLICY_AGENT_IMAGE
+ export POLICY_AGENT_INTERNAL_PORT
+ export POLICY_AGENT_INTERNAL_SECURE_PORT
+ export POLICY_AGENT_EXTERNAL_PORT
+ export POLICY_AGENT_EXTERNAL_SECURE_PORT
+ export POLICY_AGENT_CONFIG_MOUNT_PATH
+ export POLICY_AGENT_DATA_MOUNT_PATH
+ export POLICY_AGENT_CONFIG_CONFIGMAP_NAME=$POLICY_AGENT_APP_NAME"-config"
+ export POLICY_AGENT_DATA_CONFIGMAP_NAME=$POLICY_AGENT_APP_NAME"-data"
+ export POLICY_AGENT_PKG_NAME
+
+ export POLICY_AGENT_DATA_PV_NAME=$POLICY_AGENT_APP_NAME"-pv"
+ export POLICY_AGENT_DATA_PVC_NAME=$POLICY_AGENT_APP_NAME"-pvc"
+ ##Create a unique path for the pv each time to prevent a previous volume to be reused
+ export POLICY_AGENT_PV_PATH="padata-"$(date +%s)
+ export POLICY_AGENT_CONTAINER_MNT_DIR
+
+ if [ $1 == "PROXY" ]; then
+ AGENT_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT #Set if proxy is started
+ AGENT_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
+ if [ $AGENT_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$AGENT_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
+ AGENT_HTTP_PROXY_CONFIG_PORT=0
+ AGENT_HTTP_PROXY_CONFIG_HOST_NAME=""
+ echo " Configured without http proxy"