blob: a1e614533708c27ecccaefed3d5ebb2c4f638848 [file] [log] [blame]
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001#!/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() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +020038 __check_and_pull_image $1 "$NRT_GATEWAY_DISPLAY_NAME" $NRT_GATEWAY_APP_NAME NRT_GATEWAY_IMAGE
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010039}
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
BjornMagnussonXA483ee332021-04-08 01:35:24 +020049# If a custom image repo is used then also the source image from the local repo is listed
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010050# 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
BjornMagnussonXA483ee332021-04-08 01:35:24 +020053 if [ ! -z "$NRT_GATEWAY_IMAGE_SOURCE" ]; then
54 echo -e "-- source image --\t$(docker images --format $1 $NRT_GATEWAY_IMAGE_SOURCE)" >> $2
55 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010056}
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() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +010081 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +010082 kubectl $KUBECONF logs -l "autotest=NGW" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_gateway.log 2>&1
BjornMagnussonXA663566c2021-11-08 10:25:07 +010083 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
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010093}
94
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010095# 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
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100107# 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
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100113#######################################################
114
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100115
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100116
117
118
119# Set http as the protocol to use for all communication to the nonrtric gateway
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100120# args: -
121# (Function for test scripts)
122use_gateway_http() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100123 __gateway_set_protocoll "http" $NRT_GATEWAY_INTERNAL_PORT $NRT_GATEWAY_EXTERNAL_PORT
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100124}
125
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100126# Set https as the protocol to use for all communication to the nonrtric gateway
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100127# args: -
128# (Function for test scripts)
129use_gateway_https() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100130 __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
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100137 echo -e " Using $BOLD $1 $EBOLD towards $NRT_GATEWAY_DISPLAY_NAME"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100138
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
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100150 echo ""
151}
152
153# Turn on debug level tracing in the gateway
154# args: -
155# (Function for test scripts)
156set_gateway_debug() {
157 echo -e $BOLD"Setting gateway debug logging"$EBOLD
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100158 curlString="$NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ACTUATOR -X POST -H Content-Type:application/json -d {\"configuredLevel\":\"debug\"}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100159 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)
172set_gateway_trace() {
173 echo -e $BOLD"Setting gateway trace logging"$EBOLD
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100174 curlString="$NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ACTUATOR -X POST -H Content-Type:application/json -d {\"configuredLevel\":\"trace\"}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100175 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
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100185# 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
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100207 export A1PMS_EXTERNAL_SECURE_PORT
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100208 export ICS_EXTERNAL_SECURE_PORT
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100209 export A1PMS_DOMAIN_NAME=$A1PMS_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100210 export ICS_DOMAIN_NAME=$ICS_APP_NAME.$KUBE_NONRTRIC_NAMESPACE
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100211 else
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100212 export A1PMS_DOMAIN_NAME=$A1PMS_APP_NAME
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100213 export ICS_DOMAIN_NAME=$ICS_APP_NAME
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100214 fi
215}
216
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100217# Start the Gateway container
218# args: -
219# (Function for test scripts)
220start_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
BjornMagnussonXAe25e05d2021-05-19 12:10:12 +0200255 echo -e " Creating $NRT_GATEWAY_APP_NAME app and expose service"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100256
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100257 __gateway_export_vars
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100258
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
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100281 __check_service_start $NRT_GATEWAY_APP_NAME $NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ALIVE_URL
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100282
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100283 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
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100292 __gateway_export_vars
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100293
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
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100300 __check_service_start $NRT_GATEWAY_APP_NAME $NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ALIVE_URL
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100301 fi
302 echo ""
303}
304
305
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100306# API Test function: V2 GET /status towards A1PMS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100307# args: <response-code>
308# (Function for test scripts)
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100309gateway_a1pms_get_status() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100310 __log_test_start $@
311 if [ $# -ne 1 ]; then
312 __print_err "<response-code>" $@
313 return 1
314 fi
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100315 query=$A1PMS_API_PREFIX"/v2/status"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100316 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
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100328# API Test function: GET /ei-producer/v1/eitypes towards ICS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100329# Note: This is just to test service response
330# args: <response-code>
331# (Function for test scripts)
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100332gateway_ics_get_types() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100333 __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}