Integrated gateway and updated kube 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 ################ 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 that exist with staging, snapshot,release tags
27 __CONSUL_imagesetup() {
28         __check_and_create_image_var CONSUL "CONSUL_IMAGE" "CONSUL_IMAGE_BASE" "CONSUL_IMAGE_TAG" REMOTE_PROXY "$CONSUL_DISPLAY_NAME"
29
30 }
31
32 # Create the image var used during the test
33 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
34 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
35 __CBS_imagesetup() {
36         __check_and_create_image_var  CBS "CBS_IMAGE" "CBS_IMAGE_BASE" "CBS_IMAGE_TAG" REMOTE_RELEASE_ONAP "$CBS_DISPLAY_NAME"
37
38 }
39
40 # Pull image from remote repo or use locally built image
41 # arg: <pull-policy-override> <pull-policy-original>
42 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
43 # <pull-policy-original> Shall be used for images that does not allow overriding
44 # Both var may contain: 'remote', 'remote-remove' or 'local'
45 __CONSUL_imagepull() {
46         __check_and_pull_image $2 "$CONSUL_DISPLAY_NAME" $CONSUL_APP_NAME $CONSUL_IMAGE
47 }
48
49 # Pull image from remote repo or use locally built image
50 # arg: <pull-policy-override> <pull-policy-original>
51 # <pull-policy-override> Shall be used for images allowing overriding. For example use a local image when test is started to use released images
52 # <pull-policy-original> Shall be used for images that does not allow overriding
53 # Both var may contain: 'remote', 'remote-remove' or 'local'
54 __CBS_imagepull() {
55         __check_and_pull_image $2 "$CBS_DISPLAY_NAME" $CBS_APP_NAME $CBS_IMAGE
56 }
57
58 # Build image (only for simulator or interfaces stubs owned by the test environment)
59 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
60 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
61 __CONSUL_imagebuild() {
62         echo -e $RED" Image for app CONSUL shall never be built"$ERED
63 }
64
65 # Build image (only for simulator or interfaces stubs owned by the test environment)
66 # arg: <image-tag-suffix> (selects staging, snapshot, release etc)
67 # <image-tag-suffix> is present only for images with staging, snapshot,release tags
68 __CBS_imagebuild() {
69         echo -e $RED" Image for app CBS shall never be built"$ERED
70 }
71
72 # Generate a string for each included image using the app display name and a docker images format string
73 # arg: <docker-images-format-string> <file-to-append>
74 __CONSUL_image_data() {
75         echo -e "$CONSUL_DISPLAY_NAME\t$(docker images --format $1 $CONSUL_IMAGE)" >>   $2
76 }
77
78 # Generate a string for each included image using the app display name and a docker images format string
79 # arg: <docker-images-format-string> <file-to-append>
80 __CBS_image_data() {
81         echo -e "$CBS_DISPLAY_NAME\t$(docker images --format $1 $CBS_IMAGE)" >>   $2
82 }
83
84 # Scale kubernetes resources to zero
85 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
86 # This function is called for apps fully managed by the test script
87 __CONSUL_kube_scale_zero() {
88         echo -e $RED" Image for app CONSUL is not used in kube"$ERED
89 }
90
91 # Scale kubernetes resources to zero
92 # All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
93 # This function is called for apps fully managed by the test script
94 __CBS_kube_scale_zero() {
95         echo -e $RED" Image for app CBS is not used in kube"$ERED
96 }
97
98 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
99 # This function is called for prestarted apps not managed by the test script.
100 __CONSUL_kube_scale_zero_and_wait() {
101         echo -e $RED" CONSUL app is not used in kube"$ERED
102 }
103
104 # Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
105 # This function is called for prestarted apps not managed by the test script.
106 __CBS_kube_scale_zero_and_wait() {
107         echo -e $RED" CBS app is not used in kube"$ERED
108 }
109
110 # Delete all kube resouces for the app
111 # This function is called for apps managed by the test script.
112 __CONSUL_kube_delete_all() {
113         echo -e $RED" CONSUL app is not used in kube"$ERED
114 }
115
116 # Delete all kube resouces for the app
117 # This function is called for apps managed by the test script.
118 __CBS_kube_delete_all() {
119         echo -e $RED" CBS app is not used in kube"$ERED
120 }
121
122 # Store docker logs
123 # This function is called for apps managed by the test script.
124 # args: <log-dir> <file-prexix>
125 __CONSUL_store_docker_logs() {
126         docker logs $CONSUL_APP_NAME > $1/$2_consul.log 2>&1
127 }
128
129 # Store docker logs
130 # This function is called for apps managed by the test script.
131 # args: <log-dir> <file-prexix>
132 __CBS_store_docker_logs() {
133         docker logs $CBS_APP_NAME > $1$2_cbs.log 2>&1
134         body="$(__do_curl $LOCALHOST_HTTP:$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
135         echo "$body" > $1$2_consul_config.json 2>&1
136 }
137
138 #######################################################
139
140 CONSUL_PATH="http://$LOCALHOST:$CONSUL_EXTERNAL_PORT"
141
142 ####################
143 ### Consul functions
144 ####################
145
146 # Function to load config from a file into consul for the Policy Agent
147 # arg: <json-config-file>
148 # (Function for test scripts)
149 consul_config_app() {
150
151         echo -e $BOLD"Configuring Consul"$EBOLD
152
153         if [ $# -ne 1 ]; then
154                 ((RES_CONF_FAIL++))
155         __print_err "need one arg,  <json-config-file>" $@
156                 exit 1
157         fi
158
159         echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
160
161         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
162         result=$(__do_curl "$curlString")
163         if [ $? -ne 0 ]; then
164                 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
165                 ((RES_CONF_FAIL++))
166                 return 1
167         fi
168         body="$(__do_curl $LOCALHOST_HTTP:$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_CONFIG_KEY)"
169         echo $body > "./tmp/.output"$1
170
171         if [ $? -ne 0 ]; then
172                 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
173                 ((RES_CONF_FAIL++))
174                 return 1
175         else
176                 targetJson=$(< $1)
177                 targetJson="{\"config\":"$targetJson"}"
178                 echo "TARGET JSON: $targetJson" >> $HTTPLOG
179                 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
180                 if [ $res -ne 0 ]; then
181                         echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
182                         ((RES_CONF_FAIL++))
183                         return 1
184                 else
185                         echo -e $GREEN" Config loaded ok to consul"$EGREEN
186                 fi
187         fi
188
189         echo ""
190
191 }
192
193 # Function to perpare the consul configuration according to the current simulator configuration
194 # args: SDNC|NOSDNC <output-file>
195 # (Function for test scripts)
196 prepare_consul_config() {
197         echo -e $BOLD"Prepare Consul config"$EBOLD
198
199         echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
200
201         if [ $# != 2 ];  then
202                 ((RES_CONF_FAIL++))
203         __print_err "need two args,  SDNC|NOSDNC <output-file>" $@
204                 exit 1
205         fi
206
207         if [ $1 == "SDNC" ]; then
208                 echo -e " Config$BOLD including SDNC$EBOLD configuration"
209         elif [ $1 == "NOSDNC" ];  then
210                 echo -e " Config$BOLD excluding SDNC$EBOLD configuration"
211         else
212                 ((RES_CONF_FAIL++))
213         __print_err "need two args,  SDNC|NOSDNC <output-file>" $@
214                 exit 1
215         fi
216
217         config_json="\n            {"
218         if [ $1 == "SDNC" ]; then
219                 config_json=$config_json"\n   \"controller\": ["
220                 config_json=$config_json"\n                     {"
221                 config_json=$config_json"\n                       \"name\": \"$SDNC_APP_NAME\","
222                 config_json=$config_json"\n                       \"baseUrl\": \"$SDNC_SERVICE_PATH\","
223                 config_json=$config_json"\n                       \"userName\": \"$SDNC_USER\","
224                 config_json=$config_json"\n                       \"password\": \"$SDNC_PWD\""
225                 config_json=$config_json"\n                     }"
226                 config_json=$config_json"\n   ],"
227         fi
228
229         config_json=$config_json"\n   \"streams_publishes\": {"
230         config_json=$config_json"\n                            \"dmaap_publisher\": {"
231         config_json=$config_json"\n                              \"type\": \"message-router\","
232         config_json=$config_json"\n                              \"dmaap_info\": {"
233         config_json=$config_json"\n                                \"topic_url\": \"$MR_SERVICE_PATH$MR_WRITE_URL\""
234         config_json=$config_json"\n                              }"
235         config_json=$config_json"\n                            }"
236         config_json=$config_json"\n   },"
237         config_json=$config_json"\n   \"streams_subscribes\": {"
238         config_json=$config_json"\n                             \"dmaap_subscriber\": {"
239         config_json=$config_json"\n                               \"type\": \"message-router\","
240         config_json=$config_json"\n                               \"dmaap_info\": {"
241         config_json=$config_json"\n                                   \"topic_url\": \"$MR_SERVICE_PATH$MR_READ_URL\""
242         config_json=$config_json"\n                                 }"
243         config_json=$config_json"\n                               }"
244         config_json=$config_json"\n   },"
245
246         config_json=$config_json"\n   \"ric\": ["
247
248         if [ $RUNMODE == "KUBE" ]; then
249                 result=$(kubectl get pods -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
250                 rics=""
251                 ric_cntr=0
252                 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
253                         for im in $result; do
254                                 if [[ $im != *"-0" ]]; then
255                                         ric_subdomain=$(kubectl get pod $im -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.spec.subdomain}')
256                                         rics=$rics" "$im"."$ric_subdomain".nonrtric"
257                                         let ric_cntr=ric_cntr+1
258                                 fi
259                         done
260                 fi
261                 if [ $ric_cntr -eq 0 ]; then
262                         echo $YELLOW"Warning: No rics found for the configuration"$EYELLOW
263                 fi
264         else
265                 rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
266                 if [ $? -ne 0 ] || [ -z "$rics" ]; then
267                         echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
268                         ((RES_CONF_FAIL++))
269                         return 1
270                 fi
271         fi
272         cntr=0
273         for ric in $rics; do
274                 if [ $cntr -gt 0 ]; then
275                         config_json=$config_json"\n          ,"
276                 fi
277                 config_json=$config_json"\n          {"
278                 if [ $RUNMODE == "KUBE" ]; then
279                         ric_id=${ric%.*.*} #extract pod id from full hosthame
280                         ric_id=$(echo "$ric_id" | tr '-' '_')
281                 else
282                         ric_id=$ric
283                 fi
284                 echo " Found a1 sim: "$ric_id
285                 config_json=$config_json"\n            \"name\": \"$ric_id\","
286                 config_json=$config_json"\n            \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
287                 if [ $1 == "SDNC" ]; then
288                         config_json=$config_json"\n            \"controller\": \"$SDNC_APP_NAME\","
289                 fi
290                 config_json=$config_json"\n            \"managedElementIds\": ["
291                 config_json=$config_json"\n              \"me1_$ric_id\","
292                 config_json=$config_json"\n              \"me2_$ric_id\""
293                 config_json=$config_json"\n            ]"
294                 config_json=$config_json"\n          }"
295                 let cntr=cntr+1
296         done
297
298         config_json=$config_json"\n           ]"
299         config_json=$config_json"\n}"
300
301         if [ $RUNMODE == "KUBE" ]; then
302                 config_json="{\"config\":"$config_json"}"
303         fi
304
305         printf "$config_json">$2
306
307         echo ""
308 }
309
310 # Start Consul and CBS
311 # args: -
312 # (Function for test scripts)
313 start_consul_cbs() {
314
315         echo -e $BOLD"Starting $CONSUL_DISPLAY_NAME and $CBS_DISPLAY_NAME"$EBOLD
316         __check_included_image 'CONSUL'
317         if [ $? -eq 1 ]; then
318                 echo -e $RED"The Consul image has not been checked for this test run due to arg to the test script"$ERED
319                 echo -e $RED"Consul will not be started"$ERED
320                 exit
321         fi
322         export CONSUL_APP_NAME
323         export CONSUL_INTERNAL_PORT
324         export CONSUL_EXTERNAL_PORT
325         export CBS_APP_NAME
326         export CBS_INTERNAL_PORT
327         export CBS_EXTERNAL_PORT
328         export CONSUL_HOST
329         export CONSUL_DISPLAY_NAME
330         export CBS_DISPLAY_NAME
331
332         __start_container $CONSUL_CBS_COMPOSE_DIR "" NODOCKERARGS 2 $CONSUL_APP_NAME $CBS_APP_NAME
333
334         __check_service_start $CONSUL_APP_NAME "http://"$LOCALHOST_NAME":"$CONSUL_EXTERNAL_PORT$CONSUL_ALIVE_URL
335         __check_service_start $CBS_APP_NAME "http://"$LOCALHOST_NAME":"$CBS_EXTERNAL_PORT$CBS_ALIVE_URL
336
337         echo ""
338 }
339