blob: 830ee7332cdf32a064bb13e733254e34716f762e [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/bin/bash
YongchaoWu9a84f512019-12-16 22:54:11 +01002
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003# ============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#
YongchaoWu9a84f512019-12-16 22:54:11 +010019
YongchaoWuffde6eb2020-01-17 13:58:51 +010020# This is a script that contains all the functions needed for auto test
BjornMagnussonXA80a92002020-03-19 14:31:06 +010021# Arg: local|remote|remote-remove [auto-clean]
YongchaoWuffde6eb2020-01-17 13:58:51 +010022
BjornMagnussonXA72667f12020-04-24 09:20:18 +020023
BjornMagnussonXA80a92002020-03-19 14:31:06 +010024#Formatting for 'echo' cmd
25BOLD="\033[1m"
26EBOLD="\033[0m"
27RED="\033[31m\033[1m"
28ERED="\033[0m"
29GREEN="\033[32m\033[1m"
30EGREEN="\033[0m"
31YELLOW="\033[33m\033[1m"
32EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020033SAMELINE="\033[0K\r"
34
35tmp=$(which python3)
36if [ $? -ne 0 ] || [ -z tmp ]; then
37 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
38 exit 1
39fi
40tmp=$(which docker)
41if [ $? -ne 0 ] || [ -z tmp ]; then
42 echo -e $RED"docker is required to run the test environment, pls install"$ERED
43 exit 1
44fi
YongchaoWu9a84f512019-12-16 22:54:11 +010045
BjornMagnussonXA80a92002020-03-19 14:31:06 +010046# Just resetting any previous echo formatting...
47echo -ne $EBOLD$ERED$EGREEN
48
49# source test environment variables
50. ../common/test_env.sh
51
52echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
53
54#Vars for A1 interface version and container count
55G1_A1_VERSION=""
56G2_A1_VERSION=""
57G3_A1_VERSION=""
58G1_COUNT=0
59G2_COUNT=0
60G3_COUNT=0
61
BjornMagnussonXA72667f12020-04-24 09:20:18 +020062# Var to switch between http and https. Extra curl flag needed for https
63export RIC_SIM_HTTPX="http"
64export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
65export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
66export RIC_SIM_CERT_MOUNT_DIR="./fakedir" #Fake dir so that the sim container does not find any cert
67
BjornMagnussonXA80a92002020-03-19 14:31:06 +010068#Localhost constant
69LOCALHOST="http://localhost:"
70
71# Make curl retries for http response codes set in this env var, space separated list of codes
72AGENT_RETRY_CODES=""
73
74# Var to hold 'auto' in case containers shall be stopped when test case ends
75AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +010076
77# Set a description string for the test case
78if [ -z "$TC_ONELINE_DESCR" ]; then
79 TC_ONELINE_DESCR="<no-description>"
80 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
81fi
82
BjornMagnussonXA80a92002020-03-19 14:31:06 +010083# Counter for test suites
84if [ -f .tmp_tcsuite_ctr ]; then
85 tmpval=$(< .tmp_tcsuite_ctr)
86 ((tmpval++))
87 echo $tmpval > .tmp_tcsuite_ctr
88fi
YongchaoWu9a84f512019-12-16 22:54:11 +010089
BjornMagnussonXA80a92002020-03-19 14:31:06 +010090# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
91# FTC1.sh -> ATC == FTC1
92ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
YongchaoWu9a84f512019-12-16 22:54:11 +010093
94# Create the logs dir if not already created in the current dir
95if [ ! -d "logs" ]; then
96 mkdir logs
97fi
YongchaoWu9a84f512019-12-16 22:54:11 +010098TESTLOGS=$PWD/logs
99
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100100# Create a http message log for this testcase
101HTTPLOG=$PWD"/.httplog_"$ATC".txt"
102echo "" > $HTTPLOG
103
104# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100105mkdir -p $TESTLOGS/$ATC
106
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100107# Clear the log dir for the test case
108rm $TESTLOGS/$ATC/*.log &> /dev/null
109rm $TESTLOGS/$ATC/*.txt &> /dev/null
110rm $TESTLOGS/$ATC/*.json &> /dev/null
111
112# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100113TCLOG=$TESTLOGS/$ATC/TC.log
114exec &> >(tee ${TCLOG})
115
116#Variables for counting tests as well as passed and failed tests
117RES_TEST=0
118RES_PASS=0
119RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100120RES_CONF_FAIL=0
121
122#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100123TCTEST_START=$SECONDS
124
125echo "-------------------------------------------------------------------------------------------------"
126echo "----------------------------------- Test case: "$ATC
127echo "----------------------------------- Started: "$(date)
128echo "-------------------------------------------------------------------------------------------------"
129echo "-- Description: "$TC_ONELINE_DESCR
130echo "-------------------------------------------------------------------------------------------------"
131echo "----------------------------------- Test case setup -----------------------------------"
132
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100133echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100134
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100135#Temp var to check for image variable name errors
136IMAGE_ERR=0
137#Create a file with image info for later printing as a table
138image_list_file=".image-list"
139echo -e " Container\tImage\ttag" > $image_list_file
140
141# Check if image env var is set and if so export the env var with image to use (used by docker compose files)
142# arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>
143__check_image_var() {
144 if [ $# -ne 5 ]; then
145 echo "Expected arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>"
146 ((IMAGE_ERR++))
147 return
148 fi
149 tmp=${1}"\t"
150 #Create var from the input var names
151 image="${!4}"
152 tag="${!5}"
153
154 if [ -z $image ]; then
155 echo -e $RED"\$"$4" not set in test_env"$ERED
156 ((IMAGE_ERR++))
157 echo ""
158 tmp=$tmp"<no-image>\t"
159 else
160 tmp=$tmp$image"\t"
161 fi
162 if [ -z $tag ]; then
163 echo -e $RED"\$"$5" not set in test_env"$ERED
164 ((IMAGE_ERR++))
165 echo ""
166 tmp=$tmp"<no-tag>\t"
167 else
168 tmp=$tmp$tag
169 fi
170 echo -e "$tmp" >> $image_list_file
171 #Export the env var
172 export "${3}"=$image":"$tag
173
174 #echo " Configured image for ${1} (script start arg=${2}): "$image":"$tag
175}
176
177# Check that image env setting are available
178echo ""
179if [ $# -lt 1 ] || [ $# -gt 2 ]; then
180 echo "Expected arg: local|remote|remote-remove [auto-clean]"
181 exit 1
182elif [ $1 == "local" ]; then
183
184 #Local agent image
185 __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
186
187 #Local Control Panel image
188 __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
189
190 #Local SNDC image
191 __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
192
193 #Local ric sim image
194 __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
195
196elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
197
198 #Remote agent image
199 __check_image_var " Policy Agent" $1 "POLICY_AGENT_IMAGE" "POLICY_AGENT_REMOTE_IMAGE" "POLICY_AGENT_REMOTE_IMAGE_TAG"
200
201 #Remote Control Panel image
202 __check_image_var " Control Panel" $1 "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE_TAG"
203
204 #Remote SDNC image
205 __check_image_var " SDNC A1 Controller" $1 "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE_TAG"
206
207 #Remote ric sim image
208 __check_image_var " RIC Simulator" $1 "RIC_SIM_IMAGE" "RIC_SIM_REMOTE_IMAGE" "RIC_SIM_REMOTE_IMAGE_TAG"
209
210else
211 echo "Expected arg: local|remote|remote-remove [auto-clean]"
212 exit 1
YongchaoWu9a84f512019-12-16 22:54:11 +0100213fi
214
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100215if [ $# -eq 2 ]; then
216 if [ $2 == "auto-clean" ]; then
217 echo "Stting automatic cleaning of container when test case ends"
218 AUTO_CLEAN="auto"
219 else
220 echo "Expected arg: local|remote|remote-remove [auto-clean]"
221 exit 1
YongchaoWu9a84f512019-12-16 22:54:11 +0100222 fi
223fi
224
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100225# These images are not built as part of this project official images, just check that env vars are set correctly
226__check_image_var " Message Router" $1 "MRSTUB_IMAGE" "MRSTUB_LOCAL_IMAGE" "MRSTUB_LOCAL_IMAGE_TAG"
227__check_image_var " Callback Receiver" $1 "CR_IMAGE" "CR_LOCAL_IMAGE" "CR_LOCAL_IMAGE_TAG"
228__check_image_var " Consul" $1 "CONSUL_IMAGE" "CONSUL_REMOTE_IMAGE" "CONSUL_REMOTE_IMAGE_TAG"
229__check_image_var " CBS" $1 "CBS_IMAGE" "CBS_REMOTE_IMAGE" "CBS_REMOTE_IMAGE_TAG"
230__check_image_var " SDNC DB" $1 "SDNC_DB_IMAGE" "SDNC_DB_REMOTE_IMAGE" "SDNC_DB_REMOTE_IMAGE_TAG"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200231__check_image_var " SDNC ONAP A1 Adapter" $1 "SDNC_ONAP_A1_ADAPTER_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE_TAG"
232__check_image_var " SDNC ONAP DB" $1 "SDNC_ONAP_DB_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE_TAG"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100233
234#Errors in image setting - exit
235if [ $IMAGE_ERR -ne 0 ]; then
236 exit 1
237fi
238
239#Print a tables of the image settings
240echo -e $BOLD"Images configured for start arg: "$1 $EBOLD
241column -t -s $'\t' $image_list_file
242
YongchaoWuf309b1b2020-01-22 13:24:48 +0100243echo ""
244
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100245
246#Set the SIM_GROUP var
247echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
248if [ -z "$SIM_GROUP" ]; then
249 SIM_GROUP=$PWD/../simulator-group
250 if [ ! -d $SIM_GROUP ]; then
251 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
252 echo -e $RED"Please set the SIM_GROUP manually in the test_env.sh"$ERED
253 exit 1
254 else
255 echo " SIM_GROUP auto set to: " $SIM_GROUP
256 fi
257elif [ $SIM_GROUP = *simulator_group ]; then
258 echo -e $RED"Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the repo, check common/test_env.sh"$ERED
259 exit 1
260else
261 echo " SIM_GROUP env var already set to: " $SIM_GROUP
maximesson28ee8a52020-03-17 09:32:03 +0100262fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100263
264echo ""
265
266#Temp var to check for image pull errors
267IMAGE_ERR=0
268
269#Function to check if image exist and stop+remove the container+pull new images as needed
270#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag>
271__check_and_pull_image() {
272
273 echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$4$EBOLD"
274 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
275 tmp_im=$(docker images --format $format_string ${4})
276
277 if [ $1 == "local" ]; then
278 if [ -z "$tmp_im" ]; then
279 echo -e " "$2" (local image): \033[1m"$4"\033[0m $RED does not exist in local registry, need to be built (or manually pulled)"$ERED
280 ((IMAGE_ERR++))
281 return 1
282 else
283 echo -e " "$2" (local image): \033[1m"$4"\033[0m "$GREEN"OK"$EGREEN
284 fi
285 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
286 if [ $1 == "remote-remove" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200287 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100288 tmp="$(docker ps -aq --filter name=${3})"
289 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200290 docker stop $tmp &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100291 if [ $? -ne 0 ]; then
292 ((IMAGE_ERR++))
293 echo ""
294 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200295 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100296 return 1
297 fi
298 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200299 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100300 tmp="$(docker ps -aq --filter name=${3})" &> /dev/null
301 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200302 docker rm $tmp &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100303 if [ $? -ne 0 ]; then
304 ((IMAGE_ERR++))
305 echo ""
306 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200307 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100308 return 1
309 fi
310 fi
311 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200312 echo -ne " Removing image - ${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100313 tmp="$(docker images -q ${4})" &> /dev/null
314 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200315 docker rmi $4 &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100316 if [ $? -ne 0 ]; then
317 ((IMAGE_ERR++))
318 echo ""
319 echo -e $RED" Image could not be removed - try manual removal of the image"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200320 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100321 return 1
322 fi
323 echo -e " Removing image - "$GREEN"removed"$EGREEN
324 else
325 echo -e " Removing image - "$GREEN"image not in repository"$EGREEN
326 fi
327 tmp_im=""
328 fi
329 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200330 echo -ne " Pulling image${SAMELINE}"
331 docker pull $4 &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100332 tmp_im=$(docker images ${4} | grep -v REPOSITORY)
333 if [ -z "$tmp_im" ]; then
334 echo ""
335 echo -e " Pulling image -$RED could not be pulled"$ERED
336 ((IMAGE_ERR++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200337 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100338 return 1
339 fi
340 echo -e " Pulling image -$GREEN Pulled $EGREEN"
341 else
342 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
343 fi
344 fi
345 return 0
346}
347
348
349echo -e $BOLD"Pulling configured images, if needed"$EBOLD
350
351app="Policy Agent"; __check_and_pull_image $1 "$app" $POLICY_AGENT_APP_NAME $POLICY_AGENT_IMAGE
352app="Non-RT RIC Control Panel"; __check_and_pull_image $1 "$app" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_IMAGE
353app="SDNC A1 Controller"; __check_and_pull_image $1 "$app" $SDNC_APP_NAME $SDNC_A1_CONTROLLER_IMAGE
354app="Near-RT RIC Simulator"; __check_and_pull_image $1 "$app" $RIC_SIM_PREFIX"_"$RIC_SIM_BASE $RIC_SIM_IMAGE
355
356app="Consul"; __check_and_pull_image $1 "$app" $CONSUL_APP_NAME $CONSUL_IMAGE
357app="CBS"; __check_and_pull_image $1 "$app" $CBS_APP_NAME $CBS_IMAGE
358app="SDNC DB"; __check_and_pull_image $1 "$app" $SDNC_APP_NAME $SDNC_DB_IMAGE
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200359app="SDNC ONAP A1 Adapter"; __check_and_pull_image $1 "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_A1_ADAPTER_IMAGE
360app="SDNC ONAP DB"; __check_and_pull_image $1 "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_DB_IMAGE
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100361
362# MR stub image not checked, will be built by this script - only local image
363# CR stub image not checked, will be built by this script - only local image
364
365
366#Errors in image setting - exit
367if [ $IMAGE_ERR -ne 0 ]; then
368 echo ""
369 echo "#################################################################################################"
370 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
371 echo "#################################################################################################"
372 echo ""
373 exit 1
374fi
375
376echo ""
377
378echo -e $BOLD"Building images needed for test"$EBOLD
379
380curdir=$PWD
381cd $curdir
382cd ../mrstub
383echo " Building mrstub image: mrstub:latest"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200384docker build -t mrstub . &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100385if [ $? -eq 0 ]; then
386 echo -e $GREEN" Build Ok"$EGREEN
387else
388 echo -e $RED" Build Failed"$ERED
389 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200390 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100391fi
392cd $curdir
393
394cd ../cr
395echo " Building Callback Receiver image: callback-receiver:latest"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200396docker build -t callback-receiver . &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100397if [ $? -eq 0 ]; then
398 echo -e $GREEN" Build Ok"$EGREEN
399else
400 echo -e $RED" Build Failed"$ERED
401 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200402 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100403fi
YongchaoWuf309b1b2020-01-22 13:24:48 +0100404cd $curdir
405
406echo ""
407
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100408# Create a table of the images used in the script
409echo -e $BOLD"Local docker registry images used in the this test script"$EBOLD
410
411docker_tmp_file=.docker-images-table
412format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}"
413echo -e " Application\tRepository\tTag\tCreated Since\tSize" > $docker_tmp_file
414echo -e " Policy Agent\t$(docker images --format $format_string $POLICY_AGENT_IMAGE)" >> $docker_tmp_file
415echo -e " Control Panel\t$(docker images --format $format_string $CONTROL_PANEL_IMAGE)" >> $docker_tmp_file
416echo -e " SDNC A1 Controller\t$(docker images --format $format_string $SDNC_A1_CONTROLLER_IMAGE)" >> $docker_tmp_file
417echo -e " RIC Simulator\t$(docker images --format $format_string $RIC_SIM_IMAGE)" >> $docker_tmp_file
418echo -e " Message Router\t$(docker images --format $format_string $MRSTUB_IMAGE)" >> $docker_tmp_file
419echo -e " Callback Receiver\t$(docker images --format $format_string $CR_IMAGE)" >> $docker_tmp_file
420echo -e " Consul\t$(docker images --format $format_string $CONSUL_IMAGE)" >> $docker_tmp_file
421echo -e " CBS\t$(docker images --format $format_string $CBS_IMAGE)" >> $docker_tmp_file
422echo -e " SDNC DB\t$(docker images --format $format_string $SDNC_DB_IMAGE)" >> $docker_tmp_file
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200423echo -e " SDNC ONAP A1 Adapter\t$(docker images --format $format_string $SDNC_ONAP_A1_ADAPTER_IMAGE)" >> $docker_tmp_file
424echo -e " SDNC ONAP DB\t$(docker images --format $format_string $SDNC_ONAP_DB_IMAGE)" >> $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100425
426column -t -s $'\t' $docker_tmp_file
427
YongchaoWuf309b1b2020-01-22 13:24:48 +0100428echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100429
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100430echo -e $BOLD"======================================================="$EBOLD
431echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
432echo -e $BOLD"======================================================="$EBOLD
433echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100434
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100435# Function to print the test result, shall be the last cmd in a test script
436# args: -
437# (Function for test scripts)
438print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +0100439
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100440 TCTEST_END=$SECONDS
441 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +0100442
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100443 echo "-------------------------------------------------------------------------------------------------"
444 echo "------------------------------------- Test case: "$ATC
445 echo "------------------------------------- Ended: "$(date)
446 echo "-------------------------------------------------------------------------------------------------"
447 echo "-- Description: "$TC_ONELINE_DESCR
448 echo "-- Execution time: " $duration " seconds"
449 echo "-------------------------------------------------------------------------------------------------"
450 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +0100451 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +0100452
YongchaoWu21f17bb2020-03-05 12:44:08 +0100453
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100454 total=$((RES_PASS+RES_FAIL))
455 if [ $RES_TEST -eq 0 ]; then
456 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
457 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
458 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
459 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
460 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
461 elif [ $total != $RES_TEST ]; then
462 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
463 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
464 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
465 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
466 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
467 elif [ $RES_CONF_FAIL -ne 0 ]; then
468 echo -e "\033[1mOne or more configure regest has failed. Check the script log....\033[0m"
469 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
470 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
471 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
472 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
473 elif [ $RES_PASS = $RES_TEST ]; then
474 echo -e "All tests \033[32m\033[1mPASS\033[0m"
475 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
476 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
477 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
478 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
479 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +0100480
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100481 # Update test suite counter
482 if [ -f .tmp_tcsuite_pass_ctr ]; then
483 tmpval=$(< .tmp_tcsuite_pass_ctr)
484 ((tmpval++))
485 echo $tmpval > .tmp_tcsuite_pass_ctr
486 fi
487 if [ -f .tmp_tcsuite_pass ]; then
488 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
489 fi
490 else
491 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
492 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
493 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
494 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
495 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
496 echo ""
497 # Update test suite counter
498 if [ -f .tmp_tcsuite_fail_ctr ]; then
499 tmpval=$(< .tmp_tcsuite_fail_ctr)
500 ((tmpval++))
501 echo $tmpval > .tmp_tcsuite_fail_ctr
502 fi
503 if [ -f .tmp_tcsuite_fail ]; then
504 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
505 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +0100506 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +0100507
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100508 echo "++++ Number of tests: "$RES_TEST
509 echo "++++ Number of passed tests: "$RES_PASS
510 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +0100511 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100512 echo "++++ Number of failed configs: "$RES_CONF_FAIL
513 echo "------------------------------------- Test case complete ---------------------------------"
514 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +0100515 echo ""
516}
517
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100518#####################################################################
519###### Functions for start, configuring, stoping, cleaning etc ######
520#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +0100521
522
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100523# Stop and remove all containers
524# args: -
525# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +0100526clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100527
528 echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
529
530 CONTAINTER_NAMES=("Policy Agent " $POLICY_AGENT_APP_NAME\
531 "Non-RT RIC Simulator(s)" $RIC_SIM_PREFIX\
532 "Message Router " $MR_APP_NAME\
533 "Callback Receiver " $CR_APP_NAME\
534 "Control Panel " $CONTROL_PANEL_APP_NAME\
535 "SDNC A1 Controller " $SDNC_APP_NAME\
536 "SDNC DB " $SDNC_DB_APP_NAME\
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200537 "SDNC ONAP A1 Adapter " $SDNC_ONAP_APP_NAME\
538 "SDNC DB " $SDNC_ONAP_DB_APP_NAME\
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100539 "CBS " $CBS_APP_NAME\
540 "Consul " $CONSUL_APP_NAME)
541
542 nw=0 # Calc max width of container name, to make a nice table
543 for (( i=1; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
544 if [ ${#CONTAINTER_NAMES[i]} -gt $nw ]; then
545 nw=${#CONTAINTER_NAMES[i]}
546 fi
547 done
548
549 for (( i=0; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
550 APP="${CONTAINTER_NAMES[i]}"
551 CONTR="${CONTAINTER_NAMES[i+1]}"
552 for((w=${#CONTR}; w<$nw; w=w+1)); do
553 CONTR="$CONTR "
554 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200555 echo -ne " $APP: $CONTR - ${GREEN}stopping${EGREEN}${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100556 docker stop $(docker ps -qa --filter name=${CONTR}) &> /dev/null
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200557 echo -ne " $APP: $CONTR - ${GREEN}stopped${EGREEN}${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100558 docker rm $(docker ps -qa --filter name=${CONTR}) &> /dev/null
559 echo -e " $APP: $CONTR - ${GREEN}stopped removed${EGREEN}"
560 done
561
YongchaoWu9a84f512019-12-16 22:54:11 +0100562 echo ""
563}
564
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100565# Function stop and remove all container in the end of the test script, if the arg 'auto-clean' is given at test script start
566# args: -
567# (Function for test scripts)
568auto_clean_containers() {
569 echo
570 if [ "$AUTO_CLEAN" == "auto" ]; then
571 echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
572 clean_containers
YongchaoWu9a84f512019-12-16 22:54:11 +0100573 fi
574}
575
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100576# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
577# args: <sleep-time-in-sec> [any-text-in-quoteds-to-printed]
578# (Function for test scripts)
579sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +0100580
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100581 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
582 if [ $# -lt 1 ]; then
583 ((RES_CONF_FAIL++))
584 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
585 exit 1
586 fi
587 #echo "---- Sleep for " $1 " seconds ---- "$2
588 start=$SECONDS
589 duration=$((SECONDS-start))
590 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200591 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100592 sleep 1
593 duration=$((SECONDS-start))
594 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200595 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100596 echo ""
597}
598
599# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
600# Not to be called from the test script itself.
601__print_err() {
602 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
603 if [ $# -gt 1 ]; then
604 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
605 fi
606}
607
608
609# Helper function to get a the port of a specific ric simulatpor
610# args: <ric-id>
611# (Not for test scripts)
612__find_sim_port() {
613 name=$1" " #Space appended to prevent matching 10 if 1 is desired....
614 cmdstr="docker ps --filter name=${name} --format \"{{.Names}} {{.Ports}}\" | grep '${name}' | sed s/0.0.0.0:// | cut -f 2 -d ' ' | cut -f 1 -d '-'"
615 res=$(eval $cmdstr)
616 if [[ "$res" =~ ^[0-9]+$ ]]; then
617 echo $res
618 else
619 echo "0"
620 fi
621}
622
623# Function to create the docker network for the test
624# Not to be called from the test script itself.
625__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200626 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100627 if [ $? -ne 0 ]; then
628 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
629 return 1
630 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200631 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100632 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
633 docker network create $DOCKER_SIM_NWNAME
634 if [ $? -ne 0 ]; then
635 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
636 return 1
637 fi
638 else
639 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
640 fi
641}
642
643# Check if container is started by calling url on localhost using a port, expects response code 2XX
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200644# args: <container-name> <port> <url> https|https
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100645# Not to be called from the test script itself.
646__check_container_start() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200647 paramError=0
648 if [ $# -ne 4 ]; then
649 paramError=1
650 elif [ $4 != "http" ] && [ $4 != "https" ]; then
651 paramError=1
652 fi
653 if [ $paramError -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100654 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200655 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100656 return 1
657 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200658 echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
YongchaoWu9a84f512019-12-16 22:54:11 +0100659 appname=$1
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100660 localport=$2
661 url=$3
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200662 app_started=0
YongchaoWu9a84f512019-12-16 22:54:11 +0100663 for i in {1..10}; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200664 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100665 echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200666 app_started=1
YongchaoWu9a84f512019-12-16 22:54:11 +0100667 break
668 else
669 sleep $i
670 fi
671 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200672 if [ $app_started -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100673 ((RES_CONF_FAIL++))
674 echo ""
675 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
676 return 1
677 fi
678 if [ $localport -eq 0 ]; then
679 while [ $localport -eq 0 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200680 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100681 localport=$(__find_sim_port $appname)
682 sleep 1
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200683 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100684 done
685 echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
686 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100687 fi
688
689 pa_st=false
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200690 echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100691 for i in {1..20}; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200692 if [ $4 == "https" ]; then
693 result="$(__do_curl "-k https://localhost:"${localport}${url})"
694 else
695 result="$(__do_curl $LOCALHOST${localport}${url})"
696 fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100697 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100698 if [ ${#result} -gt 15 ]; then
699 #If response is too long, truncate
700 result="...response text too long, omitted"
701 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200702 echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100703 echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN"
YongchaoWu9a84f512019-12-16 22:54:11 +0100704 pa_st=true
705 break
706 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100707 #echo " Retrying in $i seconds"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200708 echo -ne " Waiting for container ${appname} service status...retrying in $i seconds${SAMELINE}"
YongchaoWu9a84f512019-12-16 22:54:11 +0100709 sleep $i
710 fi
711 done
712
713 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100714 ((RES_CONF_FAIL++))
715 echo -e $RED" Container ${appname} did not respond to service status"$ERED
716 return 0
717 fi
718
719 echo ""
720 return 0
721}
722
723
724# Function to start a container and wait until it responds on the given port and url.
725#args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
726__start_container() {
727
728 variableArgCount=$(($#-2))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200729 if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100730 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200731 __print_err "need 6 or more args, <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> http|https [<app-name> <port-number> <alive-url> http|https ]*" $@
YongchaoWu9a84f512019-12-16 22:54:11 +0100732 exit 1
733 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100734
735 __create_docker_network
736
737 curdir=$PWD
738 cd $SIM_GROUP
739 cd $1
740
741 if [ "$2" == "NODOCKERARGS" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200742 docker-compose up -d &> .dockererr
743 if [ $? -ne 0 ]; then
744 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
745 cat .dockererr
746 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100747 else
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200748 docker-compose up -d $2 &> .dockererr
749 if [ $? -ne 0 ]; then
750 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
751 cat .dockererr
752 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100753 fi
754
755 shift; shift;
756 cntr=0
757 while [ $cntr -lt $variableArgCount ]; do
758 app=$1; shift;
759 port=$1; shift;
760 url=$1; shift;
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200761 httpx=$1; shift;
762 let cntr=cntr+4
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100763
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200764 __check_container_start "$app" "$port" "$url" $httpx
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100765 done
766
767 cd $curdir
768 echo ""
769 return 0
YongchaoWu9a84f512019-12-16 22:54:11 +0100770}
771
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100772####################
773### Consul functions
774####################
775
776# Function to load config from a file into consul for the Policy Agent
777# arg: <json-config-file>
778# (Function for test scripts)
779consul_config_app() {
780
781 echo -e $BOLD"Configuring Consul"$EBOLD
782
783 if [ $# -ne 1 ]; then
784 ((RES_CONF_FAIL++))
785 __print_err "need one arg, <json-config-file>" $@
786 exit 1
787 fi
788
789 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
790
791 curl -s $LOCALHOST${CONSUL_EXTERNAL_PORT}/v1/kv/${POLICY_AGENT_APP_NAME}?dc=dc1 -X PUT -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'X-Requested-With: XMLHttpRequest' --data-binary "@"$1 >/dev/null
792 if [ $? -ne 0 ]; then
793 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
794 ((RES_CONF_FAIL++))
795 return 1
796 fi
797 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
798
799 if [ $? -ne 0 ]; then
800 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
801 ((RES_CONF_FAIL++))
802 return 1
803 else
804 targetJson=$(< $1)
805 targetJson="{\"config\":"$targetJson"}"
806 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200807 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100808 if [ $res -ne 0 ]; then
809 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
810 ((RES_CONF_FAIL++))
811 return 1
812 else
813 echo -e $GREEN" Config loaded ok to consul"$EGREEN
814 fi
815 fi
816
817 echo ""
818
819}
820
821# Function to perpare the consul configuration according to the current simulator configuration
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200822# args: SDNC|SDNC_ONAP|NOSDNC <output-file>
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100823# (Function for test scripts)
824prepare_consul_config() {
825 echo -e $BOLD"Prepare Consul config"$EBOLD
826
827 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
828
829 if [ $# != 2 ]; then
830 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200831 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100832 exit 1
833 fi
834
835 if [ $1 == "SDNC" ]; then
836 echo -e " Config$BOLD including SDNC$EBOLD configuration"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200837 elif [ $1 == "SDNC_ONAP" ]; then
838 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100839 elif [ $1 == "NOSDNC" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200840 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100841 else
842 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200843 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100844 exit 1
845 fi
846
847 config_json="\n {"
848 if [ $1 == "SDNC" ]; then
849 config_json=$config_json"\n \"controller\": ["
850 config_json=$config_json"\n {"
851 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
852 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_APP_NAME:$SDNC_INTERNAL_PORT\","
853 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
854 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
855 config_json=$config_json"\n }"
856 config_json=$config_json"\n ],"
857 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200858 if [ $1 == "SDNC_ONAP" ]; then
859 config_json=$config_json"\n \"controller\": ["
860 config_json=$config_json"\n {"
861 config_json=$config_json"\n \"name\": \"$SDNC_ONAP_APP_NAME\","
862 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
863 config_json=$config_json"\n \"userName\": \"$SDNC_ONAP_USER\","
864 config_json=$config_json"\n \"password\": \"$SDNC_ONAP_PWD\""
865 config_json=$config_json"\n }"
866 config_json=$config_json"\n ],"
867 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100868
869
870 config_json=$config_json"\n \"streams_publishes\": {"
871 config_json=$config_json"\n \"dmaap_publisher\": {"
872 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
873 config_json=$config_json"\n \"dmaap_info\": {"
874 config_json=$config_json"\n \"topic_url\": \"http://$MR_APP_NAME:$MR_INTERNAL_PORT/events/A1-POLICY-AGENT-WRITE\""
875 config_json=$config_json"\n }"
876 config_json=$config_json"\n }"
877 config_json=$config_json"\n },"
878 config_json=$config_json"\n \"streams_subscribes\": {"
879 config_json=$config_json"\n \"dmaap_subscriber\": {"
880 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
881 config_json=$config_json"\n \"dmaap_info\": {"
882 config_json=$config_json"\n \"topic_url\": \"http://$MR_APP_NAME:$MR_INTERNAL_PORT/events/A1-POLICY-AGENT-READ/users/policy-agent\""
883 config_json=$config_json"\n }"
884 config_json=$config_json"\n }"
885 config_json=$config_json"\n },"
886
887 config_json=$config_json"\n \"ric\": ["
888
889 rics=$(docker ps | grep ricsim | awk '{print $NF}')
890
891 if [ $? -ne 0 ] || [ -z "$rics" ]; then
892 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
893 ((RES_CONF_FAIL++))
894 return 1
895 fi
896
897 cntr=0
898 for ric in $rics; do
899 if [ $cntr -gt 0 ]; then
900 config_json=$config_json"\n ,"
901 fi
902 config_json=$config_json"\n {"
903 config_json=$config_json"\n \"name\": \"$ric\","
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200904 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100905 if [ $1 == "SDNC" ]; then
906 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200907 elif [ $1 == "SDNC_ONAP" ]; then
908 config_json=$config_json"\n \"controller\": \"$SDNC_ONAP_APP_NAME\","
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100909 fi
910 config_json=$config_json"\n \"managedElementIds\": ["
911 config_json=$config_json"\n \"me1_$ric\","
912 config_json=$config_json"\n \"me2_$ric\""
913 config_json=$config_json"\n ]"
914 config_json=$config_json"\n }"
915 let cntr=cntr+1
916 done
917
918 config_json=$config_json"\n ]"
919 config_json=$config_json"\n}"
920
921
922 printf "$config_json">$2
923
924 echo ""
925}
926
927
928# Start Consul and CBS
929# args: -
930# (Function for test scripts)
931start_consul_cbs() {
932
933 echo -e $BOLD"Starting Consul and CBS"$EBOLD
934
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200935 __start_container consul_cbs NODOCKERARGS "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
936 "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100937}
938
939###########################
940### RIC Simulator functions
941###########################
942
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200943use_simulator_http() {
944 echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
945 export RIC_SIM_HTTPX="http"
946 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
947 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
948 export RIC_SIM_CERT_MOUNT_DIR="./fakedir" #Fake dir so that the sim container does not find any cert
949 echo ""
950}
951
952use_simulator_https() {
953 echo -e "Using secure $BOLD https $EBOLD towards the simulators"
954 export RIC_SIM_HTTPX="https"
955 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
956 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
957 export RIC_SIM_CERT_MOUNT_DIR="./cert"
958 echo ""
959}
960
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100961# Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
962# args: ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
963# (Function for test scripts)
964start_ric_simulators() {
965
966 echo -e $BOLD"Starting RIC Simulators"$EBOLD
967
968 if [ $# != 3 ]; then
969 ((RES_CONF_FAIL++))
970 __print_err "need three args, ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
971 exit 1
972 fi
973 echo " $2 simulators using basename: $1 on interface: $3"
974 #Set env var for simulator count and A1 interface vesion for the given group
975 if [ $1 == "ricsim_g1" ]; then
976 G1_COUNT=$2
977 G1_A1_VERSION=$3
978 elif [ $1 == "ricsim_g2" ]; then
979 G2_COUNT=$2
980 G2_A1_VERSION=$3
981 elif [ $1 == "ricsim_g3" ]; then
982 G3_COUNT=$2
983 G3_A1_VERSION=$3
984 else
985 ((RES_CONF_FAIL++))
986 __print_err "need three args, gricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
987 exit 1
988 fi
989
990 # Create .env file to compose project, all ric container will get this prefix
991 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
992
993 export G1_A1_VERSION
994 export G2_A1_VERSION
995 export G3_A1_VERSION
996
997 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
998 app_data=""
999 cntr=1
1000 while [ $cntr -le $2 ]; do
1001 app=$1"_"$cntr
1002 port=0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001003 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001004 let cntr=cntr+1
1005 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001006 __start_container ric "$docker_args" $app_data
1007
1008}
1009
1010###########################
1011### Control Panel functions
1012###########################
1013
1014# Start the Control Panel container
1015# args: -
1016# (Function for test scripts)
1017start_control_panel() {
1018
1019 echo -e $BOLD"Starting Control Panel"$EBOLD
1020
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001021 __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001022
1023}
1024
1025##################
1026### SDNC functions
1027##################
1028
1029# Start the SDNC A1 Controller
1030# args: -
1031# (Function for test scripts)
1032start_sdnc() {
1033
1034 echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1035
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001036 __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT "/apidoc/explorer" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001037
1038}
1039
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001040#######################
1041### SDNC ONAP functions
1042#######################
1043
1044# Start the SDNC ONAP A1 Adapter
1045# args: -
1046# (Function for test scripts)
1047start_sdnc_onap() {
1048
1049 echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1050
1051 __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT "/apidoc/explorer" "http"
1052
1053}
1054
1055# Configure the SDNC ONAP A1 Adapter
1056# args: -
1057# (Function for test scripts)
1058config_sdnc_onap() {
1059
1060 echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1061
1062 LOCALFILE=".sdnc_onap.prop"
1063 REMOTEFILE="/tmp/.sdnc_onap.prop"
1064
1065 docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1066 if [ $? -ne 0 ]; then
1067 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1068 exit 1
1069 fi
1070
1071 #Config of the prop file shall be inserted here
1072
1073 #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1074
1075 docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1076 if [ $? -ne 0 ]; then
1077 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1078 exit 1
1079 fi
1080
1081 docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1082 if [ $? -ne 0 ]; then
1083 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1084 exit 1
1085 fi
1086}
1087
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001088#####################
1089### MR stub functions
1090#####################
1091
1092# Start the Message Router stub interface in the simulator group
1093# args: -
1094# (Function for test scripts)
1095start_mr() {
1096
1097 echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001098 export MR_CERT_MOUNT_DIR="./cert"
1099 __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001100
1101}
1102
1103################
1104### CR functions
1105################
1106
1107# Start the Callback reciver in the simulator group
1108# args: -
1109# (Function for test scripts)
1110start_cr() {
1111
1112 echo -e $BOLD"Starting Callback Receiver"$EBOLD
1113
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001114 __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001115
1116}
1117
1118###########################
1119### Policy Agents functions
1120###########################
1121
1122# Start the policy agwent
1123# args: -
1124# (Function for test scripts)
1125start_policy_agent() {
1126
1127 echo -e $BOLD"Starting Policy Agent"$EBOLD
1128
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001129 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001130
1131}
1132
1133# All calls to the agent will be directed to the agent REST interface from now on
1134# args: -
1135# (Function for test scripts)
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001136use_agent_rest_http() {
1137 echo -e $BOLD"Using agent REST interface with http"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001138 export ADAPTER=$RESTBASE
1139 echo ""
1140}
1141
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001142# All calls to the agent will be directed to the agent REST interface from now on
1143# args: -
1144# (Function for test scripts)
1145use_agent_rest_https() {
1146 echo -e $BOLD"Using agent REST interface with https"$EBOLD
1147 export ADAPTER=$RESTBASE_SECURE
1148 echo ""
1149}
1150
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001151# All calls to the agent will be directed to the agent dmaap interface from now on
1152# args: -
1153# (Function for test scripts)
1154use_agent_dmaap() {
1155 echo -e $BOLD"Using agent DMAAP interface"$EBOLD
1156 export ADAPTER=$DMAAPBASE
1157 echo ""
1158
1159}
1160
1161# Turn on debug level tracing in the agent
1162# args: -
1163# (Function for test scripts)
1164set_agent_debug() {
1165 echo -e $BOLD"Setting agent debug"$EBOLD
1166 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1167 if [ $? -ne 0 ]; then
1168 __print_err "could not set debug mode" $@
1169 return 1
1170 fi
1171 return 0
1172 echo ""
1173}
1174
1175# Perform curl retries when making direct call to the agent for the specified http response codes
1176# Speace separated list of http response codes
1177# args: [<response-code>]*
1178use_agent_retries() {
1179 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1180 AGENT_RETRY_CODES=$@
1181 echo ""
1182}
1183
1184#################
1185### Log functions
1186#################
1187
1188# Check the agent logs for WARNINGs and ERRORs
1189# args: -
1190# (Function for test scripts)
1191
YongchaoWu9a84f512019-12-16 22:54:11 +01001192check_policy_agent_logs() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001193 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
YongchaoWu9a84f512019-12-16 22:54:11 +01001194}
1195
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001196check_control_panel_logs() {
1197 __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
YongchaoWu9a84f512019-12-16 22:54:11 +01001198}
1199
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001200__check_container_logs() {
1201 dispname=$1
1202 appname=$2
1203 logpath=$3
1204 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1205
1206 #tmp=$(docker ps | grep $appname)
1207 tmp=$(docker ps -q --filter name=$appname) #get the container id
1208 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
1209 echo $dispname" is not running, no check made"
1210 return
1211 fi
1212 foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1213 if [ $? -ne 0 ];then
1214 echo " Problem to search $appname log $logpath"
1215 else
1216 if [ $foundentries -eq 0 ]; then
1217 echo " No WARN entries found in $appname log $logpath"
1218 else
1219 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1220 fi
1221 fi
1222 foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1223 if [ $? -ne 0 ];then
1224 echo " Problem to search $appname log $logpath"
1225 else
1226 if [ $foundentries -eq 0 ]; then
1227 echo " No ERR entries found in $appname log $logpath"
1228 else
1229 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1230 fi
1231 fi
1232 echo ""
1233}
1234
1235# Store all container logs and other logs in the log dir for the script
1236# Logs are stored with a prefix in case logs should be stored several times during a test
1237# args: <logfile-prefix>
1238# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01001239store_logs() {
1240 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001241 ((RES_CONF_FAIL++))
1242 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01001243 exit 1
1244 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001245 echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1
YongchaoWu9a84f512019-12-16 22:54:11 +01001246
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001247 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1248 docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1249 docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1250 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1251 docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1252 docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1253 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1254
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001255 docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1256
1257 docker exec -it $SDNC_ONAP_APP_NAME cat $SDNC_ONAP_KARAF_LOG > $TESTLOGS/$ATC/$1_SDNC_ONAP_karaf.log 2>&1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001258
1259 rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1260 for ric in $rics; do
1261 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1262 done
1263 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1264 echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1265 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01001266}
1267
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001268###############
1269## Generic curl
1270###############
1271# Generic curl function, assumed all 200-codes are ok
1272# args: <url>
1273# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
1274# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01001275__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001276 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1277 curlString="curl -skw %{http_code} $1"
1278 echo " CMD: $curlString" >> $HTTPLOG
1279 res=$($curlString)
1280 echo " RESP: $res" >> $HTTPLOG
YongchaoWu9a84f512019-12-16 22:54:11 +01001281 http_code="${res:${#res}-3}"
1282 if [ ${#res} -eq 3 ]; then
1283 echo "<no-response-from-server>"
1284 return 1
1285 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001286 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01001287 echo "<not found, resp:${http_code}>"
1288 return 1
1289 fi
1290 if [ $# -eq 2 ]; then
1291 echo "${res:0:${#res}-3}" | xargs
1292 else
1293 echo "${res:0:${#res}-3}"
1294 fi
1295
1296 return 0
1297 fi
1298}
1299
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001300#######################################
1301### Basic helper function for test cases
1302#######################################
1303
1304# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1305# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
1306# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1307# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
1308# before setting pass or fail depending on the result of comparing variable and target using the operator.
1309# If the <variable-name> has the 'json:' prefix, the the variable will be used as url and the <target-value> will be compared towards the length of the json array in the response.
1310# Not to be called from test script.
1311
1312__var_test() {
1313 checkjsonarraycount=0
1314
1315 if [ $# -eq 6 ]; then
1316 if [[ $3 == "json:"* ]]; then
1317 checkjsonarraycount=1
1318 fi
1319
1320 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
1321 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"
1322 ((RES_TEST++))
1323 start=$SECONDS
1324 ctr=0
1325 for (( ; ; )); do
1326 if [ $checkjsonarraycount -eq 0 ]; then
1327 result="$(__do_curl $2$3)"
1328 retcode=$?
1329 result=${result//[[:blank:]]/} #Strip blanks
1330 else
1331 path=${3:5}
1332 result="$(__do_curl $2$path)"
1333 retcode=$?
1334 echo "$result" > .tmp.curl.json
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001335 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001336 fi
1337 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001338 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001339 let ctr=ctr+1
1340 if [ $retcode -ne 0 ]; then
1341 if [ $duration -gt $6 ]; then
1342 ((RES_FAIL++))
1343 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1344 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1345 return
1346 fi
1347 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1348 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001349 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001350 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1351 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
1352 return
1353 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1354 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001355 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001356 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1357 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1358 return
1359 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1360 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001361 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001362 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1363 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1364 return
1365 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1366 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001367 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001368 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1369 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1370 return
1371 else
1372 if [ $duration -gt $6 ]; then
1373 ((RES_FAIL++))
1374 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1375 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1376 return
1377 fi
1378 fi
1379 sleep 1
1380 done
1381 elif [ $# -eq 5 ]; then
1382 if [[ $3 == "json:"* ]]; then
1383 checkjsonarraycount=1
1384 fi
1385
1386 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
1387 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1388 ((RES_TEST++))
1389 if [ $checkjsonarraycount -eq 0 ]; then
1390 result="$(__do_curl $2$3)"
1391 retcode=$?
1392 result=${result//[[:blank:]]/} #Strip blanks
1393 else
1394 path=${3:5}
1395 result="$(__do_curl $2$path)"
1396 retcode=$?
1397 echo "$result" > .tmp.curl.json
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001398 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001399 fi
1400 if [ $retcode -ne 0 ]; then
1401 ((RES_FAIL++))
1402 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1403 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
1404 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1405 ((RES_PASS++))
1406 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1407 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met"
1408 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1409 ((RES_PASS++))
1410 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1411 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1412 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1413 ((RES_PASS++))
1414 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1415 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1416 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1417 ((RES_PASS++))
1418 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1419 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1420 else
1421 ((RES_FAIL++))
1422 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
1423 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1424 fi
1425 else
1426 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1427 echo "Got:" $@
1428 exit 1
1429 fi
1430}
1431
1432
1433### Generic test cases for varaible checking
1434
1435# Tests if a variable value in the CR is equal to a target value and and optional timeout.
1436# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1437# equal to the target or not.
1438# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1439# before setting pass or fail depending on if the variable value becomes equal to the target
1440# value or not.
1441# (Function for test scripts)
1442cr_equal() {
1443 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1444 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1445 else
1446 ((RES_CONF_FAIL++))
1447 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1448 fi
1449}
1450
1451# Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1452# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1453# equal to the target or not.
1454# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1455# before setting pass or fail depending on if the variable value becomes equal to the target
1456# value or not.
1457# (Function for test scripts)
1458mr_equal() {
1459 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1460 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1461 else
1462 ((RES_CONF_FAIL++))
1463 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1464 fi
1465}
1466
1467# Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1468# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1469# greater than the target or not.
1470# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1471# before setting pass or fail depending on if the variable value becomes greater than the target
1472# value or not.
1473# (Function for test scripts)
1474mr_greater() {
1475 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001476 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001477 else
1478 ((RES_CONF_FAIL++))
1479 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1480 fi
1481}
1482
1483# Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1484mr_read() {
1485 echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1486}
1487
1488# Print a variable value from the MR stub.
1489# arg: <variable-name>
1490# (Function for test scripts)
1491mr_print() {
1492 if [ $# != 1 ]; then
1493 ((RES_CONF_FAIL++))
1494 __print_err "need one arg, <mr-param>" $@
1495 exit 1
1496 fi
1497 echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD
1498}
1499
1500