X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fcommon%2Ftestcase_common.sh;h=5ebc9d3ae5e77c6abfe0f5c6b9b1677fbea1302d;hb=0a882454d203e62e18f55ed1d3d5b3790801169b;hp=a94d3e2274b0b6d7e2e43bc5567e6b5eaff29084;hpb=df5eeb6e3fe42f87ac399f624edef20c87d1e475;p=nonrtric.git diff --git a/test/common/testcase_common.sh b/test/common/testcase_common.sh index a94d3e22..5ebc9d3a 100755 --- a/test/common/testcase_common.sh +++ b/test/common/testcase_common.sh @@ -29,7 +29,8 @@ __print_args() { echo " [--use-staging-image +] [--use-release-image +] [--image-repo ]" echo " [--repo-policy local|remote] [--cluster-timeout ] [--print-stats]" echo " [--override ] [--pre-clean] [--gen-stats] [--delete-namespaces]" - echo " [--delete-containers] [--endpoint-stats] [--kubeconfig ]" + echo " [--delete-containers] [--endpoint-stats] [--kubeconfig ] [--host-path-dir ]" + echo " [--kubecontext ]" } if [ $# -eq 1 ] && [ "$1" == "help" ]; then @@ -65,6 +66,8 @@ if [ $# -eq 1 ] && [ "$1" == "help" ]; then echo "--delete-containers - Delete docker containers before starting tests - but only those created by the test scripts. Docker mode only." echo "--endpoint-stats - Collect endpoint statistics" echo "--kubeconfig - Configure kubectl to use cluster specific cluster config file" + echo "--host-path-dir - (Base-)path on local-hostmounted to all VMs (nodes), for hostpath volumes in kube" + echo "--kubecontext - Configure kubectl to use a certain context, e.g 'minikube'" echo "" echo "List of app short names supported: "$APP_SHORT_NAMES exit 0 @@ -100,11 +103,6 @@ TEST_ENV_VAR_FILE_OVERRIDE="" echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@ -#Localhost constants -LOCALHOST_NAME="localhost" -LOCALHOST_HTTP="http://localhost" -LOCALHOST_HTTPS="https://localhost" - # Var to hold 'auto' in case containers shall be stopped when test case ends AUTO_CLEAN="" @@ -267,9 +265,17 @@ DELETE_KUBE_NAMESPACES=0 #Var to control if containers shall be delete before test setup DELETE_CONTAINERS=0 -#Var to configure kubectl from a config file. +#Var to configure kubectl from a config file or context KUBECONF="" +#Var pointing to dir mounted to each kubernetes node (master and workers) +#Persistent volumes using "hostpath" are allocated beneath the point. +#Typically it is a dir on local host mounted to each VM running the master and worker. +#So the intention is make this dir available so the PODs can be restarted on any +#node and still access the persistent data +#If not set from cmd line, the path is defaults to "/tmp" +HOST_PATH_BASE_DIR="" + #File to keep deviation messages DEVIATION_FILE=".tmp_deviations" rm $DEVIATION_FILE &> /dev/null @@ -441,7 +447,7 @@ __collect_endpoint_stats_image_info() { return fi ENDPOINT_STAT_FILE=$TESTLOGS/$ATC/imageinfo_$ATC_$1".log" - echo $POLICY_AGENT_IMAGE > $ENDPOINT_STAT_FILE + echo $A1PMS_IMAGE > $ENDPOINT_STAT_FILE } #Var for measuring execution time @@ -824,10 +830,17 @@ while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do fi else if [ -f $1 ]; then - KUBECONF="--kubeconfig $1" - echo "Option set - Kubeconfig path: "$1 - shift; - foundparm=0 + if [ ! -z "$KUBECONF" ]; then + paramerror=1 + if [ -z "$paramerror_str" ]; then + paramerror_str="Only one of --kubeconfig/--kubecontext can be set" + fi + else + KUBECONF="--kubeconfig $1" + echo "Option set - Kubeconfig path: "$1 + shift; + foundparm=0 + fi else paramerror=1 if [ -z "$paramerror_str" ]; then @@ -837,6 +850,44 @@ while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do fi fi fi + if [ $paramerror -eq 0 ]; then + if [ "$1" == "--kubecontext" ]; then + shift; + if [ -z "$1" ]; then + paramerror=1 + if [ -z "$paramerror_str" ]; then + paramerror_str="No context-name found for : '--kubecontext'" + fi + else + if [ ! -z "$KUBECONF" ]; then + paramerror=1 + if [ -z "$paramerror_str" ]; then + paramerror_str="Only one of --kubeconfig or --kubecontext can be set" + fi + else + KUBECONF="--context $1" + echo "Option set - Kubecontext name: "$1 + shift; + foundparm=0 + fi + fi + fi + fi + if [ $paramerror -eq 0 ]; then + if [ "$1" == "--host-path-dir" ]; then + shift; + if [ -z "$1" ]; then + paramerror=1 + if [ -z "$paramerror_str" ]; then + paramerror_str="No path found for : '--host-path-dir'" + fi + else + HOST_PATH_BASE_DIR=$1 + shift + foundparm=0 + fi + fi + fi done echo "" @@ -854,6 +905,14 @@ if [ $paramerror -eq 1 ]; then exit 1 fi +#Localhost constants +LOCALHOST_NAME="localhost" +# if [ ! -z "$DOCKER_HOST" ]; then +# LOCALHOST_NAME=$(echo $DOCKER_HOST | awk -F[/:] '{print $4}' ) +# fi +LOCALHOST_HTTP="http://$LOCALHOST_NAME" +LOCALHOST_HTTPS="https://$LOCALHOST_NAME" + # sourcing the selected env variables for the test case if [ -f "$TEST_ENV_VAR_FILE" ]; then echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD @@ -998,29 +1057,27 @@ fi echo " docker is installed and using versions:" echo " $(docker version --format 'Client version {{.Client.Version}} Server version {{.Server.Version}}')" -tmp=$(which docker-compose) -if [ $? -ne 0 ] || [ -z "$tmp" ]; then - if [ $RUNMODE == "DOCKER" ]; then +if [ $RUNMODE == "DOCKER" ]; then + tmp=$(which docker-compose) + if [ $? -ne 0 ] || [ -z "$tmp" ]; then echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED exit 1 + else + tmp=$(docker-compose version --short) + echo " docker-compose installed and using version $tmp" + if [[ "$tmp" == *'v2'* ]]; then + DOCKER_COMPOSE_VERION="V2" + fi fi fi -tmp=$(docker-compose version --short) -echo " docker-compose installed and using version $tmp" -if [[ "$tmp" == *'v2'* ]]; then - DOCKER_COMPOSE_VERION="V2" -fi - -tmp=$(which kubectl) -if [ $? -ne 0 ] || [ -z tmp ]; then - if [ $RUNMODE == "KUBE" ]; then +if [ $RUNMODE == "KUBE" ]; then + tmp=$(which kubectl) + if [ $? -ne 0 ] || [ -z tmp ]; then echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED exit 1 - fi -else - if [ $RUNMODE == "KUBE" ]; then + else echo " kubectl is installed and using versions:" - echo $(kubectl version --short=true) | indent2 + echo $(kubectl $KUBECONF version --short=true) | indent2 res=$(kubectl $KUBECONF cluster-info 2>&1) if [ $? -ne 0 ]; then echo -e "$BOLD$RED############################################# $ERED$EBOLD" @@ -1052,6 +1109,16 @@ else fi echo " Node(s) and container runtime config" kubectl $KUBECONF get nodes -o wide | indent2 + echo "" + if [ -z "$HOST_PATH_BASE_DIR" ]; then + HOST_PATH_BASE_DIR="/tmp" + echo " Persistent volumes will be mounted to $HOST_PATH_BASE_DIR on applicable node" + echo " No guarantee that persistent volume data is available on all nodes in the cluster" + else + echo "Persistent volumes will be mounted to base dir: $HOST_PATH_BASE_DIR" + echo "Assuming this dir is mounted from each node to a dir on the localhost or other" + echo "file system available to all nodes" + fi fi fi @@ -1709,8 +1776,8 @@ setup_testenvironment() { echo -e " Pulling remote snapshot or staging images my in some case result in pulling newer image versions outside the control of the test engine" export KUBE_IMAGE_PULL_POLICY="Always" fi - CLUSTER_IP=$(kubectl $KUBECONF config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}') - echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW + #CLUSTER_IP=$(kubectl $KUBECONF config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}') + #echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW echo "=================================================================================" echo "=================================================================================" @@ -2226,7 +2293,7 @@ __kube_delete_all_resources() { namespace=$1 labelname=$2 labelid=$3 - resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes serviceaccounts clusterrolebindings" + resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes serviceaccounts clusterrolebindings secrets" deleted_resourcetypes="" for restype in $resources; do ns_flag="-n $namespace" @@ -2309,7 +2376,7 @@ __kube_create_namespace() { echo " Message: $(<./tmp/kubeerr)" return 1 else - kubectl $KUBECONF label ns $1 autotest=engine + kubectl $KUBECONF label ns $1 autotest=engine > /dev/null echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN" fi else @@ -2606,14 +2673,14 @@ clean_environment() { __clean_kube if [ $PRE_CLEAN -eq 1 ]; then echo " Cleaning docker resouces to free up resources, may take time..." - ../common/clean_docker.sh 2>&1 /dev/null + ../common/clean_docker.sh 2>&1 > /dev/null echo "" fi else __clean_containers if [ $PRE_CLEAN -eq 1 ]; then echo " Cleaning kubernetes resouces to free up resources, may take time..." - ../common/clean_kube.sh $KUBECONF 2>&1 /dev/null + ../common/clean_kube.sh $KUBECONF 2>&1 > /dev/null echo "" fi fi @@ -2790,7 +2857,7 @@ __check_service_start() { echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}" - pa_st=false + a1pmsst=false echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}" TSTART=$SECONDS loop_ctr=0 @@ -2803,7 +2870,7 @@ __check_service_start() { fi echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}" echo -ne " The ${ENTITY} $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN on ${url} after $(($SECONDS-$TSTART)) seconds" - pa_st=true + a1pmsst=true break else TS_TMP=$SECONDS @@ -2819,7 +2886,7 @@ __check_service_start() { let loop_ctr=loop_ctr+1 done - if [ "$pa_st" = "false" ]; then + if [ "$a1pmsst" = "false" ]; then ((RES_CONF_FAIL++)) echo -e $RED" The ${ENTITY} ${appname} did not respond to service status on ${url} in $(($SECONDS-$TSTART)) seconds"$ERED return 1 @@ -2851,7 +2918,7 @@ __check_container_logs() { #tmp=$(docker ps | grep $appname) tmp=$(docker ps -q --filter name=$appname) #get the container id - if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps + if [ -z "$tmp" ]; then #Only check logs for running A1PMS apps echo " "$dispname" is not running, no check made" return fi @@ -2929,6 +2996,7 @@ store_logs() { ## Generic curl ############### # Generic curl function, assumes all 200-codes are ok +# Used proxy, set # args: # returns: or "" or ">"" # returns: The return code is 0 for ok and 1 for not ok @@ -2975,6 +3043,46 @@ __do_curl() { fi } +# Generic curl function, assumes all 200-codes are ok +# Uses no proxy, even if it is set +# args: +# returns: or "" or ">"" +# returns: The return code is 0 for ok and 1 for not ok +__do_curl_no_proxy() { + echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG + curlString="curl -skw %{http_code} $@" + echo " CMD: $curlString" >> $HTTPLOG + res=$($curlString) + retcode=$? + echo " RESP: $res" >> $HTTPLOG + echo " RETCODE: $retcode" >> $HTTPLOG + if [ $retcode -ne 0 ]; then + echo "" + return 1 + fi + http_code="${res:${#res}-3}" + if [ ${#res} -eq 3 ]; then + if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then + echo "" + return 1 + else + return 0 + fi + else + if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then + echo "" + return 1 + fi + if [ $# -eq 2 ]; then + echo "${res:0:${#res}-3}" | xargs + else + echo "${res:0:${#res}-3}" + fi + + return 0 + fi +} + ####################################### ### Basic helper function for test cases #######################################