Test updates for ECS, PMS and SDNC
[nonrtric.git] / test / common / testcase_common.sh
index 66cbd96..b63d4d1 100755 (executable)
@@ -27,7 +27,7 @@ __print_args() {
        echo "Args: remote|remote-remove docker|kube --env-file <environment-filename> [release] [auto-clean] [--stop-at-error] "
        echo "      [--ricsim-prefix <prefix> ] [--use-local-image <app-nam>+]  [--use-snapshot-image <app-nam>+]"
        echo "      [--use-staging-image <app-nam>+] [--use-release-image <app-nam>+] [--image-repo <repo-address]"
-       echo "      [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>]"
+       echo "      [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>] [--print-stats]"
 }
 
 if [ $# -eq 1 ] && [ "$1" == "help" ]; then
@@ -55,6 +55,8 @@ if [ $# -eq 1 ] && [ "$1" == "help" ]; then
        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
@@ -194,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
@@ -204,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
@@ -220,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
 }
 
@@ -231,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
 }
 
@@ -238,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
 }
 
@@ -245,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
 }
 
@@ -255,6 +272,7 @@ __log_test_pass() {
        fi
        ((RES_PASS++))
        echo -e $GREEN" PASS"$EGREEN
+       __print_current_stats
 }
 
 #Counter for configurations
@@ -272,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
 }
 
@@ -279,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
 }
 
@@ -286,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
 }
 
@@ -295,6 +324,7 @@ __log_conf_ok() {
                echo $@
        fi
        echo -e $GREEN" OK"$EGREEN
+       __print_current_stats
 }
 
 #Var for measuring execution time
@@ -576,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 ""
 
@@ -1198,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 <App-short-name>'"$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 ""
@@ -1506,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 ""
 }
 
@@ -2466,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
@@ -2496,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
@@ -2503,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
@@ -2535,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
@@ -2551,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