Adaptation of test env to helm chart
[nonrtric.git] / test / common / controller_api_functions.sh
index 9e60175..4027f30 100644 (file)
 
 # This is a script that contains container/service management functions and test functions for A1 Controller API
 
-SDNC_HTTPX="http"
-SDNC_HOST_NAME=$LOCALHOST_NAME
-SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
-SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
-#Docker/Kube internal path
-if [ $RUNMODE == "KUBE" ]; then
-       SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
-    #presume correct
-       SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
-       #test
-       #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
-else
-       SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
-fi
+################ Test engine functions ################
 
-use_sdnc_http() {
-       echo -e $BOLD"SDNC NB protocol setting"$EBOLD
-       echo -e " Using $BOLD http $EBOLD towards SDNC"
-       SDNC_HTTPX="http"
-       SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
-       SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
+# 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
+__SDNC_imagesetup() {
+
+       sdnc_suffix_tag=$1
+
+       for oia_name in $ONAP_IMAGES_APP_NAMES; do
+               if [ "$oia_name" == "SDNC" ]; then
+                       sdnc_suffix_tag="REMOTE_RELEASE_ONAP"
+               fi
+       done
+       __check_and_create_image_var SDNC "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_IMAGE_BASE" "SDNC_A1_CONTROLLER_IMAGE_TAG" $sdnc_suffix_tag "$SDNC_DISPLAY_NAME"
+       __check_and_create_image_var SDNC "SDNC_DB_IMAGE" "SDNC_DB_IMAGE_BASE" "SDNC_DB_IMAGE_TAG" REMOTE_PROXY "SDNC DB"
+
+}
+
+# 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'
+__SDNC_imagepull() {
+       __check_and_pull_image $1 "$SDNC_DISPLAY_NAME" $SDNC_APP_NAME SDNC_A1_CONTROLLER_IMAGE
+       __check_and_pull_image $2 "SDNC DB" $SDNC_APP_NAME SDNC_DB_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
+__SDNC_imagebuild() {
+       echo -e $RED" Image for app SDNC 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>
+__SDNC_image_data() {
+       echo -e "$SDNC_DISPLAY_NAME\t$(docker images --format $1 $SDNC_A1_CONTROLLER_IMAGE)" >>   $2
+       if [ ! -z "$SDNC_A1_CONTROLLER_IMAGE_SOURCE" ]; then
+               echo -e "-- source image --\t$(docker images --format $1 $SDNC_A1_CONTROLLER_IMAGE_SOURCE)" >>   $2
+       fi
+       echo -e "SDNC DB\t$(docker images --format $1 $SDNC_DB_IMAGE)" >>   $2
+       if [ ! -z "$SDNC_DB_IMAGE_SOURCE" ]; then
+               echo -e "-- source image --\t$(docker images --format $1 $SDNC_DB_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
+__SDNC_kube_scale_zero() {
+       __kube_scale_all_resources $KUBE_SNDC_NAMESPACE autotest SDNC
+}
+
+# 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.
+__SDNC_kube_scale_zero_and_wait() {
+       echo -e " SDNC replicas kept as is"
+}
+
+# Delete all kube resouces for the app
+# This function is called for apps managed by the test script.
+__SDNC_kube_delete_all() {
+       __kube_delete_all_resources $KUBE_SNDC_NAMESPACE autotest SDNC
+}
+
+# Store docker logs
+# This function is called for apps managed by the test script.
+# args: <log-dir> <file-prexix>
+__SDNC_store_docker_logs() {
        if [ $RUNMODE == "KUBE" ]; then
-               #presume correct
-               SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
-               #test
-               #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
+               kubectl  logs -l "autotest=SDNC" -n $KUBE_SNDC_NAMESPACE --tail=-1 > $1$2_SDNC.log 2>&1
+               podname=$(kubectl get pods -n $KUBE_SNDC_NAMESPACE -l "autotest=SDNC" -o custom-columns=":metadata.name")
+               kubectl exec -t -n $KUBE_SNDC_NAMESPACE $podname -- cat $SDNC_KARAF_LOG> $1$2_SDNC_karaf.log 2>&1
        else
-               SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_PORT
+               docker exec -t $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $1$2_SDNC_karaf.log 2>&1
        fi
-       echo ""
 }
 
+# Initial setup of protocol, host and ports
+# This function is called for apps managed by the test script.
+# args: -
+__SDNC_initial_setup() {
+       use_sdnc_http
+}
+
+#######################################################
+
+# Set http as the protocol to use for all communication to SDNC
+# args: -
+# (Function for test scripts)
+use_sdnc_http() {
+       __sdnc_set_protocoll "http" $SDNC_INTERNAL_PORT $SDNC_EXTERNAL_PORT
+}
+
+# Set https as the protocol to use for all communication to SDNC
+# args: -
+# (Function for test scripts)
 use_sdnc_https() {
-       echo -e $BOLD"SDNC NB protocol setting"$EBOLD
-       echo -e " Using $BOLD https $EBOLD towards SDNC"
-       SDNC_HTTPX="https"
-       SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT
-       SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT$SDNC_API_URL
+       __sdnc_set_protocoll "https" $SDNC_INTERNAL_SECURE_PORT $SDNC_EXTERNAL_SECURE_PORT
+}
+
+# Setup paths to svc/container for internal and external access
+# args: <protocol> <internal-port> <external-port>
+__sdnc_set_protocoll() {
+       echo -e $BOLD"$SDNC_DISPLAY_NAME protocol setting"$EBOLD
+       echo -e " Using $BOLD http $EBOLD towards $SDNC_DISPLAY_NAME"
+
+       ## Access to SDNC
+
+       SDNC_SERVICE_PATH=$1"://"$SDNC_APP_NAME":"$2  # docker access, container->container and script->container via proxy
+       SDNC_SERVICE_API_PATH=$1"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_APP_NAME":"$1$SDNC_API_URL
        if [ $RUNMODE == "KUBE" ]; then
-               #presume correct
-               SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
-               #test
-               #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
-       else
-               SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_INTERNAL_SECURE_PORT
+               SDNC_SERVICE_PATH=$1"://"$SDNC_APP_NAME.$KUBE_SNDC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
+               SDNC_SERVICE_API_PATH=$1"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_APP_NAME.KUBE_SNDC_NAMESPACE":"$1$SDNC_API_URL
        fi
        echo ""
+
+}
+
+# Export env vars for config files, docker compose and kube resources
+# args:
+__sdnc_export_vars() {
+       export KUBE_SNDC_NAMESPACE
+       export DOCKER_SIM_NWNAME
+
+       export SDNC_APP_NAME
+       export SDNC_DISPLAY_NAME
+
+       export SDNC_A1_CONTROLLER_IMAGE
+       export SDNC_INTERNAL_PORT
+       export SDNC_EXTERNAL_PORT
+       export SDNC_INTERNAL_SECURE_PORT
+       export SDNC_EXTERNAL_SECURE_PORT
+       export SDNC_A1_TRUSTSTORE_PASSWORD
+       export SDNC_DB_APP_NAME
+       export SDNC_DB_IMAGE
+       export SDNC_USER
+       export SDNC_PWD
 }
 
 ##################
@@ -104,7 +199,7 @@ start_sdnc() {
                if [ $retcode_p -eq 0 ]; then
                        echo -e " Using existing $SDNC_APP_NAME deployment and service"
                        echo " Setting SDNC replicas=1"
-                       __kube_scale deployment $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
+                       __kube_scale deployment $SDNC_APP_NAME $KUBE_SNDC_NAMESPACE 1
                fi
 
                                # Check if app shall be fully managed by the test script
@@ -112,19 +207,10 @@ start_sdnc() {
 
                        echo -e " Creating $SDNC_APP_NAME app and expose service"
 
-                       #Check if nonrtric namespace exists, if not create it
-                       __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
+                       #Check if namespace exists, if not create it
+                       __kube_create_namespace $KUBE_SNDC_NAMESPACE
 
-                       export KUBE_NONRTRIC_NAMESPACE
-                       export SDNC_APP_NAME
-                       export SDNC_A1_CONTROLLER_IMAGE
-                       export SDNC_INTERNAL_PORT
-                       export SDNC_EXTERNAL_PORT
-                       export SDNC_INTERNAL_SECURE_PORT
-                       export SDNC_EXTERNAL_SECURE_PORT
-                       export SDNC_A1_TRUSTSTORE_PASSWORD
-                       export SDNC_DB_APP_NAME
-                       export SDNC_DB_IMAGE
+                       __sdnc_export_vars
 
                        # Create service
                        input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"svc.yaml
@@ -132,36 +218,13 @@ start_sdnc() {
                        __kube_create_instance service $SDNC_APP_NAME $input_yaml $output_yaml
 
                        # Create app
-                       input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"app.yaml
+                       input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"$SDNC_KUBE_APP_FILE
                        output_yaml=$PWD/tmp/sdnc_app.yaml
                        __kube_create_instance app $SDNC_APP_NAME $input_yaml $output_yaml
 
                fi
 
-        echo " Retrieving host and ports for service..."
-               SDNC_HOST_NAME=$(__kube_get_service_host $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
-               SDNC_EXTERNAL_PORT=$(__kube_get_service_port $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
-               SDNC_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $SDNC_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
-
-               echo " Host IP, http port, https port: $SDNC_HOST_NAME $SDNC_EXTERNAL_PORT $SDNC_EXTERNAL_SECURE_PORT"
-
-        if [ $SDNC_HTTPX == "http" ]; then
-                       SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT
-                       SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_PORT$SDNC_API_URL
-            #presume correct
-                       SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_PORT
-                       #test
-                       #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_PORT
-               else
-                       SDNC_PATH=$SDNC_HTTPX"://"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT
-                       SDNC_API_PATH=$SDNC_HTTPX"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_HOST_NAME":"$SDNC_EXTERNAL_SECURE_PORT$SDNC_API_URL
-            #presume correct
-                       SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME"."$KUBE_NONRTRIC_NAMESPACE":"$SDNC_EXTERNAL_SECURE_PORT
-                       #test
-                       #SDNC_SERVICE_PATH=$SDNC_HTTPX"://"$SDNC_APP_NAME":"$SDNC_EXTERNAL_SECURE_PORT
-               fi
-
-               __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
+               __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
        else
 
                __check_included_image 'SDNC'
@@ -171,24 +234,73 @@ start_sdnc() {
                        exit
                fi
 
-               export SDNC_DB_APP_NAME
-        export SDNC_APP_NAME
-        export SDNC_INTERNAL_PORT
-        export SDNC_EXTERNAL_PORT
-        export SDNC_INTERNAL_SECURE_PORT
-        export SDNC_EXTERNAL_SECURE_PORT
-        export SDNC_A1_TRUSTSTORE_PASSWORD
-        export DOCKER_SIM_NWNAME
+               __sdnc_export_vars
 
-               __start_container $SDNC_COMPOSE_DIR NODOCKERARGS 1 $SDNC_APP_NAME
+               __start_container $SDNC_COMPOSE_DIR $SDNC_COMPOSE_FILE NODOCKERARGS 1 $SDNC_APP_NAME
 
-               __check_service_start $SDNC_APP_NAME $SDNC_PATH$SDNC_ALIVE_URL
+               __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
        fi
     echo ""
     return 0
 }
 
 
+# Stop the sndc
+# args: -
+# args: -
+# (Function for test scripts)
+stop_sdnc() {
+       echo -e $BOLD"Stopping $SDNC_DISPLAY_NAME"$EBOLD
+
+       if [ $RUNMODE == "KUBE" ]; then
+               __log_conf_fail_not_supported " Cannot stop sndc in KUBE mode"
+               return 1
+       else
+               docker stop $SDNC_APP_NAME &> ./tmp/.dockererr
+               if [ $? -ne 0 ]; then
+                       __print_err "Could not stop $SDNC_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 sdnc
+# args: -
+# (Function for test scripts)
+start_stopped_sdnc() {
+       echo -e $BOLD"Starting (the previously stopped) $SDNC_DISPLAY_NAME"$EBOLD
+
+       if [ $RUNMODE == "KUBE" ]; then
+               __log_conf_fail_not_supported " Cannot restart sndc in KUBE mode"
+               return 1
+       else
+               docker start $SDNC_APP_NAME &> ./tmp/.dockererr
+               if [ $? -ne 0 ]; then
+                       __print_err "Could not start (the stopped) $SDNC_APP_NAME" $@
+                       cat ./tmp/.dockererr
+                       ((RES_CONF_FAIL++))
+                       return 1
+               fi
+       fi
+       __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
+       if [ $? -ne 0 ]; then
+               return 1
+       fi
+       echo ""
+       return 0
+}
+
+# Check the agent logs for WARNINGs and ERRORs
+# args: -
+# (Function for test scripts)
+check_sdnc_logs() {
+       __check_container_logs "SDNC A1 Controller" $SDNC_APP_NAME $SDNC_KARAF_LOG WARN ERROR
+}
 
 # Generic function to query the RICs via the A1-controller API.
 # args: <operation> <url> [<body>]
@@ -215,7 +327,15 @@ __do_curl_to_controller() {
        payload="./tmp/.sdnc.payload.json"
     echo "$json" > $payload
     echo "  FILE ($payload) : $json"  >> $HTTPLOG
-    curlString="curl -skw %{http_code} -X POST $SDNC_API_PATH$1 -H accept:application/json -H Content-Type:application/json --data-binary @$payload"
+       proxyflag=""
+       if [ ! -z "$KUBE_PROXY_PATH" ]; then
+               if [ $KUBE_PROXY_HTTPX == "http" ]; then
+                       proxyflag=" --proxy $KUBE_PROXY_PATH"
+               else
+                       proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
+               fi
+       fi
+    curlString="curl -skw %{http_code} $proxyflag -X POST $SDNC_SERVICE_API_PATH$1 -H accept:application/json -H Content-Type:application/json --data-binary @$payload"
     echo "  CMD: "$curlString >> $HTTPLOG
     res=$($curlString)
     retcode=$?
@@ -236,7 +356,7 @@ __do_curl_to_controller() {
        echo "  JSON: "$body >> $HTTPLOG
        reply="./tmp/.sdnc-reply.json"
     echo "$body" > $reply
-    res=$(python3 ../common/extract_sdnc_reply.py $reply)
+    res=$(python3 ../common/extract_sdnc_reply.py $SDNC_RESPONSE_JSON_KEY $reply)
     echo "  EXTRACED BODY+CODE: "$res >> $HTTPLOG
     echo "$res"
     return 0