Fix docker-compose version check in tests
[nonrtric.git] / test / common / ricsim_api_functions.sh
index 435c208..8cb24b8 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 #  ============LICENSE_START===============================================
-#  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+#  Copyright (C) 2020-2023 Nordix Foundation. All rights reserved.
 #  ========================================================================
 #  Licensed under the Apache License, Version 2.0 (the "License");
 #  you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
 __RICSIM_imagesetup() {
-       __check_and_create_image_var RICSIM "RIC_SIM_IMAGE" "RIC_SIM_IMAGE_BASE" "RIC_SIM_IMAGE_TAG" $1 "$RIC_SIM_DISPLAY_NAME"
+       __check_and_create_image_var RICSIM "RIC_SIM_IMAGE" "RIC_SIM_IMAGE_BASE" "RIC_SIM_IMAGE_TAG" $1 "$RIC_SIM_DISPLAY_NAME" ""
 }
 
 # Pull image from remote repo or use locally built image
@@ -55,13 +55,13 @@ __RICSIM_kube_scale_zero() {
 }
 
 # 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.
+# This function is called for pre-started apps not managed by the test script.
 __RICSIM_kube_scale_zero_and_wait() {
        #__kube_scale_and_wait_all_resources $KUBE_A1SIM_NAMESPACE app $KUBE_A1SIM_NAMESPACE"-"$RIC_SIM_PREFIX
-       __kube_scale_and_wait_all_resources $KUBE_A1SIM_NAMESPACE app # the values of the app label is not known
+       __kube_scale_and_wait_all_resources $KUBE_A1SIM_NAMESPACE app $KUBE_A1SIM_NAMESPACE"-a1simulator"
 }
 
