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