3 # ============LICENSE_START===============================================
4 # Copyright (C) 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 RICMEDIATORSIM A1 simulators
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 __RICMEDIATORSIM_imagesetup() {
28 __check_and_create_image_var RICMEDIATORSIM "RICMEDIATOR_SIM_IMAGE" "RICMEDIATOR_SIM_IMAGE_BASE" "RICMEDIATOR_SIM_IMAGE_TAG" REMOTE_RELEASE_ORAN "$RICMEDIATOR_SIM_DISPLAY_NAME" ""
29 __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" ""
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'
37 __RICMEDIATORSIM_imagepull() {
38 __check_and_pull_image $1 "$RICMEDIATOR_SIM_DISPLAY_NAME" $RICMEDIATOR_SIM_PREFIX"_"$RICMEDIATOR_SIM_BASE RICMEDIATOR_SIM_IMAGE
39 __check_and_pull_image $1 "$RICMEDIATOR_SIM_DB_DISPLAY_NAME" $RICMEDIATOR_SIM_PREFIX"_"$RICMEDIATOR_SIM_BASE RICMEDIATOR_SIM_DB_IMAGE
42 # Build image (only for simulator or interfaces stubs owned by the test environment)
43 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
44 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
45 __RICMEDIATORSIM_imagebuild() {
46 echo -e $RED" Image for app RICMEDIATORSIM shall never be built"$ERED
49 # Generate a string for each included image using the app display name and a docker images format string
50 # If a custom image repo is used then also the source image from the local repo is listed
51 # arg: <docker-images-format-string> <file-to-append>
52 __RICMEDIATORSIM_image_data() {
53 echo -e "$RICMEDIATOR_SIM_DISPLAY_NAME\t$(docker images --format $1 $RICMEDIATOR_SIM_IMAGE)" >> $2
54 if [ ! -z "$RICMEDIATOR_SIM_IMAGE_SOURCE" ]; then
55 echo -e "-- source image --\t$(docker images --format $1 $RICMEDIATOR_SIM_IMAGE_SOURCE)" >> $2
57 echo -e "$RICMEDIATOR_SIM_DB_DISPLAY_NAME\t$(docker images --format $1 $RICMEDIATOR_SIM_DB_IMAGE)" >> $2
58 if [ ! -z "$RICMEDIATOR_SIM_DB_IMAGE_SOURCE" ]; then
59 echo -e "-- source image --\t$(docker images --format $1 $RICMEDIATOR_SIM_DB_IMAGE_SOURCE)" >> $2
63 # Scale kubernetes resources to zero
64 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
65 # This function is called for apps fully managed by the test script
66 __RICMEDIATORSIM_kube_scale_zero() {
67 __kube_scale_all_resources $KUBE_A1SIM_NAMESPACE autotest RICMEDIATORSIM
70 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
71 # This function is called for pre-started apps not managed by the test script.
72 __RICMEDIATORSIM_kube_scale_zero_and_wait() {
73 __kube_scale_and_wait_all_resources $KUBE_A1SIM_NAMESPACE app $KUBE_A1SIM_NAMESPACE"-neara1simulator"
76 # Delete all kube resources for the app
77 # This function is called for apps managed by the test script.
78 __RICMEDIATORSIM_kube_delete_all() {
79 __kube_delete_all_resources $KUBE_A1SIM_NAMESPACE autotest RICMEDIATORSIM
83 # This function is called for apps managed by the test script.
84 # args: <log-dir> <file-prefix>
85 __RICMEDIATORSIM_store_docker_logs() {
86 if [ $RUNMODE == "KUBE" ]; then
87 for podname in $(kubectl $KUBECONF get pods -n $KUBE_A1SIM_NAMESPACE -l "autotest=RICMEDIATORSIM" -o custom-columns=":metadata.name"); do
88 kubectl $KUBECONF logs -n $KUBE_A1SIM_NAMESPACE $podname --tail=-1 > $1$2_$podname.log 2>&1
92 RICMEDIATORs=$(docker ps --filter "name=$RICMEDIATOR_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --filter "label=orana1sim" --format {{.Names}})
94 docker logs $ric > $1$2_$ric.log 2>&1
99 # Initial setup of protocol, host and ports
100 # This function is called for apps managed by the test script.
102 __RICMEDIATORSIM_initial_setup() {
103 use_ricmediator_simulator_http
106 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes pods or docker containers
107 # For docker, the namespace shall be excluded
108 # This function is called for apps managed by the test script as well as for pre-started apps.
110 __RICMEDIATORSIM_statistics_setup() {
111 for ((RICMEDIATOR_SIMINSTANCE=10; RICMEDIATOR_SIMINSTANCE>0; RICMEDIATOR_SIMINSTANCE-- )); do
112 if [ $RUNMODE == "KUBE" ]; then
113 RICMEDIATOR_SIMINSTANCE_KUBE=$(($RICMEDIATOR_SIMINSTANCE-1))
114 echo -n " RICMEDIATOR_SIMG1_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g1-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
115 echo -n " RICMEDIATOR_SIMG2_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g2-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
116 echo -n " RICMEDIATOR_SIMG3_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g3-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
117 echo -n " RICMEDIATOR_SIMG4_$RICMEDIATOR_SIMINSTANCE_KUBE ${RICMEDIATOR_SIM_PREFIX}-g4-$RICMEDIATOR_SIMINSTANCE_KUBE $KUBE_A1SIM_NAMESPACE "
119 echo -n " RICMEDIATOR_SIMG1_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g1-$RICMEDIATOR_SIMINSTANCE "
120 echo -n " RICMEDIATOR_SIMG2_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g2-$RICMEDIATOR_SIMINSTANCE "
121 echo -n " RICMEDIATOR_SIMG3_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g3-$RICMEDIATOR_SIMINSTANCE "
122 echo -n " RICMEDIATOR_SIMG4_$RICMEDIATOR_SIMINSTANCE ${RICMEDIATOR_SIM_PREFIX}-g4-$RICMEDIATOR_SIMINSTANCE "
127 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
129 __RICMEDIATORSIM_test_requirements() {
133 #######################################################
136 RICMEDIATOR_SIM_HTTPX="http"
137 RICMEDIATOR_SIM_PORT=$RICMEDIATOR_SIM_INTERNAL_PORT
140 #Vars for container count
148 ###########################
149 ### RICMEDIATOR Simulator functions
150 ###########################
152 use_ricmediator_simulator_http() {
153 echo -e $BOLD"RICMEDIATORSIM protocol setting"$EBOLD
154 echo -e " Using $BOLD http $EBOLD towards the simulators"
155 RICMEDIATOR_SIM_HTTPX="http"
156 RICMEDIATOR_SIM_PORT=$RICMEDIATOR_SIM_INTERNAL_PORT
160 use_ricmediator_simulator_https() {
161 __log_test_fail_not_supported
164 # Start one group (ricsim_g1, ricsim_g2 .. ricsim_g5) with a number of RIC Simulators using a given A interface
165 # 'ricsim' may be set on command line to other prefix
166 # args: ricsim_g1|ricsim_g2|ricsim_g3|ricsim_g4|ricsim_g5 <count> <interface-id>
167 # (Function for test scripts)
168 start_ricmediator_simulators() {
170 echo -e $BOLD"Starting $RICMEDIATOR_SIM_DISPLAY_NAME"$EBOLD
172 if [ $RUNMODE == "KUBE" ]; then
174 # Check if app shall be fully managed by the test script
175 __check_included_image "RICMEDIATORSIM"
178 # Check if app shall only be used by the test script
179 __check_prestarted_image "RICMEDIATORSIM"
182 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
183 echo -e $RED"The $1 app is not included as managed nor prestarted in this test script"$ERED
184 echo -e $RED"The $1 will not be started"$ERED
187 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
188 echo -e $RED"The $1 stub app is included both as managed and prestarted in this test script"$ERED
189 echo -e $RED"The $1 will not be started"$ERED
193 if [ $retcode_p -eq 0 ]; then
194 echo -e " Using existing $1 statefulset and service"
195 echo " Using existing simulator deployment and service for statefulset $1"
196 echo " Setting $1 replicas=$2"
197 __kube_scale statefulset $1 $KUBE_A1SIM_NAMESPACE $2
203 RICMEDIATOR1=$RICMEDIATOR_SIM_PREFIX"_g1"
204 RICMEDIATOR2=$RICMEDIATOR_SIM_PREFIX"_g2"
205 RICMEDIATOR3=$RICMEDIATOR_SIM_PREFIX"_g3"
206 RICMEDIATOR4=$RICMEDIATOR_SIM_PREFIX"_g4"
207 RICMEDIATOR5=$RICMEDIATOR_SIM_PREFIX"_g5"
211 __print_err "need three args, $RICMEDIATOR1|$RICMEDIATOR2|$RICMEDIATOR3|$RICMEDIATOR4|$RICMEDIATOR5 <count> <interface-id>" $@
215 echo " $2 simulators using basename: $1 on interface: $3"
216 #Set env var for simulator count and A1 interface vesion for the given group
217 if [ $1 == "$RICMEDIATOR1" ]; then
219 elif [ $1 == "$RICMEDIATOR2" ]; then
221 elif [ $1 == "$RICMEDIATOR3" ]; then
223 elif [ $1 == "$RICMEDIATOR4" ]; then
225 elif [ $1 == "$RICMEDIATOR5" ]; then
229 __print_err "need three args, $RICMEDIATOR1|$RICMEDIATOR2|$RICMEDIATOR3|$RICMEDIATOR4|$RICMEDIATOR5 <count> <interface-id>" $@
233 if [ $RUNMODE == "KUBE" ]; then
235 if [ $retcode_i -eq 0 ]; then
237 #export needed env var for statefulset
238 export RICMEDIATOR_SIM_SET_NAME=$(echo "$1" | tr '_' '-') #kube does not accept underscore in names
239 export KUBE_A1SIM_NAMESPACE
240 export RICMEDIATOR_SIM_IMAGE
241 export RICMEDIATOR_SIM_DB_IMAGE
242 #Adding 1 more instance, instance 0 is never used. This is done to keep test scripts compatible
243 # with docker that starts instance index on 1.....
244 export RICMEDIATOR_SIM_COUNT=$(($2+1))
246 export RICMEDIATOR_SIM_INTERNAL_PORT
247 export RICMEDIATOR_SIM_INTERNAL_SECURE_PORT
249 echo -e " Creating $RICMEDIATOR_SIM_PREFIX app and expose service"
251 #Check if nonrtric namespace exists, if not create it
252 __kube_create_namespace $KUBE_A1SIM_NAMESPACE
255 input_yaml=$SIM_GROUP"/"$RICMEDIATOR_SIM_COMPOSE_DIR"/"svc.yaml
256 output_yaml=$PWD/tmp/ric_${1}_svc.yaml
257 __kube_create_instance service $RICMEDIATOR_SIM_SET_NAME $input_yaml $output_yaml
260 input_yaml=$SIM_GROUP"/"$RICMEDIATOR_SIM_COMPOSE_DIR"/"app.yaml
261 output_yaml=$PWD/tmp/ric_${1}_app.yaml
262 __kube_create_instance app $RICMEDIATOR_SIM_SET_NAME $input_yaml $output_yaml
264 #Using only instance from index 1 to keep compatability with docker
265 for (( count=1; count<${RICMEDIATOR_SIM_COUNT}; count++ )); do
266 host=$(__find_ricmediatorsim_host $RICMEDIATOR_SIM_SET_NAME"-"$count)
267 __check_service_start $RICMEDIATOR_SIM_SET_NAME"-"$count $host$RICMEDIATOR_SIM_ALIVE_URL
271 __check_included_image 'RICMEDIATORSIM'
272 if [ $? -eq 1 ]; then
273 echo -e $RED"The Near-RT RICMEDIATOR Simulator app is not included as managed in this test script"$ERED
274 echo -e $RED"Near-RT RICMEDIATOR Simulator will not be started"$ERED
278 # Create .env file to compose project, all ric container will get this prefix
279 echo "COMPOSE_PROJECT_NAME="$RICMEDIATOR_SIM_PREFIX > $SIM_GROUP/$RICMEDIATOR_SIM_COMPOSE_DIR/.env
281 #extract service name (group), g1, g2, g3, g4 or g5 from var $1
282 #E.g. ricsim_g1 -> g1 is the service name
284 RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME=$(echo "${TMP_GRP##*_}")
286 export RICMEDIATOR_SIMCOMPOSE_A1_VERSION=$3
287 export RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME
288 export RICMEDIATOR_SIM_INTERNAL_PORT
289 export RICMEDIATOR_SIM_INTERNAL_SECURE_PORT
290 export RICMEDIATOR_SIM_CERT_MOUNT_DIR
291 export DOCKER_SIM_NWNAME
292 export RICMEDIATOR_SIM_DISPLAY_NAME
294 docker_args=" --scale $RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME=$2"
296 #Create a list of container names
297 #Will be <ricsim-prefix>_<service-name>_<index>
299 # <ricsim-prefix>-<service-name>-<index>
302 app_name_prefix=$RICMEDIATOR_SIM_PREFIX"-"$RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME"-"
303 while [ $cntr -le $2 ]; do
304 app=$app_name_prefix$cntr
305 app_data="$app_data $app"
309 __start_container $RICMEDIATOR_SIM_COMPOSE_DIR "" "$docker_args" $2 $app_data
312 while [ $cntr -le $2 ]; do
313 app=$RICMEDIATOR_SIM_PREFIX"-"$RICMEDIATOR_SIMCOMPOSE_SERVICE_NAME"-"$cntr
314 __check_service_start $app $RICMEDIATOR_SIM_HTTPX"://"$app:$RICMEDIATOR_SIM_PORT$RICMEDIATOR_SIM_ALIVE_URL
323 # Translate ric name to kube host name
326 get_kube_ricmediatorsim_host() {
327 name=$(echo "$1" | tr '_' '-') #kube does not accept underscore in names
328 #example gnb_1_2 -> gnb-1-2
329 set_name=$(echo $name | rev | cut -d- -f2- | rev) # Cut index part of ric name to get the name of statefulset
330 # example gnb-g1-2 -> gnb-g1 where gnb-g1-2 is the ric name and gnb-g1 is the set name
331 echo $name"."$set_name"."$KUBE_A1SIM_NAMESPACE
334 # Helper function to get a the port and host name of a specific ric simulator
336 # (Not for test scripts)
337 __find_ricmediatorsim_host() {
338 if [ $RUNMODE == "KUBE" ]; then
339 ricname=$(echo "$1" | tr '_' '-') # Kube does not accept underscore in names as docker do
340 if [ -z "$RICMEDIATOR_SIM_COMMON_SVC_NAME" ]; then
341 ric_setname="${ricname%-*}" #Extract the stateful set name
343 ric_setname=$RICMEDIATOR_SIM_COMMON_SVC_NAME # Use the common svc name in the host name of the sims
345 echo $RICMEDIATOR_SIM_HTTPX"://"$ricname.$ric_setname.$KUBE_A1SIM_NAMESPACE":"$RICMEDIATOR_SIM_PORT
347 ricname=$(echo "$1" | tr '_' '-')
348 echo $RICMEDIATOR_SIM_HTTPX"://"$ricname":"$RICMEDIATOR_SIM_PORT
352 # Generate a UUID to use as prefix for policy ids
353 nearsim_generate_policy_uuid() {
354 UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)')
355 #Reduce length to make space for serial id, uses 'a' as marker where the serial id is added
356 UUID=${UUID:0:${#UUID}-4}"a"
359 # Execute a curl cmd towards a ricsimulator and check the response code.
360 # args: <expected-response-code> <curl-cmd-string>
361 __execute_curl_to_ricmediatorsim() {
362 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
364 if [ ! -z "$KUBE_PROXY_PATH" ]; then
365 if [ $KUBE_PROXY_HTTPX == "http" ]; then
366 proxyflag=" --proxy $KUBE_PROXY_PATH"
368 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
371 if [ -z "$KUBE_PROXY_CURL_JWT" ]; then
372 echo " CMD: $2 $proxyflag" >> $HTTPLOG
373 res="$($2 $proxyflag)"
375 echo " CMD: $2 $proxyflag -H Authorization: Bearer $KUBE_PROXY_CURL_JWT" >> $HTTPLOG
376 res=$($2 $proxyflag -H 'Authorization: Bearer '$KUBE_PROXY_CURL_JWT)
379 echo " RESP: $res" >> $HTTPLOG
381 if [ $retcode -ne 0 ]; then
383 echo " RETCODE: "$retcode
384 echo -e $RED" FAIL - fatal error when executing curl."$ERED
387 status=${res:${#res}-3}
388 if [ $status -eq $1 ]; then
389 echo -e $GREEN" OK"$EGREEN
392 echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
397 # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout.
398 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is
399 # equal to the target or not.
400 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
401 # before setting pass or fail depending on if the variable value becomes equal to the target
403 # (Function for test scripts)
404 ricmediatorsim_equal() {
405 __log_test_fail_not_supported
408 # Print a variable value from the RICMEDIATOR sim.
409 # args: <ric-id> <variable-name>
410 # (Function for test scripts)
411 ricmediatorsim_print() {
412 __log_test_info_not_supported
415 # Tests if a variable value in the RICMEDIATOR simulator contains the target string and and optional timeout
416 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
418 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
419 # before setting pass or fail depending on if the variable value contains the target
421 # (Function for test scripts)
422 ricmediatorsim_contains_str() {
423 __log_test_fail_not_supported
426 # Simulator API: Put a policy type in a ric
427 # args: <response-code> <ric-id> <policy-type-id> <policy-type-file>
428 # (Function for test scripts)
429 ricmediatorsim_put_policy_type() {
431 if [ $# -ne 4 ]; then
432 __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@
435 host=$(__find_ricmediatorsim_host $2)
436 curlString="curl -X PUT -skw %{http_code} "$host"/a1-p/policytypes/"$3" -H Content-Type:application/json --data-binary @"$4
437 __execute_curl_to_ricmediatorsim $1 "$curlString"
441 # Simulator API: Delete a policy type in a ric
442 # <response-code> <ric-id> <policy-type-id>
443 # (Function for test scripts)
444 ricmediatorsim_delete_policy_type() {
446 if [ $# -ne 3 ]; then
447 __print_err "<response-code> <ric-id> <policy_type_id>" $@
450 host=$(__find_ricmediatorsim_host $2)
451 curlString="curl -X DELETE -skw %{http_code} "$host"/a1-p/policytypes/"$3
452 __execute_curl_to_ricmediatorsim $1 "$curlString"
456 # Simulator API: Delete instances (and status), for one ric
457 # <response-code> <ric-id>
458 # (Function for test scripts)
459 ricmediatorsim_post_delete_instances() {
460 __log_test_fail_not_supported
463 # Simulator API: Delete all (instances/types/statuses/settings), for one ric
464 # <response-code> <ric-id>
465 # (Function for test scripts)
466 ricmediatorsim_post_delete_all() {
467 __log_test_fail_not_supported
470 # Simulator API: Set (or reset) response code for next A1 message, for one ric
471 # <response-code> <ric-id> [<forced_response_code>]
472 # (Function for test scripts)
473 ricmediatorsim_post_forcedresponse() {
474 __log_test_fail_not_supported
477 # Simulator API: Set (or reset) A1 response delay, for one ric
478 # <response-code> <ric-id> [<delay-in-seconds>]
479 # (Function for test scripts)
480 ricmediatorsim_post_forcedelay() {
481 __log_test_fail_not_supported