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