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 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 #######################################################
124 #######################################################
126 # Set http as the protocol to use for all communication to the Kafka procon
128 # (Function for test scripts)
130 __kafkapc_set_protocoll "http" $KAFKAPC_INTERNAL_PORT $KAFKAPC_EXTERNAL_PORT
133 # Set httpS as the protocol to use for all communication to the Kafka procon
135 # (Function for test scripts)
136 use_kafkapc_https() {
137 __kafkapc_set_protocoll "https" $KAFKAPC_INTERNAL_SECURE_PORT $KAFKAPC_EXTERNAL_SECURE_PORT
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"
146 ## Access to Kafka procon
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
153 KAFKAPC_ADAPTER_TYPE="REST"
154 KAFKAPC_ADAPTER=$KAFKAPC_SERVICE_PATH
159 ### Admin API functions Kafka procon
161 ###########################
162 ### Kafka Procon functions
163 ###########################
165 # Export env vars for config files, docker compose and kube resources
167 __kafkapc_export_vars() {
168 export KAFKAPC_APP_NAME
169 export KAFKAPC_DISPLAY_NAME
171 export DOCKER_SIM_NWNAME
172 export KUBE_SIM_NAMESPACE
175 export KAFKAPC_INTERNAL_PORT
176 export KAFKAPC_INTERNAL_SECURE_PORT
177 export KAFKAPC_EXTERNAL_PORT
178 export KAFKAPC_EXTERNAL_SECURE_PORT
180 export MR_KAFKA_SERVICE_PATH
184 # Start the Kafka procon in the simulator group
186 # (Function for test scripts)
189 echo -e $BOLD"Starting $KAFKAPC_DISPLAY_NAME"$EBOLD
191 if [ $RUNMODE == "KUBE" ]; then
193 # Check if app shall be fully managed by the test script
194 __check_included_image "KAFKAPC"
197 # Check if app shall only be used by the testscipt
198 __check_prestarted_image "KAFKAPC"
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
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
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
218 if [ $retcode_i -eq 0 ]; then
219 echo -e " Creating $KAFKAPC_APP_NAME deployment and service"
221 __kube_create_namespace $KUBE_SIM_NAMESPACE
223 __kafkapc_export_vars
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
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
236 __check_service_start $KAFKAPC_APP_NAME $KAFKAPC_SERVICE_PATH$KAFKAPC_ALIVE_URL
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
248 __kafkapc_export_vars
250 __start_container $KAFKAPC_COMPOSE_DIR "" NODOCKERARGS 1 $KAFKAPC_APP_NAME
252 __check_service_start $KAFKAPC_APP_NAME $KAFKAPC_SERVICE_PATH$KAFKAPC_ALIVE_URL
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
264 # (Function for test scripts)
266 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
267 __var_test KAFPAPC "$KAFKAPC_SERVICE_PATH/" $1 "=" $2 $3
269 __print_err "Wrong args to kafkapc_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
273 # KAFKA PC API: Reset all, POST /reset
274 # Arg: <response-code>
275 # (Function for test scripts)
276 kafkapc_api_reset() {
279 if [ $# -ne 1 ]; then
280 __print_err "<response-code>" $@
284 res="$(__do_curl_to_api KAFKAPC POST /reset)"
285 status=${res:${#res}-3}
287 if [ $status -ne $1 ]; then
288 __log_conf_fail_status_code $1 $status
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() {
302 if [ $# -ne 3 ]; then
303 __print_err "<response-code> <topic-name> <mime-type>" $@
307 res="$(__do_curl_to_api KAFKAPC PUT /topics/$2?type=$3)"
308 status=${res:${#res}-3}
310 if [ $status -ne $1 ]; then
311 __log_conf_fail_status_code $1 $status
319 # KAFKA PC API: Get topics, GET /topics
320 # args: <response-code> [ EMPTY | [<topic>]+ ]
321 # (Function for test scripts)
322 kafkapc_api_get_topics() {
325 if [ $# -lt 1 ]; then
326 __print_err "<response-code> EMPTY | [<policy-type-id>]*" $@
330 res="$(__do_curl_to_api KAFKAPC GET /topics)"
331 status=${res:${#res}-3}
333 if [ $status -ne $1 ]; then
334 __log_test_fail_status_code $1 $status
337 if [ $# -gt 1 ]; then
338 body=${res:0:${#res}-3}
341 for pid in ${@:2} ; do
342 if [ "$targetJson" != "[" ]; then
343 targetJson=$targetJson","
345 if [ $pid != "EMPTY" ]; then
346 targetJson=$targetJson"\"$pid\""
349 targetJson=$targetJson"]"
350 echo " TARGET JSON: $targetJson" >> $HTTPLOG
351 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
353 if [ $res -ne 0 ]; then
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() {
368 if [ $# -ne 3 ]; then
369 __print_err "<response-code> <topic> <mime-type>" $@
373 res="$(__do_curl_to_api KAFKAPC GET /topics/$2)"
374 status=${res:${#res}-3}
376 if [ $status -ne $1 ]; then
377 __log_test_fail_status_code $1 $status
381 body=${res:0:${#res}-3}
382 if [ "$body" != $3 ]; then
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() {
397 if [ $# -ne 2 ]; then
398 __print_err "<response-code> <topic>" $@
402 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/startsend)"
403 status=${res:${#res}-3}
405 if [ $status -ne $1 ]; then
406 __log_test_fail_status_code $1 $status
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() {
420 if [ $# -ne 2 ]; then
421 __print_err "<response-code> <topic>" $@
425 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/startreceive)"
426 status=${res:${#res}-3}
428 if [ $status -ne $1 ]; then
429 __log_test_fail_status_code $1 $status
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() {
443 if [ $# -ne 2 ]; then
444 __print_err "<response-code> <topic>" $@
448 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/stopsend)"
449 status=${res:${#res}-3}
451 if [ $status -ne $1 ]; then
452 __log_test_fail_status_code $1 $status
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() {
466 if [ $# -ne 2 ]; then
467 __print_err "<response-code> <topic>" $@
471 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/stopreceive)"
472 status=${res:${#res}-3}
474 if [ $status -ne $1 ]; then
475 __log_test_fail_status_code $1 $status
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() {
489 if [ $# -ne 4 ]; then
490 __print_err "<response-code> <topic> <mime-type> <msg>" $@
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}
498 if [ $status -ne $1 ]; then
499 __log_test_fail_status_code $1 $status
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() {
514 if [ $# -lt 3 ]; then
515 __print_err "<response-code> <topic> ([ <mime-type> <msg> ] | NOMSG )" $@
518 mime_type="text/plain"
519 if [ ! -z "$3" ]; then
522 res="$(__do_curl_to_api KAFKAPC GET /topics/$2/msg)"
523 status=${res:${#res}-3}
525 if [ $status -ne $1 ]; then
526 __log_test_fail_status_code $1 $status
529 if [ $# -eq 4 ]; then
530 body=${res:0:${#res}-3}
531 if [ "$body" != "$4" ]; then
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() {
547 if [ $# -ne 4 ]; then
548 __print_err "<response-code> <topic> <mime-type> <file>" $@
551 res="$(__do_curl_to_api KAFKAPC POST /topics/$2/msg $4 $3)"
552 status=${res:${#res}-3}
554 if [ $status -ne $1 ]; then
555 __log_test_fail_status_code $1 $status
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() {
569 if [ $# -ne 4 ]; then
570 __print_err "<response-code> <topic> <mime-type> <file> " $@
577 __log_test_fail_general "Message file "$4", does not exist"
581 mime_type="text/plain"
583 res="$(__do_curl_to_api KAFKAPC GET /topics/$2/msg)"
584 status=${res:${#res}-3}
586 if [ $status -ne $1 ]; then
587 __log_test_fail_status_code $1 $status
591 body=${res:0:${#res}-3}
592 if [ "$body" != "$msgfile" ]; then
602 # Create json file for payload
603 # arg: <size-in-kb> <filename>
604 kafkapc_api_generate_json_payload_file() {
606 if [ $# -ne 2 ]; then
607 __print_err "<topic-url> <json-file>" $@
610 if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
611 __log_conf_fail_general "Only size between 1k and 10000k supported"
614 echo -n "{\"abcdefghijklmno\":[" > $2
616 echo -n "\""ABCDEFG"\"" >> $2
617 for ((idx=1; idx<$LEN; idx++))
619 echo -n ",\"ABCDEFG\"" >> $2
627 # Create text file for payload
628 # arg: <size-in-kb> <filename>
629 kafkapc_api_generate_text_payload_file() {
631 if [ $# -ne 2 ]; then
632 __print_err "<topic-url> <text-file>" $@
635 if [ $1 -lt 1 ] || [ $1 -gt 10000 ]; then
636 __log_conf_fail_general "Only size between 1k and 10000k supported"
641 for ((idx=0; idx<$LEN; idx++))
643 echo -n "ABCDEFGHIJ" >> $2