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] [--stop-at-error] [--ricsim-prefix <prefix> ] [--use-local-image <app-nam> [<app-name>]*]
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 tmp=$(which docker-compose)
47 if [ $? -ne 0 ] || [ -z tmp ]; then
48 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
52 # Just resetting any previous echo formatting...
55 # source test environment variables
56 . ../common/test_env.sh
58 echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
60 #Vars for A1 interface version and container count
68 # Vars to switch between http and https. Extra curl flag needed for https
69 export RIC_SIM_HTTPX="http"
70 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
71 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
72 export RIC_SIM_CERT_MOUNT_DIR="./cert"
74 export MR_HTTPX="http"
75 export MR_PORT=$MR_INTERNAL_PORT
76 export MR_LOCAL_PORT=$MR_EXTERNAL_PORT #When agent is running outside the docker net
78 export SDNC_HTTPX="http"
79 export SDNC_PORT=$SDNC_INTERNAL_PORT
80 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT #When agent is running outside the docker net
83 LOCALHOST="http://localhost:"
85 # Make curl retries for http response codes set in this env var, space separated list of codes
88 # Var to contol if the agent runs in a container (normal = 0) or as application on the local machine ( = 1)
91 # Var to hold 'auto' in case containers shall be stopped when test case ends
94 # Var to hold the app names to use local image for when running 'remote' or 'remote-remove'
97 # List of available apps to override with local image
98 AVAILABLE_LOCAL_IMAGES_OVERRIDE="PA CP SDNC RICSIM"
100 # Use this var (STOP_AT_ERROR=1 in the test script) for debugging/trouble shooting to take all logs and exit at first FAIL test case
103 # Set a description string for the test case
104 if [ -z "$TC_ONELINE_DESCR" ]; then
105 TC_ONELINE_DESCR="<no-description>"
106 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
109 # Counter for test suites
110 if [ -f .tmp_tcsuite_ctr ]; then
111 tmpval=$(< .tmp_tcsuite_ctr)
113 echo $tmpval > .tmp_tcsuite_ctr
116 # Create a test case id, ATC (Auto Test Case), from the name of the test case script.
117 # FTC1.sh -> ATC == FTC1
118 ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
120 # Create the logs dir if not already created in the current dir
121 if [ ! -d "logs" ]; then
126 # Create a http message log for this testcase
127 HTTPLOG=$PWD"/.httplog_"$ATC".txt"
130 #Create result file (containing '1' for error) for this test case
131 #Will be replaced with a file containing '0' if script is ok
133 echo "1" > "$PWD/.result$ATC.txt"
135 # Create a log dir for the test case
136 mkdir -p $TESTLOGS/$ATC
138 # Clear the log dir for the test case
139 rm $TESTLOGS/$ATC/*.log &> /dev/null
140 rm $TESTLOGS/$ATC/*.txt &> /dev/null
141 rm $TESTLOGS/$ATC/*.json &> /dev/null
143 # Log all output from the test case to a TC log
144 TCLOG=$TESTLOGS/$ATC/TC.log
145 exec &> >(tee ${TCLOG})
147 #Variables for counting tests as well as passed and failed tests
154 #File to keep deviation messages
155 DEVIATION_FILE=".tmp_deviations"
156 rm $DEVIATION_FILE &> /dev/null
158 #Var for measuring execution time
159 TCTEST_START=$SECONDS
161 #File to save timer measurement results
162 TIMER_MEASUREMENTS=".timer_measurement.txt"
163 echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
166 echo "-------------------------------------------------------------------------------------------------"
167 echo "----------------------------------- Test case: "$ATC
168 echo "----------------------------------- Started: "$(date)
169 echo "-------------------------------------------------------------------------------------------------"
170 echo "-- Description: "$TC_ONELINE_DESCR
171 echo "-------------------------------------------------------------------------------------------------"
172 echo "----------------------------------- Test case setup -----------------------------------"
176 if [ $# -lt 1 ]; then
179 if [ $paramerror -eq 0 ]; then
180 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ] && [ "$1" != "local" ]; then
187 while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
189 if [ $paramerror -eq 0 ]; then
190 if [ "$1" == "auto-clean" ]; then
192 echo "Option set - Auto clean at end of test script"
197 if [ $paramerror -eq 0 ]; then
198 if [ "$1" == "--stop-at-error" ]; then
200 echo "Option set - Stop at first error"
205 if [ $paramerror -eq 0 ]; then
206 if [ "$1" == "--ricsim-prefix" ]; then
212 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
218 if [ $paramerror -eq 0 ]; then
219 if [ "$1" == "--use-local-image" ]; then
222 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
223 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
224 if [[ "$AVAILABLE_LOCAL_IMAGES_OVERRIDE" != *"$1"* ]]; then
230 if [ -z "$USE_LOCAL_IMAGES" ]; then
233 echo "Option set - Override remote images for app(s):"$USE_LOCAL_IMAGES
241 if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
245 if [ $paramerror -eq 1 ]; then
246 echo -e $RED"Expected arg: local|remote|remote-remove [auto-clean] [--stop-at-error] [--ricsim-prefix <prefix> ] [--use-local-image <app-nam> [<app-name>]*]"$ERED
250 echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
252 #Temp var to check for image variable name errors
254 #Create a file with image info for later printing as a table
255 image_list_file=".image-list"
256 echo -e " Container\tImage\ttag" > $image_list_file
258 # Check if image env var is set and if so export the env var with image to use (used by docker compose files)
259 # arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>
260 __check_image_var() {
261 if [ $# -ne 5 ]; then
262 echo "Expected arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>"
267 #Create var from the input var names
271 if [ -z $image ]; then
272 echo -e $RED"\$"$4" not set in test_env"$ERED
275 tmp=$tmp"<no-image>\t"
280 echo -e $RED"\$"$5" not set in test_env"$ERED
287 echo -e "$tmp" >> $image_list_file
289 export "${3}"=$image":"$tag
291 #echo " Configured image for ${1} (script start arg=${2}): "$image":"$tag
295 #Check if app local image shall override remote image
296 __check_image_local_override() {
297 for im in $USE_LOCAL_IMAGES; do
298 if [ "$1" == "$im" ]; then
305 #Check if app uses image excluded from this test run
306 __check_excluded_image() {
307 for im in $EXCLUDED_IMAGES; do
308 if [ "$1" == "$im" ]; then
315 # Check that image env setting are available
318 if [ $START_ARG == "local" ]; then
321 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
323 #Local Control Panel image
324 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
327 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
330 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
332 elif [ $START_ARG == "remote" ] || [ $START_ARG == "remote-remove" ]; then
334 __check_image_local_override 'PA'
335 if [ $? -eq 0 ]; then
337 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_REMOTE_IMAGE" "POLICY_AGENT_REMOTE_IMAGE_TAG"
340 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
343 __check_image_local_override 'CP'
344 if [ $? -eq 0 ]; then
345 #Remote Control Panel image
346 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE_TAG"
348 #Local Control Panel image
349 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
352 __check_image_local_override 'SDNC'
353 if [ $? -eq 0 ]; then
355 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE_TAG"
358 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
361 __check_image_local_override 'RICSIM'
362 if [ $? -eq 0 ]; then
363 #Remote ric sim image
364 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_REMOTE_IMAGE" "RIC_SIM_REMOTE_IMAGE_TAG"
367 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
371 #Should never get here....
372 echo "Unknow args: "$@
377 # These images are not built as part of this project official images, just check that env vars are set correctly
378 __check_image_var " Message Router" $START_ARG "MRSTUB_IMAGE" "MRSTUB_LOCAL_IMAGE" "MRSTUB_LOCAL_IMAGE_TAG"
379 __check_image_var " Callback Receiver" $START_ARG "CR_IMAGE" "CR_LOCAL_IMAGE" "CR_LOCAL_IMAGE_TAG"
380 __check_image_var " Consul" $START_ARG "CONSUL_IMAGE" "CONSUL_REMOTE_IMAGE" "CONSUL_REMOTE_IMAGE_TAG"
381 __check_image_var " CBS" $START_ARG "CBS_IMAGE" "CBS_REMOTE_IMAGE" "CBS_REMOTE_IMAGE_TAG"
382 __check_image_var " SDNC DB" $START_ARG "SDNC_DB_IMAGE" "SDNC_DB_REMOTE_IMAGE" "SDNC_DB_REMOTE_IMAGE_TAG"
383 __check_excluded_image 'SDNC_ONAP'
384 if [ $? -eq 0 ]; then
385 __check_image_var " SDNC ONAP A1 Adapter" $START_ARG "SDNC_ONAP_A1_ADAPTER_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE_TAG"
386 __check_image_var " SDNC ONAP DB" $START_ARG "SDNC_ONAP_DB_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE_TAG"
389 #Errors in image setting - exit
390 if [ $IMAGE_ERR -ne 0 ]; then
394 #Print a tables of the image settings
395 echo -e $BOLD"Images configured for start arg: "$START $EBOLD
396 column -t -s $'\t' $image_list_file
401 #Set the SIM_GROUP var
402 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
403 if [ -z "$SIM_GROUP" ]; then
404 SIM_GROUP=$PWD/../simulator-group
405 if [ ! -d $SIM_GROUP ]; then
406 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
407 echo -e $RED"Please set the SIM_GROUP manually in the test_env.sh"$ERED
410 echo " SIM_GROUP auto set to: " $SIM_GROUP
412 elif [ $SIM_GROUP = *simulator_group ]; then
413 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
416 echo " SIM_GROUP env var already set to: " $SIM_GROUP
421 #Temp var to check for image pull errors
424 #Function to check if image exist and stop+remove the container+pull new images as needed
425 #args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag>
426 __check_and_pull_image() {
428 echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$4$EBOLD"
429 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
430 tmp_im=$(docker images --format $format_string ${4})
432 if [ $1 == "local" ]; then
433 if [ -z "$tmp_im" ]; then
434 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
438 echo -e " "$2" (local image): \033[1m"$4"\033[0m "$GREEN"OK"$EGREEN
440 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
441 if [ $1 == "remote-remove" ]; then
442 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
443 tmp="$(docker ps -aq --filter name=${3})"
444 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
445 docker stop $tmp &> .dockererr
446 if [ $? -ne 0 ]; then
449 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
454 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
455 tmp="$(docker ps -aq --filter name=${3})" &> /dev/null
456 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
457 docker rm $tmp &> .dockererr
458 if [ $? -ne 0 ]; then
461 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
466 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
467 echo -ne " Removing image - ${SAMELINE}"
468 tmp="$(docker images -q ${4})" &> /dev/null
469 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
470 docker rmi $4 &> .dockererr
471 if [ $? -ne 0 ]; then
474 echo -e $RED" Image could not be removed - try manual removal of the image"$ERED
478 echo -e " Removing image - "$GREEN"removed"$EGREEN
480 echo -e " Removing image - "$GREEN"image not in repository"$EGREEN
484 if [ -z "$tmp_im" ]; then
485 echo -ne " Pulling image${SAMELINE}"
486 docker pull $4 &> .dockererr
487 tmp_im=$(docker images ${4} | grep -v REPOSITORY)
488 if [ -z "$tmp_im" ]; then
490 echo -e " Pulling image -$RED could not be pulled"$ERED
495 echo -e " Pulling image -$GREEN Pulled $EGREEN"
497 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
504 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
506 START_ARG_MOD=$START_ARG
507 __check_image_local_override 'PA'
508 if [ $? -eq 1 ]; then
509 START_ARG_MOD="local"
511 app="Policy Agent"; __check_and_pull_image $START_ARG_MOD "$app" $POLICY_AGENT_APP_NAME $POLICY_AGENT_IMAGE
513 START_ARG_MOD=$START_ARG
514 __check_image_local_override 'CP'
515 if [ $? -eq 1 ]; then
516 START_ARG_MOD="local"
518 app="Non-RT RIC Control Panel"; __check_and_pull_image $START_ARG_MOD "$app" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_IMAGE
520 START_ARG_MOD=$START_ARG
521 __check_image_local_override 'RICSIM'
522 if [ $? -eq 1 ]; then
523 START_ARG_MOD="local"
525 app="Near-RT RIC Simulator"; __check_and_pull_image $START_ARG_MOD "$app" $RIC_SIM_PREFIX"_"$RIC_SIM_BASE $RIC_SIM_IMAGE
527 app="Consul"; __check_and_pull_image $START_ARG "$app" $CONSUL_APP_NAME $CONSUL_IMAGE
528 app="CBS"; __check_and_pull_image $START_ARG "$app" $CBS_APP_NAME $CBS_IMAGE
529 __check_excluded_image 'SDNC'
530 if [ $? -eq 0 ]; then
531 START_ARG_MOD=$START_ARG
532 __check_image_local_override 'SDNC'
533 if [ $? -eq 1 ]; then
534 START_ARG_MOD="local"
536 app="SDNC A1 Controller"; __check_and_pull_image $START_ARG_MOD "$app" $SDNC_APP_NAME $SDNC_A1_CONTROLLER_IMAGE
537 app="SDNC DB"; __check_and_pull_image $START_ARG "$app" $SDNC_APP_NAME $SDNC_DB_IMAGE
539 echo -e $YELLOW" Excluding SDNC image and related DB image from image check/pull"$EYELLOW
541 __check_excluded_image 'SDNC_ONAP'
542 if [ $? -eq 0 ]; then
543 app="SDNC ONAP A1 Adapter"; __check_and_pull_image $START_ARG "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_A1_ADAPTER_IMAGE
544 app="SDNC ONAP DB"; __check_and_pull_image $START_ARG "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_DB_IMAGE
546 echo -e $YELLOW" Excluding ONAP SDNC image and related DB image from image check/pull"$EYELLOW
548 # MR stub image not checked, will be built by this script - only local image
549 # CR stub image not checked, will be built by this script - only local image
552 #Errors in image setting - exit
553 if [ $IMAGE_ERR -ne 0 ]; then
555 echo "#################################################################################################"
556 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
557 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
558 echo "#################################################################################################"
565 echo -e $BOLD"Building images needed for test"$EBOLD
570 echo " Building mrstub image: mrstub:latest"
571 docker build -t mrstub . &> .dockererr
572 if [ $? -eq 0 ]; then
573 echo -e $GREEN" Build Ok"$EGREEN
575 echo -e $RED" Build Failed"$ERED
582 echo " Building Callback Receiver image: callback-receiver:latest"
583 docker build -t callback-receiver . &> .dockererr
584 if [ $? -eq 0 ]; then
585 echo -e $GREEN" Build Ok"$EGREEN
587 echo -e $RED" Build Failed"$ERED
595 # Create a table of the images used in the script
596 echo -e $BOLD"Local docker registry images used in the this test script"$EBOLD
598 docker_tmp_file=.docker-images-table
599 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}"
600 echo -e " Application\tRepository\tTag\tCreated Since\tSize" > $docker_tmp_file
601 echo -e " Policy Agent\t$(docker images --format $format_string $POLICY_AGENT_IMAGE)" >> $docker_tmp_file
602 echo -e " Control Panel\t$(docker images --format $format_string $CONTROL_PANEL_IMAGE)" >> $docker_tmp_file
603 echo -e " RIC Simulator\t$(docker images --format $format_string $RIC_SIM_IMAGE)" >> $docker_tmp_file
604 echo -e " Message Router\t$(docker images --format $format_string $MRSTUB_IMAGE)" >> $docker_tmp_file
605 echo -e " Callback Receiver\t$(docker images --format $format_string $CR_IMAGE)" >> $docker_tmp_file
606 echo -e " Consul\t$(docker images --format $format_string $CONSUL_IMAGE)" >> $docker_tmp_file
607 echo -e " CBS\t$(docker images --format $format_string $CBS_IMAGE)" >> $docker_tmp_file
608 __check_excluded_image 'SDNC'
609 if [ $? -eq 0 ]; then
610 echo -e " SDNC A1 Controller\t$(docker images --format $format_string $SDNC_A1_CONTROLLER_IMAGE)" >> $docker_tmp_file
611 echo -e " SDNC DB\t$(docker images --format $format_string $SDNC_DB_IMAGE)" >> $docker_tmp_file
613 __check_excluded_image 'SDNC_ONAP'
614 if [ $? -eq 0 ]; then
615 echo -e " SDNC ONAP A1 Adapter\t$(docker images --format $format_string $SDNC_ONAP_A1_ADAPTER_IMAGE)" >> $docker_tmp_file
616 echo -e " SDNC ONAP DB\t$(docker images --format $format_string $SDNC_ONAP_DB_IMAGE)" >> $docker_tmp_file
619 column -t -s $'\t' $docker_tmp_file
623 echo -e $BOLD"======================================================="$EBOLD
624 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
625 echo -e $BOLD"======================================================="$EBOLD
628 # Function to print the test result, shall be the last cmd in a test script
630 # (Function for test scripts)
634 duration=$((TCTEST_END-TCTEST_START))
636 echo "-------------------------------------------------------------------------------------------------"
637 echo "------------------------------------- Test case: "$ATC
638 echo "------------------------------------- Ended: "$(date)
639 echo "-------------------------------------------------------------------------------------------------"
640 echo "-- Description: "$TC_ONELINE_DESCR
641 echo "-- Execution time: " $duration " seconds"
642 echo "-------------------------------------------------------------------------------------------------"
643 echo "------------------------------------- RESULTS"
647 if [ $RES_DEVIATION -gt 0 ]; then
648 echo "Test case deviations"
649 echo "===================================="
653 echo "Timer measurement in the test script"
654 echo "===================================="
655 column -t -s $'\t' $TIMER_MEASUREMENTS
658 total=$((RES_PASS+RES_FAIL))
659 if [ $RES_TEST -eq 0 ]; then
660 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
661 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
662 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
663 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
664 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
665 elif [ $total != $RES_TEST ]; then
666 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
667 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
668 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
669 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
670 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
671 elif [ $RES_CONF_FAIL -ne 0 ]; then
672 echo -e "\033[1mOne or more configure regest has failed. Check the script log....\033[0m"
673 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
674 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
675 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
676 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
677 elif [ $RES_PASS = $RES_TEST ]; then
678 echo -e "All tests \033[32m\033[1mPASS\033[0m"
679 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
680 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
681 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
682 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
685 # Update test suite counter
686 if [ -f .tmp_tcsuite_pass_ctr ]; then
687 tmpval=$(< .tmp_tcsuite_pass_ctr)
689 echo $tmpval > .tmp_tcsuite_pass_ctr
691 if [ -f .tmp_tcsuite_pass ]; then
692 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
694 #Create file with OK exit code
695 echo "0" > "$PWD/.result$ATC.txt"
697 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
698 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
699 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
700 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
701 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
703 # Update test suite counter
704 if [ -f .tmp_tcsuite_fail_ctr ]; then
705 tmpval=$(< .tmp_tcsuite_fail_ctr)
707 echo $tmpval > .tmp_tcsuite_fail_ctr
709 if [ -f .tmp_tcsuite_fail ]; then
710 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
714 echo "++++ Number of tests: "$RES_TEST
715 echo "++++ Number of passed tests: "$RES_PASS
716 echo "++++ Number of failed tests: "$RES_FAIL
718 echo "++++ Number of failed configs: "$RES_CONF_FAIL
720 echo "++++ Number of test case deviations: "$RES_DEVIATION
722 echo "------------------------------------- Test case complete ---------------------------------"
723 echo "-------------------------------------------------------------------------------------------------"
727 #####################################################################
728 ###### Functions for start, configuring, stoping, cleaning etc ######
729 #####################################################################
731 # Start timer for time measurement
732 # args - (any args will be printed though)
734 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
736 echo " Timer started"
739 # Print the value of the time (in seconds)
740 # args - <timer message to print> - timer value and message will be printed both on screen
741 # and in the timer measurement report
743 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
744 if [ $# -lt 1 ]; then
746 __print_err "need 1 or more args, <timer message to print>" $@
749 duration=$(($SECONDS-$TC_TIMER))
750 if [ $duration -eq 0 ]; then
753 duration=$duration" seconds"
755 echo " Timer duration :" $duration
757 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
760 # Print the value of the time (in seconds) and reset the timer
761 # args - <timer message to print> - timer value and message will be printed both on screen
762 # and in the timer measurement report
763 print_and_reset_timer() {
764 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
765 if [ $# -lt 1 ]; then
767 __print_err "need 1 or more args, <timer message to print>" $@
770 duration=$(($SECONDS-$TC_TIMER))" seconds"
771 if [ $duration -eq 0 ]; then
774 duration=$duration" seconds"
776 echo " Timer duration :" $duration
780 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
783 # Print info about a deviations from intended tests
784 # Each deviation counted is also printed in the testreport
785 # args <deviation message to print>
787 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
788 if [ $# -lt 1 ]; then
790 __print_err "need 1 or more args, <deviation message to print>" $@
794 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
795 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
799 # Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
800 __check_stop_at_error() {
801 if [ $STOP_AT_ERROR -eq 1 ]; then
802 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
803 store_logs "STOP_AT_ERROR"
809 # Stop and remove all containers
811 # (Function for test scripts)
814 echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
816 CONTAINTER_NAMES=("Policy Agent " $POLICY_AGENT_APP_NAME\
817 "Non-RT RIC Simulator(s)" $RIC_SIM_PREFIX\
818 "Message Router " $MR_APP_NAME\
819 "Callback Receiver " $CR_APP_NAME\
820 "Control Panel " $CONTROL_PANEL_APP_NAME\
821 "SDNC A1 Controller " $SDNC_APP_NAME\
822 "SDNC DB " $SDNC_DB_APP_NAME\
823 "SDNC ONAP A1 Adapter " $SDNC_ONAP_APP_NAME\
824 "SDNC DB " $SDNC_ONAP_DB_APP_NAME\
825 "CBS " $CBS_APP_NAME\
826 "Consul " $CONSUL_APP_NAME)
828 nw=0 # Calc max width of container name, to make a nice table
829 for (( i=1; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
830 if [ ${#CONTAINTER_NAMES[i]} -gt $nw ]; then
831 nw=${#CONTAINTER_NAMES[i]}
835 for (( i=0; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
836 APP="${CONTAINTER_NAMES[i]}"
837 CONTR="${CONTAINTER_NAMES[i+1]}"
838 for((w=${#CONTR}; w<$nw; w=w+1)); do
841 echo -ne " $APP: $CONTR - ${GREEN}stopping${EGREEN}${SAMELINE}"
842 docker stop $(docker ps -qa --filter name=${CONTR}) &> /dev/null
843 echo -ne " $APP: $CONTR - ${GREEN}stopped${EGREEN}${SAMELINE}"
844 docker rm --force $(docker ps -qa --filter name=${CONTR}) &> /dev/null
845 echo -e " $APP: $CONTR - ${GREEN}stopped removed${EGREEN}"
850 echo -e $BOLD" Removing docker network"$EBOLD
851 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
852 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
853 docker network rm $DOCKER_SIM_NWNAME
854 if [ $? -ne 0 ]; then
855 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
860 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
861 docker network prune --force #&> /dev/null
863 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
864 docker volume prune --force #&> /dev/null
866 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
867 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
870 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
871 if [ $? -eq 0 ]; then
872 if [ $CONTRS -ne 0 ]; then
873 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
879 # Function stop and remove all container in the end of the test script, if the arg 'auto-clean' is given at test script start
881 # (Function for test scripts)
882 auto_clean_containers() {
884 if [ "$AUTO_CLEAN" == "auto" ]; then
885 echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
890 # Function to sleep a test case for a numner of seconds. Prints the optional text args as info
891 # args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
892 # (Function for test scripts)
895 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
896 if [ $# -lt 1 ]; then
898 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
901 #echo "---- Sleep for " $1 " seconds ---- "$2
903 duration=$((SECONDS-start))
904 while [ $duration -lt $1 ]; do
905 echo -ne " Slept for ${duration} seconds${SAMELINE}"
907 duration=$((SECONDS-start))
909 echo -ne " Slept for ${duration} seconds${SAMELINE}"
913 # Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
914 # Not to be called from the test script itself.
916 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
917 if [ $# -gt 1 ]; then
918 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
923 # Helper function to get a the port of a specific ric simulatpor
925 # (Not for test scripts)
927 name=$1" " #Space appended to prevent matching 10 if 1 is desired....
928 cmdstr="docker inspect --format='{{(index (index .NetworkSettings.Ports \"$RIC_SIM_PORT/tcp\") 0).HostPort}}' ${name}"
930 if [[ "$res" =~ ^[0-9]+$ ]]; then
937 # Function to create the docker network for the test
938 # Not to be called from the test script itself.
939 __create_docker_network() {
940 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
941 if [ $? -ne 0 ]; then
942 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
945 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
946 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
947 docker network create $DOCKER_SIM_NWNAME
948 if [ $? -ne 0 ]; then
949 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
953 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
957 # Check if container is started by calling url on localhost using a port, expects response code 2XX
958 # args: <container-name> <port> <url> https|https
959 # Not to be called from the test script itself.
960 __check_container_start() {
962 if [ $# -ne 4 ]; then
964 elif [ $4 != "http" ] && [ $4 != "https" ]; then
967 if [ $paramError -ne 0 ]; then
969 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
972 echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
976 if [[ $appname != "STANDALONE_"* ]] ; then
979 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
980 echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
987 if [ $app_started -eq 0 ]; then
990 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
993 if [ $localport -eq 0 ]; then
994 while [ $localport -eq 0 ]; do
995 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
996 localport=$(__find_sim_port $appname)
998 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
1000 echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
1006 echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
1008 for i in {1..50}; do
1009 if [ $4 == "https" ]; then
1010 result="$(__do_curl "-k https://localhost:"${localport}${url})"
1012 result="$(__do_curl $LOCALHOST${localport}${url})"
1014 if [ $? -eq 0 ]; then
1015 if [ ${#result} -gt 15 ]; then
1016 #If response is too long, truncate
1017 result="...response text too long, omitted"
1019 echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
1020 echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN after $(($SECONDS-$TSTART)) seconds"
1025 while [ $(($TS_TMP+$i)) -gt $SECONDS ]; do
1026 echo -ne " Waiting for container ${appname} service status...retrying in $(($TS_TMP+$i-$SECONDS)) seconds ${SAMELINE}"
1032 if [ "$pa_st" = "false" ]; then
1034 echo -e $RED" Container ${appname} did not respond to service status"$ERED
1043 # Function to start a container and wait until it responds on the given port and url.
1044 #args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
1045 __start_container() {
1047 variableArgCount=$(($#-2))
1048 if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
1050 __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 ]*" $@
1054 __create_docker_network
1060 if [ "$2" == "NODOCKERARGS" ]; then
1061 docker-compose up -d &> .dockererr
1062 if [ $? -ne 0 ]; then
1063 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
1066 elif [ "$2" == "STANDALONE" ]; then
1067 echo "Skipping docker-compose"
1069 docker-compose up -d $2 &> .dockererr
1070 if [ $? -ne 0 ]; then
1071 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
1076 if [ "$2" == "STANDALONE" ]; then
1077 app_prefix="STANDALONE_"
1081 while [ $cntr -lt $variableArgCount ]; do
1082 app=$app_prefix$1; shift;
1088 __check_container_start "$app" "$port" "$url" $httpx
1096 # Generate a UUID to use as prefix for policy ids
1098 UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)')
1099 #Reduce length to make space for serial id, us 'a' as marker where the serial id is added
1100 UUID=${UUID:0:${#UUID}-4}"a"
1103 ####################
1104 ### Consul functions
1105 ####################
1107 # Function to load config from a file into consul for the Policy Agent
1108 # arg: <json-config-file>
1109 # (Function for test scripts)
1110 consul_config_app() {
1112 echo -e $BOLD"Configuring Consul"$EBOLD
1114 if [ $# -ne 1 ]; then
1116 __print_err "need one arg, <json-config-file>" $@
1120 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
1122 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
1123 if [ $? -ne 0 ]; then
1124 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
1128 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1129 echo $body > ".output"$1
1131 if [ $? -ne 0 ]; then
1132 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
1137 targetJson="{\"config\":"$targetJson"}"
1138 echo "TARGET JSON: $targetJson" >> $HTTPLOG
1139 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
1140 if [ $res -ne 0 ]; then
1141 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
1145 echo -e $GREEN" Config loaded ok to consul"$EGREEN
1153 # Function to perpare the consul configuration according to the current simulator configuration
1154 # args: SDNC|SDNC_ONAP|NOSDNC <output-file>
1155 # (Function for test scripts)
1156 prepare_consul_config() {
1157 echo -e $BOLD"Prepare Consul config"$EBOLD
1159 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
1161 if [ $# != 2 ]; then
1163 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
1167 if [ $1 == "SDNC" ]; then
1168 echo -e " Config$BOLD including SDNC$EBOLD configuration"
1169 elif [ $1 == "SDNC_ONAP" ]; then
1170 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
1171 elif [ $1 == "NOSDNC" ]; then
1172 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
1175 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
1180 if [ $1 == "SDNC" ]; then
1181 config_json=$config_json"\n \"controller\": ["
1182 config_json=$config_json"\n {"
1183 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
1184 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1185 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://$SDNC_APP_NAME:$SDNC_PORT\","
1187 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://localhost:$SDNC_LOCAL_PORT\","
1189 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
1190 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
1191 config_json=$config_json"\n }"
1192 config_json=$config_json"\n ],"
1194 if [ $1 == "SDNC_ONAP" ]; then
1195 config_json=$config_json"\n \"controller\": ["
1196 config_json=$config_json"\n {"
1197 config_json=$config_json"\n \"name\": \"$SDNC_ONAP_APP_NAME\","
1198 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1199 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
1201 config_json=$config_json"\n \"baseUrl\": \"http://localhost:$SDNC_ONAP_EXTERNAL_PORT\","
1203 config_json=$config_json"\n \"userName\": \"$SDNC_ONAP_USER\","
1204 config_json=$config_json"\n \"password\": \"$SDNC_ONAP_PWD\""
1205 config_json=$config_json"\n }"
1206 config_json=$config_json"\n ],"
1210 config_json=$config_json"\n \"streams_publishes\": {"
1211 config_json=$config_json"\n \"dmaap_publisher\": {"
1212 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1213 config_json=$config_json"\n \"dmaap_info\": {"
1214 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1215 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_WRITE_URL\""
1217 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_WRITE_URL\""
1219 config_json=$config_json"\n }"
1220 config_json=$config_json"\n }"
1221 config_json=$config_json"\n },"
1222 config_json=$config_json"\n \"streams_subscribes\": {"
1223 config_json=$config_json"\n \"dmaap_subscriber\": {"
1224 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1225 config_json=$config_json"\n \"dmaap_info\": {"
1226 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1227 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_READ_URL\""
1229 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_READ_URL\""
1231 config_json=$config_json"\n }"
1232 config_json=$config_json"\n }"
1233 config_json=$config_json"\n },"
1235 config_json=$config_json"\n \"ric\": ["
1237 rics=$(docker ps | grep $RIC_SIM_PREFIX | awk '{print $NF}')
1239 if [ $? -ne 0 ] || [ -z "$rics" ]; then
1240 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
1246 for ric in $rics; do
1247 if [ $cntr -gt 0 ]; then
1248 config_json=$config_json"\n ,"
1250 config_json=$config_json"\n {"
1251 config_json=$config_json"\n \"name\": \"$ric\","
1252 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1253 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
1255 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://localhost:$(__find_sim_port $ric)\","
1257 if [ $1 == "SDNC" ]; then
1258 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
1259 elif [ $1 == "SDNC_ONAP" ]; then
1260 config_json=$config_json"\n \"controller\": \"$SDNC_ONAP_APP_NAME\","
1262 config_json=$config_json"\n \"managedElementIds\": ["
1263 config_json=$config_json"\n \"me1_$ric\","
1264 config_json=$config_json"\n \"me2_$ric\""
1265 config_json=$config_json"\n ]"
1266 config_json=$config_json"\n }"
1270 config_json=$config_json"\n ]"
1271 config_json=$config_json"\n}"
1274 printf "$config_json">$2
1280 # Start Consul and CBS
1282 # (Function for test scripts)
1283 start_consul_cbs() {
1285 echo -e $BOLD"Starting Consul and CBS"$EBOLD
1287 __start_container consul_cbs NODOCKERARGS "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
1288 "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
1291 ###########################
1292 ### RIC Simulator functions
1293 ###########################
1295 use_simulator_http() {
1296 echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
1297 export RIC_SIM_HTTPX="http"
1298 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1299 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
1303 use_simulator_https() {
1304 echo -e "Using secure $BOLD https $EBOLD towards the simulators"
1305 export RIC_SIM_HTTPX="https"
1306 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1307 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
1311 # Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
1312 # 'ricsim' may be set on command line to other prefix
1313 # args: ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
1314 # (Function for test scripts)
1315 start_ric_simulators() {
1317 echo -e $BOLD"Starting RIC Simulators"$EBOLD
1319 RIC1=$RIC_SIM_PREFIX"_g1"
1320 RIC2=$RIC_SIM_PREFIX"_g2"
1321 RIC3=$RIC_SIM_PREFIX"_g3"
1323 if [ $# != 3 ]; then
1325 __print_err "need three args, $RIC1|$RIC2|$RIC3 <count> <interface-id>" $@
1328 echo " $2 simulators using basename: $1 on interface: $3"
1329 #Set env var for simulator count and A1 interface vesion for the given group
1330 if [ $1 == "$RIC1" ]; then
1333 elif [ $1 == "$RIC2" ]; then
1336 elif [ $1 == "$RIC3" ]; then
1341 __print_err "need three args, $RIC1|$RIC2|$RIC3 <count> <interface-id>" $@
1345 # Create .env file to compose project, all ric container will get this prefix
1346 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
1348 export G1_A1_VERSION
1349 export G2_A1_VERSION
1350 export G3_A1_VERSION
1352 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1355 while [ $cntr -le $2 ]; do
1358 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
1361 __start_container ric "$docker_args" $app_data
1365 ###########################
1366 ### Control Panel functions
1367 ###########################
1369 # Start the Control Panel container
1371 # (Function for test scripts)
1372 start_control_panel() {
1374 echo -e $BOLD"Starting Control Panel"$EBOLD
1376 __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
1384 # Start the SDNC A1 Controller
1386 # (Function for test scripts)
1389 echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1391 __check_excluded_image 'SDNC'
1392 if [ $? -eq 1 ]; then
1393 echo -e $RED"The image for SDNC and the related DB has not been checked for this test run due to arg to the test script"$ERED
1394 echo -e $RED"SDNC will not be started"$ERED
1398 __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT $SDNC_ALIVE_URL "http"
1403 echo -e $BOLD"Using http between agent and SDNC"$EBOLD
1404 export SDNC_HTTPX="http"
1405 export SDNC_PORT=$SDNC_INTERNAL_PORT
1406 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT
1411 echo -e $BOLD"Using https between agent and SDNC"$EBOLD
1412 export SDNC_HTTPX="https"
1413 export SDNC_PORT=$SDNC_INTERNAL_SECURE_PORT
1414 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_SECURE_PORT
1418 #######################
1419 ### SDNC ONAP functions
1420 #######################
1422 # Start the SDNC ONAP A1 Adapter
1424 # (Function for test scripts)
1427 echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1429 __check_excluded_image 'SDNC_ONAP'
1430 if [ $? -eq 1 ]; then
1431 echo -e $RED"The image for SDNC ONAP and the related DB has not been checked for this test run due to arg to the test script"$ERED
1432 echo -e $RED"SDNC ONAP will not be started"$ERED
1436 __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT $SDNC_ONAP_ALIVE_URL "http"
1440 # Configure the SDNC ONAP A1 Adapter
1442 # (Function for test scripts)
1443 config_sdnc_onap() {
1445 echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1447 LOCALFILE=".sdnc_onap.prop"
1448 REMOTEFILE="/tmp/.sdnc_onap.prop"
1450 docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1451 if [ $? -ne 0 ]; then
1452 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1457 #Config of the prop file shall be inserted here
1459 #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1461 docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1462 if [ $? -ne 0 ]; then
1463 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1467 docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1468 if [ $? -ne 0 ]; then
1469 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1474 #####################
1475 ### MR stub functions
1476 #####################
1478 # Start the Message Router stub interface in the simulator group
1480 # (Function for test scripts)
1483 echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
1484 export MR_CERT_MOUNT_DIR="./cert"
1485 __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
1489 echo -e $BOLD"Using http between agent and MR"$EBOLD
1490 export MR_HTTPX="http"
1491 export MR_PORT=$MR_INTERNAL_PORT
1492 export MR_LOCAL_PORT=$MR_EXTERNAL_PORT
1497 echo -e $BOLD"Using https between agent and MR"$EBOLD
1498 export MR_HTTPX="https"
1499 export MR_PORT=$MR_INTERNAL_SECURE_PORT
1500 export MR_LOCAL_PORT=$MR_EXTERNAL_SECURE_PORT
1509 # Start the Callback reciver in the simulator group
1511 # (Function for test scripts)
1514 echo -e $BOLD"Starting Callback Receiver"$EBOLD
1516 __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
1520 ###########################
1521 ### Policy Agents functions
1522 ###########################
1524 # Use an agent on the local machine instead of container
1525 use_agent_stand_alone() {
1529 # Start the policy agent
1531 # (Function for test scripts)
1532 start_policy_agent() {
1534 echo -e $BOLD"Starting Policy Agent"$EBOLD
1536 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1537 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1539 echo -e $RED"The consul config produced by this test script (filename '<fullpath-to-autotest-dir>.output<file-name>"$ERED
1540 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
1541 echo -e $RED"application.yaml"$ERED
1542 echo -e $RED"The application jar may need to be built beforefor continuing"$ERED
1543 echo -e $RED"The agent shall now be running on port $POLICY_AGENT_EXTERNAL_PORT for http"$ERED
1545 read -p "<press any key to continue>"
1546 __start_container policy_agent "STANDALONE" $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1551 # All calls to the agent will be directed to the agent REST interface from now on
1553 # (Function for test scripts)
1554 use_agent_rest_http() {
1555 echo -e $BOLD"Using agent REST interface with http"$EBOLD
1556 export ADAPTER=$RESTBASE
1560 # All calls to the agent will be directed to the agent REST interface from now on
1562 # (Function for test scripts)
1563 use_agent_rest_https() {
1564 echo -e $BOLD"Using agent REST interface with https"$EBOLD
1565 export ADAPTER=$RESTBASE_SECURE
1570 # All calls to the agent will be directed to the agent dmaap interface from now on
1572 # (Function for test scripts)
1574 echo -e $BOLD"Agent using DMAAP interface"$EBOLD
1575 export ADAPTER=$DMAAPBASE
1581 # Turn on debug level tracing in the agent
1583 # (Function for test scripts)
1585 echo -e $BOLD"Setting agent debug"$EBOLD
1586 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1587 if [ $? -ne 0 ]; then
1588 __print_err "could not set debug mode" $@
1595 # Turn on trace level tracing in the agent
1597 # (Function for test scripts)
1599 echo -e $BOLD"Setting agent trace"$EBOLD
1600 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"trace"}' &> /dev/null
1601 if [ $? -ne 0 ]; then
1602 __print_err "could not set trace mode" $@
1609 # Perform curl retries when making direct call to the agent for the specified http response codes
1610 # Speace separated list of http response codes
1611 # args: [<response-code>]*
1612 use_agent_retries() {
1613 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1614 AGENT_RETRY_CODES=$@
1623 # Check the agent logs for WARNINGs and ERRORs
1625 # (Function for test scripts)
1627 check_policy_agent_logs() {
1628 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
1631 check_control_panel_logs() {
1632 __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
1635 __check_container_logs() {
1639 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1641 #tmp=$(docker ps | grep $appname)
1642 tmp=$(docker ps -q --filter name=$appname) #get the container id
1643 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
1644 echo $dispname" is not running, no check made"
1647 foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1648 if [ $? -ne 0 ];then
1649 echo " Problem to search $appname log $logpath"
1651 if [ $foundentries -eq 0 ]; then
1652 echo " No WARN entries found in $appname log $logpath"
1654 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1657 foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1658 if [ $? -ne 0 ];then
1659 echo " Problem to search $appname log $logpath"
1661 if [ $foundentries -eq 0 ]; then
1662 echo " No ERR entries found in $appname log $logpath"
1664 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1670 # Store all container logs and other logs in the log dir for the script
1671 # Logs are stored with a prefix in case logs should be stored several times during a test
1672 # args: <logfile-prefix>
1673 # (Function for test scripts)
1675 if [ $# != 1 ]; then
1677 __print_err "need one arg, <file-prefix>" $@
1680 echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1$EBOLD
1682 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
1683 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1684 docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1685 docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1686 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1687 docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1688 docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1689 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1691 docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1693 docker exec -it $SDNC_ONAP_APP_NAME cat $SDNC_ONAP_KARAF_LOG > $TESTLOGS/$ATC/$1_SDNC_ONAP_karaf.log 2>&1
1695 rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1696 for ric in $rics; do
1697 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1699 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1700 echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1707 # Generic curl function, assumed all 200-codes are ok
1709 # returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
1710 # returns: The return code is 0 for ok and 1 for not ok
1712 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1713 curlString="curl -skw %{http_code} $1"
1714 echo " CMD: $curlString" >> $HTTPLOG
1716 echo " RESP: $res" >> $HTTPLOG
1717 http_code="${res:${#res}-3}"
1718 if [ ${#res} -eq 3 ]; then
1719 echo "<no-response-from-server>"
1722 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
1723 echo "<not found, resp:${http_code}>"
1726 if [ $# -eq 2 ]; then
1727 echo "${res:0:${#res}-3}" | xargs
1729 echo "${res:0:${#res}-3}"
1736 #######################################
1737 ### Basic helper function for test cases
1738 #######################################
1740 # Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1741 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
1742 # immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1743 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
1744 # before setting pass or fail depending on the result of comparing variable and target using the operator.
1745 # 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.
1746 # Not to be called from test script.
1749 checkjsonarraycount=0
1751 if [ $# -eq 6 ]; then
1752 if [[ $3 == "json:"* ]]; then
1753 checkjsonarraycount=1
1756 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
1761 if [ $checkjsonarraycount -eq 0 ]; then
1762 result="$(__do_curl $2$3)"
1764 result=${result//[[:blank:]]/} #Strip blanks
1767 result="$(__do_curl $2$path)"
1769 echo "$result" > .tmp.curl.json
1770 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1772 duration=$((SECONDS-start))
1773 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
1775 if [ $retcode -ne 0 ]; then
1776 if [ $duration -gt $6 ]; then
1778 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1779 __check_stop_at_error
1782 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1784 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1785 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1787 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1789 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1790 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1792 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1794 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1795 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1797 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1799 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1800 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1803 if [ $duration -gt $6 ]; then
1805 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1806 __check_stop_at_error
1812 elif [ $# -eq 5 ]; then
1813 if [[ $3 == "json:"* ]]; then
1814 checkjsonarraycount=1
1817 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1819 if [ $checkjsonarraycount -eq 0 ]; then
1820 result="$(__do_curl $2$3)"
1822 result=${result//[[:blank:]]/} #Strip blanks
1825 result="$(__do_curl $2$path)"
1827 echo "$result" > .tmp.curl.json
1828 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1830 if [ $retcode -ne 0 ]; then
1832 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
1833 __check_stop_at_error
1834 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1836 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1837 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1839 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1840 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1842 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1843 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1845 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1848 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
1849 __check_stop_at_error
1852 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1859 ### Generic test cases for varaible checking
1861 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
1862 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1863 # equal to the target or not.
1864 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1865 # before setting pass or fail depending on if the variable value becomes equal to the target
1867 # (Function for test scripts)
1869 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1870 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1873 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1877 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1878 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1879 # equal to the target or not.
1880 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1881 # before setting pass or fail depending on if the variable value becomes equal to the target
1883 # (Function for test scripts)
1885 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1886 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1889 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1893 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1894 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1895 # greater than the target or not.
1896 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1897 # before setting pass or fail depending on if the variable value becomes greater than the target
1899 # (Function for test scripts)
1901 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1902 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
1905 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1909 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1911 echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1914 # Print a variable value from the MR stub.
1915 # arg: <variable-name>
1916 # (Function for test scripts)
1918 if [ $# != 1 ]; then
1920 __print_err "need one arg, <mr-param>" $@
1923 echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD