3 # ============LICENSE_START===============================================
4 # Copyright (C) 2023 Nordix Foundation. All rights reserved.
5 # Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
6 # ========================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=================================================
21 # This is a script that contains container/service management functions and test functions for RICMEDIATORSIM A1 simulators
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
28 __RICMEDIATORSIM_imagesetup() {
29 __check_and_create_image_var RICMEDIATORSIM "RICMEDIATOR_SIM_IMAGE" "RICMEDIATOR_SIM_IMAGE_BASE" "RICMEDIATOR_SIM_IMAGE_TAG" REMOTE_RELEASE_ORAN "$RICMEDIATOR_SIM_DISPLAY_NAME" ""
30 __check_and_create_image_var RICMEDIATORSIM "RICMEDIATOR_SIM_DB_IMAGE" "RICMEDIATOR_SIM_DB_IMAGE_BASE" "RICMEDIATOR_SIM_DB_IMAGE_TAG" REMOTE_RELEASE_ORAN "$RICMEDIATOR_SIM_DB_DISPLAY_NAME" ""
33 # Pull image from remote repo or use locally built image
34 # arg: <pull-policy-override> <pull-policy-original>
35 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
36 # <pull-policy-original> Shall be used for images that does not allow overriding
37 # Both var may contain: 'remote', 'remote-remove' or 'local'
38 __RICMEDIATORSIM_imagepull() {
39 __check_and_pull_image $1 "$RICMEDIATOR_SIM_DISPLAY_NAME" $RICMEDIATOR_SIM_PREFIX"_"$RICMEDIATOR_SIM_BASE RICMEDIATOR_SIM_IMAGE
40 __check_and_pull_image $1 "$RICMEDIATOR_SIM_DB_DISPLAY_NAME" $RICMEDIATOR_SIM_PREFIX"_"$RICMEDIATOR_SIM_BASE RICMEDIATOR_SIM_DB_IMAGE
43 # Build image (only for simulator or interfaces stubs owned by the test environment)
44 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
45 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
46 __RICMEDIATORSIM_imagebuild() {
47 echo -e $RED" Image for app RICMEDIATORSIM shall never be built"$ERED
50 # Generate a string for each included image using the app display name and a docker images format string
51 # If a custom image repo is used then also the source image from the local repo is listed
52 # arg: <docker-images-format-string> <file-to-append>
53 __RICMEDIATORSIM_image_data() {
54 echo -e "$RICMEDIATOR_SIM_DISPLAY_NAME\t$(docker images --format $1 $RICMEDIATOR_SIM_IMAGE)" >> $2
55 if [ ! -z "$RICMEDIATOR_SIM_IMAGE_SOURCE" ]; then
56 echo -e "-- source image --\t$(docker images --format $1 $RICMEDIATOR_SIM_IMAGE_SOURCE)" >> $2
58 echo -e "$RICMEDIATOR_SIM_DB_DISPLAY_NAME\t$(docker images --format $1 $RICMEDIATOR_SIM_DB_IMAGE)" >> $2
59 if [ ! -z "$RICMEDIATOR_SIM_DB_IMAGE_SOURCE" ]; then
60 echo -e "-- source image --\t$(docker images --format $1 $RICMEDIATOR_SIM_DB_IMAGE_SOURCE)" >> $2
64 # Scale kubernetes resources to zero
65 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
66 # This function is called for apps fully managed by the test script
67 __RICMEDIATORSIM_kube_scale_zero() {
68 __kube_scale_all_resources $KUBE_A1SIM_NAMESPACE autotest RICMEDIATORSIM
71 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
72 # This function is called for pre-started apps not managed by the test script.
73 __RICMEDIATORSIM_kube_scale_zero_and_wait() {
74 __kube_scale_and_wait_all_resources $KUBE_A1SIM_NAMESPACE app $KUBE_A1SIM_NAMESPACE"-neara1simulator"
77 # Delete all kube resources for the app
78 # This function is called for apps managed by the test script.
79 __RICMEDIATORSIM_kube_delete_all() {
80 __kube_delete_all_resources $KUBE_A1SIM_NAMESPACE autotest RICMEDIATORSIM
84 # This function is called for apps managed by the test script.
85 # args: <log-dir> <file-prefix>
86 __RICMEDIATORSIM_store_docker_logs() {
87 if [ $RUNMODE == "KUBE" ]; then
88 for podname in $(kubectl $KUBECONF get pods -n $KUBE_A1SIM_NAMESPACE -l "autotest=RICMEDIATORSIM" -o custom-columns=":metadata.name"); do
89 kubectl $KUBECONF logs -n $KUBE_A1SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
93 RICMEDIATORs=$(docker ps --filter "name=$RICMEDIATOR_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --filter "label=orana1sim" --format {{.Names}})
95 docker logs $ric > $1$2_$ric.log 2>&1
100 # Initial setup of protocol, host and ports
101 # This function is called for apps managed by the test script.
103 __RICMEDIATORSIM_initial_setup() {
104 use_ricmediator_simulator_http
107 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
108 # For docker, the namespace shall be excluded
109 # This function is called for apps managed by the test script as well as for pre-started apps.
111 __RICMEDIATORSIM_statistics_setup() {
112 for ((RICMEDIATOR_SIMINSTANCE=10; RICMEDIATOR_SIMINSTANCE>0; RICMEDIATOR_SIMINSTANCE-- )); do
113 if [ $RUNMODE == "KUBE" ]; then
114 RICMEDIATOR_SIMINSTANCE_KUBE=$(($RICMEDIATOR_SIMINSTANCE-1))
115 echo -n " RICMEDIATOR_SIMG1_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g1-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
116 echo -n " RICMEDIATOR_SIMG2_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g2-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
117 echo -n " RICMEDIATOR_SIMG3_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g3-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
118 echo -n " RICMEDIATOR_SIMG4_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g4-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
120 echo -n " RICMEDIATOR_SIMG1_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g1-$RICMEDIATOR_SIMINSTANCE "
121 echo -n " RICMEDIATOR_SIMG2_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g2-$RICMEDIATOR_SIMINSTANCE "
122 echo -n " RICMEDIATOR_SIMG3_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g3-$RICMEDIATOR_SIMINSTANCE "
123 echo -n " RICMEDIATOR_SIMG4_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g4-$RICMEDIATOR_SIMINSTANCE "
128 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
130 __RICMEDIATORSIM_test_requirements() {
134 #######################################################
137 RICMEDIATOR_SIM_HTTPX="http"
138 RICMEDIATOR_SIM_PORT=$RICMEDIATOR_SIM_INTERNAL_PORT
141 #Vars for container count
149 ###########################
150 ### RICMEDIATOR Simulator functions
151 ###########################
153 use_ricmediator_simulator_http() {
154 echo -e $BOLD"RICMEDIATORSIM protocol setting"$EBOLD
155 echo -e " Using $BOLD http $EBOLD towards the simulators"
156 RICMEDIATOR_SIM_HTTPX="http"
157 RICMEDIATOR_SIM_PORT=$RICMEDIATOR_SIM_INTERNAL_PORT
161 use_ricmediator_simulator_https() {
162 __log_test_fail_not_supported
165 # Start one group (ricsim_g1, ricsim_g2 .. ricsim_g5) with a number of RIC Simulators using a given A interface
166 # 'ricsim' may be set on command line to other prefix
167 # args: ricsim_g1|ricsim_g2|ricsim_g3|ricsim_g4|ricsim_g5 <count> <interface-id>
168 # (Function for test scripts)
169 start_ricmediator_simulators() {
171 echo -e $BOLD"Starting $RICMEDIATOR_SIM_DISPLAY_NAME"$EBOLD
173 if [ $RUNMODE == "KUBE" ]; then
175 # Check if app shall be fully managed by the test script
176 __check_included_image "RICMEDIATORSIM"
179 # Check if app shall only be used by the test script
180 __check_prestarted_image "RICMEDIATORSIM"
183 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
184 echo -e $RED"The $1 app is not included as managed nor prestarted in this test script"$ERED
185 echo -e $RED"The $1 will not be started"$ERED
188 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
189 echo -e $RED"The $1 stub app is included both as managed and prestarted in this test script"$ERED
190 echo -e $RED"The $1 will not be started"$ERED
194 if [ $retcode_p -eq 0 ]; then
195 echo -e " Using existing $1 statefulset and service"
196 echo " Using existing simulator deployment and service for statefulset $1"
197 echo " Setting $1 replicas=$2"
198 __kube_scale statefulset $1 $KUBE_A1SIM_NAMESPACE $2
204 RICMEDIATOR1=$RICMEDIATOR_SIM_PREFIX"_g1"
205 RICMEDIATOR2=$RICMEDIATOR_SIM_PREFIX"_g2"
206 RICMEDIATOR3=$RICMEDIATOR_SIM_PREFIX"_g3"
207 RICMEDIATOR4=$RICMEDIATOR_SIM_PREFIX"_g4"
208 RICMEDIATOR5=$RICMEDIATOR_SIM_PREFIX"_g5"
212 __print_err "need three args, $RICMEDIATOR1|$RICMEDIATOR2|$RICMEDIATOR3|$RICMEDIATOR4|$RICMEDIATOR5 <count> <interface-id>" $@
216 echo " $2 simulators using basename: $1 on interface: $3"
217 #Set env var for simulator count and A1 interface vesion for the given group
218 if [ $1 == "$RICMEDIATOR1" ]; then
220 elif [ $1 == "$RICMEDIATOR2" ]; then
222 elif [ $1 == "$RICMEDIATOR3" ]; then
224 elif [ $1 == "$RICMEDIATOR4" ]; then
226 elif [ $1 == "$RICMEDIATOR5" ]; then
230 __print_err "need three args, $RICMEDIATOR1|$RICMEDIATOR2|$RICMEDIATOR3|$RICMEDIATOR4|$RICMEDIATOR5 <count> <interface-id>" $@
234 if [ $RUNMODE == "KUBE" ]; then
236 if [ $retcode_i -eq 0 ]; then
238 #export needed env var for statefulset
239 export RICMEDIATOR_SIM_SET_NAME=$(echo "$1" | tr '_' '-') #kube does not accept underscore in names
240 export KUBE_A1SIM_NAMESPACE
241 export RICMEDIATOR_SIM_IMAGE
242 export RICMEDIATOR_SIM_DB_IMAGE
243 #Adding 1 more instance, instance 0 is never used. This is done to keep test scripts compatible
244 # with docker that starts instance index on 1.....
245 export RICMEDIATOR_SIM_COUNT=$(($2+1))
247 export RICMEDIATOR_SIM_INTERNAL_PORT
248 export RICMEDIATOR_SIM_INTERNAL_SECURE_PORT
250 echo -e " Creating $RICMEDIATOR_SIM_PREFIX app and expose service"
252 #Check if nonrtric namespace exists, if not create it
253 __kube_create_namespace $KUBE_A1SIM_NAMESPACE
256 input_yaml=$SIM_GROUP"/"$RICMEDIATOR_SIM_COMPOSE_DIR"/"svc.yaml
257 output_yaml=$PWD/tmp/ric_${1}_svc.yaml
258 __kube_create_instance service $RICMEDIATOR_SIM_SET_NAME $input_yaml $output_yaml
261 input_yaml=$SIM_GROUP"/"$RICMEDIATOR_SIM_COMPOSE_DIR"/"app.yaml
262 output_yaml=$PWD/tmp/ric_${1}_app.yaml
263 __kube_create_instance app $RICMEDIATOR_SIM_SET_NAME $input_yaml $output_yaml
265 #Using only instance from index 1 to keep compatability with docker
266 for (( count=1; count<${RICMEDIATOR_SIM_COUNT}; count++ )); do
267 host=$(__find_ricmediatorsim_host $RICMEDIATOR_SIM_SET_NAME"-"$count)
268 __check_service_start $RICMEDIATOR_SIM_SET_NAME"-"$count $host$RICMEDIATOR_SIM_ALIVE_URL
272 __check_included_image 'RICMEDIATORSIM'
273 if [ $? -eq 1 ]; then
274 echo -e $RED"The Near-RT RICMEDIATOR Simulator app is not included as managed in this test script"$ERED
275 echo -e $RED"Near-RT RICMEDIATOR Simulator will not be started"$ERED
279 # Create .env file to compose project, all ric container will get this prefix
280 echo "COMPOSE_PROJECT_NAME="$RICMEDIATOR_SIM_PREFIX > $SIM_GROUP/$RICMEDIATOR_SIM_COMPOSE_DIR/.env
282 #extract service name (group), g1, g2, g3, g4 or g5 from var $1
283 #E.g. ricsim_g1 -> g1 is the service name
285 RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME=$(echo "${TMP_GRP##*_}")
287 export RICMEDIATOR_SIMCOMPOSE_A1_VERSION=$3
288 export RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME
289 export RICMEDIATOR_SIM_INTERNAL_PORT
290 export RICMEDIATOR_SIM_INTERNAL_SECURE_PORT
291 export RICMEDIATOR_SIM_CERT_MOUNT_DIR
292 export DOCKER_SIM_NWNAME
293 export RICMEDIATOR_SIM_DISPLAY_NAME
295 docker_args=" --scale $RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME=$2"
297 #Create a list of container names
298 #Will be <ricsim-prefix>_<service-name>_<index>
300 # <ricsim-prefix>-<service-name>-<index>
303 app_name_prefix=$RICMEDIATOR_SIM_PREFIX"-"$RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME"-"
304 while [ $cntr -le $2 ]; do
305 app=$app_name_prefix$cntr
306 app_data="$app_data $app"
310 __start_container $RICMEDIATOR_SIM_COMPOSE_DIR "" "$docker_args" $2 $app_data
313 while [ $cntr -le $2 ]; do
314 app=$RICMEDIATOR_SIM_PREFIX"-"$RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME"-"$cntr
315 __check_service_start $app $RICMEDIATOR_SIM_HTTPX"://"$app:$RICMEDIATOR_SIM_PORT$RICMEDIATOR_SIM_ALIVE_URL
324 # Translate ric name to kube host name
327 get_kube_ricmediatorsim_host() {
328 name=$(echo "$1" | tr '_' '-') #kube does not accept underscore in names
329 #example gnb_1_2 -> gnb-1-2
330 set_name=$(echo $name | rev | cut -d- -f2- | rev) # Cut index part of ric name to get the name of statefulset
331 # example gnb-g1-2 -> gnb-g1 where gnb-g1-2 is the ric name and gnb-g1 is the set name
332 echo $name"."$set_name"."$KUBE_A1SIM_NAMESPACE
335 # Helper function to get a the port and host name of a specific ric simulator
337 # (Not for test scripts)
338 __find_ricmediatorsim_host() {
339 if [ $RUNMODE == "KUBE" ]; then
340 ricname=$(echo "$1" | tr '_' '-') # Kube does not accept underscore in names as docker do
341 if [ -z "$RICMEDIATOR_SIM_COMMON_SVC_NAME" ]; then
342 ric_setname="${ricname%-*}" #Extract the stateful set name
344 ric_setname=$RICMEDIATOR_SIM_COMMON_SVC_NAME # Use the common svc name in the host name of the sims
346 echo $RICMEDIATOR_SIM_HTTPX"://"$ricname.$ric_setname.$KUBE_A1SIM_NAMESPACE":"$RICMEDIATOR_SIM_PORT
348 ricname=$(echo "$1" | tr '_' '-')
349 echo $RICMEDIATOR_SIM_HTTPX"://"$ricname":"$RICMEDIATOR_SIM_PORT
353 # Generate a UUID to use as prefix for policy ids
354 nearsim_generate_policy_uuid() {
355 UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)')
356 #Reduce length to make space for serial id, uses 'a' as marker where the serial id is added
357 UUID=${UUID:0:${#UUID}-4}"a"
360 # Execute a curl cmd towards a ricsimulator and check the response code.
361 # args: <expected-response-code> <curl-cmd-string>
362 __execute_curl_to_ricmediatorsim() {
363 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
365 if [ ! -z "$KUBE_PROXY_PATH" ]; then
366 if [ $KUBE_PROXY_HTTPX == "http" ]; then
367 proxyflag=" --proxy $KUBE_PROXY_PATH"
369 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
372 if [ -z "$KUBE_PROXY_CURL_JWT" ]; then
373 echo " CMD: $2 $proxyflag" >> $HTTPLOG
374 res="$($2 $proxyflag)"
376 echo " CMD: $2 $proxyflag -H Authorization: Bearer $KUBE_PROXY_CURL_JWT" >> $HTTPLOG
377 res=$($2 $proxyflag -H 'Authorization: Bearer '$KUBE_PROXY_CURL_JWT)
380 echo " RESP: $res" >> $HTTPLOG
382 if [ $retcode -ne 0 ]; then
384 echo " RETCODE: "$retcode
385 echo -e $RED" FAIL - fatal error when executing curl."$ERED
388 status=${res:${#res}-3}
389 if [ $status -eq $1 ]; then
390 echo -e $GREEN" OK"$EGREEN
393 echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
398 # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout.
399 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is
400 # equal to the target or not.
401 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
402 # before setting pass or fail depending on if the variable value becomes equal to the target
404 # (Function for test scripts)
405 ricmediatorsim_equal() {
406 __log_test_fail_not_supported
409 # Print a variable value from the RICMEDIATOR sim.
410 # args: <ric-id> <variable-name>
411 # (Function for test scripts)
412 ricmediatorsim_print() {
413 __log_test_info_not_supported
416 # Tests if a variable value in the RICMEDIATOR simulator contains the target string and and optional timeout
417 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
419 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
420 # before setting pass or fail depending on if the variable value contains the target
422 # (Function for test scripts)
423 ricmediatorsim_contains_str() {
424 __log_test_fail_not_supported
427 # Simulator API: Put a policy type in a ric
428 # args: <response-code> <ric-id> <policy-type-id> <policy-type-file>
429 # (Function for test scripts)
430 ricmediatorsim_put_policy_type() {
432 if [ $# -ne 4 ]; then
433 __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@
436 host=$(__find_ricmediatorsim_host $2)
437 curlString="curl -X PUT -skw %{http_code} "$host"/A1-P/v2/policytypes/"$3" -H Content-Type:application/json --data-binary @"$4
438 __execute_curl_to_ricmediatorsim $1 "$curlString"
442 # Simulator API: Delete a policy type in a ric
443 # <response-code> <ric-id> <policy-type-id>
444 # (Function for test scripts)
445 ricmediatorsim_delete_policy_type() {
447 if [ $# -ne 3 ]; then
448 __print_err "<response-code> <ric-id> <policy_type_id>" $@
451 host=$(__find_ricmediatorsim_host $2)
452 curlString="curl -X DELETE -skw %{http_code} "$host"/A1-P/v2/policytypes/"$3
453 __execute_curl_to_ricmediatorsim $1 "$curlString"
457 # Simulator API: Delete instances (and status), for one ric
458 # <response-code> <ric-id>
459 # (Function for test scripts)
460 ricmediatorsim_post_delete_instances() {
461 __log_test_fail_not_supported
464 # Simulator API: Delete all (instances/types/statuses/settings), for one ric
465 # <response-code> <ric-id>
466 # (Function for test scripts)
467 ricmediatorsim_post_delete_all() {
468 __log_test_fail_not_supported
471 # Simulator API: Set (or reset) response code for next A1 message, for one ric
472 # <response-code> <ric-id> [<forced_response_code>]
473 # (Function for test scripts)
474 ricmediatorsim_post_forcedresponse() {
475 __log_test_fail_not_supported
478 # Simulator API: Set (or reset) A1 response delay, for one ric
479 # <response-code> <ric-id> [<delay-in-seconds>]
480 # (Function for test scripts)
481 ricmediatorsim_post_forcedelay() {
482 __log_test_fail_not_supported