3 # ============LICENSE_START===============================================
4 # Copyright (C) 2020 Nordix Foundation. All rights reserved.
5 # ========================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=================================================
20 # This is a script that contains all the functions needed for auto test
21 # Arg: local|remote|remote-remove [auto-clean]
24 #Formatting for 'echo' cmd
29 GREEN="\033[32m\033[1m"
31 YELLOW="\033[33m\033[1m"
36 if [ $? -ne 0 ] || [ -z tmp ]; then
37 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
41 if [ $? -ne 0 ] || [ -z tmp ]; then
42 echo -e $RED"docker is required to run the test environment, pls install"$ERED
46 # Just resetting any previous echo formatting...
47 echo -ne $EBOLD$ERED$EGREEN
49 # source test environment variables
50 . ../common/test_env.sh
52 echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
54 #Vars for A1 interface version and container count
62 # Var to switch between http and https. Extra curl flag needed for https
63 export RIC_SIM_HTTPX="http"
64 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
65 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
66 export RIC_SIM_CERT_MOUNT_DIR="./fakedir" #Fake dir so that the sim container does not find any cert
69 LOCALHOST="http://localhost:"
71 # Make curl retries for http response codes set in this env var, space separated list of codes
74 # Var to hold 'auto' in case containers shall be stopped when test case ends
77 # Set a description string for the test case
78 if [ -z "$TC_ONELINE_DESCR" ]; then
79 TC_ONELINE_DESCR="<no-description>"
80 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
83 # Counter for test suites
84 if [ -f .tmp_tcsuite_ctr ]; then
85 tmpval=$(< .tmp_tcsuite_ctr)
87 echo $tmpval > .tmp_tcsuite_ctr
90 # Create a test case id, ATC (Auto Test Case), from the name of the test case script.
91 # FTC1.sh -> ATC == FTC1
92 ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
94 # Create the logs dir if not already created in the current dir
95 if [ ! -d "logs" ]; then
100 # Create a http message log for this testcase
101 HTTPLOG=$PWD"/.httplog_"$ATC".txt"
104 # Create a log dir for the test case
105 mkdir -p $TESTLOGS/$ATC
107 # Clear the log dir for the test case
108 rm $TESTLOGS/$ATC/*.log &> /dev/null
109 rm $TESTLOGS/$ATC/*.txt &> /dev/null
110 rm $TESTLOGS/$ATC/*.json &> /dev/null
112 # Log all output from the test case to a TC log
113 TCLOG=$TESTLOGS/$ATC/TC.log
114 exec &> >(tee ${TCLOG})
116 #Variables for counting tests as well as passed and failed tests
122 #Var for measuring execution time
123 TCTEST_START=$SECONDS
125 echo "-------------------------------------------------------------------------------------------------"
126 echo "----------------------------------- Test case: "$ATC
127 echo "----------------------------------- Started: "$(date)
128 echo "-------------------------------------------------------------------------------------------------"
129 echo "-- Description: "$TC_ONELINE_DESCR
130 echo "-------------------------------------------------------------------------------------------------"
131 echo "----------------------------------- Test case setup -----------------------------------"
133 echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
135 #Temp var to check for image variable name errors
137 #Create a file with image info for later printing as a table
138 image_list_file=".image-list"
139 echo -e " Container\tImage\ttag" > $image_list_file
141 # Check if image env var is set and if so export the env var with image to use (used by docker compose files)
142 # arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>
143 __check_image_var() {
144 if [ $# -ne 5 ]; then
145 echo "Expected arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>"
150 #Create var from the input var names
154 if [ -z $image ]; then
155 echo -e $RED"\$"$4" not set in test_env"$ERED
158 tmp=$tmp"<no-image>\t"
163 echo -e $RED"\$"$5" not set in test_env"$ERED
170 echo -e "$tmp" >> $image_list_file
172 export "${3}"=$image":"$tag
174 #echo " Configured image for ${1} (script start arg=${2}): "$image":"$tag
177 # Check that image env setting are available
179 if [ $# -lt 1 ] || [ $# -gt 2 ]; then
180 echo "Expected arg: local|remote|remote-remove [auto-clean]"
182 elif [ $1 == "local" ]; then
185 __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
187 #Local Control Panel image
188 __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
191 __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
194 __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
196 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
199 __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_REMOTE_IMAGE" "POLICY_AGENT_REMOTE_IMAGE_TAG"
201 #Remote Control Panel image
202 __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE_TAG"
205 __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE_TAG"
207 #Remote ric sim image
208 __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_REMOTE_IMAGE" "RIC_SIM_REMOTE_IMAGE_TAG"
211 echo "Expected arg: local|remote|remote-remove [auto-clean]"
215 if [ $# -eq 2 ]; then
216 if [ $2 == "auto-clean" ]; then
217 echo "Stting automatic cleaning of container when test case ends"
220 echo "Expected arg: local|remote|remote-remove [auto-clean]"
225 # These images are not built as part of this project official images, just check that env vars are set correctly
226 __check_image_var " Message Router" $1 "MRSTUB_IMAGE" "MRSTUB_LOCAL_IMAGE" "MRSTUB_LOCAL_IMAGE_TAG"
227 __check_image_var " Callback Receiver" $1 "CR_IMAGE" "CR_LOCAL_IMAGE" "CR_LOCAL_IMAGE_TAG"
228 __check_image_var " Consul" $1 "CONSUL_IMAGE" "CONSUL_REMOTE_IMAGE" "CONSUL_REMOTE_IMAGE_TAG"
229 __check_image_var " CBS" $1 "CBS_IMAGE" "CBS_REMOTE_IMAGE" "CBS_REMOTE_IMAGE_TAG"
230 __check_image_var " SDNC DB" $1 "SDNC_DB_IMAGE" "SDNC_DB_REMOTE_IMAGE" "SDNC_DB_REMOTE_IMAGE_TAG"
231 __check_image_var " SDNC ONAP A1 Adapter" $1 "SDNC_ONAP_A1_ADAPTER_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE_TAG"
232 __check_image_var " SDNC ONAP DB" $1 "SDNC_ONAP_DB_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE_TAG"
234 #Errors in image setting - exit
235 if [ $IMAGE_ERR -ne 0 ]; then
239 #Print a tables of the image settings
240 echo -e $BOLD"Images configured for start arg: "$1 $EBOLD
241 column -t -s $'\t' $image_list_file
246 #Set the SIM_GROUP var
247 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
248 if [ -z "$SIM_GROUP" ]; then
249 SIM_GROUP=$PWD/../simulator-group
250 if [ ! -d $SIM_GROUP ]; then
251 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
252 echo -e $RED"Please set the SIM_GROUP manually in the test_env.sh"$ERED
255 echo " SIM_GROUP auto set to: " $SIM_GROUP
257 elif [ $SIM_GROUP = *simulator_group ]; then
258 echo -e $RED"Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the repo, check common/test_env.sh"$ERED
261 echo " SIM_GROUP env var already set to: " $SIM_GROUP
266 #Temp var to check for image pull errors
269 #Function to check if image exist and stop+remove the container+pull new images as needed
270 #args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag>
271 __check_and_pull_image() {
273 echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$4$EBOLD"
274 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
275 tmp_im=$(docker images --format $format_string ${4})
277 if [ $1 == "local" ]; then
278 if [ -z "$tmp_im" ]; then
279 echo -e " "$2" (local image): \033[1m"$4"\033[0m $RED does not exist in local registry, need to be built (or manually pulled)"$ERED
283 echo -e " "$2" (local image): \033[1m"$4"\033[0m "$GREEN"OK"$EGREEN
285 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
286 if [ $1 == "remote-remove" ]; then
287 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
288 tmp="$(docker ps -aq --filter name=${3})"
289 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
290 docker stop $tmp &> .dockererr
291 if [ $? -ne 0 ]; then
294 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
299 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
300 tmp="$(docker ps -aq --filter name=${3})" &> /dev/null
301 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
302 docker rm $tmp &> .dockererr
303 if [ $? -ne 0 ]; then
306 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
311 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
312 echo -ne " Removing image - ${SAMELINE}"
313 tmp="$(docker images -q ${4})" &> /dev/null
314 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
315 docker rmi $4 &> .dockererr
316 if [ $? -ne 0 ]; then
319 echo -e $RED" Image could not be removed - try manual removal of the image"$ERED
323 echo -e " Removing image - "$GREEN"removed"$EGREEN
325 echo -e " Removing image - "$GREEN"image not in repository"$EGREEN
329 if [ -z "$tmp_im" ]; then
330 echo -ne " Pulling image${SAMELINE}"
331 docker pull $4 &> .dockererr
332 tmp_im=$(docker images ${4} | grep -v REPOSITORY)
333 if [ -z "$tmp_im" ]; then
335 echo -e " Pulling image -$RED could not be pulled"$ERED
340 echo -e " Pulling image -$GREEN Pulled $EGREEN"
342 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
349 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
351 app="Policy Agent"; __check_and_pull_image $1 "$app" $POLICY_AGENT_APP_NAME $POLICY_AGENT_IMAGE
352 app="Non-RT RIC Control Panel"; __check_and_pull_image $1 "$app" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_IMAGE
353 app="SDNC A1 Controller"; __check_and_pull_image $1 "$app" $SDNC_APP_NAME $SDNC_A1_CONTROLLER_IMAGE
354 app="Near-RT RIC Simulator"; __check_and_pull_image $1 "$app" $RIC_SIM_PREFIX"_"$RIC_SIM_BASE $RIC_SIM_IMAGE
356 app="Consul"; __check_and_pull_image $1 "$app" $CONSUL_APP_NAME $CONSUL_IMAGE
357 app="CBS"; __check_and_pull_image $1 "$app" $CBS_APP_NAME $CBS_IMAGE
358 app="SDNC DB"; __check_and_pull_image $1 "$app" $SDNC_APP_NAME $SDNC_DB_IMAGE
359 app="SDNC ONAP A1 Adapter"; __check_and_pull_image $1 "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_A1_ADAPTER_IMAGE
360 app="SDNC ONAP DB"; __check_and_pull_image $1 "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_DB_IMAGE
362 # MR stub image not checked, will be built by this script - only local image
363 # CR stub image not checked, will be built by this script - only local image
366 #Errors in image setting - exit
367 if [ $IMAGE_ERR -ne 0 ]; then
369 echo "#################################################################################################"
370 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
371 echo "#################################################################################################"
378 echo -e $BOLD"Building images needed for test"$EBOLD
383 echo " Building mrstub image: mrstub:latest"
384 docker build -t mrstub . &> .dockererr
385 if [ $? -eq 0 ]; then
386 echo -e $GREEN" Build Ok"$EGREEN
388 echo -e $RED" Build Failed"$ERED
395 echo " Building Callback Receiver image: callback-receiver:latest"
396 docker build -t callback-receiver . &> .dockererr
397 if [ $? -eq 0 ]; then
398 echo -e $GREEN" Build Ok"$EGREEN
400 echo -e $RED" Build Failed"$ERED
408 # Create a table of the images used in the script
409 echo -e $BOLD"Local docker registry images used in the this test script"$EBOLD
411 docker_tmp_file=.docker-images-table
412 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}"
413 echo -e " Application\tRepository\tTag\tCreated Since\tSize" > $docker_tmp_file
414 echo -e " Policy Agent\t$(docker images --format $format_string $POLICY_AGENT_IMAGE)" >> $docker_tmp_file
415 echo -e " Control Panel\t$(docker images --format $format_string $CONTROL_PANEL_IMAGE)" >> $docker_tmp_file
416 echo -e " SDNC A1 Controller\t$(docker images --format $format_string $SDNC_A1_CONTROLLER_IMAGE)" >> $docker_tmp_file
417 echo -e " RIC Simulator\t$(docker images --format $format_string $RIC_SIM_IMAGE)" >> $docker_tmp_file
418 echo -e " Message Router\t$(docker images --format $format_string $MRSTUB_IMAGE)" >> $docker_tmp_file
419 echo -e " Callback Receiver\t$(docker images --format $format_string $CR_IMAGE)" >> $docker_tmp_file
420 echo -e " Consul\t$(docker images --format $format_string $CONSUL_IMAGE)" >> $docker_tmp_file
421 echo -e " CBS\t$(docker images --format $format_string $CBS_IMAGE)" >> $docker_tmp_file
422 echo -e " SDNC DB\t$(docker images --format $format_string $SDNC_DB_IMAGE)" >> $docker_tmp_file
423 echo -e " SDNC ONAP A1 Adapter\t$(docker images --format $format_string $SDNC_ONAP_A1_ADAPTER_IMAGE)" >> $docker_tmp_file
424 echo -e " SDNC ONAP DB\t$(docker images --format $format_string $SDNC_ONAP_DB_IMAGE)" >> $docker_tmp_file
426 column -t -s $'\t' $docker_tmp_file
430 echo -e $BOLD"======================================================="$EBOLD
431 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
432 echo -e $BOLD"======================================================="$EBOLD
435 # Function to print the test result, shall be the last cmd in a test script
437 # (Function for test scripts)
441 duration=$((TCTEST_END-TCTEST_START))
443 echo "-------------------------------------------------------------------------------------------------"
444 echo "------------------------------------- Test case: "$ATC
445 echo "------------------------------------- Ended: "$(date)
446 echo "-------------------------------------------------------------------------------------------------"
447 echo "-- Description: "$TC_ONELINE_DESCR
448 echo "-- Execution time: " $duration " seconds"
449 echo "-------------------------------------------------------------------------------------------------"
450 echo "------------------------------------- RESULTS"
454 total=$((RES_PASS+RES_FAIL))
455 if [ $RES_TEST -eq 0 ]; then
456 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
457 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
458 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
459 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
460 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
461 elif [ $total != $RES_TEST ]; then
462 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
463 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
464 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
465 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
466 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
467 elif [ $RES_CONF_FAIL -ne 0 ]; then
468 echo -e "\033[1mOne or more configure regest has failed. Check the script log....\033[0m"
469 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
470 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
471 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
472 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
473 elif [ $RES_PASS = $RES_TEST ]; then
474 echo -e "All tests \033[32m\033[1mPASS\033[0m"
475 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
476 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
477 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
478 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
481 # Update test suite counter
482 if [ -f .tmp_tcsuite_pass_ctr ]; then
483 tmpval=$(< .tmp_tcsuite_pass_ctr)
485 echo $tmpval > .tmp_tcsuite_pass_ctr
487 if [ -f .tmp_tcsuite_pass ]; then
488 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
491 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
492 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
493 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
494 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
495 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
497 # Update test suite counter
498 if [ -f .tmp_tcsuite_fail_ctr ]; then
499 tmpval=$(< .tmp_tcsuite_fail_ctr)
501 echo $tmpval > .tmp_tcsuite_fail_ctr
503 if [ -f .tmp_tcsuite_fail ]; then
504 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
508 echo "++++ Number of tests: "$RES_TEST
509 echo "++++ Number of passed tests: "$RES_PASS
510 echo "++++ Number of failed tests: "$RES_FAIL
512 echo "++++ Number of failed configs: "$RES_CONF_FAIL
513 echo "------------------------------------- Test case complete ---------------------------------"
514 echo "-------------------------------------------------------------------------------------------------"
518 #####################################################################
519 ###### Functions for start, configuring, stoping, cleaning etc ######
520 #####################################################################
523 # Stop and remove all containers
525 # (Function for test scripts)
528 echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
530 CONTAINTER_NAMES=("Policy Agent " $POLICY_AGENT_APP_NAME\
531 "Non-RT RIC Simulator(s)" $RIC_SIM_PREFIX\
532 "Message Router " $MR_APP_NAME\
533 "Callback Receiver " $CR_APP_NAME\
534 "Control Panel " $CONTROL_PANEL_APP_NAME\
535 "SDNC A1 Controller " $SDNC_APP_NAME\
536 "SDNC DB " $SDNC_DB_APP_NAME\
537 "SDNC ONAP A1 Adapter " $SDNC_ONAP_APP_NAME\
538 "SDNC DB " $SDNC_ONAP_DB_APP_NAME\
539 "CBS " $CBS_APP_NAME\
540 "Consul " $CONSUL_APP_NAME)
542 nw=0 # Calc max width of container name, to make a nice table
543 for (( i=1; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
544 if [ ${#CONTAINTER_NAMES[i]} -gt $nw ]; then
545 nw=${#CONTAINTER_NAMES[i]}
549 for (( i=0; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
550 APP="${CONTAINTER_NAMES[i]}"
551 CONTR="${CONTAINTER_NAMES[i+1]}"
552 for((w=${#CONTR}; w<$nw; w=w+1)); do
555 echo -ne " $APP: $CONTR - ${GREEN}stopping${EGREEN}${SAMELINE}"
556 docker stop $(docker ps -qa --filter name=${CONTR}) &> /dev/null
557 echo -ne " $APP: $CONTR - ${GREEN}stopped${EGREEN}${SAMELINE}"
558 docker rm $(docker ps -qa --filter name=${CONTR}) &> /dev/null
559 echo -e " $APP: $CONTR - ${GREEN}stopped removed${EGREEN}"
565 # Function stop and remove all container in the end of the test script, if the arg 'auto-clean' is given at test script start
567 # (Function for test scripts)
568 auto_clean_containers() {
570 if [ "$AUTO_CLEAN" == "auto" ]; then
571 echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
576 # Function to sleep a test case for a numner of seconds. Prints the optional text args as info
577 # args: <sleep-time-in-sec> [any-text-in-quoteds-to-printed]
578 # (Function for test scripts)
581 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
582 if [ $# -lt 1 ]; then
584 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
587 #echo "---- Sleep for " $1 " seconds ---- "$2
589 duration=$((SECONDS-start))
590 while [ $duration -lt $1 ]; do
591 echo -ne " Slept for ${duration} seconds${SAMELINE}"
593 duration=$((SECONDS-start))
595 echo -ne " Slept for ${duration} seconds${SAMELINE}"
599 # Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
600 # Not to be called from the test script itself.
602 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
603 if [ $# -gt 1 ]; then
604 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
609 # Helper function to get a the port of a specific ric simulatpor
611 # (Not for test scripts)
613 name=$1" " #Space appended to prevent matching 10 if 1 is desired....
614 cmdstr="docker ps --filter name=${name} --format \"{{.Names}} {{.Ports}}\" | grep '${name}' | sed s/0.0.0.0:// | cut -f 2 -d ' ' | cut -f 1 -d '-'"
616 if [[ "$res" =~ ^[0-9]+$ ]]; then
623 # Function to create the docker network for the test
624 # Not to be called from the test script itself.
625 __create_docker_network() {
626 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
627 if [ $? -ne 0 ]; then
628 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
631 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
632 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
633 docker network create $DOCKER_SIM_NWNAME
634 if [ $? -ne 0 ]; then
635 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
639 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
643 # Check if container is started by calling url on localhost using a port, expects response code 2XX
644 # args: <container-name> <port> <url> https|https
645 # Not to be called from the test script itself.
646 __check_container_start() {
648 if [ $# -ne 4 ]; then
650 elif [ $4 != "http" ] && [ $4 != "https" ]; then
653 if [ $paramError -ne 0 ]; then
655 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
658 echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
664 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
665 echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
672 if [ $app_started -eq 0 ]; then
675 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
678 if [ $localport -eq 0 ]; then
679 while [ $localport -eq 0 ]; do
680 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
681 localport=$(__find_sim_port $appname)
683 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
685 echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
690 echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
692 if [ $4 == "https" ]; then
693 result="$(__do_curl "-k https://localhost:"${localport}${url})"
695 result="$(__do_curl $LOCALHOST${localport}${url})"
697 if [ $? -eq 0 ]; then
698 if [ ${#result} -gt 15 ]; then
699 #If response is too long, truncate
700 result="...response text too long, omitted"
702 echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
703 echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN"
707 #echo " Retrying in $i seconds"
708 echo -ne " Waiting for container ${appname} service status...retrying in $i seconds${SAMELINE}"
713 if [ "$pa_st" = "false" ]; then
715 echo -e $RED" Container ${appname} did not respond to service status"$ERED
724 # Function to start a container and wait until it responds on the given port and url.
725 #args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
726 __start_container() {
728 variableArgCount=$(($#-2))
729 if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
731 __print_err "need 6 or more args, <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> http|https [<app-name> <port-number> <alive-url> http|https ]*" $@
735 __create_docker_network
741 if [ "$2" == "NODOCKERARGS" ]; then
742 docker-compose up -d &> .dockererr
743 if [ $? -ne 0 ]; then
744 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
748 docker-compose up -d $2 &> .dockererr
749 if [ $? -ne 0 ]; then
750 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
757 while [ $cntr -lt $variableArgCount ]; do
764 __check_container_start "$app" "$port" "$url" $httpx
776 # Function to load config from a file into consul for the Policy Agent
777 # arg: <json-config-file>
778 # (Function for test scripts)
779 consul_config_app() {
781 echo -e $BOLD"Configuring Consul"$EBOLD
783 if [ $# -ne 1 ]; then
785 __print_err "need one arg, <json-config-file>" $@
789 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
791 curl -s $LOCALHOST${CONSUL_EXTERNAL_PORT}/v1/kv/${POLICY_AGENT_APP_NAME}?dc=dc1 -X PUT -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'X-Requested-With: XMLHttpRequest' --data-binary "@"$1 >/dev/null
792 if [ $? -ne 0 ]; then
793 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
797 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
799 if [ $? -ne 0 ]; then
800 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
805 targetJson="{\"config\":"$targetJson"}"
806 echo "TARGET JSON: $targetJson" >> $HTTPLOG
807 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
808 if [ $res -ne 0 ]; then
809 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
813 echo -e $GREEN" Config loaded ok to consul"$EGREEN
821 # Function to perpare the consul configuration according to the current simulator configuration
822 # args: SDNC|SDNC_ONAP|NOSDNC <output-file>
823 # (Function for test scripts)
824 prepare_consul_config() {
825 echo -e $BOLD"Prepare Consul config"$EBOLD
827 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
831 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
835 if [ $1 == "SDNC" ]; then
836 echo -e " Config$BOLD including SDNC$EBOLD configuration"
837 elif [ $1 == "SDNC_ONAP" ]; then
838 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
839 elif [ $1 == "NOSDNC" ]; then
840 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
843 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
848 if [ $1 == "SDNC" ]; then
849 config_json=$config_json"\n \"controller\": ["
850 config_json=$config_json"\n {"
851 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
852 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_APP_NAME:$SDNC_INTERNAL_PORT\","
853 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
854 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
855 config_json=$config_json"\n }"
856 config_json=$config_json"\n ],"
858 if [ $1 == "SDNC_ONAP" ]; then
859 config_json=$config_json"\n \"controller\": ["
860 config_json=$config_json"\n {"
861 config_json=$config_json"\n \"name\": \"$SDNC_ONAP_APP_NAME\","
862 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
863 config_json=$config_json"\n \"userName\": \"$SDNC_ONAP_USER\","
864 config_json=$config_json"\n \"password\": \"$SDNC_ONAP_PWD\""
865 config_json=$config_json"\n }"
866 config_json=$config_json"\n ],"
870 config_json=$config_json"\n \"streams_publishes\": {"
871 config_json=$config_json"\n \"dmaap_publisher\": {"
872 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
873 config_json=$config_json"\n \"dmaap_info\": {"
874 config_json=$config_json"\n \"topic_url\": \"http://$MR_APP_NAME:$MR_INTERNAL_PORT/events/A1-POLICY-AGENT-WRITE\""
875 config_json=$config_json"\n }"
876 config_json=$config_json"\n }"
877 config_json=$config_json"\n },"
878 config_json=$config_json"\n \"streams_subscribes\": {"
879 config_json=$config_json"\n \"dmaap_subscriber\": {"
880 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
881 config_json=$config_json"\n \"dmaap_info\": {"
882 config_json=$config_json"\n \"topic_url\": \"http://$MR_APP_NAME:$MR_INTERNAL_PORT/events/A1-POLICY-AGENT-READ/users/policy-agent\""
883 config_json=$config_json"\n }"
884 config_json=$config_json"\n }"
885 config_json=$config_json"\n },"
887 config_json=$config_json"\n \"ric\": ["
889 rics=$(docker ps | grep ricsim | awk '{print $NF}')
891 if [ $? -ne 0 ] || [ -z "$rics" ]; then
892 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
899 if [ $cntr -gt 0 ]; then
900 config_json=$config_json"\n ,"
902 config_json=$config_json"\n {"
903 config_json=$config_json"\n \"name\": \"$ric\","
904 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
905 if [ $1 == "SDNC" ]; then
906 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
907 elif [ $1 == "SDNC_ONAP" ]; then
908 config_json=$config_json"\n \"controller\": \"$SDNC_ONAP_APP_NAME\","
910 config_json=$config_json"\n \"managedElementIds\": ["
911 config_json=$config_json"\n \"me1_$ric\","
912 config_json=$config_json"\n \"me2_$ric\""
913 config_json=$config_json"\n ]"
914 config_json=$config_json"\n }"
918 config_json=$config_json"\n ]"
919 config_json=$config_json"\n}"
922 printf "$config_json">$2
928 # Start Consul and CBS
930 # (Function for test scripts)
933 echo -e $BOLD"Starting Consul and CBS"$EBOLD
935 __start_container consul_cbs NODOCKERARGS "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
936 "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
939 ###########################
940 ### RIC Simulator functions
941 ###########################
943 use_simulator_http() {
944 echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
945 export RIC_SIM_HTTPX="http"
946 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
947 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
948 export RIC_SIM_CERT_MOUNT_DIR="./fakedir" #Fake dir so that the sim container does not find any cert
952 use_simulator_https() {
953 echo -e "Using secure $BOLD https $EBOLD towards the simulators"
954 export RIC_SIM_HTTPX="https"
955 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
956 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
957 export RIC_SIM_CERT_MOUNT_DIR="./cert"
961 # Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
962 # args: ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
963 # (Function for test scripts)
964 start_ric_simulators() {
966 echo -e $BOLD"Starting RIC Simulators"$EBOLD
970 __print_err "need three args, ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
973 echo " $2 simulators using basename: $1 on interface: $3"
974 #Set env var for simulator count and A1 interface vesion for the given group
975 if [ $1 == "ricsim_g1" ]; then
978 elif [ $1 == "ricsim_g2" ]; then
981 elif [ $1 == "ricsim_g3" ]; then
986 __print_err "need three args, gricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
990 # Create .env file to compose project, all ric container will get this prefix
991 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
997 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1000 while [ $cntr -le $2 ]; do
1003 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
1006 __start_container ric "$docker_args" $app_data
1010 ###########################
1011 ### Control Panel functions
1012 ###########################
1014 # Start the Control Panel container
1016 # (Function for test scripts)
1017 start_control_panel() {
1019 echo -e $BOLD"Starting Control Panel"$EBOLD
1021 __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
1029 # Start the SDNC A1 Controller
1031 # (Function for test scripts)
1034 echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1036 __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT "/apidoc/explorer" "http"
1040 #######################
1041 ### SDNC ONAP functions
1042 #######################
1044 # Start the SDNC ONAP A1 Adapter
1046 # (Function for test scripts)
1049 echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1051 __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT "/apidoc/explorer" "http"
1055 # Configure the SDNC ONAP A1 Adapter
1057 # (Function for test scripts)
1058 config_sdnc_onap() {
1060 echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1062 LOCALFILE=".sdnc_onap.prop"
1063 REMOTEFILE="/tmp/.sdnc_onap.prop"
1065 docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1066 if [ $? -ne 0 ]; then
1067 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1071 #Config of the prop file shall be inserted here
1073 #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1075 docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1076 if [ $? -ne 0 ]; then
1077 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1081 docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1082 if [ $? -ne 0 ]; then
1083 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1088 #####################
1089 ### MR stub functions
1090 #####################
1092 # Start the Message Router stub interface in the simulator group
1094 # (Function for test scripts)
1097 echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
1098 export MR_CERT_MOUNT_DIR="./cert"
1099 __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
1107 # Start the Callback reciver in the simulator group
1109 # (Function for test scripts)
1112 echo -e $BOLD"Starting Callback Receiver"$EBOLD
1114 __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
1118 ###########################
1119 ### Policy Agents functions
1120 ###########################
1122 # Start the policy agwent
1124 # (Function for test scripts)
1125 start_policy_agent() {
1127 echo -e $BOLD"Starting Policy Agent"$EBOLD
1129 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1133 # All calls to the agent will be directed to the agent REST interface from now on
1135 # (Function for test scripts)
1136 use_agent_rest_http() {
1137 echo -e $BOLD"Using agent REST interface with http"$EBOLD
1138 export ADAPTER=$RESTBASE
1142 # All calls to the agent will be directed to the agent REST interface from now on
1144 # (Function for test scripts)
1145 use_agent_rest_https() {
1146 echo -e $BOLD"Using agent REST interface with https"$EBOLD
1147 export ADAPTER=$RESTBASE_SECURE
1151 # All calls to the agent will be directed to the agent dmaap interface from now on
1153 # (Function for test scripts)
1155 echo -e $BOLD"Using agent DMAAP interface"$EBOLD
1156 export ADAPTER=$DMAAPBASE
1161 # Turn on debug level tracing in the agent
1163 # (Function for test scripts)
1165 echo -e $BOLD"Setting agent debug"$EBOLD
1166 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1167 if [ $? -ne 0 ]; then
1168 __print_err "could not set debug mode" $@
1175 # Perform curl retries when making direct call to the agent for the specified http response codes
1176 # Speace separated list of http response codes
1177 # args: [<response-code>]*
1178 use_agent_retries() {
1179 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1180 AGENT_RETRY_CODES=$@
1188 # Check the agent logs for WARNINGs and ERRORs
1190 # (Function for test scripts)
1192 check_policy_agent_logs() {
1193 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
1196 check_control_panel_logs() {
1197 __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
1200 __check_container_logs() {
1204 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1206 #tmp=$(docker ps | grep $appname)
1207 tmp=$(docker ps -q --filter name=$appname) #get the container id
1208 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
1209 echo $dispname" is not running, no check made"
1212 foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1213 if [ $? -ne 0 ];then
1214 echo " Problem to search $appname log $logpath"
1216 if [ $foundentries -eq 0 ]; then
1217 echo " No WARN entries found in $appname log $logpath"
1219 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1222 foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1223 if [ $? -ne 0 ];then
1224 echo " Problem to search $appname log $logpath"
1226 if [ $foundentries -eq 0 ]; then
1227 echo " No ERR entries found in $appname log $logpath"
1229 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1235 # Store all container logs and other logs in the log dir for the script
1236 # Logs are stored with a prefix in case logs should be stored several times during a test
1237 # args: <logfile-prefix>
1238 # (Function for test scripts)
1240 if [ $# != 1 ]; then
1242 __print_err "need one arg, <file-prefix>" $@
1245 echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1
1247 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1248 docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1249 docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1250 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1251 docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1252 docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1253 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1255 docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1257 docker exec -it $SDNC_ONAP_APP_NAME cat $SDNC_ONAP_KARAF_LOG > $TESTLOGS/$ATC/$1_SDNC_ONAP_karaf.log 2>&1
1259 rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1260 for ric in $rics; do
1261 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1263 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1264 echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1271 # Generic curl function, assumed all 200-codes are ok
1273 # returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
1274 # returns: The return code is 0 for ok and 1 for not ok
1276 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1277 curlString="curl -skw %{http_code} $1"
1278 echo " CMD: $curlString" >> $HTTPLOG
1280 echo " RESP: $res" >> $HTTPLOG
1281 http_code="${res:${#res}-3}"
1282 if [ ${#res} -eq 3 ]; then
1283 echo "<no-response-from-server>"
1286 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
1287 echo "<not found, resp:${http_code}>"
1290 if [ $# -eq 2 ]; then
1291 echo "${res:0:${#res}-3}" | xargs
1293 echo "${res:0:${#res}-3}"
1300 #######################################
1301 ### Basic helper function for test cases
1302 #######################################
1304 # Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1305 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
1306 # immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1307 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
1308 # before setting pass or fail depending on the result of comparing variable and target using the operator.
1309 # 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.
1310 # Not to be called from test script.
1313 checkjsonarraycount=0
1315 if [ $# -eq 6 ]; then
1316 if [[ $3 == "json:"* ]]; then
1317 checkjsonarraycount=1
1320 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
1321 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"
1326 if [ $checkjsonarraycount -eq 0 ]; then
1327 result="$(__do_curl $2$3)"
1329 result=${result//[[:blank:]]/} #Strip blanks
1332 result="$(__do_curl $2$path)"
1334 echo "$result" > .tmp.curl.json
1335 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1337 duration=$((SECONDS-start))
1338 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
1340 if [ $retcode -ne 0 ]; then
1341 if [ $duration -gt $6 ]; then
1343 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1344 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1347 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1349 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1350 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1351 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
1353 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1355 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1356 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1357 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1359 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1361 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1362 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1363 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1365 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1367 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1368 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1369 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1372 if [ $duration -gt $6 ]; then
1374 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1375 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1381 elif [ $# -eq 5 ]; then
1382 if [[ $3 == "json:"* ]]; then
1383 checkjsonarraycount=1
1386 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
1387 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1389 if [ $checkjsonarraycount -eq 0 ]; then
1390 result="$(__do_curl $2$3)"
1392 result=${result//[[:blank:]]/} #Strip blanks
1395 result="$(__do_curl $2$path)"
1397 echo "$result" > .tmp.curl.json
1398 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1400 if [ $retcode -ne 0 ]; then
1402 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1403 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
1404 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1406 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1407 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met"
1408 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1410 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1411 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1412 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1414 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1415 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1416 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1418 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1419 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1422 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
1423 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1426 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1433 ### Generic test cases for varaible checking
1435 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
1436 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1437 # equal to the target or not.
1438 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1439 # before setting pass or fail depending on if the variable value becomes equal to the target
1441 # (Function for test scripts)
1443 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1444 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1447 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1451 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1452 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1453 # equal to the target or not.
1454 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1455 # before setting pass or fail depending on if the variable value becomes equal to the target
1457 # (Function for test scripts)
1459 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1460 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1463 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1467 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1468 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1469 # greater than the target or not.
1470 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1471 # before setting pass or fail depending on if the variable value becomes greater than the target
1473 # (Function for test scripts)
1475 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1476 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
1479 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1483 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1485 echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1488 # Print a variable value from the MR stub.
1489 # arg: <variable-name>
1490 # (Function for test scripts)
1492 if [ $# != 1 ]; then
1494 __print_err "need one arg, <mr-param>" $@
1497 echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD