002657c82ae69b303a19cd371bd34e70ac5627ad
[nonrtric.git] / test / common / kafkapc_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 for Kafka producer/consumer
21
22 ################ Test engine functions ################
23
24 # Create the image var used during the test
25 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
26 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
27 __KAFKAPC_imagesetup() {
28         __check_and_create_image_var KAFKAPC "KAFKAPC_IMAGE" "KAFKAPC_IMAGE_BASE" "KAFKAPC_IMAGE_TAG" LOCAL "$KAFKAPC_DISPLAY_NAME"
29 }
30
31 # Pull image from remote repo or use locally built image
32 # arg: <pull-policy-override> <pull-policy-original>
33 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
34 # <pull-policy-original> Shall be used for images that does not allow overriding
35 # Both var may contain: 'remote', 'remote-remove' or 'local'
36 __KAFKAPC_imagepull() {
37         __check_and_pull_image $2 "$KAFKAPC_DISPLAY_NAME" $KAFKAPC_APP_NAME KAFKAPC_IMAGE
38 }
39
40 # Build image (only for simulator or interfaces stubs owned by the test environment)
41 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
42 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
43 __KAFKAPC_imagebuild() {
44
45         cd ../$KAFKAPC_BUILD_DIR
46         echo " Building KAFKAPC - $KAFKAPC_DISPLAY_NAME - image: $KAFKAPC_IMAGE"
47         docker build  --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $KAFKAPC_IMAGE . &> .dockererr
48         if [ $? -eq 0 ]; then
49                 echo -e  $GREEN"  Build Ok"$EGREEN
50                 __retag_and_push_image KAFKAPC_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 __KAFKAPC_image_data() {
67         echo -e "$KAFKAPC_DISPLAY_NAME\t$(docker images --format $1 $KAFKAPC_IMAGE)" >>   $2
68         if [ ! -z "$KAFKAPC_IMAGE_SOURCE" ]; then
69                 echo -e "-- source image --\t$(docker images --format $1 $KAFKAPC_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 __KAFKAPC_kube_scale_zero() {
77         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest KAFKAPC
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 __KAFKAPC_kube_scale_zero_and_wait() {
83         echo -e $RED" KAFKAPC 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 __KAFKAPC_kube_delete_all() {
89         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest KAFKAPC
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 __KAFKAPC_store_docker_logs() {
96         if [ $RUNMODE == "KUBE" ]; then
97                 kubectl  logs -l "autotest=KAFKAPC" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_kafkapc.log 2>&1
98         else
99                 docker logs $KAFKAPC_APP_NAME > $1$2_kafkapc.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 __KAFKAPC_initial_setup() {
107         use_kafkapc_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 __KAFKAPC_statisics_setup() {
115         if [ $RUNMODE == "KUBE" ]; then
116                 echo "KAFKAPC $KAFKAPC_APP_NAME $KUBE_SIM_NAMESPACE"
117         else
118                 echo "KAFKAPC $KAFKAPC_APP_NAME"
119         fi
120 }
121
122 #######################################################
123
124 #######################################################
125
126 # Set http as the protocol to use for all communication to the Kafka procon
127 # args: -
128 # (Function for test scripts)
129 use_kafkapc_http() {
130         __kafkapc_set_protocoll "http" $KAFKAPC_INTERNAL_PORT $KAFKAPC_EXTERNAL_PORT
131 }
132
133 # Set httpS as the protocol to use for all communication to the Kafka procon
134 # args: -
135 # (Function for test scripts)
136 use_kafkapc_https() {
137         __kafkapc_set_protocoll "https" $KAFKAPC_INTERNAL_SECURE_PORT $KAFKAPC_EXTERNAL_SECURE_PORT
138 }
139
140 # Setup paths to svc/container for internal and external access
141 # args: <protocol> <internal-port> <external-port>
142 __kafkapc_set_protocoll() {
143         echo -e $BOLD"$KAFKAPC_DISPLAY_NAME protocol setting"$EBOLD
144         echo -e " Using $BOLD $1 $EBOLD towards $KAFKAPC_DISPLAY_NAME"
145
146         ## Access to Kafka procon
147
148         KAFKAPC_SERVICE_PATH=$1"://"$KAFKAPC_APP_NAME":"$2  # docker access, container->container and script->container via proxy
149         if [ $RUNMODE == "KUBE" ]; then
150                 KAFKAPC_SERVICE_PATH=$1"://"$KAFKAPC_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
151         fi
152
153         KAFKAPC_ADAPTER_TYPE="REST"
154         KAFKAPC_ADAPTER=$KAFKAPC_SERVICE_PATH
155
156         echo ""
157 }
158
159 ### Admin API functions Kafka procon
160
161 ###########################
162 ### Kafka Procon functions
163 ###########################
164
165 # Export env vars for config files, docker compose and kube resources
166 # args:
167 __kafkapc_export_vars() {
168         export KAFKAPC_APP_NAME
169         export KAFKAPC_DISPLAY_NAME
170
171         export DOCKER_SIM_NWNAME
172         export KUBE_SIM_NAMESPACE
173
174         export KAFKAPC_IMAGE
175         export KAFKAPC_INTERNAL_PORT
176         export KAFKAPC_INTERNAL_SECURE_PORT
177         export KAFKAPC_EXTERNAL_PORT
178         export KAFKAPC_EXTERNAL_SECURE_PORT
179
180         export MR_KAFKA_SERVICE_PATH
181 }
182
183
184 # Start the Kafka procon in the simulator group
185 # args: -
186 # (Function for test scripts)
187 start_kafkapc() {
188
189         echo -e $BOLD"Starting $KAFKAPC_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 "KAFKAPC"
195                 retcode_i=$?
196
197                 # Check if app shall only be used by the testscipt
198                 __check_prestarted_image "KAFKAPC"
199                 retcode_p=$?
200
201                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
202                         echo -e $RED"The $ICS_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
203                         echo -e $RED"The $ICS_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 $ICS_APP_NAME app is included both as managed and prestarted in this test script"$ERED
208                         echo -e $RED"The $ICS_APP_NAME will not be started"$ERED
209                         exit
210                 fi
211
212                 if [ $retcode_p -eq 0 ]; then
213                         echo -e " Using existing $KAFKAPC_APP_NAME deployment and service"
214                         echo " Setting RC replicas=1"
215                         __kube_scale deployment $KAFKAPC_APP_NAME $KUBE_SIM_NAMESPACE 1
216                 fi
217
218                 if [ $retcode_i -eq 0 ]; then
219                         echo -e " Creating $KAFKAPC_APP_NAME deployment and service"
220
221             __kube_create_namespace $KUBE_SIM_NAMESPACE
222
223                         __kafkapc_export_vars
224
225                         # Create service
226                         input_yaml=$SIM_GROUP"/"$KAFKAPC_COMPOSE_DIR"/"svc.yaml
227                         output_yaml=$PWD/tmp/kafkapc_svc.yaml
228                         __kube_create_instance service $KAFKAPC_APP_NAME $input_yaml $output_yaml
229
230                         # Create app
231                         input_yaml=$SIM_GROUP"/"$KAFKAPC_COMPOSE_DIR"/"app.yaml
232                         output_yaml=$PWD/tmp/kafkapc_app.yaml
233                         __kube_create_instance app $KAFKAPC_APP_NAME $input_yaml $output_yaml
234                 fi
235
236                 __check_service_start $KAFKAPC_APP_NAME $KAFKAPC_SERVICE_PATH$KAFKAPC_ALIVE_URL
237
238         else
239
240                 # Check if docker app shall be fully managed by the test script
241                 __check_included_image 'KAFKAPC'
242                 if [ $? -eq 1 ]; then
243                         echo -e $RED"The Kafka procon app is not included as managed in this test script"$ERED
244                         echo -e $RED"The Kafka procon will not be started"$ERED
245                         exit
246                 fi
247
248                 __kafkapc_export_vars
249
250                 __start_container $KAFKAPC_COMPOSE_DIR "" NODOCKERARGS 1 $KAFKAPC_APP_NAME
251
252         __check_service_start $KAFKAPC_APP_NAME $KAFKAPC_SERVICE_PATH$KAFKAPC_ALIVE_URL
253         fi
254     echo ""
255     return 0
256 }
257
258 # Tests if a variable value in the KAFPAPC is equal to a target value and and optional timeout.
259 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
260 # equal to the target or not.
261 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
262 # before setting pass or fail depending on if the variable value becomes equal to the target
263 # value or not.
264 # (Function for test scripts)
265 kafkapc_equal() {
266         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
267                 __var_test KAFPAPC "$KAFKAPC_SERVICE_PATH/" $1 "=" $2 $3
268         else
269                 __print_err "Wrong args to kafkapc_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
270         fi
271 }
272
273 # KAFKA PC API: Reset all, POST /reset
274 # Arg: <response-code>
275 # (Function for test scripts)
276 kafkapc_api_reset() {
277         __log_conf_start $@
278
279         if [ $# -ne 1 ]; then
280                 __print_err "<response-code>" $@
281                 return 1
282         fi
283
284         res="$(__do_curl_to_api KAFKAPC POST /reset)"
285         status=${res:${#res}-3}
286
287         if [ $status -ne $1 ]; then
288                 __log_conf_fail_status_code $1 $status
289                 return 1
290         fi
291
292         __log_conf_ok
293         return 0
294 }
295
296 # KAFKA PC API: Create a topic of a data-type, PUT /topics/<topic>
297 # Arg: <response-code> <topic-name>  <mime-type>
298 # (Function for test scripts)
299 kafkapc_api_create_topic() {
300         __log_conf_start $@
301
302     if [ $# -ne 3 ]; then
303         __print_err "<response-code> <topic-name>  <mime-type>" $@
304         return 1
305         fi
306
307         res="$(__do_curl_to_api KAFKAPC PUT /topics/$2?type=$3)"
308         status=${res:${#res}-3}
309
310         if [ $status -ne $1 ]; then
311                 __log_conf_fail_status_code $1 $status
312                 return 1
313         fi
314
315         __log_conf_ok
316         return 0
317 }
318
319 # KAFKA PC API: Get topics, GET /topics
320 # args: <response-code> [ EMPTY | [<topic>]+ ]
321 # (Function for test scripts)
322 kafkapc_api_get_topics() {
323         __log_test_start $@
324
325     if [ $# -lt 1 ]; then
326                 __print_err "<response-code> EMPTY | [<policy-type-id>]*" $@
327                 return 1
328         fi
329
330     res="$(__do_curl_to_api KAFKAPC GET /topics)"
331     status=${res:${#res}-3}
332
333         if [ $status -ne $1 ]; then
334                 __log_test_fail_status_code $1 $status
335                 return 1
336         fi
337         if [ $# -gt 1 ]; then
338                 body=${res:0:${#res}-3}
339                 targetJson="["
340
341                 for pid in ${@:2} ; do
342                         if [ "$targetJson" != "[" ]; then
343                                 targetJson=$targetJson","
344                         fi
345                         if [ $pid != "EMPTY" ]; then
346                                 targetJson=$targetJson"\"$pid\""
347                         fi
348                 done
349                 targetJson=$targetJson"]"
350                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
351                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
352
353                 if [ $res -ne 0 ]; then
354                         __log_test_fail_body
355                         return 1
356                 fi
357         fi
358         __log_test_pass
359         return 0
360 }
361
362 # KAFKA PC API: Get a topic, GET /topic/<topic>
363 # args: <response-code> <topic> <mime-type>
364 # (Function for test scripts)
365 kafkapc_api_get_topic() {
366         __log_test_start $@
367
368     if [ $# -ne 3 ]; then
369                 __print_err "<response-code> <topic> <mime-type>" $@
370                 return 1
371         fi
372
373     res="$(__do_curl_to_api KAFKAPC GET /topics/$2)"
374     status=${res:${#res}-3}
375
376         if [ $status -ne $1 ]; then
377                 __log_test_fail_status_code $1 $status
378                 return 1
379         fi
380
381         body=${res:0:${#res}-3}
382         if [ "$body" != $3 ]; then
383                 __log_test_fail_body
384                 return 1
385         fi
386
387         __log_test_pass
388         return 0
389 }
390
391 # KAFKA PC API: Start sending on a topic, POST /topic/<topic>/startsend
392 # args: <response-code> <topic>
393 # (Function for test scripts)
394 kafkapc_api_start_sending() {
395         __log_test_start $@
396
397     if [ $# -ne 2 ]; then
398                 __print_err "<response-code> <topic>" $@
399                 return 1
400         fi
401
402     res="$(__do_curl_to_api KAFKAPC POST /topics/$2/startsend)"
403     status=${res:${#res}-3}
404
405         if [ $status -ne $1 ]; then
406                 __log_test_fail_status_code $1 $status
407                 return 1
408         fi
409
410         __log_test_pass
411         return 0
412 }
413
414 # KAFKA PC API: Start receiving on a topic, POST /topic/<topic>/startreceive
415 # args: <response-code> <topic>
416 # (Function for test scripts)
417 kafkapc_api_start_receiving() {
418         __log_test_start $@
419
420     if [ $# -ne 2 ]; then
421                 __print_err "<response-code> <topic>" $@
422                 return 1
423         fi
424
425     res="$(__do_curl_to_api KAFKAPC POST /topics/$2/startreceive)"
426     status=${res:${#res}-3}
427
428         if [ $status -ne $1 ]; then
429                 __log_test_fail_status_code $1 $status
430                 return 1
431         fi
432
433         __log_test_pass
434         return 0
435 }
436
437 # KAFKA PC API: Stop sending on a topic, POST /topic/<topic>/stopsend
438 # args: <response-code> <topic>
439 # (Function for test scripts)
440 kafkapc_api_stop_sending() {
441         __log_test_start $@
442
443     if [ $# -ne 2 ]; then
444                 __print_err "<response-code> <topic>" $@
445                 return 1
446         fi
447
448     res="$(__do_curl_to_api KAFKAPC POST /topics/$2/stopsend)"
449     status=${res:${#res}-3}
450
451         if [ $status -ne $1 ]; then
452                 __log_test_fail_status_code $1 $status
453                 return 1
454         fi
455
456         __log_test_pass
457         return 0
458 }
459
460 # KAFKA PC API: Stop receiving on a topic, POST /topic/<topic>/stopreceive
461 # args: <response-code> <topic>
462 # (Function for test scripts)
463 kafkapc_api_stop_receiving() {
464         __log_test_start $@
465
466     if [ $# -ne 2 ]; then
467                 __print_err "<response-code> <topic>" $@
468                 return 1
469         fi
470
471     res="$(__do_curl_to_api KAFKAPC POST /topics/$2/stopreceive)"
472     status=${res:${#res}-3}
473
474         if [ $status -ne $1 ]; then
475                 __log_test_fail_status_code $1 $status
476                 return 1
477         fi
478
479         __log_test_pass
480         return 0
481 }
482
483 # KAFKA PC API: Send a message on a topic, POST /topic/<topic>/msg
484 # args: <response-code> <topic> <mime-type> <msg>
485 # (Function for test scripts)
486 kafkapc_api_post_msg() {
487         __log_test_start $@
488
489     if [ $# -ne 4 ]; then
490                 __print_err "<response-code> <topic> <mime-type> <msg>" $@
491                 return 1
492         fi
493         payload="tmp/.kafkapayload"
494         echo -n $4 > $payload     #-n prevent a newline to be added...
495     res="$(__do_curl_to_api KAFKAPC POST /topics/$2/msg $payload $3)"
496     status=${res:${#res}-3}
497
498         if [ $status -ne $1 ]; then
499                 __log_test_fail_status_code $1 $status
500                 return 1
501         fi
502
503         __log_test_pass
504         return 0
505 }
506
507
508 # KAFKA PC API: Get a msg on a topic, GET /topic/<topic>/msg
509 # args: <response-code> <topic>  ([ <mime-type>  <msg> ] | NOMSG )
510 # (Function for test scripts)
511 kafkapc_api_get_msg() {
512         __log_test_start $@
513
514     if [ $# -lt 3 ]; then
515                 __print_err "<response-code> <topic>  ([ <mime-type>  <msg> ] | NOMSG )" $@
516                 return 1
517         fi
518         mime_type="text/plain"
519         if [ ! -z "$3" ]; then
520                 mime_type=$3
521         fi
522     res="$(__do_curl_to_api KAFKAPC GET /topics/$2/msg)"
523     status=${res:${#res}-3}
524
525         if [ $status -ne $1 ]; then
526                 __log_test_fail_status_code $1 $status
527                 return 1
528         fi
529         if [ $# -eq 4 ]; then
530                 body=${res:0:${#res}-3}
531                 if [ "$body" != "$4" ]; then
532                         __log_test_fail_body
533                         return 1
534                 fi
535         fi
536
537         __log_test_pass
538         return 0
539 }
540
541 # KAFKA PC API: Send a message from a file on a topic, POST /topic/<topic>/msg
542 # args: <response-code> <topic> <mime-type> <file>
543 # (Function for test scripts)
544 kafkapc_api_post_msg_from_file() {
545         __log_test_start $@
546
547     if [ $# -ne 4 ]; then
548                 __print_err "<response-code> <topic> <mime-type> <file>" $@
549                 return 1
550         fi
551     res="$(__do_curl_to_api KAFKAPC POST /topics/$2/msg $4 $3)"
552     status=${res:${#res}-3}
553
554         if [ $status -ne $1 ]; then
555                 __log_test_fail_status_code $1 $status
556                 return 1
557         fi
558
559         __log_test_pass
560         return 0
561 }
562
563 # KAFKA PC API: Get a msg on a topic and compare with file, GET /topic/<topic>/msg
564 # args: <response-code> <topic>  <mime-type>  <file>
565 # (Function for test scripts)
566 kafkapc_api_get_msg_from_file() {
567         __log_test_start $@
568
569     if [ $# -ne 4 ]; then
570                 __print_err "<response-code> <topic>  <mime-type>  <file> " $@
571                 return 1
572         fi
573
574         if [ -f $4 ]; then
575                 msgfile=$(cat $4)
576         else
577                 __log_test_fail_general "Message file "$4", does not exist"
578                 return 1
579         fi
580
581         mime_type="text/plain"
582
583     res="$(__do_curl_to_api KAFKAPC GET /topics/$2/msg)"
584     status=${res:${#res}-3}
585
586         if [ $status -ne $1 ]; then
587                 __log_test_fail_status_code $1 $status
588                 return 1
589         fi
590
591         body=${res:0:${#res}-3}
592         if [ "$body" != "$msgfile" ]; then
593                 __log_test_fail_body
594                 return 1
595         fi
596
597         __log_test_pass
598         return 0
599 }
600
601
602 # Create json file for payload
603 # arg: <size-in-kb> <filename>
604 kafkapc_api_generate_json_payload_file() {
605         __log_conf_start $@
606     if [ $# -ne 2 ]; then
607         __print_err "<topic-url> <json-file>" $@
608         return 1
609     fi
610         if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
611                 __log_conf_fail_general "Only size between 1k and 10000k supported"
612                 return 1
613         fi
614         echo -n "{\"abcdefghijklmno\":[" > $2
615         LEN=$(($1*100-2))
616         echo -n "\""ABCDEFG"\"" >> $2
617         for ((idx=1; idx<$LEN; idx++))
618         do
619                 echo -n ",\"ABCDEFG\"" >> $2
620         done
621         echo -n "]}" >> $2
622
623         __log_conf_ok
624         return 0
625 }
626
627 # Create text file for payload
628 # arg: <size-in-kb> <filename>
629 kafkapc_api_generate_text_payload_file() {
630         __log_conf_start $@
631     if [ $# -ne 2 ]; then
632         __print_err "<topic-url> <text-file>" $@
633         return 1
634     fi
635         if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
636                 __log_conf_fail_general "Only size between 1k and 10000k supported"
637                 return 1
638         fi
639         echo -n "" > $2
640         LEN=$(($1*100))
641         for ((idx=0; idx<$LEN; idx++))
642         do
643                 echo -n "ABCDEFGHIJ" >> $2
644         done
645
646         __log_conf_ok
647         return 0
648 }