Test env updates for dmaap messages in batch and for running external agent
[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="./fakedir"  #Fake dir so that the sim container does not find any 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 ps --filter name=${name} --format \"{{.Names}} {{.Ports}}\" | grep '${name}' | sed s/0.0.0.0:// | cut -f 2 -d ' ' | cut -f 1 -d '-'"
719         cmdstr="docker ps --filter name=${name} --format \"{{.Names}} {{.Ports}}\" | grep '${name}' | cut -f 3 -d ',' | sed s/0.0.0.0:// | cut -f 2 -d ' ' | cut -f 1 -d '-'"
720
721         res=$(eval $cmdstr)
722         if [[ "$res" =~ ^[0-9]+$ ]]; then
723                 echo $res
724         else
725                 echo "0"
726     fi
727 }
728
729 # Function to create the docker network for the test
730 # Not to be called from the test script itself.
731 __create_docker_network() {
732         tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
733         if [ $? -ne 0 ]; then
734                 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
735                 return 1
736         fi
737         if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
738                 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
739                 docker network create $DOCKER_SIM_NWNAME
740                 if [ $? -ne 0 ]; then
741                         echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
742                         return 1
743                 fi
744         else
745                 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
746         fi
747 }
748
749 # Check if container is started by calling url on localhost using a port, expects response code 2XX
750 # args: <container-name> <port> <url> https|https
751 # Not to be called from the test script itself.
752 __check_container_start() {
753         paramError=0
754         if [ $# -ne 4 ]; then
755                 paramError=1
756         elif [ $4 != "http" ] && [ $4 != "https" ]; then
757                 paramError=1
758         fi
759         if [ $paramError -ne 0 ]; then
760                 ((RES_CONF_FAIL++))
761                 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
762                 return 1
763         fi
764         echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
765         appname=$1
766         localport=$2
767         url=$3
768         if [[ $appname != "STANDALONE_"* ]]     ; then
769                 app_started=0
770                 for i in {1..10}; do
771                         if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
772                                         echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
773                                         app_started=1
774                                         break
775                                 else
776                                         sleep $i
777                         fi
778                 done
779                 if [ $app_started -eq 0 ]; then
780                         ((RES_CONF_FAIL++))
781                         echo ""
782                         echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
783                         return 1
784                 fi
785                 if [ $localport -eq 0 ]; then
786                         while [ $localport -eq 0 ]; do
787                                 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
788                                 localport=$(__find_sim_port $appname)
789                                 sleep 1
790                                 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
791                         done
792                         echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
793                         echo ""
794                 fi
795         fi
796
797         pa_st=false
798         echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
799         for i in {1..20}; do
800                 if [ $4 == "https" ]; then
801                         result="$(__do_curl "-k https://localhost:"${localport}${url})"
802                 else
803                         result="$(__do_curl $LOCALHOST${localport}${url})"
804                 fi
805                 if [ $? -eq 0 ]; then
806                         if [ ${#result} -gt 15 ]; then
807                                 #If response is too long, truncate
808                                 result="...response text too long, omitted"
809                         fi
810                         echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
811                         echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN"
812                         pa_st=true
813                         break
814                 else
815                         #echo " Retrying in $i seconds"
816                         echo -ne " Waiting for container ${appname} service status...retrying in $i seconds${SAMELINE}"
817                         sleep $i
818                 fi
819         done
820
821         if [ "$pa_st" = "false"  ]; then
822                 ((RES_CONF_FAIL++))
823                 echo -e $RED" Container ${appname} did not respond to service status"$ERED
824                 return 0
825         fi
826
827         echo ""
828         return 0
829 }
830
831
832 # Function to start a container and wait until it responds on the given port and url.
833 #args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
834 __start_container() {
835
836         variableArgCount=$(($#-2))
837         if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
838                 ((RES_CONF_FAIL++))
839         __print_err "need 6 or more args,  <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> http|https [<app-name> <port-number> <alive-url> http|https ]*" $@
840                 exit 1
841         fi
842
843         __create_docker_network
844
845         curdir=$PWD
846         cd $SIM_GROUP
847         cd $1
848
849         if [ "$2" == "NODOCKERARGS" ]; then
850                 docker-compose up -d &> .dockererr
851                 if [ $? -ne 0 ]; then
852                         echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
853                         cat .dockererr
854                 fi
855         elif [ "$2" == "STANDALONE" ]; then
856                 echo "Skipping docker-compose"
857         else
858                 docker-compose up -d $2 &> .dockererr
859                 if [ $? -ne 0 ]; then
860                         echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
861                         cat .dockererr
862                 fi
863         fi
864         app_prefix=""
865         if [ "$2" == "STANDALONE" ]; then
866                 app_prefix="STANDALONE_"
867         fi
868         shift; shift;
869         cntr=0
870         while [ $cntr -lt $variableArgCount ]; do
871                 app=$app_prefix$1; shift;
872                 port=$1; shift;
873                 url=$1; shift;
874                 httpx=$1; shift;
875                 let cntr=cntr+4
876
877                 __check_container_start "$app" "$port" "$url" $httpx
878         done
879
880         cd $curdir
881         echo ""
882         return 0
883 }
884
885 ####################
886 ### Consul functions
887 ####################
888
889 # Function to load config from a file into consul for the Policy Agent
890 # arg: <json-config-file>
891 # (Function for test scripts)
892 consul_config_app() {
893
894         echo -e $BOLD"Configuring Consul"$EBOLD
895
896         if [ $# -ne 1 ]; then
897                 ((RES_CONF_FAIL++))
898         __print_err "need one arg,  <json-config-file>" $@
899                 exit 1
900         fi
901
902         echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
903
904         curl -s $LOCALHOST${CONSUL_EXTERNAL_PORT}/v1/kv/${POLICY_AGENT_APP_NAME}?dc=dc1 -X PUT -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'X-Requested-With: XMLHttpRequest' --data-binary "@"$1 >/dev/null
905         if [ $? -ne 0 ]; then
906                 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
907                 ((RES_CONF_FAIL++))
908                 return 1
909         fi
910         body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
911         echo $body > ".output"$1
912
913         if [ $? -ne 0 ]; then
914                 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
915                 ((RES_CONF_FAIL++))
916                 return 1
917         else
918                 targetJson=$(< $1)
919                 targetJson="{\"config\":"$targetJson"}"
920                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
921                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
922                 if [ $res -ne 0 ]; then
923                         echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
924                         ((RES_CONF_FAIL++))
925                         return 1
926                 else
927                         echo -e $GREEN" Config loaded ok to consul"$EGREEN
928                 fi
929         fi
930
931         echo ""
932
933 }
934
935 # Function to perpare the consul configuration according to the current simulator configuration
936 # args: SDNC|SDNC_ONAP|NOSDNC <output-file>
937 # (Function for test scripts)
938 prepare_consul_config() {
939         echo -e $BOLD"Prepare Consul config"$EBOLD
940
941         echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
942
943         if [ $# != 2 ];  then
944                 ((RES_CONF_FAIL++))
945         __print_err "need two args,  SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
946                 exit 1
947         fi
948
949         if [ $1 == "SDNC" ]; then
950                 echo -e " Config$BOLD including SDNC$EBOLD configuration"
951         elif [ $1 == "SDNC_ONAP" ]; then
952                 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
953         elif [ $1 == "NOSDNC" ];  then
954                 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
955         else
956                 ((RES_CONF_FAIL++))
957         __print_err "need two args,  SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
958                 exit 1
959         fi
960
961         config_json="\n            {"
962         if [ $1 == "SDNC" ]; then
963                 config_json=$config_json"\n   \"controller\": ["
964                 config_json=$config_json"\n                     {"
965                 config_json=$config_json"\n                       \"name\": \"$SDNC_APP_NAME\","
966                 if [ $AGENT_STAND_ALONE -eq 0 ]; then
967                         config_json=$config_json"\n                       \"baseUrl\": \"$SDNC_HTTPX://$SDNC_APP_NAME:$SDNC_PORT\","
968                 else
969                         config_json=$config_json"\n                       \"baseUrl\": \"$SDNC_HTTPX://localhost:$SDNC_LOCAL_PORT\","
970                 fi
971                 config_json=$config_json"\n                       \"userName\": \"$SDNC_USER\","
972                 config_json=$config_json"\n                       \"password\": \"$SDNC_PWD\""
973                 config_json=$config_json"\n                     }"
974                 config_json=$config_json"\n   ],"
975         fi
976         if [ $1 == "SDNC_ONAP" ]; then
977                 config_json=$config_json"\n   \"controller\": ["
978                 config_json=$config_json"\n                     {"
979                 config_json=$config_json"\n                       \"name\": \"$SDNC_ONAP_APP_NAME\","
980                 if [ $AGENT_STAND_ALONE -eq 0 ]; then
981                         config_json=$config_json"\n                       \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
982                 else
983                         config_json=$config_json"\n                       \"baseUrl\": \"http://localhost:$SDNC_ONAP_EXTERNAL_PORT\","
984                 fi
985                 config_json=$config_json"\n                       \"userName\": \"$SDNC_ONAP_USER\","
986                 config_json=$config_json"\n                       \"password\": \"$SDNC_ONAP_PWD\""
987                 config_json=$config_json"\n                     }"
988                 config_json=$config_json"\n   ],"
989         fi
990
991
992         config_json=$config_json"\n   \"streams_publishes\": {"
993         config_json=$config_json"\n                            \"dmaap_publisher\": {"
994         config_json=$config_json"\n                              \"type\": \"$MR_APP_NAME\","
995         config_json=$config_json"\n                              \"dmaap_info\": {"
996         if [ $AGENT_STAND_ALONE -eq 0 ]; then
997                 config_json=$config_json"\n                                \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_WRITE_URL\""
998         else
999                 config_json=$config_json"\n                                \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_WRITE_URL\""
1000         fi
1001         config_json=$config_json"\n                              }"
1002         config_json=$config_json"\n                            }"
1003         config_json=$config_json"\n   },"
1004         config_json=$config_json"\n   \"streams_subscribes\": {"
1005         config_json=$config_json"\n                             \"dmaap_subscriber\": {"
1006         config_json=$config_json"\n                               \"type\": \"$MR_APP_NAME\","
1007         config_json=$config_json"\n                               \"dmaap_info\": {"
1008         if [ $AGENT_STAND_ALONE -eq 0 ]; then
1009                 config_json=$config_json"\n                                   \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_READ_URL\""
1010         else
1011                 config_json=$config_json"\n                                   \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_READ_URL\""
1012         fi
1013         config_json=$config_json"\n                                 }"
1014         config_json=$config_json"\n                               }"
1015         config_json=$config_json"\n   },"
1016
1017         config_json=$config_json"\n   \"ric\": ["
1018
1019         rics=$(docker ps | grep ricsim | awk '{print $NF}')
1020
1021         if [ $? -ne 0 ] || [ -z "$rics" ]; then
1022                 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
1023                 ((RES_CONF_FAIL++))
1024                 return 1
1025         fi
1026
1027         cntr=0
1028         for ric in $rics; do
1029                 if [ $cntr -gt 0 ]; then
1030                         config_json=$config_json"\n          ,"
1031                 fi
1032                 config_json=$config_json"\n          {"
1033                 config_json=$config_json"\n            \"name\": \"$ric\","
1034                 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1035                         config_json=$config_json"\n            \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
1036                 else
1037                         config_json=$config_json"\n            \"baseUrl\": \"$RIC_SIM_HTTPX://localhost:$(__find_sim_port $ric)\","
1038                 fi
1039                 if [ $1 == "SDNC" ]; then
1040                         config_json=$config_json"\n            \"controller\": \"$SDNC_APP_NAME\","
1041                 elif [ $1 == "SDNC_ONAP" ]; then
1042                         config_json=$config_json"\n            \"controller\": \"$SDNC_ONAP_APP_NAME\","
1043                 fi
1044                 config_json=$config_json"\n            \"managedElementIds\": ["
1045                 config_json=$config_json"\n              \"me1_$ric\","
1046                 config_json=$config_json"\n              \"me2_$ric\""
1047                 config_json=$config_json"\n            ]"
1048                 config_json=$config_json"\n          }"
1049                 let cntr=cntr+1
1050         done
1051
1052         config_json=$config_json"\n           ]"
1053         config_json=$config_json"\n}"
1054
1055
1056         printf "$config_json">$2
1057
1058         echo ""
1059 }
1060
1061
1062 # Start Consul and CBS
1063 # args: -
1064 # (Function for test scripts)
1065 start_consul_cbs() {
1066
1067         echo -e $BOLD"Starting Consul and CBS"$EBOLD
1068
1069         __start_container consul_cbs NODOCKERARGS  "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
1070                                                      "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
1071 }
1072
1073 ###########################
1074 ### RIC Simulator functions
1075 ###########################
1076
1077 use_simulator_http() {
1078         echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
1079         export RIC_SIM_HTTPX="http"
1080         export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1081         export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
1082         export RIC_SIM_CERT_MOUNT_DIR="./fakedir"  #Fake dir so that the sim container does not find any cert
1083         echo ""
1084 }
1085
1086 use_simulator_https() {
1087         echo -e "Using secure $BOLD https $EBOLD towards the simulators"
1088         export RIC_SIM_HTTPX="https"
1089         export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1090         export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
1091         export RIC_SIM_CERT_MOUNT_DIR="./cert"
1092         echo ""
1093 }
1094
1095 # Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
1096 # args:  ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
1097 # (Function for test scripts)
1098 start_ric_simulators() {
1099
1100         echo -e $BOLD"Starting RIC Simulators"$EBOLD
1101
1102         if [ $# != 3 ]; then
1103                 ((RES_CONF_FAIL++))
1104         __print_err "need three args,  ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1105                 exit 1
1106         fi
1107         echo " $2 simulators using basename: $1 on interface: $3"
1108         #Set env var for simulator count and A1 interface vesion for the given group
1109         if [ $1 == "ricsim_g1" ]; then
1110                 G1_COUNT=$2
1111                 G1_A1_VERSION=$3
1112         elif [ $1 == "ricsim_g2" ]; then
1113                 G2_COUNT=$2
1114                 G2_A1_VERSION=$3
1115         elif [ $1 == "ricsim_g3" ]; then
1116                 G3_COUNT=$2
1117                 G3_A1_VERSION=$3
1118         else
1119                 ((RES_CONF_FAIL++))
1120         __print_err "need three args, gricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1121                 exit 1
1122         fi
1123
1124         # Create .env file to compose project, all ric container will get this prefix
1125         echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
1126
1127         export G1_A1_VERSION
1128         export G2_A1_VERSION
1129         export G3_A1_VERSION
1130
1131         docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1132         app_data=""
1133         cntr=1
1134         while [ $cntr -le $2 ]; do
1135                 app=$1"_"$cntr
1136                 port=0
1137                 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
1138                 let cntr=cntr+1
1139         done
1140         __start_container ric "$docker_args" $app_data
1141
1142 }
1143
1144 ###########################
1145 ### Control Panel functions
1146 ###########################
1147
1148 # Start the Control Panel container
1149 # args: -
1150 # (Function for test scripts)
1151 start_control_panel() {
1152
1153         echo -e $BOLD"Starting Control Panel"$EBOLD
1154
1155         __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
1156
1157 }
1158
1159 ##################
1160 ### SDNC functions
1161 ##################
1162
1163 # Start the SDNC A1 Controller
1164 # args: -
1165 # (Function for test scripts)
1166 start_sdnc() {
1167
1168         echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1169
1170         __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT $SDNC_ALIVE_URL "http"
1171
1172 }
1173
1174 use_sdnc_http() {
1175         echo -e $BOLD"Using http between agent and SDNC"$EBOLD
1176         export SDNC_HTTPX="http"
1177         export SDNC_PORT=$SDNC_INTERNAL_PORT
1178         export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT
1179         echo ""
1180 }
1181
1182 use_sdnc_https() {
1183         echo -e $BOLD"Using https between agent and SDNC"$EBOLD
1184         export SDNC_HTTPX="https"
1185         export SDNC_PORT=$SDNC_INTERNAL_SECURE_PORT
1186         export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_SECURE_PORT
1187         echo ""
1188 }
1189
1190 #######################
1191 ### SDNC ONAP functions
1192 #######################
1193
1194 # Start the SDNC ONAP A1 Adapter
1195 # args: -
1196 # (Function for test scripts)
1197 start_sdnc_onap() {
1198
1199         echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1200
1201         __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT $SDNC_ONAP_ALIVE_URL "http"
1202
1203 }
1204
1205 # Configure the SDNC ONAP A1 Adapter
1206 # args: -
1207 # (Function for test scripts)
1208 config_sdnc_onap() {
1209
1210         echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1211
1212         LOCALFILE=".sdnc_onap.prop"
1213         REMOTEFILE="/tmp/.sdnc_onap.prop"
1214
1215         docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1216         if [ $? -ne 0 ]; then
1217                 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1218                 exit 1
1219         fi
1220
1221
1222         #Config of the prop file shall be inserted here
1223
1224         #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1225
1226         docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1227         if [ $? -ne 0 ]; then
1228                 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1229                 exit 1
1230         fi
1231
1232         docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1233         if [ $? -ne 0 ]; then
1234                 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1235                 exit 1
1236         fi
1237 }
1238
1239 #####################
1240 ### MR stub functions
1241 #####################
1242
1243 # Start the Message Router stub interface in the simulator group
1244 # args: -
1245 # (Function for test scripts)
1246 start_mr() {
1247
1248         echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
1249         export MR_CERT_MOUNT_DIR="./cert"
1250         __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
1251 }
1252
1253 use_mr_http() {
1254         echo -e $BOLD"Using http between agent and MR"$EBOLD
1255         export MR_HTTPX="http"
1256         export MR_PORT=$MR_INTERNAL_PORT
1257         export MR_LOCAL_PORT=$MR_EXTERNAL_PORT
1258         echo ""
1259 }
1260
1261 use_mr_https() {
1262         echo -e $BOLD"Using https between agent and MR"$EBOLD
1263         export MR_HTTPX="https"
1264         export MR_PORT=$MR_INTERNAL_SECURE_PORT
1265         export MR_LOCAL_PORT=$MR_EXTERNAL_SECURE_PORT
1266         echo ""
1267 }
1268
1269
1270 ################
1271 ### CR functions
1272 ################
1273
1274 # Start the Callback reciver in the simulator group
1275 # args: -
1276 # (Function for test scripts)
1277 start_cr() {
1278
1279         echo -e $BOLD"Starting Callback Receiver"$EBOLD
1280
1281         __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
1282
1283 }
1284
1285 ###########################
1286 ### Policy Agents functions
1287 ###########################
1288
1289 # Use an agent on the local machine instead of container
1290 use_agent_stand_alone() {
1291         AGENT_STAND_ALONE=1
1292 }
1293
1294 # Start the policy agent
1295 # args: -
1296 # (Function for test scripts)
1297 start_policy_agent() {
1298
1299         echo -e $BOLD"Starting Policy Agent"$EBOLD
1300
1301         if [ $AGENT_STAND_ALONE -eq 0 ]; then
1302                 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1303         else
1304                 echo -e $RED"The consul config produced by this test script (filename '<fullpath-to-autotest-dir>.output<file-name>"$ERED
1305                 echo -e $RED"where the file name is the file in the consul_config_app command in this script) must be pointed out by the agent "$ERED
1306                 echo -e $RED"application.yaml"$ERED
1307                 echo -e $RED"The application jar may need to be built beforefor continuing"$ERED
1308                 echo -e $RED"The agent shall now be running on port $POLICY_AGENT_EXTERNAL_PORT for http"$ERED
1309
1310                 read -p "<press any key to continue>"
1311                 __start_container policy_agent "STANDALONE" $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1312         fi
1313
1314 }
1315
1316 # All calls to the agent will be directed to the agent REST interface from now on
1317 # args: -
1318 # (Function for test scripts)
1319 use_agent_rest_http() {
1320         echo -e $BOLD"Using agent REST interface with http"$EBOLD
1321         export ADAPTER=$RESTBASE
1322         echo ""
1323 }
1324
1325 # All calls to the agent will be directed to the agent REST interface from now on
1326 # args: -
1327 # (Function for test scripts)
1328 use_agent_rest_https() {
1329         echo -e $BOLD"Using agent REST interface with https"$EBOLD
1330         export ADAPTER=$RESTBASE_SECURE
1331         echo ""
1332         return 0
1333 }
1334
1335 # All calls to the agent will be directed to the agent dmaap interface from now on
1336 # args: -
1337 # (Function for test scripts)
1338 use_agent_dmaap() {
1339         echo -e $BOLD"Agent using DMAAP interface"$EBOLD
1340         export ADAPTER=$DMAAPBASE
1341         echo ""
1342         return 0
1343 }
1344
1345
1346 # Turn on debug level tracing in the agent
1347 # args: -
1348 # (Function for test scripts)
1349 set_agent_debug() {
1350         echo -e $BOLD"Setting agent debug"$EBOLD
1351         curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST  -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1352         if [ $? -ne 0 ]; then
1353                 __print_err "could not set debug mode" $@
1354                 return 1
1355         fi
1356         echo ""
1357         return 0
1358 }
1359
1360 # Perform curl retries when making direct call to the agent for the specified http response codes
1361 # Speace separated list of http response codes
1362 # args: [<response-code>]*
1363 use_agent_retries() {
1364         echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1365         AGENT_RETRY_CODES=$@
1366         echo ""
1367         return
1368 }
1369
1370 #################
1371 ### Log functions
1372 #################
1373
1374 # Check the agent logs for WARNINGs and ERRORs
1375 # args: -
1376 # (Function for test scripts)
1377
1378 check_policy_agent_logs() {
1379         __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
1380 }
1381
1382 check_control_panel_logs() {
1383         __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
1384 }
1385
1386 __check_container_logs() {
1387         dispname=$1
1388         appname=$2
1389         logpath=$3
1390         echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1391
1392         #tmp=$(docker ps | grep $appname)
1393         tmp=$(docker ps -q --filter name=$appname) #get the container id
1394         if [ -z "$tmp" ]; then  #Only check logs for running Policy Agent apps
1395                 echo $dispname" is not running, no check made"
1396                 return
1397         fi
1398         foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1399         if [ $? -ne  0 ];then
1400                 echo "  Problem to search $appname log $logpath"
1401         else
1402                 if [ $foundentries -eq 0 ]; then
1403                         echo "  No WARN entries found in $appname log $logpath"
1404                 else
1405                         echo -e "  Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1406                 fi
1407         fi
1408         foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1409         if [ $? -ne  0 ];then
1410                 echo "  Problem to search $appname log $logpath"
1411         else
1412                 if [ $foundentries -eq 0 ]; then
1413                         echo "  No ERR entries found in $appname log $logpath"
1414                 else
1415                         echo -e $RED"  Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1416                 fi
1417         fi
1418         echo ""
1419 }
1420
1421 # Store all container logs and other logs in the log dir for the script
1422 # Logs are stored with a prefix in case logs should be stored several times during a test
1423 # args: <logfile-prefix>
1424 # (Function for test scripts)
1425 store_logs() {
1426         if [ $# != 1 ]; then
1427                 ((RES_CONF_FAIL++))
1428         __print_err "need one arg, <file-prefix>" $@
1429                 exit 1
1430         fi
1431         echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1$EBOLD
1432
1433         docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1434         docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1435         docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1436         docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1437         docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1438         docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1439         cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1440
1441         docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1442
1443         docker exec -it $SDNC_ONAP_APP_NAME cat $SDNC_ONAP_KARAF_LOG > $TESTLOGS/$ATC/$1_SDNC_ONAP_karaf.log 2>&1
1444
1445         rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1446         for ric in $rics; do
1447                 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1448         done
1449         body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1450         echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1451         echo ""
1452 }
1453
1454 ###############
1455 ## Generic curl
1456 ###############
1457 # Generic curl function, assumed all 200-codes are ok
1458 # args: <url>
1459 # returns: <returned response (without respose code)>  or "<no-response-from-server>" or "<not found, <http-code>>""
1460 # returns: The return code is 0 for ok and 1 for not ok
1461 __do_curl() {
1462         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1463         curlString="curl -skw %{http_code} $1"
1464         echo " CMD: $curlString" >> $HTTPLOG
1465         res=$($curlString)
1466         echo " RESP: $res" >> $HTTPLOG
1467         http_code="${res:${#res}-3}"
1468         if [ ${#res} -eq 3 ]; then
1469                 echo "<no-response-from-server>"
1470                 return 1
1471         else
1472                 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
1473                         echo "<not found, resp:${http_code}>"
1474                         return 1
1475                 fi
1476                 if [ $# -eq 2 ]; then
1477                         echo "${res:0:${#res}-3}" | xargs
1478                 else
1479                         echo "${res:0:${#res}-3}"
1480                 fi
1481
1482                 return 0
1483         fi
1484 }
1485
1486 #######################################
1487 ### Basic helper function for test cases
1488 #######################################
1489
1490 # Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1491 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value>  - This test is done
1492 # immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1493 # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout>  - This test waits up to the timeout
1494 # before setting pass or fail depending on the result of comparing variable and target using the operator.
1495 # If the <variable-name> has the 'json:' prefix, the the variable will be used as url and the <target-value> will be compared towards the length of the json array in the response.
1496 # Not to be called from test script.
1497
1498 __var_test() {
1499         checkjsonarraycount=0
1500
1501         if [ $# -eq 6 ]; then
1502                 if [[ $3 == "json:"* ]]; then
1503                         checkjsonarraycount=1
1504                 fi
1505
1506                 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
1507                 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
1508                 ((RES_TEST++))
1509                 start=$SECONDS
1510                 ctr=0
1511                 for (( ; ; )); do
1512                         if [ $checkjsonarraycount -eq 0 ]; then
1513                                 result="$(__do_curl $2$3)"
1514                                 retcode=$?
1515                                 result=${result//[[:blank:]]/} #Strip blanks
1516                         else
1517                                 path=${3:5}
1518                                 result="$(__do_curl $2$path)"
1519                                 retcode=$?
1520                                 echo "$result" > .tmp.curl.json
1521                                 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1522                         fi
1523                         duration=$((SECONDS-start))
1524                         echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
1525                         let ctr=ctr+1
1526                         if [ $retcode -ne 0 ]; then
1527                                 if [ $duration -gt $6 ]; then
1528                                         ((RES_FAIL++))
1529                                         #echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5}  not reached in ${6} seconds, result = ${result} ----"
1530                                         echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1531                                         return
1532                                 fi
1533                         elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1534                                 ((RES_PASS++))
1535                                 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1536                                 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1537                                 #echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
1538                                 return
1539                         elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1540                                 ((RES_PASS++))
1541                                 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1542                                 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1543                                 #echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
1544                                 return
1545                         elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1546                                 ((RES_PASS++))
1547                                 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1548                                 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1549                                 #echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
1550                                 return
1551                         elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1552                                 ((RES_PASS++))
1553                                 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
1554                                 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1555                                 #echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result}  ----"
1556                                 return
1557                         else
1558                                 if [ $duration -gt $6 ]; then
1559                                         ((RES_FAIL++))
1560                                         echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1561                                         #echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5}  not reached in ${6} seconds, result = ${result} ----"
1562                                         return
1563                                 fi
1564                         fi
1565                         sleep 1
1566                 done
1567         elif [ $# -eq 5 ]; then
1568                 if [[ $3 == "json:"* ]]; then
1569                         checkjsonarraycount=1
1570                 fi
1571
1572                 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
1573                 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1574                 ((RES_TEST++))
1575                 if [ $checkjsonarraycount -eq 0 ]; then
1576                         result="$(__do_curl $2$3)"
1577                         retcode=$?
1578                         result=${result//[[:blank:]]/} #Strip blanks
1579                 else
1580                         path=${3:5}
1581                         result="$(__do_curl $2$path)"
1582                         retcode=$?
1583                         echo "$result" > .tmp.curl.json
1584                         result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
1585                 fi
1586                 if [ $retcode -ne 0 ]; then
1587                         ((RES_FAIL++))
1588                         #echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1589                         echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
1590                 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1591                         ((RES_PASS++))
1592                         echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1593                         #echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met"
1594                 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
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 = "<" ] && [ "$result" -lt $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                 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1603                         ((RES_PASS++))
1604                         echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1605                         #echo -e "----  \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1606                 else
1607                         ((RES_FAIL++))
1608                         echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
1609                         #echo -e "----  \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1610                 fi
1611         else
1612                 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1613                 echo "Got:" $@
1614                 exit 1
1615         fi
1616 }
1617
1618
1619 ### Generic test cases for varaible checking
1620
1621 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
1622 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1623 # equal to the target or not.
1624 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1625 # before setting pass or fail depending on if the variable value becomes equal to the target
1626 # value or not.
1627 # (Function for test scripts)
1628 cr_equal() {
1629         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1630                 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1631         else
1632                 ((RES_CONF_FAIL++))
1633                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1634         fi
1635 }
1636
1637 # Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1638 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1639 # equal to the target or not.
1640 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1641 # before setting pass or fail depending on if the variable value becomes equal to the target
1642 # value or not.
1643 # (Function for test scripts)
1644 mr_equal() {
1645         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1646                 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1647         else
1648                 ((RES_CONF_FAIL++))
1649                 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1650         fi
1651 }
1652
1653 # Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1654 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1655 # greater than the target or not.
1656 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
1657 # before setting pass or fail depending on if the variable value becomes greater than the target
1658 # value or not.
1659 # (Function for test scripts)
1660 mr_greater() {
1661         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1662                 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
1663         else
1664                 ((RES_CONF_FAIL++))
1665                 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1666         fi
1667 }
1668
1669 # Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1670 mr_read() {
1671         echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1672 }
1673
1674 # Print a variable value from the MR stub.
1675 # arg: <variable-name>
1676 # (Function for test scripts)
1677 mr_print() {
1678         if [ $# != 1 ]; then
1679                 ((RES_CONF_FAIL++))
1680         __print_err "need one arg, <mr-param>" $@
1681                 exit 1
1682         fi
1683         echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD
1684 }