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