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="./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 inspect --format='{{(index (index .NetworkSettings.Ports \"$RIC_SIM_PORT/tcp\") 0).HostPort}}' ${name}"
720 if [[ "$res" =~ ^[0-9]+$ ]]; then
727 # Function to create the docker network for the test
728 # Not to be called from the test script itself.
729 __create_docker_network() {
730 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
731 if [ $? -ne 0 ]; then
732 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
735 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
736 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
737 docker network create $DOCKER_SIM_NWNAME
738 if [ $? -ne 0 ]; then
739 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
743 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
747 # Check if container is started by calling url on localhost using a port, expects response code 2XX
748 # args: <container-name> <port> <url> https|https
749 # Not to be called from the test script itself.
750 __check_container_start() {
752 if [ $# -ne 4 ]; then
754 elif [ $4 != "http" ] && [ $4 != "https" ]; then
757 if [ $paramError -ne 0 ]; then
759 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
762 echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
766 if [[ $appname != "STANDALONE_"* ]] ; then
769 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
770 echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
777 if [ $app_started -eq 0 ]; then
780 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
783 if [ $localport -eq 0 ]; then
784 while [ $localport -eq 0 ]; do
785 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
786 localport=$(__find_sim_port $appname)
788 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
790 echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
796 echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
798 if [ $4 == "https" ]; then
799 result="$(__do_curl "-k https://localhost:"${localport}${url})"
801 result="$(__do_curl $LOCALHOST${localport}${url})"
803 if [ $? -eq 0 ]; then
804 if [ ${#result} -gt 15 ]; then
805 #If response is too long, truncate
806 result="...response text too long, omitted"
808 echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
809 echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN"
813 #echo " Retrying in $i seconds"
814 echo -ne " Waiting for container ${appname} service status...retrying in $i seconds${SAMELINE}"
819 if [ "$pa_st" = "false" ]; then
821 echo -e $RED" Container ${appname} did not respond to service status"$ERED
830 # Function to start a container and wait until it responds on the given port and url.
831 #args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
832 __start_container() {
834 variableArgCount=$(($#-2))
835 if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
837 __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 ]*" $@
841 __create_docker_network
847 if [ "$2" == "NODOCKERARGS" ]; then
848 docker-compose up -d &> .dockererr
849 if [ $? -ne 0 ]; then
850 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
853 elif [ "$2" == "STANDALONE" ]; then
854 echo "Skipping docker-compose"
856 docker-compose up -d $2 &> .dockererr
857 if [ $? -ne 0 ]; then
858 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
863 if [ "$2" == "STANDALONE" ]; then
864 app_prefix="STANDALONE_"
868 while [ $cntr -lt $variableArgCount ]; do
869 app=$app_prefix$1; shift;
875 __check_container_start "$app" "$port" "$url" $httpx
887 # Function to load config from a file into consul for the Policy Agent
888 # arg: <json-config-file>
889 # (Function for test scripts)
890 consul_config_app() {
892 echo -e $BOLD"Configuring Consul"$EBOLD
894 if [ $# -ne 1 ]; then
896 __print_err "need one arg, <json-config-file>" $@
900 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
902 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
903 if [ $? -ne 0 ]; then
904 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
908 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
909 echo $body > ".output"$1
911 if [ $? -ne 0 ]; then
912 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
917 targetJson="{\"config\":"$targetJson"}"
918 echo "TARGET JSON: $targetJson" >> $HTTPLOG
919 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
920 if [ $res -ne 0 ]; then
921 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
925 echo -e $GREEN" Config loaded ok to consul"$EGREEN
933 # Function to perpare the consul configuration according to the current simulator configuration
934 # args: SDNC|SDNC_ONAP|NOSDNC <output-file>
935 # (Function for test scripts)
936 prepare_consul_config() {
937 echo -e $BOLD"Prepare Consul config"$EBOLD
939 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
943 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
947 if [ $1 == "SDNC" ]; then
948 echo -e " Config$BOLD including SDNC$EBOLD configuration"
949 elif [ $1 == "SDNC_ONAP" ]; then
950 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
951 elif [ $1 == "NOSDNC" ]; then
952 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
955 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
960 if [ $1 == "SDNC" ]; then
961 config_json=$config_json"\n \"controller\": ["
962 config_json=$config_json"\n {"
963 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
964 if [ $AGENT_STAND_ALONE -eq 0 ]; then
965 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://$SDNC_APP_NAME:$SDNC_PORT\","
967 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://localhost:$SDNC_LOCAL_PORT\","
969 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
970 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
971 config_json=$config_json"\n }"
972 config_json=$config_json"\n ],"
974 if [ $1 == "SDNC_ONAP" ]; then
975 config_json=$config_json"\n \"controller\": ["
976 config_json=$config_json"\n {"
977 config_json=$config_json"\n \"name\": \"$SDNC_ONAP_APP_NAME\","
978 if [ $AGENT_STAND_ALONE -eq 0 ]; then
979 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
981 config_json=$config_json"\n \"baseUrl\": \"http://localhost:$SDNC_ONAP_EXTERNAL_PORT\","
983 config_json=$config_json"\n \"userName\": \"$SDNC_ONAP_USER\","
984 config_json=$config_json"\n \"password\": \"$SDNC_ONAP_PWD\""
985 config_json=$config_json"\n }"
986 config_json=$config_json"\n ],"
990 config_json=$config_json"\n \"streams_publishes\": {"
991 config_json=$config_json"\n \"dmaap_publisher\": {"
992 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
993 config_json=$config_json"\n \"dmaap_info\": {"
994 if [ $AGENT_STAND_ALONE -eq 0 ]; then
995 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_WRITE_URL\""
997 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_WRITE_URL\""
999 config_json=$config_json"\n }"
1000 config_json=$config_json"\n }"
1001 config_json=$config_json"\n },"
1002 config_json=$config_json"\n \"streams_subscribes\": {"
1003 config_json=$config_json"\n \"dmaap_subscriber\": {"
1004 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1005 config_json=$config_json"\n \"dmaap_info\": {"
1006 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1007 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_READ_URL\""
1009 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_READ_URL\""
1011 config_json=$config_json"\n }"
1012 config_json=$config_json"\n }"
1013 config_json=$config_json"\n },"
1015 config_json=$config_json"\n \"ric\": ["
1017 rics=$(docker ps | grep ricsim | awk '{print $NF}')
1019 if [ $? -ne 0 ] || [ -z "$rics" ]; then
1020 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
1026 for ric in $rics; do
1027 if [ $cntr -gt 0 ]; then
1028 config_json=$config_json"\n ,"
1030 config_json=$config_json"\n {"
1031 config_json=$config_json"\n \"name\": \"$ric\","
1032 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1033 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
1035 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://localhost:$(__find_sim_port $ric)\","
1037 if [ $1 == "SDNC" ]; then
1038 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
1039 elif [ $1 == "SDNC_ONAP" ]; then
1040 config_json=$config_json"\n \"controller\": \"$SDNC_ONAP_APP_NAME\","
1042 config_json=$config_json"\n \"managedElementIds\": ["
1043 config_json=$config_json"\n \"me1_$ric\","
1044 config_json=$config_json"\n \"me2_$ric\""
1045 config_json=$config_json"\n ]"
1046 config_json=$config_json"\n }"
1050 config_json=$config_json"\n ]"
1051 config_json=$config_json"\n}"
1054 printf "$config_json">$2
1060 # Start Consul and CBS
1062 # (Function for test scripts)
1063 start_consul_cbs() {
1065 echo -e $BOLD"Starting Consul and CBS"$EBOLD
1067 __start_container consul_cbs NODOCKERARGS "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
1068 "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
1071 ###########################
1072 ### RIC Simulator functions
1073 ###########################
1075 use_simulator_http() {
1076 echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
1077 export RIC_SIM_HTTPX="http"
1078 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1079 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
1083 use_simulator_https() {
1084 echo -e "Using secure $BOLD https $EBOLD towards the simulators"
1085 export RIC_SIM_HTTPX="https"
1086 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1087 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
1091 # Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
1092 # args: ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
1093 # (Function for test scripts)
1094 start_ric_simulators() {
1096 echo -e $BOLD"Starting RIC Simulators"$EBOLD
1098 if [ $# != 3 ]; then
1100 __print_err "need three args, ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1103 echo " $2 simulators using basename: $1 on interface: $3"
1104 #Set env var for simulator count and A1 interface vesion for the given group
1105 if [ $1 == "ricsim_g1" ]; then
1108 elif [ $1 == "ricsim_g2" ]; then
1111 elif [ $1 == "ricsim_g3" ]; then
1116 __print_err "need three args, gricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1120 # Create .env file to compose project, all ric container will get this prefix
1121 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
1123 export G1_A1_VERSION
1124 export G2_A1_VERSION
1125 export G3_A1_VERSION
1127 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1130 while [ $cntr -le $2 ]; do
1133 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
1136 __start_container ric "$docker_args" $app_data
1140 ###########################
1141 ### Control Panel functions
1142 ###########################
1144 # Start the Control Panel container
1146 # (Function for test scripts)
1147 start_control_panel() {
1149 echo -e $BOLD"Starting Control Panel"$EBOLD
1151 __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
1159 # Start the SDNC A1 Controller
1161 # (Function for test scripts)
1164 echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1166 __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT $SDNC_ALIVE_URL "http"
1171 echo -e $BOLD"Using http between agent and SDNC"$EBOLD
1172 export SDNC_HTTPX="http"
1173 export SDNC_PORT=$SDNC_INTERNAL_PORT
1174 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT
1179 echo -e $BOLD"Using https between agent and SDNC"$EBOLD
1180 export SDNC_HTTPX="https"
1181 export SDNC_PORT=$SDNC_INTERNAL_SECURE_PORT
1182 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_SECURE_PORT
1186 #######################
1187 ### SDNC ONAP functions
1188 #######################
1190 # Start the SDNC ONAP A1 Adapter
1192 # (Function for test scripts)
1195 echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1197 __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT $SDNC_ONAP_ALIVE_URL "http"
1201 # Configure the SDNC ONAP A1 Adapter
1203 # (Function for test scripts)
1204 config_sdnc_onap() {
1206 echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1208 LOCALFILE=".sdnc_onap.prop"
1209 REMOTEFILE="/tmp/.sdnc_onap.prop"
1211 docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1212 if [ $? -ne 0 ]; then
1213 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1218 #Config of the prop file shall be inserted here
1220 #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1222 docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1223 if [ $? -ne 0 ]; then
1224 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1228 docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1229 if [ $? -ne 0 ]; then
1230 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1235 #####################
1236 ### MR stub functions
1237 #####################
1239 # Start the Message Router stub interface in the simulator group
1241 # (Function for test scripts)
1244 echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
1245 export MR_CERT_MOUNT_DIR="./cert"
1246 __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
1250 echo -e $BOLD"Using http between agent and MR"$EBOLD
1251 export MR_HTTPX="http"
1252 export MR_PORT=$MR_INTERNAL_PORT
1253 export MR_LOCAL_PORT=$MR_EXTERNAL_PORT
1258 echo -e $BOLD"Using https between agent and MR"$EBOLD
1259 export MR_HTTPX="https"
1260 export MR_PORT=$MR_INTERNAL_SECURE_PORT
1261 export MR_LOCAL_PORT=$MR_EXTERNAL_SECURE_PORT
1270 # Start the Callback reciver in the simulator group
1272 # (Function for test scripts)
1275 echo -e $BOLD"Starting Callback Receiver"$EBOLD
1277 __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
1281 ###########################
1282 ### Policy Agents functions
1283 ###########################
1285 # Use an agent on the local machine instead of container
1286 use_agent_stand_alone() {
1290 # Start the policy agent
1292 # (Function for test scripts)
1293 start_policy_agent() {
1295 echo -e $BOLD"Starting Policy Agent"$EBOLD
1297 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1298 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1300 echo -e $RED"The consul config produced by this test script (filename '<fullpath-to-autotest-dir>.output<file-name>"$ERED
1301 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
1302 echo -e $RED"application.yaml"$ERED
1303 echo -e $RED"The application jar may need to be built beforefor continuing"$ERED
1304 echo -e $RED"The agent shall now be running on port $POLICY_AGENT_EXTERNAL_PORT for http"$ERED
1306 read -p "<press any key to continue>"
1307 __start_container policy_agent "STANDALONE" $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1312 # All calls to the agent will be directed to the agent REST interface from now on
1314 # (Function for test scripts)
1315 use_agent_rest_http() {
1316 echo -e $BOLD"Using agent REST interface with http"$EBOLD
1317 export ADAPTER=$RESTBASE
1321 # All calls to the agent will be directed to the agent REST interface from now on
1323 # (Function for test scripts)
1324 use_agent_rest_https() {
1325 echo -e $BOLD"Using agent REST interface with https"$EBOLD
1326 export ADAPTER=$RESTBASE_SECURE
1331 # All calls to the agent will be directed to the agent dmaap interface from now on
1333 # (Function for test scripts)
1335 echo -e $BOLD"Agent using DMAAP interface"$EBOLD
1336 export ADAPTER=$DMAAPBASE
1342 # Turn on debug level tracing in the agent
1344 # (Function for test scripts)
1346 echo -e $BOLD"Setting agent debug"$EBOLD
1347 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1348 if [ $? -ne 0 ]; then
1349 __print_err "could not set debug mode" $@
1356 # Perform curl retries when making direct call to the agent for the specified http response codes
1357 # Speace separated list of http response codes
1358 # args: [<response-code>]*
1359 use_agent_retries() {
1360 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1361 AGENT_RETRY_CODES=$@
1370 # Check the agent logs for WARNINGs and ERRORs
1372 # (Function for test scripts)
1374 check_policy_agent_logs() {
1375 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
1378 check_control_panel_logs() {
1379 __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
1382 __check_container_logs() {
1386 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1388 #tmp=$(docker ps | grep $appname)
1389 tmp=$(docker ps -q --filter name=$appname) #get the container id
1390 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
1391 echo $dispname" is not running, no check made"
1394 foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1395 if [ $? -ne 0 ];then
1396 echo " Problem to search $appname log $logpath"
1398 if [ $foundentries -eq 0 ]; then
1399 echo " No WARN entries found in $appname log $logpath"
1401 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1404 foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1405 if [ $? -ne 0 ];then
1406 echo " Problem to search $appname log $logpath"
1408 if [ $foundentries -eq 0 ]; then
1409 echo " No ERR entries found in $appname log $logpath"
1411 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1417 # Store all container logs and other logs in the log dir for the script
1418 # Logs are stored with a prefix in case logs should be stored several times during a test
1419 # args: <logfile-prefix>
1420 # (Function for test scripts)
1422 if [ $# != 1 ]; then
1424 __print_err "need one arg, <file-prefix>" $@
1427 echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1$EBOLD
1429 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1430 docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1431 docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1432 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1433 docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1434 docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1435 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1437 docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1439 docker exec -it $SDNC_ONAP_APP_NAME cat $SDNC_ONAP_KARAF_LOG > $TESTLOGS/$ATC/$1_SDNC_ONAP_karaf.log 2>&1
1441 rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1442 for ric in $rics; do
1443 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1445 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1446 echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1453 # Generic curl function, assumed all 200-codes are ok
1455 # returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
1456 # returns: The return code is 0 for ok and 1 for not ok
1458 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1459 curlString="curl -skw %{http_code} $1"
1460 echo " CMD: $curlString" >> $HTTPLOG
1462 echo " RESP: $res" >> $HTTPLOG
1463 http_code="${res:${#res}-3}"
1464 if [ ${#res} -eq 3 ]; then
1465 echo "<no-response-from-server>"
1468 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
1469 echo "<not found, resp:${http_code}>"
1472 if [ $# -eq 2 ]; then
1473 echo "${res:0:${#res}-3}" | xargs
1475 echo "${res:0:${#res}-3}"
1482 #######################################
1483 ### Basic helper function for test cases
1484 #######################################
1486 # Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1487 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
1488 # immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1489 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
1490 # before setting pass or fail depending on the result of comparing variable and target using the operator.
1491 # 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.
1492 # Not to be called from test script.
1495 checkjsonarraycount=0
1497 if [ $# -eq 6 ]; then
1498 if [[ $3 == "json:"* ]]; then
1499 checkjsonarraycount=1
1502 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
1503 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
1508 if [ $checkjsonarraycount -eq 0 ]; then
1509 result="$(__do_curl $2$3)"
1511 result=${result//[[:blank:]]/} #Strip blanks
1514 result="$(__do_curl $2$path)"
1516 echo "$result" > .tmp.curl.json
1517 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1519 duration=$((SECONDS-start))
1520 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
1522 if [ $retcode -ne 0 ]; then
1523 if [ $duration -gt $6 ]; then
1525 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1526 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1529 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1531 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1532 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1533 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
1535 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1537 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1538 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1539 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1541 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1543 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1544 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1545 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1547 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1549 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1550 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1551 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1554 if [ $duration -gt $6 ]; then
1556 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1557 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1563 elif [ $# -eq 5 ]; then
1564 if [[ $3 == "json:"* ]]; then
1565 checkjsonarraycount=1
1568 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
1569 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1571 if [ $checkjsonarraycount -eq 0 ]; then
1572 result="$(__do_curl $2$3)"
1574 result=${result//[[:blank:]]/} #Strip blanks
1577 result="$(__do_curl $2$path)"
1579 echo "$result" > .tmp.curl.json
1580 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1582 if [ $retcode -ne 0 ]; then
1584 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1585 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
1586 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1588 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1589 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met"
1590 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1592 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1593 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1594 elif [ $4 = "<" ] && [ "$result" -lt $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 = "contain_str" ] && [[ $result =~ $5 ]]; then
1600 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1601 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1604 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
1605 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1608 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1615 ### Generic test cases for varaible checking
1617 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
1618 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1619 # equal to the target or not.
1620 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1621 # before setting pass or fail depending on if the variable value becomes equal to the target
1623 # (Function for test scripts)
1625 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1626 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1629 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1633 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1634 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1635 # equal to the target or not.
1636 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1637 # before setting pass or fail depending on if the variable value becomes equal to the target
1639 # (Function for test scripts)
1641 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1642 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1645 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1649 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1650 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1651 # greater than the target or not.
1652 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1653 # before setting pass or fail depending on if the variable value becomes greater than the target
1655 # (Function for test scripts)
1657 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1658 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
1661 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1665 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1667 echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1670 # Print a variable value from the MR stub.
1671 # arg: <variable-name>
1672 # (Function for test scripts)
1674 if [ $# != 1 ]; then
1676 __print_err "need one arg, <mr-param>" $@
1679 echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD