blob: 1b5c8a8e9bd902e5b8b668121e02a6fdfe51322c [file] [log] [blame]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001#!/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
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010022################ 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() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +020046 __check_and_pull_image $2 "$CONSUL_DISPLAY_NAME" $CONSUL_APP_NAME CONSUL_IMAGE
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010047}
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() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +020055 __check_and_pull_image $2 "$CBS_DISPLAY_NAME" $CBS_APP_NAME CBS_IMAGE
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010056}
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
BjornMagnussonXA483ee332021-04-08 01:35:24 +020073# If a custom image repo is used then also the source image from the local repo is listed
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010074# arg: <docker-images-format-string> <file-to-append>
75__CONSUL_image_data() {
76 echo -e "$CONSUL_DISPLAY_NAME\t$(docker images --format $1 $CONSUL_IMAGE)" >> $2
BjornMagnussonXA483ee332021-04-08 01:35:24 +020077 if [ ! -z "$CONSUL_IMAGE_SOURCE" ]; then
78 echo -e "-- source image --\t$(docker images --format $1 $CONSUL_IMAGE_SOURCE)" >> $2
79 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010080}
81
82# Generate a string for each included image using the app display name and a docker images format string
BjornMagnussonXA483ee332021-04-08 01:35:24 +020083# If a custom image repo is used then also the source image from the local repo is listed
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010084# arg: <docker-images-format-string> <file-to-append>
85__CBS_image_data() {
86 echo -e "$CBS_DISPLAY_NAME\t$(docker images --format $1 $CBS_IMAGE)" >> $2
BjornMagnussonXA483ee332021-04-08 01:35:24 +020087 if [ ! -z "$CBS_IMAGE_SOURCE" ]; then
88 echo -e "-- source image --\t$(docker images --format $1 $CBS_IMAGE_SOURCE)" >> $2
89 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010090}
91
92# Scale kubernetes resources to zero
93# All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
94# This function is called for apps fully managed by the test script
95__CONSUL_kube_scale_zero() {
96 echo -e $RED" Image for app CONSUL is not used in kube"$ERED
97}
98
99# Scale kubernetes resources to zero
100# All resources shall be ordered to be scaled to 0, if relevant. If not relevant to scale, then do no action.
101# This function is called for apps fully managed by the test script
102__CBS_kube_scale_zero() {
103 echo -e $RED" Image for app CBS is not used in kube"$ERED
104}
105
106# Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
107# This function is called for prestarted apps not managed by the test script.
108__CONSUL_kube_scale_zero_and_wait() {
109 echo -e $RED" CONSUL app is not used in kube"$ERED
110}
111
112# Scale kubernetes resources to zero and wait until this has been accomplished, if relevant. If not relevant to scale, then do no action.
113# This function is called for prestarted apps not managed by the test script.
114__CBS_kube_scale_zero_and_wait() {
115 echo -e $RED" CBS app is not used in kube"$ERED
116}
117
118# Delete all kube resouces for the app
119# This function is called for apps managed by the test script.
120__CONSUL_kube_delete_all() {
121 echo -e $RED" CONSUL app is not used in kube"$ERED
122}
123
124# Delete all kube resouces for the app
125# This function is called for apps managed by the test script.
126__CBS_kube_delete_all() {
127 echo -e $RED" CBS app is not used in kube"$ERED
128}
129
130# Store docker logs
131# This function is called for apps managed by the test script.
132# args: <log-dir> <file-prexix>
133__CONSUL_store_docker_logs() {
134 docker logs $CONSUL_APP_NAME > $1/$2_consul.log 2>&1
135}
136
137# Store docker logs
138# This function is called for apps managed by the test script.
139# args: <log-dir> <file-prexix>
140__CBS_store_docker_logs() {
141 docker logs $CBS_APP_NAME > $1$2_cbs.log 2>&1
142 body="$(__do_curl $LOCALHOST_HTTP:$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
143 echo "$body" > $1$2_consul_config.json 2>&1
144}
145
146#######################################################
147
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100148CONSUL_PATH="http://$LOCALHOST:$CONSUL_EXTERNAL_PORT"
149
150####################
151### Consul functions
152####################
153
154# Function to load config from a file into consul for the Policy Agent
155# arg: <json-config-file>
156# (Function for test scripts)
157consul_config_app() {
158
159 echo -e $BOLD"Configuring Consul"$EBOLD
160
161 if [ $# -ne 1 ]; then
162 ((RES_CONF_FAIL++))
163 __print_err "need one arg, <json-config-file>" $@
164 exit 1
165 fi
166
167 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
168
169 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
170 result=$(__do_curl "$curlString")
171 if [ $? -ne 0 ]; then
172 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
173 ((RES_CONF_FAIL++))
174 return 1
175 fi
176 body="$(__do_curl $LOCALHOST_HTTP:$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_CONFIG_KEY)"
177 echo $body > "./tmp/.output"$1
178
179 if [ $? -ne 0 ]; then
180 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
181 ((RES_CONF_FAIL++))
182 return 1
183 else
184 targetJson=$(< $1)
185 targetJson="{\"config\":"$targetJson"}"
186 echo "TARGET JSON: $targetJson" >> $HTTPLOG
187 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
188 if [ $res -ne 0 ]; then
189 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
190 ((RES_CONF_FAIL++))
191 return 1
192 else
193 echo -e $GREEN" Config loaded ok to consul"$EGREEN
194 fi
195 fi
196
197 echo ""
198
199}
200
201# Function to perpare the consul configuration according to the current simulator configuration
202# args: SDNC|NOSDNC <output-file>
203# (Function for test scripts)
204prepare_consul_config() {
205 echo -e $BOLD"Prepare Consul config"$EBOLD
206
207 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
208
209 if [ $# != 2 ]; then
210 ((RES_CONF_FAIL++))
211 __print_err "need two args, SDNC|NOSDNC <output-file>" $@
212 exit 1
213 fi
214
215 if [ $1 == "SDNC" ]; then
216 echo -e " Config$BOLD including SDNC$EBOLD configuration"
217 elif [ $1 == "NOSDNC" ]; then
218 echo -e " Config$BOLD excluding SDNC$EBOLD configuration"
219 else
220 ((RES_CONF_FAIL++))
221 __print_err "need two args, SDNC|NOSDNC <output-file>" $@
222 exit 1
223 fi
224
225 config_json="\n {"
226 if [ $1 == "SDNC" ]; then
227 config_json=$config_json"\n \"controller\": ["
228 config_json=$config_json"\n {"
229 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
230 config_json=$config_json"\n \"baseUrl\": \"$SDNC_SERVICE_PATH\","
231 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
232 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
233 config_json=$config_json"\n }"
234 config_json=$config_json"\n ],"
235 fi
236
237 config_json=$config_json"\n \"streams_publishes\": {"
238 config_json=$config_json"\n \"dmaap_publisher\": {"
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_WRITE_URL\""
242 config_json=$config_json"\n }"
243 config_json=$config_json"\n }"
244 config_json=$config_json"\n },"
245 config_json=$config_json"\n \"streams_subscribes\": {"
246 config_json=$config_json"\n \"dmaap_subscriber\": {"
247 config_json=$config_json"\n \"type\": \"message-router\","
248 config_json=$config_json"\n \"dmaap_info\": {"
249 config_json=$config_json"\n \"topic_url\": \"$MR_SERVICE_PATH$MR_READ_URL\""
250 config_json=$config_json"\n }"
251 config_json=$config_json"\n }"
252 config_json=$config_json"\n },"
253
254 config_json=$config_json"\n \"ric\": ["
255
256 if [ $RUNMODE == "KUBE" ]; then
257 result=$(kubectl get pods -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.items[?(@.metadata.labels.autotest=="RICSIM")].metadata.name}')
258 rics=""
259 ric_cntr=0
260 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
261 for im in $result; do
262 if [[ $im != *"-0" ]]; then
263 ric_subdomain=$(kubectl get pod $im -n $KUBE_NONRTRIC_NAMESPACE -o jsonpath='{.spec.subdomain}')
264 rics=$rics" "$im"."$ric_subdomain".nonrtric"
265 let ric_cntr=ric_cntr+1
266 fi
267 done
268 fi
269 if [ $ric_cntr -eq 0 ]; then
270 echo $YELLOW"Warning: No rics found for the configuration"$EYELLOW
271 fi
272 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100273 rics=$(docker ps --filter "name=$RIC_SIM_PREFIX" --filter "network=$DOCKER_SIM_NWNAME" --filter "status=running" --format {{.Names}})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100274 if [ $? -ne 0 ] || [ -z "$rics" ]; then
275 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
276 ((RES_CONF_FAIL++))
277 return 1
278 fi
279 fi
280 cntr=0
281 for ric in $rics; do
282 if [ $cntr -gt 0 ]; then
283 config_json=$config_json"\n ,"
284 fi
285 config_json=$config_json"\n {"
286 if [ $RUNMODE == "KUBE" ]; then
287 ric_id=${ric%.*.*} #extract pod id from full hosthame
288 ric_id=$(echo "$ric_id" | tr '-' '_')
289 else
290 ric_id=$ric
291 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100292 echo " Found a1 sim: "$ric_id
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100293 config_json=$config_json"\n \"name\": \"$ric_id\","
294 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
295 if [ $1 == "SDNC" ]; then
296 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
297 fi
298 config_json=$config_json"\n \"managedElementIds\": ["
299 config_json=$config_json"\n \"me1_$ric_id\","
300 config_json=$config_json"\n \"me2_$ric_id\""
301 config_json=$config_json"\n ]"
302 config_json=$config_json"\n }"
303 let cntr=cntr+1
304 done
305
306 config_json=$config_json"\n ]"
307 config_json=$config_json"\n}"
308
309 if [ $RUNMODE == "KUBE" ]; then
310 config_json="{\"config\":"$config_json"}"
311 fi
312
313 printf "$config_json">$2
314
315 echo ""
316}
317
318# Start Consul and CBS
319# args: -
320# (Function for test scripts)
321start_consul_cbs() {
322
323 echo -e $BOLD"Starting $CONSUL_DISPLAY_NAME and $CBS_DISPLAY_NAME"$EBOLD
324 __check_included_image 'CONSUL'
325 if [ $? -eq 1 ]; then
326 echo -e $RED"The Consul image has not been checked for this test run due to arg to the test script"$ERED
327 echo -e $RED"Consul will not be started"$ERED
328 exit
329 fi
330 export CONSUL_APP_NAME
331 export CONSUL_INTERNAL_PORT
332 export CONSUL_EXTERNAL_PORT
333 export CBS_APP_NAME
334 export CBS_INTERNAL_PORT
335 export CBS_EXTERNAL_PORT
336 export CONSUL_HOST
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100337 export CONSUL_DISPLAY_NAME
338 export CBS_DISPLAY_NAME
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100339
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100340 __start_container $CONSUL_CBS_COMPOSE_DIR "" NODOCKERARGS 2 $CONSUL_APP_NAME $CBS_APP_NAME
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100341
342 __check_service_start $CONSUL_APP_NAME "http://"$LOCALHOST_NAME":"$CONSUL_EXTERNAL_PORT$CONSUL_ALIVE_URL
343 __check_service_start $CBS_APP_NAME "http://"$LOCALHOST_NAME":"$CBS_EXTERNAL_PORT$CBS_ALIVE_URL
344
345 echo ""
346}
347