Reorganize files in O-RU use case prep for Docker
[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" REMOTE_PROXY "$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         echo -e $RED"Image for app HTTPPROXY shall never be built"$ERED
45 }
46
47 # Generate a string for each included image using the app display name and a docker images format string
48 # If a custom image repo is used then also the source image from the local repo is listed
49 # arg: <docker-images-format-string> <file-to-append>
50 __HTTPPROXY_image_data() {
51         echo -e "$HTTP_PROXY_DISPLAY_NAME\t$(docker images --format $1 $HTTP_PROXY_IMAGE)" >>   $2
52         if [ ! -z "$HTTP_PROXY_IMAGE_SOURCE" ]; then
53                 echo -e "-- source image --\t$(docker images --format $1 $HTTP_PROXY_IMAGE_SOURCE)" >>   $2
54         fi
55 }
56
57 # Scale kubernetes resources to zero
58 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
59 # This function is called for apps fully managed by the test script
60 __HTTPPROXY_kube_scale_zero() {
61         __kube_scale_all_resources $KUBE_SIM_NAMESPACE autotest HTTPPROXY
62 }
63
64 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
65 # This function is called for prestarted apps not managed by the test script.
66 __HTTPPROXY_kube_scale_zero_and_wait() {
67         echo -e $RED" NGW replicas kept as is"$ERED
68 }
69
70 # Delete all kube resouces for the app
71 # This function is called for apps managed by the test script.
72 __HTTPPROXY_kube_delete_all() {
73         __kube_delete_all_resources $KUBE_SIM_NAMESPACE autotest HTTPPROXY
74 }
75
76 # Store docker logs
77 # This function is called for apps managed by the test script.
78 # args: <log-dir> <file-prexix>
79 __HTTPPROXY_store_docker_logs() {
80         docker logs $HTTP_PROXY_APP_NAME > $1$2_httpproxy.log 2>&1
81 }
82
83 #######################################################
84
85
86 ## Access to Http Proxy Receiver
87 # Host name may be changed if app started by kube
88 # Direct access from script
89 HTTP_PROXY_HTTPX="http"
90 HTTP_PROXY_HOST_NAME=$LOCALHOST_NAME
91 HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_EXTERNAL_PORT
92
93 #########################
94 ### Http Proxy functions
95 #########################
96
97 # Start the Http Proxy in the simulator group
98 # args: -
99 # (Function for test scripts)
100 start_http_proxy() {
101
102         echo -e $BOLD"Starting $HTTP_PROXY_DISPLAY_NAME"$EBOLD
103
104         if [ $RUNMODE == "KUBE" ]; then
105
106                 # Check if app shall be fully managed by the test script
107                 __check_included_image "HTTPPROXY"
108                 retcode_i=$?
109
110                 # Check if app shall only be used by the testscipt
111                 __check_prestarted_image "HTTPPROXY"
112                 retcode_p=$?
113
114                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
115                         echo -e $RED"The $HTTP_PROXY_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
116                         echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED
117                         exit
118                 fi
119                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
120                         echo -e $RED"The $HTTP_PROXY_APP_NAME app is included both as managed and prestarted in this test script"$ERED
121                         echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED
122                         exit
123                 fi
124
125                 # Check if app shall be used - not managed - by the test script
126                 if [ $retcode_p -eq 0 ]; then
127                         echo -e " Using existing $HTTP_PROXY_APP_NAME deployment and service"
128                         echo " Setting HTTPPROXY replicas=1"
129                         __kube_scale deployment $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE 1
130                 fi
131
132                 if [ $retcode_i -eq 0 ]; then
133                         echo -e " Creating $HTTP_PROXY_APP_NAME deployment and service"
134                         export HTTP_PROXY_APP_NAME
135                         export HTTP_PROXY_WEB_EXTERNAL_PORT
136                         export HTTP_PROXY_WEB_INTERNAL_PORT
137                         export HTTP_PROXY_EXTERNAL_PORT
138                         export HTTP_PROXY_INTERNAL_PORT
139                         export KUBE_SIM_NAMESPACE
140                         export HTTP_PROXY_IMAGE
141
142                         __kube_create_namespace $KUBE_SIM_NAMESPACE
143
144                         # Create service
145                         input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"svc.yaml
146                         output_yaml=$PWD/tmp/proxy_svc.yaml
147                         __kube_create_instance service $HTTP_PROXY_APP_NAME $input_yaml $output_yaml
148
149                         # Create app
150                         input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"app.yaml
151                         output_yaml=$PWD/tmp/proxy_app.yaml
152                         __kube_create_instance app $HTTP_PROXY_APP_NAME $input_yaml $output_yaml
153
154                 fi
155
156                 echo " Retrieving host and ports for service..."
157                 HTTP_PROXY_HOST_NAME=$(__kube_get_service_host $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE)
158                 HTTP_PROXY_WEB_EXTERNAL_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web")
159                 HTTP_PROXY_EXTERNAL_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http")
160
161                 HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_EXTERNAL_PORT
162                 HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_EXTERNAL_PORT
163                 HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME"."$KUBE_SIM_NAMESPACE
164
165                 echo " Host IP, http port: $HTTP_PROXY_HOST_NAME $HTTP_PROXY_WEB_EXTERNAL_PORT"
166
167                 __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_PATH$HTTP_PROXY_ALIVE_URL
168
169         else
170                 # Check if docker app shall be fully managed by the test script
171                 __check_included_image 'HTTPPROXY'
172                 if [ $? -eq 1 ]; then
173                         echo -e $RED"The Http Proxy app is not included in this test script"$ERED
174                         echo -e $RED"The Http Proxy will not be started"$ERED
175                         exit
176                 fi
177
178                 export HTTP_PROXY_APP_NAME
179                 export HTTP_PROXY_WEB_EXTERNAL_PORT
180                 export HTTP_PROXY_WEB_INTERNAL_PORT
181                 export DOCKER_SIM_NWNAME
182
183                 export HTTP_PROXY_DISPLAY_NAME
184
185                 __start_container $HTTP_PROXY_COMPOSE_DIR "" NODOCKERARGS 1 $HTTP_PROXY_APP_NAME
186
187         __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_PATH$HTTP_PROXY_ALIVE_URL
188
189                 HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_INTERNAL_PORT
190                 HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME
191
192         fi
193         echo ""
194 }
195