Run test env towards selected kube cluster
[nonrtric.git] / test / common / pa_api_functions.sh
index 5367060..9a0a162 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 #  ============LICENSE_START===============================================
-#  Copyright (C) 2020 Nordix Foundation. All rights reserved.
+#  Copyright (C) 2021 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.
@@ -78,7 +78,7 @@ __PA_kube_delete_all() {
 # args: <log-dir> <file-prexix>
 __PA_store_docker_logs() {
        if [ $RUNMODE == "KUBE" ]; then
-               kubectl  logs -l "autotest=PA" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_policy-agent.log 2>&1
+               kubectl $KUBECONF  logs -l "autotest=PA" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_policy-agent.log 2>&1
        else
                docker logs $POLICY_AGENT_APP_NAME > $1$2_policy-agent.log 2>&1
        fi
@@ -103,6 +103,12 @@ __PA_statisics_setup() {
        fi
 }
 
+# Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
+# args: -
+__PA_test_requirements() {
+       :
+}
+
 
 #######################################################
 
@@ -149,7 +155,7 @@ use_agent_dmaap_https() {
 # args: <protocol> <internal-port> <external-port>
 __agent_set_protocoll() {
        echo -e $BOLD"$POLICY_AGENT_DISPLAY_NAME protocol setting"$EBOLD
-       echo -e " Using $BOLD http $EBOLD towards $POLICY_AGENT_DISPLAY_NAME"
+       echo -e " Using $BOLD $1 $EBOLD towards $POLICY_AGENT_DISPLAY_NAME"
 
        ## Access to Dmaap adapter
 
@@ -305,7 +311,7 @@ start_policy_agent() {
 
                # 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
-                       __PA_WORKER_NODE=$(kubectl get pod -l "autotest=PA" -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[*].spec.nodeName}')
+                       __PA_WORKER_NODE=$(kubectl $KUBECONF get pod -l "autotest=PA" -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[*].spec.nodeName}')
                        if [ -z "$__PA_WORKER_NODE" ]; then
                                echo -e $YELLOW" Cannot find worker node for pod for $POLICY_AGENT_APP_NAME, persistency may not work"$EYELLOW
                        fi
@@ -352,6 +358,8 @@ start_policy_agent() {
 
                __check_service_start $POLICY_AGENT_APP_NAME $PA_SERVICE_PATH$POLICY_AGENT_ALIVE_URL
        fi
+
+       __collect_endpoint_stats_image_info "PMS" $POLICY_AGENT_IMAGE
        echo ""
        return 0
 }
@@ -374,7 +382,7 @@ stop_policy_agent() {
                fi
                __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest PA
                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=PA")
+               tmp=$(kubectl $KUBECONF delete rs -n $KUBE_NONRTRIC_NAMESPACE -l "autotest=PA")
                if [ $? -ne 0 ]; then
                        echo -e $RED" Could not delete replica set "$RED
                        ((RES_CONF_FAIL++))
@@ -420,7 +428,7 @@ start_stopped_policy_agent() {
                else
                        echo -e $BOLD" Setting nodeSelector kubernetes.io/hostname=$__PA_WORKER_NODE to deployment for $POLICY_AGENT_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 $POLICY_AGENT_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE --patch '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "'$__PA_WORKER_NODE'"}}}}}')
+                       tmp=$(kubectl $KUBECONF patch deployment $POLICY_AGENT_APP_NAME -n $KUBE_NONRTRIC_NAMESPACE --patch '{"spec": {"template": {"spec": {"nodeSelector": {"kubernetes.io/hostname": "'$__PA_WORKER_NODE'"}}}}}')
                        if [ $? -ne 0 ]; then
                                echo -e $YELLOW" Cannot set nodeSelector to deployment for $POLICY_AGENT_APP_NAME, persistency may not work"$EYELLOW
                        fi
@@ -500,13 +508,13 @@ prepare_consul_config() {
        config_json=$config_json"\n   \"ric\": ["
 
        if [ $RUNMODE == "KUBE" ]; then
-               result=$(kubectl get pods -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
+               result=$(kubectl $KUBECONF get pods -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
                rics=""
                ric_cntr=0
                if [ $? -eq 0 ] && [ ! -z "$result" ]; then
                        for im in $result; do
                                if [[ $im != *"-0" ]]; then
-                                       ric_subdomain=$(kubectl get pod $im -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.spec.subdomain}')
+                                       ric_subdomain=$(kubectl $KUBECONF get pod $im -n $KUBE_A1SIM_NAMESPACE -o jsonpath='{.spec.subdomain}')
                                        rics=$rics" "$im"."$ric_subdomain"."$KUBE_A1SIM_NAMESPACE
                                        let ric_cntr=ric_cntr+1
                                fi
@@ -533,7 +541,11 @@ prepare_consul_config() {
                        ric_id=${ric%.*.*} #extract pod id from full hosthame
                        ric_id=$(echo "$ric_id" | tr '-' '_')
                else
-                       ric_id=$ric
+                       if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
+                               ric_id=$ric
+                       else
+                               ric_id=$(echo "$ric" | tr '-' '_')  #ric id still needs underscore as it is different from the container name
+                       fi
                fi
                echo " Found a1 sim: "$ric_id
                config_json=$config_json"\n            \"name\": \"$ric_id\","
@@ -814,7 +826,7 @@ api_get_policies() {
                        fi
                fi
        fi
-
+       __collect_endpoint_stats "PMS" 00 "GET" $PMS_API_PREFIX"/v2/policy-instances" $status
        __log_test_pass
        return 0
 
@@ -896,6 +908,7 @@ api_get_policy() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 01 "GET" $PMS_API_PREFIX"/v2/policies/{policy_id}" $status
        __log_test_pass
        return 0
 }
@@ -984,11 +997,11 @@ api_put_policy() {
                        __log_test_fail_status_code $1 $status
                        return 1
                fi
-
                let pid=$pid+1
                let count=$count+1
                echo -ne " Executed  "$count"("$max")${SAMELINE}"
        done
+       __collect_endpoint_stats "PMS" 02 "PUT" $PMS_API_PREFIX"/v2/policies" $status $max
        echo ""
 
        __log_test_pass
@@ -1103,6 +1116,7 @@ api_put_policy_batch() {
                let count=$count+1
                echo -ne " Accepted(batch)  "$count"("$max")${SAMELINE}"
        done
+       __collect_endpoint_stats "PMS" 02 "PUT" $PMS_API_PREFIX"/v2/policies" $1 $max
 
        echo ""
 
@@ -1214,6 +1228,7 @@ api_put_policy_parallel() {
                fi
        done
        if [ -z $msg ]; then
+               __collect_endpoint_stats "PMS" 02 "PUT" $PMS_API_PREFIX"/v2/policies" $resp_code $(($count*$num_rics))
                __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
                return 0
        fi
@@ -1257,10 +1272,12 @@ api_delete_policy() {
                        __log_test_fail_status_code $1 $status
                        return 1
                fi
+
                let pid=$pid+1
                let count=$count+1
                echo -ne " Executed  "$count"("$max")${SAMELINE}"
        done
+       __collect_endpoint_stats "PMS" 03 "DELETE" $PMS_API_PREFIX"/v2/policies/{policy_id}" $status $max
        echo ""
 
        __log_test_pass
@@ -1327,6 +1344,7 @@ api_delete_policy_batch() {
                let count=$count+1
                echo -ne " Deleted(batch)  "$count"("$max")${SAMELINE}"
        done
+       __collect_endpoint_stats "PMS" 03 "DELETE" $PMS_API_PREFIX"/v2/policies/{policy_id}" $1 $max
 
        echo ""
 
@@ -1403,6 +1421,7 @@ api_delete_policy_parallel() {
                fi
        done
        if [ -z $msg ]; then
+               __collect_endpoint_stats "PMS" 03 "DELETE" $PMS_API_PREFIX"/v2/policies/{policy_id}" $resp_code $(($count*$num_rics))
                __log_test_pass " $(($count*$num_rics)) policy request(s) executed"
                return 0
        fi
@@ -1502,6 +1521,7 @@ api_get_policy_ids() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 04 "GET" $PMS_API_PREFIX"/v2/policies" $status
        __log_test_pass
        return 0
 }
@@ -1546,6 +1566,7 @@ api_get_policy_type() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 05 "GET" $PMS_API_PREFIX"/v2/policy-types/{policyTypeId}" $status
        __log_test_pass
        return 0
 }
@@ -1589,6 +1610,7 @@ api_get_policy_schema() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 06 "GET" $PMS_API_PREFIX"/v2/policy_schema" $status
        __log_test_pass
        return 0
 }
@@ -1656,30 +1678,32 @@ api_get_policy_schemas() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 07 "GET" $PMS_API_PREFIX"/v2/policy-schemas" $status
        __log_test_pass
        return 0
 }
 
 # API Test function: GET /policy_status and V2 GET /policies/{policy_id}/status
-# arg: <response-code> <policy-id> (STD|STD2 <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)
+# arg: <response-code> <policy-id> [ (STD|STD2 <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>) ]
 # (Function for test scripts)
 api_get_policy_status() {
        __log_test_start $@
 
-    if [ $# -lt 4 ] || [ $# -gt 5 ]; then
-               __print_err "<response-code> <policy-id> (STD <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)" $@
+    if [ $# -lt 2 ] || [ $# -gt 5 ]; then
+               __print_err "<response-code> <policy-id> [(STD <enforce-status>|EMPTY [<reason>|EMPTY])|(OSC <instance-status> <has-been-deleted>)]" $@
                return 1
        fi
 
        targetJson=""
-
-       if [ $3 == "STD" ]; then
+       if [ $# -eq 2 ]; then
+               :
+       elif [ "$3" == "STD" ]; then
                targetJson="{\"enforceStatus\":\"$4\""
                if [ $# -eq 5 ]; then
                        targetJson=$targetJson",\"reason\":\"$5\""
                fi
                targetJson=$targetJson"}"
-       elif [ $3 == "STD2" ]; then
+       elif [ "$3" == "STD2" ]; then
                if [ $4 == "EMPTY" ]; then
                        targetJson="{\"enforceStatus\":\"\""
                else
@@ -1693,7 +1717,7 @@ api_get_policy_status() {
                        fi
                fi
                targetJson=$targetJson"}"
-       elif [ $3 == "OSC" ]; then
+       elif [ "$3" == "OSC" ]; then
                targetJson="{\"instance_status\":\"$4\""
                if [ $# -eq 5 ]; then
                        targetJson=$targetJson",\"has_been_deleted\":\"$5\""
@@ -1718,16 +1742,17 @@ api_get_policy_status() {
                __log_test_fail_status_code $1 $status
                return 1
        fi
+       if [ $# -gt 2 ]; then
+               echo "TARGET JSON: $targetJson" >> $HTTPLOG
+               body=${res:0:${#res}-3}
+               res=$(python3 ../common/compare_json.py "$targetJson" "$body")
 
-       echo "TARGET JSON: $targetJson" >> $HTTPLOG
-       body=${res:0:${#res}-3}
-       res=$(python3 ../common/compare_json.py "$targetJson" "$body")
-
-       if [ $res -ne 0 ]; then
-               __log_test_fail_body
-               return 1
+               if [ $res -ne 0 ]; then
+                       __log_test_fail_body
+                       return 1
+               fi
        fi
-
+       __collect_endpoint_stats "PMS" 08 "GET" $PMS_API_PREFIX"/v2/policies/{policy_id}/status" $status
        __log_test_pass
        return 0
 }
@@ -1796,6 +1821,7 @@ api_get_policy_types() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 09 "GET" $PMS_API_PREFIX"/v2/policy-types" $status
        __log_test_pass
        return 0
 }
@@ -1826,6 +1852,33 @@ api_get_status() {
                return 1
        fi
 
+       __collect_endpoint_stats "PMS" 10 "GET" $PMS_API_PREFIX"/v2/status" $status
+       __log_test_pass
+       return 0
+}
+
+# API Test function: GET /status (root) without api prefix
+# args: <response-code>
+# (Function for test scripts)
+api_get_status_root() {
+       __log_test_start $@
+    if [ $# -ne 1 ]; then
+               __print_err "<response-code>" $@
+               return 1
+       fi
+       query="/status"
+       TMP_PREFIX=$PMS_API_PREFIX
+       PMS_API_PREFIX=""
+    res="$(__do_curl_to_api PA GET $query)"
+       PMS_API_PREFIX=$TMP_PREFIX
+    status=${res:${#res}-3}
+
+       if [ $status -ne $1 ]; then
+               __log_test_fail_status_code $1 $status
+               return 1
+       fi
+
+       __collect_endpoint_stats "PMS" 19 "GET" "/status" $status
        __log_test_pass
        return 0
 }
@@ -1912,6 +1965,8 @@ api_get_ric() {
                        fi
                fi
        fi
+
+       __collect_endpoint_stats "PMS" 11 "GET" $PMS_API_PREFIX"/v2/rics/ric" $status
        __log_test_pass
        return 0
 }
@@ -1973,6 +2028,7 @@ api_get_rics() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 12 "GET" $PMS_API_PREFIX"/v2/rics" $status
        __log_test_pass
        return 0
 }
@@ -2009,6 +2065,7 @@ api_put_service() {
                return 1
        fi
 
+       __collect_endpoint_stats "PMS" 13 "PUT" $PMS_API_PREFIX"/v2/service" $status
        __log_test_pass
        return 0
 }
@@ -2092,6 +2149,7 @@ api_get_services() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 14 "GET" $PMS_API_PREFIX"/v2/services" $status
        __log_test_pass
        return 0
 }
@@ -2145,6 +2203,7 @@ api_get_service_ids() {
                return 1
        fi
 
+       __collect_endpoint_stats "PMS" 14 "GET" $PMS_API_PREFIX"/v2/services" $status
        __log_test_pass
        return 0
 }
@@ -2172,6 +2231,7 @@ api_delete_services() {
                return 1
        fi
 
+       __collect_endpoint_stats "PMS" 15 "DELETE" $PMS_API_PREFIX"/v2/services/{serviceId}" $status
        __log_test_pass
        return 0
 }
@@ -2200,6 +2260,7 @@ api_put_services_keepalive() {
                return 1
        fi
 
+       __collect_endpoint_stats "PMS" 16 "PUT" $PMS_API_PREFIX"/v2/services/{service_id}/keepalive" $status
        __log_test_pass
        return 0
 }
@@ -2228,7 +2289,9 @@ api_put_configuration() {
                return 1
        fi
        inputJson=$(< $2)
-       inputJson="{\"config\":"$inputJson"}"
+       if [ $RUNMODE == "DOCKER" ]; then  #In kube the file already has a header
+               inputJson="{\"config\":"$inputJson"}"
+       fi
        file="./tmp/.config.json"
        echo $inputJson > $file
        query="/v2/configuration"
@@ -2240,6 +2303,7 @@ api_put_configuration() {
                return 1
        fi
 
+       __collect_endpoint_stats "PMS" 17 "PUT" $PMS_API_PREFIX"/v2/configuration" $status
        __log_test_pass
        return 0
 }
@@ -2288,6 +2352,7 @@ api_get_configuration() {
                fi
        fi
 
+       __collect_endpoint_stats "PMS" 18 "GET" $PMS_API_PREFIX"/v2/configuration" $status
        __log_test_pass
        return 0
 }
@@ -2305,7 +2370,7 @@ api_get_configuration() {
 pms_kube_pvc_reset() {
        __log_test_start $@
 
-       pvc_name=$(kubectl get pvc -n $KUBE_NONRTRIC_NAMESPACE  --no-headers -o custom-columns=":metadata.name" | grep policy)
+       pvc_name=$(kubectl $KUBECONF get pvc -n $KUBE_NONRTRIC_NAMESPACE  --no-headers -o custom-columns=":metadata.name" | grep policy)
        if [ -z "$pvc_name" ]; then
                pvc_name=policymanagementservice-vardata-pvc
        fi