a1e614533708c27ecccaefed3d5ebb2c4f638848
[nonrtric.git] / test / common / ngw_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
21 # for NonRTRIC Gateway
22
23 ################ Test engine functions ################
24
25 # Create the image var used during the test
26 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
27 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
28 __NGW_imagesetup() {
29         __check_and_create_image_var NGW "NRT_GATEWAY_IMAGE" "NRT_GATEWAY_IMAGE_BASE" "NRT_GATEWAY_IMAGE_TAG" $1 "$NRT_GATEWAY_DISPLAY_NAME"
30 }
31
32 # Pull image from remote repo or use locally built image
33 # arg: <pull-policy-override> <pull-policy-original>
34 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
35 # <pull-policy-original> Shall be used for images that does not allow overriding
36 # Both var may contain: 'remote', 'remote-remove' or 'local'
37 __NGW_imagepull() {
38         __check_and_pull_image $1 "$NRT_GATEWAY_DISPLAY_NAME" $NRT_GATEWAY_APP_NAME NRT_GATEWAY_IMAGE
39 }
40
41 # Build image (only for simulator or interfaces stubs owned by the test environment)
42 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
43 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
44 __NGW_imagebuild() {
45         echo -e $RED"Image for app NGW shall never be built"$ERED
46 }
47
48 # Generate a string for each included image using the app display name and a docker images format string
49 # If a custom image repo is used then also the source image from the local repo is listed
50 # arg: <docker-images-format-string> <file-to-append>
51 __NGW_image_data() {
52         echo -e "$NRT_GATEWAY_DISPLAY_NAME\t$(docker images --format $1 $NRT_GATEWAY_IMAGE)" >>   $2
53         if [ ! -z "$NRT_GATEWAY_IMAGE_SOURCE" ]; then
54                 echo -e "-- source image --\t$(docker images --format $1 $NRT_GATEWAY_IMAGE_SOURCE)" >>   $2
55         fi
56 }
57
58 # Scale kubernetes resources to zero
59 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
60 # This function is called for apps fully managed by the test script
61 __NGW_kube_scale_zero() {
62         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest NGW
63 }
64
65 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
66 # This function is called for prestarted apps not managed by the test script.
67 __NGW_kube_scale_zero_and_wait() {
68         echo -e " NGW replicas kept as is"
69 }
70
71 # Delete all kube resouces for the app
72 # This function is called for apps managed by the test script.
73 __NGW_kube_delete_all() {
74         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest NGW
75 }
76
77 # Store docker logs
78 # This function is called for apps managed by the test script.
79 # args: <log-dir> <file-prexix>
80 __NGW_store_docker_logs() {
81         if [ $RUNMODE == "KUBE" ]; then
82                 kubectl $KUBECONF  logs -l "autotest=NGW" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_gateway.log 2>&1
83         else
84                 docker logs $NRT_GATEWAY_APP_NAME > $1$2_gateway.log 2>&1
85         fi
86 }
87
88 # Initial setup of protocol, host and ports
89 # This function is called for apps managed by the test script.
90 # args: -
91 __NGW_initial_setup() {
92         use_gateway_http
93 }
94
95 # Set app short-name, app name and namespace for logging runtime statistics of kubernets pods or docker containers
96 # For docker, the namespace shall be excluded
97 # This function is called for apps managed by the test script as well as for prestarted apps.
98 # args: -
99 __NGW_statisics_setup() {
100         if [ $RUNMODE == "KUBE" ]; then
101                 echo "NGW $NRT_GATEWAY_APP_NAME $KUBE_NONRTRIC_NAMESPACE"
102         else
103                 echo "NGW $NRT_GATEWAY_APP_NAME"
104         fi
105 }
106
107 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
108 # args: -
109 __NGW_test_requirements() {
110         :
111 }
112
113 #######################################################
114
115
116
117
118
119 # Set http as the protocol to use for all communication to the nonrtric gateway
120 # args: -
121 # (Function for test scripts)
122 use_gateway_http() {
123         __gateway_set_protocoll "http" $NRT_GATEWAY_INTERNAL_PORT $NRT_GATEWAY_EXTERNAL_PORT
124 }
125
126 # Set https as the protocol to use for all communication to the nonrtric gateway
127 # args: -
128 # (Function for test scripts)
129 use_gateway_https() {
130         __gateway_set_protocoll "https" $NRT_GATEWAY_INTERNAL_SECURE_PORT $NRT_GATEWAY_EXTERNAL_SECURE_PORT
131 }
132
133 # Setup paths to svc/container for internal and external access
134 # args: <protocol> <internal-port> <external-port>
135 __gateway_set_protocoll() {
136         echo -e $BOLD"$NRT_GATEWAY_DISPLAY_NAME protocol setting"$EBOLD
137         echo -e " Using $BOLD $1 $EBOLD towards $NRT_GATEWAY_DISPLAY_NAME"
138
139         ## Access to nonrtric gateway
140
141         NRT_GATEWAY_SERVICE_PATH=$1"://"$NRT_GATEWAY_APP_NAME":"$2  # docker access, container->container and script->container via proxy
142         if [ $RUNMODE == "KUBE" ]; then
143                 NRT_GATEWAY_SERVICE_PATH=$1"://"$NRT_GATEWAY_APP_NAME.$KUBE_NONRTRIC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
144         fi
145
146         # NRT_GATEWAY_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
147         NRT_GATEWAY_ADAPTER_TYPE="REST"
148         NRT_GATEWAY_ADAPTER=$DMAAP_ADP_SERVICE_PATH
149
150         echo ""
151 }
152
153 # Turn on debug level tracing in the gateway
154 # args: -
155 # (Function for test scripts)
156 set_gateway_debug() {
157         echo -e $BOLD"Setting gateway debug logging"$EBOLD
158         curlString="$NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
159         result=$(__do_curl "$curlString")
160         if [ $? -ne 0 ]; then
161                 __print_err "could not set debug mode" $@
162                 ((RES_CONF_FAIL++))
163                 return 1
164         fi
165         echo ""
166         return 0
167 }
168
169 # Turn on trace level tracing in the gateway
170 # args: -
171 # (Function for test scripts)
172 set_gateway_trace() {
173         echo -e $BOLD"Setting gateway trace logging"$EBOLD
174         curlString="$NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
175         result=$(__do_curl "$curlString")
176         if [ $? -ne 0 ]; then
177                 __print_err "could not set trace mode" $@
178                 ((RES_CONF_FAIL++))
179                 return 1
180         fi
181         echo ""
182         return 0
183 }
184
185 # Export env vars for config files, docker compose and kube resources
186 # args: -
187 __gateway_export_vars() {
188
189         export NRT_GATEWAY_APP_NAME
190         export NRT_GATEWAY_DISPLAY_NAME
191
192         export KUBE_NONRTRIC_NAMESPACE
193         export DOCKER_SIM_NWNAME
194
195         export NRT_GATEWAY_IMAGE
196         export NRT_GATEWAY_INTERNAL_PORT
197         export NRT_GATEWAY_INTERNAL_SECURE_PORT
198         export NRT_GATEWAY_EXTERNAL_PORT
199         export NRT_GATEWAY_EXTERNAL_SECURE_PORT
200         export NRT_GATEWAY_CONFIG_MOUNT_PATH
201         export NRT_GATEWAY_CONFIG_FILE
202         export NGW_CONFIG_CONFIGMAP_NAME=$NRT_GATEWAY_APP_NAME"-config"
203         export NRT_GATEWAY_HOST_MNT_DIR
204         export NRT_GATEWAY_COMPOSE_DIR
205
206         if [ $RUNMODE == "KUBE" ]; then
207                 export A1PMS_EXTERNAL_SECURE_PORT
208                 export ICS_EXTERNAL_SECURE_PORT
209                 export A1PMS_DOMAIN_NAME=$A1PMS_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
210                 export ICS_DOMAIN_NAME=$ICS_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
211         else
212                 export A1PMS_DOMAIN_NAME=$A1PMS_APP_NAME
213                 export ICS_DOMAIN_NAME=$ICS_APP_NAME
214         fi
215 }
216
217 # Start the Gateway container
218 # args: -
219 # (Function for test scripts)
220 start_gateway() {
221
222         echo -e $BOLD"Starting $NRT_GATEWAY_DISPLAY_NAME"$EBOLD
223
224         if [ $RUNMODE == "KUBE" ]; then
225
226                 # Check if app shall be fully managed by the test script
227                 __check_included_image "NGW"
228                 retcode_i=$?
229
230                 # Check if app shall only be used by the testscipt
231                 __check_prestarted_image "NGW"
232                 retcode_p=$?
233
234                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
235                         echo -e $RED"The $NRT_GATEWAY_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
236                         echo -e $RED"The $NRT_GATEWAY_APP_NAME will not be started"$ERED
237                         exit
238                 fi
239                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
240                         echo -e $RED"The $NRT_GATEWAY_APP_NAME app is included both as managed and prestarted in this test script"$ERED
241                         echo -e $RED"The $NRT_GATEWAY_APP_NAME will not be started"$ERED
242                         exit
243                 fi
244
245                 # Check if app shall be used - not managed - by the test script
246                 __check_prestarted_image "NGW"
247                 if [ $? -eq 0 ]; then
248                         echo -e " Using existing $NRT_GATEWAY_APP_NAME deployment and service"
249                         echo " Setting NGW replicas=1"
250                         __kube_scale deployment $NRT_GATEWAY_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
251                 fi
252
253                 if [ $retcode_i -eq 0 ]; then
254
255                         echo -e " Creating $NRT_GATEWAY_APP_NAME app and expose service"
256
257                         __gateway_export_vars
258
259                         #Check if nonrtric namespace exists, if not create it
260                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
261
262                         # Create config map for config
263                         datafile=$PWD/tmp/$NRT_GATEWAY_CONFIG_FILE
264                         #Add config to properties file
265                         envsubst < $1 > $datafile
266                         output_yaml=$PWD/tmp/ngw_cfc.yaml
267                         __kube_create_configmap $NGW_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest NGW $datafile $output_yaml
268
269                         # Create service
270                         input_yaml=$SIM_GROUP"/"$NRT_GATEWAY_COMPOSE_DIR"/"svc.yaml
271                         output_yaml=$PWD/tmp/ngw_svc.yaml
272                         __kube_create_instance service $NRT_GATEWAY_APP_NAME $input_yaml $output_yaml
273
274                         # Create app
275                         input_yaml=$SIM_GROUP"/"$NRT_GATEWAY_COMPOSE_DIR"/"app.yaml
276                         output_yaml=$PWD/tmp/ngw_app.yaml
277                         __kube_create_instance app $NRT_GATEWAY_APP_NAME $input_yaml $output_yaml
278
279                 fi
280
281                 __check_service_start $NRT_GATEWAY_APP_NAME $NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ALIVE_URL
282
283         else
284                 # Check if docker app shall be fully managed by the test script
285                 __check_included_image 'NGW'
286                 if [ $? -eq 1 ]; then
287                         echo -e $RED"The Gateway app is not included in this test script"$ERED
288                         echo -e $RED"The Gateway will not be started"$ERED
289                         exit
290                 fi
291
292                 __gateway_export_vars
293
294                 dest_file=$SIM_GROUP/$NRT_GATEWAY_COMPOSE_DIR/$NRT_GATEWAY_HOST_MNT_DIR/$NRT_GATEWAY_CONFIG_FILE
295
296                 envsubst < $1 > $dest_file
297
298                 __start_container $NRT_GATEWAY_COMPOSE_DIR "" NODOCKERARGS 1 $NRT_GATEWAY_APP_NAME
299
300                 __check_service_start $NRT_GATEWAY_APP_NAME $NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ALIVE_URL
301         fi
302         echo ""
303 }
304
305
306 # API Test function: V2 GET /status towards A1PMS
307 # args: <response-code>
308 # (Function for test scripts)
309 gateway_a1pms_get_status() {
310         __log_test_start $@
311     if [ $# -ne 1 ]; then
312                 __print_err "<response-code>" $@
313                 return 1
314         fi
315         query=$A1PMS_API_PREFIX"/v2/status"
316     res="$(__do_curl_to_api NGW GET $query)"
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 /ei-producer/v1/eitypes towards ICS
329 # Note: This is just to test service response
330 # args: <response-code>
331 # (Function for test scripts)
332 gateway_ics_get_types() {
333         __log_test_start $@
334     if [ $# -ne 1 ]; then
335                 __print_err "<response-code>" $@
336                 return 1
337         fi
338         query="/ei-producer/v1/eitypes"
339     res="$(__do_curl_to_api NGW GET $query)"
340     status=${res:${#res}-3}
341
342         if [ $status -ne $1 ]; then
343                 __log_test_fail_status_code $1 $status
344                 return 1
345         fi
346
347         __log_test_pass
348         return 0
349 }