511627375da4100f38132ee043a80ff420e0731c
[nonrtric.git] / test / common / cr_api_functions.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 container/service managemnt functions test functions for the Callback Reciver
21
22
23 ################ Test engine functions ################
24
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __CR_imagesetup() {
29         __check_and_create_image_var CR "CR_IMAGE" "CR_IMAGE_BASE" "CR_IMAGE_TAG" LOCAL "$CR_DISPLAY_NAME"
30 }
31
32 # Pull image from remote repo or use locally built image
33 # arg: <pull-policy-override> <pull-policy-original>
34 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35 # <pull-policy-original> Shall be used for images that does not allow overriding
36 # Both var may contain: 'remote', 'remote-remove' or 'local'
37 __CR_imagepull() {
38         echo -e $RED" Image for app CR shall never be pulled from remote repo"$ERED
39 }
40
41 # Build image (only for simulator or interfaces stubs owned by the test environment)
42 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
44 __CR_imagebuild() {
45         cd ../cr
46         echo " Building CR - $CR_DISPLAY_NAME - image: $CR_IMAGE"
47         docker build  --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $CR_IMAGE . &> .dockererr
48         if [ $? -eq 0 ]; then
49                 echo -e  $GREEN"  Build Ok"$EGREEN
50                 __retag_and_push_image CR_IMAGE
51                 if [ $? -ne 0 ]; then
52                         exit 1
53                 fi
54         else
55                 echo -e $RED"  Build Failed"$ERED
56                 ((RES_CONF_FAIL++))
57                 cat .dockererr
58                 echo -e $RED"Exiting...."$ERED
59                 exit 1
60         fi
61 }
62
63 # Generate a string for each included image using the app display name and a docker images format string
64 # If a custom image repo is used then also the source image from the local repo is listed
65 # arg: <docker-images-format-string> <file-to-append>
66 __CR_image_data() {
67         echo -e "$CR_DISPLAY_NAME\t$(docker images --format $1 $CR_IMAGE)" >>   $2
68         if [ ! -z "$CR_IMAGE_SOURCE" ]; then
69                 echo -e "-- source image --\t$(docker images --format $1 $CR_IMAGE_SOURCE)" >>   $2
70         fi
71 }
72
73 # Scale kubernetes resources to zero
74 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
75 # This function is called for apps fully managed by the test script
76 __CR_kube_scale_zero() {
77         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest CR
78 }
79
80 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
81 # This function is called for prestarted apps not managed by the test script.
82 __CR_kube_scale_zero_and_wait() {
83         echo -e $RED" CR app is not scaled in this state"$ERED
84 }
85
86 # Delete all kube resouces for the app
87 # This function is called for apps managed by the test script.
88 __CR_kube_delete_all() {
89         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest CR
90 }
91
92 # Store docker logs
93 # This function is called for apps managed by the test script.
94 # args: <log-dir> <file-prexix>
95 __CR_store_docker_logs() {
96         if [ $RUNMODE == "KUBE" ]; then
97                 for podname in $(kubectl get pods -n $KUBE_SIM_NAMESPACE -l "autotest=CR" -o custom-columns=":metadata.name"); do
98                         kubectl logs -n $KUBE_SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
99                 done
100         else
101                 crs=$(docker ps --filter "name=$CR_APP_NAME" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
102                 for crid in $crs; do
103                         docker logs $crid > $1$2_$crid.log 2>&1
104                 done
105         fi
106 }
107
108 # Initial setup of protocol, host and ports
109 # This function is called for apps managed by the test script.
110 # args: -
111 __CR_initial_setup() {
112         use_cr_http
113 }
114
115 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
116 # For docker, the namespace shall be excluded
117 # This function is called for apps managed by the test script as well as for prestarted apps.
118 # args: -
119 __CR_statisics_setup() {
120         for ((CR_INSTANCE=MAX_CR_APP_COUNT; CR_INSTANCE>0; CR_INSTANCE-- )); do
121                 if [ $RUNMODE == "KUBE" ]; then
122                         CR_INSTANCE_KUBE=$(($CR_INSTANCE-1))
123                         echo -n " CR-$CR_INSTANCE_KUBE $CR_APP_NAME-$CR_INSTANCE_KUBE $KUBE_SIM_NAMESPACE "
124                 else
125                         if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
126                                 echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}_cr_$CR_INSTANCE "
127                         else
128                                 echo -n " CR_$CR_INSTANCE ${CR_APP_NAME}-cr-$CR_INSTANCE "
129                         fi
130                 fi
131         done
132 }
133
134 #######################################################
135
136 ################
137 ### CR functions
138 ################
139
140 #Var to hold the current number of CR instances
141 CR_APP_COUNT=1
142 MAX_CR_APP_COUNT=10
143
144 # Set http as the protocol to use for all communication to the Dmaap adapter
145 # args: -
146 # (Function for test scripts)
147 use_cr_http() {
148         __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
149 }
150
151 # Set https as the protocol to use for all communication to the Dmaap adapter
152 # args: -
153 # (Function for test scripts)
154 use_cr_https() {
155         __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
156 }
157
158 # Setup paths to svc/container for internal and external access
159 # args: <protocol> <internal-port> <external-port>
160 __cr_set_protocoll() {
161
162         echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
163         echo -e " Using $BOLD $1 $EBOLD towards $CR_DISPLAY_NAME"
164         ## Access to Dmaap adapter
165         for ((CR_INSTANCE=0; CR_INSTANCE<$MAX_CR_APP_COUNT; CR_INSTANCE++ )); do
166                 CR_DOCKER_INSTANCE=$(($CR_INSTANCE+1))
167                 # CR_SERVICE_PATH is the base path to cr
168                 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
169                         __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"_cr_"${CR_DOCKER_INSTANCE}":"$2  # docker access, container->container and script->container via proxy
170                 else
171                         __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-cr-"${CR_DOCKER_INSTANCE}":"$2  # docker access, container->container and script->container via proxy
172                 fi
173                 if [ $RUNMODE == "KUBE" ]; then
174                         __CR_SERVICE_PATH=$1"://"$CR_APP_NAME"-"$CR_INSTANCE.$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
175                 fi
176                 export CR_SERVICE_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH
177                 # Service paths are used in test script to provide callbacck urls to app
178                 export CR_SERVICE_MR_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_MR  #Only for messages from dmaap adapter/mediator
179                 export CR_SERVICE_TEXT_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT  #Callbacks for text payload
180                 export CR_SERVICE_APP_PATH"_"${CR_INSTANCE}=$__CR_SERVICE_PATH$CR_APP_CALLBACK    #For general callbacks from apps
181
182                 if [ $CR_INSTANCE -eq 0 ]; then
183                         # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
184                         # CR_ADDAPTER need to be set before each call to CR....only set for instance 0 here
185                         CR_ADAPTER_TYPE="REST"
186                         CR_ADAPTER=$__CR_SERVICE_PATH
187                 fi
188         done
189         echo ""
190 }
191
192 # Export env vars for config files, docker compose and kube resources
193 # args: <proxy-flag>
194 __cr_export_vars() {
195         export CR_APP_NAME
196         export CR_DISPLAY_NAME
197
198         export KUBE_SIM_NAMESPACE
199         export DOCKER_SIM_NWNAME
200
201         export CR_IMAGE
202
203         export CR_INTERNAL_PORT
204         export CR_INTERNAL_SECURE_PORT
205         export CR_EXTERNAL_PORT
206         export CR_EXTERNAL_SECURE_PORT
207
208         export CR_APP_COUNT
209 }
210
211 # Start the Callback reciver in the simulator group
212 # args: <app-count>
213 # (Function for test scripts)
214 start_cr() {
215
216         echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
217
218         if [ $# -ne 1 ]; then
219                 echo -e $RED" Number of CR instances missing, usage: start_cr <app-count>"$ERED
220                 exit 1
221         fi
222         if [ $1 -lt 1 ] || [ $1 -gt 10 ]; then
223                 echo -e $RED" Number of CR shall be 1...10, usage: start_cr <app-count>"$ERED
224                 exit 1
225         fi
226         export CR_APP_COUNT=$1
227
228         if [ $RUNMODE == "KUBE" ]; then
229
230                 # Check if app shall be fully managed by the test script
231                 __check_included_image "CR"
232                 retcode_i=$?
233
234                 # Check if app shall only be used by the testscipt
235                 __check_prestarted_image "CR"
236                 retcode_p=$?
237
238                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
239                         echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
240                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
241                         exit
242                 fi
243                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
244                         echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
245                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
246                         exit
247                 fi
248
249                 # Check if app shall be used - not managed - by the test script
250                 if [ $retcode_p -eq 0 ]; then
251                         echo -e " Using existing $CR_APP_NAME deployment and service"
252                         echo " Setting CR replicas=1"
253                         __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
254                 fi
255
256                 if [ $retcode_i -eq 0 ]; then
257                         echo -e " Creating $CR_APP_NAME deployment and service"
258
259                         __cr_export_vars
260
261                         __kube_create_namespace $KUBE_SIM_NAMESPACE
262
263                         # Create service
264                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
265                         output_yaml=$PWD/tmp/cr_svc.yaml
266                         __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
267
268                         # Create app
269                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
270                         output_yaml=$PWD/tmp/cr_app.yaml
271                         __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
272
273                 fi
274
275                 for ((CR_INSTANCE=0; CR_INSTANCE<$CR_APP_COUNT; CR_INSTANCE++ )); do
276                         __dynvar="CR_SERVICE_PATH_"$CR_INSTANCE
277                         __cr_app_name=$CR_APP_NAME"-"$CR_INSTANCE
278                         __check_service_start $__cr_app_name ${!__dynvar}$CR_ALIVE_URL
279                         result=$(__do_curl ${!__dynvar}/reset)
280                 done
281
282         else
283                 # Check if docker app shall be fully managed by the test script
284                 __check_included_image 'CR'
285                 if [ $? -eq 1 ]; then
286                         echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
287                         echo -e $RED"The Callback Receiver will not be started"$ERED
288                         exit
289                 fi
290
291                 __cr_export_vars
292
293                 app_data=""
294                 cntr=1
295                 while [ $cntr -le $CR_APP_COUNT ]; do
296                         if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
297                                 app=$CR_APP_NAME"_cr_"$cntr
298                         else
299                                 app=$CR_APP_NAME"-cr-"$cntr
300                         fi
301                         app_data="$app_data $app"
302                         let cntr=cntr+1
303                 done
304
305                 echo "COMPOSE_PROJECT_NAME="$CR_APP_NAME > $SIM_GROUP/$CR_COMPOSE_DIR/.env
306
307                 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS $CR_APP_COUNT $app_data
308
309                 cntr=1   #Counter for docker instance, starts on 1
310                 cntr2=0  #Couter for env var name, starts with 0 to be compablible with kube
311                 while [ $cntr -le $CR_APP_COUNT ]; do
312                         if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
313                                 app=$CR_APP_NAME"_cr_"$cntr
314                         else
315                                 app=$CR_APP_NAME"-cr-"$cntr
316                         fi
317                         __dynvar="CR_SERVICE_PATH_"$cntr2
318                         __check_service_start $app ${!__dynvar}$CR_ALIVE_URL
319                         let cntr=cntr+1
320                         let cntr2=cntr2+1
321                 done
322         fi
323         echo ""
324 }
325
326 #Convert a cr path id to the value of the environment var holding the url
327 # arg: <cr-path-id>
328 # returns: <base-url-to-the-app>
329 __cr_get_service_path(){
330         if [ $# -ne 1 ]; then
331                 echo "DUMMY"
332                 return 1
333         fi
334         if [ $1 -lt 0 ] || [ $1 -ge $MAX_CR_APP_COUNT ]; then
335                 echo "DUMMY"
336                 return 1
337         fi
338         __dynvar="CR_SERVICE_PATH_"$1
339         echo ${!__dynvar}
340         return 0
341 }
342
343 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
344 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
345 # equal to the target or not.
346 # Arg: <cr-path-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
347 # before setting pass or fail depending on if the variable value becomes equal to the target
348 # value or not.
349 # (Function for test scripts)
350 cr_equal() {
351         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
352                 CR_SERVICE_PATH=$(__cr_get_service_path $1)
353                 CR_ADAPTER=$CR_SERVICE_PATH
354                 if [ $? -ne 0 ]; then
355                         __print_err "<cr-path-id> missing or incorrect" $@
356                         return 1
357                 fi
358                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "=" $3 $4
359         else
360                 __print_err "Wrong args to cr_equal, needs three or four args: <cr-path-id>  <variable-name> <target-value> [ timeout ]" $@
361         fi
362 }
363
364 # Tests if a variable value in the CR contains the target string and and optional timeout
365 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
366 # the target or not.
367 # Arg: <cr-path-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
368 # before setting pass or fail depending on if the variable value contains the target
369 # value or not.
370 # (Function for test scripts)
371 cr_contains_str() {
372
373         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
374                 CR_SERVICE_PATH=$(__cr_get_service_path $1)
375                 CR_ADAPTER=$CR_SERVICE_PATH
376                 if [ $? -ne 0 ]; then
377                         __print_err "<cr-path-id> missing or incorrect" $@
378                         return 1
379                 fi
380                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $2 "contain_str" $3 $4
381                 return 0
382         else
383                 __print_err "needs two or three args: <cr-path-id> <variable-name> <target-value> [ timeout ]"
384                 return 1
385         fi
386 }
387
388 # Read a variable value from CR sim and send to stdout. Arg: <variable-name>
389 cr_read() {
390         CR_SERVICE_PATH=$(__cr_get_service_path $1)
391         CR_ADAPTER=$CR_SERVICE_PATH
392         if [ $? -ne 0 ]; then
393                 __print_err "<cr-path-id> missing or incorrect" $@
394                 return  1
395         fi
396         echo "$(__do_curl $CR_SERVICE_PATH/counter/$1)"
397 }
398
399 # Function to configure write delay on callbacks
400 # Delay given in seconds.
401 # arg <response-code> <cr-path-id>  <delay-in-sec>
402 # (Function for test scripts)
403 cr_delay_callback() {
404         __log_conf_start $@
405
406         if [ $# -ne 3 ]; then
407         __print_err "<response-code> <cr-path-id> <delay-in-sec>]" $@
408         return 1
409         fi
410
411         CR_SERVICE_PATH=$(__cr_get_service_path $2)
412         CR_ADAPTER=$CR_SERVICE_PATH
413         if [ $? -ne 0 ]; then
414                 __print_err "<cr-path-id> missing or incorrect" $@
415                 return 1
416         fi
417
418         res="$(__do_curl_to_api CR POST /forcedelay?delay=$3)"
419         status=${res:${#res}-3}
420
421         if [ $status -ne 200 ]; then
422                 __log_conf_fail_status_code $1 $status
423                 return 1
424         fi
425
426         __log_conf_ok
427         return 0
428 }
429
430 # CR API: Check the contents of all current ric sync events for one id from PMS
431 # <response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]
432 # (Function for test scripts)
433 cr_api_check_all_sync_events() {
434         __log_test_start $@
435
436         if [ "$PMS_VERSION" != "V2" ]; then
437                 __log_test_fail_not_supported
438                 return 1
439         fi
440
441     if [ $# -lt 3 ]; then
442         __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <ric-id> )+ ]" $@
443         return 1
444     fi
445
446         CR_SERVICE_PATH=$(__cr_get_service_path $2)
447         CR_ADAPTER=$CR_SERVICE_PATH
448         if [ $? -ne 0 ]; then
449                 __print_err "<cr-path-id> missing or incorrect" $@
450                 return 1
451         fi
452
453         query="/get-all-events/"$3
454         res="$(__do_curl_to_api CR GET $query)"
455         status=${res:${#res}-3}
456
457         if [ $status -ne $1 ]; then
458                 __log_test_fail_status_code $1 $status
459                 return 1
460         fi
461
462         if [ $# -gt 3 ]; then
463                 body=${res:0:${#res}-3}
464                 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
465                         targetJson="["
466                 else
467                         targetJson="["
468                         arr=(${@:4})
469
470                         for ((i=0; i<$(($#-3)); i=i+1)); do
471
472                                 if [ "$targetJson" != "[" ]; then
473                                         targetJson=$targetJson","
474                                 fi
475                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
476                         done
477                 fi
478
479                 targetJson=$targetJson"]"
480                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
481                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
482
483                 if [ $res -ne 0 ]; then
484                         __log_test_fail_body
485                         return 1
486                 fi
487         fi
488         __log_test_pass
489         return 0
490 }
491
492 # CR API: Check the contents of all current status events for one id from ICS
493 # <response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]
494 # (Function for test scripts)
495 cr_api_check_all_ics_events() {
496         __log_test_start $@
497
498     if [ $# -lt 3 ]; then
499         __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <status> )+ ]" $@
500         return 1
501     fi
502
503         CR_SERVICE_PATH=$(__cr_get_service_path $2)
504         CR_ADAPTER=$CR_SERVICE_PATH
505         if [ $? -ne 0 ]; then
506                 __print_err "<cr-path-id> missing or incorrect" $@
507                 return 1
508         fi
509
510         query="/get-all-events/"$3
511         res="$(__do_curl_to_api CR GET $query)"
512         status=${res:${#res}-3}
513
514         if [ $status -ne $1 ]; then
515                 __log_test_fail_status_code $1 $status
516                 return 1
517         fi
518
519         if [ $# -gt 3 ]; then
520                 body=${res:0:${#res}-3}
521                 if [ $# -eq 4 ] && [ $4 == "EMPTY" ]; then
522                         targetJson="["
523                 else
524                         targetJson="["
525                         arr=(${@:4})
526
527                         for ((i=0; i<$(($#-3)); i=i+1)); do
528
529                                 if [ "$targetJson" != "[" ]; then
530                                         targetJson=$targetJson","
531                                 fi
532                                 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
533                         done
534                 fi
535
536                 targetJson=$targetJson"]"
537                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
538                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
539
540                 if [ $res -ne 0 ]; then
541                         __log_test_fail_body
542                         return 1
543                 fi
544         fi
545         __log_test_pass
546         return 0
547 }
548
549 # CR API: Check the contents of all current type subscription events for one id from ICS
550 # <response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
551 # (Function for test scripts)
552 cr_api_check_all_ics_subscription_events() {
553         __log_test_start $@
554
555         #Valid number of parameter 3,4,8,12
556         paramError=1
557         if [ $# -eq 3 ]; then
558                 paramError=0
559         fi
560         if [ $# -eq 4 ] && [ "$4" == "EMPTY" ]; then
561                 paramError=0
562         fi
563         variablecount=$(($#-3))
564         if [ $# -gt 4 ] && [ $(($variablecount%3)) -eq 0 ]; then
565                 paramError=0
566         fi
567         if [ $paramError -eq 1 ]; then
568                 __print_err "<response-code> <cr-path-id> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
569                 return 1
570         fi
571
572         CR_SERVICE_PATH=$(__cr_get_service_path $2)
573         CR_ADAPTER=$CR_SERVICE_PATH
574         if [ $? -ne 0 ]; then
575                 __print_err "<cr-path-id> missing or incorrect" $@
576                 return 1
577         fi
578
579         query="/get-all-events/"$3
580         res="$(__do_curl_to_api CR GET $query)"
581         status=${res:${#res}-3}
582
583         if [ $status -ne $1 ]; then
584                 __log_test_fail_status_code $1 $status
585                 return 1
586         fi
587
588         if [ $# -gt 3 ]; then
589                 body=${res:0:${#res}-3}
590                 targetJson="["
591                 if [ $# -gt 4 ]; then
592                         arr=(${@:4})
593                         for ((i=0; i<$(($#-4)); i=i+3)); do
594                                 if [ "$targetJson" != "[" ]; then
595                                         targetJson=$targetJson","
596                                 fi
597                                 if [ -f ${arr[$i+1]} ]; then
598                                         jobfile=$(cat ${arr[$i+1]})
599                                 else
600                                         __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
601                                         return 1
602                                 fi
603                                 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
604                         done
605                 fi
606                 targetJson=$targetJson"]"
607
608                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
609                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
610
611                 if [ $res -ne 0 ]; then
612                         __log_test_fail_body
613                         return 1
614                 fi
615         fi
616
617         __log_test_pass
618         return 0
619 }
620
621
622 # CR API: Reset all events and counters
623 # Arg: <cr-path-id>
624 # (Function for test scripts)
625 cr_api_reset() {
626         __log_conf_start $@
627
628         if [ $# -ne 1 ]; then
629                 __print_err "<cr-path-id>" $@
630                 return 1
631         fi
632
633         CR_SERVICE_PATH=$(__cr_get_service_path $1)
634         CR_ADAPTER=$CR_SERVICE_PATH
635         if [ $? -ne 0 ]; then
636                 __print_err "<cr-path-id> missing or incorrect" $@
637                 return 1
638         fi
639
640         res="$(__do_curl_to_api CR GET /reset)"
641         status=${res:${#res}-3}
642
643         if [ $status -ne 200 ]; then
644                 __log_conf_fail_status_code $1 $status
645                 return 1
646         fi
647
648         __log_conf_ok
649         return 0
650 }
651
652
653 # CR API: Check the contents of all json events for path
654 # <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg>+ )
655 # (Function for test scripts)
656 cr_api_check_all_genric_json_events() {
657         __log_test_start $@
658
659         if [ $# -lt 4 ]; then
660                 __print_err "<response-code> <cr-path-id>  <topic-url> (EMPTY | <json-msg>+ )" $@
661                 return 1
662         fi
663
664         CR_SERVICE_PATH=$(__cr_get_service_path $2)
665         CR_ADAPTER=$CR_SERVICE_PATH
666         if [ $? -ne 0 ]; then
667                 __print_err "<cr-path-id> missing or incorrect" $@
668                 return 1
669         fi
670
671         query="/get-all-events/"$3
672         res="$(__do_curl_to_api CR GET $query)"
673         status=${res:${#res}-3}
674
675         if [ $status -ne $1 ]; then
676                 __log_test_fail_status_code $1 $status
677                 return 1
678         fi
679         body=${res:0:${#res}-3}
680         targetJson="["
681
682         if [ $4 != "EMPTY" ]; then
683                 shift
684                 shift
685                 shift
686                 while [ $# -gt 0 ]; do
687                         if [ "$targetJson" != "[" ]; then
688                                 targetJson=$targetJson","
689                         fi
690                         targetJson=$targetJson$1
691                         shift
692                 done
693         fi
694         targetJson=$targetJson"]"
695
696         echo " TARGET JSON: $targetJson" >> $HTTPLOG
697         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
698
699         if [ $res -ne 0 ]; then
700                 __log_test_fail_body
701                 return 1
702         fi
703
704         __log_test_pass
705         return 0
706 }
707
708
709 # CR API: Check a single (oldest) json event (or none if empty) for path
710 # <response-code> <cr-path-id> <topic-url> (EMPTY | <json-msg> )
711 # (Function for test scripts)
712 cr_api_check_single_genric_json_event() {
713         __log_test_start $@
714
715         if [ $# -ne 4 ]; then
716                 __print_err "<response-code> <cr-path-id>  <topic-url> (EMPTY | <json-msg> )" $@
717                 return 1
718         fi
719
720         CR_SERVICE_PATH=$(__cr_get_service_path $2)
721         CR_ADAPTER=$CR_SERVICE_PATH
722         if [ $? -ne 0 ]; then
723                 __print_err "<cr-path-id> missing or incorrect" $@
724                 return 1
725         fi
726
727         query="/get-event/"$3
728         res="$(__do_curl_to_api CR GET $query)"
729         status=${res:${#res}-3}
730
731         if [ $status -ne $1 ]; then
732                 __log_test_fail_status_code $1 $status
733                 return 1
734         fi
735         body=${res:0:${#res}-3}
736         targetJson=$4
737
738         if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then
739                 __log_test_fail_body
740                 return 1
741         fi
742         echo " TARGET JSON: $targetJson" >> $HTTPLOG
743         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
744
745         if [ $res -ne 0 ]; then
746                 __log_test_fail_body
747                 return 1
748         fi
749
750         __log_test_pass
751         return 0
752 }
753
754 # CR API: Check a single (oldest) json in md5 format (or none if empty) for path.
755 # Note that if a json message is given, it shall be compact, no ws except inside string.
756 # The MD5 will generate different hash if ws is present or not in otherwise equivalent json
757 # arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )
758 # (Function for test scripts)
759 cr_api_check_single_genric_event_md5() {
760         __log_test_start $@
761
762         if [ $# -ne 4 ]; then
763                 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-msg> )" $@
764                 return 1
765         fi
766
767         CR_SERVICE_PATH=$(__cr_get_service_path $2)
768         CR_ADAPTER=$CR_SERVICE_PATH
769         if [ $? -ne 0 ]; then
770                 __print_err "<cr-path-id> missing or incorrect" $@
771                 return 1
772         fi
773
774         query="/get-event/"$3
775         res="$(__do_curl_to_api CR GET $query)"
776         status=${res:${#res}-3}
777
778         if [ $status -ne $1 ]; then
779                 __log_test_fail_status_code $1 $status
780                 return 1
781         fi
782         body=${res:0:${#res}-3}
783         if [ $4 == "EMPTY" ]; then
784                 if [ ${#body} -ne 0 ]; then
785                         __log_test_fail_body
786                         return 1
787                 else
788                         __log_test_pass
789                         return 0
790                 fi
791         fi
792         command -v md5 > /dev/null # Mac
793         if [ $? -eq 0 ]; then
794                 targetMd5=$(echo -n "$4" | md5)
795         else
796                 command -v md5sum > /dev/null # Linux
797                 if [ $? -eq 0 ]; then
798                         targetMd5=$(echo -n "$4" | md5sum | cut -d' ' -f 1)  # Need to cut additional info printed by cmd
799                 else
800                         __log_test_fail_general "Command md5 nor md5sum is available"
801                         return 1
802                 fi
803         fi
804         targetMd5="\""$targetMd5"\"" #Quotes needed
805
806         echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
807
808         if [ "$body" != "$targetMd5" ]; then
809                 __log_test_fail_body
810                 return 1
811         fi
812
813         __log_test_pass
814         return 0
815 }
816
817 # CR API: Check a single (oldest) event in md5 format (or none if empty) for path.
818 # Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines.
819 # The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json
820 # arg: <response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )
821 # (Function for test scripts)
822 cr_api_check_single_genric_event_md5_file() {
823         __log_test_start $@
824
825         if [ $# -ne 4 ]; then
826                 __print_err "<response-code> <cr-path-id> <topic-url> (EMPTY | <data-file> )" $@
827                 return 1
828         fi
829
830         CR_SERVICE_PATH=$(__cr_get_service_path $2)
831         CR_ADAPTER=$CR_SERVICE_PATH
832         if [ $? -ne 0 ]; then
833                 __print_err "<cr-path-id> missing or incorrect" $@
834                 return 1
835         fi
836
837         query="/get-event/"$3
838         res="$(__do_curl_to_api CR GET $query)"
839         status=${res:${#res}-3}
840
841         if [ $status -ne $1 ]; then
842                 __log_test_fail_status_code $1 $status
843                 return 1
844         fi
845         body=${res:0:${#res}-3}
846         if [ $4 == "EMPTY" ]; then
847                 if [ ${#body} -ne 0 ]; then
848                         __log_test_fail_body
849                         return 1
850                 else
851                         __log_test_pass
852                         return 0
853                 fi
854         fi
855
856         if [ ! -f $4 ]; then
857                 __log_test_fail_general "File $3 does not exist"
858                 return 1
859         fi
860
861         filedata=$(cat $4)
862
863         command -v md5 > /dev/null # Mac
864         if [ $? -eq 0 ]; then
865                 targetMd5=$(echo -n "$filedata" | md5)
866         else
867                 command -v md5sum > /dev/null # Linux
868                 if [ $? -eq 0 ]; then
869                         targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1)  # Need to cut additional info printed by cmd
870                 else
871                         __log_test_fail_general "Command md5 nor md5sum is available"
872                         return 1
873                 fi
874         fi
875         targetMd5="\""$targetMd5"\""   #Quotes needed
876
877         echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
878
879         if [ "$body" != "$targetMd5" ]; then
880                 __log_test_fail_body
881                 return 1
882         fi
883
884         __log_test_pass
885         return 0
886 }