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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=================================================
20 # This is a script that contains container/service managemnt functions for Kafka producer/consumer
22 ################ Test engine functions ################
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"
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
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() {
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
49 echo -e $GREEN" Build Ok"$EGREEN
50 __retag_and_push_image KAFKAPC_IMAGE
55 echo -e $RED" Build Failed"$ERED
58 echo -e $RED"Exiting...."$ERED
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
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
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
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
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
99 docker logs $KAFKAPC_APP_NAME > $1$2_kafkapc.log 2>&1
103 # Initial setup of protocol, host and ports
104 # This function is called for apps managed by the test script.
106 __KAFKAPC_initial_setup() {
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.
114 __KAFKAPC_statisics_setup() {
115 if [ $RUNMODE == "KUBE" ]; then
116 echo "KAFKAPC $KAFKAPC_APP_NAME $KUBE_SIM_NAMESPACE"
118 echo "KAFKAPC $KAFKAPC_APP_NAME"
122 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
124 __KAFKAPC_test_requirements() {
128 #######################################################
130 #######################################################
132 # Set http as the protocol to use for all communication to the Kafka procon
134 # (Function for test scripts)
136 __kafkapc_set_protocoll "http" $KAFKAPC_INTERNAL_PORT $KAFKAPC_EXTERNAL_PORT
139 # Set httpS as the protocol to use for all communication to the Kafka procon
141 # (Function for test scripts)
142 use_kafkapc_https() {
143 __kafkapc_set_protocoll "https" $KAFKAPC_INTERNAL_SECURE_PORT $KAFKAPC_EXTERNAL_SECURE_PORT
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"
152 ## Access to Kafka procon
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
159 KAFKAPC_ADAPTER_TYPE="REST"
160 KAFKAPC_ADAPTER=$KAFKAPC_SERVICE_PATH
165 ### Admin API functions Kafka procon
167 ###########################
168 ### Kafka Procon functions
169 ###########################
171 # Export env vars for config files, docker compose and kube resources
173 __kafkapc_export_vars() {
174 export KAFKAPC_APP_NAME
175 export KAFKAPC_DISPLAY_NAME
177 export DOCKER_SIM_NWNAME
178 export KUBE_SIM_NAMESPACE
181 export KAFKAPC_INTERNAL_PORT
182 export KAFKAPC_INTERNAL_SECURE_PORT
183 export KAFKAPC_EXTERNAL_PORT
184 export KAFKAPC_EXTERNAL_SECURE_PORT
186 export MR_KAFKA_SERVICE_PATH
190 # Start the Kafka procon in the simulator group
192 # (Function for test scripts)
195 echo -e $BOLD"Starting $KAFKAPC_DISPLAY_NAME"$EBOLD
197 if [ $RUNMODE == "KUBE" ]; then
199 # Check if app shall be fully managed by the test script
200 __check_included_image "KAFKAPC"
203 # Check if app shall only be used by the testscipt
204 __check_prestarted_image "KAFKAPC"
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
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
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
224 if [ $retcode_i -eq 0 ]; then
225 echo -e " Creating $KAFKAPC_APP_NAME deployment and service"
227 __kube_create_namespace $KUBE_SIM_NAMESPACE
229 __kafkapc_export_vars
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
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
242 __check_service_start $KAFKAPC_APP_NAME $KAFKAPC_SERVICE_PATH$KAFKAPC_ALIVE_URL
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
254 __kafkapc_export_vars
256 __start_container $KAFKAPC_COMPOSE_DIR "" NODOCKERARGS 1 $KAFKAPC_APP_NAME
258 __check_service_start $KAFKAPC_APP_NAME $KAFKAPC_SERVICE_PATH$KAFKAPC_ALIVE_URL
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
270 # (Function for test scripts)
272 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
273 __var_test KAFPAPC "$KAFKAPC_SERVICE_PATH/" $1 "=" $2 $3
275 __print_err "Wrong args to kafkapc_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
279 # KAFKA PC API: Reset all, POST /reset
280 # Arg: <response-code>
281 # (Function for test scripts)
282 kafkapc_api_reset() {
285 if [ $# -ne 1 ]; then
286 __print_err "<response-code>" $@
290 res="$(__do_curl_to_api KAFKAPC POST /reset)"
291 status=${res:${#res}-3}
293 if [ $status -ne $1 ]; then
294 __log_conf_fail_status_code $1 $status
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() {
308 if [ $# -ne 3 ]; then
309 __print_err "<response-code> <topic-name> <mime-type>" $@
313 res="$(__do_curl_to_api KAFKAPC PUT /topics/$2?type=$3)"
314 status=${res:${#res}-3}
316 if [ $status -ne $1 ]; then
317 __log_conf_fail_status_code $1 $status
325 # KAFKA PC API: Get topics, GET /topics
326 # args: <response-code> [ EMPTY | [<topic>]+ ]
327 # (Function for test scripts)
328 kafkapc_api_get_topics() {
331 if [ $# -lt 1 ]; then
332 __print_err "<response-code> EMPTY | [<policy-type-id>]*" $@
336 res="$(__do_curl_to_api KAFKAPC GET /topics)"
337 status=${res:${#res}-3}
339 if [ $status -ne $1 ]; then
340 __log_test_fail_status_code $1 $status
343 if [ $# -gt 1 ]; then
344 body=${res:0:${#res}-3}
347 for pid in ${@:2} ; do
348 if [ "$targetJson" != "[" ]; then
349 targetJson=$targetJson","
351 if [ $pid != "EMPTY" ]; then
352 targetJson=$targetJson"\"$pid\""
355 targetJson=$targetJson"]"
356 echo " TARGET JSON: $targetJson" >> $HTTPLOG
357 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
359 if [ $res -ne 0 ]; then
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() {
374 if [ $# -ne 3 ]; then
375 __print_err "<response-code> <topic> <mime-type>" $@
379 res="$(__do_curl_to_api KAFKAPC GET /topics/$2)"
380 status=${res:${#res}-3}
382 if [ $status -ne $1 ]; then
383 __log_test_fail_status_code $1 $status
387 body=${res:0:${#res}-3}
388 if [ "$body" != $3 ]; then
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() {
403 if [ $# -ne 2 ]; then
404 __print_err "<response-code> <topic>" $@
408 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/startsend)"
409 status=${res:${#res}-3}
411 if [ $status -ne $1 ]; then
412 __log_test_fail_status_code $1 $status
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() {
426 if [ $# -ne 2 ]; then
427 __print_err "<response-code> <topic>" $@
431 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/startreceive)"
432 status=${res:${#res}-3}
434 if [ $status -ne $1 ]; then
435 __log_test_fail_status_code $1 $status
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() {
449 if [ $# -ne 2 ]; then
450 __print_err "<response-code> <topic>" $@
454 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/stopsend)"
455 status=${res:${#res}-3}
457 if [ $status -ne $1 ]; then
458 __log_test_fail_status_code $1 $status
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() {
472 if [ $# -ne 2 ]; then
473 __print_err "<response-code> <topic>" $@
477 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/stopreceive)"
478 status=${res:${#res}-3}
480 if [ $status -ne $1 ]; then
481 __log_test_fail_status_code $1 $status
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() {
495 if [ $# -ne 4 ]; then
496 __print_err "<response-code> <topic> <mime-type> <msg>" $@
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}
504 if [ $status -ne $1 ]; then
505 __log_test_fail_status_code $1 $status
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() {
520 if [ $# -lt 3 ]; then
521 __print_err "<response-code> <topic> ([ <mime-type> <msg> ] | NOMSG )" $@
524 mime_type="text/plain"
525 if [ ! -z "$3" ]; then
528 res="$(__do_curl_to_api KAFKAPC GET /topics/$2/msg)"
529 status=${res:${#res}-3}
531 if [ $status -ne $1 ]; then
532 __log_test_fail_status_code $1 $status
535 if [ $# -eq 4 ]; then
536 body=${res:0:${#res}-3}
537 if [ "$body" != "$4" ]; then
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() {
553 if [ $# -ne 4 ]; then
554 __print_err "<response-code> <topic> <mime-type> <file>" $@
557 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/msg $4 $3)"
558 status=${res:${#res}-3}
560 if [ $status -ne $1 ]; then
561 __log_test_fail_status_code $1 $status
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() {
575 if [ $# -ne 4 ]; then
576 __print_err "<response-code> <topic> <mime-type> <file> " $@
583 __log_test_fail_general "Message file "$4", does not exist"
587 mime_type="text/plain"
589 res="$(__do_curl_to_api KAFKAPC GET /topics/$2/msg)"
590 status=${res:${#res}-3}
592 if [ $status -ne $1 ]; then
593 __log_test_fail_status_code $1 $status
597 body=${res:0:${#res}-3}
598 if [ "$body" != "$msgfile" ]; then
608 # Create json file for payload
609 # arg: <size-in-kb> <filename>
610 kafkapc_api_generate_json_payload_file() {
612 if [ $# -ne 2 ]; then
613 __print_err "<topic-url> <json-file>" $@
616 if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
617 __log_conf_fail_general "Only size between 1k and 10000k supported"
620 echo -n "{\"abcdefghijklmno\":[" > $2
622 echo -n "\""ABCDEFG"\"" >> $2
623 for ((idx=1; idx<$LEN; idx++))
625 echo -n ",\"ABCDEFG\"" >> $2
633 # Create text file for payload
634 # arg: <size-in-kb> <filename>
635 kafkapc_api_generate_text_payload_file() {
637 if [ $# -ne 2 ]; then
638 __print_err "<topic-url> <text-file>" $@
641 if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
642 __log_conf_fail_general "Only size between 1k and 10000k supported"
647 for ((idx=0; idx<$LEN; idx++))
649 echo -n "ABCDEFGHIJ" >> $2