Integrated gateway and updated kube support
[nonrtric.git] / test / common / gateway_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 # arg: <docker-images-format-string> <file-to-append>
50 __NGW_image_data() {
51         echo -e "$NRT_GATEWAY_DISPLAY_NAME\t$(docker images --format $1 $NRT_GATEWAY_IMAGE)" >>   $2
52 }
53
54 # Scale kubernetes resources to zero
55 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
56 # This function is called for apps fully managed by the test script
57 __NGW_kube_scale_zero() {
58         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest NGW
59 }
60
61 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
62 # This function is called for prestarted apps not managed by the test script.
63 __NGW_kube_scale_zero_and_wait() {
64         echo -e " NGW replicas kept as is"
65 }
66
67 # Delete all kube resouces for the app
68 # This function is called for apps managed by the test script.
69 __NGW_kube_delete_all() {
70         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest NGW
71 }
72
73 # Store docker logs
74 # This function is called for apps managed by the test script.
75 # args: <log-dir> <file-prexix>
76 __NGW_store_docker_logs() {
77         docker logs $NRT_GATEWAY_APP_NAME > $1$2_gateway.log 2>&1
78 }
79
80 #######################################################
81
82 ## Access to Gateway
83 # Host name may be changed if app started by kube
84 # Direct access from script
85 NGW_HTTPX="http"
86 NGW_HOST_NAME=$LOCALHOST_NAME
87 NGW_PATH=$NGW_HTTPX"://"$NGW_HOST_NAME":"$NRT_GATEWAY_EXTERNAL_PORT
88 # NGW_ADAPTER used for switch between REST and DMAAP (only REST supported currently)
89 NGW_ADAPTER_TYPE="REST"
90 NGW_ADAPTER=$NGW_PATH
91 ###########################
92 ### Gateway functions
93 ###########################
94
95 # Set http as the protocol to use for all communication to the Gateway
96 # args: -
97 # (Function for test scripts)
98 use_gateway_http() {
99         echo -e $BOLD"Gateway, NGW, protocol setting"$EBOLD
100         echo -e " Using $BOLD http $EBOLD towards NGW"
101         NGW_HTTPX="http"
102         NGW_PATH=$NGW_HTTPX"://"$NGW_HOST_NAME":"$NRT_GATEWAY_EXTERNAL_PORT
103         NGW_ADAPTER_TYPE="REST"
104         NGW_ADAPTER=$NGW_PATH
105         echo ""
106 }
107
108 # Set https as the protocol to use for all communication to the Gateway
109 # args: -
110 # (Function for test scripts)
111 use_gateway_https() {
112         echo -e $BOLD"Gateway, NGW, protocol setting"$EBOLD
113         echo -e " Using $BOLD https $EBOLD towards NGW"
114         NGW_HTTPX="https"
115         NGW_PATH=$NGW_HTTPX"://"$NGW_HOST_NAME":"$NRT_GATEWAY_EXTERNAL_SECURE_PORT
116         NGW_ADAPTER_TYPE="REST"
117         NGW_ADAPTER=$NGW_PATH
118         echo ""
119 }
120
121 # Turn on debug level tracing in the gateway
122 # args: -
123 # (Function for test scripts)
124 set_gateway_debug() {
125         echo -e $BOLD"Setting gateway debug logging"$EBOLD
126         curlString="$NGW_PATH$NRT_GATEWAY_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
127         result=$(__do_curl "$curlString")
128         if [ $? -ne 0 ]; then
129                 __print_err "could not set debug mode" $@
130                 ((RES_CONF_FAIL++))
131                 return 1
132         fi
133         echo ""
134         return 0
135 }
136
137 # Turn on trace level tracing in the gateway
138 # args: -
139 # (Function for test scripts)
140 set_gateway_trace() {
141         echo -e $BOLD"Setting gateway trace logging"$EBOLD
142         curlString="$NGW_PATH$NRT_GATEWAY_ACTUATOR -X POST  -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
143         result=$(__do_curl "$curlString")
144         if [ $? -ne 0 ]; then
145                 __print_err "could not set trace mode" $@
146                 ((RES_CONF_FAIL++))
147                 return 1
148         fi
149         echo ""
150         return 0
151 }
152
153 # Start the Gateway container
154 # args: -
155 # (Function for test scripts)
156 start_gateway() {
157
158         echo -e $BOLD"Starting $NRT_GATEWAY_DISPLAY_NAME"$EBOLD
159
160         if [ $RUNMODE == "KUBE" ]; then
161
162                 # Check if app shall be fully managed by the test script
163                 __check_included_image "NGW"
164                 retcode_i=$?
165
166                 # Check if app shall only be used by the testscipt
167                 __check_prestarted_image "NGW"
168                 retcode_p=$?
169
170                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
171                         echo -e $RED"The $NRT_GATEWAY_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
172                         echo -e $RED"The $NRT_GATEWAY_APP_NAME will not be started"$ERED
173                         exit
174                 fi
175                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
176                         echo -e $RED"The $NRT_GATEWAY_APP_NAME app is included both as managed and prestarted in this test script"$ERED
177                         echo -e $RED"The $NRT_GATEWAY_APP_NAME will not be started"$ERED
178                         exit
179                 fi
180
181                 # Check if app shall be used - not managed - by the test script
182                 __check_prestarted_image "NGW"
183                 if [ $? -eq 0 ]; then
184                         echo -e " Using existing $NRT_GATEWAY_APP_NAME deployment and service"
185                         echo " Setting NGW replicas=1"
186                         __kube_scale deployment $NRT_GATEWAY_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
187                 fi
188
189                 if [ $retcode_i -eq 0 ]; then
190
191                         echo -e " Creating $NGW_APP_NAME app and expose service"
192
193                         #Export all vars needed for service and deployment
194                         export NRT_GATEWAY_APP_NAME
195                         export KUBE_NONRTRIC_NAMESPACE
196                         export NRT_GATEWAY_IMAGE
197                         export NRT_GATEWAY_INTERNAL_PORT
198                         export NRT_GATEWAY_INTERNAL_SECURE_PORT
199                         export NRT_GATEWAY_EXTERNAL_PORT
200                         export NRT_GATEWAY_EXTERNAL_SECURE_PORT
201                         export NRT_GATEWAY_CONFIG_MOUNT_PATH
202                         export NRT_GATEWAY_CONFIG_FILE
203                         export NGW_CONFIG_CONFIGMAP_NAME=$NRT_GATEWAY_APP_NAME"-config"
204
205                         export POLICY_AGENT_EXTERNAL_SECURE_PORT
206                         export ECS_EXTERNAL_SECURE_PORT
207                         export POLICY_AGENT_DOMAIN_NAME=$POLICY_AGENT_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
208                         export ECS_DOMAIN_NAME=$ECS_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
209
210                         #Check if nonrtric namespace exists, if not create it
211                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
212
213                         # Create config map for config
214                         datafile=$PWD/tmp/$NRT_GATEWAY_CONFIG_FILE
215                         #Add config to properties file
216                         envsubst < $1 > $datafile
217                         output_yaml=$PWD/tmp/ngw_cfc.yaml
218                         __kube_create_configmap $NGW_CONFIG_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest NGW $datafile $output_yaml
219
220                         # Create service
221                         input_yaml=$SIM_GROUP"/"$NRT_GATEWAY_COMPOSE_DIR"/"svc.yaml
222                         output_yaml=$PWD/tmp/ngw_svc.yaml
223                         __kube_create_instance service $NRT_GATEWAY_APP_NAME $input_yaml $output_yaml
224
225                         # Create app
226                         input_yaml=$SIM_GROUP"/"$NRT_GATEWAY_COMPOSE_DIR"/"app.yaml
227                         output_yaml=$PWD/tmp/ngw_app.yaml
228                         __kube_create_instance app $NRT_GATEWAY_APP_NAME $input_yaml $output_yaml
229
230                 fi
231
232                 echo " Retrieving host and ports for service..."
233                 NGW_HOST_NAME=$(__kube_get_service_host $NRT_GATEWAY_APP_NAME $KUBE_NONRTRIC_NAMESPACE)
234
235                 NRT_GATEWAY_EXTERNAL_PORT=$(__kube_get_service_port $NRT_GATEWAY_APP_NAME $KUBE_NONRTRIC_NAMESPACE "http")
236                 NRT_GATEWAY_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $NRT_GATEWAY_APP_NAME $KUBE_NONRTRIC_NAMESPACE "https")
237
238                 echo " Host IP, http port, https port: $NGW_HOST_NAME $NRT_GATEWAY_EXTERNAL_PORT $NRT_GATEWAY_EXTERNAL_SECURE_PORT"
239                 if [ $NGW_HTTPX == "http" ]; then
240                         NGW_PATH=$NGW_HTTPX"://"$NGW_HOST_NAME":"$NRT_GATEWAY_EXTERNAL_PORT
241                 else
242                         NGW_PATH=$NGW_HTTPX"://"$NGW_HOST_NAME":"$NRT_GATEWAY_EXTERNAL_SECURE_PORT
243                 fi
244
245                 __check_service_start $NRT_GATEWAY_APP_NAME $NGW_PATH$NRT_GATEWAY_ALIVE_URL
246
247                 # Update the curl adapter if set to rest, no change if type dmaap
248                 if [ $NGW_ADAPTER_TYPE == "REST" ]; then
249                         NGW_ADAPTER=$NGW_PATH
250                 fi
251         else
252                 # Check if docker app shall be fully managed by the test script
253                 __check_included_image 'NGW'
254                 if [ $? -eq 1 ]; then
255                         echo -e $RED"The Gateway app is not included in this test script"$ERED
256                         echo -e $RED"The Gateway will not be started"$ERED
257                         exit
258                 fi
259
260                 # Export needed vars for docker compose
261         export NRT_GATEWAY_APP_NAME
262         export NRT_GATEWAY_INTERNAL_PORT
263         export NRT_GATEWAY_EXTERNAL_PORT
264         #export NRT_GATEWAY_INTERNAL_SECURE_PORT
265         #export NRT_GATEWAY_EXTERNAL_SECURE_PORT
266
267         export DOCKER_SIM_NWNAME
268                 export NRT_GATEWAY_HOST_MNT_DIR
269                 export NRT_GATEWAY_CONFIG_FILE
270                 export NRT_GATEWAY_CONFIG_MOUNT_PATH
271                 export NRT_GATEWAY_COMPOSE_DIR
272
273                 export POLICY_AGENT_DOMAIN_NAME=$POLICY_AGENT_APP_NAME
274                 export POLICY_AGENT_EXTERNAL_SECURE_PORT
275                 export ECS_DOMAIN_NAME=$ECS_APP_NAME
276                 export ECS_EXTERNAL_SECURE_PORT
277
278                 export NRT_GATEWAY_DISPLAY_NAME
279
280                 dest_file=$SIM_GROUP/$NRT_GATEWAY_COMPOSE_DIR/$NRT_GATEWAY_HOST_MNT_DIR/$NRT_GATEWAY_CONFIG_FILE
281
282                 envsubst < $1 > $dest_file
283
284                 __start_container $NRT_GATEWAY_COMPOSE_DIR "" NODOCKERARGS 1 $NRT_GATEWAY_APP_NAME
285
286                 __check_service_start $NRT_GATEWAY_APP_NAME $NGW_PATH$NRT_GATEWAY_ALIVE_URL
287         fi
288         echo ""
289 }
290
291
292 # API Test function: V2 GET /status towards PMS
293 # args: <response-code>
294 # (Function for test scripts)
295 gateway_pms_get_status() {
296         __log_test_start $@
297     if [ $# -ne 1 ]; then
298                 __print_err "<response-code>" $@
299                 return 1
300         fi
301         query=$PMS_API_PREFIX"/v2/status"
302     res="$(__do_curl_to_api NGW GET $query)"
303     status=${res:${#res}-3}
304
305         if [ $status -ne $1 ]; then
306                 __log_test_fail_status_code $1 $status
307                 return 1
308         fi
309
310         __log_test_pass
311         return 0
312 }
313
314 # API Test function: GET /ei-producer/v1/eitypes towards ECS
315 # Note: This is just to test service response
316 # args: <response-code>
317 # (Function for test scripts)
318 gateway_ecs_get_types() {
319         __log_test_start $@
320     if [ $# -ne 1 ]; then
321                 __print_err "<response-code>" $@
322                 return 1
323         fi
324         query="/ei-producer/v1/eitypes"
325     res="$(__do_curl_to_api NGW GET $query)"
326     status=${res:${#res}-3}
327
328         if [ $status -ne $1 ]; then
329                 __log_test_fail_status_code $1 $status
330                 return 1
331         fi
332
333         __log_test_pass
334         return 0
335 }