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