3 # ============LICENSE_START===============================================
4 # Copyright (C) 2020-2023 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 management functions and test functions for A1 Controller API
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
31 for oia_name in $ONAP_IMAGES_APP_NAMES; do
32 if [ "$oia_name" == "SDNC" ]; then
33 sdnc_suffix_tag="REMOTE_RELEASE_ONAP"
36 __check_and_create_image_var SDNC "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_IMAGE_BASE" "SDNC_A1_CONTROLLER_IMAGE_TAG" $sdnc_suffix_tag "$SDNC_DISPLAY_NAME" ""
37 __check_and_create_image_var SDNC "SDNC_DB_IMAGE" "SDNC_DB_IMAGE_BASE" "SDNC_DB_IMAGE_TAG" REMOTE_PROXY "SDNC DB" ""
41 # Pull image from remote repo or use locally built image
42 # arg: <pull-policy-override> <pull-policy-original>
43 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
44 # <pull-policy-original> Shall be used for images that does not allow overriding
45 # Both var may contain: 'remote', 'remote-remove' or 'local'
47 __check_and_pull_image $1 "$SDNC_DISPLAY_NAME" $SDNC_APP_NAME SDNC_A1_CONTROLLER_IMAGE
48 __check_and_pull_image $2 "SDNC DB" $SDNC_APP_NAME SDNC_DB_IMAGE
51 # Build image (only for simulator or interfaces stubs owned by the test environment)
52 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
53 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
55 echo -e $RED" Image for app SDNC shall never be built"$ERED
58 # Generate a string for each included image using the app display name and a docker images format string
59 # If a custom image repo is used then also the source image from the local repo is listed
60 # arg: <docker-images-format-string> <file-to-append>
62 echo -e "$SDNC_DISPLAY_NAME\t$(docker images --format $1 $SDNC_A1_CONTROLLER_IMAGE)" >> $2
63 if [ ! -z "$SDNC_A1_CONTROLLER_IMAGE_SOURCE" ]; then
64 echo -e "-- source image --\t$(docker images --format $1 $SDNC_A1_CONTROLLER_IMAGE_SOURCE)" >> $2
66 echo -e "SDNC DB\t$(docker images --format $1 $SDNC_DB_IMAGE)" >> $2
67 if [ ! -z "$SDNC_DB_IMAGE_SOURCE" ]; then
68 echo -e "-- source image --\t$(docker images --format $1 $SDNC_DB_IMAGE_SOURCE)" >> $2
72 # Scale kubernetes resources to zero
73 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
74 # This function is called for apps fully managed by the test script
75 __SDNC_kube_scale_zero() {
76 __kube_scale_all_resources $KUBE_SDNC_NAMESPACE autotest SDNC
79 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
80 # This function is called for pre-started apps not managed by the test script.
81 __SDNC_kube_scale_zero_and_wait() {
82 echo -e " SDNC replicas kept as is"
85 # Delete all kube resources for the app
86 # This function is called for apps managed by the test script.
87 __SDNC_kube_delete_all() {
88 __kube_delete_all_resources $KUBE_SDNC_NAMESPACE autotest SDNC
92 # This function is called for apps managed by the test script.
93 # args: <log-dir> <file-prefix>
94 __SDNC_store_docker_logs() {
95 if [ $RUNMODE == "KUBE" ]; then
96 kubectl $KUBECONF logs -l "autotest=SDNC" -n $KUBE_SDNC_NAMESPACE --tail=-1 > $1$2_SDNC.log 2>&1
97 podname=$(kubectl $KUBECONF get pods -n $KUBE_SDNC_NAMESPACE -l "autotest=SDNC" -o custom-columns=":metadata.name")
98 kubectl $KUBECONF exec -t -n $KUBE_SDNC_NAMESPACE $podname -- cat $SDNC_KARAF_LOG> $1$2_SDNC_karaf.log 2>&1
100 docker exec -t $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $1$2_SDNC_karaf.log 2>&1
104 # Initial setup of protocol, host and ports
105 # This function is called for apps managed by the test script.
107 __SDNC_initial_setup() {
111 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
112 # For docker, the namespace shall be excluded
113 # This function is called for apps managed by the test script as well as for pre-started apps.
115 __SDNC_statistics_setup() {
116 if [ $RUNMODE == "KUBE" ]; then
117 echo "SDNC $SDNC_APP_NAME $KUBE_SDNC_NAMESPACE"
119 echo "SDNC $SDNC_APP_NAME"
123 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
125 __SDNC_test_requirements() {
129 #######################################################
131 # Set http as the protocol to use for all communication to SDNC
133 # (Function for test scripts)
135 __sdnc_set_protocoll "http" $SDNC_INTERNAL_PORT $SDNC_EXTERNAL_PORT
138 # Set https as the protocol to use for all communication to SDNC
140 # (Function for test scripts)
142 __sdnc_set_protocoll "https" $SDNC_INTERNAL_SECURE_PORT $SDNC_EXTERNAL_SECURE_PORT
145 # Setup paths to svc/container for internal and external access
146 # args: <protocol> <internal-port> <external-port>
147 __sdnc_set_protocoll() {
148 echo -e $BOLD"$SDNC_DISPLAY_NAME protocol setting"$EBOLD
149 echo -e " Using $BOLD $1 $EBOLD towards $SDNC_DISPLAY_NAME"
153 SDNC_SERVICE_PATH=$1"://"$SDNC_APP_NAME":"$2 # docker access, container->container and script->container via proxy
154 SDNC_SERVICE_API_PATH=$1"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_APP_NAME":"$2$SDNC_API_URL
155 if [ $RUNMODE == "KUBE" ]; then
156 SDNC_SERVICE_PATH=$1"://"$SDNC_APP_NAME.$KUBE_SDNC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
157 SDNC_SERVICE_API_PATH=$1"://"$SDNC_USER":"$SDNC_PWD"@"$SDNC_APP_NAME.$KUBE_SDNC_NAMESPACE":"$3$SDNC_API_URL
163 # Export env vars for config files, docker compose and kube resources
165 __sdnc_export_vars() {
166 export KUBE_SDNC_NAMESPACE
167 export DOCKER_SIM_NWNAME
170 export SDNC_DISPLAY_NAME
172 export SDNC_A1_CONTROLLER_IMAGE
173 export SDNC_INTERNAL_PORT
174 export SDNC_EXTERNAL_PORT
175 export SDNC_INTERNAL_SECURE_PORT
176 export SDNC_EXTERNAL_SECURE_PORT
177 export SDNC_A1_TRUSTSTORE_PASSWORD
178 export SDNC_DB_APP_NAME
188 # Start the SDNC A1 Controller
190 # (Function for test scripts)
193 echo -e $BOLD"Starting $SDNC_DISPLAY_NAME"$EBOLD
195 if [ $RUNMODE == "KUBE" ]; then
197 # Check if app shall be fully managed by the test script
198 __check_included_image "SDNC"
201 # Check if app shall only be used by the test script
202 __check_prestarted_image "SDNC"
205 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
206 echo -e $RED"The $SDNC_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
207 echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
210 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
211 echo -e $RED"The $SDNC_APP_NAME app is included both as managed and prestarted in this test script"$ERED
212 echo -e $RED"The $SDNC_APP_NAME will not be started"$ERED
217 if [ $retcode_p -eq 0 ]; then
218 echo -e " Using existing $SDNC_APP_NAME deployment and service"
219 echo " Setting SDNC replicas=1"
220 __kube_scale deployment $SDNC_APP_NAME $KUBE_SDNC_NAMESPACE 1
223 # Check if app shall be fully managed by the test script
224 if [ $retcode_i -eq 0 ]; then
226 echo -e " Creating $SDNC_APP_NAME app and expose service"
228 #Check if namespace exists, if not create it
229 __kube_create_namespace $KUBE_SDNC_NAMESPACE
234 input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"svc.yaml
235 output_yaml=$PWD/tmp/sdnc_svc.yaml
236 __kube_create_instance service $SDNC_APP_NAME $input_yaml $output_yaml
239 input_yaml=$SIM_GROUP"/"$SDNC_COMPOSE_DIR"/"$SDNC_KUBE_APP_FILE
240 output_yaml=$PWD/tmp/sdnc_app.yaml
241 __kube_create_instance app $SDNC_APP_NAME $input_yaml $output_yaml
245 __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
248 __check_included_image 'SDNC'
249 if [ $? -eq 1 ]; then
250 echo -e $RED"The SDNC A1 Controller app is not included in this test script"$ERED
251 echo -e $RED"The A1PMS will not be started"$ERED
257 __start_container $SDNC_COMPOSE_DIR $SDNC_COMPOSE_FILE NODOCKERARGS 1 $SDNC_APP_NAME
259 __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
269 # (Function for test scripts)
271 echo -e $BOLD"Stopping $SDNC_DISPLAY_NAME"$EBOLD
273 if [ $RUNMODE == "KUBE" ]; then
274 __log_conf_fail_not_supported " Cannot stop sndc in KUBE mode"
277 docker stop $SDNC_APP_NAME &> ./tmp/.dockererr
278 if [ $? -ne 0 ]; then
279 __print_err "Could not stop $SDNC_APP_NAME" $@
285 echo -e $BOLD$GREEN"Stopped"$EGREEN$EBOLD
290 # Start a previously stopped sdnc
292 # (Function for test scripts)
293 start_stopped_sdnc() {
294 echo -e $BOLD"Starting (the previously stopped) $SDNC_DISPLAY_NAME"$EBOLD
296 if [ $RUNMODE == "KUBE" ]; then
297 __log_conf_fail_not_supported " Cannot restart sndc in KUBE mode"
300 docker start $SDNC_APP_NAME &> ./tmp/.dockererr
301 if [ $? -ne 0 ]; then
302 __print_err "Could not start (the stopped) $SDNC_APP_NAME" $@
308 __check_service_start $SDNC_APP_NAME $SDNC_SERVICE_PATH$SDNC_ALIVE_URL
309 if [ $? -ne 0 ]; then
316 # Check the sdnc logs for WARNINGs and ERRORs
318 # (Function for test scripts)
320 __check_container_logs "SDNC A1 Controller" $SDNC_APP_NAME $SDNC_KARAF_LOG WARN ERROR
323 # Generic function to query the RICs via the A1-controller API.
324 # args: <operation> <url> [<body>]
325 # <operation>: getA1Policy,putA1Policy,getA1PolicyType,deleteA1Policy,getA1PolicyStatus
326 # response: <json-body><3-digit-response-code>
327 # (Not for test scripts)
328 __do_curl_to_controller() {
329 echo " (${BASH_LINENO[0]}): ${FUNCNAME[0]}" $@ >> $HTTPLOG
330 if [ $# -ne 2 ] && [ $# -ne 3 ]; then
332 echo "-Incorrect number of parameters to __do_curl_to_controller " $@ >> $HTTPLOG
333 echo "-Expected: <operation> <url> [<body>]" >> $HTTPLOG
334 echo "-Returning response 000" >> $HTTPLOG
338 if [ $# -eq 2 ]; then
339 json='{"input":{"near-rt-ric-url":"'$2'"}}'
341 # Escape quotes in the body
342 body=$(echo "$3" | sed 's/"/\\"/g')
343 json='{"input":{"near-rt-ric-url":"'$2'","body":"'"$body"'"}}'
345 payload="./tmp/.sdnc.payload.json"
346 echo "$json" > $payload
347 echo " FILE ($payload) : $json" >> $HTTPLOG
349 if [ ! -z "$KUBE_PROXY_PATH" ]; then
350 if [ $KUBE_PROXY_HTTPX == "http" ]; then
351 proxyflag=" --proxy $KUBE_PROXY_PATH"
353 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
356 curlString="curl -skw %{http_code} $proxyflag -X POST $SDNC_SERVICE_API_PATH$1 -H accept:application/json -H Content-Type:application/json --data-binary @$payload"
357 echo " CMD: "$curlString >> $HTTPLOG
360 echo " RESP: "$res >> $HTTPLOG
361 if [ $retcode -ne 0 ]; then
362 echo " RETCODE: "$retcode >> $HTTPLOG
367 status=${res:${#res}-3}
369 if [ $status -ne 200 ]; then
373 body=${res:0:${#res}-3}
374 echo " JSON: "$body >> $HTTPLOG
375 reply="./tmp/.sdnc-reply.json"
376 echo "$body" > $reply
377 res=$(python3 ../common/extract_sdnc_reply.py $SDNC_RESPONSE_JSON_KEY $reply)
378 echo " EXTRACED BODY+CODE: "$res >> $HTTPLOG
383 # Controller API Test function: getA1Policy (return ids only)
384 # arg: <response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )
385 # (Function for test scripts)
386 controller_api_get_A1_policy_ids() {
389 ric_id=$(__find_sim_host $3)
391 if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
392 url="$ric_id/a1-p/policytypes/$4/policies"
394 elif [ $# -gt 2 ] && [ $2 == "STD" ]; then
395 url="$ric_id/A1-P/v1/policies"
399 if [ $paramError -ne 0 ]; then
400 __print_err "<response-code> (OSC <ric-id> <policy-type-id> [ <policy-id> [<policy-id>]* ]) | ( STD <ric-id> [ <policy-id> [<policy-id>]* ] )" $@
404 res=$(__do_curl_to_controller getA1Policy "$url")
406 status=${res:${#res}-3}
408 if [ $retcode -ne 0 ]; then
409 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
413 if [ $status -ne $1 ]; then
414 __log_test_fail_status_code $1 $status
417 body=${res:0:${#res}-3}
421 if [ $2 == "OSC" ]; then
424 for pid in ${@:$start} ; do
425 if [ "$targetJson" != "[" ]; then
426 targetJson=$targetJson","
428 targetJson=$targetJson"\"$UUID$pid\""
430 targetJson=$targetJson"]"
432 echo " TARGET JSON: $targetJson" >> $HTTPLOG
434 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
436 if [ $res -ne 0 ]; then
446 # Controller API Test function: getA1PolicyType
447 # arg: <response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]
448 # (Function for test scripts)
449 controller_api_get_A1_policy_type() {
452 ric_id=$(__find_sim_host $3)
454 if [ $# -gt 3 ] && [ $2 == "OSC" ]; then
455 url="$ric_id/a1-p/policytypes/$4"
459 if [ $paramError -ne 0 ]; then
460 __print_err "<response-code> OSC <ric-id> <policy-type-id> [<policy-type-file>]" $@
464 res=$(__do_curl_to_controller getA1PolicyType "$url")
466 status=${res:${#res}-3}
468 if [ $retcode -ne 0 ]; then
469 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
473 if [ $status -ne $1 ]; then
474 __log_test_fail_status_code $1 $status
477 body=${res:0:${#res}-3}
479 if [ $# -eq 5 ]; then
481 body=${res:0:${#res}-3}
484 echo " TARGET JSON: $targetJson" >> $HTTPLOG
485 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
487 if [ $res -ne 0 ]; then
497 # Controller API Test function: deleteA1Policy
498 # arg: <response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)
499 # (Function for test scripts)
500 controller_api_delete_A1_policy() {
503 ric_id=$(__find_sim_host $3)
505 if [ $# -eq 5 ] && [ $2 == "OSC" ]; then
506 url="$ric_id/a1-p/policytypes/$4/policies/$UUID$5"
508 elif [ $# -eq 4 ] && [ $2 == "STD" ]; then
509 url="$ric_id/A1-P/v1/policies/$UUID$4"
513 if [ $paramError -ne 0 ]; then
514 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
518 res=$(__do_curl_to_controller deleteA1Policy "$url")
520 status=${res:${#res}-3}
522 if [ $retcode -ne 0 ]; then
523 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
527 if [ $status -ne $1 ]; then
528 __log_test_fail_status_code $1 $status
536 # Controller API Test function: putA1Policy
537 # arg: <response-code> (STD <ric-id> <policy-id> <template-file> ) | (OSC <ric-id> <policy-type-id> <policy-id> <template-file>)
538 # (Function for test scripts)
539 controller_api_put_A1_policy() {
542 ric_id=$(__find_sim_host $3)
544 if [ $# -eq 6 ] && [ $2 == "OSC" ]; then
545 url="$ric_id/a1-p/policytypes/$4/policies/$UUID$5"
546 body=$(sed 's/XXX/'${5}'/g' $6)
549 elif [ $# -eq 5 ] && [ $2 == "STD" ]; then
550 url="$ric_id/A1-P/v1/policies/$UUID$4"
551 body=$(sed 's/XXX/'${4}'/g' $5)
555 if [ $paramError -ne 0 ]; then
556 __print_err "<response-code> (STD <ric-id> <policy-id>) | (OSC <ric-id> <policy-type-id> <policy-id>)" $@
560 res=$(__do_curl_to_controller putA1Policy "$url" "$body")
562 status=${res:${#res}-3}
564 if [ $retcode -ne 0 ]; then
565 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
569 if [ $status -ne $1 ]; then
570 __log_test_fail_status_code $1 $status
579 # Controller API Test function: getA1PolicyStatus
580 # arg: <response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)
581 # (Function for test scripts)
582 controller_api_get_A1_policy_status() {
585 ric_id=$(__find_sim_host $3)
588 if [ $# -ge 5 ] && [ $2 == "OSC" ]; then
589 url="$ric_id/a1-p/policytypes/$4/policies/$UUID$5/status"
590 if [ $# -gt 5 ]; then
591 targetJson="{\"instance_status\":\"$6\""
592 targetJson=$targetJson",\"has_been_deleted\":\"$7\""
593 targetJson=$targetJson",\"created_at\":\"????\"}"
596 elif [ $# -ge 4 ] && [ $2 == "STD" ]; then
597 url="$ric_id/A1-P/v1/policies/$UUID$4/status"
598 if [ $# -gt 4 ]; then
599 targetJson="{\"enforceStatus\":\"$5\""
600 if [ $# -eq 6 ]; then
601 targetJson=$targetJson",\"reason\":\"$6\""
603 targetJson=$targetJson"}"
608 if [ $paramError -ne 0 ]; then
609 __print_err "<response-code> (STD <ric-id> <policy-id> <enforce-status> [<reason>]) | (OSC <ric-id> <policy-type-id> <policy-id> <instance-status> <has-been-deleted>)" $@
613 res=$(__do_curl_to_controller getA1PolicyStatus "$url")
615 status=${res:${#res}-3}
617 if [ $retcode -ne 0 ]; then
618 __log_test_fail_status_code $1 $retcode "(likely remote server error)"
622 if [ $status -ne $1 ]; then
623 __log_test_fail_status_code $1 $status
627 if [ ! -z "$targetJson" ]; then
629 body=${res:0:${#res}-3}
630 echo " TARGET JSON: $targetJson" >> $HTTPLOG
631 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
633 if [ $res -ne 0 ]; then
643 # Wait for http status on url
644 # args: <response-code> <ric-id>
645 # (Function for test scripts)
646 controller_api_wait_for_status_ok() {
649 if [ $# -ne 2 ]; then
650 __print_err "<response-code> <ric-id> " $@
653 ric_id=$(__find_sim_host $2)
657 while [ $(($TS_START+500)) -gt $SECONDS ]; do
658 echo -ne " Waiting for http status $1 on $url via sdnc, waited: $(($SECONDS-$TS_START))"$SAMELINE
659 res=$(__do_curl_to_controller getA1Policy "$url")
661 status=${res:${#res}-3}
662 if [ $retcode -eq 0 ]; then
663 if [ $status -eq $1 ]; then
672 __log_conf_fail_general