Upgraded test env with Kubernetes support
[nonrtric.git] / test / common / rapp_catalogue_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 RAPP Catalogue API
21
22 ## Access to RAPP Catalogue
23 # Host name may be changed if app started by kube
24 # Direct access from script
25 RC_HTTPX="http"
26 RC_HOST_NAME=$LOCALHOST_NAME
27 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
28 # RC_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
29 RC_ADAPTER_TYPE="REST"
30 RC_ADAPTER=$RC_PATH
31
32
33 ###########################
34 ### RAPP Catalogue
35 ###########################
36
37 # Set http as the protocol to use for all communication to the RAPP Catalogue
38 # args: -
39 # (Function for test scripts)
40 use_rapp_catalogue_http() {
41         echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
42         echo -e " Using $BOLD http $EBOLD towards the RAPP Catalogue"
43         RC_HTTPX="http"
44         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
45         RC_ADAPTER_TYPE="REST"
46         RC_ADAPTER=$RC_PATH
47         echo ""
48 }
49
50 # Set https as the protocol to use for all communication to the RAPP Catalogue
51 # args: -
52 # (Function for test scripts)
53 use_rapp_catalogue_https() {
54         echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
55         echo -e " Using $BOLD https $EBOLD towards the RAPP Catalogue"
56         RC_HTTPX="https"
57         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
58         RC_ADAPTER_TYPE="REST"
59         RC_ADAPTER=$RC_PATH
60         echo ""
61 }
62
63 # Start the RAPP Catalogue container
64 # args: -
65 # (Function for test scripts)
66 start_rapp_catalogue() {
67
68         echo -e $BOLD"Starting $RAPP_CAT_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 "RC"
74                 retcode_i=$?
75
76                 # Check if app shall only be used by the testscipt
77                 __check_prestarted_image "RC"
78                 retcode_p=$?
79
80                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
81                         echo -e $RED"The $RAPP_CAT_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
82                         echo -e $RED"The $RAPP_CAT_APP_NAME 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 $RAPP_CAT_APP_NAME app is included both as managed and prestarted in this test script"$ERED
87                         echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED
88                         exit
89                 fi
90
91                 if [ $retcode_p -eq 0 ]; then
92                         echo -e " Using existing $RAPP_CAT_APP_NAME deployment and service"
93                         echo " Setting $RAPP_CAT_APP_NAME replicas=1"
94                         __kube_scale deployment $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
95                 fi
96
97                 if [ $retcode_i -eq 0 ]; then
98
99                         echo -e " Creating $RAPP_CAT_APP_NAME app and expose service"
100
101                         #Check if nonrtric namespace exists, if not create it
102                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
103
104                         export RAPP_CAT_APP_NAME
105                         export KUBE_NONRTRIC_NAMESPACE
106                         export RAPP_CAT_IMAGE
107                         export RAPP_CAT_INTERNAL_PORT
108                         export RAPP_CAT_INTERNAL_SECURE_PORT
109                         export RAPP_CAT_EXTERNAL_PORT
110                         export RAPP_CAT_EXTERNAL_SECURE_PORT
111
112                         #Create service
113                         input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"svc.yaml
114                         output_yaml=$PWD/tmp/rac_svc.yaml
115                         __kube_create_instance service $RAPP_CAT_APP_NAME $input_yaml $output_yaml
116
117                         #Create app
118                         input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"app.yaml
119                         output_yaml=$PWD/tmp/rac_app.yaml
120                         __kube_create_instance app $RAPP_CAT_APP_NAME $input_yaml $output_yaml
121                 fi
122
123                 echo " Retrieving host and ports for service..."
124                 RC_HOST_NAME=$(__kube_get_service_host $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
125
126                 RAPP_CAT_EXTERNAL_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
127                 RAPP_CAT_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
128
129                 echo " Host IP, http port, https port: $RC_HOST_NAME $RAPP_CAT_EXTERNAL_PORT $RAPP_CAT_EXTERNAL_SECURE_PORT"
130                 if [ $RC_HTTPX == "http" ]; then
131                         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
132                 else
133                         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
134                 fi
135
136                 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
137
138                 # Update the curl adapter if set to rest, no change if type dmaap
139                 if [ $RC_ADAPTER_TYPE == "REST" ]; then
140                         RC_ADAPTER=$RC_PATH
141                 fi
142         else
143                 __check_included_image 'RC'
144                 if [ $? -eq 1 ]; then
145                         echo -e $RED"The RAPP Catalogue app is not included as managed in this test script"$ERED
146                         echo -e $RED"The RAPP Catalogue will not be started"$ERED
147                         exit
148                 fi
149
150                 export RAPP_CAT_APP_NAME
151         export RAPP_CAT_INTERNAL_PORT
152         export RAPP_CAT_EXTERNAL_PORT
153         export RAPP_CAT_INTERNAL_SECURE_PORT
154         export RAPP_CAT_EXTERNAL_SECURE_PORT
155         export DOCKER_SIM_NWNAME
156
157                 __start_container $RAPP_CAT_COMPOSE_DIR NODOCKERARGS 1 $RAPP_CAT_APP_NAME
158
159                 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
160         fi
161         echo ""
162 }
163
164 # Tests if a variable value in the RAPP Catalogue is equal to a target value and and optional timeout.
165 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
166 # equal to the target or not.
167 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
168 # before setting pass or fail depending on if the variable value becomes equal to the target
169 # value or not.
170 # (Function for test scripts)
171 rc_equal() {
172         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
173                 #__var_test RC "$LOCALHOST_HTTP:$RC_EXTERNAL_PORT/" $1 "=" $2 $3
174                 __var_test RC "$RC_PATH/" $1 "=" $2 $3
175         else
176                 __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
177         fi
178 }
179
180
181 ##########################################
182 #########  RAPP Catalogue API   ##########
183 ##########################################
184 #Function prefix: rapp_cat_api
185
186 # API Test function: GET /services
187 # args: <response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]
188 # (Function for test scripts)
189 rapp_cat_api_get_services() {
190         __log_test_start $@
191
192         if [ $# -lt 1 ]; then
193                 __print_err "<response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]" $@
194                 return 1
195         fi
196         query="/services"
197     res="$(__do_curl_to_api RC GET $query)"
198     status=${res:${#res}-3}
199
200         if [ $status -ne $1 ]; then
201                 __log_test_fail_status_code $1 $status
202                 return 1
203         fi
204
205         if [ $# -gt 1 ]; then
206                 body=${res:0:${#res}-3}
207                 targetJson="["
208                 arr=(${@:2})
209
210                 if [ $# -eq 2 ]; then
211                         targetJson="[]"
212                 else
213                         for ((i=0; i<$(($#-1)); i=i+4)); do
214                                 if [ "$targetJson" != "[" ]; then
215                                         targetJson=$targetJson","
216                                 fi
217                                 targetJson=$targetJson"{\"name\": \"${arr[$i]}\",\"version\": \"${arr[$i+1]}\",\"display_name\": \"${arr[$i+2]}\",\"description\": \"${arr[$i+3]}\",\"registrationDate\": \"????\"}"
218                         done
219                         targetJson=$targetJson"]"
220                 fi
221                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
222                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
223
224                 if [ $res -ne 0 ]; then
225                         __log_test_fail_body
226                         return 1
227                 fi
228         fi
229
230         __log_test_pass
231         return 0
232 }
233
234 # API Test function: PUT ​/services/{service-id}
235 # args: <response-code> <service-id> <version> <display-name> <description>
236 # (Function for test scripts)
237 rapp_cat_api_put_service() {
238         __log_test_start $@
239
240         if [ $# -ne 5 ]; then
241                 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
242                 return 1
243         fi
244
245         inputJson="{\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\"}"
246         file="./tmp/.p.json"
247         echo "$inputJson" > $file
248         query="/services/$2"
249         res="$(__do_curl_to_api RC PUT $query $file)"
250     status=${res:${#res}-3}
251
252         if [ $status -ne $1 ]; then
253                 __log_test_fail_status_code $1 $status
254                 return 1
255         fi
256
257         __log_test_pass
258         return 0
259 }
260
261 # API Test function: GET ​/services/{service-id}
262 # args: <response-code> <service-id>
263 # (Function for test scripts)
264 rapp_cat_api_get_service() {
265         __log_test_start $@
266
267         if [ $# -lt 2 ] || [ $# -gt 5 ]; then
268                 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
269                 return 1
270         fi
271
272         query="/services/$2"
273     res="$(__do_curl_to_api RC GET $query)"
274     status=${res:${#res}-3}
275
276         if [ $status -ne $1 ]; then
277                 __log_test_fail_status_code $1 $status
278                 return 1
279         fi
280
281         if [ $# -gt 2 ]; then
282                 body=${res:0:${#res}-3}
283                 targetJson="{\"name\": \"$2\",\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\",\"registrationDate\": \"????\"}"
284                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
285                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
286
287                 if [ $res -ne 0 ]; then
288                         __log_test_fail_body
289                         return 1
290                 fi
291         fi
292
293         __log_test_pass
294         return 0
295 }
296
297 # API Test function: DELETE ​/services/{service-id}
298 # args: <response-code> <service-id>
299 # (Function for test scripts)
300 rapp_cat_api_delete_service() {
301         __log_test_start $@
302
303         if [ $# -ne 2 ]; then
304                 __print_err "<response-code> <service-id>" $@
305                 return 1
306         fi
307
308         query="/services/$2"
309         res="$(__do_curl_to_api RC DELETE $query)"
310     status=${res:${#res}-3}
311
312         if [ $status -ne $1 ]; then
313                 __log_test_fail_status_code $1 $status
314                 return 1
315         fi
316
317         __log_test_pass
318         return 0
319 }