Merge "Update image tag"
[nonrtric.git] / test / common / cr_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 managemnt functions test functions for the Callback Reciver
21
22 ## Access to Callback Receiver
23 # Host name may be changed if app started by kube
24 # Direct access from script
25 CR_HTTPX="http"
26 CR_HOST_NAME=$LOCALHOST_NAME
27 CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
28 #Docker/Kube internal path
29 if [ $RUNMODE == "KUBE" ]; then
30         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
31 else
32         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK
33 fi
34 # CR_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
35 CR_ADAPTER_TYPE="REST"
36 CR_ADAPTER=$CR_PATH
37
38 ################
39 ### CR functions
40 ################
41
42 # Set http as the protocol to use for all communication to the Callback Receiver
43 # args: -
44 # (Function for test scripts)
45 use_cr_http() {
46         echo -e $BOLD"CR protocol setting"$EBOLD
47         echo -e " Using $BOLD http $EBOLD towards CR"
48
49         CR_HTTPX="http"
50         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
51
52         #Docker/Kube internal path
53         if [ $RUNMODE == "KUBE" ]; then
54                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
55         else
56                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_PORT$CR_APP_CALLBACK
57         fi
58         CR_ADAPTER_TYPE="REST"
59         CR_ADAPTER=$CR_PATH
60         echo ""
61 }
62
63 # Set https as the protocol to use for all communication to the Callback Receiver
64 # args: -
65 # (Function for test scripts)
66 use_cr_https() {
67         echo -e $BOLD"CR protocol setting"$EBOLD
68         echo -e " Using $BOLD https $EBOLD towards CR"
69
70         CR_HTTPX="https"
71         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT
72
73         if [ $RUNMODE == "KUBE" ]; then
74                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK
75         else
76                 CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME":"$CR_INTERNAL_SECURE_PORT$CR_APP_CALLBACK
77         fi
78
79         CR_ADAPTER_TYPE="REST"
80         CR_ADAPTER=$CR_PATH
81         echo ""
82 }
83
84 # Start the Callback reciver in the simulator group
85 # args: -
86 # (Function for test scripts)
87 start_cr() {
88
89         echo -e $BOLD"Starting $CR_DISPLAY_NAME"$EBOLD
90
91         if [ $RUNMODE == "KUBE" ]; then
92
93                 # Check if app shall be fully managed by the test script
94                 __check_included_image "CR"
95                 retcode_i=$?
96
97                 # Check if app shall only be used by the testscipt
98                 __check_prestarted_image "CR"
99                 retcode_p=$?
100
101                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
102                         echo -e $RED"The $CR_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
103                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
104                         exit
105                 fi
106                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
107                         echo -e $RED"The $CR_APP_NAME app is included both as managed and prestarted in this test script"$ERED
108                         echo -e $RED"The $CR_APP_NAME will not be started"$ERED
109                         exit
110                 fi
111
112                 # Check if app shall be used - not managed - by the test script
113                 if [ $retcode_p -eq 0 ]; then
114                         echo -e " Using existing $CR_APP_NAME deployment and service"
115                         echo " Setting CR replicas=1"
116                         __kube_scale deployment $CR_APP_NAME $KUBE_SIM_NAMESPACE 1
117                 fi
118
119                 if [ $retcode_i -eq 0 ]; then
120                         echo -e " Creating $CR_APP_NAME deployment and service"
121                         export CR_APP_NAME
122                         export KUBE_SIM_NAMESPACE
123                         export CR_IMAGE
124                         export CR_INTERNAL_PORT
125                         export CR_INTERNAL_SECURE_PORT
126                         export CR_EXTERNAL_PORT
127                         export CR_EXTERNAL_SECURE_PORT
128
129                         __kube_create_namespace $KUBE_SIM_NAMESPACE
130
131                         # Create service
132                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"svc.yaml
133                         output_yaml=$PWD/tmp/cr_svc.yaml
134                         __kube_create_instance service $CR_APP_NAME $input_yaml $output_yaml
135
136                         # Create app
137                         input_yaml=$SIM_GROUP"/"$CR_COMPOSE_DIR"/"app.yaml
138                         output_yaml=$PWD/tmp/cr_app.yaml
139                         __kube_create_instance app $CR_APP_NAME $input_yaml $output_yaml
140
141                 fi
142
143                 echo " Retrieving host and ports for service..."
144                 CR_HOST_NAME=$(__kube_get_service_host $CR_APP_NAME $KUBE_SIM_NAMESPACE)
145
146                 CR_EXTERNAL_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "http")
147                 CR_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $CR_APP_NAME $KUBE_SIM_NAMESPACE "https")
148
149                 echo " Host IP, http port, https port: $CR_HOST_NAME $CR_EXTERNAL_PORT $CR_EXTERNAL_SECURE_PORT"
150                 if [ $CR_HTTPX == "http" ]; then
151                         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_PORT
152                         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_PORT$CR_APP_CALLBACK
153                 else
154                         CR_PATH=$CR_HTTPX"://"$CR_HOST_NAME":"$CR_EXTERNAL_SECURE_PORT
155                         CR_SERVICE_PATH=$CR_HTTPX"://"$CR_APP_NAME"."$KUBE_SIM_NAMESPACE":"$CR_EXTERNAL_SECURE_PORT$CR_APP_CALLBACK
156                 fi
157                 if [ $CR_ADAPTER_TYPE == "REST" ]; then
158                         CR_ADAPTER=$CR_PATH
159                 fi
160
161                 __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL
162
163                 echo -ne " Service $CR_APP_NAME - reset  "$SAMELINE
164                 result=$(__do_curl $CR_APP_NAME $CR_PATH/reset)
165                 if [ $? -ne 0 ]; then
166                         echo -e " Service $CR_APP_NAME - reset  $RED Failed $ERED - will continue"
167                 else
168                         echo -e " Service $CR_APP_NAME - reset  $GREEN OK $EGREEN"
169                 fi
170         else
171                 # Check if docker app shall be fully managed by the test script
172                 __check_included_image 'CR'
173                 if [ $? -eq 1 ]; then
174                         echo -e $RED"The Callback Receiver app is not included in this test script"$ERED
175                         echo -e $RED"The Callback Receiver will not be started"$ERED
176                         exit
177                 fi
178
179                 export CR_APP_NAME
180                 export CR_INTERNAL_PORT
181                 export CR_EXTERNAL_PORT
182                 export CR_INTERNAL_SECURE_PORT
183                 export CR_EXTERNAL_SECURE_PORT
184                 export DOCKER_SIM_NWNAME
185
186                 __start_container $CR_COMPOSE_DIR NODOCKERARGS 1 $CR_APP_NAME
187
188         __check_service_start $CR_APP_NAME $CR_PATH$CR_ALIVE_URL
189         fi
190         echo ""
191 }
192
193
194 # Tests if a variable value in the CR is equal to a target value and and optional timeout.
195 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
196 # equal to the target or not.
197 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
198 # before setting pass or fail depending on if the variable value becomes equal to the target
199 # value or not.
200 # (Function for test scripts)
201 cr_equal() {
202         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
203                 __var_test "CR" "$CR_PATH/counter/" $1 "=" $2 $3
204         else
205                 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
206         fi
207 }
208
209 # CR API: Check the contents of all current ric sync events for one id from PMS
210 # <response-code> <id> [ EMPTY | ( <ric-id> )+ ]
211 # (Function for test scripts)
212 cr_api_check_all_sync_events() {
213         __log_test_start $@
214
215         if [ "$PMS_VERSION" != "V2" ]; then
216                 __log_test_fail_not_supported
217                 return 1
218         fi
219
220     if [ $# -lt 2 ]; then
221         __print_err "<response-code> <id> [ EMPTY | ( <ric-id> )+ ]" $@
222         return 1
223     fi
224
225         query="/get-all-events/"$2
226         res="$(__do_curl_to_api CR GET $query)"
227         status=${res:${#res}-3}
228
229         if [ $status -ne $1 ]; then
230                 __log_test_fail_status_code $1 $status
231                 return 1
232         fi
233
234         if [ $# -gt 2 ]; then
235                 body=${res:0:${#res}-3}
236                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
237                         targetJson="["
238                 else
239                         targetJson="["
240                         arr=(${@:3})
241
242                         for ((i=0; i<$(($#-2)); i=i+1)); do
243
244                                 if [ "$targetJson" != "[" ]; then
245                                         targetJson=$targetJson","
246                                 fi
247                                 targetJson=$targetJson"{\"ric_id\":\"${arr[$i]}\",\"event_type\":\"AVAILABLE\"}"
248                         done
249                 fi
250
251                 targetJson=$targetJson"]"
252                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
253                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
254
255                 if [ $res -ne 0 ]; then
256                         __log_test_fail_body
257                         return 1
258                 fi
259         fi
260         __log_test_pass
261         return 0
262 }
263
264 # CR API: Check the contents of all current status events for one id from ECS
265 # <response-code> <id> [ EMPTY | ( <status> )+ ]
266 # (Function for test scripts)
267 cr_api_check_all_ecs_events() {
268         __log_test_start $@
269
270     if [ $# -lt 2 ]; then
271         __print_err "<response-code> <id> [ EMPTY | ( <status> )+ ]" $@
272         return 1
273     fi
274
275         query="/get-all-events/"$2
276         res="$(__do_curl_to_api CR GET $query)"
277         status=${res:${#res}-3}
278
279         if [ $status -ne $1 ]; then
280                 __log_test_fail_status_code $1 $status
281                 return 1
282         fi
283
284         if [ $# -gt 2 ]; then
285                 body=${res:0:${#res}-3}
286                 if [ $# -eq 3 ] && [ $3 == "EMPTY" ]; then
287                         targetJson="["
288                 else
289                         targetJson="["
290                         arr=(${@:3})
291
292                         for ((i=0; i<$(($#-2)); i=i+1)); do
293
294                                 if [ "$targetJson" != "[" ]; then
295                                         targetJson=$targetJson","
296                                 fi
297                                 targetJson=$targetJson"{\"eiJobStatus\":\"${arr[$i]}\"}"
298                         done
299                 fi
300
301                 targetJson=$targetJson"]"
302                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
303                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
304
305                 if [ $res -ne 0 ]; then
306                         __log_test_fail_body
307                         return 1
308                 fi
309         fi
310         __log_test_pass
311         return 0
312 }