Adaptation of test env to helm chart
[nonrtric.git] / test / common / http_proxy_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 managemnt functions for Http Proxy
21
22 ################ Test engine functions ################
23
24 # Create the image var used during the test
25 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
26 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
27 __HTTPPROXY_imagesetup() {
28         __check_and_create_image_var HTTPPROXY "HTTP_PROXY_IMAGE" "HTTP_PROXY_IMAGE_BASE" "HTTP_PROXY_IMAGE_TAG" LOCAL "$HTTP_PROXY_DISPLAY_NAME"
29 }
30
31 # Pull image from remote repo or use locally built image
32 # arg: <pull-policy-override> <pull-policy-original>
33 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
34 # <pull-policy-original> Shall be used for images that does not allow overriding
35 # Both var may contain: 'remote', 'remote-remove' or 'local'
36 __HTTPPROXY_imagepull() {
37         __check_and_pull_image $2 "$HTTP_PROXY_DISPLAY_NAME" $HTTP_PROXY_APP_NAME HTTP_PROXY_IMAGE
38 }
39
40 # Build image (only for simulator or interfaces stubs owned by the test environment)
41 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
42 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
43 __HTTPPROXY_imagebuild() {
44         cd ../$HTTP_PROXY_BUILD_DIR       # Note: Reusing same impl as for kube proxy
45         echo " Building HTTPPROXY - $HTTP_PROXY_DISPLAY_NAME - image: $HTTP_PROXY_IMAGE"
46         docker build  --build-arg NEXUS_PROXY_REPO=$NEXUS_PROXY_REPO -t $HTTP_PROXY_IMAGE . &> .dockererr
47         if [ $? -eq 0 ]; then
48                 echo -e  $GREEN"  Build Ok"$EGREEN
49                 __retag_and_push_image HTTP_PROXY_IMAGE
50                 if [ $? -ne 0 ]; then
51                         exit 1
52                 fi
53         else
54                 echo -e $RED"  Build Failed"$ERED
55                 ((RES_CONF_FAIL++))
56                 cat .dockererr
57                 echo -e $RED"Exiting...."$ERED
58                 exit 1
59         fi
60 }
61
62 # Generate a string for each included image using the app display name and a docker images format string
63 # If a custom image repo is used then also the source image from the local repo is listed
64 # arg: <docker-images-format-string> <file-to-append>
65 __HTTPPROXY_image_data() {
66         echo -e "$HTTP_PROXY_DISPLAY_NAME\t$(docker images --format $1 $HTTP_PROXY_IMAGE)" >>   $2
67         if [ ! -z "$HTTP_PROXY_IMAGE_SOURCE" ]; then
68                 echo -e "-- source image --\t$(docker images --format $1 $HTTP_PROXY_IMAGE_SOURCE)" >>   $2
69         fi
70 }
71
72 # Scale kubernetes resources to zero
73 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
74 # This function is called for apps fully managed by the test script
75 __HTTPPROXY_kube_scale_zero() {
76         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest HTTPPROXY
77 }
78
79 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
80 # This function is called for prestarted apps not managed by the test script.
81 __HTTPPROXY_kube_scale_zero_and_wait() {
82         echo -e $RED" HTTPPROXY replicas kept as is"$ERED
83 }
84
85 # Delete all kube resouces for the app
86 # This function is called for apps managed by the test script.
87 __HTTPPROXY_kube_delete_all() {
88         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest HTTPPROXY
89 }
90
91 # Store docker logs
92 # This function is called for apps managed by the test script.
93 # args: <log-dir> <file-prexix>
94 __HTTPPROXY_store_docker_logs() {
95         if [ $RUNMODE == "KUBE" ]; then
96                 kubectl  logs -l "autotest=HTTPPROXY" -n $KUBE_SIM_NAMESPACE --tail=-1 > $1$2_httpproxy.log 2>&1
97         else
98                 docker logs $HTTP_PROXY_APP_NAME > $1$2_httpproxy.log 2>&1
99         fi
100 }
101
102 # Initial setup of protocol, host and ports
103 # This function is called for apps managed by the test script.
104 # args: -
105 __HTTPPROXY_initial_setup() {
106         :
107 }
108
109 #######################################################
110
111
112 ## Access to Http Proxy Receiver
113 # Host name may be changed if app started by kube
114 # Direct access from script
115 HTTP_PROXY_HTTPX="http"
116 HTTP_PROXY_HOST_NAME=$LOCALHOST_NAME
117 HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_EXTERNAL_PORT
118
119 #########################
120 ### Http Proxy functions
121 #########################
122
123 # All calls to httpproxy will be directed to the http interface
124 # args: -
125 # (Function for test scripts)
126 use_http_proxy_http() {
127         echo -e $BOLD"$HTTP_PROXY_DISPLAY_NAME protocol setting"$EBOLD
128         echo -e " Using $BOLD http $EBOLD"
129         HTTP_PROXY_HTTPX="http"
130         HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_EXTERNAL_PORT
131
132         echo ""
133 }
134
135 # All calls to httpproxy will be directed to the https interface
136 # args: -
137 # (Function for test scripts)
138 use_http_proxy_https() {
139         echo -e $BOLD"$HTTP_PROXY_DISPLAY_NAME protocol setting"$EBOLD
140         echo -e " Using $BOLD https $EBOLD"
141         HTTP_PROXY_HTTPX="https"
142         HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_EXTERNAL_SECURE_PORT
143
144         echo ""
145 }
146
147 # Start the Http Proxy in the simulator group
148 # args: -
149 # (Function for test scripts)
150 start_http_proxy() {
151
152         echo -e $BOLD"Starting $HTTP_PROXY_DISPLAY_NAME"$EBOLD
153
154         if [ $RUNMODE == "KUBE" ]; then
155
156                 # Check if app shall be fully managed by the test script
157                 __check_included_image "HTTPPROXY"
158                 retcode_i=$?
159
160                 # Check if app shall only be used by the testscipt
161                 __check_prestarted_image "HTTPPROXY"
162                 retcode_p=$?
163
164                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
165                         echo -e $RED"The $HTTP_PROXY_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
166                         echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED
167                         exit
168                 fi
169                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
170                         echo -e $RED"The $HTTP_PROXY_APP_NAME app is included both as managed and prestarted in this test script"$ERED
171                         echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED
172                         exit
173                 fi
174
175                 # Check if app shall be used - not managed - by the test script
176                 if [ $retcode_p -eq 0 ]; then
177                         echo -e " Using existing $HTTP_PROXY_APP_NAME deployment and service"
178                         echo " Setting HTTPPROXY replicas=1"
179                         __kube_scale deployment $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE 1
180                 fi
181
182                 if [ $retcode_i -eq 0 ]; then
183                         echo -e " Creating $HTTP_PROXY_APP_NAME deployment and service"
184                         export HTTP_PROXY_APP_NAME
185
186                         export HTTP_PROXY_WEB_EXTERNAL_PORT
187                         export HTTP_PROXY_WEB_INTERNAL_PORT
188                         export HTTP_PROXY_EXTERNAL_PORT
189                         export HTTP_PROXY_INTERNAL_PORT
190
191                         export HTTP_PROXY_WEB_EXTERNAL_SECURE_PORT
192                         export HTTP_PROXY_WEB_INTERNAL_SECURE_PORT
193                         export HTTP_PROXY_EXTERNAL_SECURE_PORT
194                         export HTTP_PROXY_INTERNAL_SECURE_PORT
195
196                         export KUBE_SIM_NAMESPACE
197                         export HTTP_PROXY_IMAGE
198
199                         __kube_create_namespace $KUBE_SIM_NAMESPACE
200
201                         # Create service
202                         input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"svc.yaml
203                         output_yaml=$PWD/tmp/proxy_svc.yaml
204                         __kube_create_instance service $HTTP_PROXY_APP_NAME $input_yaml $output_yaml
205
206                         # Create app
207                         input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"app.yaml
208                         output_yaml=$PWD/tmp/proxy_app.yaml
209                         __kube_create_instance app $HTTP_PROXY_APP_NAME $input_yaml $output_yaml
210
211                 fi
212
213                 echo " Retrieving host and ports for service..."
214                 HTTP_PROXY_HOST_NAME=$(__kube_get_service_host $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE)
215                 HTTP_PROXY_WEB_EXTERNAL_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web")
216                 HTTP_PROXY_WEB_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "webs")
217
218                 HTTP_PROXY_EXTERNAL_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http")
219                 HTTP_PROXY_EXTERNAL_SECURE_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "https")
220
221                 if [ $HTTP_PROXY_HTTPX == "http" ]; then
222                         HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_EXTERNAL_PORT
223                         HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_EXTERNAL_PORT
224                         HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME"."$KUBE_SIM_NAMESPACE
225
226                         echo " Host IP, http port: $HTTP_PROXY_HOST_NAME $HTTP_PROXY_WEB_EXTERNAL_PORT"
227                 else
228                         HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_EXTERNAL_SECURE_PORT
229                         HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_EXTERNAL_SECURE_PORT
230                         HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME"."$KUBE_SIM_NAMESPACE
231
232                         echo " Host IP, https port: $HTTP_PROXY_HOST_NAME $HTTP_PROXY_WEB_EXTERNAL_SECURE_PORT"
233                 fi
234
235                 __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_PATH$HTTP_PROXY_ALIVE_URL
236
237         else
238                 # Check if docker app shall be fully managed by the test script
239                 __check_included_image 'HTTPPROXY'
240                 if [ $? -eq 1 ]; then
241                         echo -e $RED"The Http Proxy app is not included in this test script"$ERED
242                         echo -e $RED"The Http Proxy will not be started"$ERED
243                         exit
244                 fi
245
246                 export HTTP_PROXY_APP_NAME
247                 export HTTP_PROXY_EXTERNAL_PORT
248                 export HTTP_PROXY_INTERNAL_PORT
249                 export HTTP_PROXY_EXTERNAL_SECURE_PORT
250                 export HTTP_PROXY_INTERNAL_SECURE_PORT
251                 export HTTP_PROXY_WEB_EXTERNAL_PORT
252                 export HTTP_PROXY_WEB_INTERNAL_PORT
253                 export HTTP_PROXY_WEB_EXTERNAL_SECURE_PORT
254                 export HTTP_PROXY_WEB_INTERNAL_SECURE_PORT
255                 export DOCKER_SIM_NWNAME
256
257                 export HTTP_PROXY_DISPLAY_NAME
258
259                 __start_container $HTTP_PROXY_COMPOSE_DIR "" NODOCKERARGS 1 $HTTP_PROXY_APP_NAME
260
261                 if [ $HTTP_PROXY_HTTPX == "http" ]; then
262                         HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_INTERNAL_PORT
263                 else
264                         HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_INTERNAL_SECURE_PORT
265                 fi
266         __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_PATH$HTTP_PROXY_ALIVE_URL
267
268                 if [ $HTTP_PROXY_HTTPX == "http" ]; then
269                         HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_INTERNAL_PORT
270                 else
271                         HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_INTERNAL_SECURE_PORT
272                 fi
273                 HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME
274
275         fi
276         echo ""
277 }
278