Name alignment, agent to a1pms, in test env

Possibility to set kube context for tests in remote clusters

Issue-ID: NONRTRIC-728
Signed-off-by: BjornMagnussonXA <bjorn.magnusson@est.tech>
Change-Id: I697853aa54798526527a4c5999bd2c2291568399
diff --git a/test/common/testcase_common.sh b/test/common/testcase_common.sh
index 66e6dfa..d16ee6e 100755
--- a/test/common/testcase_common.sh
+++ b/test/common/testcase_common.sh
@@ -30,6 +30,7 @@
 	echo "      [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>] [--print-stats]"
 	echo "      [--override <override-environment-filename>] [--pre-clean] [--gen-stats] [--delete-namespaces]"
 	echo "      [--delete-containers] [--endpoint-stats] [--kubeconfig <config-file>] [--host-path-dir <local-host-dir>]"
+	echo "      [--kubecontext <context-name>]"
 }
 
 if [ $# -eq 1 ] && [ "$1" == "help" ]; then
@@ -66,6 +67,7 @@
 	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
@@ -101,11 +103,6 @@
 
 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=""
 
@@ -268,7 +265,7 @@
 #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)
@@ -450,7 +447,7 @@
 		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
@@ -833,10 +830,17 @@
 				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
@@ -847,6 +851,29 @@
 		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
@@ -878,6 +905,14 @@
 	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
@@ -1022,27 +1057,25 @@
 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 $KUBECONF version --short=true) | indent2
 		res=$(kubectl $KUBECONF cluster-info 2>&1)
@@ -1743,8 +1776,8 @@
 			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 "================================================================================="
@@ -2260,7 +2293,7 @@
 	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"
@@ -2343,7 +2376,7 @@
 			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
@@ -2640,14 +2673,14 @@
 		__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
@@ -2824,7 +2857,7 @@
 	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
@@ -2837,7 +2870,7 @@
 			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
@@ -2853,7 +2886,7 @@
 		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
@@ -2885,7 +2918,7 @@
 
 	#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