-# Delete all kube resouces for the app
+# Delete all kube resources for the app
 # This function is called for apps managed by the test script.
 __RICSIM_kube_delete_all() {
        __kube_delete_all_resources $KUBE_A1SIM_NAMESPACE autotest RICSIM
@@ -69,15 +69,15 @@ __RICSIM_kube_delete_all() {
 
 # Store docker logs
 # This function is called for apps managed by the test script.
-# args: <log-dir> <file-prexix>
+# args: <log-dir> <file-prefix>
 __RICSIM_store_docker_logs() {
        if [ $RUNMODE == "KUBE" ]; then
-               for podname in $(kubectl get pods -n $KUBE_A1SIM_NAMESPACE -l "autotest=RICSIM" -o custom-columns=":metadata.name"); do
-                       kubectl logs -n $KUBE_A1SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
+               for podname in $(kubectl $KUBECONF get pods -n $KUBE_A1SIM_NAMESPACE -l "autotest=RICSIM" -o custom-columns=":metadata.name"); do
+                       kubectl $KUBECONF logs -n $KUBE_A1SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
                done
        else
 
-               rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
+               rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --filter "label=a1sim" --format {{.Names}})
                for ric in $rics; do
                        docker logs $ric > $1$2_$ric.log 2>&1
                done
@@ -91,11 +91,11 @@ __RICSIM_initial_setup() {
        use_simulator_http
 }
 
-# Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
+# Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
 # For docker, the namespace shall be excluded
-# This function is called for apps managed by the test script as well as for prestarted apps.
+# This function is called for apps managed by the test script as well as for pre-started apps.
 # args: -
-__RICSIM_statisics_setup() {
+__RICSIM_statistics_setup() {
        for ((RICSIM_INSTANCE=10; RICSIM_INSTANCE>0; RICSIM_INSTANCE-- )); do
                if [ $RUNMODE == "KUBE" ]; then
                        RICSIM_INSTANCE_KUBE=$(($RICSIM_INSTANCE-1))
@@ -103,19 +103,19 @@ __RICSIM_statisics_setup() {
                        echo -n " RICSIM_G2_$RICSIM_INSTANCE_KUBE ${RIC_SIM_PREFIX}-g2-$RICSIM_INSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
                        echo -n " RICSIM_G3_$RICSIM_INSTANCE_KUBE ${RIC_SIM_PREFIX}-g3-$RICSIM_INSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
                else
-                       if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
-                               echo -n " RICSIM_G1_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}_g1_$RICSIM_INSTANCE "
-                               echo -n " RICSIM_G2_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}_g2_$RICSIM_INSTANCE "
-                               echo -n " RICSIM_G3_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}_g3_$RICSIM_INSTANCE "
-                       else
-                               echo -n " RICSIM_G1_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g1-$RICSIM_INSTANCE "
-                               echo -n " RICSIM_G2_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g2-$RICSIM_INSTANCE "
-                               echo -n " RICSIM_G3_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g3-$RICSIM_INSTANCE "
-                       fi
+                       echo -n " RICSIM_G1_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g1-$RICSIM_INSTANCE "
+                       echo -n " RICSIM_G2_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g2-$RICSIM_INSTANCE "
+                       echo -n " RICSIM_G3_$RICSIM_INSTANCE ${RIC_SIM_PREFIX}-g3-$RICSIM_INSTANCE "
                fi
        done
 }
 
+# Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
+# args: -
+__RICSIM_test_requirements() {
+       :
+}
+
 #######################################################
 
 
@@ -165,7 +165,7 @@ start_ric_simulators() {
                __check_included_image "RICSIM"
                retcode_i=$?
 
-               # Check if app shall only be used by the testscipt
+               # Check if app shall only be used by the test script
                __check_prestarted_image "RICSIM"
                retcode_p=$?
 
@@ -203,7 +203,7 @@ start_ric_simulators() {
        fi
 
        echo " $2 simulators using basename: $1 on interface: $3"
-       #Set env var for simulator count and A1 interface vesion for the given group
+       #Set env var for simulator count and A1 interface version for the given group
        if [ $1 == "$RIC1" ]; then
                G1_COUNT=$2
        elif [ $1 == "$RIC2" ]; then
@@ -235,7 +235,7 @@ start_ric_simulators() {
                        export RIC_SIM_INTERNAL_PORT
                        export RIC_SIM_INTERNAL_SECURE_PORT
 
-                       echo -e " Creating $POLICY_AGENT_APP_NAME app and expose service"
+                       echo -e " Creating $RIC_SIM_PREFIX app and expose service"
 
                        #Check if nonrtric namespace exists, if not create it
                        __kube_create_namespace $KUBE_A1SIM_NAMESPACE
@@ -247,7 +247,7 @@ start_ric_simulators() {
 
                        # Create app
                        input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"app.yaml
-                       output_yaml=$PWD/tmp/pa_${1}_app.yaml
+                       output_yaml=$PWD/tmp/ric_${1}_app.yaml
                        __kube_create_instance app $RIC_SIM_SET_NAME $input_yaml $output_yaml
 
                        #Using only instance from index 1 to keep compatability with docker
@@ -257,7 +257,6 @@ start_ric_simulators() {
                        done
                fi
        else
-
                __check_included_image 'RICSIM'
                if [ $? -eq 1 ]; then
                        echo -e $RED"The Near-RT RIC Simulator app is not included as managed in this test script"$ERED
@@ -281,19 +280,14 @@ start_ric_simulators() {
                export DOCKER_SIM_NWNAME
                export RIC_SIM_DISPLAY_NAME
 
-               docker_args="--no-recreate --scale $RICSIM_COMPOSE_SERVICE_NAME=$2"
+               echo -e $BOLD$YELLOW" Warning: Using docker compose --force-recreate "$EYELLOW$EBOLD
+               docker_args="--force-recreate --scale $RICSIM_COMPOSE_SERVICE_NAME=$2"
 
-               #Create a list of contsiner names
-               #Will be <ricsim-prefix>_<service-name>_<index>
-               # or
-               # <ricsim-prefix>-<service-name>-<index>
+               #Create a list of container names
+               #Will be <ricsim-prefix>-<service-name>-<index>
                app_data=""
                cntr=1
-               if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
-                       app_name_prefix=$RIC_SIM_PREFIX"_"$RICSIM_COMPOSE_SERVICE_NAME"_"
-               else
-                       app_name_prefix=$RIC_SIM_PREFIX"-"$RICSIM_COMPOSE_SERVICE_NAME"-"
-               fi
+               app_name_prefix=$RIC_SIM_PREFIX"-"$RICSIM_COMPOSE_SERVICE_NAME"-"
                while [ $cntr -le $2 ]; do
                        app=$app_name_prefix$cntr
                        app_data="$app_data $app"
@@ -304,11 +298,7 @@ start_ric_simulators() {
 
                cntr=1
                while [ $cntr -le $2 ]; do
-                       if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
-                               app=$RIC_SIM_PREFIX"_"$RICSIM_COMPOSE_SERVICE_NAME"_"$cntr
-                       else
-                               app=$RIC_SIM_PREFIX"-"$RICSIM_COMPOSE_SERVICE_NAME"-"$cntr
-                       fi
+                       app=$RIC_SIM_PREFIX"-"$RICSIM_COMPOSE_SERVICE_NAME"-"$cntr
                        __check_service_start $app $RIC_SIM_HTTPX"://"$app:$RIC_SIM_PORT$RIC_SIM_ALIVE_URL
                        let cntr=cntr+1
                done
@@ -335,27 +325,27 @@ get_kube_sim_host() {
 __find_sim_host() {
        if [ $RUNMODE == "KUBE" ]; then
                ricname=$(echo "$1" | tr '_' '-') # Kube does not accept underscore in names as docker do
-               ric_setname="${ricname%-*}"  #Extract the stateful set name
-               echo $RIC_SIM_HTTPX"://"$ricname.$ric_setname.$KUBE_A1SIM_NAMESPACE":"$RIC_SIM_PORT
-       else
-               if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
-                       echo $RIC_SIM_HTTPX"://"$1":"$RIC_SIM_PORT
+               if [ -z "$RIC_SIM_COMMON_SVC_NAME" ]; then
+                       ric_setname="${ricname%-*}"  #Extract the stateful set name
                else
-                       ricname=$(echo "$1" | tr '_' '-')
-                       echo $RIC_SIM_HTTPX"://"$ricname":"$RIC_SIM_PORT
+                       ric_setname=$RIC_SIM_COMMON_SVC_NAME # Use the common svc name in the host name of the sims
                fi
+               echo $RIC_SIM_HTTPX"://"$ricname.$ric_setname.$KUBE_A1SIM_NAMESPACE":"$RIC_SIM_PORT
+       else
+               ricname=$(echo "$1" | tr '_' '-')
+               echo $RIC_SIM_HTTPX"://"$ricname":"$RIC_SIM_PORT
 
        fi
 }
 
 # Generate a UUID to use as prefix for policy ids
-generate_policy_uuid() {
+sim_generate_policy_uuid() {
        UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)')
        #Reduce length to make space for serial id, uses 'a' as marker where the serial id is added
        UUID=${UUID:0:${#UUID}-4}"a"
 }
 
-# Excute a curl cmd towards a ricsimulator and check the response code.
+# Execute a curl cmd towards a ricsimulator and check the response code.
 # args: <expected-response-code> <curl-cmd-string>
 __execute_curl_to_sim() {
        echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
@@ -367,8 +357,14 @@ __execute_curl_to_sim() {
                        proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
                fi
        fi
-       echo " CMD: $2 $proxyflag" >> $HTTPLOG
-       res="$($2 $proxyflag)"
+       if [ -z "$KUBE_PROXY_CURL_JWT" ]; then
+               echo " CMD: $2 $proxyflag" >> $HTTPLOG
+               res="$($2 $proxyflag)"
+       else
+               echo " CMD: $2 $proxyflag -H Authorization: Bearer $KUBE_PROXY_CURL_JWT" >> $HTTPLOG
+               res=$($2 $proxyflag -H 'Authorization: Bearer '$KUBE_PROXY_CURL_JWT)
+       fi
+
        echo " RESP: $res" >> $HTTPLOG
        retcode=$?
     if [ $retcode -ne 0 ]; then