Remove unused imports
[nonrtric.git] / test / common / testcase_common.sh
1 #!/bin/bash
2
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
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
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=================================================
18 #
19
20 # This is a script that contains all the functions needed for auto test
21 # Arg: local|remote|remote-remove [auto-clean]
22
23
24 #Formatting for 'echo' cmd
25 BOLD="\033[1m"
26 EBOLD="\033[0m"
27 RED="\033[31m\033[1m"
28 ERED="\033[0m"
29 GREEN="\033[32m\033[1m"
30 EGREEN="\033[0m"
31 YELLOW="\033[33m\033[1m"
32 EYELLOW="\033[0m"
33 SAMELINE="\033[0K\r"
34
35 tmp=$(which python3)
36 if [ $? -ne 0 ] || [ -z tmp ]; then
37         echo -e $RED"python3 is required to run the test environment, pls install"$ERED
38         exit 1
39 fi
40 tmp=$(which docker)
41 if [ $? -ne 0 ] || [ -z tmp ]; then
42         echo -e $RED"docker is required to run the test environment, pls install"$ERED
43         exit 1
44 fi
45
46 # Just resetting any previous echo formatting...
47 echo -ne $EBOLD
48
49 # source test environment variables
50 . ../common/test_env.sh
51
52 echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
53
54 #Vars for A1 interface version and container count
55 G1_A1_VERSION=""
56 G2_A1_VERSION=""
57 G3_A1_VERSION=""
58 G1_COUNT=0
59 G2_COUNT=0
60 G3_COUNT=0
61
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"
67
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
71
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
75
76 #Localhost constant
77 LOCALHOST="http://localhost:"
78
79 # Make curl retries for http response codes set in this env var, space separated list of codes
80 AGENT_RETRY_CODES=""
81
82 # Var to contol if the agent runs in a container (normal = 0) or as application on the local machine ( = 1)
83 AGENT_STAND_ALONE=0
84
85 # Var to hold 'auto' in case containers shall be stopped when test case ends
86 AUTO_CLEAN=""
87
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
92 fi
93
94 # Counter for test suites
95 if [ -f .tmp_tcsuite_ctr ]; then
96         tmpval=$(< .tmp_tcsuite_ctr)
97         ((tmpval++))
98         echo $tmpval > .tmp_tcsuite_ctr
99 fi
100
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)
104
105 # Create the logs dir if not already created in the current dir
106 if [ ! -d "logs" ]; then
107     mkdir logs
108 fi
109 TESTLOGS=$PWD/logs
110
111 # Create a http message log for this testcase
112 HTTPLOG=$PWD"/.httplog_"$ATC".txt"
113 echo "" > $HTTPLOG
114
115 # Create a log dir for the test case
116 mkdir -p $TESTLOGS/$ATC
117
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
122
123 # Log all output from the test case to a TC log
124 TCLOG=$TESTLOGS/$ATC/TC.log
125 exec &>  >(tee ${TCLOG})
126
127 #Variables for counting tests as well as passed and failed tests
128 RES_TEST=0
129 RES_PASS=0
130 RES_FAIL=0
131 RES_CONF_FAIL=0
132 RES_DEVIATION=0
133
134 #File to keep deviation messages
135 DEVIATION_FILE=".tmp_deviations"
136 rm $DEVIATION_FILE &> /dev/null
137
138 #Var for measuring execution time
139 TCTEST_START=$SECONDS
140
141 #File to save timer measurement results
142 TIMER_MEASUREMENTS=".timer_measurement.txt"
143 echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
144
145
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      -----------------------------------"
153
154 echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
155
156 #Temp var to check for image variable name errors
157 IMAGE_ERR=0
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
161
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>"
167                 ((IMAGE_ERR++))
168                 return
169         fi
170         tmp=${1}"\t"
171         #Create var from the input var names
172         image="${!4}"
173         tag="${!5}"
174
175         if [ -z $image ]; then
176                 echo -e $RED"\$"$4" not set in test_env"$ERED
177                 ((IMAGE_ERR++))
178                 echo ""
179                 tmp=$tmp"<no-image>\t"
180         else
181                 tmp=$tmp$image"\t"
182         fi
183         if [ -z $tag ]; then
184                 echo -e $RED"\$"$5" not set in test_env"$ERED
185                 ((IMAGE_ERR++))
186                 echo ""
187                 tmp=$tmp"<no-tag>\t"
188         else
189                 tmp=$tmp$tag
190         fi
191         echo -e "$tmp" >> $image_list_file
192         #Export the env var
193         export "${3}"=$image":"$tag
194
195         #echo " Configured image for ${1} (script start arg=${2}): "$image":"$tag
196 }
197
198 # Check that image env setting are available
199 echo ""
200 if [ $# -lt 1 ] || [ $# -gt 2 ]; then
201         echo "Expected arg: local|remote|remote-remove [auto-clean]"
202         exit 1
203 elif [ $1 == "local" ]; then
204
205         #Local agent image
206         __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
207
208         #Local Control Panel image
209         __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
210
211         #Local SNDC image
212         __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
213
214         #Local ric sim image
215         __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
216
217 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
218
219         #Remote agent image
220         __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_REMOTE_IMAGE" "POLICY_AGENT_REMOTE_IMAGE_TAG"
221
222         #Remote Control Panel image
223         __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE_TAG"
224
225         #Remote SDNC image
226         __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE_TAG"
227
228         #Remote ric sim image
229         __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_REMOTE_IMAGE" "RIC_SIM_REMOTE_IMAGE_TAG"
230
231 else
232         echo "Expected arg: local|remote|remote-remove [auto-clean]"
233         exit 1
234 fi
235
236 if [ $# -eq 2 ]; then
237         if [ $2 == "auto-clean" ]; then
238                 echo "Stting automatic cleaning of container when test case ends"
239                 AUTO_CLEAN="auto"
240         else
241                 echo "Expected arg: local|remote|remote-remove [auto-clean]"
242                 exit 1
243         fi
244 fi
245
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"
254
255 #Errors in image setting - exit
256 if [ $IMAGE_ERR -ne 0 ]; then
257         exit 1
258 fi
259
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
263
264 echo ""
265
266
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
274                 exit 1
275         else
276                 echo " SIM_GROUP auto set to: " $SIM_GROUP
277         fi
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
280         exit 1
281 else
282         echo " SIM_GROUP env var already set to: " $SIM_GROUP
283 fi
284
285 echo ""
286
287 #Temp var to check for image pull errors
288 IMAGE_ERR=0
289
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() {
293
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})
297
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
301                         ((IMAGE_ERR++))
302                         return 1
303                 else
304                         echo -e "  "$2" (local image): \033[1m"$4"\033[0m "$GREEN"OK"$EGREEN
305                 fi
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
313                                         ((IMAGE_ERR++))
314                                         echo ""
315                                         echo -e $RED"  Container(s) could not be stopped - try manual stopping the container(s)"$ERED
316                                         cat .dockererr
317                                         return 1
318                                 fi
319                         fi
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
325                                         ((IMAGE_ERR++))
326                                         echo ""
327                                         echo -e $RED"  Container(s) could not be removed - try manual removal of the container(s)"$ERED
328                                         cat .dockererr
329                                         return 1
330                                 fi
331                         fi
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
338                                         ((IMAGE_ERR++))
339                                         echo ""
340                                         echo -e $RED"  Image could not be removed - try manual removal of the image"$ERED
341                                         cat .dockererr
342                                         return 1
343                                 fi
344                                 echo -e "  Removing image - "$GREEN"removed"$EGREEN
345                         else
346                                 echo -e "  Removing image - "$GREEN"image not in repository"$EGREEN
347                         fi
348                         tmp_im=""
349                 fi
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
355                                 echo ""
356                                 echo -e "  Pulling image -$RED could not be pulled"$ERED
357                                 ((IMAGE_ERR++))
358                                 cat .dockererr
359                                 return 1
360                         fi
361                         echo -e "  Pulling image -$GREEN Pulled $EGREEN"
362                 else
363                         echo -e "  Pulling image -$GREEN OK $EGREEN(exists in local repository)"
364                 fi
365         fi
366         return 0
367 }
368
369
370 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
371
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
376
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
380
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
384
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
387
388
389 #Errors in image setting - exit
390 if [ $IMAGE_ERR -ne 0 ]; then
391         echo ""
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 "#################################################################################################"
395         echo ""
396         exit 1
397 fi
398
399 echo ""
400
401 echo -e $BOLD"Building images needed for test"$EBOLD
402
403 curdir=$PWD
404 cd $curdir
405 cd ../mrstub
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
410 else
411         echo -e $RED" Build Failed"$ERED
412         ((RES_CONF_FAIL++))
413         cat .dockererr
414 fi
415 cd $curdir
416
417 cd ../cr
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
422 else
423         echo -e $RED" Build Failed"$ERED
424         ((RES_CONF_FAIL++))
425         cat .dockererr
426 fi
427 cd $curdir
428
429 echo ""
430
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
433
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
448
449 column -t -s $'\t' $docker_tmp_file
450
451 echo ""
452
453 echo -e $BOLD"======================================================="$EBOLD
454 echo -e $BOLD"== Common test setup completed -  test script begins =="$EBOLD
455 echo -e $BOLD"======================================================="$EBOLD
456 echo ""
457
458 # Function to print the test result, shall be the last cmd in a test script
459 # args: -
460 # (Function for test scripts)
461 print_result() {
462
463         TCTEST_END=$SECONDS
464         duration=$((TCTEST_END-TCTEST_START))
465
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"
474         echo ""
475
476
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"
502                 echo ""
503
504                 # Update test suite counter
505                 if [ -f .tmp_tcsuite_pass_ctr ]; then
506                         tmpval=$(< .tmp_tcsuite_pass_ctr)
507                         ((tmpval++))
508                         echo $tmpval > .tmp_tcsuite_pass_ctr
509                 fi
510                 if [ -f .tmp_tcsuite_pass ]; then
511                         echo " - "$ATC " -- "$TC_ONELINE_DESCR"  Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
512                 fi
513         else
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"
519                 echo ""
520                 # Update test suite counter
521                 if [ -f .tmp_tcsuite_fail_ctr ]; then
522                         tmpval=$(< .tmp_tcsuite_fail_ctr)
523                         ((tmpval++))
524                         echo $tmpval > .tmp_tcsuite_fail_ctr
525                 fi
526                 if [ -f .tmp_tcsuite_fail ]; then
527                         echo " - "$ATC " -- "$TC_ONELINE_DESCR"  Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
528                 fi
529         fi
530
531         if [ $RES_DEVIATION -gt 0 ]; then
532                 echo "Test case deviations"
533                 echo "===================================="
534                 cat $DEVIATION_FILE
535         fi
536         echo ""
537         echo "Timer measurement in the test script"
538         echo "===================================="
539         column -t -s $'\t' $TIMER_MEASUREMENTS
540         echo ""
541
542         echo "++++ Number of tests:          "$RES_TEST
543         echo "++++ Number of passed tests:   "$RES_PASS
544         echo "++++ Number of failed tests:   "$RES_FAIL
545         echo ""
546         echo "++++ Number of failed configs: "$RES_CONF_FAIL
547         echo ""
548         echo "++++ Number of test case deviations: "$RES_DEVIATION
549         echo ""
550         echo "-------------------------------------     Test case complete    ---------------------------------"
551         echo "-------------------------------------------------------------------------------------------------"
552         echo ""
553 }
554
555 #####################################################################
556 ###### Functions for start, configuring, stoping, cleaning etc ######
557 #####################################################################
558
559 # Start timer for time measurement
560 # args - (any args will be printed though)
561 start_timer() {
562         echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
563         TC_TIMER=$SECONDS
564         echo " Timer started"
565 }
566
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
570 print_timer() {
571         echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
572         if [ $# -lt 1 ]; then
573                 ((RES_CONF_FAIL++))
574         __print_err "need 1 or more args,  <timer message to print>" $@
575                 exit 1
576         fi
577         duration=$(($SECONDS-$TC_TIMER))
578         if [ $duration -eq 0 ]; then
579                 duration="<1 second"
580         else
581                 duration=$duration" seconds"
582         fi
583         echo " Timer duration :" $duration
584
585         echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
586 }
587
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
594                 ((RES_CONF_FAIL++))
595         __print_err "need 1 or more args,  <timer message to print>" $@
596                 exit 1
597         fi
598         duration=$(($SECONDS-$TC_TIMER))" seconds"
599         if [ $duration -eq 0 ]; then
600                 duration="<1 second"
601         else
602                 duration=$duration" seconds"
603         fi
604         echo " Timer duration :" $duration
605         TC_TIMER=$SECONDS
606         echo " Timer reset"
607
608         echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
609
610 }
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>
614 deviation() {
615         echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
616         if [ $# -lt 1 ]; then
617                 ((RES_CONF_FAIL++))
618                 __print_err "need 1 or more args,  <deviation message to print>" $@
619                 exit 1
620         fi
621         ((RES_DEVIATION++))
622         echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
623         echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
624         echo ""
625 }
626
627 # Stop and remove all containers
628 # args: -
629 # (Function for test scripts)
630 clean_containers() {
631
632         echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
633
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)
645
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]}
650                 fi
651         done
652
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
657                         CONTR="$CONTR "
658                 done
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}"
664         done
665
666         echo ""
667 }
668
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
670 # args: -
671 # (Function for test scripts)
672 auto_clean_containers() {
673         echo
674         if [ "$AUTO_CLEAN" == "auto" ]; then
675                 echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
676                 clean_containers
677         fi
678 }
679
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)
683 sleep_wait() {
684
685         echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
686         if [ $# -lt 1 ]; then
687                 ((RES_CONF_FAIL++))
688                 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
689                 exit 1
690         fi
691         #echo "---- Sleep for " $1 " seconds ---- "$2
692         start=$SECONDS
693         duration=$((SECONDS-start))
694         while [ $duration -lt $1 ]; do
695                 echo -ne "  Slept for ${duration} seconds${SAMELINE}"
696                 sleep 1
697                 duration=$((SECONDS-start))
698         done
699         echo -ne "  Slept for ${duration} seconds${SAMELINE}"
700         echo ""
701 }
702
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.
705 __print_err() {
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
709         fi
710 }
711
712
713 # Helper function to get a the port of a specific ric simulatpor
714 # args: <ric-id>
715 # (Not for test scripts)
716 __find_sim_port() {
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}"
719     res=$(eval $cmdstr)
720         if [[ "$res" =~ ^[0-9]+$ ]]; then
721                 echo $res
722         else
723                 echo "0"
724     fi
725 }
726
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
733                 return 1
734         fi
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
740                         return 1
741                 fi
742         else
743                 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
744         fi
745 }
746
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() {
751         paramError=0
752         if [ $# -ne 4 ]; then
753                 paramError=1
754         elif [ $4 != "http" ] && [ $4 != "https" ]; then
755                 paramError=1
756         fi
757         if [ $paramError -ne 0 ]; then
758                 ((RES_CONF_FAIL++))
759                 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
760                 return 1
761         fi
762         echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
763         appname=$1
764         localport=$2
765         url=$3
766         if [[ $appname != "STANDALONE_"* ]]     ; then
767                 app_started=0
768                 for i in {1..10}; do
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"
771                                         app_started=1
772                                         break
773                                 else
774                                         sleep $i
775                         fi
776                 done
777                 if [ $app_started -eq 0 ]; then
778                         ((RES_CONF_FAIL++))
779                         echo ""
780                         echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
781                         return 1
782                 fi
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)
787                                 sleep 1
788                                 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
789                         done
790                         echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
791                         echo ""
792                 fi
793         fi
794
795         pa_st=false
796         echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
797         for i in {1..20}; do
798                 if [ $4 == "https" ]; then
799                         result="$(__do_curl "-k https://localhost:"${localport}${url})"
800                 else
801                         result="$(__do_curl $LOCALHOST${localport}${url})"
802                 fi
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"
807                         fi
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"
810                         pa_st=true
811                         break
812                 else
813                         #echo " Retrying in $i seconds"
814                         echo -ne " Waiting for container ${appname} service status...retrying in $i seconds${SAMELINE}"
815                         sleep $i
816                 fi
817         done
818
819         if [ "$pa_st" = "false"  ]; then
820                 ((RES_CONF_FAIL++))
821                 echo -e $RED" Container ${appname} did not respond to service status"$ERED
822                 return 0
823         fi
824
825         echo ""
826         return 0
827 }
828
829
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() {
833
834         variableArgCount=$(($#-2))
835         if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
836                 ((RES_CONF_FAIL++))
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 ]*" $@
838                 exit 1
839         fi
840
841         __create_docker_network
842
843         curdir=$PWD
844         cd $SIM_GROUP
845         cd $1
846
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
851                         cat .dockererr
852                 fi
853         elif [ "$2" == "STANDALONE" ]; then
854                 echo "Skipping docker-compose"
855         else
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
859                         cat .dockererr
860                 fi
861         fi
862         app_prefix=""
863         if [ "$2" == "STANDALONE" ]; then
864                 app_prefix="STANDALONE_"
865         fi
866         shift; shift;
867         cntr=0
868         while [ $cntr -lt $variableArgCount ]; do
869                 app=$app_prefix$1; shift;
870                 port=$1; shift;
871                 url=$1; shift;
872                 httpx=$1; shift;
873                 let cntr=cntr+4
874
875                 __check_container_start "$app" "$port" "$url" $httpx
876         done
877
878         cd $curdir
879         echo ""
880         return 0
881 }
882
883 ####################
884 ### Consul functions
885 ####################
886
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() {
891
892         echo -e $BOLD"Configuring Consul"$EBOLD
893
894         if [ $# -ne 1 ]; then
895                 ((RES_CONF_FAIL++))
896         __print_err "need one arg,  <json-config-file>" $@
897                 exit 1
898         fi
899
900         echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
901
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
905                 ((RES_CONF_FAIL++))
906                 return 1
907         fi
908         body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
909         echo $body > ".output"$1
910
911         if [ $? -ne 0 ]; then
912                 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
913                 ((RES_CONF_FAIL++))
914                 return 1
915         else
916                 targetJson=$(< $1)
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
922                         ((RES_CONF_FAIL++))
923                         return 1
924                 else
925                         echo -e $GREEN" Config loaded ok to consul"$EGREEN
926                 fi
927         fi
928
929         echo ""
930
931 }
932
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
938
939         echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
940
941         if [ $# != 2 ];  then
942                 ((RES_CONF_FAIL++))
943         __print_err "need two args,  SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
944                 exit 1
945         fi
946
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"
953         else
954                 ((RES_CONF_FAIL++))
955         __print_err "need two args,  SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
956                 exit 1
957         fi
958
959         config_json="\n            {"
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\","
966                 else
967                         config_json=$config_json"\n                       \"baseUrl\": \"$SDNC_HTTPX://localhost:$SDNC_LOCAL_PORT\","
968                 fi
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   ],"
973         fi
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\","
980                 else
981                         config_json=$config_json"\n                       \"baseUrl\": \"http://localhost:$SDNC_ONAP_EXTERNAL_PORT\","
982                 fi
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   ],"
987         fi
988
989
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\""
996         else
997                 config_json=$config_json"\n                                \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_WRITE_URL\""
998         fi
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\""
1008         else
1009                 config_json=$config_json"\n                                   \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_READ_URL\""
1010         fi
1011         config_json=$config_json"\n                                 }"
1012         config_json=$config_json"\n                               }"
1013         config_json=$config_json"\n   },"
1014
1015         config_json=$config_json"\n   \"ric\": ["
1016
1017         rics=$(docker ps | grep ricsim | awk '{print $NF}')
1018
1019         if [ $? -ne 0 ] || [ -z "$rics" ]; then
1020                 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
1021                 ((RES_CONF_FAIL++))
1022                 return 1
1023         fi
1024
1025         cntr=0
1026         for ric in $rics; do
1027                 if [ $cntr -gt 0 ]; then
1028                         config_json=$config_json"\n          ,"
1029                 fi
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\","
1034                 else
1035                         config_json=$config_json"\n            \"baseUrl\": \"$RIC_SIM_HTTPX://localhost:$(__find_sim_port $ric)\","
1036                 fi
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\","
1041                 fi
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          }"
1047                 let cntr=cntr+1
1048         done
1049
1050         config_json=$config_json"\n           ]"
1051         config_json=$config_json"\n}"
1052
1053
1054         printf "$config_json">$2
1055
1056         echo ""
1057 }
1058
1059
1060 # Start Consul and CBS
1061 # args: -
1062 # (Function for test scripts)
1063 start_consul_cbs() {
1064
1065         echo -e $BOLD"Starting Consul and CBS"$EBOLD
1066
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"
1069 }
1070
1071 ###########################
1072 ### RIC Simulator functions
1073 ###########################
1074
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
1080         echo ""
1081 }
1082
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
1088         echo ""
1089 }
1090
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() {
1095
1096         echo -e $BOLD"Starting RIC Simulators"$EBOLD
1097
1098         if [ $# != 3 ]; then
1099                 ((RES_CONF_FAIL++))
1100         __print_err "need three args,  ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1101                 exit 1
1102         fi
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
1106                 G1_COUNT=$2
1107                 G1_A1_VERSION=$3
1108         elif [ $1 == "ricsim_g2" ]; then
1109                 G2_COUNT=$2
1110                 G2_A1_VERSION=$3
1111         elif [ $1 == "ricsim_g3" ]; then
1112                 G3_COUNT=$2
1113                 G3_A1_VERSION=$3
1114         else
1115                 ((RES_CONF_FAIL++))
1116         __print_err "need three args, gricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1117                 exit 1
1118         fi
1119
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
1122
1123         export G1_A1_VERSION
1124         export G2_A1_VERSION
1125         export G3_A1_VERSION
1126
1127         docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1128         app_data=""
1129         cntr=1
1130         while [ $cntr -le $2 ]; do
1131                 app=$1"_"$cntr
1132                 port=0
1133                 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
1134                 let cntr=cntr+1
1135         done
1136         __start_container ric "$docker_args" $app_data
1137
1138 }
1139
1140 ###########################
1141 ### Control Panel functions
1142 ###########################
1143
1144 # Start the Control Panel container
1145 # args: -
1146 # (Function for test scripts)
1147 start_control_panel() {
1148
1149         echo -e $BOLD"Starting Control Panel"$EBOLD
1150
1151         __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
1152
1153 }
1154
1155 ##################
1156 ### SDNC functions
1157 ##################
1158
1159 # Start the SDNC A1 Controller
1160 # args: -
1161 # (Function for test scripts)
1162 start_sdnc() {
1163
1164         echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1165
1166         __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT $SDNC_ALIVE_URL "http"
1167
1168 }
1169
1170 use_sdnc_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
1175         echo ""
1176 }
1177
1178 use_sdnc_https() {
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
1183         echo ""
1184 }
1185
1186 #######################
1187 ### SDNC ONAP functions
1188 #######################
1189
1190 # Start the SDNC ONAP A1 Adapter
1191 # args: -
1192 # (Function for test scripts)
1193 start_sdnc_onap() {
1194
1195         echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1196
1197         __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT $SDNC_ONAP_ALIVE_URL "http"
1198
1199 }
1200
1201 # Configure the SDNC ONAP A1 Adapter
1202 # args: -
1203 # (Function for test scripts)
1204 config_sdnc_onap() {
1205
1206         echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1207
1208         LOCALFILE=".sdnc_onap.prop"
1209         REMOTEFILE="/tmp/.sdnc_onap.prop"
1210
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
1214                 exit 1
1215         fi
1216
1217
1218         #Config of the prop file shall be inserted here
1219
1220         #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1221
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
1225                 exit 1
1226         fi
1227
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
1231                 exit 1
1232         fi
1233 }
1234
1235 #####################
1236 ### MR stub functions
1237 #####################
1238
1239 # Start the Message Router stub interface in the simulator group
1240 # args: -
1241 # (Function for test scripts)
1242 start_mr() {
1243
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"
1247 }
1248
1249 use_mr_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
1254         echo ""
1255 }
1256
1257 use_mr_https() {
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
1262         echo ""
1263 }
1264
1265
1266 ################
1267 ### CR functions
1268 ################
1269
1270 # Start the Callback reciver in the simulator group
1271 # args: -
1272 # (Function for test scripts)
1273 start_cr() {
1274
1275         echo -e $BOLD"Starting Callback Receiver"$EBOLD
1276
1277         __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
1278
1279 }
1280
1281 ###########################
1282 ### Policy Agents functions
1283 ###########################
1284
1285 # Use an agent on the local machine instead of container
1286 use_agent_stand_alone() {
1287         AGENT_STAND_ALONE=1
1288 }
1289
1290 # Start the policy agent
1291 # args: -
1292 # (Function for test scripts)
1293 start_policy_agent() {
1294
1295         echo -e $BOLD"Starting Policy Agent"$EBOLD
1296
1297         if [ $AGENT_STAND_ALONE -eq 0 ]; then
1298                 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1299         else
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
1305
1306                 read -p "<press any key to continue>"
1307                 __start_container policy_agent "STANDALONE" $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1308         fi
1309
1310 }
1311
1312 # All calls to the agent will be directed to the agent REST interface from now on
1313 # args: -
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
1318         echo ""
1319 }
1320
1321 # All calls to the agent will be directed to the agent REST interface from now on
1322 # args: -
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
1327         echo ""
1328         return 0
1329 }
1330
1331 # All calls to the agent will be directed to the agent dmaap interface from now on
1332 # args: -
1333 # (Function for test scripts)
1334 use_agent_dmaap() {
1335         echo -e $BOLD"Agent using DMAAP interface"$EBOLD
1336         export ADAPTER=$DMAAPBASE
1337         echo ""
1338         return 0
1339 }
1340
1341
1342 # Turn on debug level tracing in the agent
1343 # args: -
1344 # (Function for test scripts)
1345 set_agent_debug() {
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" $@
1350                 return 1
1351         fi
1352         echo ""
1353         return 0
1354 }
1355
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=$@
1362         echo ""
1363         return
1364 }
1365
1366 #################
1367 ### Log functions
1368 #################
1369
1370 # Check the agent logs for WARNINGs and ERRORs
1371 # args: -
1372 # (Function for test scripts)
1373
1374 check_policy_agent_logs() {
1375         __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
1376 }
1377
1378 check_control_panel_logs() {
1379         __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
1380 }
1381
1382 __check_container_logs() {
1383         dispname=$1
1384         appname=$2
1385         logpath=$3
1386         echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1387
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"
1392                 return
1393         fi
1394         foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1395         if [ $? -ne  0 ];then
1396                 echo "  Problem to search $appname log $logpath"
1397         else
1398                 if [ $foundentries -eq 0 ]; then
1399                         echo "  No WARN entries found in $appname log $logpath"
1400                 else
1401                         echo -e "  Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1402                 fi
1403         fi
1404         foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1405         if [ $? -ne  0 ];then
1406                 echo "  Problem to search $appname log $logpath"
1407         else
1408                 if [ $foundentries -eq 0 ]; then
1409                         echo "  No ERR entries found in $appname log $logpath"
1410                 else
1411                         echo -e $RED"  Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1412                 fi
1413         fi
1414         echo ""
1415 }
1416
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)
1421 store_logs() {
1422         if [ $# != 1 ]; then
1423                 ((RES_CONF_FAIL++))
1424         __print_err "need one arg, <file-prefix>" $@
1425                 exit 1
1426         fi
1427         echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1$EBOLD
1428
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
1436
1437         docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1438
1439         docker exec -it $SDNC_ONAP_APP_NAME cat $SDNC_ONAP_KARAF_LOG > $TESTLOGS/$ATC/$1_SDNC_ONAP_karaf.log 2>&1
1440
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
1444         done
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
1447         echo ""
1448 }
1449
1450 ###############
1451 ## Generic curl
1452 ###############
1453 # Generic curl function, assumed all 200-codes are ok
1454 # args: <url>
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
1457 __do_curl() {
1458         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1459         curlString="curl -skw %{http_code} $1"
1460         echo " CMD: $curlString" >> $HTTPLOG
1461         res=$($curlString)
1462         echo " RESP: $res" >> $HTTPLOG
1463         http_code="${res:${#res}-3}"
1464         if [ ${#res} -eq 3 ]; then
1465                 echo "<no-response-from-server>"
1466                 return 1
1467         else
1468                 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
1469                         echo "<not found, resp:${http_code}>"
1470                         return 1
1471                 fi
1472                 if [ $# -eq 2 ]; then
1473                         echo "${res:0:${#res}-3}" | xargs
1474                 else
1475                         echo "${res:0:${#res}-3}"
1476                 fi
1477
1478                 return 0
1479         fi
1480 }
1481
1482 #######################################
1483 ### Basic helper function for test cases
1484 #######################################
1485
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.
1493
1494 __var_test() {
1495         checkjsonarraycount=0
1496
1497         if [ $# -eq 6 ]; then
1498                 if [[ $3 == "json:"* ]]; then
1499                         checkjsonarraycount=1
1500                 fi
1501
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
1504                 ((RES_TEST++))
1505                 start=$SECONDS
1506                 ctr=0
1507                 for (( ; ; )); do
1508                         if [ $checkjsonarraycount -eq 0 ]; then
1509                                 result="$(__do_curl $2$3)"
1510                                 retcode=$?
1511                                 result=${result//[[:blank:]]/} #Strip blanks
1512                         else
1513                                 path=${3:5}
1514                                 result="$(__do_curl $2$path)"
1515                                 retcode=$?
1516                                 echo "$result" > .tmp.curl.json
1517                                 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1518                         fi
1519                         duration=$((SECONDS-start))
1520                         echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
1521                         let ctr=ctr+1
1522                         if [ $retcode -ne 0 ]; then
1523                                 if [ $duration -gt $6 ]; then
1524                                         ((RES_FAIL++))
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}"
1527                                         return
1528                                 fi
1529                         elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1530                                 ((RES_PASS++))
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 ----"
1534                                 return
1535                         elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1536                                 ((RES_PASS++))
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}  ----"
1540                                 return
1541                         elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1542                                 ((RES_PASS++))
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}  ----"
1546                                 return
1547                         elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1548                                 ((RES_PASS++))
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}  ----"
1552                                 return
1553                         else
1554                                 if [ $duration -gt $6 ]; then
1555                                         ((RES_FAIL++))
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} ----"
1558                                         return
1559                                 fi
1560                         fi
1561                         sleep 1
1562                 done
1563         elif [ $# -eq 5 ]; then
1564                 if [[ $3 == "json:"* ]]; then
1565                         checkjsonarraycount=1
1566                 fi
1567
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
1570                 ((RES_TEST++))
1571                 if [ $checkjsonarraycount -eq 0 ]; then
1572                         result="$(__do_curl $2$3)"
1573                         retcode=$?
1574                         result=${result//[[:blank:]]/} #Strip blanks
1575                 else
1576                         path=${3:5}
1577                         result="$(__do_curl $2$path)"
1578                         retcode=$?
1579                         echo "$result" > .tmp.curl.json
1580                         result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1581                 fi
1582                 if [ $retcode -ne 0 ]; then
1583                         ((RES_FAIL++))
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
1587                         ((RES_PASS++))
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
1591                         ((RES_PASS++))
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
1595                         ((RES_PASS++))
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
1599                         ((RES_PASS++))
1600                         echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1601                         #echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1602                 else
1603                         ((RES_FAIL++))
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} ----"
1606                 fi
1607         else
1608                 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1609                 echo "Got:" $@
1610                 exit 1
1611         fi
1612 }
1613
1614
1615 ### Generic test cases for varaible checking
1616
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
1622 # value or not.
1623 # (Function for test scripts)
1624 cr_equal() {
1625         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1626                 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1627         else
1628                 ((RES_CONF_FAIL++))
1629                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1630         fi
1631 }
1632
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
1638 # value or not.
1639 # (Function for test scripts)
1640 mr_equal() {
1641         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1642                 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1643         else
1644                 ((RES_CONF_FAIL++))
1645                 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1646         fi
1647 }
1648
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
1654 # value or not.
1655 # (Function for test scripts)
1656 mr_greater() {
1657         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1658                 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
1659         else
1660                 ((RES_CONF_FAIL++))
1661                 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1662         fi
1663 }
1664
1665 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1666 mr_read() {
1667         echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1668 }
1669
1670 # Print a variable value from the MR stub.
1671 # arg: <variable-name>
1672 # (Function for test scripts)
1673 mr_print() {
1674         if [ $# != 1 ]; then
1675                 ((RES_CONF_FAIL++))
1676         __print_err "need one arg, <mr-param>" $@
1677                 exit 1
1678         fi
1679         echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD
1680 }