Upgraded test env with Kubernetes support
[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 ## Access to Http Proxy Receiver
23 # Host name may be changed if app started by kube
24 # Direct access from script
25 HTTP_PROXY_HTTPX="http"
26 HTTP_PROXY_HOST_NAME=$LOCALHOST_NAME
27 HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_EXTERNAL_PORT
28
29 #########################
30 ### Http Proxy functions
31 #########################
32
33 # Start the Http Proxy in the simulator group
34 # args: -
35 # (Function for test scripts)
36 start_http_proxy() {
37
38         echo -e $BOLD"Starting $HTTP_PROXY_DISPLAY_NAME"$EBOLD
39
40         if [ $RUNMODE == "KUBE" ]; then
41
42                 # Check if app shall be fully managed by the test script
43                 __check_included_image "HTTPPROXY"
44                 retcode_i=$?
45
46                 # Check if app shall only be used by the testscipt
47                 __check_prestarted_image "HTTPPROXY"
48                 retcode_p=$?
49
50                 if [ $retcode_i -ne 0 ] && [ $retcode_p -ne 0 ]; then
51                         echo -e $RED"The $HTTP_PROXY_APP_NAME app is not included as managed nor prestarted in this test script"$ERED
52                         echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED
53                         exit
54                 fi
55                 if [ $retcode_i -eq 0 ] && [ $retcode_p -eq 0 ]; then
56                         echo -e $RED"The $HTTP_PROXY_APP_NAME app is included both as managed and prestarted in this test script"$ERED
57                         echo -e $RED"The $HTTP_PROXY_APP_NAME will not be started"$ERED
58                         exit
59                 fi
60
61                 # Check if app shall be used - not managed - by the test script
62                 if [ $retcode_p -eq 0 ]; then
63                         echo -e " Using existing $HTTP_PROXY_APP_NAME deployment and service"
64                         echo " Setting HTTPPROXY replicas=1"
65                         __kube_scale deployment $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE 1
66                 fi
67
68                 if [ $retcode_i -eq 0 ]; then
69                         echo -e " Creating $HTTP_PROXY_APP_NAME deployment and service"
70                         export HTTP_PROXY_APP_NAME
71                         export HTTP_PROXY_WEB_EXTERNAL_PORT
72                         export HTTP_PROXY_WEB_INTERNAL_PORT
73                         export HTTP_PROXY_EXTERNAL_PORT
74                         export HTTP_PROXY_INTERNAL_PORT
75                         export KUBE_SIM_NAMESPACE
76                         export HTTP_PROXY_IMAGE
77
78                         __kube_create_namespace $KUBE_SIM_NAMESPACE
79
80                         # Create service
81                         input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"svc.yaml
82                         output_yaml=$PWD/tmp/proxy_svc.yaml
83                         __kube_create_instance service $HTTP_PROXY_APP_NAME $input_yaml $output_yaml
84
85                         # Create app
86                         input_yaml=$SIM_GROUP"/"$HTTP_PROXY_COMPOSE_DIR"/"app.yaml
87                         output_yaml=$PWD/tmp/proxy_app.yaml
88                         __kube_create_instance app $HTTP_PROXY_APP_NAME $input_yaml $output_yaml
89
90                 fi
91
92                 echo " Retrieving host and ports for service..."
93                 HTTP_PROXY_HOST_NAME=$(__kube_get_service_host $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE)
94                 HTTP_PROXY_WEB_EXTERNAL_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "web")
95                 HTTP_PROXY_EXTERNAL_PORT=$(__kube_get_service_port $HTTP_PROXY_APP_NAME $KUBE_SIM_NAMESPACE "http")
96
97                 HTTP_PROXY_PATH=$HTTP_PROXY_HTTPX"://"$HTTP_PROXY_HOST_NAME":"$HTTP_PROXY_WEB_EXTERNAL_PORT
98                 HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_EXTERNAL_PORT
99                 HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME"."$KUBE_SIM_NAMESPACE
100
101                 echo " Host IP, http port: $HTTP_PROXY_HOST_NAME $HTTP_PROXY_WEB_EXTERNAL_PORT"
102
103                 __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_PATH$HTTP_PROXY_ALIVE_URL
104
105         else
106                 # Check if docker app shall be fully managed by the test script
107                 __check_included_image 'HTTPPROXY'
108                 if [ $? -eq 1 ]; then
109                         echo -e $RED"The Http Proxy app is not included in this test script"$ERED
110                         echo -e $RED"The Http Proxy will not be started"$ERED
111                         exit
112                 fi
113
114                 export HTTP_PROXY_APP_NAME
115                 export HTTP_PROXY_WEB_EXTERNAL_PORT
116                 export HTTP_PROXY_WEB_INTERNAL_PORT
117                 export DOCKER_SIM_NWNAME
118
119                 __start_container $HTTP_PROXY_COMPOSE_DIR NODOCKERARGS 1 $HTTP_PROXY_APP_NAME
120
121         __check_service_start $HTTP_PROXY_APP_NAME $HTTP_PROXY_PATH$HTTP_PROXY_ALIVE_URL
122
123                 HTTP_PROXY_CONFIG_PORT=$HTTP_PROXY_INTERNAL_PORT
124                 HTTP_PROXY_CONFIG_HOST_NAME=$HTTP_PROXY_APP_NAME
125
126         fi
127         echo ""
128 }
129