+#######################################
+### Basic helper function for test cases
+#######################################
+
+# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
+# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
+# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
+# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
+# before setting pass or fail depending on the result of comparing variable and target using the operator.
+# If the <variable-name> has the 'json:' prefix, the the variable will be used as url and the <target-value> will be compared towards the length of the json array in the response.
+# Not to be called from test script.
+
+__var_test() {
+ checkjsonarraycount=0
+
+ if [ $# -eq 6 ]; then
+ if [[ $3 == "json:"* ]]; then
+ checkjsonarraycount=1
+ fi
+
+ #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
+ echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"
+ ((RES_TEST++))
+ start=$SECONDS
+ ctr=0
+ for (( ; ; )); do
+ if [ $checkjsonarraycount -eq 0 ]; then
+ result="$(__do_curl $2$3)"
+ retcode=$?
+ result=${result//[[:blank:]]/} #Strip blanks
+ else
+ path=${3:5}
+ result="$(__do_curl $2$path)"
+ retcode=$?
+ echo "$result" > .tmp.curl.json
+ result=$(python ../common/count_json_elements.py ".tmp.curl.json")
+ fi
+ duration=$((SECONDS-start))
+ echo -ne " Result=${result} after ${duration} seconds\033[0K\r"
+ let ctr=ctr+1
+ if [ $retcode -ne 0 ]; then
+ if [ $duration -gt $6 ]; then
+ ((RES_FAIL++))
+ #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
+ echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
+ return
+ fi
+ elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
+ ((RES_PASS++))
+ echo -e " Result=${result} after ${duration} seconds\033[0K\r"
+ echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
+ return
+ elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
+ ((RES_PASS++))
+ echo -e " Result=${result} after ${duration} seconds\033[0K\r"
+ echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
+ return
+ elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
+ ((RES_PASS++))
+ echo -e " Result=${result} after ${duration} seconds\033[0K\r"
+ echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
+ return
+ elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
+ ((RES_PASS++))
+ echo -e " Result=${result} after ${duration} seconds\033[0K\r"
+ echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
+ return
+ else
+ if [ $duration -gt $6 ]; then
+ ((RES_FAIL++))
+ echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
+ #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
+ return
+ fi
+ fi
+ sleep 1
+ done
+ elif [ $# -eq 5 ]; then
+ if [[ $3 == "json:"* ]]; then
+ checkjsonarraycount=1
+ fi
+
+ #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
+ echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
+ ((RES_TEST++))
+ if [ $checkjsonarraycount -eq 0 ]; then
+ result="$(__do_curl $2$3)"
+ retcode=$?
+ result=${result//[[:blank:]]/} #Strip blanks
+ else
+ path=${3:5}
+ result="$(__do_curl $2$path)"
+ retcode=$?
+ echo "$result" > .tmp.curl.json
+ result=$(python ../common/count_json_elements.py ".tmp.curl.json")
+ fi
+ if [ $retcode -ne 0 ]; then
+ ((RES_FAIL++))
+ #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
+ echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
+ elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
+ ((RES_PASS++))
+ echo -e $GREEN" PASS${EGREEN} - Result=${result}"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met"
+ elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
+ ((RES_PASS++))
+ echo -e $GREEN" PASS${EGREEN} - Result=${result}"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
+ elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
+ ((RES_PASS++))
+ echo -e $GREEN" PASS${EGREEN} - Result=${result}"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
+ elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
+ ((RES_PASS++))
+ echo -e $GREEN" PASS${EGREEN} - Result=${result}"
+ #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
+ else
+ ((RES_FAIL++))
+ echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
+ #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
+ fi
+ else
+ echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
+ echo "Got:" $@
+ exit 1
+ fi
+}
+
+
+### Generic test cases for varaible checking
+
+# Tests if a variable value in the CR is equal to a target value and and optional timeout.
+# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
+# equal to the target or not.
+# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
+# before setting pass or fail depending on if the variable value becomes equal to the target
+# value or not.
+# (Function for test scripts)
+cr_equal() {
+ if [ $# -eq 2 ] || [ $# -eq 3 ]; then
+ __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
+ else
+ ((RES_CONF_FAIL++))
+ __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
+ fi
+}
+
+# Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
+# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
+# equal to the target or not.
+# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
+# before setting pass or fail depending on if the variable value becomes equal to the target
+# value or not.
+# (Function for test scripts)
+mr_equal() {
+ if [ $# -eq 2 ] || [ $# -eq 3 ]; then
+ __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
+ else
+ ((RES_CONF_FAIL++))
+ __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
+ fi
+}
+
+# Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
+# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
+# greater than the target or not.
+# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
+# before setting pass or fail depending on if the variable value becomes greater than the target
+# value or not.
+# (Function for test scripts)
+mr_greater() {
+ if [ $# -eq 2 ] || [ $# -eq 3 ]; then
+ __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
+ else
+ ((RES_CONF_FAIL++))
+ __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
+ fi
+}
+
+# Read a variable value from MR sim and send to stdout. Arg: <variable-name>
+mr_read() {
+ echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
+}
+
+# Print a variable value from the MR stub.
+# arg: <variable-name>
+# (Function for test scripts)
+mr_print() {
+ if [ $# != 1 ]; then
+ ((RES_CONF_FAIL++))
+ __print_err "need one arg, <mr-param>" $@
+ exit 1
+ fi
+ echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD
+}
+
+