Upgraded test env with Kubernetes support
[nonrtric.git] / test / common / consul_cbs_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 management functions and test functions for Consul/CBS
21
22 CONSUL_PATH="http://$LOCALHOST:$CONSUL_EXTERNAL_PORT"
23
24 ####################
25 ### Consul functions
26 ####################
27
28 # Function to load config from a file into consul for the Policy Agent
29 # arg: <json-config-file>
30 # (Function for test scripts)
31 consul_config_app() {
32
33         echo -e $BOLD"Configuring Consul"$EBOLD
34
35         if [ $# -ne 1 ]; then
36                 ((RES_CONF_FAIL++))
37         __print_err "need one arg,  <json-config-file>" $@
38                 exit 1
39         fi
40
41         echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
42
43         curlString="$LOCALHOST_HTTP:${CONSUL_EXTERNAL_PORT}/v1/kv/${POLICY_AGENT_CONFIG_KEY}?dc=dc1 -X PUT -H Accept:application/json -H Content-Type:application/json -H X-Requested-With:XMLHttpRequest --data-binary @"$1
44         result=$(__do_curl "$curlString")
45         if [ $? -ne 0 ]; then
46                 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
47                 ((RES_CONF_FAIL++))
48                 return 1
49         fi
50         body="$(__do_curl $LOCALHOST_HTTP:$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_CONFIG_KEY)"
51         echo $body > "./tmp/.output"$1
52
53         if [ $? -ne 0 ]; then
54                 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
55                 ((RES_CONF_FAIL++))
56                 return 1
57         else
58                 targetJson=$(< $1)
59                 targetJson="{\"config\":"$targetJson"}"
60                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
61                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
62                 if [ $res -ne 0 ]; then
63                         echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
64                         ((RES_CONF_FAIL++))
65                         return 1
66                 else
67                         echo -e $GREEN" Config loaded ok to consul"$EGREEN
68                 fi
69         fi
70
71         echo ""
72
73 }
74
75 # Function to perpare the consul configuration according to the current simulator configuration
76 # args: SDNC|NOSDNC <output-file>
77 # (Function for test scripts)
78 prepare_consul_config() {
79         echo -e $BOLD"Prepare Consul config"$EBOLD
80
81         echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
82
83         if [ $# != 2 ];  then
84                 ((RES_CONF_FAIL++))
85         __print_err "need two args,  SDNC|NOSDNC <output-file>" $@
86                 exit 1
87         fi
88
89         if [ $1 == "SDNC" ]; then
90                 echo -e " Config$BOLD including SDNC$EBOLD configuration"
91         elif [ $1 == "NOSDNC" ];  then
92                 echo -e " Config$BOLD excluding SDNC$EBOLD configuration"
93         else
94                 ((RES_CONF_FAIL++))
95         __print_err "need two args,  SDNC|NOSDNC <output-file>" $@
96                 exit 1
97         fi
98
99         config_json="\n            {"
100         if [ $1 == "SDNC" ]; then
101                 config_json=$config_json"\n   \"controller\": ["
102                 config_json=$config_json"\n                     {"
103                 config_json=$config_json"\n                       \"name\": \"$SDNC_APP_NAME\","
104                 config_json=$config_json"\n                       \"baseUrl\": \"$SDNC_SERVICE_PATH\","
105                 config_json=$config_json"\n                       \"userName\": \"$SDNC_USER\","
106                 config_json=$config_json"\n                       \"password\": \"$SDNC_PWD\""
107                 config_json=$config_json"\n                     }"
108                 config_json=$config_json"\n   ],"
109         fi
110
111         config_json=$config_json"\n   \"streams_publishes\": {"
112         config_json=$config_json"\n                            \"dmaap_publisher\": {"
113         config_json=$config_json"\n                              \"type\": \"message-router\","
114         config_json=$config_json"\n                              \"dmaap_info\": {"
115         config_json=$config_json"\n                                \"topic_url\": \"$MR_SERVICE_PATH$MR_WRITE_URL\""
116         config_json=$config_json"\n                              }"
117         config_json=$config_json"\n                            }"
118         config_json=$config_json"\n   },"
119         config_json=$config_json"\n   \"streams_subscribes\": {"
120         config_json=$config_json"\n                             \"dmaap_subscriber\": {"
121         config_json=$config_json"\n                               \"type\": \"message-router\","
122         config_json=$config_json"\n                               \"dmaap_info\": {"
123         config_json=$config_json"\n                                   \"topic_url\": \"$MR_SERVICE_PATH$MR_READ_URL\""
124         config_json=$config_json"\n                                 }"
125         config_json=$config_json"\n                               }"
126         config_json=$config_json"\n   },"
127
128         config_json=$config_json"\n   \"ric\": ["
129
130         if [ $RUNMODE == "KUBE" ]; then
131                 result=$(kubectl get pods -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
132                 rics=""
133                 ric_cntr=0
134                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
135                         for im in $result; do
136                                 if [[ $im != *"-0" ]]; then
137                                         ric_subdomain=$(kubectl get pod $im -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.spec.subdomain}')
138                                         rics=$rics" "$im"."$ric_subdomain".nonrtric"
139                                         let ric_cntr=ric_cntr+1
140                                 fi
141                         done
142                 fi
143                 if [ $ric_cntr -eq 0 ]; then
144                         echo $YELLOW"Warning: No rics found for the configuration"$EYELLOW
145                 fi
146         else
147                 rics=$(docker ps | grep $RIC_SIM_PREFIX | awk '{print $NF}')
148
149                 if [ $? -ne 0 ] || [ -z "$rics" ]; then
150                         echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
151                         ((RES_CONF_FAIL++))
152                         return 1
153                 fi
154         fi
155         cntr=0
156         for ric in $rics; do
157                 if [ $cntr -gt 0 ]; then
158                         config_json=$config_json"\n          ,"
159                 fi
160                 config_json=$config_json"\n          {"
161                 if [ $RUNMODE == "KUBE" ]; then
162                         ric_id=${ric%.*.*} #extract pod id from full hosthame
163                         ric_id=$(echo "$ric_id" | tr '-' '_')
164                 else
165                         ric_id=$ric
166                 fi
167                 config_json=$config_json"\n            \"name\": \"$ric_id\","
168                 config_json=$config_json"\n            \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
169                 if [ $1 == "SDNC" ]; then
170                         config_json=$config_json"\n            \"controller\": \"$SDNC_APP_NAME\","
171                 fi
172                 config_json=$config_json"\n            \"managedElementIds\": ["
173                 config_json=$config_json"\n              \"me1_$ric_id\","
174                 config_json=$config_json"\n              \"me2_$ric_id\""
175                 config_json=$config_json"\n            ]"
176                 config_json=$config_json"\n          }"
177                 let cntr=cntr+1
178         done
179
180         config_json=$config_json"\n           ]"
181         config_json=$config_json"\n}"
182
183         if [ $RUNMODE == "KUBE" ]; then
184                 config_json="{\"config\":"$config_json"}"
185         fi
186
187         printf "$config_json">$2
188
189         echo ""
190 }
191
192 # Start Consul and CBS
193 # args: -
194 # (Function for test scripts)
195 start_consul_cbs() {
196
197         echo -e $BOLD"Starting $CONSUL_DISPLAY_NAME and $CBS_DISPLAY_NAME"$EBOLD
198         __check_included_image 'CONSUL'
199         if [ $? -eq 1 ]; then
200                 echo -e $RED"The Consul image has not been checked for this test run due to arg to the test script"$ERED
201                 echo -e $RED"Consul will not be started"$ERED
202                 exit
203         fi
204         export CONSUL_APP_NAME
205         export CONSUL_INTERNAL_PORT
206         export CONSUL_EXTERNAL_PORT
207         export CBS_APP_NAME
208         export CBS_INTERNAL_PORT
209         export CBS_EXTERNAL_PORT
210         export CONSUL_HOST
211
212         __start_container $CONSUL_CBS_COMPOSE_DIR NODOCKERARGS 2 $CONSUL_APP_NAME $CBS_APP_NAME
213
214         __check_service_start $CONSUL_APP_NAME "http://"$LOCALHOST_NAME":"$CONSUL_EXTERNAL_PORT$CONSUL_ALIVE_URL
215         __check_service_start $CBS_APP_NAME "http://"$LOCALHOST_NAME":"$CBS_EXTERNAL_PORT$CBS_ALIVE_URL
216
217         echo ""
218 }
219