X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fcommon%2Ftestcase_common.sh;h=b63d4d129e7d55b1f09d1c485ab6d7ba0f5a765d;hb=84ffd4a403baee1b7aa86f9986f0c1659a5a0881;hp=b4ed9b4a05e5233a53175a85f3285951f1eda351;hpb=c4d4c18bdac10d5ae35f24d8bcd9db2271124c0b;p=nonrtric.git diff --git a/test/common/testcase_common.sh b/test/common/testcase_common.sh index b4ed9b4a..b63d4d12 100755 --- a/test/common/testcase_common.sh +++ b/test/common/testcase_common.sh @@ -27,7 +27,7 @@ __print_args() { echo "Args: remote|remote-remove docker|kube --env-file [release] [auto-clean] [--stop-at-error] " echo " [--ricsim-prefix ] [--use-local-image +] [--use-snapshot-image +]" echo " [--use-staging-image +] [--use-release-image +] [--image-repo ]" + echo " [--repo-policy local|remote] [--cluster-timeout ] [--print-stats]" } if [ $# -eq 1 ] && [ "$1" == "help" ]; then @@ -53,7 +53,10 @@ if [ $# -eq 1 ] && [ "$1" == "help" ]; then echo "--use-staging-image - The script will use images from the nexus staging repo for the supplied apps, space separated list of app short names" echo "--use-release-image - The script will use images from the nexus release repo for the supplied apps, space separated list of app short names" echo "--image-repo - Url to optional image repo. Only locally built images will be re-tagged and pushed to this repo" + echo "--repo-policy - Policy controlling which images to re-tag and push if param --image-repo is set. Default is 'local'" echo "--cluster-timeout - Optional timeout for cluster where it takes time to obtain external ip/host-name. Timeout in seconds. " + echo "--print-stats - Print current test stats after each test." + echo "" echo "List of app short names supported: "$APP_SHORT_NAMES exit 0 @@ -193,6 +196,9 @@ RES_FAIL=0 RES_CONF_FAIL=0 RES_DEVIATION=0 +#Var to control if current stats shall be printed +PRINT_CURRENT_STATS=0 + #File to keep deviation messages DEVIATION_FILE=".tmp_deviations" rm $DEVIATION_FILE &> /dev/null @@ -203,6 +209,7 @@ trap_fnc() { if [ $? -eq 127 ]; then echo -e $RED"Function not found, setting script to FAIL"$ERED ((RES_CONF_FAIL++)) + __print_current_stats fi } trap trap_fnc ERR @@ -219,10 +226,18 @@ __log_test_start() { ((TEST_SEQUENCE_NR++)) } +# Function to print current statistics +__print_current_stats() { + if [ $PRINT_CURRENT_STATS -ne 0 ]; then + echo " Current stats - exe-time, tests, passes, fails, conf fails, deviations: $(($SECONDS-$TCTEST_START)), $RES_TEST, $RES_PASS, $RES_FAIL, $RES_CONF_FAIL, $RES_DEVIATION" + fi +} + # General function to log a failed test case __log_test_fail_general() { echo -e $RED" FAIL."$1 $ERED ((RES_FAIL++)) + __print_current_stats __check_stop_at_error } @@ -230,6 +245,7 @@ __log_test_fail_general() { __log_test_fail_status_code() { echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED ((RES_FAIL++)) + __print_current_stats __check_stop_at_error } @@ -237,6 +253,7 @@ __log_test_fail_status_code() { __log_test_fail_body() { echo -e $RED" FAIL, returned body not correct"$ERED ((RES_FAIL++)) + __print_current_stats __check_stop_at_error } @@ -244,6 +261,7 @@ __log_test_fail_body() { __log_test_fail_not_supported() { echo -e $RED" FAIL, function not supported"$ERED ((RES_FAIL++)) + __print_current_stats __check_stop_at_error } @@ -254,6 +272,7 @@ __log_test_pass() { fi ((RES_PASS++)) echo -e $GREEN" PASS"$EGREEN + __print_current_stats } #Counter for configurations @@ -271,6 +290,7 @@ __log_conf_start() { __log_conf_fail_general() { echo -e $RED" FAIL."$1 $ERED ((RES_CONF_FAIL++)) + __print_current_stats __check_stop_at_error } @@ -278,6 +298,7 @@ __log_conf_fail_general() { __log_conf_fail_status_code() { echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED ((RES_CONF_FAIL++)) + __print_current_stats __check_stop_at_error } @@ -285,6 +306,15 @@ __log_conf_fail_status_code() { __log_conf_fail_body() { echo -e $RED" FAIL, returned body not correct"$ERED ((RES_CONF_FAIL++)) + __print_current_stats + __check_stop_at_error +} + +# Function to log a configuration that is not supported +__log_conf_fail_not_supported() { + echo -e $RED" FAIL, function not supported"$ERED$@ + ((RES_CONF_FAIL++)) + __print_current_stats __check_stop_at_error } @@ -294,6 +324,7 @@ __log_conf_ok() { echo $@ fi echo -e $GREEN" OK"$EGREEN + __print_current_stats } #Var for measuring execution time @@ -303,8 +334,9 @@ TCTEST_START=$SECONDS TIMER_MEASUREMENTS=".timer_measurement.txt" echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS -# If this is set, all used images will be re-tagged and pushed to this repo before any +# If this is set, some images (control by the parameter repo-polcy) will be re-tagged and pushed to this repo before any IMAGE_REPO_ADR="" +IMAGE_REPO_POLICY="local" CLUSTER_TIME_OUT=0 echo "-------------------------------------------------------------------------------------------------" @@ -525,6 +557,29 @@ while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do fi fi fi + if [ $paramerror -eq 0 ]; then + if [ "$1" == "--repo-policy" ]; then + shift; + IMAGE_REPO_POLICY=$1 + if [ -z "$1" ]; then + paramerror=1 + if [ -z "$paramerror_str" ]; then + paramerror_str="No policy found for : '--repo-policy'" + fi + else + if [ "$1" == "local" ] || [ "$1" == "remote" ]; then + echo "Option set - Image repo policy: "$1 + shift; + foundparm=0 + else + paramerror=1 + if [ -z "$paramerror_str" ]; then + paramerror_str="Repo policy shall be 'local' or 'remote'" + fi + fi + fi + fi + fi if [ $paramerror -eq 0 ]; then if [ "$1" == "--cluster-timeout" ]; then shift; @@ -551,6 +606,14 @@ while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do fi fi fi + if [ $paramerror -eq 0 ]; then + if [ "$1" == "--print-stats" ]; then + PRINT_CURRENT_STATS=1 + echo "Option set - Print stats" + shift; + foundparm=0 + fi + fi done echo "" @@ -647,6 +710,37 @@ 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 + res=$(kubectl cluster-info 2>&1) + if [ $? -ne 0 ]; then + echo -e "$BOLD$RED############################################# $ERED$EBOLD" + echo -e $BOLD$RED"Command 'kubectl cluster-info' returned error $ERED$EBOLD" + echo -e "$BOLD$RED############################################# $ERED$EBOLD" + echo " " + echo "kubectl response:" + echo $res + echo " " + echo "This script may have been started with user with no permission to run kubectl" + echo "Try running with 'sudo' or set 'KUBECONFIG'" + echo "Do either 1, 2 or 3 " + echo " " + echo "1" + echo "Run with sudo" + echo -e $BOLD"sudo "$EBOLD + echo " " + echo "2" + echo "Export KUBECONFIG and pass env to sudo - (replace user)" + echo -e $BOLD"export KUBECONFIG='/home//.kube/config'"$EBOLD + echo -e $BOLD"sudo -E "$EBOLD + echo " " + echo "3" + echo "Set KUBECONFIG inline (replace user)" + echo -e $BOLD"sudo KUBECONFIG='/home//.kube/config' "$EBOLD + + exit 1 + fi + fi fi echo -e $BOLD"Checking configured image setting for this test case"$EBOLD @@ -735,8 +829,24 @@ __check_and_create_image_var() { echo -e "$tmp" >> $image_list_file #Export the env var export "${2}"=$image":"$tag #Note, this var may be set to the value of the target value below in __check_and_pull_image - if [ ! -z "$IMAGE_REPO_ADR" ] && [ $5 == "LOCAL" ]; then # Only push local images if repo is given + + remote_or_local_push=false + if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then + if [ $5 == "LOCAL" ]; then + remote_or_local_push=true + fi + if [[ $5 == *"REMOTE"* ]]; then + if [ "$IMAGE_REPO_POLICY" == "remote" ]; then + remote_or_local_push=true + fi + fi + fi + if $remote_or_local_push; then # Only re-tag and push images according to policy, if repo is given export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image + if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo + optional_image_repo_target_tmp=${optional_image_repo_target//\//_} + optional_image_repo_target=$optional_image_repo_target_tmp + fi export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed else export "${2}_SOURCE"="" @@ -897,6 +1007,11 @@ __retag_and_push_image() { source_image="${!1}" trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var target_image="${!trg_var_name}" + + if [ -z $target_image ]; then + return 0 # Image with no target shall not be pushed + fi + echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}" tmp=$(docker image tag $source_image ${target_image} ) if [ $? -ne 0 ]; then @@ -1077,8 +1192,8 @@ setup_testenvironment() { echo -e $BOLD"Pulling configured images, if needed"$EBOLD - if [ ! -z "$IMAGE_REPO_ADR" ]; then - echo -e $YELLOW" Excluding all remote image check/pull when running with image repo: $IMAGE_REPO_ADR"$EYELLOW + if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then + echo -e $YELLOW" Excluding all remote image check/pull when running with image repo: $IMAGE_REPO_ADR and image policy $IMAGE_REPO_POLICY"$EYELLOW else for imagename in $APP_SHORT_NAMES; do __check_included_image $imagename @@ -1121,8 +1236,11 @@ setup_testenvironment() { echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED echo -e $RED"Or local image, overriding remote image, does not exist"$ERED if [ $IMAGE_CATEGORY == "DEV" ]; then - echo -e $RED"Note that SNAPSHOT images may be purged from nexus after a certain period."$ERED - echo -e $RED"In that case, switch to use a released image instead."$ERED + echo "" + echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED + echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED + echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image '"$ERED + echo -e $RED"Use the 'App-short-name' for the applicable image from the above table: 'Images configured for start arg'."$ERED fi echo "#################################################################################################" echo "" @@ -1429,6 +1547,7 @@ deviation() { ((RES_DEVIATION++)) echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE + __print_current_stats echo "" } @@ -1573,6 +1692,23 @@ __clean_containers() { ### Functions for kube management ################################### +# Get resource type for scaling +# args: +__kube_get_resource_type() { + kubectl get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr + if [ $? -eq 0 ]; then + echo "deployment" + return 0 + fi + kubectl get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr + if [ $? -eq 0 ]; then + echo "sts" + return 0 + fi + echo "unknown-resource-type" + return 1 +} + # Scale a kube resource to a specific count # args: # (Not for test scripts) @@ -1927,7 +2063,7 @@ __kube_clean_pvc() { envsubst < $input_yaml > $output_yaml - kubectl delete -f $output_yaml #> /dev/null 2>&1 # Delete the previous terminated pod - if existing + kubectl delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing __kube_create_instance pod pvc-cleaner $input_yaml $output_yaml if [ $? -ne 0 ]; then @@ -2104,13 +2240,6 @@ __start_container() { appcount=$1 shift - os_version=$(uname -a 2> /dev/null | awk '{print tolower($0)}' | grep "microsoft") - if [[ "$os_version" == *"microsoft"* ]]; then - echo -e $YELLOW" Workaround for Linux on Win - delay container start, 1 sec, to make sure files mounted in the container are available on disk - WLS problem"$EYELLOW - sleep 1 - fi - - if [ "$compose_args" == "NODOCKERARGS" ]; then docker-compose -f $compose_file up -d &> .dockererr if [ $? -ne 0 ]; then @@ -2325,7 +2454,11 @@ __do_curl() { proxyflag="" if [ $RUNMODE == "KUBE" ]; then if [ ! -z "$KUBE_PROXY_PATH" ]; then - proxyflag=" --proxy $KUBE_PROXY_PATH" + if [ $KUBE_PROXY_HTTPX == "http" ]; then + proxyflag=" --proxy $KUBE_PROXY_PATH" + else + proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH" + fi fi fi curlString="curl -skw %{http_code} $proxyflag $@" @@ -2375,13 +2508,15 @@ __do_curl() { __var_test() { checkjsonarraycount=0 - + TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") if [ $# -eq 6 ]; then if [[ $3 == "json:"* ]]; then checkjsonarraycount=1 fi echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD + echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG + ((RES_TEST++)) ((TEST_SEQUENCE_NR++)) start=$SECONDS @@ -2405,6 +2540,7 @@ __var_test() { if [ $duration -gt $6 ]; then ((RES_FAIL++)) echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}" + __print_current_stats __check_stop_at_error return fi @@ -2412,26 +2548,31 @@ __var_test() { ((RES_PASS++)) echo -e " Result=${result} after ${duration} seconds${SAMELINE}" echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" + __print_current_stats return elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then ((RES_PASS++)) echo -e " Result=${result} after ${duration} seconds${SAMELINE}" echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" + __print_current_stats return elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then ((RES_PASS++)) echo -e " Result=${result} after ${duration} seconds${SAMELINE}" echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" + __print_current_stats return elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then ((RES_PASS++)) echo -e " Result=${result} after ${duration} seconds${SAMELINE}" echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" + __print_current_stats return else if [ $duration -gt $6 ]; then ((RES_FAIL++)) echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}" + __print_current_stats __check_stop_at_error return fi @@ -2444,6 +2585,7 @@ __var_test() { fi echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD + echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG ((RES_TEST++)) ((TEST_SEQUENCE_NR++)) if [ $checkjsonarraycount -eq 0 ]; then @@ -2460,22 +2602,28 @@ __var_test() { if [ $retcode -ne 0 ]; then ((RES_FAIL++)) echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}" + __print_current_stats __check_stop_at_error elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then ((RES_PASS++)) echo -e $GREEN" PASS${EGREEN} - Result=${result}" + __print_current_stats elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then ((RES_PASS++)) echo -e $GREEN" PASS${EGREEN} - Result=${result}" + __print_current_stats elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then ((RES_PASS++)) echo -e $GREEN" PASS${EGREEN} - Result=${result}" + __print_current_stats elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then ((RES_PASS++)) echo -e $GREEN" PASS${EGREEN} - Result=${result}" + __print_current_stats else ((RES_FAIL++)) echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}" + __print_current_stats __check_stop_at_error fi else