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 test functions for the Callback Reciver
23 ################ Test engine functions ################
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
29 __check_and_create_image_var CR "CR_IMAGE" "CR_IMAGE_BASE" "CR_IMAGE_TAG" LOCAL "$CR_DISPLAY_NAME"
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'
38 echo -e $RED" Image for app CR shall never be pulled from remote repo"$ERED
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
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
49 echo -e $GREEN" Build Ok"$EGREEN
50 __retag_and_push_image CR_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>
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
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
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
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
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
99 docker logs $CR_APP_NAME > $1$2_cr.log 2>&1
103 # Initial setup of protocol, host and ports
104 # This function is called for apps managed by the test script.
106 __CR_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 __CR_statisics_setup() {
115 if [ $RUNMODE == "KUBE" ]; then
116 echo "CR $CR_APP_NAME $KUBE_SIM_NAMESPACE"
118 echo "CR $CR_APP_NAME"
122 #######################################################
128 # Set http as the protocol to use for all communication to the Dmaap adapter
130 # (Function for test scripts)
132 __cr_set_protocoll "http" $CR_INTERNAL_PORT $CR_EXTERNAL_PORT
135 # Set https as the protocol to use for all communication to the Dmaap adapter
137 # (Function for test scripts)
139 __cr_set_protocoll "https" $CR_INTERNAL_SECURE_PORT $CR_EXTERNAL_SECURE_PORT
142 # Setup paths to svc/container for internal and external access
143 # args: <protocol> <internal-port> <external-port>
144 __cr_set_protocoll() {
145 echo -e $BOLD"$CR_DISPLAY_NAME protocol setting"$EBOLD
146 echo -e " Using $BOLD http $EBOLD towards $CR_DISPLAY_NAME"
148 ## Access to Dmaap adapter
150 # CR_SERVICE_PATH is the base path to cr
151 CR_SERVICE_PATH=$1"://"$CR_APP_NAME":"$2 # docker access, container->container and script->container via proxy
152 if [ $RUNMODE == "KUBE" ]; then
153 CR_SERVICE_PATH=$1"://"$CR_APP_NAME.$KUBE_SIM_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
155 # Service paths are used in test script to provide callbacck urls to app
156 CR_SERVICE_MR_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK_MR #Only for messages from dmaap adapter/mediator
157 CR_SERVICE_TEXT_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK_TEXT #Callbacks for text payload
158 CR_SERVICE_APP_PATH=$CR_SERVICE_PATH$CR_APP_CALLBACK #For general callbacks from apps
160 # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
161 CR_ADAPTER_TYPE="REST"
162 CR_ADAPTER=$CR_SERVICE_PATH
167 # Export env vars for config files, docker compose and kube resources
171 export CR_DISPLAY_NAME
173 export KUBE_SIM_NAMESPACE
174 export DOCKER_SIM_NWNAME
178 export CR_INTERNAL_PORT
179 export CR_INTERNAL_SECURE_PORT
180 export CR_EXTERNAL_PORT
181 export CR_EXTERNAL_SECURE_PORT
184 # Start the Callback reciver in the simulator group
186 # (Function for test scripts)
189 echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
191 if [ $RUNMODE == "KUBE" ]; then
193 # Check if app shall be fully managed by the test script
194 __check_included_image "CR"
197 # Check if app shall only be used by the testscipt
198 __check_prestarted_image "CR"
201 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
202 echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
203 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
206 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
207 echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
208 echo -e $RED"The $CR_APP_NAME will not be started"$ERED
212 # Check if app shall be used - not managed - by the test script
213 if [ $retcode_p -eq 0 ]; then
214 echo -e " Using existing $CR_APP_NAME deployment and service"
215 echo " Setting CR replicas=1"
216 __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
219 if [ $retcode_i -eq 0 ]; then
220 echo -e " Creating $CR_APP_NAME deployment and service"
224 __kube_create_namespace $KUBE_SIM_NAMESPACE
227 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
228 output_yaml=$PWD/tmp/cr_svc.yaml
229 __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
232 input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
233 output_yaml=$PWD/tmp/cr_app.yaml
234 __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
238 __check_service_start $CR_APP_NAME $CR_SERVICE_PATH$CR_ALIVE_URL
240 echo -ne " Service $CR_APP_NAME - reset "$SAMELINE
241 result=$(__do_curl CR $CR_SERVICE_PATH/reset)
242 if [ $? -ne 0 ]; then
243 echo -e " Service $CR_APP_NAME - reset $RED Failed $ERED - will continue"
245 echo -e " Service $CR_APP_NAME - reset $GREEN OK $EGREEN"
248 # Check if docker app shall be fully managed by the test script
249 __check_included_image 'CR'
250 if [ $? -eq 1 ]; then
251 echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
252 echo -e $RED"The Callback Receiver will not be started"$ERED
258 __start_container $CR_COMPOSE_DIR "" NODOCKERARGS 1 $CR_APP_NAME
260 __check_service_start $CR_APP_NAME $CR_SERVICE_PATH$CR_ALIVE_URL
266 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
267 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
268 # equal to the target or not.
269 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
270 # before setting pass or fail depending on if the variable value becomes equal to the target
272 # (Function for test scripts)
274 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
275 __var_test "CR" "$CR_SERVICE_PATH/counter/" $1 "=" $2 $3
277 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
281 # Tests if a variable value in the CR contains the target string and and optional timeout
282 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
284 # Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
285 # before setting pass or fail depending on if the variable value contains the target
287 # (Function for test scripts)
290 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
291 __var_test "CR" "$CR_SERVICE_PATH/counter/" $1 "contain_str" $2 $3
294 __print_err "needs two or three args: <sim-param> <target-value> [ timeout ]"
299 # Read a variable value from CR sim and send to stdout. Arg: <variable-name>
301 echo "$(__do_curl $CR_SERVICE_PATH/counter/$1)"
304 # Function to configure write delay on callbacks
305 # Delay given in seconds.
306 # arg <response-code> <delay-in-sec>
307 # (Function for test scripts)
308 cr_delay_callback() {
311 if [ $# -ne 2 ]; then
312 __print_err "<response-code> <delay-in-sec>]" $@
316 res="$(__do_curl_to_api CR POST /forcedelay?delay=$2)"
317 status=${res:${#res}-3}
319 if [ $status -ne 200 ]; then
320 __log_conf_fail_status_code $1 $status
328 # CR API: Check the contents of all current ric sync events for one id from PMS
329 # <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
330 # (Function for test scripts)
331 cr_api_check_all_sync_events() {
334 if [ "$PMS_VERSION" != "V2" ]; then
335 __log_test_fail_not_supported
339 if [ $# -lt 2 ]; then
340 __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
344 query="/get-all-events/"$2
345 res="$(__do_curl_to_api CR GET $query)"
346 status=${res:${#res}-3}
348 if [ $status -ne $1 ]; then
349 __log_test_fail_status_code $1 $status
353 if [ $# -gt 2 ]; then
354 body=${res:0:${#res}-3}
355 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
361 for ((i=0; i<$(($#-2)); i=i+1)); do
363 if [ "$targetJson" != "[" ]; then
364 targetJson=$targetJson","
366 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
370 targetJson=$targetJson"]"
371 echo "TARGET JSON: $targetJson" >> $HTTPLOG
372 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
374 if [ $res -ne 0 ]; then
383 # CR API: Check the contents of all current status events for one id from ECS
384 # <response-code> <id> [ EMPTY | ( <status> )+ ]
385 # (Function for test scripts)
386 cr_api_check_all_ecs_events() {
389 if [ $# -lt 2 ]; then
390 __print_err "<response-code> <id> [ EMPTY | ( <status> )+ ]" $@
394 query="/get-all-events/"$2
395 res="$(__do_curl_to_api CR GET $query)"
396 status=${res:${#res}-3}
398 if [ $status -ne $1 ]; then
399 __log_test_fail_status_code $1 $status
403 if [ $# -gt 2 ]; then
404 body=${res:0:${#res}-3}
405 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
411 for ((i=0; i<$(($#-2)); i=i+1)); do
413 if [ "$targetJson" != "[" ]; then
414 targetJson=$targetJson","
416 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
420 targetJson=$targetJson"]"
421 echo "TARGET JSON: $targetJson" >> $HTTPLOG
422 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
424 if [ $res -ne 0 ]; then
433 # CR API: Check the contents of all current type subscription events for one id from ECS
434 # <response-code> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]
435 # (Function for test scripts)
436 cr_api_check_all_ecs_subscription_events() {
439 #Valid number of parameter 2,3,7,11
441 if [ $# -eq 2 ]; then
444 if [ $# -eq 3 ] && [ "$3" == "EMPTY" ]; then
447 variablecount=$(($#-2))
448 if [ $# -gt 3 ] && [ $(($variablecount%3)) -eq 0 ]; then
451 if [ $paramError -eq 1 ]; then
452 __print_err "<response-code> <id> [ EMPTY | ( <type-id> <schema> <registration-status> )+ ]" $@
456 query="/get-all-events/"$2
457 res="$(__do_curl_to_api CR GET $query)"
458 status=${res:${#res}-3}
460 if [ $status -ne $1 ]; then
461 __log_test_fail_status_code $1 $status
465 if [ $# -gt 2 ]; then
466 body=${res:0:${#res}-3}
468 if [ $# -gt 3 ]; then
470 for ((i=0; i<$(($#-3)); i=i+3)); do
471 if [ "$targetJson" != "[" ]; then
472 targetJson=$targetJson","
474 if [ -f ${arr[$i+1]} ]; then
475 jobfile=$(cat ${arr[$i+1]})
477 __log_test_fail_general "Job schema file "${arr[$i+1]}", does not exist"
480 targetJson=$targetJson"{\"info_type_id\":\"${arr[$i]}\",\"job_data_schema\":$jobfile,\"status\":\"${arr[$i+2]}\"}"
483 targetJson=$targetJson"]"
485 echo " TARGET JSON: $targetJson" >> $HTTPLOG
486 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
488 if [ $res -ne 0 ]; then
499 # CR API: Reset all events and counters
501 # (Function for test scripts)
505 res="$(__do_curl_to_api CR GET /reset)"
506 status=${res:${#res}-3}
508 if [ $status -ne 200 ]; then
509 __log_conf_fail_status_code $1 $status
518 # CR API: Check the contents of all json events for path
519 # <response-code> <topic-url> (EMPTY | <json-msg>+ )
520 # (Function for test scripts)
521 cr_api_check_all_genric_json_events() {
524 if [ $# -lt 3 ]; then
525 __print_err "<response-code> <topic-url> (EMPTY | <json-msg>+ )" $@
529 query="/get-all-events/"$2
530 res="$(__do_curl_to_api CR GET $query)"
531 status=${res:${#res}-3}
533 if [ $status -ne $1 ]; then
534 __log_test_fail_status_code $1 $status
537 body=${res:0:${#res}-3}
540 if [ $3 != "EMPTY" ]; then
543 while [ $# -gt 0 ]; do
544 if [ "$targetJson" != "[" ]; then
545 targetJson=$targetJson","
547 targetJson=$targetJson$1
551 targetJson=$targetJson"]"
553 echo " TARGET JSON: $targetJson" >> $HTTPLOG
554 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
556 if [ $res -ne 0 ]; then
567 # CR API: Check a single (oldest) json event (or none if empty) for path
568 # <response-code> <topic-url> (EMPTY | <json-msg> )
569 # (Function for test scripts)
570 cr_api_check_single_genric_json_event() {
573 if [ $# -ne 3 ]; then
574 __print_err "<response-code> <topic-url> (EMPTY | <json-msg> )" $@
578 query="/get-event/"$2
579 res="$(__do_curl_to_api CR GET $query)"
580 status=${res:${#res}-3}
582 if [ $status -ne $1 ]; then
583 __log_test_fail_status_code $1 $status
586 body=${res:0:${#res}-3}
589 if [ $targetJson == "EMPTY" ] && [ ${#body} -ne 0 ]; then
593 echo " TARGET JSON: $targetJson" >> $HTTPLOG
594 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
596 if [ $res -ne 0 ]; then
605 # CR API: Check a single (oldest) json in md5 format (or none if empty) for path.
606 # Note that if a json message is given, it shall be compact, no ws except inside string.
607 # The MD5 will generate different hash if ws is present or not in otherwise equivalent json
608 # arg: <response-code> <topic-url> (EMPTY | <data-msg> )
609 # (Function for test scripts)
610 cr_api_check_single_genric_event_md5() {
613 if [ $# -ne 3 ]; then
614 __print_err "<response-code> <topic-url> (EMPTY | <data-msg> )" $@
618 query="/get-event/"$2
619 res="$(__do_curl_to_api CR GET $query)"
620 status=${res:${#res}-3}
622 if [ $status -ne $1 ]; then
623 __log_test_fail_status_code $1 $status
626 body=${res:0:${#res}-3}
627 if [ $3 == "EMPTY" ]; then
628 if [ ${#body} -ne 0 ]; then
636 command -v md5 > /dev/null # Mac
637 if [ $? -eq 0 ]; then
638 targetMd5=$(echo -n "$3" | md5)
640 command -v md5sum > /dev/null # Linux
641 if [ $? -eq 0 ]; then
642 targetMd5=$(echo -n "$3" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
644 __log_test_fail_general "Command md5 nor md5sum is available"
648 targetMd5="\""$targetMd5"\"" #Quotes needed
650 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
652 if [ "$body" != "$targetMd5" ]; then
661 # CR API: Check a single (oldest) event in md5 format (or none if empty) for path.
662 # Note that if a file with json message is given, the json shall be compact, no ws except inside string and not newlines.
663 # The MD5 will generate different hash if ws/newlines is present or not in otherwise equivalent json
664 # arg: <response-code> <topic-url> (EMPTY | <data-file> )
665 # (Function for test scripts)
666 cr_api_check_single_genric_event_md5_file() {
669 if [ $# -ne 3 ]; then
670 __print_err "<response-code> <topic-url> (EMPTY | <data-file> )" $@
674 query="/get-event/"$2
675 res="$(__do_curl_to_api CR GET $query)"
676 status=${res:${#res}-3}
678 if [ $status -ne $1 ]; then
679 __log_test_fail_status_code $1 $status
682 body=${res:0:${#res}-3}
683 if [ $3 == "EMPTY" ]; then
684 if [ ${#body} -ne 0 ]; then
694 __log_test_fail_general "File $3 does not exist"
700 command -v md5 > /dev/null # Mac
701 if [ $? -eq 0 ]; then
702 targetMd5=$(echo -n "$filedata" | md5)
704 command -v md5sum > /dev/null # Linux
705 if [ $? -eq 0 ]; then
706 targetMd5=$(echo -n "$filedata" | md5sum | cut -d' ' -f 1) # Need to cut additional info printed by cmd
708 __log_test_fail_general "Command md5 nor md5sum is available"
712 targetMd5="\""$targetMd5"\"" #Quotes needed
714 echo " TARGET MD5 hash: $targetMd5" >> $HTTPLOG
716 if [ "$body" != "$targetMd5" ]; then