X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=test%2Fcommon%2Fcr_api_functions.sh;h=a29d010f8a09cbcc63e3ce17380e5bf9bf01bbef;hb=refs%2Fchanges%2F62%2F8262%2F1;hp=c19797a0a7052a4eacb38d2ce18695578d0ea7a4;hpb=3cc0b58e9a786f619af2af318ef2028179ed2dab;p=nonrtric.git diff --git a/test/common/cr_api_functions.sh b/test/common/cr_api_functions.sh index c19797a0..a29d010f 100644 --- a/test/common/cr_api_functions.sh +++ b/test/common/cr_api_functions.sh @@ -93,81 +93,144 @@ __CR_kube_delete_all() { # This function is called for apps managed by the test script. # args: __CR_store_docker_logs() { - docker logs $CR_APP_NAME > $1$2_cr.log 2>&1 + if [ $RUNMODE == "KUBE" ]; then + for podname in $(kubectl $KUBECONF get pods -n $KUBE_SIM_NAMESPACE -l "autotest=CR" -o custom-columns=":metadata.name"); do + kubectl $KUBECONF logs -n $KUBE_SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1 + done + else + crs=$(docker ps --filter "name=$CR_APP_NAME" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}}) + for crid in $crs; do + docker logs $crid > $1$2_$crid.log 2>&1 + done + fi } -####################################################### +# Initial setup of protocol, host and ports +# This function is called for apps managed by the test script. +# args: - +__CR_initial_setup() { + use_cr_http +} + +# Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers +# For docker, the namespace shall be excluded +# This function is called for apps managed by the test script as well as for prestarted apps. +# args: - +__CR_statisics_setup() { + for ((CR_INSTANCE=MAX_CR_APP_COUNT; CR_INSTANCE>0; CR_INSTANCE-- )); do + if [ $RUNMODE == "KUBE" ]; then + CR_INSTANCE_KUBE=$(($CR_INSTANCE-1)) + echo -n " CR-$CR_INSTANCE_KUBE $CR_APP_NAME-$CR_INSTANCE_KUBE $KUBE_SIM_NAMESPACE " + else + if [ $DOCKER_COMPOSE_VERSION == "V1" ]; then + echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}_cr_$CR_INSTANCE " + else + echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}-cr-$CR_INSTANCE " + fi + fi + done +} +# Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied +# args: - +__CR_test_requirements() { + : +} -## Access to Callback Receiver -# Host name may be changed if app started by kube -# Direct access from script -CR_HTTPX="http" -CR_HOST_NAME=$LOCALHOST_NAME -CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT -#Docker/Kube internal path -if [ $RUNMODE == "KUBE" ]; then - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK -else - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK -fi -# CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently) -CR_ADAPTER_TYPE="REST" -CR_ADAPTER=$CR_PATH +####################################################### ################ ### CR functions ################ -# Set http as the protocol to use for all communication to the Callback Receiver +#Var to hold the current number of CR instances +CR_APP_COUNT=1 +MAX_CR_APP_COUNT=10 + +# Set http as the protocol to use for all communication to the Dmaap adapter # args: - # (Function for test scripts) use_cr_http() { - echo -e $BOLD"CR protocol setting"$EBOLD - echo -e " Using $BOLD http $EBOLD towards CR" - - CR_HTTPX="http" - CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT - - #Docker/Kube internal path - if [ $RUNMODE == "KUBE" ]; then - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK - else - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK - fi - CR_ADAPTER_TYPE="REST" - CR_ADAPTER=$CR_PATH - echo "" + __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT } -# Set https as the protocol to use for all communication to the Callback Receiver +# Set https as the protocol to use for all communication to the Dmaap adapter # args: - # (Function for test scripts) use_cr_https() { - echo -e $BOLD"CR protocol setting"$EBOLD - echo -e " Using $BOLD https $EBOLD towards CR" + __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT +} - CR_HTTPX="https" - CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT +# Setup paths to svc/container for internal and external access +# args: +__cr_set_protocoll() { + + echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD + echo -e " Using $BOLD $1 $EBOLD towards $CR_DISPLAY_NAME" + ## Access to Dmaap adapter + for ((CR_INSTANCE=0; CR_INSTANCE<$MAX_CR_APP_COUNT; CR_INSTANCE++ )); do + CR_DOCKER_INSTANCE=$(($CR_INSTANCE+1)) + # CR_SERVICE_PATH is the base path to cr + if [ $DOCKER_COMPOSE_VERSION == "V1" ]; then + __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"_cr_"${CR_DOCKER_INSTANCE}":"$2 # docker access, container->container and script->container via proxy + else + __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-cr-"${CR_DOCKER_INSTANCE}":"$2 # docker access, container->container and script->container via proxy + fi + if [ $RUNMODE == "KUBE" ]; then + __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-"$CR_INSTANCE.$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy + fi + export CR_SERVICE_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH + # Service paths are used in test script to provide callbacck urls to app + export CR_SERVICE_MR_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_MR #Only for messages from dmaap adapter/mediator + export CR_SERVICE_TEXT_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT #Callbacks for text payload + export CR_SERVICE_APP_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK #For general callbacks from apps + + if [ $CR_INSTANCE -eq 0 ]; then + # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently) + # CR_ADDAPTER need to be set before each call to CR....only set for instance 0 here + CR_ADAPTER_TYPE="REST" + CR_ADAPTER=$__CR_SERVICE_PATH + fi + done + echo "" +} - if [ $RUNMODE == "KUBE" ]; then - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK - else - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_SECURE_PORT$CR_APP_CALLBACK - fi +# Export env vars for config files, docker compose and kube resources +# args: +__cr_export_vars() { + export CR_APP_NAME + export CR_DISPLAY_NAME - CR_ADAPTER_TYPE="REST" - CR_ADAPTER=$CR_PATH - echo "" + export KUBE_SIM_NAMESPACE + export DOCKER_SIM_NWNAME + + export CR_IMAGE + + export CR_INTERNAL_PORT + export CR_INTERNAL_SECURE_PORT + export CR_EXTERNAL_PORT + export CR_EXTERNAL_SECURE_PORT + + export CR_APP_COUNT } # Start the Callback reciver in the simulator group -# args: - +# args: # (Function for test scripts) start_cr() { echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD + if [ $# -ne 1 ]; then + echo -e $RED" Number of CR instances missing, usage: start_cr "$ERED + exit 1 + fi + if [ $1 -lt 1 ] || [ $1 -gt 10 ]; then + echo -e $RED" Number of CR shall be 1...10, usage: start_cr "$ERED + exit 1 + fi + export CR_APP_COUNT=$1 + if [ $RUNMODE == "KUBE" ]; then # Check if app shall be fully managed by the test script @@ -198,13 +261,8 @@ start_cr() { if [ $retcode_i -eq 0 ]; then echo -e " Creating $CR_APP_NAME deployment and service" - export CR_APP_NAME - export KUBE_SIM_NAMESPACE - export CR_IMAGE - export CR_INTERNAL_PORT - export CR_INTERNAL_SECURE_PORT - export CR_EXTERNAL_PORT - export CR_EXTERNAL_SECURE_PORT + + __cr_export_vars __kube_create_namespace $KUBE_SIM_NAMESPACE @@ -220,33 +278,13 @@ start_cr() { fi - echo " Retrieving host and ports for service..." - CR_HOST_NAME=$(__kube_get_service_host $CR_APP_NAME $KUBE_SIM_NAMESPACE) - - CR_EXTERNAL_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "http") - CR_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "https") - - echo " Host IP, http port, https port: $CR_HOST_NAME $CR_EXTERNAL_PORT $CR_EXTERNAL_SECURE_PORT" - if [ $CR_HTTPX == "http" ]; then - CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK - else - CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT - CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK - fi - if [ $CR_ADAPTER_TYPE == "REST" ]; then - CR_ADAPTER=$CR_PATH - fi - - __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL + for ((CR_INSTANCE=0; CR_INSTANCE<$CR_APP_COUNT; CR_INSTANCE++ )); do + __dynvar="CR_SERVICE_PATH_"$CR_INSTANCE + __cr_app_name=$CR_APP_NAME"-"$CR_INSTANCE + __check_service_start $__cr_app_name ${!__dynvar}$CR_ALIVE_URL + result=$(__do_curl ${!__dynvar}/reset) + done - echo -ne " Service $CR_APP_NAME - reset "$SAMELINE - result=$(__do_curl $CR_APP_NAME $CR_PATH/reset) - if [ $? -ne 0 ]; then - echo -e " Service $CR_APP_NAME - reset $RED Failed $ERED - will continue" - else - echo -e " Service $CR_APP_NAME - reset $GREEN OK $EGREEN" - fi else # Check if docker app shall be fully managed by the test script __check_included_image 'CR' @@ -256,54 +294,190 @@ start_cr() { exit fi - export CR_APP_NAME - export CR_INTERNAL_PORT - export CR_EXTERNAL_PORT - export CR_INTERNAL_SECURE_PORT - export CR_EXTERNAL_SECURE_PORT - export DOCKER_SIM_NWNAME - export CR_DISPLAY_NAME - - __start_container $CR_COMPOSE_DIR "" NODOCKERARGS 1 $CR_APP_NAME - - __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL + __cr_export_vars + + app_data="" + cntr=1 + while [ $cntr -le $CR_APP_COUNT ]; do + if [ $DOCKER_COMPOSE_VERSION == "V1" ]; then + app=$CR_APP_NAME"_cr_"$cntr + else + app=$CR_APP_NAME"-cr-"$cntr + fi + app_data="$app_data $app" + let cntr=cntr+1 + done + + echo "COMPOSE_PROJECT_NAME="$CR_APP_NAME > $SIM_GROUP/$CR_COMPOSE_DIR/.env + + __start_container $CR_COMPOSE_DIR "" NODOCKERARGS $CR_APP_COUNT $app_data + + cntr=1 #Counter for docker instance, starts on 1 + cntr2=0 #Couter for env var name, starts with 0 to be compablible with kube + while [ $cntr -le $CR_APP_COUNT ]; do + if [ $DOCKER_COMPOSE_VERSION == "V1" ]; then + app=$CR_APP_NAME"_cr_"$cntr + else + app=$CR_APP_NAME"-cr-"$cntr + fi + __dynvar="CR_SERVICE_PATH_"$cntr2 + __check_service_start $app ${!__dynvar}$CR_ALIVE_URL + let cntr=cntr+1 + let cntr2=cntr2+1 + done fi echo "" } +#Convert a cr path id to the value of the environment var holding the url +# arg: +# returns: +__cr_get_service_path(){ + if [ $# -ne 1 ]; then + echo "DUMMY" + return 1 + fi + if [ $1 -lt 0 ] || [ $1 -ge $MAX_CR_APP_COUNT ]; then + echo "DUMMY" + return 1 + fi + __dynvar="CR_SERVICE_PATH_"$1 + echo ${!__dynvar} + return 0 +} # Tests if a variable value in the CR is equal to a target value and and optional timeout. # Arg: - This test set pass or fail depending on if the variable is # equal to the target or not. -# Arg: - This test waits up to the timeout seconds +# Arg: - 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" "$CR_PATH/counter/" $1 "=" $2 $3 + if [ $# -eq 3 ] || [ $# -eq 4 ]; then + CR_SERVICE_PATH=$(__cr_get_service_path $1) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "=" $3 $4 + else + __print_err "Wrong args to cr_equal, needs three or four args: [ timeout ]" $@ + fi +} + +# Tests if a variable value in the CR is equal to or greater than the target value and and optional timeout. +# Arg: - This test set pass or fail depending on if the variable is +# equal to the target or not. +# Arg: - This test waits up to the timeout seconds +# before setting pass or fail depending on if the variable value becomes equal to or greater than the target +# value or not. +# (Function for test scripts) +cr_greater_or_equal() { + if [ $# -eq 3 ] || [ $# -eq 4 ]; then + CR_SERVICE_PATH=$(__cr_get_service_path $1) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 ">=" $3 $4 + else + __print_err "Wrong args to cr_equal, needs three or four args: [ timeout ]" $@ + fi +} + +# Tests if a variable value in the CR contains the target string and and optional timeout +# Arg: - This test set pass or fail depending on if the variable contains +# the target or not. +# Arg: - This test waits up to the timeout seconds +# before setting pass or fail depending on if the variable value contains the target +# value or not. +# (Function for test scripts) +cr_contains_str() { + + if [ $# -eq 3 ] || [ $# -eq 4 ]; then + CR_SERVICE_PATH=$(__cr_get_service_path $1) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "contain_str" $3 $4 + return 0 else - __print_err "Wrong args to cr_equal, needs two or three args: [ timeout ]" $@ + __print_err "needs two or three args: [ timeout ]" + return 1 + fi +} + +# Read a variable value from CR sim and send to stdout. Arg: +cr_read() { + CR_SERVICE_PATH=$(__cr_get_service_path $1) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + echo "$(__do_curl $CR_SERVICE_PATH/counter/$2)" +} + +# Function to configure write delay on callbacks +# Delay given in seconds. +# arg +# (Function for test scripts) +cr_delay_callback() { + __log_conf_start $@ + + if [ $# -ne 3 ]; then + __print_err " ]" $@ + return 1 + fi + + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + + res="$(__do_curl_to_api CR POST /forcedelay?delay=$3)" + status=${res:${#res}-3} + + if [ $status -ne 200 ]; then + __log_conf_fail_status_code $1 $status + return 1 fi + + __log_conf_ok + return 0 } -# CR API: Check the contents of all current ric sync events for one id from PMS -# [ EMPTY | ( )+ ] +# CR API: Check the contents of all current ric sync events for one id from A1PMS +# [ EMPTY | ( )+ ] # (Function for test scripts) cr_api_check_all_sync_events() { __log_test_start $@ - if [ "$PMS_VERSION" != "V2" ]; then + if [ "$A1PMS_VERSION" != "V2" ]; then __log_test_fail_not_supported return 1 fi - if [ $# -lt 2 ]; then - __print_err " [ EMPTY | ( )+ ]" $@ + if [ $# -lt 3 ]; then + __print_err " [ EMPTY | ( )+ ]" $@ return 1 fi - query="/get-all-events/"$2 + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + + query="/get-all-events/"$3 res="$(__do_curl_to_api CR GET $query)" status=${res:${#res}-3} @@ -312,15 +486,15 @@ cr_api_check_all_sync_events() { return 1 fi - if [ $# -gt 2 ]; then + if [ $# -gt 3 ]; then body=${res:0:${#res}-3} - if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then + if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then targetJson="[" else targetJson="[" - arr=(${@:3}) + arr=(${@:4}) - for ((i=0; i<$(($#-2)); i=i+1)); do + for ((i=0; i<$(($#-3)); i=i+1)); do if [ "$targetJson" != "[" ]; then targetJson=$targetJson"," @@ -342,18 +516,25 @@ cr_api_check_all_sync_events() { return 0 } -# CR API: Check the contents of all current status events for one id from ECS -# [ EMPTY | ( )+ ] +# CR API: Check the contents of all current status events for one id from ICS +# [ EMPTY | ( )+ ] # (Function for test scripts) -cr_api_check_all_ecs_events() { +cr_api_check_all_ics_events() { __log_test_start $@ - if [ $# -lt 2 ]; then - __print_err " [ EMPTY | ( )+ ]" $@ + if [ $# -lt 3 ]; then + __print_err " [ EMPTY | ( )+ ]" $@ return 1 fi - query="/get-all-events/"$2 + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + + query="/get-all-events/"$3 res="$(__do_curl_to_api CR GET $query)" status=${res:${#res}-3} @@ -362,15 +543,15 @@ cr_api_check_all_ecs_events() { return 1 fi - if [ $# -gt 2 ]; then + if [ $# -gt 3 ]; then body=${res:0:${#res}-3} - if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then + if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then targetJson="[" else targetJson="[" - arr=(${@:3}) + arr=(${@:4}) - for ((i=0; i<$(($#-2)); i=i+1)); do + for ((i=0; i<$(($#-3)); i=i+1)); do if [ "$targetJson" != "[" ]; then targetJson=$targetJson"," @@ -392,30 +573,37 @@ cr_api_check_all_ecs_events() { return 0 } -# CR API: Check the contents of all current type subscription events for one id from ECS -# [ EMPTY | ( )+ ] +# CR API: Check the contents of all current type subscription events for one id from ICS +# [ EMPTY | ( )+ ] # (Function for test scripts) -cr_api_check_all_ecs_subscription_events() { +cr_api_check_all_ics_subscription_events() { __log_test_start $@ - #Valid number of parameter 2,3,7,11 + #Valid number of parameter 3,4,8,12 paramError=1 - if [ $# -eq 2 ]; then + if [ $# -eq 3 ]; then paramError=0 fi - if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then + if [ $# -eq 4 ] && [ "$4" == "EMPTY" ]; then paramError=0 fi - variablecount=$(($#-2)) - if [ $# -gt 3 ] && [ $(($variablecount%3)) -eq 0 ]; then + variablecount=$(($#-3)) + if [ $# -gt 4 ] && [ $(($variablecount%3)) -eq 0 ]; then paramError=0 fi if [ $paramError -eq 1 ]; then - __print_err " [ EMPTY | ( )+ ]" $@ + __print_err " [ EMPTY | ( )+ ]" $@ + return 1 + fi + + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ return 1 fi - query="/get-all-events/"$2 + query="/get-all-events/"$3 res="$(__do_curl_to_api CR GET $query)" status=${res:${#res}-3} @@ -424,12 +612,12 @@ cr_api_check_all_ecs_subscription_events() { return 1 fi - if [ $# -gt 2 ]; then + if [ $# -gt 3 ]; then body=${res:0:${#res}-3} targetJson="[" - if [ $# -gt 3 ]; then - arr=(${@:3}) - for ((i=0; i<$(($#-3)); i=i+3)); do + if [ $# -gt 4 ]; then + arr=(${@:4}) + for ((i=0; i<$(($#-4)); i=i+3)); do if [ "$targetJson" != "[" ]; then targetJson=$targetJson"," fi @@ -459,11 +647,23 @@ cr_api_check_all_ecs_subscription_events() { # CR API: Reset all events and counters -# Arg: - +# Arg: # (Function for test scripts) cr_api_reset() { __log_conf_start $@ + if [ $# -ne 1 ]; then + __print_err "" $@ + return 1 + fi + + CR_SERVICE_PATH=$(__cr_get_service_path $1) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + res="$(__do_curl_to_api CR GET /reset)" status=${res:${#res}-3} @@ -474,4 +674,240 @@ cr_api_reset() { __log_conf_ok return 0 +} + + +# CR API: Check the contents of all json events for path +# (EMPTY | + ) +# (Function for test scripts) +cr_api_check_all_genric_json_events() { + __log_test_start $@ + + if [ $# -lt 4 ]; then + __print_err " (EMPTY | + )" $@ + return 1 + fi + + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + + query="/get-all-events/"$3 + res="$(__do_curl_to_api CR GET $query)" + status=${res:${#res}-3} + + if [ $status -ne $1 ]; then + __log_test_fail_status_code $1 $status + return 1 + fi + body=${res:0:${#res}-3} + targetJson="[" + + if [ $4 != "EMPTY" ]; then + shift + shift + shift + while [ $# -gt 0 ]; do + if [ "$targetJson" != "[" ]; then + targetJson=$targetJson"," + fi + targetJson=$targetJson$1 + shift + done + fi + targetJson=$targetJson"]" + + echo " TARGET JSON: $targetJson" >> $HTTPLOG + res=$(python3 ../common/compare_json.py "$targetJson" "$body") + + if [ $res -ne 0 ]; then + __log_test_fail_body + return 1 + fi + + __log_test_pass + return 0 +} + + +# CR API: Check a single (oldest) json event (or none if empty) for path +# (EMPTY | ) +# (Function for test scripts) +cr_api_check_single_genric_json_event() { + __log_test_start $@ + + if [ $# -ne 4 ]; then + __print_err " (EMPTY | )" $@ + return 1 + fi + + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + + query="/get-event/"$3 + res="$(__do_curl_to_api CR GET $query)" + status=${res:${#res}-3} + + if [ $status -ne $1 ]; then + __log_test_fail_status_code $1 $status + return 1 + fi + body=${res:0:${#res}-3} + targetJson=$4 + + if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then + __log_test_fail_body + return 1 + fi + echo " TARGET JSON: $targetJson" >> $HTTPLOG + res=$(python3 ../common/compare_json.py "$targetJson" "$body") + + if [ $res -ne 0 ]; then + __log_test_fail_body + return 1 + fi + + __log_test_pass + return 0 +} + +# CR API: Check a single (oldest) json in md5 format (or none if empty) for path. +# Note that if a json message is given, it shall be compact, no ws except inside string. +# The MD5 will generate different hash if ws is present or not in otherwise equivalent json +# arg: (EMPTY | ) +# (Function for test scripts) +cr_api_check_single_genric_event_md5() { + __log_test_start $@ + + if [ $# -ne 4 ]; then + __print_err " (EMPTY | )" $@ + return 1 + fi + + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + + query="/get-event/"$3 + res="$(__do_curl_to_api CR GET $query)" + status=${res:${#res}-3} + + if [ $status -ne $1 ]; then + __log_test_fail_status_code $1 $status + return 1 + fi + body=${res:0:${#res}-3} + if [ $4 == "EMPTY" ]; then + if [ ${#body} -ne 0 ]; then + __log_test_fail_body + return 1 + else + __log_test_pass + return 0 + fi + fi + command -v md5 > /dev/null # Mac + if [ $? -eq 0 ]; then + targetMd5=$(echo -n "$4" | md5) + else + command -v md5sum > /dev/null # Linux + if [ $? -eq 0 ]; then + targetMd5=$(echo -n "$4" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd + else + __log_test_fail_general "Command md5 nor md5sum is available" + return 1 + fi + fi + targetMd5="\""$targetMd5"\"" #Quotes needed + + echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG + + if [ "$body" != "$targetMd5" ]; then + __log_test_fail_body + return 1 + fi + + __log_test_pass + return 0 +} + +# CR API: Check a single (oldest) event in md5 format (or none if empty) for path. +# Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines. +# The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json +# arg: (EMPTY | ) +# (Function for test scripts) +cr_api_check_single_genric_event_md5_file() { + __log_test_start $@ + + if [ $# -ne 4 ]; then + __print_err " (EMPTY | )" $@ + return 1 + fi + + CR_SERVICE_PATH=$(__cr_get_service_path $2) + CR_ADAPTER=$CR_SERVICE_PATH + if [ $? -ne 0 ]; then + __print_err " missing or incorrect" $@ + return 1 + fi + + query="/get-event/"$3 + res="$(__do_curl_to_api CR GET $query)" + status=${res:${#res}-3} + + if [ $status -ne $1 ]; then + __log_test_fail_status_code $1 $status + return 1 + fi + body=${res:0:${#res}-3} + if [ $4 == "EMPTY" ]; then + if [ ${#body} -ne 0 ]; then + __log_test_fail_body + return 1 + else + __log_test_pass + return 0 + fi + fi + + if [ ! -f $4 ]; then + __log_test_fail_general "File $3 does not exist" + return 1 + fi + + filedata=$(cat $4) + + command -v md5 > /dev/null # Mac + if [ $? -eq 0 ]; then + targetMd5=$(echo -n "$filedata" | md5) + else + command -v md5sum > /dev/null # Linux + if [ $? -eq 0 ]; then + targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd + else + __log_test_fail_general "Command md5 nor md5sum is available" + return 1 + fi + fi + targetMd5="\""$targetMd5"\"" #Quotes needed + + echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG + + if [ "$body" != "$targetMd5" ]; then + __log_test_fail_body + return 1 + fi + + __log_test_pass + return 0 } \ No newline at end of file