Merge "NONRTRIC - Implement DMaaP mediator producer service in Java"
[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 #######################################################
111
112 ################
113 ### CR functions
114 ################
115
116 # Set http as the protocol to use for all communication to the Dmaap adapter
117 # args: -
118 # (Function for test scripts)
119 use_cr_http() {
120         __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
121 }
122
123 # Set https as the protocol to use for all communication to the Dmaap adapter
124 # args: -
125 # (Function for test scripts)
126 use_cr_https() {
127         __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
128 }
129
130 # Setup paths to svc/container for internal and external access
131 # args: <protocol> <internal-port> <external-port>
132 __cr_set_protocoll() {
133         echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
134         echo -e " Using $BOLD http $EBOLD towards $CR_DISPLAY_NAME"
135
136         ## Access to Dmaap adapter
137
138         # CR_SERVICE_PATH is the base path to cr
139         CR_SERVICE_PATH=$1"://"$CR_APP_NAME":"$2  # docker access, container->container and script->container via proxy
140         if [ $RUNMODE == "KUBE" ]; then
141                 CR_SERVICE_PATH=$1"://"$CR_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
142         fi
143         # Service paths are used in test script to provide callbacck urls to app
144         CR_SERVICE_MR_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK_MR  #Only for messages from dmaap adapter/mediator
145         CR_SERVICE_APP_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK    #For general callbacks from apps
146
147         # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
148         CR_ADAPTER_TYPE="REST"
149         CR_ADAPTER=$CR_SERVICE_PATH
150
151         echo ""
152 }
153
154 # Export env vars for config files, docker compose and kube resources
155 # args: <proxy-flag>
156 __cr_export_vars() {
157         export CR_APP_NAME
158         export CR_DISPLAY_NAME
159
160         export KUBE_SIM_NAMESPACE
161         export DOCKER_SIM_NWNAME
162
163         export CR_IMAGE
164
165         export CR_INTERNAL_PORT
166         export CR_INTERNAL_SECURE_PORT
167         export CR_EXTERNAL_PORT
168         export CR_EXTERNAL_SECURE_PORT
169 }
170
171 # Start the Callback reciver in the simulator group
172 # args: -
173 # (Function for test scripts)
174 start_cr() {
175
176         echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
177
178         if [ $RUNMODE == "KUBE" ]; then
179
180                 # Check if app shall be fully managed by the test script
181                 __check_included_image "CR"
182                 retcode_i=$?
183
184                 # Check if app shall only be used by the testscipt
185                 __check_prestarted_image "CR"
186                 retcode_p=$?
187
188                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
189                         echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
190                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
191                         exit
192                 fi
193                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
194                         echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
195                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
196                         exit
197                 fi
198
199                 # Check if app shall be used - not managed - by the test script
200                 if [ $retcode_p -eq 0 ]; then
201                         echo -e " Using existing $CR_APP_NAME deployment and service"
202                         echo " Setting CR replicas=1"
203                         __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
204                 fi
205
206                 if [ $retcode_i -eq 0 ]; then
207                         echo -e " Creating $CR_APP_NAME deployment and service"
208
209                         __cr_export_vars
210
211                         __kube_create_namespace $KUBE_SIM_NAMESPACE
212
213                         # Create service
214                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
215                         output_yaml=$PWD/tmp/cr_svc.yaml
216                         __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
217
218                         # Create app
219                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
220                         output_yaml=$PWD/tmp/cr_app.yaml
221                         __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
222
223                 fi
224
225                 __check_service_start $CR_APP_NAME $CR_SERVICE_PATH$CR_ALIVE_URL
226
227                 echo -ne " Service $CR_APP_NAME - reset  "$SAMELINE
228                 result=$(__do_curl CR $CR_SERVICE_PATH/reset)
229                 if [ $? -ne 0 ]; then
230                         echo -e " Service $CR_APP_NAME - reset  $RED Failed $ERED - will continue"
231                 else
232                         echo -e " Service $CR_APP_NAME - reset  $GREEN OK $EGREEN"
233                 fi
234         else
235                 # Check if docker app shall be fully managed by the test script
236                 __check_included_image 'CR'
237                 if [ $? -eq 1 ]; then
238                         echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
239                         echo -e $RED"The Callback Receiver will not be started"$ERED
240                         exit
241                 fi
242
243                 __cr_export_vars
244
245                 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS 1 $CR_APP_NAME
246
247         __check_service_start $CR_APP_NAME $CR_SERVICE_PATH$CR_ALIVE_URL
248         fi
249         echo ""
250 }
251
252
253 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
254 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
255 # equal to the target or not.
256 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
257 # before setting pass or fail depending on if the variable value becomes equal to the target
258 # value or not.
259 # (Function for test scripts)
260 cr_equal() {
261         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
262                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $1 "=" $2 $3
263         else
264                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
265         fi
266 }
267
268 # Tests if a variable value in the CR contains the target string and and optional timeout
269 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
270 # the target or not.
271 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
272 # before setting pass or fail depending on if the variable value contains the target
273 # value or not.
274 # (Function for test scripts)
275 cr_contains_str() {
276
277         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
278                 __var_test "CR" "$CR_SERVICE_PATH/counter/" $1 "contain_str" $2 $3
279                 return 0
280         else
281                 __print_err "needs two or three args: <sim-param> <target-value> [ timeout ]"
282                 return 1
283         fi
284 }
285
286 # Read a variable value from CR sim and send to stdout. Arg: <variable-name>
287 cr_read() {
288         echo "$(__do_curl $CR_SERVICE_PATH/counter/$1)"
289 }
290
291 # Function to configure write delay on callbacks
292 # Delay given in seconds.
293 # arg <response-code> <delay-in-sec>
294 # (Function for test scripts)
295 cr_delay_callback() {
296         __log_conf_start $@
297
298         if [ $# -ne 2 ]; then
299         __print_err "<response-code> <delay-in-sec>]" $@
300         return 1
301         fi
302
303         res="$(__do_curl_to_api CR POST /forcedelay?delay=$2)"
304         status=${res:${#res}-3}
305
306         if [ $status -ne 200 ]; then
307                 __log_conf_fail_status_code $1 $status
308                 return 1
309         fi
310
311         __log_conf_ok
312         return 0
313 }
314
315 # CR API: Check the contents of all current ric sync events for one id from PMS
316 # <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
317 # (Function for test scripts)
318 cr_api_check_all_sync_events() {
319         __log_test_start $@
320
321         if [ "$PMS_VERSION" != "V2" ]; then
322                 __log_test_fail_not_supported
323                 return 1
324         fi
325
326     if [ $# -lt 2 ]; then
327         __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
328         return 1
329     fi
330
331         query="/get-all-events/"$2
332         res="$(__do_curl_to_api CR GET $query)"
333         status=${res:${#res}-3}
334
335         if [ $status -ne $1 ]; then
336                 __log_test_fail_status_code $1 $status
337                 return 1
338         fi
339
340         if [ $# -gt 2 ]; then
341                 body=${res:0:${#res}-3}
342                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
343                         targetJson="["
344                 else
345                         targetJson="["
346                         arr=(${@:3})
347
348                         for ((i=0; i<$(($#-2)); i=i+1)); do
349
350                                 if [ "$targetJson" != "[" ]; then
351                                         targetJson=$targetJson","
352                                 fi
353                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
354                         done
355                 fi
356
357                 targetJson=$targetJson"]"
358                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
359                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
360
361                 if [ $res -ne 0 ]; then
362                         __log_test_fail_body
363                         return 1
364                 fi
365         fi
366         __log_test_pass
367         return 0
368 }
369
370 # CR API: Check the contents of all current status events for one id from ECS
371 # <response-code> <id> [ EMPTY | ( <status> )+ ]
372 # (Function for test scripts)
373 cr_api_check_all_ecs_events() {
374         __log_test_start $@
375
376     if [ $# -lt 2 ]; then
377         __print_err "<response-code> <id> [ EMPTY | ( <status> )+ ]" $@
378         return 1
379     fi
380
381         query="/get-all-events/"$2
382         res="$(__do_curl_to_api CR GET $query)"
383         status=${res:${#res}-3}
384
385         if [ $status -ne $1 ]; then
386                 __log_test_fail_status_code $1 $status
387                 return 1
388         fi
389
390         if [ $# -gt 2 ]; then
391                 body=${res:0:${#res}-3}
392                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
393                         targetJson="["
394                 else
395                         targetJson="["
396                         arr=(${@:3})
397
398                         for ((i=0; i<$(($#-2)); i=i+1)); do
399
400                                 if [ "$targetJson" != "[" ]; then
401                                         targetJson=$targetJson","
402                                 fi
403                                 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
404                         done
405                 fi
406
407                 targetJson=$targetJson"]"
408                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
409                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
410
411                 if [ $res -ne 0 ]; then
412                         __log_test_fail_body
413                         return 1
414                 fi
415         fi
416         __log_test_pass
417         return 0
418 }
419
420 # CR API: Check the contents of all current type subscription events for one id from ECS
421 # <response-code> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
422 # (Function for test scripts)
423 cr_api_check_all_ecs_subscription_events() {
424         __log_test_start $@
425
426         #Valid number of parameter 2,3,7,11
427         paramError=1
428         if [ $# -eq 2 ]; then
429                 paramError=0
430         fi
431         if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
432                 paramError=0
433         fi
434         variablecount=$(($#-2))
435         if [ $# -gt 3 ] && [ $(($variablecount%3)) -eq 0 ]; then
436                 paramError=0
437         fi
438         if [ $paramError -eq 1 ]; then
439                 __print_err "<response-code> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
440                 return 1
441         fi
442
443         query="/get-all-events/"$2
444         res="$(__do_curl_to_api CR GET $query)"
445         status=${res:${#res}-3}
446
447         if [ $status -ne $1 ]; then
448                 __log_test_fail_status_code $1 $status
449                 return 1
450         fi
451
452         if [ $# -gt 2 ]; then
453                 body=${res:0:${#res}-3}
454                 targetJson="["
455                 if [ $# -gt 3 ]; then
456                         arr=(${@:3})
457                         for ((i=0; i<$(($#-3)); i=i+3)); do
458                                 if [ "$targetJson" != "[" ]; then
459                                         targetJson=$targetJson","
460                                 fi
461                                 if [ -f ${arr[$i+1]} ]; then
462                                         jobfile=$(cat ${arr[$i+1]})
463                                 else
464                                         __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
465                                         return 1
466                                 fi
467                                 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
468                         done
469                 fi
470                 targetJson=$targetJson"]"
471
472                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
473                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
474
475                 if [ $res -ne 0 ]; then
476                         __log_test_fail_body
477                         return 1
478                 fi
479         fi
480
481         __log_test_pass
482         return 0
483 }
484
485
486 # CR API: Reset all events and counters
487 # Arg: -
488 # (Function for test scripts)
489 cr_api_reset() {
490         __log_conf_start $@
491
492         res="$(__do_curl_to_api CR GET /reset)"
493         status=${res:${#res}-3}
494
495         if [ $status -ne 200 ]; then
496                 __log_conf_fail_status_code $1 $status
497                 return 1
498         fi
499
500         __log_conf_ok
501         return 0
502 }
503
504
505 # CR API: Check the contents of all json events for path
506 # <response-code> <topic-url> (EMPTY | <json-msg>+ )
507 # (Function for test scripts)
508 cr_api_check_all_genric_json_events() {
509         __log_test_start $@
510
511         if [ $# -lt 3 ]; then
512                 __print_err "<response-code> <topic-url> (EMPTY | <json-msg>+ )" $@
513                 return 1
514         fi
515
516         query="/get-all-events/"$2
517         res="$(__do_curl_to_api CR GET $query)"
518         status=${res:${#res}-3}
519
520         if [ $status -ne $1 ]; then
521                 __log_test_fail_status_code $1 $status
522                 return 1
523         fi
524         body=${res:0:${#res}-3}
525         targetJson="["
526
527         if [ $3 != "EMPTY" ]; then
528                 shift
529                 shift
530                 while [ $# -gt 0 ]; do
531                         if [ "$targetJson" != "[" ]; then
532                                 targetJson=$targetJson","
533                         fi
534                         targetJson=$targetJson$1
535                         shift
536                 done
537         fi
538         targetJson=$targetJson"]"
539
540         echo " TARGET JSON: $targetJson" >> $HTTPLOG
541         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
542
543         if [ $res -ne 0 ]; then
544                 __log_test_fail_body
545                 return 1
546         fi
547
548         __log_test_pass
549         return 0
550 }
551
552
553
554 # CR API: Check a single (oldest) json event (or none if empty) for path
555 # <response-code> <topic-url> (EMPTY | <json-msg> )
556 # (Function for test scripts)
557 cr_api_check_single_genric_json_event() {
558         __log_test_start $@
559
560         if [ $# -ne 3 ]; then
561                 __print_err "<response-code> <topic-url> (EMPTY | <json-msg> )" $@
562                 return 1
563         fi
564
565         query="/get-event/"$2
566         res="$(__do_curl_to_api CR GET $query)"
567         status=${res:${#res}-3}
568
569         if [ $status -ne $1 ]; then
570                 __log_test_fail_status_code $1 $status
571                 return 1
572         fi
573         body=${res:0:${#res}-3}
574         targetJson=$3
575
576         echo " TARGET JSON: $targetJson" >> $HTTPLOG
577         res=$(python3 ../common/compare_json.py "$targetJson" "$body")
578
579         if [ $res -ne 0 ]; then
580                 __log_test_fail_body
581                 return 1
582         fi
583
584         __log_test_pass
585         return 0
586 }