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