Upgraded test env with Kubernetes 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 RIC_SIM_HTTPX="http"
23 RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
24 RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
25
26
27 #Vars for A1 interface version and container count
28 G1_A1_VERSION=""
29 G2_A1_VERSION=""
30 G3_A1_VERSION=""
31 G4_A1_VERSION=""
32 G5_A1_VERSION=""
33 G1_COUNT=0
34 G2_COUNT=0
35 G3_COUNT=0
36 G4_COUNT=0
37 G5_COUNT=0
38
39
40 ###########################
41 ### RIC Simulator functions
42 ###########################
43
44 use_simulator_http() {
45         echo -e $BOLD"RICSIM protocol setting"$EBOLD
46         echo -e " Using $BOLD http $EBOLD towards the simulators"
47         RIC_SIM_HTTPX="http"
48         RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
49         RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
50         echo ""
51 }
52
53 use_simulator_https() {
54         echo -e $BOLD"RICSIM protocol setting"$EBOLD
55         echo -e " Using $BOLD https $EBOLD towards the simulators"
56         RIC_SIM_HTTPX="https"
57         RIC_SIM_HOST=$RIC_SIM_HTTPX"://"$LOCALHOST_NAME
58         RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
59         echo ""
60 }
61
62 # Start one group (ricsim_g1, ricsim_g2 .. ricsim_g5) with a number of RIC Simulators using a given A interface
63 # 'ricsim' may be set on command line to other prefix
64 # args:  ricsim_g1|ricsim_g2|ricsim_g3|ricsim_g4|ricsim_g5 <count> <interface-id>
65 # (Function for test scripts)
66 start_ric_simulators() {
67
68         echo -e $BOLD"Starting $RIC_SIM_DISPLAY_NAME"$EBOLD
69
70         if [ $RUNMODE == "KUBE" ]; then
71
72                 # Check if app shall be fully managed by the test script
73                 __check_included_image "RICSIM"
74                 retcode_i=$?
75
76                 # Check if app shall only be used by the testscipt
77                 __check_prestarted_image "RICSIM"
78                 retcode_p=$?
79
80                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
81                         echo -e $RED"The $1 app is not included as managed nor prestarted in this test script"$ERED
82                         echo -e $RED"The $1 will not be started"$ERED
83                         exit
84                 fi
85                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
86                         echo -e $RED"The $1 stub app is included both as managed and prestarted in this test script"$ERED
87                         echo -e $RED"The $1 will not be started"$ERED
88                         exit
89                 fi
90
91                 if [ $retcode_p -eq 0 ]; then
92                         echo -e " Using existing $1 statefulset and service"
93                         echo " Using existing simulator deployment and service for statefulset $1"
94                         echo " Setting $1 replicas=$2"
95                         __kube_scale statefulset $1 $KUBE_NONRTRIC_NAMESPACE $2
96                         echo ""
97                         return
98                 fi
99         fi
100
101         RIC1=$RIC_SIM_PREFIX"_g1"
102         RIC2=$RIC_SIM_PREFIX"_g2"
103         RIC3=$RIC_SIM_PREFIX"_g3"
104         RIC4=$RIC_SIM_PREFIX"_g4"
105         RIC5=$RIC_SIM_PREFIX"_g5"
106
107         if [ $# != 3 ]; then
108                 ((RES_CONF_FAIL++))
109                 __print_err "need three args,  $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@
110                 exit 1
111         fi
112
113         echo " $2 simulators using basename: $1 on interface: $3"
114         #Set env var for simulator count and A1 interface vesion for the given group
115         if [ $1 == "$RIC1" ]; then
116                 G1_COUNT=$2
117                 G1_A1_VERSION=$3
118         elif [ $1 == "$RIC2" ]; then
119                 G2_COUNT=$2
120                 G2_A1_VERSION=$3
121         elif [ $1 == "$RIC3" ]; then
122                 G3_COUNT=$2
123                 G3_A1_VERSION=$3
124         elif [ $1 == "$RIC4" ]; then
125                 G4_COUNT=$2
126                 G4_A1_VERSION=$3
127         elif [ $1 == "$RIC5" ]; then
128                 G5_COUNT=$2
129                 G5_A1_VERSION=$3
130         else
131                 ((RES_CONF_FAIL++))
132                 __print_err "need three args, $RIC1|$RIC2|$RIC3|$RIC4|$RIC5 <count> <interface-id>" $@
133                 exit 1
134         fi
135
136         if [ $RUNMODE == "KUBE" ]; then
137
138                 if [ $retcode_i -eq 0 ]; then
139
140                         #export needed env var for statefulset
141                         export RIC_SIM_SET_NAME=$(echo "$1" | tr '_' '-')  #kube does not accept underscore in names
142                         export KUBE_NONRTRIC_NAMESPACE
143                         export RIC_SIM_IMAGE
144                         #Adding 1 more instance, instance 0 is never used. This is done to keep test scripts compatible
145                         # with docker that starts instance index on 1.....
146                         export RIC_SIM_COUNT=$(($2+1))
147                         export A1_VERSION=$3
148                         export RIC_SIM_INTERNAL_PORT
149                         export RIC_SIM_INTERNAL_SECURE_PORT
150
151                         echo -e " Creating $POLICY_AGENT_APP_NAME app and expose service"
152
153                         #Check if nonrtric namespace exists, if not create it
154                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
155
156                         # Create service
157                         input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"svc.yaml
158                         output_yaml=$PWD/tmp/ric_${1}_svc.yaml
159                         __kube_create_instance service $RIC_SIM_SET_NAME $input_yaml $output_yaml
160
161                         # Create app
162                         input_yaml=$SIM_GROUP"/"$RIC_SIM_COMPOSE_DIR"/"app.yaml
163                         output_yaml=$PWD/tmp/pa_${1}_app.yaml
164                         __kube_create_instance app $RIC_SIM_SET_NAME $input_yaml $output_yaml
165
166                         #Using only instance from index 1 to keep compatability with docker
167                         for (( count=1; count<${RIC_SIM_COUNT}; count++ )); do
168                                 host=$(__find_sim_host $RIC_SIM_SET_NAME"-"$count)
169                                 __check_service_start $RIC_SIM_SET_NAME"-"$count $host"/"
170                         done
171                 fi
172         else
173
174                 __check_included_image 'RICSIM'
175                 if [ $? -eq 1 ]; then
176                         echo -e $RED"The Near-RT RIC Simulator app is not included as managed in this test script"$ERED
177                         echo -e $RED"Near-RT RIC Simulator will not be started"$ERED
178                         exit 1
179                 fi
180
181                 # Create .env file to compose project, all ric container will get this prefix
182                 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/$RIC_SIM_COMPOSE_DIR/.env
183
184                 export G1_A1_VERSION
185                 export G2_A1_VERSION
186                 export G3_A1_VERSION
187                 export G4_A1_VERSION
188                 export G5_A1_VERSION
189                 export RIC_SIM_INTERNAL_PORT
190                 export RIC_SIM_INTERNAL_SECURE_PORT
191                 export RIC_SIM_CERT_MOUNT_DIR
192                 export DOCKER_SIM_NWNAME
193
194                 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT --scale g4=$G4_COUNT --scale g5=$G5_COUNT"
195                 app_data=""
196                 cntr=1
197                 while [ $cntr -le $2 ]; do
198                         app=$1"_"$cntr
199                         app_data="$app_data $app"
200                         let cntr=cntr+1
201                 done
202
203                 __start_container $RIC_SIM_COMPOSE_DIR "$docker_args" $2 $app_data
204
205                 cntr=1
206                 while [ $cntr -le $2 ]; do
207                         app=$1"_"$cntr
208                         localport=0
209
210                         while [ $localport -eq 0 ]; do
211                                 echo -ne " Waiting for container ${app} to publish its ports...${SAMELINE}"
212                                 localport=$(__find_sim_port $app)
213                                 sleep 0.5
214                                 echo -ne " Waiting for container ${app} to publish its ports...retrying....${SAMELINE}"
215                         done
216                         echo -e " Waiting for container ${app} to publish its ports...retrying....$GREEN OK $EGREEN"
217                         __check_service_start $app $RIC_SIM_HOST":"$localport$RIC_SIM_ALIVE_URL
218                         let cntr=cntr+1
219                 done
220         fi
221         echo ""
222         return 0
223 }
224
225
226
227 # Excute a curl cmd towards a ricsimulator and check the response code.
228 # args: <expected-response-code> <curl-cmd-string>
229 __execute_curl_to_sim() {
230         echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
231         echo " CMD: $2" >> $HTTPLOG
232         res="$($2)"
233         echo " RESP: $res" >> $HTTPLOG
234         retcode=$?
235     if [ $retcode -ne 0 ]; then
236                 ((RES_CONF_FAIL++))
237                 echo " RETCODE: "$retcode
238         echo -e $RED" FAIL - fatal error when executing curl."$ERED
239         return 1
240     fi
241     status=${res:${#res}-3}
242     if [ $status -eq $1 ]; then
243         echo -e $GREEN" OK"$EGREEN
244         return 0
245     fi
246     echo -e $RED" FAIL - expected http response: "$1" but got http response: "$status $ERED
247         ((RES_CONF_FAIL++))
248     return 1
249 }
250
251 # Tests if a variable value in the ricsimulator is equal to a target value and and optional timeout.
252 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable is
253 # equal to the target or not.
254 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
255 # before setting pass or fail depending on if the variable value becomes equal to the target
256 # value or not.
257 # (Function for test scripts)
258 sim_equal() {
259
260         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
261                 host=$(__find_sim_host $1)
262                 __var_test $1 "$host/counter/" $2 "=" $3 $4
263                 return 0
264         else
265                 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
266                 return 1
267         fi
268 }
269
270 # Print a variable value from the RIC sim.
271 # args: <ric-id> <variable-name>
272 # (Function for test scripts)
273 sim_print() {
274
275         if [ $# != 2 ]; then
276         __print_err "need two args, <ric-id> <sim-param>" $@
277                 exit 1
278         fi
279         host=$(__find_sim_host $1)
280         echo -e $BOLD"INFO(${BASH_LINENO[0]}): $1, $2 = $(__do_curl $host/counter/$2)"$EBOLD
281 }
282
283 # Tests if a variable value in the RIC simulator contains the target string and and optional timeout
284 # Arg: <ric-id> <variable-name> <target-value> - This test set pass or fail depending on if the variable contains
285 # the target or not.
286 # Arg: <ric-id> <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
287 # before setting pass or fail depending on if the variable value contains the target
288 # value or not.
289 # (Function for test scripts)
290 sim_contains_str() {
291
292         if [ $# -eq 3 ] || [ $# -eq 4 ]; then
293                 host=$(__find_sim_host $1)
294                 __var_test $1 "$host/counter/" $2 "contain_str" $3 $4
295                 return 0
296         else
297                 __print_err "needs three or four args: <ric-id> <sim-param> <target-value> [ timeout ]"
298                 return 1
299         fi
300 }
301
302 # Simulator API: Put a policy type in a ric
303 # args: <response-code> <ric-id> <policy-type-id> <policy-type-file>
304 # (Function for test scripts)
305 sim_put_policy_type() {
306         __log_conf_start $@
307         if [ $# -ne 4 ]; then
308                 __print_err "<response-code> <ric-id> <policy-type-id> <policy-type-file>" $@
309                 return 1
310         fi
311         host=$(__find_sim_host $2)
312     curlString="curl -X PUT -skw %{http_code} "$host"/policytype?id="$3" -H Content-Type:application/json --data-binary @"$4
313         __execute_curl_to_sim $1 "$curlString"
314         return $?
315 }
316
317 # Simulator API: Delete a policy type in a ric
318 # <response-code> <ric-id> <policy-type-id>
319 # (Function for test scripts)
320 sim_delete_policy_type() {
321         __log_conf_start $@
322         if [ $# -ne 3 ]; then
323                 __print_err "<response-code> <ric-id> <policy_type_id>" $@
324                 return 1
325         fi
326         host=$(__find_sim_host $2)
327     curlString="curl -X DELETE -skw %{http_code} "$host"/policytype?id="$3
328     __execute_curl_to_sim $1 "$curlString"
329         return $?
330 }
331
332 # Simulator API: Delete instances (and status), for one ric
333 # <response-code> <ric-id>
334 # (Function for test scripts)
335 sim_post_delete_instances() {
336         __log_conf_start $@
337         if [ $# -ne 2 ]; then
338                 __print_err "<response-code> <ric-id>" $@
339                 return 1
340         fi
341         host=$(__find_sim_host $2)
342     curlString="curl -X POST -skw %{http_code} "$host"/deleteinstances"
343     __execute_curl_to_sim $1 "$curlString"
344         return $?
345 }
346
347 # Simulator API: Delete all (instances/types/statuses/settings), for one ric
348 # <response-code> <ric-id>
349 # (Function for test scripts)
350 sim_post_delete_all() {
351         __log_conf_start $@
352         if [ $# -ne 3 ]; then
353                 __print_err "<response-code> <numericic-id>" $@
354                 return 1
355         fi
356         host=$(__find_sim_host $2)
357     curlString="curl -X POST -skw %{http_code} "$host"/deleteall"
358     __execute_curl_to_sim $1 "$curlString"
359         return $?
360 }
361
362 # Simulator API: Set (or reset) response code for next A1 message, for one ric
363 # <response-code> <ric-id> [<forced_response_code>]
364 # (Function for test scripts)
365 sim_post_forcedresponse() {
366         __log_conf_start $@
367         if [ $# -ne 3 ]; then
368                 __print_err "<response-code> <ric-id> <forced_response_code>" $@
369                 return 1
370         fi
371         host=$(__find_sim_host $2)
372     curlString="curl -X POST -skw %{http_code} "$host"/forceresponse"
373         if [ $# -eq 3 ]; then
374                 curlString=$curlString"?code="$3
375         fi
376     __execute_curl_to_sim $1 "$curlString"
377         return $?
378 }
379
380 # Simulator API: Set (or reset) A1 response delay, for one ric
381 # <response-code> <ric-id> [<delay-in-seconds>]
382 # (Function for test scripts)
383 sim_post_forcedelay() {
384         __log_conf_start $@
385         if [ $# -ne 3 ]; then
386                 __print_err "<response-code> <ric-id> [<delay-in-seconds>]" $@
387                 return 1
388         fi
389         host=$(__find_sim_host $2)
390     curlString="curl -X POST -skw %{http_code} $host/forcedelay"
391         if [ $# -eq 3 ]; then
392                 curlString=$curlString"?delay="$3
393         fi
394     __execute_curl_to_sim $1 "$curlString"
395         return $?
396 }