35280a4ca1585677deee764cfb5a183e9fe50357
[nonrtric.git] / test / common / dmaapmed_api_functions.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2021 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 managemnt functions test functions for the Dmaap Adatper
21
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 __DMAAPMED_imagesetup() {
29         __check_and_create_image_var DMAAPMED "DMAAP_MED_IMAGE" "DMAAP_MED_IMAGE_BASE" "DMAAP_MED_IMAGE_TAG" $1 "$DMAAP_MED_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 __DMAAPMED_imagepull() {
38         __check_and_pull_image $1 "$DMAAP_MED_DISPLAY_NAME" $DMAAP_MED_APP_NAME DMAAP_MED_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 __DMAAPMED_imagebuild() {
45         echo -e $RED" Image for app DMAAPMED 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 __DMAAPMED_image_data() {
52         echo -e "$DMAAP_MED_DISPLAY_NAME\t$(docker images --format $1 $DMAAP_MED_IMAGE)" >>   $2
53         if [ ! -z "$DMAAP_MED_IMAGE_SOURCE" ]; then
54                 echo -e "-- source image --\t$(docker images --format $1 $DMAAP_MED_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 __DMAAPMED_kube_scale_zero() {
62         __kube_scale_all_resources $KUBE_NONRTRIC_NAMESPACE autotest DMAAPMED
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 __DMAAPMED_kube_scale_zero_and_wait() {
68         __kube_scale_and_wait_all_resources $KUBE_NONRTRIC_NAMESPACE app "$KUBE_NONRTRIC_NAMESPACE"-dmaapmediatorservice
69 }
70
71 # Delete all kube resouces for the app
72 # This function is called for apps managed by the test script.
73 __DMAAPMED_kube_delete_all() {
74         __kube_delete_all_resources $KUBE_NONRTRIC_NAMESPACE autotest DMAAPMED
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 __DMAAPMED_store_docker_logs() {
81         if [ $RUNMODE == "KUBE" ]; then
82                 kubectl  logs -l "autotest=DMAAPMED" -n $KUBE_NONRTRIC_NAMESPACE --tail=-1 > $1$2_dmaapmediator.log 2>&1
83         else
84                 docker logs $DMAAP_MED_APP_NAME > $1$2_dmaapmediator.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 __DMAAPMED_initial_setup() {
92         use_dmaapmed_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 __DMAAPMED_statisics_setup() {
100         if [ $RUNMODE == "KUBE" ]; then
101                 echo "DMAAPMED $DMAAP_MED_APP_NAME $KUBE_NONRTRIC_NAMESPACE"
102         else
103                 echo "DMAAPMED $DMAAP_MED_APP_NAME"
104         fi
105 }
106
107 #######################################################
108
109 # Set http as the protocol to use for all communication to the Dmaap mediator
110 # args: -
111 # (Function for test scripts)
112 use_dmaapmed_http() {
113         __dmaapmed_set_protocoll "http" $DMAAP_MED_INTERNAL_PORT $DMAAP_MED_EXTERNAL_PORT
114 }
115
116 # Set https as the protocol to use for all communication to the Dmaap mediator
117 # args: -
118 # (Function for test scripts)
119 use_dmaapmed_https() {
120         __dmaapmed_set_protocoll "https" $DMAAP_MED_INTERNAL_SECURE_PORT $DMAAP_MED_EXTERNAL_SECURE_PORT
121 }
122
123 # Setup paths to svc/container for internal and external access
124 # args: <protocol> <internal-port> <external-port>
125 __dmaapmed_set_protocoll() {
126         echo -e $BOLD"$DMAAP_MED_DISPLAY_NAME protocol setting"$EBOLD
127         echo -e " Using $BOLD $1 $EBOLD towards $DMAAP_MED_DISPLAY_NAME"
128
129         ## Access to Dmaap mediator
130
131         DMAAP_MED_SERVICE_PATH=$1"://"$DMAAP_MED_APP_NAME":"$2  # docker access, container->container and script->container via proxy
132         if [ $RUNMODE == "KUBE" ]; then
133                 DMAAP_MED_SERVICE_PATH=$1"://"$DMAAP_MED_APP_NAME.$KUBE_NONRTRIC_NAMESPACE":"$3 # kube access, pod->svc and script->svc via proxy
134         fi
135
136         # DMAAP_MED_ADAPTER used for switching between REST and DMAAP (only REST supported currently)
137         DMAAP_MED_ADAPTER_TYPE="REST"
138         DMAAP_MED_ADAPTER=$DMAAP_MED_SERVICE_PATH
139
140         echo ""
141 }
142
143 # Export env vars for config files, docker compose and kube resources
144 # args: PROXY|NOPROXY
145 __dmaapmed_export_vars() {
146
147         export DMAAP_MED_APP_NAME
148         export DMAAP_MED_DISPLAY_NAME
149
150         export KUBE_NONRTRIC_NAMESPACE
151         export DOCKER_SIM_NWNAME
152
153         export DMAAP_MED_IMAGE
154
155         export DMAAP_MED_INTERNAL_PORT
156         export DMAAP_MED_INTERNAL_SECURE_PORT
157         export DMAAP_MED_EXTERNAL_PORT
158         export DMAAP_MED_EXTERNAL_SECURE_PORT
159
160         export DMAAP_MED_DATA_MOUNT_PATH
161         export DMAAP_MED_HOST_MNT_DIR
162         export DMAAP_MED_DATA_FILE
163         export DMAAP_MED_DATA_CONFIGMAP_NAME=$DMAAP_MED_APP_NAME"-data"
164
165         if [ $1 == "PROXY" ]; then
166                 export DMAAP_MED_HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_CONFIG_PORT  #Set if proxy is started
167                 export DMAAP_MED_HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_CONFIG_HOST_NAME #Set if proxy is started
168                 if [ $DMAAP_MED_HTTP_PROXY_CONFIG_PORT -eq 0 ] || [ -z "$DMAAP_MED_HTTP_PROXY_CONFIG_HOST_NAME" ]; then
169                         echo -e $YELLOW" Warning: HTTP PROXY will not be configured, proxy app not started"$EYELLOW
170                 else
171                         echo " Configured with http proxy"
172                 fi
173         else
174                 export DMAAP_MED_HTTP_PROXY_CONFIG_PORT=0
175                 export DMAAP_MED_HTTP_PROXY_CONFIG_HOST_NAME=""
176                 echo " Configured without http proxy"
177         fi
178
179         # paths to other components
180         export ICS_SERVICE_PATH
181
182         export DMAAP_MED_CONF_SELF_HOST=$(echo $DMAAP_MED_SERVICE_PATH | cut -d: -f1-2)
183         export DMAAP_MED_CONF_SELF_PORT=$(echo $DMAAP_MED_SERVICE_PATH | cut -d: -f3)
184         export MR_SERVICE_PATH
185 }
186
187 # Start the Dmaap mediator
188 # args: -
189 # (Function for test scripts)
190 start_dmaapmed() {
191
192         echo -e $BOLD"Starting $DMAAP_MED_DISPLAY_NAME"$EBOLD
193
194         if [ $RUNMODE == "KUBE" ]; then
195
196                 # Check if app shall be fully managed by the test script
197                 __check_included_image "DMAAPMED"
198                 retcode_i=$?
199
200                 # Check if app shall only be used by the testscipt
201                 __check_prestarted_image "DMAAPMED"
202                 retcode_p=$?
203
204                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
205                         echo -e $RED"The $DMAAP_MED_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
206                         echo -e $RED"The $DMAAP_MED_APP_NAME will not be started"$ERED
207                         exit
208                 fi
209                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
210                         echo -e $RED"The $DMAAP_MED_APP_NAME app is included both as managed and prestarted in this test script"$ERED
211                         echo -e $RED"The $DMAAP_MED_APP_NAME will not be started"$ERED
212                         exit
213                 fi
214
215                 # Check if app shall be used - not managed - by the test script
216                 if [ $retcode_p -eq 0 ]; then
217                         echo -e " Using existing $DMAAP_MED_APP_NAME deployment and service"
218                         echo " Setting DMAAPMED replicas=1"
219                         __kube_scale statefulset $DMAAP_MED_APP_NAME $KUBE_NONRTRIC_NAMESPACE 1
220                 fi
221
222                 if [ $retcode_i -eq 0 ]; then
223                         echo -e " Creating $DMAAP_MED_APP_NAME deployment and service"
224
225                         __kube_create_namespace $KUBE_NONRTRIC_NAMESPACE
226
227                         __dmaapmed_export_vars $1
228
229                         # Create config map for data
230                         data_json=$PWD/tmp/$DMAAP_MED_DATA_FILE
231                         if [ $# -lt 2 ]; then
232                                 #create empty dummy file
233                                 echo "{}" > $data_json
234                         else
235                                 cp $2 $data_json
236                         fi
237                         output_yaml=$PWD/tmp/dmaapmed_cfd.yaml
238                         __kube_create_configmap $DMAAP_MED_DATA_CONFIGMAP_NAME $KUBE_NONRTRIC_NAMESPACE autotest DMAAPMED $data_json $output_yaml
239
240                         # Create service
241                         input_yaml=$SIM_GROUP"/"$DMAAP_MED_COMPOSE_DIR"/"svc.yaml
242                         output_yaml=$PWD/tmp/dmaapmed_svc.yaml
243                         __kube_create_instance service $DMAAP_MED_APP_NAME $input_yaml $output_yaml
244
245                         # Create app
246                         input_yaml=$SIM_GROUP"/"$DMAAP_MED_COMPOSE_DIR"/"app.yaml
247                         output_yaml=$PWD/tmp/dmaapmed_app.yaml
248                         __kube_create_instance app $DMAAP_MED_APP_NAME $input_yaml $output_yaml
249
250                 fi
251
252                 __check_service_start $DMAAP_MED_APP_NAME $DMAAP_MED_SERVICE_PATH$DMAAP_MED_ALIVE_URL
253
254         else
255                 # Check if docker app shall be fully managed by the test script
256                 __check_included_image 'DMAAPMED'
257                 if [ $? -eq 1 ]; then
258                         echo -e $RED"The $DMAAP_MED_DISPLAY_NAME app is not included in this test script"$ERED
259                         echo -e $RED"The $DMAAP_MED_DISPLAY_NAME will not be started"$ERED
260                         exit
261                 fi
262
263                 __dmaapmed_export_vars $1
264
265                 dest_file=$SIM_GROUP/$DMAAP_MED_COMPOSE_DIR/$DMAAP_MED_HOST_MNT_DIR/$DMAAP_MED_DATA_FILE
266
267                 envsubst < $2 > $dest_file
268
269                 __start_container $DMAAP_MED_COMPOSE_DIR "" NODOCKERARGS 1 $DMAAP_MED_APP_NAME
270
271                 __check_service_start $DMAAP_MED_APP_NAME $DMAAP_MED_SERVICE_PATH$DMAAP_MED_ALIVE_URL
272         fi
273         echo ""
274 }