blob: 4818bc0adbd2fc5778aa448be7c3afbda24fd2c6 [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
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020046tmp=$(which docker-compose)
47if [ $? -ne 0 ] || [ -z tmp ]; then
48 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
49 exit 1
50fi
51
BjornMagnussonXA80a92002020-03-19 14:31:06 +010052# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020053echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +010054
55# source test environment variables
56. ../common/test_env.sh
57
58echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
59
60#Vars for A1 interface version and container count
61G1_A1_VERSION=""
62G2_A1_VERSION=""
63G3_A1_VERSION=""
64G1_COUNT=0
65G2_COUNT=0
66G3_COUNT=0
67
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020068# Vars to switch between http and https. Extra curl flag needed for https
BjornMagnussonXA72667f12020-04-24 09:20:18 +020069export RIC_SIM_HTTPX="http"
70export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
71export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
ecaiyanlinux99a769b2020-05-15 13:58:02 +020072export RIC_SIM_CERT_MOUNT_DIR="./cert"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020073
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020074export MR_HTTPX="http"
75export MR_PORT=$MR_INTERNAL_PORT
76export MR_LOCAL_PORT=$MR_EXTERNAL_PORT #When agent is running outside the docker net
77
78export SDNC_HTTPX="http"
79export SDNC_PORT=$SDNC_INTERNAL_PORT
80export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT #When agent is running outside the docker net
81
BjornMagnussonXA80a92002020-03-19 14:31:06 +010082#Localhost constant
83LOCALHOST="http://localhost:"
84
85# Make curl retries for http response codes set in this env var, space separated list of codes
86AGENT_RETRY_CODES=""
87
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020088# Var to contol if the agent runs in a container (normal = 0) or as application on the local machine ( = 1)
89AGENT_STAND_ALONE=0
90
BjornMagnussonXA80a92002020-03-19 14:31:06 +010091# Var to hold 'auto' in case containers shall be stopped when test case ends
92AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +010093
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020094# Var to hold the app names to use local image for when running 'remote' or 'remote-remove'
95USE_LOCAL_IMAGES=""
96
YongchaoWu9a84f512019-12-16 22:54:11 +010097# Set a description string for the test case
98if [ -z "$TC_ONELINE_DESCR" ]; then
99 TC_ONELINE_DESCR="<no-description>"
100 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
101fi
102
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100103# Counter for test suites
104if [ -f .tmp_tcsuite_ctr ]; then
105 tmpval=$(< .tmp_tcsuite_ctr)
106 ((tmpval++))
107 echo $tmpval > .tmp_tcsuite_ctr
108fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100109
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100110# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
111# FTC1.sh -> ATC == FTC1
112ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
YongchaoWu9a84f512019-12-16 22:54:11 +0100113
114# Create the logs dir if not already created in the current dir
115if [ ! -d "logs" ]; then
116 mkdir logs
117fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100118TESTLOGS=$PWD/logs
119
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100120# Create a http message log for this testcase
121HTTPLOG=$PWD"/.httplog_"$ATC".txt"
122echo "" > $HTTPLOG
123
124# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100125mkdir -p $TESTLOGS/$ATC
126
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100127# Clear the log dir for the test case
128rm $TESTLOGS/$ATC/*.log &> /dev/null
129rm $TESTLOGS/$ATC/*.txt &> /dev/null
130rm $TESTLOGS/$ATC/*.json &> /dev/null
131
132# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100133TCLOG=$TESTLOGS/$ATC/TC.log
134exec &> >(tee ${TCLOG})
135
136#Variables for counting tests as well as passed and failed tests
137RES_TEST=0
138RES_PASS=0
139RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100140RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200141RES_DEVIATION=0
142
143#File to keep deviation messages
144DEVIATION_FILE=".tmp_deviations"
145rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100146
147#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100148TCTEST_START=$SECONDS
149
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200150#File to save timer measurement results
151TIMER_MEASUREMENTS=".timer_measurement.txt"
152echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
153
154
YongchaoWu9a84f512019-12-16 22:54:11 +0100155echo "-------------------------------------------------------------------------------------------------"
156echo "----------------------------------- Test case: "$ATC
157echo "----------------------------------- Started: "$(date)
158echo "-------------------------------------------------------------------------------------------------"
159echo "-- Description: "$TC_ONELINE_DESCR
160echo "-------------------------------------------------------------------------------------------------"
161echo "----------------------------------- Test case setup -----------------------------------"
162
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200163START_ARG=$1
164paramerror=0
165if [ $# -lt 1 ]; then
166 paramerror=1
167fi
168if [ $paramerror -eq 0 ]; then
169 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ] && [ "$1" != "local" ]; then
170 paramerror=1
171 else
172 shift;
173 fi
174fi
175if [ $paramerror -eq 0 ]; then
176 if [ "$1" == "auto-clean" ]; then
177 AUTO_CLEAN="auto"
178 shift;
179 fi
180fi
181if [ $paramerror -eq 0 ]; then
182 if [ "$1" == "-use-local-image" ]; then
183 USE_LOCAL_IMAGES=${@:2}
184 while [ $# -gt 0 ]; do
185 shift;
186 done
187 fi
188fi
189
190if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
191 paramerror=1
192fi
193
194if [ $paramerror -eq 1 ]; then
195 echo -e $RED"Expected arg: local|remote|remote-remove [auto-clean] [-use-local-image <app-nam> [<app-name>]]"$ERED
196 exit 1
197fi
198
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100199echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100200
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100201#Temp var to check for image variable name errors
202IMAGE_ERR=0
203#Create a file with image info for later printing as a table
204image_list_file=".image-list"
205echo -e " Container\tImage\ttag" > $image_list_file
206
207# Check if image env var is set and if so export the env var with image to use (used by docker compose files)
208# arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>
209__check_image_var() {
210 if [ $# -ne 5 ]; then
211 echo "Expected arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>"
212 ((IMAGE_ERR++))
213 return
214 fi
215 tmp=${1}"\t"
216 #Create var from the input var names
217 image="${!4}"
218 tag="${!5}"
219
220 if [ -z $image ]; then
221 echo -e $RED"\$"$4" not set in test_env"$ERED
222 ((IMAGE_ERR++))
223 echo ""
224 tmp=$tmp"<no-image>\t"
225 else
226 tmp=$tmp$image"\t"
227 fi
228 if [ -z $tag ]; then
229 echo -e $RED"\$"$5" not set in test_env"$ERED
230 ((IMAGE_ERR++))
231 echo ""
232 tmp=$tmp"<no-tag>\t"
233 else
234 tmp=$tmp$tag
235 fi
236 echo -e "$tmp" >> $image_list_file
237 #Export the env var
238 export "${3}"=$image":"$tag
239
240 #echo " Configured image for ${1} (script start arg=${2}): "$image":"$tag
241}
242
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200243
244#Check if app local image shall override remote image
245check_image_local_override() {
246 for im in $USE_LOCAL_IMAGES; do
247 if [ "$1" == "$im" ]; then
248 return 1
249 fi
250 done
251 return 0
252}
253
254#Check if app uses image excluded from this test run
255check_excluded_image() {
256 for im in $EXCLUDED_IMAGES; do
257 if [ "$1" == "$im" ]; then
258 return 1
259 fi
260 done
261 return 0
262}
263
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100264# Check that image env setting are available
265echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200266
267if [ $START_ARG == "local" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100268
269 #Local agent image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200270 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100271
272 #Local Control Panel image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200273 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100274
275 #Local SNDC image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200276 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100277
278 #Local ric sim image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200279 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100280
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200281elif [ $START_ARG == "remote" ] || [ $START_ARG == "remote-remove" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100282
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200283 check_image_local_override 'PA'
284 if [ $? -eq 0 ]; then
285 #Remote agent image
286 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_REMOTE_IMAGE" "POLICY_AGENT_REMOTE_IMAGE_TAG"
287 else
288 #Local agent image
289 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
290 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100291
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200292 check_image_local_override 'CP'
293 if [ $? -eq 0 ]; then
294 #Remote Control Panel image
295 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE_TAG"
296 else
297 #Local Control Panel image
298 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
299 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100300
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200301 check_image_local_override 'SDNC'
302 if [ $? -eq 0 ]; then
303 #Remote SDNC image
304 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE_TAG"
305 else
306 #Local SNDC image
307 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
308 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100309
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200310 check_image_local_override 'RICSIM'
311 if [ $? -eq 0 ]; then
312 #Remote ric sim image
313 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_REMOTE_IMAGE" "RIC_SIM_REMOTE_IMAGE_TAG"
314 else
315 #Local ric sim image
316 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
317 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100318
319else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200320 #Should never get here....
321 echo "Unknow args: "$@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100322 exit 1
YongchaoWu9a84f512019-12-16 22:54:11 +0100323fi
324
YongchaoWu9a84f512019-12-16 22:54:11 +0100325
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100326# These images are not built as part of this project official images, just check that env vars are set correctly
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200327__check_image_var " Message Router" $START_ARG "MRSTUB_IMAGE" "MRSTUB_LOCAL_IMAGE" "MRSTUB_LOCAL_IMAGE_TAG"
328__check_image_var " Callback Receiver" $START_ARG "CR_IMAGE" "CR_LOCAL_IMAGE" "CR_LOCAL_IMAGE_TAG"
329__check_image_var " Consul" $START_ARG "CONSUL_IMAGE" "CONSUL_REMOTE_IMAGE" "CONSUL_REMOTE_IMAGE_TAG"
330__check_image_var " CBS" $START_ARG "CBS_IMAGE" "CBS_REMOTE_IMAGE" "CBS_REMOTE_IMAGE_TAG"
331__check_image_var " SDNC DB" $START_ARG "SDNC_DB_IMAGE" "SDNC_DB_REMOTE_IMAGE" "SDNC_DB_REMOTE_IMAGE_TAG"
332check_excluded_image 'SDNC_ONAP'
333if [ $? -eq 0 ]; then
334 __check_image_var " SDNC ONAP A1 Adapter" $START_ARG "SDNC_ONAP_A1_ADAPTER_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE" "SDNC_ONAP_A1_ADAPTER_REMOTE_IMAGE_TAG"
335 __check_image_var " SDNC ONAP DB" $START_ARG "SDNC_ONAP_DB_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE_TAG"
336fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100337
338#Errors in image setting - exit
339if [ $IMAGE_ERR -ne 0 ]; then
340 exit 1
341fi
342
343#Print a tables of the image settings
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200344echo -e $BOLD"Images configured for start arg: "$START $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100345column -t -s $'\t' $image_list_file
346
YongchaoWuf309b1b2020-01-22 13:24:48 +0100347echo ""
348
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100349
350#Set the SIM_GROUP var
351echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
352if [ -z "$SIM_GROUP" ]; then
353 SIM_GROUP=$PWD/../simulator-group
354 if [ ! -d $SIM_GROUP ]; then
355 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
356 echo -e $RED"Please set the SIM_GROUP manually in the test_env.sh"$ERED
357 exit 1
358 else
359 echo " SIM_GROUP auto set to: " $SIM_GROUP
360 fi
361elif [ $SIM_GROUP = *simulator_group ]; then
362 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
363 exit 1
364else
365 echo " SIM_GROUP env var already set to: " $SIM_GROUP
maximesson28ee8a52020-03-17 09:32:03 +0100366fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100367
368echo ""
369
370#Temp var to check for image pull errors
371IMAGE_ERR=0
372
373#Function to check if image exist and stop+remove the container+pull new images as needed
374#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag>
375__check_and_pull_image() {
376
377 echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$4$EBOLD"
378 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
379 tmp_im=$(docker images --format $format_string ${4})
380
381 if [ $1 == "local" ]; then
382 if [ -z "$tmp_im" ]; then
383 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
384 ((IMAGE_ERR++))
385 return 1
386 else
387 echo -e " "$2" (local image): \033[1m"$4"\033[0m "$GREEN"OK"$EGREEN
388 fi
389 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
390 if [ $1 == "remote-remove" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200391 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100392 tmp="$(docker ps -aq --filter name=${3})"
393 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200394 docker stop $tmp &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100395 if [ $? -ne 0 ]; then
396 ((IMAGE_ERR++))
397 echo ""
398 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200399 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100400 return 1
401 fi
402 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200403 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100404 tmp="$(docker ps -aq --filter name=${3})" &> /dev/null
405 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200406 docker rm $tmp &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100407 if [ $? -ne 0 ]; then
408 ((IMAGE_ERR++))
409 echo ""
410 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200411 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100412 return 1
413 fi
414 fi
415 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200416 echo -ne " Removing image - ${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100417 tmp="$(docker images -q ${4})" &> /dev/null
418 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200419 docker rmi $4 &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100420 if [ $? -ne 0 ]; then
421 ((IMAGE_ERR++))
422 echo ""
423 echo -e $RED" Image could not be removed - try manual removal of the image"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200424 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100425 return 1
426 fi
427 echo -e " Removing image - "$GREEN"removed"$EGREEN
428 else
429 echo -e " Removing image - "$GREEN"image not in repository"$EGREEN
430 fi
431 tmp_im=""
432 fi
433 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200434 echo -ne " Pulling image${SAMELINE}"
435 docker pull $4 &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100436 tmp_im=$(docker images ${4} | grep -v REPOSITORY)
437 if [ -z "$tmp_im" ]; then
438 echo ""
439 echo -e " Pulling image -$RED could not be pulled"$ERED
440 ((IMAGE_ERR++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200441 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100442 return 1
443 fi
444 echo -e " Pulling image -$GREEN Pulled $EGREEN"
445 else
446 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
447 fi
448 fi
449 return 0
450}
451
452
453echo -e $BOLD"Pulling configured images, if needed"$EBOLD
454
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200455START_ARG_MOD=$START_ARG
456check_image_local_override 'PA'
457if [ $? -eq 1 ]; then
458 START_ARG_MOD="local"
459fi
460app="Policy Agent"; __check_and_pull_image $START_ARG_MOD "$app" $POLICY_AGENT_APP_NAME $POLICY_AGENT_IMAGE
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100461
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200462START_ARG_MOD=$START_ARG
463check_image_local_override 'CP'
464if [ $? -eq 1 ]; then
465 START_ARG_MOD="local"
466fi
467app="Non-RT RIC Control Panel"; __check_and_pull_image $START_ARG_MOD "$app" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_IMAGE
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200468
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200469START_ARG_MOD=$START_ARG
470check_image_local_override 'RICSIM'
471if [ $? -eq 1 ]; then
472 START_ARG_MOD="local"
473fi
474app="Near-RT RIC Simulator"; __check_and_pull_image $START_ARG_MOD "$app" $RIC_SIM_PREFIX"_"$RIC_SIM_BASE $RIC_SIM_IMAGE
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100475
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200476app="Consul"; __check_and_pull_image $START_ARG "$app" $CONSUL_APP_NAME $CONSUL_IMAGE
477app="CBS"; __check_and_pull_image $START_ARG "$app" $CBS_APP_NAME $CBS_IMAGE
478check_excluded_image 'SDNC'
479if [ $? -eq 0 ]; then
480 START_ARG_MOD=$START_ARG
481 check_image_local_override 'SDNC'
482 if [ $? -eq 1 ]; then
483 START_ARG_MOD="local"
484 fi
485 app="SDNC A1 Controller"; __check_and_pull_image $START_ARG_MOD "$app" $SDNC_APP_NAME $SDNC_A1_CONTROLLER_IMAGE
486 app="SDNC DB"; __check_and_pull_image $START_ARG "$app" $SDNC_APP_NAME $SDNC_DB_IMAGE
487else
488 echo -e $YELLOW" Excluding SDNC image and related DB image from image check/pull"$EYELLOW
489fi
490check_excluded_image 'SDNC_ONAP'
491if [ $? -eq 0 ]; then
492 app="SDNC ONAP A1 Adapter"; __check_and_pull_image $START_ARG "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_A1_ADAPTER_IMAGE
493 app="SDNC ONAP DB"; __check_and_pull_image $START_ARG "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_DB_IMAGE
494else
495 echo -e $YELLOW" Excluding ONAP SDNC image and related DB image from image check/pull"$EYELLOW
496fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100497# MR stub image not checked, will be built by this script - only local image
498# CR stub image not checked, will be built by this script - only local image
499
500
501#Errors in image setting - exit
502if [ $IMAGE_ERR -ne 0 ]; then
503 echo ""
504 echo "#################################################################################################"
505 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200506 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100507 echo "#################################################################################################"
508 echo ""
509 exit 1
510fi
511
512echo ""
513
514echo -e $BOLD"Building images needed for test"$EBOLD
515
516curdir=$PWD
517cd $curdir
518cd ../mrstub
519echo " Building mrstub image: mrstub:latest"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200520docker build -t mrstub . &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100521if [ $? -eq 0 ]; then
522 echo -e $GREEN" Build Ok"$EGREEN
523else
524 echo -e $RED" Build Failed"$ERED
525 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200526 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100527fi
528cd $curdir
529
530cd ../cr
531echo " Building Callback Receiver image: callback-receiver:latest"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200532docker build -t callback-receiver . &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100533if [ $? -eq 0 ]; then
534 echo -e $GREEN" Build Ok"$EGREEN
535else
536 echo -e $RED" Build Failed"$ERED
537 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200538 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100539fi
YongchaoWuf309b1b2020-01-22 13:24:48 +0100540cd $curdir
541
542echo ""
543
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100544# Create a table of the images used in the script
545echo -e $BOLD"Local docker registry images used in the this test script"$EBOLD
546
547docker_tmp_file=.docker-images-table
548format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}"
549echo -e " Application\tRepository\tTag\tCreated Since\tSize" > $docker_tmp_file
550echo -e " Policy Agent\t$(docker images --format $format_string $POLICY_AGENT_IMAGE)" >> $docker_tmp_file
551echo -e " Control Panel\t$(docker images --format $format_string $CONTROL_PANEL_IMAGE)" >> $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100552echo -e " RIC Simulator\t$(docker images --format $format_string $RIC_SIM_IMAGE)" >> $docker_tmp_file
553echo -e " Message Router\t$(docker images --format $format_string $MRSTUB_IMAGE)" >> $docker_tmp_file
554echo -e " Callback Receiver\t$(docker images --format $format_string $CR_IMAGE)" >> $docker_tmp_file
555echo -e " Consul\t$(docker images --format $format_string $CONSUL_IMAGE)" >> $docker_tmp_file
556echo -e " CBS\t$(docker images --format $format_string $CBS_IMAGE)" >> $docker_tmp_file
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200557check_excluded_image 'SDNC'
558if [ $? -eq 0 ]; then
559 echo -e " SDNC A1 Controller\t$(docker images --format $format_string $SDNC_A1_CONTROLLER_IMAGE)" >> $docker_tmp_file
560 echo -e " SDNC DB\t$(docker images --format $format_string $SDNC_DB_IMAGE)" >> $docker_tmp_file
561fi
562check_excluded_image 'SDNC_ONAP'
563if [ $? -eq 0 ]; then
564 echo -e " SDNC ONAP A1 Adapter\t$(docker images --format $format_string $SDNC_ONAP_A1_ADAPTER_IMAGE)" >> $docker_tmp_file
565 echo -e " SDNC ONAP DB\t$(docker images --format $format_string $SDNC_ONAP_DB_IMAGE)" >> $docker_tmp_file
566fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100567
568column -t -s $'\t' $docker_tmp_file
569
YongchaoWuf309b1b2020-01-22 13:24:48 +0100570echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100571
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100572echo -e $BOLD"======================================================="$EBOLD
573echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
574echo -e $BOLD"======================================================="$EBOLD
575echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100576
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100577# Function to print the test result, shall be the last cmd in a test script
578# args: -
579# (Function for test scripts)
580print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +0100581
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100582 TCTEST_END=$SECONDS
583 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +0100584
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100585 echo "-------------------------------------------------------------------------------------------------"
586 echo "------------------------------------- Test case: "$ATC
587 echo "------------------------------------- Ended: "$(date)
588 echo "-------------------------------------------------------------------------------------------------"
589 echo "-- Description: "$TC_ONELINE_DESCR
590 echo "-- Execution time: " $duration " seconds"
591 echo "-------------------------------------------------------------------------------------------------"
592 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +0100593 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +0100594
YongchaoWu21f17bb2020-03-05 12:44:08 +0100595
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200596 if [ $RES_DEVIATION -gt 0 ]; then
597 echo "Test case deviations"
598 echo "===================================="
599 cat $DEVIATION_FILE
600 fi
601 echo ""
602 echo "Timer measurement in the test script"
603 echo "===================================="
604 column -t -s $'\t' $TIMER_MEASUREMENTS
605 echo ""
606
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100607 total=$((RES_PASS+RES_FAIL))
608 if [ $RES_TEST -eq 0 ]; then
609 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
610 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
611 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
612 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
613 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
614 elif [ $total != $RES_TEST ]; then
615 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
616 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
617 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
618 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
619 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
620 elif [ $RES_CONF_FAIL -ne 0 ]; then
621 echo -e "\033[1mOne or more configure regest has failed. Check the script log....\033[0m"
622 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
623 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
624 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
625 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
626 elif [ $RES_PASS = $RES_TEST ]; then
627 echo -e "All tests \033[32m\033[1mPASS\033[0m"
628 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
629 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
630 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
631 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
632 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +0100633
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100634 # Update test suite counter
635 if [ -f .tmp_tcsuite_pass_ctr ]; then
636 tmpval=$(< .tmp_tcsuite_pass_ctr)
637 ((tmpval++))
638 echo $tmpval > .tmp_tcsuite_pass_ctr
639 fi
640 if [ -f .tmp_tcsuite_pass ]; then
641 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
642 fi
643 else
644 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
645 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
646 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
647 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
648 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
649 echo ""
650 # Update test suite counter
651 if [ -f .tmp_tcsuite_fail_ctr ]; then
652 tmpval=$(< .tmp_tcsuite_fail_ctr)
653 ((tmpval++))
654 echo $tmpval > .tmp_tcsuite_fail_ctr
655 fi
656 if [ -f .tmp_tcsuite_fail ]; then
657 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
658 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +0100659 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +0100660
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100661 echo "++++ Number of tests: "$RES_TEST
662 echo "++++ Number of passed tests: "$RES_PASS
663 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +0100664 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100665 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200666 echo ""
667 echo "++++ Number of test case deviations: "$RES_DEVIATION
668 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100669 echo "------------------------------------- Test case complete ---------------------------------"
670 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +0100671 echo ""
672}
673
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100674#####################################################################
675###### Functions for start, configuring, stoping, cleaning etc ######
676#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +0100677
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200678# Start timer for time measurement
679# args - (any args will be printed though)
680start_timer() {
681 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
682 TC_TIMER=$SECONDS
683 echo " Timer started"
684}
685
686# Print the value of the time (in seconds)
687# args - <timer message to print> - timer value and message will be printed both on screen
688# and in the timer measurement report
689print_timer() {
690 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
691 if [ $# -lt 1 ]; then
692 ((RES_CONF_FAIL++))
693 __print_err "need 1 or more args, <timer message to print>" $@
694 exit 1
695 fi
696 duration=$(($SECONDS-$TC_TIMER))
697 if [ $duration -eq 0 ]; then
698 duration="<1 second"
699 else
700 duration=$duration" seconds"
701 fi
702 echo " Timer duration :" $duration
703
704 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
705}
706
707# Print the value of the time (in seconds) and reset the timer
708# args - <timer message to print> - timer value and message will be printed both on screen
709# and in the timer measurement report
710print_and_reset_timer() {
711 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
712 if [ $# -lt 1 ]; then
713 ((RES_CONF_FAIL++))
714 __print_err "need 1 or more args, <timer message to print>" $@
715 exit 1
716 fi
717 duration=$(($SECONDS-$TC_TIMER))" seconds"
718 if [ $duration -eq 0 ]; then
719 duration="<1 second"
720 else
721 duration=$duration" seconds"
722 fi
723 echo " Timer duration :" $duration
724 TC_TIMER=$SECONDS
725 echo " Timer reset"
726
727 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
728
729}
730# Print info about a deviations from intended tests
731# Each deviation counted is also printed in the testreport
732# args <deviation message to print>
733deviation() {
734 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
735 if [ $# -lt 1 ]; then
736 ((RES_CONF_FAIL++))
737 __print_err "need 1 or more args, <deviation message to print>" $@
738 exit 1
739 fi
740 ((RES_DEVIATION++))
741 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
742 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
743 echo ""
744}
YongchaoWu21f17bb2020-03-05 12:44:08 +0100745
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100746# Stop and remove all containers
747# args: -
748# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +0100749clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100750
751 echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
752
753 CONTAINTER_NAMES=("Policy Agent " $POLICY_AGENT_APP_NAME\
754 "Non-RT RIC Simulator(s)" $RIC_SIM_PREFIX\
755 "Message Router " $MR_APP_NAME\
756 "Callback Receiver " $CR_APP_NAME\
757 "Control Panel " $CONTROL_PANEL_APP_NAME\
758 "SDNC A1 Controller " $SDNC_APP_NAME\
759 "SDNC DB " $SDNC_DB_APP_NAME\
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200760 "SDNC ONAP A1 Adapter " $SDNC_ONAP_APP_NAME\
761 "SDNC DB " $SDNC_ONAP_DB_APP_NAME\
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100762 "CBS " $CBS_APP_NAME\
763 "Consul " $CONSUL_APP_NAME)
764
765 nw=0 # Calc max width of container name, to make a nice table
766 for (( i=1; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
767 if [ ${#CONTAINTER_NAMES[i]} -gt $nw ]; then
768 nw=${#CONTAINTER_NAMES[i]}
769 fi
770 done
771
772 for (( i=0; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
773 APP="${CONTAINTER_NAMES[i]}"
774 CONTR="${CONTAINTER_NAMES[i+1]}"
775 for((w=${#CONTR}; w<$nw; w=w+1)); do
776 CONTR="$CONTR "
777 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200778 echo -ne " $APP: $CONTR - ${GREEN}stopping${EGREEN}${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100779 docker stop $(docker ps -qa --filter name=${CONTR}) &> /dev/null
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200780 echo -ne " $APP: $CONTR - ${GREEN}stopped${EGREEN}${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200781 docker rm --force $(docker ps -qa --filter name=${CONTR}) &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100782 echo -e " $APP: $CONTR - ${GREEN}stopped removed${EGREEN}"
783 done
784
YongchaoWu9a84f512019-12-16 22:54:11 +0100785 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200786
787 echo -e $BOLD" Removing docker network"$EBOLD
788 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
789 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
790 docker network rm $DOCKER_SIM_NWNAME
791 if [ $? -ne 0 ]; then
792 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
793 exit 1
794 fi
795 fi
796
797 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
798 docker network prune --force #&> /dev/null
799
800 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
801 docker volume prune --force #&> /dev/null
802
803 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
804 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
805 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100806}
807
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100808# Function stop and remove all container in the end of the test script, if the arg 'auto-clean' is given at test script start
809# args: -
810# (Function for test scripts)
811auto_clean_containers() {
812 echo
813 if [ "$AUTO_CLEAN" == "auto" ]; then
814 echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
815 clean_containers
YongchaoWu9a84f512019-12-16 22:54:11 +0100816 fi
817}
818
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100819# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
820# args: <sleep-time-in-sec> [any-text-in-quoteds-to-printed]
821# (Function for test scripts)
822sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +0100823
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100824 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
825 if [ $# -lt 1 ]; then
826 ((RES_CONF_FAIL++))
827 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
828 exit 1
829 fi
830 #echo "---- Sleep for " $1 " seconds ---- "$2
831 start=$SECONDS
832 duration=$((SECONDS-start))
833 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200834 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100835 sleep 1
836 duration=$((SECONDS-start))
837 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200838 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100839 echo ""
840}
841
842# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
843# Not to be called from the test script itself.
844__print_err() {
845 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
846 if [ $# -gt 1 ]; then
847 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
848 fi
849}
850
851
852# Helper function to get a the port of a specific ric simulatpor
853# args: <ric-id>
854# (Not for test scripts)
855__find_sim_port() {
856 name=$1" " #Space appended to prevent matching 10 if 1 is desired....
ecaiyanlinux99a769b2020-05-15 13:58:02 +0200857 cmdstr="docker inspect --format='{{(index (index .NetworkSettings.Ports \"$RIC_SIM_PORT/tcp\") 0).HostPort}}' ${name}"
858 res=$(eval $cmdstr)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100859 if [[ "$res" =~ ^[0-9]+$ ]]; then
860 echo $res
861 else
862 echo "0"
863 fi
864}
865
866# Function to create the docker network for the test
867# Not to be called from the test script itself.
868__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200869 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100870 if [ $? -ne 0 ]; then
871 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
872 return 1
873 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200874 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100875 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
876 docker network create $DOCKER_SIM_NWNAME
877 if [ $? -ne 0 ]; then
878 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
879 return 1
880 fi
881 else
882 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
883 fi
884}
885
886# Check if container is started by calling url on localhost using a port, expects response code 2XX
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200887# args: <container-name> <port> <url> https|https
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100888# Not to be called from the test script itself.
889__check_container_start() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200890 paramError=0
891 if [ $# -ne 4 ]; then
892 paramError=1
893 elif [ $4 != "http" ] && [ $4 != "https" ]; then
894 paramError=1
895 fi
896 if [ $paramError -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100897 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200898 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100899 return 1
900 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200901 echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
YongchaoWu9a84f512019-12-16 22:54:11 +0100902 appname=$1
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100903 localport=$2
904 url=$3
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200905 if [[ $appname != "STANDALONE_"* ]] ; then
906 app_started=0
907 for i in {1..10}; do
908 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
909 echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
910 app_started=1
911 break
912 else
913 sleep $i
914 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100915 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200916 if [ $app_started -eq 0 ]; then
917 ((RES_CONF_FAIL++))
918 echo ""
919 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
920 return 1
921 fi
922 if [ $localport -eq 0 ]; then
923 while [ $localport -eq 0 ]; do
924 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
925 localport=$(__find_sim_port $appname)
926 sleep 1
927 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
928 done
929 echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
930 echo ""
931 fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100932 fi
933
934 pa_st=false
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200935 echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200936 TSTART=$SECONDS
937 for i in {1..50}; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200938 if [ $4 == "https" ]; then
939 result="$(__do_curl "-k https://localhost:"${localport}${url})"
940 else
941 result="$(__do_curl $LOCALHOST${localport}${url})"
942 fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100943 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100944 if [ ${#result} -gt 15 ]; then
945 #If response is too long, truncate
946 result="...response text too long, omitted"
947 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200948 echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200949 echo -ne " Container $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN after $(($SECONDS-$TSTART)) seconds"
YongchaoWu9a84f512019-12-16 22:54:11 +0100950 pa_st=true
951 break
952 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200953 TS_TMP=$SECONDS
954 while [ $(($TS_TMP+$i)) -gt $SECONDS ]; do
955 echo -ne " Waiting for container ${appname} service status...retrying in $(($TS_TMP+$i-$SECONDS)) seconds ${SAMELINE}"
956 sleep 1
957 done
YongchaoWu9a84f512019-12-16 22:54:11 +0100958 fi
959 done
960
961 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100962 ((RES_CONF_FAIL++))
963 echo -e $RED" Container ${appname} did not respond to service status"$ERED
964 return 0
965 fi
966
967 echo ""
968 return 0
969}
970
971
972# Function to start a container and wait until it responds on the given port and url.
973#args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
974__start_container() {
975
976 variableArgCount=$(($#-2))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200977 if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100978 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200979 __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 +0100980 exit 1
981 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100982
983 __create_docker_network
984
985 curdir=$PWD
986 cd $SIM_GROUP
987 cd $1
988
989 if [ "$2" == "NODOCKERARGS" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200990 docker-compose up -d &> .dockererr
991 if [ $? -ne 0 ]; then
992 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
993 cat .dockererr
994 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200995 elif [ "$2" == "STANDALONE" ]; then
996 echo "Skipping docker-compose"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100997 else
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200998 docker-compose up -d $2 &> .dockererr
999 if [ $? -ne 0 ]; then
1000 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
1001 cat .dockererr
1002 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001003 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001004 app_prefix=""
1005 if [ "$2" == "STANDALONE" ]; then
1006 app_prefix="STANDALONE_"
1007 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001008 shift; shift;
1009 cntr=0
1010 while [ $cntr -lt $variableArgCount ]; do
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001011 app=$app_prefix$1; shift;
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001012 port=$1; shift;
1013 url=$1; shift;
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001014 httpx=$1; shift;
1015 let cntr=cntr+4
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001016
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001017 __check_container_start "$app" "$port" "$url" $httpx
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001018 done
1019
1020 cd $curdir
1021 echo ""
1022 return 0
YongchaoWu9a84f512019-12-16 22:54:11 +01001023}
1024
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001025####################
1026### Consul functions
1027####################
1028
1029# Function to load config from a file into consul for the Policy Agent
1030# arg: <json-config-file>
1031# (Function for test scripts)
1032consul_config_app() {
1033
1034 echo -e $BOLD"Configuring Consul"$EBOLD
1035
1036 if [ $# -ne 1 ]; then
1037 ((RES_CONF_FAIL++))
1038 __print_err "need one arg, <json-config-file>" $@
1039 exit 1
1040 fi
1041
1042 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
1043
1044 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
1045 if [ $? -ne 0 ]; then
1046 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
1047 ((RES_CONF_FAIL++))
1048 return 1
1049 fi
1050 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001051 echo $body > ".output"$1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001052
1053 if [ $? -ne 0 ]; then
1054 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
1055 ((RES_CONF_FAIL++))
1056 return 1
1057 else
1058 targetJson=$(< $1)
1059 targetJson="{\"config\":"$targetJson"}"
1060 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001061 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001062 if [ $res -ne 0 ]; then
1063 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
1064 ((RES_CONF_FAIL++))
1065 return 1
1066 else
1067 echo -e $GREEN" Config loaded ok to consul"$EGREEN
1068 fi
1069 fi
1070
1071 echo ""
1072
1073}
1074
1075# Function to perpare the consul configuration according to the current simulator configuration
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001076# args: SDNC|SDNC_ONAP|NOSDNC <output-file>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001077# (Function for test scripts)
1078prepare_consul_config() {
1079 echo -e $BOLD"Prepare Consul config"$EBOLD
1080
1081 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
1082
1083 if [ $# != 2 ]; then
1084 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001085 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001086 exit 1
1087 fi
1088
1089 if [ $1 == "SDNC" ]; then
1090 echo -e " Config$BOLD including SDNC$EBOLD configuration"
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001091 elif [ $1 == "SDNC_ONAP" ]; then
1092 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001093 elif [ $1 == "NOSDNC" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001094 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001095 else
1096 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001097 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001098 exit 1
1099 fi
1100
1101 config_json="\n {"
1102 if [ $1 == "SDNC" ]; then
1103 config_json=$config_json"\n \"controller\": ["
1104 config_json=$config_json"\n {"
1105 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001106 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1107 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://$SDNC_APP_NAME:$SDNC_PORT\","
1108 else
1109 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://localhost:$SDNC_LOCAL_PORT\","
1110 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001111 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
1112 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
1113 config_json=$config_json"\n }"
1114 config_json=$config_json"\n ],"
1115 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001116 if [ $1 == "SDNC_ONAP" ]; then
1117 config_json=$config_json"\n \"controller\": ["
1118 config_json=$config_json"\n {"
1119 config_json=$config_json"\n \"name\": \"$SDNC_ONAP_APP_NAME\","
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001120 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1121 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
1122 else
1123 config_json=$config_json"\n \"baseUrl\": \"http://localhost:$SDNC_ONAP_EXTERNAL_PORT\","
1124 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001125 config_json=$config_json"\n \"userName\": \"$SDNC_ONAP_USER\","
1126 config_json=$config_json"\n \"password\": \"$SDNC_ONAP_PWD\""
1127 config_json=$config_json"\n }"
1128 config_json=$config_json"\n ],"
1129 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001130
1131
1132 config_json=$config_json"\n \"streams_publishes\": {"
1133 config_json=$config_json"\n \"dmaap_publisher\": {"
1134 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1135 config_json=$config_json"\n \"dmaap_info\": {"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001136 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1137 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_WRITE_URL\""
1138 else
1139 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_WRITE_URL\""
1140 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001141 config_json=$config_json"\n }"
1142 config_json=$config_json"\n }"
1143 config_json=$config_json"\n },"
1144 config_json=$config_json"\n \"streams_subscribes\": {"
1145 config_json=$config_json"\n \"dmaap_subscriber\": {"
1146 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1147 config_json=$config_json"\n \"dmaap_info\": {"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001148 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1149 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_READ_URL\""
1150 else
1151 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_READ_URL\""
1152 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001153 config_json=$config_json"\n }"
1154 config_json=$config_json"\n }"
1155 config_json=$config_json"\n },"
1156
1157 config_json=$config_json"\n \"ric\": ["
1158
1159 rics=$(docker ps | grep ricsim | awk '{print $NF}')
1160
1161 if [ $? -ne 0 ] || [ -z "$rics" ]; then
1162 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
1163 ((RES_CONF_FAIL++))
1164 return 1
1165 fi
1166
1167 cntr=0
1168 for ric in $rics; do
1169 if [ $cntr -gt 0 ]; then
1170 config_json=$config_json"\n ,"
1171 fi
1172 config_json=$config_json"\n {"
1173 config_json=$config_json"\n \"name\": \"$ric\","
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001174 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1175 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
1176 else
1177 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://localhost:$(__find_sim_port $ric)\","
1178 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001179 if [ $1 == "SDNC" ]; then
1180 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001181 elif [ $1 == "SDNC_ONAP" ]; then
1182 config_json=$config_json"\n \"controller\": \"$SDNC_ONAP_APP_NAME\","
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001183 fi
1184 config_json=$config_json"\n \"managedElementIds\": ["
1185 config_json=$config_json"\n \"me1_$ric\","
1186 config_json=$config_json"\n \"me2_$ric\""
1187 config_json=$config_json"\n ]"
1188 config_json=$config_json"\n }"
1189 let cntr=cntr+1
1190 done
1191
1192 config_json=$config_json"\n ]"
1193 config_json=$config_json"\n}"
1194
1195
1196 printf "$config_json">$2
1197
1198 echo ""
1199}
1200
1201
1202# Start Consul and CBS
1203# args: -
1204# (Function for test scripts)
1205start_consul_cbs() {
1206
1207 echo -e $BOLD"Starting Consul and CBS"$EBOLD
1208
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001209 __start_container consul_cbs NODOCKERARGS "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
1210 "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001211}
1212
1213###########################
1214### RIC Simulator functions
1215###########################
1216
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001217use_simulator_http() {
1218 echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
1219 export RIC_SIM_HTTPX="http"
1220 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1221 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001222 echo ""
1223}
1224
1225use_simulator_https() {
1226 echo -e "Using secure $BOLD https $EBOLD towards the simulators"
1227 export RIC_SIM_HTTPX="https"
1228 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1229 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001230 echo ""
1231}
1232
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001233# Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
1234# args: ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
1235# (Function for test scripts)
1236start_ric_simulators() {
1237
1238 echo -e $BOLD"Starting RIC Simulators"$EBOLD
1239
1240 if [ $# != 3 ]; then
1241 ((RES_CONF_FAIL++))
1242 __print_err "need three args, ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1243 exit 1
1244 fi
1245 echo " $2 simulators using basename: $1 on interface: $3"
1246 #Set env var for simulator count and A1 interface vesion for the given group
1247 if [ $1 == "ricsim_g1" ]; then
1248 G1_COUNT=$2
1249 G1_A1_VERSION=$3
1250 elif [ $1 == "ricsim_g2" ]; then
1251 G2_COUNT=$2
1252 G2_A1_VERSION=$3
1253 elif [ $1 == "ricsim_g3" ]; then
1254 G3_COUNT=$2
1255 G3_A1_VERSION=$3
1256 else
1257 ((RES_CONF_FAIL++))
1258 __print_err "need three args, gricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>" $@
1259 exit 1
1260 fi
1261
1262 # Create .env file to compose project, all ric container will get this prefix
1263 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
1264
1265 export G1_A1_VERSION
1266 export G2_A1_VERSION
1267 export G3_A1_VERSION
1268
1269 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1270 app_data=""
1271 cntr=1
1272 while [ $cntr -le $2 ]; do
1273 app=$1"_"$cntr
1274 port=0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001275 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001276 let cntr=cntr+1
1277 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001278 __start_container ric "$docker_args" $app_data
1279
1280}
1281
1282###########################
1283### Control Panel functions
1284###########################
1285
1286# Start the Control Panel container
1287# args: -
1288# (Function for test scripts)
1289start_control_panel() {
1290
1291 echo -e $BOLD"Starting Control Panel"$EBOLD
1292
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001293 __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001294
1295}
1296
1297##################
1298### SDNC functions
1299##################
1300
1301# Start the SDNC A1 Controller
1302# args: -
1303# (Function for test scripts)
1304start_sdnc() {
1305
1306 echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1307
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001308 check_excluded_image 'SDNC'
1309 if [ $? -eq 1 ]; then
1310 echo -e $RED"The image for SDNC and the related DB has not been checked for this test run due to arg to the test script"$ERED
1311 echo -e $RED"SDNC will not be started"$ERED
1312 exit
1313 fi
1314
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001315 __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT $SDNC_ALIVE_URL "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001316
1317}
1318
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001319use_sdnc_http() {
1320 echo -e $BOLD"Using http between agent and SDNC"$EBOLD
1321 export SDNC_HTTPX="http"
1322 export SDNC_PORT=$SDNC_INTERNAL_PORT
1323 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT
1324 echo ""
1325}
1326
1327use_sdnc_https() {
1328 echo -e $BOLD"Using https between agent and SDNC"$EBOLD
1329 export SDNC_HTTPX="https"
1330 export SDNC_PORT=$SDNC_INTERNAL_SECURE_PORT
1331 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_SECURE_PORT
1332 echo ""
1333}
1334
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001335#######################
1336### SDNC ONAP functions
1337#######################
1338
1339# Start the SDNC ONAP A1 Adapter
1340# args: -
1341# (Function for test scripts)
1342start_sdnc_onap() {
1343
1344 echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1345
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001346 check_excluded_image 'SDNC_ONAP'
1347 if [ $? -eq 1 ]; then
1348 echo -e $RED"The image for SDNC ONAP and the related DB has not been checked for this test run due to arg to the test script"$ERED
1349 echo -e $RED"SDNC ONAP will not be started"$ERED
1350 exit
1351 fi
1352
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001353 __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT $SDNC_ONAP_ALIVE_URL "http"
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001354
1355}
1356
1357# Configure the SDNC ONAP A1 Adapter
1358# args: -
1359# (Function for test scripts)
1360config_sdnc_onap() {
1361
1362 echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1363
1364 LOCALFILE=".sdnc_onap.prop"
1365 REMOTEFILE="/tmp/.sdnc_onap.prop"
1366
1367 docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1368 if [ $? -ne 0 ]; then
1369 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1370 exit 1
1371 fi
1372
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001373
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001374 #Config of the prop file shall be inserted here
1375
1376 #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1377
1378 docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1379 if [ $? -ne 0 ]; then
1380 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1381 exit 1
1382 fi
1383
1384 docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1385 if [ $? -ne 0 ]; then
1386 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1387 exit 1
1388 fi
1389}
1390
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001391#####################
1392### MR stub functions
1393#####################
1394
1395# Start the Message Router stub interface in the simulator group
1396# args: -
1397# (Function for test scripts)
1398start_mr() {
1399
1400 echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001401 export MR_CERT_MOUNT_DIR="./cert"
1402 __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001403}
1404
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001405use_mr_http() {
1406 echo -e $BOLD"Using http between agent and MR"$EBOLD
1407 export MR_HTTPX="http"
1408 export MR_PORT=$MR_INTERNAL_PORT
1409 export MR_LOCAL_PORT=$MR_EXTERNAL_PORT
1410 echo ""
1411}
1412
1413use_mr_https() {
1414 echo -e $BOLD"Using https between agent and MR"$EBOLD
1415 export MR_HTTPX="https"
1416 export MR_PORT=$MR_INTERNAL_SECURE_PORT
1417 export MR_LOCAL_PORT=$MR_EXTERNAL_SECURE_PORT
1418 echo ""
1419}
1420
1421
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001422################
1423### CR functions
1424################
1425
1426# Start the Callback reciver in the simulator group
1427# args: -
1428# (Function for test scripts)
1429start_cr() {
1430
1431 echo -e $BOLD"Starting Callback Receiver"$EBOLD
1432
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001433 __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001434
1435}
1436
1437###########################
1438### Policy Agents functions
1439###########################
1440
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001441# Use an agent on the local machine instead of container
1442use_agent_stand_alone() {
1443 AGENT_STAND_ALONE=1
1444}
1445
1446# Start the policy agent
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001447# args: -
1448# (Function for test scripts)
1449start_policy_agent() {
1450
1451 echo -e $BOLD"Starting Policy Agent"$EBOLD
1452
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001453 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1454 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1455 else
1456 echo -e $RED"The consul config produced by this test script (filename '<fullpath-to-autotest-dir>.output<file-name>"$ERED
1457 echo -e $RED"where the file name is the file in the consul_config_app command in this script) must be pointed out by the agent "$ERED
1458 echo -e $RED"application.yaml"$ERED
1459 echo -e $RED"The application jar may need to be built beforefor continuing"$ERED
1460 echo -e $RED"The agent shall now be running on port $POLICY_AGENT_EXTERNAL_PORT for http"$ERED
1461
1462 read -p "<press any key to continue>"
1463 __start_container policy_agent "STANDALONE" $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1464 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001465
1466}
1467
1468# All calls to the agent will be directed to the agent REST interface from now on
1469# args: -
1470# (Function for test scripts)
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001471use_agent_rest_http() {
1472 echo -e $BOLD"Using agent REST interface with http"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001473 export ADAPTER=$RESTBASE
1474 echo ""
1475}
1476
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001477# All calls to the agent will be directed to the agent REST interface from now on
1478# args: -
1479# (Function for test scripts)
1480use_agent_rest_https() {
1481 echo -e $BOLD"Using agent REST interface with https"$EBOLD
1482 export ADAPTER=$RESTBASE_SECURE
1483 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001484 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001485}
1486
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001487# All calls to the agent will be directed to the agent dmaap interface from now on
1488# args: -
1489# (Function for test scripts)
1490use_agent_dmaap() {
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001491 echo -e $BOLD"Agent using DMAAP interface"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001492 export ADAPTER=$DMAAPBASE
1493 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001494 return 0
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001495}
1496
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001497
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001498# Turn on debug level tracing in the agent
1499# args: -
1500# (Function for test scripts)
1501set_agent_debug() {
1502 echo -e $BOLD"Setting agent debug"$EBOLD
1503 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1504 if [ $? -ne 0 ]; then
1505 __print_err "could not set debug mode" $@
1506 return 1
1507 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001508 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001509 return 0
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001510}
1511
1512# Perform curl retries when making direct call to the agent for the specified http response codes
1513# Speace separated list of http response codes
1514# args: [<response-code>]*
1515use_agent_retries() {
1516 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1517 AGENT_RETRY_CODES=$@
1518 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001519 return
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001520}
1521
1522#################
1523### Log functions
1524#################
1525
1526# Check the agent logs for WARNINGs and ERRORs
1527# args: -
1528# (Function for test scripts)
1529
YongchaoWu9a84f512019-12-16 22:54:11 +01001530check_policy_agent_logs() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001531 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
YongchaoWu9a84f512019-12-16 22:54:11 +01001532}
1533
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001534check_control_panel_logs() {
1535 __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
YongchaoWu9a84f512019-12-16 22:54:11 +01001536}
1537
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001538__check_container_logs() {
1539 dispname=$1
1540 appname=$2
1541 logpath=$3
1542 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1543
1544 #tmp=$(docker ps | grep $appname)
1545 tmp=$(docker ps -q --filter name=$appname) #get the container id
1546 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
1547 echo $dispname" is not running, no check made"
1548 return
1549 fi
1550 foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1551 if [ $? -ne 0 ];then
1552 echo " Problem to search $appname log $logpath"
1553 else
1554 if [ $foundentries -eq 0 ]; then
1555 echo " No WARN entries found in $appname log $logpath"
1556 else
1557 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1558 fi
1559 fi
1560 foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1561 if [ $? -ne 0 ];then
1562 echo " Problem to search $appname log $logpath"
1563 else
1564 if [ $foundentries -eq 0 ]; then
1565 echo " No ERR entries found in $appname log $logpath"
1566 else
1567 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1568 fi
1569 fi
1570 echo ""
1571}
1572
1573# Store all container logs and other logs in the log dir for the script
1574# Logs are stored with a prefix in case logs should be stored several times during a test
1575# args: <logfile-prefix>
1576# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01001577store_logs() {
1578 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001579 ((RES_CONF_FAIL++))
1580 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01001581 exit 1
1582 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001583 echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +01001584
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001585 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001586 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1587 docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1588 docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1589 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1590 docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1591 docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1592 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1593
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001594 docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1595
1596 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 +01001597
1598 rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1599 for ric in $rics; do
1600 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1601 done
1602 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1603 echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1604 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01001605}
1606
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001607###############
1608## Generic curl
1609###############
1610# Generic curl function, assumed all 200-codes are ok
1611# args: <url>
1612# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
1613# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01001614__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001615 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1616 curlString="curl -skw %{http_code} $1"
1617 echo " CMD: $curlString" >> $HTTPLOG
1618 res=$($curlString)
1619 echo " RESP: $res" >> $HTTPLOG
YongchaoWu9a84f512019-12-16 22:54:11 +01001620 http_code="${res:${#res}-3}"
1621 if [ ${#res} -eq 3 ]; then
1622 echo "<no-response-from-server>"
1623 return 1
1624 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001625 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01001626 echo "<not found, resp:${http_code}>"
1627 return 1
1628 fi
1629 if [ $# -eq 2 ]; then
1630 echo "${res:0:${#res}-3}" | xargs
1631 else
1632 echo "${res:0:${#res}-3}"
1633 fi
1634
1635 return 0
1636 fi
1637}
1638
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001639#######################################
1640### Basic helper function for test cases
1641#######################################
1642
1643# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1644# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
1645# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1646# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
1647# before setting pass or fail depending on the result of comparing variable and target using the operator.
1648# 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.
1649# Not to be called from test script.
1650
1651__var_test() {
1652 checkjsonarraycount=0
1653
1654 if [ $# -eq 6 ]; then
1655 if [[ $3 == "json:"* ]]; then
1656 checkjsonarraycount=1
1657 fi
1658
1659 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} within ${6} seconds ----"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001660 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001661 ((RES_TEST++))
1662 start=$SECONDS
1663 ctr=0
1664 for (( ; ; )); do
1665 if [ $checkjsonarraycount -eq 0 ]; then
1666 result="$(__do_curl $2$3)"
1667 retcode=$?
1668 result=${result//[[:blank:]]/} #Strip blanks
1669 else
1670 path=${3:5}
1671 result="$(__do_curl $2$path)"
1672 retcode=$?
1673 echo "$result" > .tmp.curl.json
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001674 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001675 fi
1676 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001677 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001678 let ctr=ctr+1
1679 if [ $retcode -ne 0 ]; then
1680 if [ $duration -gt $6 ]; then
1681 ((RES_FAIL++))
1682 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1683 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1684 return
1685 fi
1686 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1687 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001688 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001689 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1690 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds ----"
1691 return
1692 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1693 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001694 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001695 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1696 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1697 return
1698 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1699 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001700 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001701 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1702 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1703 return
1704 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1705 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001706 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001707 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
1708 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met in ${duration} seconds, result = ${result} ----"
1709 return
1710 else
1711 if [ $duration -gt $6 ]; then
1712 ((RES_FAIL++))
1713 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
1714 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached in ${6} seconds, result = ${result} ----"
1715 return
1716 fi
1717 fi
1718 sleep 1
1719 done
1720 elif [ $# -eq 5 ]; then
1721 if [[ $3 == "json:"* ]]; then
1722 checkjsonarraycount=1
1723 fi
1724
1725 #echo -e "---- ${1} sim test criteria: \033[1m ${3} \033[0m ${4} ${5} ----"
1726 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1727 ((RES_TEST++))
1728 if [ $checkjsonarraycount -eq 0 ]; then
1729 result="$(__do_curl $2$3)"
1730 retcode=$?
1731 result=${result//[[:blank:]]/} #Strip blanks
1732 else
1733 path=${3:5}
1734 result="$(__do_curl $2$path)"
1735 retcode=$?
1736 echo "$result" > .tmp.curl.json
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001737 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001738 fi
1739 if [ $retcode -ne 0 ]; then
1740 ((RES_FAIL++))
1741 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1742 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
1743 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1744 ((RES_PASS++))
1745 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1746 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met"
1747 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1748 ((RES_PASS++))
1749 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1750 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1751 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1752 ((RES_PASS++))
1753 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1754 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1755 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1756 ((RES_PASS++))
1757 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
1758 #echo -e "---- \033[32m\033[1mPASS\033[0m - Test criteria met, result = ${result} ----"
1759 else
1760 ((RES_FAIL++))
1761 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
1762 #echo -e "---- \033[31m\033[1mFAIL\033[0m - Target ${3} ${4} ${5} not reached, result = ${result} ----"
1763 fi
1764 else
1765 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1766 echo "Got:" $@
1767 exit 1
1768 fi
1769}
1770
1771
1772### Generic test cases for varaible checking
1773
1774# Tests if a variable value in the CR is equal to a target value and and optional timeout.
1775# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1776# equal to the target or not.
1777# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1778# before setting pass or fail depending on if the variable value becomes equal to the target
1779# value or not.
1780# (Function for test scripts)
1781cr_equal() {
1782 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1783 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1784 else
1785 ((RES_CONF_FAIL++))
1786 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1787 fi
1788}
1789
1790# Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1791# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1792# equal to the target or not.
1793# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1794# before setting pass or fail depending on if the variable value becomes equal to the target
1795# value or not.
1796# (Function for test scripts)
1797mr_equal() {
1798 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1799 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1800 else
1801 ((RES_CONF_FAIL++))
1802 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1803 fi
1804}
1805
1806# Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1807# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1808# greater than the target or not.
1809# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1810# before setting pass or fail depending on if the variable value becomes greater than the target
1811# value or not.
1812# (Function for test scripts)
1813mr_greater() {
1814 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001815 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001816 else
1817 ((RES_CONF_FAIL++))
1818 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1819 fi
1820}
1821
1822# Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1823mr_read() {
1824 echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1825}
1826
1827# Print a variable value from the MR stub.
1828# arg: <variable-name>
1829# (Function for test scripts)
1830mr_print() {
1831 if [ $# != 1 ]; then
1832 ((RES_CONF_FAIL++))
1833 __print_err "need one arg, <mr-param>" $@
1834 exit 1
1835 fi
1836 echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD
1837}