a537bc85bac40ec83ddc8ee24a4ee1d5537bb2ae
[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                 kubectl  logs -l "autotest=CR" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_cr.log 2>&1
98         else
99                 docker logs $CR_APP_NAME > $1$2_cr.log 2>&1
100         fi
101 }
102
103 # Initial setup of protocol, host and ports
104 # This function is called for apps managed by the test script.
105 # args: -
106 __CR_initial_setup() {
107         use_cr_http
108 }
109
110 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
111 # For docker, the namespace shall be excluded
112 # This function is called for apps managed by the test script as well as for prestarted apps.
113 # args: -
114 __CR_statisics_setup() {
115         if [ $RUNMODE == "KUBE" ]; then
116                 echo "CR $CR_APP_NAME $KUBE_SIM_NAMESPACE"
117         else
118                 echo "CR $CR_APP_NAME"
119         fi
120 }
121
122 #######################################################
123
124 ################
125 ### CR functions
126 ################
127
128 # Set http as the protocol to use for all communication to the Dmaap adapter
129 # args: -
130 # (Function for test scripts)
131 use_cr_http() {
132         __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
133 }
134
135 # Set https as the protocol to use for all communication to the Dmaap adapter
136 # args: -
137 # (Function for test scripts)
138 use_cr_https() {
139         __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
140 }
141
142 # Setup paths to svc/container for internal and external access
143 # args: <protocol> <internal-port> <external-port>
144 __cr_set_protocoll() {
145         echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
146         echo -e " Using $BOLD http $EBOLD towards $CR_DISPLAY_NAME"
147
148         ## Access to Dmaap adapter
149
150         # CR_SERVICE_PATH is the base path to cr
151         CR_SERVICE_PATH=$1"://"$CR_APP_NAME":"$2  # docker access, container->container and script->container via proxy
152         if [ $RUNMODE == "KUBE" ]; then
153                 CR_SERVICE_PATH=$1"://"$CR_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
154         fi
155         # Service paths are used in test script to provide callbacck urls to app
156         CR_SERVICE_MR_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK_MR  #Only for messages from dmaap adapter/mediator
157         CR_SERVICE_TEXT_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT  #Callbacks for text payload
158         CR_SERVICE_APP_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK    #For general callbacks from apps
159
160         # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
161         CR_ADAPTER_TYPE="REST"
162         CR_ADAPTER=$CR_SERVICE_PATH
163
164         echo ""
165 }
166
167 # Export env vars for config files, docker compose and kube resources
168 # args: <proxy-flag>
169 __cr_export_vars() {
170         export CR_APP_NAME
171         export CR_DISPLAY_NAME
172
173         export KUBE_SIM_NAMESPACE
174         export DOCKER_SIM_NWNAME
175
176         export CR_IMAGE
177
178         export CR_INTERNAL_PORT
179         export CR_INTERNAL_SECURE_PORT
180         export CR_EXTERNAL_PORT
181         export CR_EXTERNAL_SECURE_PORT
182 }
183
184 # Start the Callback reciver in the simulator group
185 # args: -
186 # (Function for test scripts)
187 start_cr() {
188
189         echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
190
191         if [ $RUNMODE == "KUBE" ]; then
192
193                 # Check if app shall be fully managed by the test script
194                 __check_included_image "CR"
195                 retcode_i=$?
196
197                 # Check if app shall only be used by the testscipt
198                 __check_prestarted_image "CR"
199                 retcode_p=$?
200
201                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
202                         echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
203                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
204                         exit
205                 fi
206                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
207                         echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
208                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
209                         exit
210                 fi
211
212                 # Check if app shall be used - not managed - by the test script
213                 if [ $retcode_p -eq 0 ]; then
214                         echo -e " Using existing $CR_APP_NAME deployment and service"
215                         echo " Setting CR replicas=1"
216                         __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
217                 fi
218
219                 if [ $retcode_i -eq 0 ]; then
220                         echo -e " Creating $CR_APP_NAME deployment and service"
221
222                         __cr_export_vars
223
224                         __kube_create_namespace $KUBE_SIM_NAMESPACE
225
226                         # Create service
227                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
228                         output_yaml=$PWD/tmp/cr_svc.yaml
229                         __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
230
231                         # Create app
232                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
233                         output_yaml=$PWD/tmp/cr_app.yaml
234                         __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
235
236                 fi
237
238                 __check_service_start $CR_APP_NAME $CR_SERVICE_PATH$CR_ALIVE_URL
239
240                 echo -ne " Service $CR_APP_NAME - reset  "$SAMELINE
241                 result=$(__do_curl CR $CR_SERVICE_PATH/reset)
242                 if [ $? -ne 0 ]; then
243                         echo -e " Service $CR_APP_NAME - reset  $RED Failed $ERED - will continue"
244                 else
245                         echo -e " Service $CR_APP_NAME - reset  $GREEN OK $EGREEN"
246                 fi
247         else
248                 # Check if docker app shall be fully managed by the test script
249                 __check_included_image 'CR'
250                 if [ $? -eq 1 ]; then
251                         echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
252                         echo -e $RED"The Callback Receiver will not be started"$ERED
253                         exit
254                 fi
255
256                 __cr_export_vars
257
258                 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS 1 $CR_APP_NAME
259
260         __check_service_start $CR_APP_NAME $CR_SERVICE_PATH$CR_ALIVE_URL
261         fi
262         echo ""
263 }
264
265
266 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
267 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
268 # equal to the target or not.
269 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
270 # before setting pass or fail depending on if the variable value becomes equal to the target
271 # value or not.
272 # (Function for test scripts)
273 cr_equal() {
274         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
275                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $1 "=" $2 $3
276         else
277                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
278         fi
279 }
280
281 # Tests if a variable value in the CR contains the target string and and optional timeout
282 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
283 # the target or not.
284 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
285 # before setting pass or fail depending on if the variable value contains the target
286 # value or not.
287 # (Function for test scripts)
288 cr_contains_str() {
289
290         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
291                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $1 "contain_str" $2 $3
292                 return 0
293         else
294                 __print_err "needs two or three args: <sim-param> <target-value> [ timeout ]"
295                 return 1
296         fi
297 }
298
299 # Read a variable value from CR sim and send to stdout. Arg: <variable-name>
300 cr_read() {
301         echo "$(__do_curl $CR_SERVICE_PATH/counter/$1)"
302 }
303
304 # Function to configure write delay on callbacks
305 # Delay given in seconds.
306 # arg <response-code> <delay-in-sec>
307 # (Function for test scripts)
308 cr_delay_callback() {
309         __log_conf_start $@
310
311         if [ $# -ne 2 ]; then
312         __print_err "<response-code> <delay-in-sec>]" $@
313         return 1
314         fi
315
316         res="$(__do_curl_to_api CR POST /forcedelay?delay=$2)"
317         status=${res:${#res}-3}
318
319         if [ $status -ne 200 ]; then
320                 __log_conf_fail_status_code $1 $status
321                 return 1
322         fi
323
324         __log_conf_ok
325         return 0
326 }
327
328 # CR API: Check the contents of all current ric sync events for one id from PMS
329 # <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
330 # (Function for test scripts)
331 cr_api_check_all_sync_events() {
332         __log_test_start $@
333
334         if [ "$PMS_VERSION" != "V2" ]; then
335                 __log_test_fail_not_supported
336                 return 1
337         fi
338
339     if [ $# -lt 2 ]; then
340         __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
341         return 1
342     fi
343
344         query="/get-all-events/"$2
345         res="$(__do_curl_to_api CR GET $query)"
346         status=${res:${#res}-3}
347
348         if [ $status -ne $1 ]; then
349                 __log_test_fail_status_code $1 $status
350                 return 1
351         fi
352
353         if [ $# -gt 2 ]; then
354                 body=${res:0:${#res}-3}
355                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
356                         targetJson="["
357                 else
358                         targetJson="["
359                         arr=(${@:3})
360
361                         for ((i=0; i<$(($#-2)); i=i+1)); do
362
363                                 if [ "$targetJson" != "[" ]; then
364                                         targetJson=$targetJson","
365                                 fi
366                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
367                         done
368                 fi
369
370                 targetJson=$targetJson"]"
371                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
372                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
373
374                 if [ $res -ne 0 ]; then
375                         __log_test_fail_body
376                         return 1
377                 fi
378         fi
379         __log_test_pass
380         return 0
381 }
382
383 # CR API: Check the contents of all current status events for one id from ECS
384 # <response-code> <id> [ EMPTY | ( <status> )+ ]
385 # (Function for test scripts)
386 cr_api_check_all_ecs_events() {
387         __log_test_start $@
388
389     if [ $# -lt 2 ]; then
390         __print_err "<response-code> <id> [ EMPTY | ( <status> )+ ]" $@
391         return 1
392     fi
393
394         query="/get-all-events/"$2
395         res="$(__do_curl_to_api CR GET $query)"
396         status=${res:${#res}-3}
397
398         if [ $status -ne $1 ]; then
399                 __log_test_fail_status_code $1 $status
400                 return 1
401         fi
402
403         if [ $# -gt 2 ]; then
404                 body=${res:0:${#res}-3}
405                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
406                         targetJson="["
407                 else
408                         targetJson="["
409                         arr=(${@:3})
410
411                         for ((i=0; i<$(($#-2)); i=i+1)); do
412
413                                 if [ "$targetJson" != "[" ]; then
414                                         targetJson=$targetJson","
415                                 fi
416                                 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
417                         done
418                 fi
419
420                 targetJson=$targetJson"]"
421                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
422                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
423
424                 if [ $res -ne 0 ]; then
425                         __log_test_fail_body
426                         return 1
427                 fi
428         fi
429         __log_test_pass
430         return 0
431 }
432
433 # CR API: Check the contents of all current type subscription events for one id from ECS
434 # <response-code> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
435 # (Function for test scripts)
436 cr_api_check_all_ecs_subscription_events() {
437         __log_test_start $@
438
439         #Valid number of parameter 2,3,7,11
440         paramError=1
441         if [ $# -eq 2 ]; then
442                 paramError=0
443         fi
444         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
445                 paramError=0
446         fi
447         variablecount=$(($#-2))
448         if [ $# -gt 3 ] && [ $(($variablecount%3)) -eq 0 ]; then
449                 paramError=0
450         fi
451         if [ $paramError -eq 1 ]; then
452                 __print_err "<response-code> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
453                 return 1
454         fi
455
456         query="/get-all-events/"$2
457         res="$(__do_curl_to_api CR GET $query)"
458         status=${res:${#res}-3}
459
460         if [ $status -ne $1 ]; then
461                 __log_test_fail_status_code $1 $status
462                 return 1
463         fi
464
465         if [ $# -gt 2 ]; then
466                 body=${res:0:${#res}-3}
467                 targetJson="["
468                 if [ $# -gt 3 ]; then
469                         arr=(${@:3})
470                         for ((i=0; i<$(($#-3)); i=i+3)); do
471                                 if [ "$targetJson" != "[" ]; then
472                                         targetJson=$targetJson","
473                                 fi
474                                 if [ -f ${arr[$i+1]} ]; then
475                                         jobfile=$(cat ${arr[$i+1]})
476                                 else
477                                         __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
478                                         return 1
479                                 fi
480                                 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
481                         done
482                 fi
483                 targetJson=$targetJson"]"
484
485                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
486                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
487
488                 if [ $res -ne 0 ]; then
489                         __log_test_fail_body
490                         return 1
491                 fi
492         fi
493
494         __log_test_pass
495         return 0
496 }
497
498
499 # CR API: Reset all events and counters
500 # Arg: -
501 # (Function for test scripts)
502 cr_api_reset() {
503         __log_conf_start $@
504
505         res="$(__do_curl_to_api CR GET /reset)"
506         status=${res:${#res}-3}
507
508         if [ $status -ne 200 ]; then
509                 __log_conf_fail_status_code $1 $status
510                 return 1
511         fi
512
513         __log_conf_ok
514         return 0
515 }
516
517
518 # CR API: Check the contents of all json events for path
519 # <response-code> <topic-url> (EMPTY | <json-msg>+ )
520 # (Function for test scripts)
521 cr_api_check_all_genric_json_events() {
522         __log_test_start $@
523
524         if [ $# -lt 3 ]; then
525                 __print_err "<response-code> <topic-url> (EMPTY | <json-msg>+ )" $@
526                 return 1
527         fi
528
529         query="/get-all-events/"$2
530         res="$(__do_curl_to_api CR GET $query)"
531         status=${res:${#res}-3}
532
533         if [ $status -ne $1 ]; then
534                 __log_test_fail_status_code $1 $status
535                 return 1
536         fi
537         body=${res:0:${#res}-3}
538         targetJson="["
539
540         if [ $3 != "EMPTY" ]; then
541                 shift
542                 shift
543                 while [ $# -gt 0 ]; do
544                         if [ "$targetJson" != "[" ]; then
545                                 targetJson=$targetJson","
546                         fi
547                         targetJson=$targetJson$1
548                         shift
549                 done
550         fi
551         targetJson=$targetJson"]"
552
553         echo " TARGET JSON: $targetJson" >> $HTTPLOG
554         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
555
556         if [ $res -ne 0 ]; then
557                 __log_test_fail_body
558                 return 1
559         fi
560
561         __log_test_pass
562         return 0
563 }
564
565
566
567 # CR API: Check a single (oldest) json event (or none if empty) for path
568 # <response-code> <topic-url> (EMPTY | <json-msg> )
569 # (Function for test scripts)
570 cr_api_check_single_genric_json_event() {
571         __log_test_start $@
572
573         if [ $# -ne 3 ]; then
574                 __print_err "<response-code> <topic-url> (EMPTY | <json-msg> )" $@
575                 return 1
576         fi
577
578         query="/get-event/"$2
579         res="$(__do_curl_to_api CR GET $query)"
580         status=${res:${#res}-3}
581
582         if [ $status -ne $1 ]; then
583                 __log_test_fail_status_code $1 $status
584                 return 1
585         fi
586         body=${res:0:${#res}-3}
587         targetJson=$3
588
589         if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then
590                 __log_test_fail_body
591                 return 1
592         fi
593         echo " TARGET JSON: $targetJson" >> $HTTPLOG
594         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
595
596         if [ $res -ne 0 ]; then
597                 __log_test_fail_body
598                 return 1
599         fi
600
601         __log_test_pass
602         return 0
603 }
604
605 # CR API: Check a single (oldest) json in md5 format (or none if empty) for path.
606 # Note that if a json message is given, it shall be compact, no ws except inside string.
607 # The MD5 will generate different hash if ws is present or not in otherwise equivalent json
608 # arg: <response-code> <topic-url> (EMPTY | <data-msg> )
609 # (Function for test scripts)
610 cr_api_check_single_genric_event_md5() {
611         __log_test_start $@
612
613         if [ $# -ne 3 ]; then
614                 __print_err "<response-code> <topic-url> (EMPTY | <data-msg> )" $@
615                 return 1
616         fi
617
618         query="/get-event/"$2
619         res="$(__do_curl_to_api CR GET $query)"
620         status=${res:${#res}-3}
621
622         if [ $status -ne $1 ]; then
623                 __log_test_fail_status_code $1 $status
624                 return 1
625         fi
626         body=${res:0:${#res}-3}
627         if [ $3 == "EMPTY" ]; then
628                 if [ ${#body} -ne 0 ]; then
629                         __log_test_fail_body
630                         return 1
631                 else
632                         __log_test_pass
633                         return 0
634                 fi
635         fi
636         command -v md5 > /dev/null # Mac
637         if [ $? -eq 0 ]; then
638                 targetMd5=$(echo -n "$3" | md5)
639         else
640                 command -v md5sum > /dev/null # Linux
641                 if [ $? -eq 0 ]; then
642                         targetMd5=$(echo -n "$3" | md5sum | cut -d' ' -f 1)  # Need to cut additional info printed by cmd
643                 else
644                         __log_test_fail_general "Command md5 nor md5sum is available"
645                         return 1
646                 fi
647         fi
648         targetMd5="\""$targetMd5"\"" #Quotes needed
649
650         echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
651
652         if [ "$body" != "$targetMd5" ]; then
653                 __log_test_fail_body
654                 return 1
655         fi
656
657         __log_test_pass
658         return 0
659 }
660
661 # CR API: Check a single (oldest) event in md5 format (or none if empty) for path.
662 # Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines.
663 # The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json
664 # arg: <response-code> <topic-url> (EMPTY | <data-file> )
665 # (Function for test scripts)
666 cr_api_check_single_genric_event_md5_file() {
667         __log_test_start $@
668
669         if [ $# -ne 3 ]; then
670                 __print_err "<response-code> <topic-url> (EMPTY | <data-file> )" $@
671                 return 1
672         fi
673
674         query="/get-event/"$2
675         res="$(__do_curl_to_api CR GET $query)"
676         status=${res:${#res}-3}
677
678         if [ $status -ne $1 ]; then
679                 __log_test_fail_status_code $1 $status
680                 return 1
681         fi
682         body=${res:0:${#res}-3}
683         if [ $3 == "EMPTY" ]; then
684                 if [ ${#body} -ne 0 ]; then
685                         __log_test_fail_body
686                         return 1
687                 else
688                         __log_test_pass
689                         return 0
690                 fi
691         fi
692
693         if [ ! -f $3 ]; then
694                 __log_test_fail_general "File $3 does not exist"
695                 return 1
696         fi
697
698         filedata=$(cat $3)
699
700         command -v md5 > /dev/null # Mac
701         if [ $? -eq 0 ]; then
702                 targetMd5=$(echo -n "$filedata" | md5)
703         else
704                 command -v md5sum > /dev/null # Linux
705                 if [ $? -eq 0 ]; then
706                         targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1)  # Need to cut additional info printed by cmd
707                 else
708                         __log_test_fail_general "Command md5 nor md5sum is available"
709                         return 1
710                 fi
711         fi
712         targetMd5="\""$targetMd5"\""   #Quotes needed
713
714         echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
715
716         if [ "$body" != "$targetMd5" ]; then
717                 __log_test_fail_body
718                 return 1
719         fi
720
721         __log_test_pass
722         return 0
723 }