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...
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 # Vars 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
68 export MR_HTTPX="http"
69 export MR_PORT=$MR_INTERNAL_PORT
70 export MR_LOCAL_PORT=$MR_EXTERNAL_PORT #When agent is running outside the docker net
72 export SDNC_HTTPX="http"
73 export SDNC_PORT=$SDNC_INTERNAL_PORT
74 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT #When agent is running outside the docker net
77 LOCALHOST="http://localhost:"
79 # Make curl retries for http response codes set in this env var, space separated list of codes
82 # Var to contol if the agent runs in a container (normal = 0) or as application on the local machine ( = 1)
85 # Var to hold 'auto' in case containers shall be stopped when test case ends
88 # Set a description string for the test case
89 if [ -z "$TC_ONELINE_DESCR" ]; then
90 TC_ONELINE_DESCR="<no-description>"
91 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
94 # Counter for test suites
95 if [ -f .tmp_tcsuite_ctr ]; then
96 tmpval=$(< .tmp_tcsuite_ctr)
98 echo $tmpval > .tmp_tcsuite_ctr
101 # Create a test case id, ATC (Auto Test Case), from the name of the test case script.
102 # FTC1.sh -> ATC == FTC1
103 ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
105 # Create the logs dir if not already created in the current dir
106 if [ ! -d "logs" ]; then
111 # Create a http message log for this testcase
112 HTTPLOG=$PWD"/.httplog_"$ATC".txt"
115 # Create a log dir for the test case
116 mkdir -p $TESTLOGS/$ATC
118 # Clear the log dir for the test case
119 rm $TESTLOGS/$ATC/*.log &> /dev/null
120 rm $TESTLOGS/$ATC/*.txt &> /dev/null
121 rm $TESTLOGS/$ATC/*.json &> /dev/null
123 # Log all output from the test case to a TC log
124 TCLOG=$TESTLOGS/$ATC/TC.log
125 exec &> >(tee ${TCLOG})
127 #Variables for counting tests as well as passed and failed tests
134 #File to keep deviation messages
135 DEVIATION_FILE=".tmp_deviations"
136 rm $DEVIATION_FILE &> /dev/null
138 #Var for measuring execution time
139 TCTEST_START=$SECONDS
141 #File to save timer measurement results
142 TIMER_MEASUREMENTS=".timer_measurement.txt"
143 echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
146 echo "-------------------------------------------------------------------------------------------------"
147 echo "----------------------------------- Test case: "$ATC
148 echo "----------------------------------- Started: "$(date)
149 echo "-------------------------------------------------------------------------------------------------"
150 echo "-- Description: "$TC_ONELINE_DESCR
151 echo "-------------------------------------------------------------------------------------------------"
152 echo "----------------------------------- Test case setup -----------------------------------"
154 echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
156 #Temp var to check for image variable name errors
158 #Create a file with image info for later printing as a table
159 image_list_file=".image-list"
160 echo -e " Container\tImage\ttag" > $image_list_file
162 # Check if image env var is set and if so export the env var with image to use (used by docker compose files)
163 # arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>
164 __check_image_var() {
165 if [ $# -ne 5 ]; then
166 echo "Expected arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>"
171 #Create var from the input var names
175 if [ -z $image ]; then
176 echo -e $RED"\$"$4" not set in test_env"$ERED
179 tmp=$tmp"<no-image>\t"
184 echo -e $RED"\$"$5" not set in test_env"$ERED
191 echo -e "$tmp" >> $image_list_file
193 export "${3}"=$image":"$tag
195 #echo " Configured image for ${1} (script start arg=${2}): "$image":"$tag
198 # Check that image env setting are available
200 if [ $# -lt 1 ] || [ $# -gt 2 ]; then
201 echo "Expected arg: local|remote|remote-remove [auto-clean]"
203 elif [ $1 == "local" ]; then
206 __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
208 #Local Control Panel image
209 __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
212 __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
215 __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
217 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
220 __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_REMOTE_IMAGE" "POLICY_AGENT_REMOTE_IMAGE_TAG"
222 #Remote Control Panel image
223 __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE_TAG"
226 __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE_TAG"
228 #Remote ric sim image
229 __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_REMOTE_IMAGE" "RIC_SIM_REMOTE_IMAGE_TAG"
232 echo "Expected arg: local|remote|remote-remove [auto-clean]"
236 if [ $# -eq 2 ]; then
237 if [ $2 == "auto-clean" ]; then
238 echo "Stting automatic cleaning of container when test case ends"
241 echo "Expected arg: local|remote|remote-remove [auto-clean]"
246 # These images are not built as part of this project official images, just check that env vars are set correctly
247 __check_image_var " Message Router" $1 "MRSTUB_IMAGE" "MRSTUB_LOCAL_IMAGE" "MRSTUB_LOCAL_IMAGE_TAG"
248 __check_image_var " Callback Receiver" $1 "CR_IMAGE" "CR_LOCAL_IMAGE" "CR_LOCAL_IMAGE_TAG"
249 __check_image_var " Consul" $1 "CONSUL_IMAGE" "CONSUL_REMOTE_IMAGE" "CONSUL_REMOTE_IMAGE_TAG"
250 __check_image_var " CBS" $1 "CBS_IMAGE" "CBS_REMOTE_IMAGE" "CBS_REMOTE_IMAGE_TAG"
251 __check_image_var " SDNC DB" $1 "SDNC_DB_IMAGE" "SDNC_DB_REMOTE_IMAGE" "SDNC_DB_REMOTE_IMAGE_TAG"
252 __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"
253 __check_image_var " SDNC ONAP DB" $1 "SDNC_ONAP_DB_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE_TAG"
255 #Errors in image setting - exit
256 if [ $IMAGE_ERR -ne 0 ]; then
260 #Print a tables of the image settings
261 echo -e $BOLD"Images configured for start arg: "$1 $EBOLD
262 column -t -s $'\t' $image_list_file
267 #Set the SIM_GROUP var
268 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
269 if [ -z "$SIM_GROUP" ]; then
270 SIM_GROUP=$PWD/../simulator-group
271 if [ ! -d $SIM_GROUP ]; then
272 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
273 echo -e $RED"Please set the SIM_GROUP manually in the test_env.sh"$ERED
276 echo " SIM_GROUP auto set to: " $SIM_GROUP
278 elif [ $SIM_GROUP = *simulator_group ]; then
279 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
282 echo " SIM_GROUP env var already set to: " $SIM_GROUP
287 #Temp var to check for image pull errors
290 #Function to check if image exist and stop+remove the container+pull new images as needed
291 #args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag>
292 __check_and_pull_image() {
294 echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$4$EBOLD"
295 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
296 tmp_im=$(docker images --format $format_string ${4})
298 if [ $1 == "local" ]; then
299 if [ -z "$tmp_im" ]; then
300 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
304 echo -e " "$2" (local image): \033[1m"$4"\033[0m "$GREEN"OK"$EGREEN
306 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
307 if [ $1 == "remote-remove" ]; then
308 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
309 tmp="$(docker ps -aq --filter name=${3})"
310 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
311 docker stop $tmp &> .dockererr
312 if [ $? -ne 0 ]; then
315 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
320 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
321 tmp="$(docker ps -aq --filter name=${3})" &> /dev/null
322 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
323 docker rm $tmp &> .dockererr
324 if [ $? -ne 0 ]; then
327 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
332 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
333 echo -ne " Removing image - ${SAMELINE}"
334 tmp="$(docker images -q ${4})" &> /dev/null
335 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
336 docker rmi $4 &> .dockererr
337 if [ $? -ne 0 ]; then
340 echo -e $RED" Image could not be removed - try manual removal of the image"$ERED
344 echo -e " Removing image - "$GREEN"removed"$EGREEN
346 echo -e " Removing image - "$GREEN"image not in repository"$EGREEN
350 if [ -z "$tmp_im" ]; then
351 echo -ne " Pulling image${SAMELINE}"
352 docker pull $4 &> .dockererr
353 tmp_im=$(docker images ${4} | grep -v REPOSITORY)
354 if [ -z "$tmp_im" ]; then
356 echo -e " Pulling image -$RED could not be pulled"$ERED
361 echo -e " Pulling image -$GREEN Pulled $EGREEN"
363 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
370 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
372 app="Policy Agent"; __check_and_pull_image $1 "$app" $POLICY_AGENT_APP_NAME $POLICY_AGENT_IMAGE
373 app="Non-RT RIC Control Panel"; __check_and_pull_image $1 "$app" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_IMAGE
374 app="SDNC A1 Controller"; __check_and_pull_image $1 "$app" $SDNC_APP_NAME $SDNC_A1_CONTROLLER_IMAGE
375 app="Near-RT RIC Simulator"; __check_and_pull_image $1 "$app" $RIC_SIM_PREFIX"_"$RIC_SIM_BASE $RIC_SIM_IMAGE
377 app="Consul"; __check_and_pull_image $1 "$app" $CONSUL_APP_NAME $CONSUL_IMAGE
378 app="CBS"; __check_and_pull_image $1 "$app" $CBS_APP_NAME $CBS_IMAGE
379 app="SDNC DB"; __check_and_pull_image $1 "$app" $SDNC_APP_NAME $SDNC_DB_IMAGE
381 echo -e $YELLOW"SDNC ONAP image is skipped"$EYELLOW
382 #app="SDNC ONAP A1 Adapter"; __check_and_pull_image $1 "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_A1_ADAPTER_IMAGE
383 #app="SDNC ONAP DB"; __check_and_pull_image $1 "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_DB_IMAGE
385 # MR stub image not checked, will be built by this script - only local image
386 # CR stub image not checked, will be built by this script - only local image
389 #Errors in image setting - exit
390 if [ $IMAGE_ERR -ne 0 ]; then
392 echo "#################################################################################################"
393 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
394 echo "#################################################################################################"
401 echo -e $BOLD"Building images needed for test"$EBOLD
406 echo " Building mrstub image: mrstub:latest"
407 docker build -t mrstub . &> .dockererr
408 if [ $? -eq 0 ]; then
409 echo -e $GREEN" Build Ok"$EGREEN
411 echo -e $RED" Build Failed"$ERED
418 echo " Building Callback Receiver image: callback-receiver:latest"
419 docker build -t callback-receiver . &> .dockererr
420 if [ $? -eq 0 ]; then
421 echo -e $GREEN" Build Ok"$EGREEN
423 echo -e $RED" Build Failed"$ERED
431 # Create a table of the images used in the script
432 echo -e $BOLD"Local docker registry images used in the this test script"$EBOLD
434 docker_tmp_file=.docker-images-table
435 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}"
436 echo -e " Application\tRepository\tTag\tCreated Since\tSize" > $docker_tmp_file
437 echo -e " Policy Agent\t$(docker images --format $format_string $POLICY_AGENT_IMAGE)" >> $docker_tmp_file
438 echo -e " Control Panel\t$(docker images --format $format_string $CONTROL_PANEL_IMAGE)" >> $docker_tmp_file
439 echo -e " SDNC A1 Controller\t$(docker images --format $format_string $SDNC_A1_CONTROLLER_IMAGE)" >> $docker_tmp_file
440 echo -e " RIC Simulator\t$(docker images --format $format_string $RIC_SIM_IMAGE)" >> $docker_tmp_file
441 echo -e " Message Router\t$(docker images --format $format_string $MRSTUB_IMAGE)" >> $docker_tmp_file
442 echo -e " Callback Receiver\t$(docker images --format $format_string $CR_IMAGE)" >> $docker_tmp_file
443 echo -e " Consul\t$(docker images --format $format_string $CONSUL_IMAGE)" >> $docker_tmp_file
444 echo -e " CBS\t$(docker images --format $format_string $CBS_IMAGE)" >> $docker_tmp_file
445 echo -e " SDNC DB\t$(docker images --format $format_string $SDNC_DB_IMAGE)" >> $docker_tmp_file
446 echo -e " SDNC ONAP A1 Adapter\t$(docker images --format $format_string $SDNC_ONAP_A1_ADAPTER_IMAGE)" >> $docker_tmp_file
447 echo -e " SDNC ONAP DB\t$(docker images --format $format_string $SDNC_ONAP_DB_IMAGE)" >> $docker_tmp_file
449 column -t -s $'\t' $docker_tmp_file
453 echo -e $BOLD"======================================================="$EBOLD
454 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
455 echo -e $BOLD"======================================================="$EBOLD
458 # Function to print the test result, shall be the last cmd in a test script
460 # (Function for test scripts)
464 duration=$((TCTEST_END-TCTEST_START))
466 echo "-------------------------------------------------------------------------------------------------"
467 echo "------------------------------------- Test case: "$ATC
468 echo "------------------------------------- Ended: "$(date)
469 echo "-------------------------------------------------------------------------------------------------"
470 echo "-- Description: "$TC_ONELINE_DESCR
471 echo "-- Execution time: " $duration " seconds"
472 echo "-------------------------------------------------------------------------------------------------"
473 echo "------------------------------------- RESULTS"
477 total=$((RES_PASS+RES_FAIL))
478 if [ $RES_TEST -eq 0 ]; then
479 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
480 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
481 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
482 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
483 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
484 elif [ $total != $RES_TEST ]; then
485 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
486 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
487 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
488 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
489 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
490 elif [ $RES_CONF_FAIL -ne 0 ]; then
491 echo -e "\033[1mOne or more configure regest has failed. Check the script log....\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"
496 elif [ $RES_PASS = $RES_TEST ]; then
497 echo -e "All tests \033[32m\033[1mPASS\033[0m"
498 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
499 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
500 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
501 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
504 # Update test suite counter
505 if [ -f .tmp_tcsuite_pass_ctr ]; then
506 tmpval=$(< .tmp_tcsuite_pass_ctr)
508 echo $tmpval > .tmp_tcsuite_pass_ctr
510 if [ -f .tmp_tcsuite_pass ]; then
511 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
514 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
515 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
516 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
517 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
518 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
520 # Update test suite counter
521 if [ -f .tmp_tcsuite_fail_ctr ]; then
522 tmpval=$(< .tmp_tcsuite_fail_ctr)
524 echo $tmpval > .tmp_tcsuite_fail_ctr
526 if [ -f .tmp_tcsuite_fail ]; then
527 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
531 if [ $RES_DEVIATION -gt 0 ]; then
532 echo "Test case deviations"
533 echo "===================================="
537 echo "Timer measurement in the test script"
538 echo "===================================="
539 column -t -s $'\t' $TIMER_MEASUREMENTS
542 echo "++++ Number of tests: "$RES_TEST
543 echo "++++ Number of passed tests: "$RES_PASS
544 echo "++++ Number of failed tests: "$RES_FAIL
546 echo "++++ Number of failed configs: "$RES_CONF_FAIL
548 echo "++++ Number of test case deviations: "$RES_DEVIATION
550 echo "------------------------------------- Test case complete ---------------------------------"
551 echo "-------------------------------------------------------------------------------------------------"
555 #####################################################################
556 ###### Functions for start, configuring, stoping, cleaning etc ######
557 #####################################################################
559 # Start timer for time measurement
560 # args - (any args will be printed though)
562 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
564 echo " Timer started"
567 # Print the value of the time (in seconds)
568 # args - <timer message to print> - timer value and message will be printed both on screen
569 # and in the timer measurement report
571 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
572 if [ $# -lt 1 ]; then
574 __print_err "need 1 or more args, <timer message to print>" $@
577 duration=$(($SECONDS-$TC_TIMER))
578 if [ $duration -eq 0 ]; then
581 duration=$duration" seconds"
583 echo " Timer duration :" $duration
585 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
588 # Print the value of the time (in seconds) and reset the timer
589 # args - <timer message to print> - timer value and message will be printed both on screen
590 # and in the timer measurement report
591 print_and_reset_timer() {
592 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
593 if [ $# -lt 1 ]; then
595 __print_err "need 1 or more args, <timer message to print>" $@
598 duration=$(($SECONDS-$TC_TIMER))" seconds"
599 if [ $duration -eq 0 ]; then
602 duration=$duration" seconds"
604 echo " Timer duration :" $duration
608 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
611 # Print info about a deviations from intended tests
612 # Each deviation counted is also printed in the testreport
613 # args <deviation message to print>
615 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
616 if [ $# -lt 1 ]; then
618 __print_err "need 1 or more args, <deviation message to print>" $@
622 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
623 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
627 # Stop and remove all containers
629 # (Function for test scripts)
632 echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
634 CONTAINTER_NAMES=("Policy Agent " $POLICY_AGENT_APP_NAME\
635 "Non-RT RIC Simulator(s)" $RIC_SIM_PREFIX\
636 "Message Router " $MR_APP_NAME\
637 "Callback Receiver " $CR_APP_NAME\
638 "Control Panel " $CONTROL_PANEL_APP_NAME\
639 "SDNC A1 Controller " $SDNC_APP_NAME\
640 "SDNC DB " $SDNC_DB_APP_NAME\
641 "SDNC ONAP A1 Adapter " $SDNC_ONAP_APP_NAME\
642 "SDNC DB " $SDNC_ONAP_DB_APP_NAME\
643 "CBS " $CBS_APP_NAME\
644 "Consul " $CONSUL_APP_NAME)
646 nw=0 # Calc max width of container name, to make a nice table
647 for (( i=1; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
648 if [ ${#CONTAINTER_NAMES[i]} -gt $nw ]; then
649 nw=${#CONTAINTER_NAMES[i]}
653 for (( i=0; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
654 APP="${CONTAINTER_NAMES[i]}"
655 CONTR="${CONTAINTER_NAMES[i+1]}"
656 for((w=${#CONTR}; w<$nw; w=w+1)); do
659 echo -ne " $APP: $CONTR - ${GREEN}stopping${EGREEN}${SAMELINE}"
660 docker stop $(docker ps -qa --filter name=${CONTR}) &> /dev/null
661 echo -ne " $APP: $CONTR - ${GREEN}stopped${EGREEN}${SAMELINE}"
662 docker rm $(docker ps -qa --filter name=${CONTR}) &> /dev/null
663 echo -e " $APP: $CONTR - ${GREEN}stopped removed${EGREEN}"
669 # Function stop and remove all container in the end of the test script, if the arg 'auto-clean' is given at test script start
671 # (Function for test scripts)
672 auto_clean_containers() {
674 if [ "$AUTO_CLEAN" == "auto" ]; then
675 echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
680 # Function to sleep a test case for a numner of seconds. Prints the optional text args as info
681 # args: <sleep-time-in-sec> [any-text-in-quoteds-to-printed]
682 # (Function for test scripts)
685 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
686 if [ $# -lt 1 ]; then
688 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
691 #echo "---- Sleep for " $1 " seconds ---- "$2
693 duration=$((SECONDS-start))
694 while [ $duration -lt $1 ]; do
695 echo -ne " Slept for ${duration} seconds${SAMELINE}"
697 duration=$((SECONDS-start))
699 echo -ne " Slept for ${duration} seconds${SAMELINE}"
703 # Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
704 # Not to be called from the test script itself.
706 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
707 if [ $# -gt 1 ]; then
708 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
713 # Helper function to get a the port of a specific ric simulatpor
715 # (Not for test scripts)
717 name=$1" " #Space appended to prevent matching 10 if 1 is desired....
718 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 '-'"
719 cmdstr="docker ps --filter name=${name} --format \"{{.Names}} {{.Ports}}\" | grep '${name}' | cut -f 3 -d ',' | sed s/0.0.0.0:// | cut -f 2 -d ' ' | cut -f 1 -d '-'"
722 if [[ "$res" =~ ^[0-9]+$ ]]; then
729 # Function to create the docker network for the test
730 # Not to be called from the test script itself.
731 __create_docker_network() {
732 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
733 if [ $? -ne 0 ]; then
734 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
737 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
738 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
739 docker network create $DOCKER_SIM_NWNAME
740 if [ $? -ne 0 ]; then
741 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
745 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
749 # Check if container is started by calling url on localhost using a port, expects response code 2XX
750 # args: <container-name> <port> <url> https|https
751 # Not to be called from the test script itself.
752 __check_container_start() {
754 if [ $# -ne 4 ]; then
756 elif [ $4 != "http" ] && [ $4 != "https" ]; then
759 if [ $paramError -ne 0 ]; then
761 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
764 echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
768 if [[ $appname != "STANDALONE_"* ]] ; then
771 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
772 echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
779 if [ $app_started -eq 0 ]; then
782 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
785 if [ $localport -eq 0 ]; then
786 while [ $localport -eq 0 ]; do
787 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
788 localport=$(__find_sim_port $appname)
790 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
792 echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
798 echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
800 if [ $4 == "https" ]; then
801 result="$(__do_curl "-k https://localhost:"${localport}${url})"
803 result="$(__do_curl $LOCALHOST${localport}${url})"
805 if [ $? -eq 0 ]; then
806 if [ ${#result} -gt 15 ]; then
807 #If response is too long, truncate
808 result="...response text too long, omitted"
810 echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
811 echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN"
815 #echo " Retrying in $i seconds"
816 echo -ne " Waiting for container ${appname} service status...retrying in $i seconds${SAMELINE}"
821 if [ "$pa_st" = "false" ]; then
823 echo -e $RED" Container ${appname} did not respond to service status"$ERED
832 # Function to start a container and wait until it responds on the given port and url.
833 #args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
834 __start_container() {
836 variableArgCount=$(($#-2))
837 if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
839 __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 ]*" $@
843 __create_docker_network
849 if [ "$2" == "NODOCKERARGS" ]; then
850 docker-compose up -d &> .dockererr
851 if [ $? -ne 0 ]; then
852 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
855 elif [ "$2" == "STANDALONE" ]; then
856 echo "Skipping docker-compose"
858 docker-compose up -d $2 &> .dockererr
859 if [ $? -ne 0 ]; then
860 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
865 if [ "$2" == "STANDALONE" ]; then
866 app_prefix="STANDALONE_"
870 while [ $cntr -lt $variableArgCount ]; do
871 app=$app_prefix$1; shift;
877 __check_container_start "$app" "$port" "$url" $httpx
889 # Function to load config from a file into consul for the Policy Agent
890 # arg: <json-config-file>
891 # (Function for test scripts)
892 consul_config_app() {
894 echo -e $BOLD"Configuring Consul"$EBOLD
896 if [ $# -ne 1 ]; then
898 __print_err "need one arg, <json-config-file>" $@
902 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
904 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
905 if [ $? -ne 0 ]; then
906 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
910 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
911 echo $body > ".output"$1
913 if [ $? -ne 0 ]; then
914 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
919 targetJson="{\"config\":"$targetJson"}"
920 echo "TARGET JSON: $targetJson" >> $HTTPLOG
921 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
922 if [ $res -ne 0 ]; then
923 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
927 echo -e $GREEN" Config loaded ok to consul"$EGREEN
935 # Function to perpare the consul configuration according to the current simulator configuration
936 # args: SDNC|SDNC_ONAP|NOSDNC <output-file>
937 # (Function for test scripts)
938 prepare_consul_config() {
939 echo -e $BOLD"Prepare Consul config"$EBOLD
941 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
945 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
949 if [ $1 == "SDNC" ]; then
950 echo -e " Config$BOLD including SDNC$EBOLD configuration"
951 elif [ $1 == "SDNC_ONAP" ]; then
952 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
953 elif [ $1 == "NOSDNC" ]; then
954 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
957 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
962 if [ $1 == "SDNC" ]; then
963 config_json=$config_json"\n \"controller\": ["
964 config_json=$config_json"\n {"
965 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
966 if [ $AGENT_STAND_ALONE -eq 0 ]; then
967 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://$SDNC_APP_NAME:$SDNC_PORT\","
969 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://localhost:$SDNC_LOCAL_PORT\","
971 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
972 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
973 config_json=$config_json"\n }"
974 config_json=$config_json"\n ],"
976 if [ $1 == "SDNC_ONAP" ]; then
977 config_json=$config_json"\n \"controller\": ["
978 config_json=$config_json"\n {"
979 config_json=$config_json"\n \"name\": \"$SDNC_ONAP_APP_NAME\","
980 if [ $AGENT_STAND_ALONE -eq 0 ]; then
981 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
983 config_json=$config_json"\n \"baseUrl\": \"http://localhost:$SDNC_ONAP_EXTERNAL_PORT\","
985 config_json=$config_json"\n \"userName\": \"$SDNC_ONAP_USER\","
986 config_json=$config_json"\n \"password\": \"$SDNC_ONAP_PWD\""
987 config_json=$config_json"\n }"
988 config_json=$config_json"\n ],"
992 config_json=$config_json"\n \"streams_publishes\": {"
993 config_json=$config_json"\n \"dmaap_publisher\": {"
994 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
995 config_json=$config_json"\n \"dmaap_info\": {"
996 if [ $AGENT_STAND_ALONE -eq 0 ]; then
997 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_WRITE_URL\""
999 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_WRITE_URL\""
1001 config_json=$config_json"\n }"
1002 config_json=$config_json"\n }"
1003 config_json=$config_json"\n },"
1004 config_json=$config_json"\n \"streams_subscribes\": {"
1005 config_json=$config_json"\n \"dmaap_subscriber\": {"
1006 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1007 config_json=$config_json"\n \"dmaap_info\": {"
1008 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1009 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_READ_URL\""
1011 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_READ_URL\""
1013 config_json=$config_json"\n }"
1014 config_json=$config_json"\n }"
1015 config_json=$config_json"\n },"
1017 config_json=$config_json"\n \"ric\": ["
1019 rics=$(docker ps | grep ricsim | awk '{print $NF}')
1021 if [ $? -ne 0 ] || [ -z "$rics" ]; then
1022 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
1028 for ric in $rics; do
1029 if [ $cntr -gt 0 ]; then
1030 config_json=$config_json"\n ,"
1032 config_json=$config_json"\n {"
1033 config_json=$config_json"\n \"name\": \"$ric\","
1034 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1035 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
1037 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://localhost:$(__find_sim_port $ric)\","
1039 if [ $1 == "SDNC" ]; then
1040 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
1041 elif [ $1 == "SDNC_ONAP" ]; then
1042 config_json=$config_json"\n \"controller\": \"$SDNC_ONAP_APP_NAME\","
1044 config_json=$config_json"\n \"managedElementIds\": ["
1045 config_json=$config_json"\n \"me1_$ric\","
1046 config_json=$config_json"\n \"me2_$ric\""
1047 config_json=$config_json"\n ]"
1048 config_json=$config_json"\n }"
1052 config_json=$config_json"\n ]"
1053 config_json=$config_json"\n}"
1056 printf "$config_json">$2
1062 # Start Consul and CBS
1064 # (Function for test scripts)
1065 start_consul_cbs() {
1067 echo -e $BOLD"Starting Consul and CBS"$EBOLD
1069 __start_container consul_cbs NODOCKERARGS "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
1070 "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
1073 ###########################
1074 ### RIC Simulator functions
1075 ###########################
1077 use_simulator_http() {
1078 echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
1079 export RIC_SIM_HTTPX="http"
1080 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1081 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
1082 export RIC_SIM_CERT_MOUNT_DIR="./fakedir" #Fake dir so that the sim container does not find any cert
1086 use_simulator_https() {
1087 echo -e "Using secure $BOLD https $EBOLD towards the simulators"
1088 export RIC_SIM_HTTPX="https"
1089 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1090 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
1091 export RIC_SIM_CERT_MOUNT_DIR="./cert"
1095 # Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
1096 # args: ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
1097 # (Function for test scripts)
1098 start_ric_simulators() {
1100 echo -e $BOLD"Starting RIC Simulators"$EBOLD
1102 if [ $# != 3 ]; then
1104 __print_err "need three args, ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1107 echo " $2 simulators using basename: $1 on interface: $3"
1108 #Set env var for simulator count and A1 interface vesion for the given group
1109 if [ $1 == "ricsim_g1" ]; then
1112 elif [ $1 == "ricsim_g2" ]; then
1115 elif [ $1 == "ricsim_g3" ]; then
1120 __print_err "need three args, gricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1124 # Create .env file to compose project, all ric container will get this prefix
1125 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
1127 export G1_A1_VERSION
1128 export G2_A1_VERSION
1129 export G3_A1_VERSION
1131 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1134 while [ $cntr -le $2 ]; do
1137 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
1140 __start_container ric "$docker_args" $app_data
1144 ###########################
1145 ### Control Panel functions
1146 ###########################
1148 # Start the Control Panel container
1150 # (Function for test scripts)
1151 start_control_panel() {
1153 echo -e $BOLD"Starting Control Panel"$EBOLD
1155 __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
1163 # Start the SDNC A1 Controller
1165 # (Function for test scripts)
1168 echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1170 __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT $SDNC_ALIVE_URL "http"
1175 echo -e $BOLD"Using http between agent and SDNC"$EBOLD
1176 export SDNC_HTTPX="http"
1177 export SDNC_PORT=$SDNC_INTERNAL_PORT
1178 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT
1183 echo -e $BOLD"Using https between agent and SDNC"$EBOLD
1184 export SDNC_HTTPX="https"
1185 export SDNC_PORT=$SDNC_INTERNAL_SECURE_PORT
1186 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_SECURE_PORT
1190 #######################
1191 ### SDNC ONAP functions
1192 #######################
1194 # Start the SDNC ONAP A1 Adapter
1196 # (Function for test scripts)
1199 echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1201 __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT $SDNC_ONAP_ALIVE_URL "http"
1205 # Configure the SDNC ONAP A1 Adapter
1207 # (Function for test scripts)
1208 config_sdnc_onap() {
1210 echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1212 LOCALFILE=".sdnc_onap.prop"
1213 REMOTEFILE="/tmp/.sdnc_onap.prop"
1215 docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1216 if [ $? -ne 0 ]; then
1217 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1222 #Config of the prop file shall be inserted here
1224 #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1226 docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1227 if [ $? -ne 0 ]; then
1228 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1232 docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1233 if [ $? -ne 0 ]; then
1234 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1239 #####################
1240 ### MR stub functions
1241 #####################
1243 # Start the Message Router stub interface in the simulator group
1245 # (Function for test scripts)
1248 echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
1249 export MR_CERT_MOUNT_DIR="./cert"
1250 __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
1254 echo -e $BOLD"Using http between agent and MR"$EBOLD
1255 export MR_HTTPX="http"
1256 export MR_PORT=$MR_INTERNAL_PORT
1257 export MR_LOCAL_PORT=$MR_EXTERNAL_PORT
1262 echo -e $BOLD"Using https between agent and MR"$EBOLD
1263 export MR_HTTPX="https"
1264 export MR_PORT=$MR_INTERNAL_SECURE_PORT
1265 export MR_LOCAL_PORT=$MR_EXTERNAL_SECURE_PORT
1274 # Start the Callback reciver in the simulator group
1276 # (Function for test scripts)
1279 echo -e $BOLD"Starting Callback Receiver"$EBOLD
1281 __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
1285 ###########################
1286 ### Policy Agents functions
1287 ###########################
1289 # Use an agent on the local machine instead of container
1290 use_agent_stand_alone() {
1294 # Start the policy agent
1296 # (Function for test scripts)
1297 start_policy_agent() {
1299 echo -e $BOLD"Starting Policy Agent"$EBOLD
1301 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1302 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1304 echo -e $RED"The consul config produced by this test script (filename '<fullpath-to-autotest-dir>.output<file-name>"$ERED
1305 echo -e $RED"where the file name is the file in the consul_config_app command in this script) must be pointed out by the agent "$ERED
1306 echo -e $RED"application.yaml"$ERED
1307 echo -e $RED"The application jar may need to be built beforefor continuing"$ERED
1308 echo -e $RED"The agent shall now be running on port $POLICY_AGENT_EXTERNAL_PORT for http"$ERED
1310 read -p "<press any key to continue>"
1311 __start_container policy_agent "STANDALONE" $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1316 # All calls to the agent will be directed to the agent REST interface from now on
1318 # (Function for test scripts)
1319 use_agent_rest_http() {
1320 echo -e $BOLD"Using agent REST interface with http"$EBOLD
1321 export ADAPTER=$RESTBASE
1325 # All calls to the agent will be directed to the agent REST interface from now on
1327 # (Function for test scripts)
1328 use_agent_rest_https() {
1329 echo -e $BOLD"Using agent REST interface with https"$EBOLD
1330 export ADAPTER=$RESTBASE_SECURE
1335 # All calls to the agent will be directed to the agent dmaap interface from now on
1337 # (Function for test scripts)
1339 echo -e $BOLD"Agent using DMAAP interface"$EBOLD
1340 export ADAPTER=$DMAAPBASE
1346 # Turn on debug level tracing in the agent
1348 # (Function for test scripts)
1350 echo -e $BOLD"Setting agent debug"$EBOLD
1351 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1352 if [ $? -ne 0 ]; then
1353 __print_err "could not set debug mode" $@
1360 # Perform curl retries when making direct call to the agent for the specified http response codes
1361 # Speace separated list of http response codes
1362 # args: [<response-code>]*
1363 use_agent_retries() {
1364 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1365 AGENT_RETRY_CODES=$@
1374 # Check the agent logs for WARNINGs and ERRORs
1376 # (Function for test scripts)
1378 check_policy_agent_logs() {
1379 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
1382 check_control_panel_logs() {
1383 __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
1386 __check_container_logs() {
1390 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1392 #tmp=$(docker ps | grep $appname)
1393 tmp=$(docker ps -q --filter name=$appname) #get the container id
1394 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
1395 echo $dispname" is not running, no check made"
1398 foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1399 if [ $? -ne 0 ];then
1400 echo " Problem to search $appname log $logpath"
1402 if [ $foundentries -eq 0 ]; then
1403 echo " No WARN entries found in $appname log $logpath"
1405 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1408 foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1409 if [ $? -ne 0 ];then
1410 echo " Problem to search $appname log $logpath"
1412 if [ $foundentries -eq 0 ]; then
1413 echo " No ERR entries found in $appname log $logpath"
1415 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1421 # Store all container logs and other logs in the log dir for the script
1422 # Logs are stored with a prefix in case logs should be stored several times during a test
1423 # args: <logfile-prefix>
1424 # (Function for test scripts)
1426 if [ $# != 1 ]; then
1428 __print_err "need one arg, <file-prefix>" $@
1431 echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1$EBOLD
1433 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1434 docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1435 docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1436 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1437 docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1438 docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1439 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1441 docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1443 docker exec -it $SDNC_ONAP_APP_NAME cat $SDNC_ONAP_KARAF_LOG > $TESTLOGS/$ATC/$1_SDNC_ONAP_karaf.log 2>&1
1445 rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1446 for ric in $rics; do
1447 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1449 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1450 echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1457 # Generic curl function, assumed all 200-codes are ok
1459 # returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
1460 # returns: The return code is 0 for ok and 1 for not ok
1462 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1463 curlString="curl -skw %{http_code} $1"
1464 echo " CMD: $curlString" >> $HTTPLOG
1466 echo " RESP: $res" >> $HTTPLOG
1467 http_code="${res:${#res}-3}"
1468 if [ ${#res} -eq 3 ]; then
1469 echo "<no-response-from-server>"
1472 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
1473 echo "<not found, resp:${http_code}>"
1476 if [ $# -eq 2 ]; then
1477 echo "${res:0:${#res}-3}" | xargs
1479 echo "${res:0:${#res}-3}"
1486 #######################################
1487 ### Basic helper function for test cases
1488 #######################################
1490 # Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1491 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
1492 # immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1493 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
1494 # before setting pass or fail depending on the result of comparing variable and target using the operator.
1495 # 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.
1496 # Not to be called from test script.
1499 checkjsonarraycount=0
1501 if [ $# -eq 6 ]; then
1502 if [[ $3 == "json:"* ]]; then
1503 checkjsonarraycount=1
1506 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
1507 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
1512 if [ $checkjsonarraycount -eq 0 ]; then
1513 result="$(__do_curl $2$3)"
1515 result=${result//[[:blank:]]/} #Strip blanks
1518 result="$(__do_curl $2$path)"
1520 echo "$result" > .tmp.curl.json
1521 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1523 duration=$((SECONDS-start))
1524 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
1526 if [ $retcode -ne 0 ]; then
1527 if [ $duration -gt $6 ]; then
1529 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1530 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1533 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1535 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1536 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1537 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
1539 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1541 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1542 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1543 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1545 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1547 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1548 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1549 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1551 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1553 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1554 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1555 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1558 if [ $duration -gt $6 ]; then
1560 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1561 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1567 elif [ $# -eq 5 ]; then
1568 if [[ $3 == "json:"* ]]; then
1569 checkjsonarraycount=1
1572 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
1573 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1575 if [ $checkjsonarraycount -eq 0 ]; then
1576 result="$(__do_curl $2$3)"
1578 result=${result//[[:blank:]]/} #Strip blanks
1581 result="$(__do_curl $2$path)"
1583 echo "$result" > .tmp.curl.json
1584 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1586 if [ $retcode -ne 0 ]; then
1588 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1589 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
1590 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1592 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1593 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met"
1594 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1596 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1597 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1598 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1600 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1601 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1602 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1604 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1605 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1608 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
1609 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1612 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1619 ### Generic test cases for varaible checking
1621 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
1622 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1623 # equal to the target or not.
1624 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1625 # before setting pass or fail depending on if the variable value becomes equal to the target
1627 # (Function for test scripts)
1629 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1630 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1633 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1637 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1638 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1639 # equal to the target or not.
1640 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1641 # before setting pass or fail depending on if the variable value becomes equal to the target
1643 # (Function for test scripts)
1645 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1646 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1649 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1653 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1654 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1655 # greater than the target or not.
1656 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1657 # before setting pass or fail depending on if the variable value becomes greater than the target
1659 # (Function for test scripts)
1661 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1662 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
1665 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1669 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1671 echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1674 # Print a variable value from the MR stub.
1675 # arg: <variable-name>
1676 # (Function for test scripts)
1678 if [ $# != 1 ]; then
1680 __print_err "need one arg, <mr-param>" $@
1683 echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD