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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=================================================
20 # This is a script that contains container/service management functions
21 # for NonRTRIC Gateway
23 ################ Test engine functions ################
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
29 __check_and_create_image_var NGW "NRT_GATEWAY_IMAGE" "NRT_GATEWAY_IMAGE_BASE" "NRT_GATEWAY_IMAGE_TAG" $1 "$NRT_GATEWAY_DISPLAY_NAME" ""
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'
38 __check_and_pull_image $1 "$NRT_GATEWAY_DISPLAY_NAME" $NRT_GATEWAY_APP_NAME NRT_GATEWAY_IMAGE
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
45 echo -e $RED"Image for app NGW shall never be built"$ERED
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>
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
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
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 pre-started apps not managed by the test script.
67 __NGW_kube_scale_zero_and_wait() {
68 echo -e " NGW replicas kept as is"
71 # Delete all kube resources 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
78 # This function is called for apps managed by the test script.
79 # args: <log-dir> <file-prefix>
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
84 docker logs $NRT_GATEWAY_APP_NAME > $1$2_gateway.log 2>&1
88 # Initial setup of protocol, host and ports
89 # This function is called for apps managed by the test script.
91 __NGW_initial_setup() {
95 # Set app short-name, app name and namespace for logging runtime statistics of kubernetes 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 pre-started apps.
99 __NGW_statistics_setup() {
100 if [ $RUNMODE == "KUBE" ]; then
101 echo "NGW $NRT_GATEWAY_APP_NAME $KUBE_NONRTRIC_NAMESPACE"
103 echo "NGW $NRT_GATEWAY_APP_NAME"
107 # Check application requirements, e.g. helm, the the test needs. Exit 1 if req not satisfied
109 __NGW_test_requirements() {
113 #######################################################
119 # Set http as the protocol to use for all communication to the nonrtric gateway
121 # (Function for test scripts)
123 __gateway_set_protocoll "http" $NRT_GATEWAY_INTERNAL_PORT $NRT_GATEWAY_EXTERNAL_PORT
126 # Set https as the protocol to use for all communication to the nonrtric gateway
128 # (Function for test scripts)
129 use_gateway_https() {
130 __gateway_set_protocoll "https" $NRT_GATEWAY_INTERNAL_SECURE_PORT $NRT_GATEWAY_EXTERNAL_SECURE_PORT
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"
139 ## Access to nonrtric gateway
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
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
153 # Turn on debug level tracing in the gateway
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" $@
169 # Turn on trace level tracing in the gateway
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" $@
185 # Export env vars for config files, docker compose and kube resources
187 __gateway_export_vars() {
189 export NRT_GATEWAY_APP_NAME
190 export NRT_GATEWAY_DISPLAY_NAME
192 export KUBE_NONRTRIC_NAMESPACE
193 export DOCKER_SIM_NWNAME
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
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
212 export A1PMS_DOMAIN_NAME=$A1PMS_APP_NAME
213 export ICS_DOMAIN_NAME=$ICS_APP_NAME
217 # Start the Gateway container
219 # (Function for test scripts)
222 echo -e $BOLD"Starting $NRT_GATEWAY_DISPLAY_NAME"$EBOLD
224 if [ $RUNMODE == "KUBE" ]; then
226 # Check if app shall be fully managed by the test script
227 __check_included_image "NGW"
230 # Check if app shall only be used by the test script
231 __check_prestarted_image "NGW"
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
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
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
253 if [ $retcode_i -eq 0 ]; then
255 echo -e " Creating $NRT_GATEWAY_APP_NAME app and expose service"
257 __gateway_export_vars
259 #Check if nonrtric namespace exists, if not create it
260 __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
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
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
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
281 __check_service_start $NRT_GATEWAY_APP_NAME $NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ALIVE_URL
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
292 __gateway_export_vars
294 dest_file=$SIM_GROUP/$NRT_GATEWAY_COMPOSE_DIR/$NRT_GATEWAY_HOST_MNT_DIR/$NRT_GATEWAY_CONFIG_FILE
296 envsubst < $1 > $dest_file
298 __start_container $NRT_GATEWAY_COMPOSE_DIR "" NODOCKERARGS 1 $NRT_GATEWAY_APP_NAME
300 __check_service_start $NRT_GATEWAY_APP_NAME $NRT_GATEWAY_SERVICE_PATH$NRT_GATEWAY_ALIVE_URL
306 # API Test function: V2 GET /status towards A1PMS
307 # args: <response-code>
308 # (Function for test scripts)
309 gateway_a1pms_get_status() {
311 if [ $# -ne 1 ]; then
312 __print_err "<response-code>" $@
315 query=$A1PMS_API_PREFIX"/v2/status"
316 res="$(__do_curl_to_api NGW GET $query)"
317 status=${res:${#res}-3}
319 if [ $status -ne $1 ]; then
320 __log_test_fail_status_code $1 $status
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() {
334 if [ $# -ne 1 ]; then
335 __print_err "<response-code>" $@
338 query="/ei-producer/v1/eitypes"
339 res="$(__do_curl_to_api NGW GET $query)"
340 status=${res:${#res}-3}
342 if [ $status -ne $1 ]; then
343 __log_test_fail_status_code $1 $status