Integrated gateway and updated kube support
[nonrtric.git] / test / common / ricsimulator_api_functions.sh
1 #!/bin/bash
2
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
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
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=================================================
18 #
19
20 # This is a script that contains container/service management functions and test functions for RICSIM A1 simulators
21
22 ################ Test engine functions ################
23
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 __RICSIM_imagesetup() {
28         __check_and_create_image_var RICSIM "RIC_SIM_IMAGE" "RIC_SIM_IMAGE_BASE" "RIC_SIM_IMAGE_TAG" $1 "$RIC_SIM_DISPLAY_NAME"
29 }
30
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 __RICSIM_imagepull() {
37         __check_and_pull_image $1 "$RIC_SIM_DISPLAY_NAME" $RIC_SIM_PREFIX"_"$RIC_SIM_BASE $RIC_SIM_IMAGE
38 }
39
40 # Generate a string for each included image using the app display name and a docker images format string
41 # arg: <docker-images-format-string> <file-to-append>
42 __RICSIM_image_data() {
43         echo -e "$RIC_SIM_DISPLAY_NAME\t$(docker images --format $1 $RIC_SIM_IMAGE)" >>   $2
44 }
45
46 # Scale kubernetes resources to zero
47 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
48 # This function is called for apps fully managed by the test script
49 __RICSIM_kube_scale_zero() {
50         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RICSIM
51 }
52
53 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
54 # This function is called for prestarted apps not managed by the test script.
55 __RICSIM_kube_scale_zero_and_wait() {
56         __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app nonrtric-a1simulator
57 }
58
59 # Delete all kube resouces for the app
60 # This function is called for apps managed by the test script.
61 __RICSIM_kube_delete_all() {
62         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RICSIM
63 }
64
65 # Store docker logs
66 # This function is called for apps managed by the test script.
67 # args: <log-dir> <file-prexix>
68 __RICSIM_store_docker_logs() {
69         rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
70         for ric in $rics; do
71                 docker logs $ric > $1$2_$ric.log 2>&1
72         done
73 }
74
75 #######################################################
76
77
78 RIC_SIM_HTTPX="http"
79 RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
80 RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
81
82
83 #Vars for A1 interface version and container count
84 G1_A1_VERSION=""
85 G2_A1_VERSION=""
86 G3_A1_VERSION=""
87 G4_A1_VERSION=""
88 G5_A1_VERSION=""
89 G1_COUNT=0
90 G2_COUNT=0
91 G3_COUNT=0
92 G4_COUNT=0
93 G5_COUNT=0
94
95
96 ###########################
97 ### RIC Simulator functions
98 ###########################
99
100 use_simulator_http() {
101         echo -e $BOLD"RICSIM protocol setting"$EBOLD
102         echo -e " Using $BOLD http $EBOLD towards the simulators"
103         RIC_SIM_HTTPX="http"
104         RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
105         RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
106         echo ""
107 }
108
109 use_simulator_https() {
110         echo -e $BOLD"RICSIM protocol setting"$EBOLD
111         echo -e " Using $BOLD https $EBOLD towards the simulators"
112         RIC_SIM_HTTPX="https"
113         RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
114         RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
115         echo ""
116 }
117
118 # Start one group (ricsim_g1, ricsim_g2 .. ricsim_g5) with a number of RIC Simulators using a given A interface
119 # 'ricsim' may be set on command line to other prefix
120 # args:  ricsim_g1|ricsim_g2|ricsim_g3|ricsim_g4|ricsim_g5 <count> <interface-id>
121 # (Function for test scripts)
122 start_ric_simulators() {
123
124         echo -e $BOLD"Starting $RIC_SIM_DISPLAY_NAME"$EBOLD
125
126         if [ $RUNMODE == "KUBE" ]; then
127
128                 # Check if app shall be fully managed by the test script
129                 __check_included_image "RICSIM"
130                 retcode_i=$?
131
132                 # Check if app shall only be used by the testscipt
133                 __check_prestarted_image "RICSIM"
134                 retcode_p=$?
135
136                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
137                         echo -e $RED"The $1 app is not included as managed nor prestarted in this test script"$ERED
138                         echo -e $RED"The $1 will not be started"$ERED
139                         exit
140                 fi
141                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
142                         echo -e $RED"The $1 stub app is included both as managed and prestarted in this test script"$ERED
143                         echo -e $RED"The $1 will not be started"$ERED
144                         exit
145                 fi
146
147                 if [ $retcode_p -eq 0 ]; then
148                         echo -e " Using existing $1 statefulset and service"
149                         echo " Using existing simulator deployment and service for statefulset $1"
150                         echo " Setting $1 replicas=$2"
151                         __kube_scale statefulset $1 $KUBE_NONRTRIC_NAMESPACE $2
152                         echo ""
153                         return
154                 fi
155         fi
156
157         RIC1=$RIC_SIM_PREFIX"_g1"
158         RIC2=$RIC_SIM_PREFIX"_g2"
159         RIC3=$RIC_SIM_PREFIX"_g3"
160         RIC4=$RIC_SIM_PREFIX"_g4"
161         RIC5=$RIC_SIM_PREFIX"_g5"
162
163         if [ $# != 3 ]; then
164                 ((RES_CONF_FAIL++))
165                 __print_err "need three args,  $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@
166                 exit 1
167         fi
168
169         echo " $2 simulators using basename: $1 on interface: $3"
170         #Set env var for simulator count and A1 interface vesion for the given group
171         if [ $1 == "$RIC1" ]; then
172                 G1_COUNT=$2
173                 G1_A1_VERSION=$3
174         elif [ $1 == "$RIC2" ]; then
175                 G2_COUNT=$2
176                 G2_A1_VERSION=$3
177         elif [ $1 == "$RIC3" ]; then
178                 G3_COUNT=$2
179                 G3_A1_VERSION=$3
180         elif [ $1 == "$RIC4" ]; then
181                 G4_COUNT=$2
182                 G4_A1_VERSION=$3
183         elif [ $1 == "$RIC5" ]; then
184                 G5_COUNT=$2
185                 G5_A1_VERSION=$3
186         else
187                 ((RES_CONF_FAIL++))
188                 __print_err "need three args, $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@
189                 exit 1
190         fi
191
192         if [ $RUNMODE == "KUBE" ]; then
193
194                 if [ $retcode_i -eq 0 ]; then
195
196                         #export needed env var for statefulset
197                         export RIC_SIM_SET_NAME=$(echo "$1" | tr '_' '-')  #kube does not accept underscore in names
198                         export KUBE_NONRTRIC_NAMESPACE
199                         export RIC_SIM_IMAGE
200                         #Adding 1 more instance, instance 0 is never used. This is done to keep test scripts compatible
201                         # with docker that starts instance index on 1.....
202                         export RIC_SIM_COUNT=$(($2+1))
203                         export A1_VERSION=$3
204                         export RIC_SIM_INTERNAL_PORT
205                         export RIC_SIM_INTERNAL_SECURE_PORT
206
207                         echo -e " Creating $POLICY_AGENT_APP_NAME app and expose service"
208
209                         #Check if nonrtric namespace exists, if not create it
210                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
211
212                         # Create service
213                         input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"svc.yaml
214                         output_yaml=$PWD/tmp/ric_${1}_svc.yaml
215                         __kube_create_instance service $RIC_SIM_SET_NAME $input_yaml $output_yaml
216
217                         # Create app
218                         input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"app.yaml
219                         output_yaml=$PWD/tmp/pa_${1}_app.yaml
220                         __kube_create_instance app $RIC_SIM_SET_NAME $input_yaml $output_yaml
221
222                         #Using only instance from index 1 to keep compatability with docker
223                         for (( count=1; count<${RIC_SIM_COUNT}; count++ )); do
224                                 host=$(__find_sim_host $RIC_SIM_SET_NAME"-"$count)
225                                 __check_service_start $RIC_SIM_SET_NAME"-"$count $host"/"
226                         done
227                 fi
228         else
229
230                 __check_included_image 'RICSIM'
231                 if [ $? -eq 1 ]; then
232                         echo -e $RED"The Near-RT RIC Simulator app is not included as managed in this test script"$ERED
233                         echo -e $RED"Near-RT RIC Simulator will not be started"$ERED
234                         exit 1
235                 fi
236
237                 # Create .env file to compose project, all ric container will get this prefix
238                 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/$RIC_SIM_COMPOSE_DIR/.env
239
240                 export G1_A1_VERSION
241                 export G2_A1_VERSION
242                 export G3_A1_VERSION
243                 export G4_A1_VERSION
244                 export G5_A1_VERSION
245                 export RIC_SIM_INTERNAL_PORT
246                 export RIC_SIM_INTERNAL_SECURE_PORT
247                 export RIC_SIM_CERT_MOUNT_DIR
248                 export DOCKER_SIM_NWNAME
249                 export RIC_SIM_DISPLAY_NAME
250
251                 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT --scale g4=$G4_COUNT --scale g5=$G5_COUNT"
252                 app_data=""
253                 cntr=1
254                 while [ $cntr -le $2 ]; do
255                         app=$1"_"$cntr
256                         app_data="$app_data $app"
257                         let cntr=cntr+1
258                 done
259
260                 __start_container $RIC_SIM_COMPOSE_DIR "" "$docker_args" $2 $app_data
261
262                 cntr=1
263                 while [ $cntr -le $2 ]; do
264                         app=$1"_"$cntr
265                         localport=0
266
267                         while [ $localport -eq 0 ]; do
268                                 echo -ne " Waiting for container ${app} to publish its ports...${SAMELINE}"
269                                 localport=$(__find_sim_port $app)
270                                 sleep 0.5
271                                 echo -ne " Waiting for container ${app} to publish its ports...retrying....${SAMELINE}"
272                         done
273                         echo -e " Waiting for container ${app} to publish its ports...retrying....$GREEN OK $EGREEN"
274                         __check_service_start $app $RIC_SIM_HOST":"$localport$RIC_SIM_ALIVE_URL
275                         let cntr=cntr+1
276                 done
277         fi
278         echo ""
279         return 0
280 }
281
282 # Translate ric name to kube host name
283 # args: <ric-name>
284 # For test scripts
285 get_kube_sim_host() {
286         name=$(echo "$1" | tr '_' '-')  #kube does not accept underscore in names
287         #example gnb_1_2 -> gnb-1-2
288         set_name=$(echo $name | rev | cut -d- -f2- | rev) # Cut index part of ric name to get the name of statefulset
289         # example gnb-g1-2 -> gnb-g1 where gnb-g1-2 is the ric name and gnb-g1 is the set name
290         echo $name"."$set_name"."$KUBE_NONRTRIC_NAMESPACE
291 }
292
293 # Helper function to get a the port of a specific ric simulator
294 # args: <ric-id>
295 # (Not for test scripts)
296 __find_sim_port() {
297     name=$1" " #Space appended to prevent matching 10 if 1 is desired....
298     cmdstr="docker inspect --format='{{(index (index .NetworkSettings.Ports \"$RIC_SIM_PORT/tcp\") 0).HostPort}}' ${name}"
299     res=$(eval $cmdstr)
300         if [[ "$res" =~ ^[0-9]+$ ]]; then
301                 echo $res
302         else
303                 echo "0"
304     fi
305 }
306
307 # Helper function to get a the port and host name of a specific ric simulator
308 # args: <ric-id>
309 # (Not for test scripts)
310 __find_sim_host() {
311         if [ $RUNMODE == "KUBE" ]; then
312                 ricname=$(echo "$1" | tr '_' '-')
313                 for timeout in {1..60}; do
314                         host=$(kubectl get pod $ricname  -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.status.podIP}' 2> /dev/null)
315                         if [ ! -z "$host" ]; then
316                                 echo $RIC_SIM_HTTPX"://"$host":"$RIC_SIM_PORT
317                                 return 0
318                         fi
319                         sleep 0.5
320                 done
321                 echo "host-not-found-fatal-error"
322         else
323                 name=$1" " #Space appended to prevent matching 10 if 1 is desired....
324                 cmdstr="docker inspect --format='{{(index (index .NetworkSettings.Ports \"$RIC_SIM_PORT/tcp\") 0).HostPort}}' ${name}"
325                 res=$(eval $cmdstr)
326                 if [[ "$res" =~ ^[0-9]+$ ]]; then
327                         echo $RIC_SIM_HOST:$res
328                         return 0
329                 else
330                         echo "0"
331                 fi
332         fi
333         return 1
334 }
335
336 # Generate a UUID to use as prefix for policy ids
337 generate_policy_uuid() {
338         UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)')
339         #Reduce length to make space for serial id, uses 'a' as marker where the serial id is added
340         UUID=${UUID:0:${#UUID}-4}"a"
341 }
342
343 # Excute a curl cmd towards a ricsimulator and check the response code.
344 # args: <expected-response-code> <curl-cmd-string>
345 __execute_curl_to_sim() {
346         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
347         proxyflag=""
348         if [ $RUNMODE == "KUBE" ]; then
349                 if [ ! -z "$CLUSTER_KUBE_PROXY_NODEPORT" ]; then
350                         proxyflag=" --proxy http://localhost:$CLUSTER_KUBE_PROXY_NODEPORT"
351                 fi
352         fi
353         echo " CMD: $2 $proxyflag" >> $HTTPLOG
354         res="$($2 $proxyflag)"
355         echo " RESP: $res" >> $HTTPLOG
356         retcode=$?
357     if [ $retcode -ne 0 ]; then
358                 ((RES_CONF_FAIL++))
359                 echo " RETCODE: "$retcode
360         echo -e $RED" FAIL - fatal error when executing curl."$ERED
361         return 1
362     fi
363     status=${res:${#res}-3}
364     if [ $status -eq $1 ]; then
365         echo -e $GREEN" OK"$EGREEN
366         return 0
367     fi
368     echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
369         ((RES_CONF_FAIL++))
370     return 1
371 }
372
373 # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout.
374 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is
375 # equal to the target or not.
376 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
377 # before setting pass or fail depending on if the variable value becomes equal to the target
378 # value or not.
379 # (Function for test scripts)
380 sim_equal() {
381
382         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
383                 host=$(__find_sim_host $1)
384                 __var_test $1 "$host/counter/" $2 "=" $3 $4
385                 return 0
386         else
387                 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
388                 return 1
389         fi
390 }
391
392 # Print a variable value from the RIC sim.
393 # args: <ric-id> <variable-name>
394 # (Function for test scripts)
395 sim_print() {
396
397         if [ $# != 2 ]; then
398         __print_err "need two args, <ric-id> <sim-param>" $@
399                 exit 1
400         fi
401         host=$(__find_sim_host $1)
402         echo -e $BOLD"INFO(${BASH_LINENO[0]}): $1, $2 = $(__do_curl $host/counter/$2)"$EBOLD
403 }
404
405 # Tests if a variable value in the RIC simulator contains the target string and and optional timeout
406 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
407 # the target or not.
408 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
409 # before setting pass or fail depending on if the variable value contains the target
410 # value or not.
411 # (Function for test scripts)
412 sim_contains_str() {
413
414         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
415                 host=$(__find_sim_host $1)
416                 __var_test $1 "$host/counter/" $2 "contain_str" $3 $4
417                 return 0
418         else
419                 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
420                 return 1
421         fi
422 }
423
424 # Simulator API: Put a policy type in a ric
425 # args: <response-code> <ric-id> <policy-type-id> <policy-type-file>
426 # (Function for test scripts)
427 sim_put_policy_type() {
428         __log_conf_start $@
429         if [ $# -ne 4 ]; then
430                 __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@
431                 return 1
432         fi
433         host=$(__find_sim_host $2)
434     curlString="curl -X PUT -skw %{http_code} "$host"/policytype?id="$3" -H Content-Type:application/json --data-binary @"$4
435         __execute_curl_to_sim $1 "$curlString"
436         return $?
437 }
438
439 # Simulator API: Delete a policy type in a ric
440 # <response-code> <ric-id> <policy-type-id>
441 # (Function for test scripts)
442 sim_delete_policy_type() {
443         __log_conf_start $@
444         if [ $# -ne 3 ]; then
445                 __print_err "<response-code> <ric-id> <policy_type_id>" $@
446                 return 1
447         fi
448         host=$(__find_sim_host $2)
449     curlString="curl -X DELETE -skw %{http_code} "$host"/policytype?id="$3
450     __execute_curl_to_sim $1 "$curlString"
451         return $?
452 }
453
454 # Simulator API: Delete instances (and status), for one ric
455 # <response-code> <ric-id>
456 # (Function for test scripts)
457 sim_post_delete_instances() {
458         __log_conf_start $@
459         if [ $# -ne 2 ]; then
460                 __print_err "<response-code> <ric-id>" $@
461                 return 1
462         fi
463         host=$(__find_sim_host $2)
464     curlString="curl -X POST -skw %{http_code} "$host"/deleteinstances"
465     __execute_curl_to_sim $1 "$curlString"
466         return $?
467 }
468
469 # Simulator API: Delete all (instances/types/statuses/settings), for one ric
470 # <response-code> <ric-id>
471 # (Function for test scripts)
472 sim_post_delete_all() {
473         __log_conf_start $@
474         if [ $# -ne 3 ]; then
475                 __print_err "<response-code> <numericic-id>" $@
476                 return 1
477         fi
478         host=$(__find_sim_host $2)
479     curlString="curl -X POST -skw %{http_code} "$host"/deleteall"
480     __execute_curl_to_sim $1 "$curlString"
481         return $?
482 }
483
484 # Simulator API: Set (or reset) response code for next A1 message, for one ric
485 # <response-code> <ric-id> [<forced_response_code>]
486 # (Function for test scripts)
487 sim_post_forcedresponse() {
488         __log_conf_start $@
489         if [ $# -ne 3 ]; then
490                 __print_err "<response-code> <ric-id> <forced_response_code>" $@
491                 return 1
492         fi
493         host=$(__find_sim_host $2)
494     curlString="curl -X POST -skw %{http_code} "$host"/forceresponse"
495         if [ $# -eq 3 ]; then
496                 curlString=$curlString"?code="$3
497         fi
498     __execute_curl_to_sim $1 "$curlString"
499         return $?
500 }
501
502 # Simulator API: Set (or reset) A1 response delay, for one ric
503 # <response-code> <ric-id> [<delay-in-seconds>]
504 # (Function for test scripts)
505 sim_post_forcedelay() {
506         __log_conf_start $@
507         if [ $# -ne 3 ]; then
508                 __print_err "<response-code> <ric-id> [<delay-in-seconds>]" $@
509                 return 1
510         fi
511         host=$(__find_sim_host $2)
512     curlString="curl -X POST -skw %{http_code} $host/forcedelay"
513         if [ $# -eq 3 ]; then
514                 curlString=$curlString"?delay="$3
515         fi
516     __execute_curl_to_sim $1 "$curlString"
517         return $?
518 }