Integrated gateway and updated kube 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 ################ 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 __RC_imagesetup() {
28         __check_and_create_image_var RC "RAPP_CAT_IMAGE" "RAPP_CAT_IMAGE_BASE" "RAPP_CAT_IMAGE_TAG" $1 "$RAPP_CAT_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 arg var may contain: 'remote', 'remote-remove' or 'local'
36 __RC_imagepull() {
37         __check_and_pull_image $1 "$c" $RAPP_CAT_APP_NAME $RAPP_CAT_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 __RC_image_data() {
43         echo -e "$RAPP_CAT_DISPLAY_NAME\t$(docker images --format $1 $RAPP_CAT_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 __RC_kube_scale_zero() {
50         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
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 __RC_kube_scale_zero_and_wait() {
56         __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app nonrtric-rappcatalogueservice
57         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
58 }
59
60 # Delete all kube resouces for the app
61 # This function is called for apps managed by the test script.
62 __RC_kube_delete_all() {
63         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest RC
64 }
65
66 # Store docker logs
67 # This function is called for apps managed by the test script.
68 # args: <log-dir> <file-prexix>
69 __RC_store_docker_logs() {
70         docker logs $RAPP_CAT_APP_NAME > $1$2_rc.log 2>&1
71 }
72
73 #######################################################
74
75 ## Access to RAPP Catalogue
76 # Host name may be changed if app started by kube
77 # Direct access from script
78 RC_HTTPX="http"
79 RC_HOST_NAME=$LOCALHOST_NAME
80 RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
81 # RC_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
82 RC_ADAPTER_TYPE="REST"
83 RC_ADAPTER=$RC_PATH
84
85
86 ###########################
87 ### RAPP Catalogue
88 ###########################
89
90 # Set http as the protocol to use for all communication to the RAPP Catalogue
91 # args: -
92 # (Function for test scripts)
93 use_rapp_catalogue_http() {
94         echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
95         echo -e " Using $BOLD http $EBOLD towards the RAPP Catalogue"
96         RC_HTTPX="http"
97         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
98         RC_ADAPTER_TYPE="REST"
99         RC_ADAPTER=$RC_PATH
100         echo ""
101 }
102
103 # Set https as the protocol to use for all communication to the RAPP Catalogue
104 # args: -
105 # (Function for test scripts)
106 use_rapp_catalogue_https() {
107         echo -e $BOLD"RAPP Catalogue protocol setting"$EBOLD
108         echo -e " Using $BOLD https $EBOLD towards the RAPP Catalogue"
109         RC_HTTPX="https"
110         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
111         RC_ADAPTER_TYPE="REST"
112         RC_ADAPTER=$RC_PATH
113         echo ""
114 }
115
116 # Start the RAPP Catalogue container
117 # args: -
118 # (Function for test scripts)
119 start_rapp_catalogue() {
120
121         echo -e $BOLD"Starting $RAPP_CAT_DISPLAY_NAME"$EBOLD
122
123         if [ $RUNMODE == "KUBE" ]; then
124
125                 # Check if app shall be fully managed by the test script
126                 __check_included_image "RC"
127                 retcode_i=$?
128
129                 # Check if app shall only be used by the testscipt
130                 __check_prestarted_image "RC"
131                 retcode_p=$?
132
133                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
134                         echo -e $RED"The $RAPP_CAT_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
135                         echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED
136                         exit
137                 fi
138                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
139                         echo -e $RED"The $RAPP_CAT_APP_NAME app is included both as managed and prestarted in this test script"$ERED
140                         echo -e $RED"The $RAPP_CAT_APP_NAME will not be started"$ERED
141                         exit
142                 fi
143
144                 if [ $retcode_p -eq 0 ]; then
145                         echo -e " Using existing $RAPP_CAT_APP_NAME deployment and service"
146                         echo " Setting $RAPP_CAT_APP_NAME replicas=1"
147                         __kube_scale deployment $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
148                 fi
149
150                 if [ $retcode_i -eq 0 ]; then
151
152                         echo -e " Creating $RAPP_CAT_APP_NAME app and expose service"
153
154                         #Check if nonrtric namespace exists, if not create it
155                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
156
157                         export RAPP_CAT_APP_NAME
158                         export KUBE_NONRTRIC_NAMESPACE
159                         export RAPP_CAT_IMAGE
160                         export RAPP_CAT_INTERNAL_PORT
161                         export RAPP_CAT_INTERNAL_SECURE_PORT
162                         export RAPP_CAT_EXTERNAL_PORT
163                         export RAPP_CAT_EXTERNAL_SECURE_PORT
164
165                         #Create service
166                         input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"svc.yaml
167                         output_yaml=$PWD/tmp/rac_svc.yaml
168                         __kube_create_instance service $RAPP_CAT_APP_NAME $input_yaml $output_yaml
169
170                         #Create app
171                         input_yaml=$SIM_GROUP"/"$RAPP_CAT_COMPOSE_DIR"/"app.yaml
172                         output_yaml=$PWD/tmp/rac_app.yaml
173                         __kube_create_instance app $RAPP_CAT_APP_NAME $input_yaml $output_yaml
174                 fi
175
176                 echo " Retrieving host and ports for service..."
177                 RC_HOST_NAME=$(__kube_get_service_host $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
178
179                 RAPP_CAT_EXTERNAL_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
180                 RAPP_CAT_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $RAPP_CAT_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
181
182                 echo " Host IP, http port, https port: $RC_HOST_NAME $RAPP_CAT_EXTERNAL_PORT $RAPP_CAT_EXTERNAL_SECURE_PORT"
183                 if [ $RC_HTTPX == "http" ]; then
184                         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_PORT
185                 else
186                         RC_PATH=$RC_HTTPX"://"$RC_HOST_NAME":"$RAPP_CAT_EXTERNAL_SECURE_PORT
187                 fi
188
189                 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
190
191                 # Update the curl adapter if set to rest, no change if type dmaap
192                 if [ $RC_ADAPTER_TYPE == "REST" ]; then
193                         RC_ADAPTER=$RC_PATH
194                 fi
195         else
196                 __check_included_image 'RC'
197                 if [ $? -eq 1 ]; then
198                         echo -e $RED"The RAPP Catalogue app is not included as managed in this test script"$ERED
199                         echo -e $RED"The RAPP Catalogue will not be started"$ERED
200                         exit
201                 fi
202
203                 export RAPP_CAT_APP_NAME
204         export RAPP_CAT_INTERNAL_PORT
205         export RAPP_CAT_EXTERNAL_PORT
206         export RAPP_CAT_INTERNAL_SECURE_PORT
207         export RAPP_CAT_EXTERNAL_SECURE_PORT
208         export DOCKER_SIM_NWNAME
209
210                 export RAPP_CAT_DISPLAY_NAME
211
212                 __start_container $RAPP_CAT_COMPOSE_DIR "" NODOCKERARGS 1 $RAPP_CAT_APP_NAME
213
214                 __check_service_start $RAPP_CAT_APP_NAME $RC_PATH$RAPP_CAT_ALIVE_URL
215         fi
216         echo ""
217 }
218
219 # Tests if a variable value in the RAPP Catalogue is equal to a target value and and optional timeout.
220 # Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
221 # equal to the target or not.
222 # Arg: <variable-name> <target-value> <timeout-in-sec>  - This test waits up to the timeout seconds
223 # before setting pass or fail depending on if the variable value becomes equal to the target
224 # value or not.
225 # (Function for test scripts)
226 rc_equal() {
227         if [ $# -eq 2 ] || [ $# -eq 3 ]; then
228                 #__var_test RC "$LOCALHOST_HTTP:$RC_EXTERNAL_PORT/" $1 "=" $2 $3
229                 __var_test RC "$RC_PATH/" $1 "=" $2 $3
230         else
231                 __print_err "Wrong args to ecs_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
232         fi
233 }
234
235
236 ##########################################
237 #########  RAPP Catalogue API   ##########
238 ##########################################
239 #Function prefix: rapp_cat_api
240
241 # API Test function: GET /services
242 # args: <response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]
243 # (Function for test scripts)
244 rapp_cat_api_get_services() {
245         __log_test_start $@
246
247         if [ $# -lt 1 ]; then
248                 __print_err "<response-code> [(<service-id> <version> <display-name> <description>)+ | EMPTY ]" $@
249                 return 1
250         fi
251         query="/services"
252     res="$(__do_curl_to_api RC GET $query)"
253     status=${res:${#res}-3}
254
255         if [ $status -ne $1 ]; then
256                 __log_test_fail_status_code $1 $status
257                 return 1
258         fi
259
260         if [ $# -gt 1 ]; then
261                 body=${res:0:${#res}-3}
262                 targetJson="["
263                 arr=(${@:2})
264
265                 if [ $# -eq 2 ]; then
266                         targetJson="[]"
267                 else
268                         for ((i=0; i<$(($#-1)); i=i+4)); do
269                                 if [ "$targetJson" != "[" ]; then
270                                         targetJson=$targetJson","
271                                 fi
272                                 targetJson=$targetJson"{\"name\": \"${arr[$i]}\",\"version\": \"${arr[$i+1]}\",\"display_name\": \"${arr[$i+2]}\",\"description\": \"${arr[$i+3]}\",\"registrationDate\": \"????\"}"
273                         done
274                         targetJson=$targetJson"]"
275                 fi
276                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
277                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
278
279                 if [ $res -ne 0 ]; then
280                         __log_test_fail_body
281                         return 1
282                 fi
283         fi
284
285         __log_test_pass
286         return 0
287 }
288
289 # API Test function: PUT ​/services/{service-id}
290 # args: <response-code> <service-id> <version> <display-name> <description>
291 # (Function for test scripts)
292 rapp_cat_api_put_service() {
293         __log_test_start $@
294
295         if [ $# -ne 5 ]; then
296                 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
297                 return 1
298         fi
299
300         inputJson="{\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\"}"
301         file="./tmp/.p.json"
302         echo "$inputJson" > $file
303         query="/services/$2"
304         res="$(__do_curl_to_api RC PUT $query $file)"
305     status=${res:${#res}-3}
306
307         if [ $status -ne $1 ]; then
308                 __log_test_fail_status_code $1 $status
309                 return 1
310         fi
311
312         __log_test_pass
313         return 0
314 }
315
316 # API Test function: GET ​/services/{service-id}
317 # args: <response-code> <service-id>
318 # (Function for test scripts)
319 rapp_cat_api_get_service() {
320         __log_test_start $@
321
322         if [ $# -lt 2 ] || [ $# -gt 5 ]; then
323                 __print_err "<response-code> <service-id> <version> <display-name> <description>" $@
324                 return 1
325         fi
326
327         query="/services/$2"
328     res="$(__do_curl_to_api RC GET $query)"
329     status=${res:${#res}-3}
330
331         if [ $status -ne $1 ]; then
332                 __log_test_fail_status_code $1 $status
333                 return 1
334         fi
335
336         if [ $# -gt 2 ]; then
337                 body=${res:0:${#res}-3}
338                 targetJson="{\"name\": \"$2\",\"version\": \"$3\",\"display_name\": \"$4\",\"description\": \"$5\",\"registrationDate\": \"????\"}"
339                 echo " TARGET JSON: $targetJson" >> $HTTPLOG
340                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
341
342                 if [ $res -ne 0 ]; then
343                         __log_test_fail_body
344                         return 1
345                 fi
346         fi
347
348         __log_test_pass
349         return 0
350 }
351
352 # API Test function: DELETE ​/services/{service-id}
353 # args: <response-code> <service-id>
354 # (Function for test scripts)
355 rapp_cat_api_delete_service() {
356         __log_test_start $@
357
358         if [ $# -ne 2 ]; then
359                 __print_err "<response-code> <service-id>" $@
360                 return 1
361         fi
362
363         query="/services/$2"
364         res="$(__do_curl_to_api RC DELETE $query)"
365     status=${res:${#res}-3}
366
367         if [ $status -ne $1 ]; then
368                 __log_test_fail_status_code $1 $status
369                 return 1
370         fi
371
372         __log_test_pass
373         return 0
374 }