blob: a69df911f37e820d1f7acf61f4898abdd8f0fe15 [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
BjornMagnussonXAad047782020-06-08 15:54:11 +020021# Arg: local|remote|remote-remove [auto-clean] [--stop-at-error] [--ricsim-prefix <prefix> ] [--use-local-image <app-nam> [<app-name>]*]
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
BjornMagnussonXA496156d2020-08-10 14:16:24 +020078export CR_HTTPX="http"
79export CR_PORT=$CR_INTERNAL_PORT
80export CR_LOCAL_PORT=$CR_EXTERNAL_PORT #When CR is running outside the docker net
81
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020082export SDNC_HTTPX="http"
83export SDNC_PORT=$SDNC_INTERNAL_PORT
84export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT #When agent is running outside the docker net
85
BjornMagnussonXA80a92002020-03-19 14:31:06 +010086#Localhost constant
87LOCALHOST="http://localhost:"
88
89# Make curl retries for http response codes set in this env var, space separated list of codes
90AGENT_RETRY_CODES=""
91
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020092# Var to contol if the agent runs in a container (normal = 0) or as application on the local machine ( = 1)
93AGENT_STAND_ALONE=0
94
BjornMagnussonXA80a92002020-03-19 14:31:06 +010095# Var to hold 'auto' in case containers shall be stopped when test case ends
96AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +010097
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020098# Var to hold the app names to use local image for when running 'remote' or 'remote-remove'
99USE_LOCAL_IMAGES=""
100
BjornMagnussonXAad047782020-06-08 15:54:11 +0200101# List of available apps to override with local image
102AVAILABLE_LOCAL_IMAGES_OVERRIDE="PA CP SDNC RICSIM"
103
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200104# Use this var (STOP_AT_ERROR=1 in the test script) for debugging/trouble shooting to take all logs and exit at first FAIL test case
105STOP_AT_ERROR=0
106
YongchaoWu9a84f512019-12-16 22:54:11 +0100107# Set a description string for the test case
108if [ -z "$TC_ONELINE_DESCR" ]; then
109 TC_ONELINE_DESCR="<no-description>"
110 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
111fi
112
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100113# Counter for test suites
114if [ -f .tmp_tcsuite_ctr ]; then
115 tmpval=$(< .tmp_tcsuite_ctr)
116 ((tmpval++))
117 echo $tmpval > .tmp_tcsuite_ctr
118fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100119
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100120# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
121# FTC1.sh -> ATC == FTC1
122ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
YongchaoWu9a84f512019-12-16 22:54:11 +0100123
124# Create the logs dir if not already created in the current dir
125if [ ! -d "logs" ]; then
126 mkdir logs
127fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100128TESTLOGS=$PWD/logs
129
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100130# Create a http message log for this testcase
131HTTPLOG=$PWD"/.httplog_"$ATC".txt"
132echo "" > $HTTPLOG
133
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200134#Create result file (containing '1' for error) for this test case
135#Will be replaced with a file containing '0' if script is ok
136
137echo "1" > "$PWD/.result$ATC.txt"
138
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100139# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100140mkdir -p $TESTLOGS/$ATC
141
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100142# Clear the log dir for the test case
143rm $TESTLOGS/$ATC/*.log &> /dev/null
144rm $TESTLOGS/$ATC/*.txt &> /dev/null
145rm $TESTLOGS/$ATC/*.json &> /dev/null
146
147# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100148TCLOG=$TESTLOGS/$ATC/TC.log
149exec &> >(tee ${TCLOG})
150
151#Variables for counting tests as well as passed and failed tests
152RES_TEST=0
153RES_PASS=0
154RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100155RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200156RES_DEVIATION=0
157
158#File to keep deviation messages
159DEVIATION_FILE=".tmp_deviations"
160rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100161
162#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100163TCTEST_START=$SECONDS
164
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200165#File to save timer measurement results
166TIMER_MEASUREMENTS=".timer_measurement.txt"
167echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
168
169
YongchaoWu9a84f512019-12-16 22:54:11 +0100170echo "-------------------------------------------------------------------------------------------------"
171echo "----------------------------------- Test case: "$ATC
172echo "----------------------------------- Started: "$(date)
173echo "-------------------------------------------------------------------------------------------------"
174echo "-- Description: "$TC_ONELINE_DESCR
175echo "-------------------------------------------------------------------------------------------------"
176echo "----------------------------------- Test case setup -----------------------------------"
177
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200178START_ARG=$1
179paramerror=0
180if [ $# -lt 1 ]; then
181 paramerror=1
182fi
183if [ $paramerror -eq 0 ]; then
184 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ] && [ "$1" != "local" ]; then
185 paramerror=1
186 else
187 shift;
188 fi
189fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200190foundparm=0
191while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
192 foundparm=1
193 if [ $paramerror -eq 0 ]; then
194 if [ "$1" == "auto-clean" ]; then
195 AUTO_CLEAN="auto"
196 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200197 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200198 foundparm=0
199 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200200 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200201 if [ $paramerror -eq 0 ]; then
202 if [ "$1" == "--stop-at-error" ]; then
203 STOP_AT_ERROR=1
204 echo "Option set - Stop at first error"
205 shift;
206 foundparm=0
207 fi
208 fi
209 if [ $paramerror -eq 0 ]; then
210 if [ "$1" == "--ricsim-prefix" ]; then
211 shift;
212 RIC_SIM_PREFIX=$1
213 if [ -z "$1" ]; then
214 paramerror=1
215 else
216 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
217 shift;
218 foundparm=0
219 fi
220 fi
221 fi
222 if [ $paramerror -eq 0 ]; then
223 if [ "$1" == "--use-local-image" ]; then
224 USE_LOCAL_IMAGES=""
225 shift
226 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
227 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
228 if [[ "$AVAILABLE_LOCAL_IMAGES_OVERRIDE" != *"$1"* ]]; then
229 paramerror=1
230 fi
231 shift;
232 done
233 foundparm=0
234 if [ -z "$USE_LOCAL_IMAGES" ]; then
235 paramerror=1
236 else
237 echo "Option set - Override remote images for app(s):"$USE_LOCAL_IMAGES
238 fi
239 fi
240 fi
241done
242echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200243
BjornMagnussonXAad047782020-06-08 15:54:11 +0200244#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200245if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
246 paramerror=1
247fi
248
249if [ $paramerror -eq 1 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200250 echo -e $RED"Expected arg: local|remote|remote-remove [auto-clean] [--stop-at-error] [--ricsim-prefix <prefix> ] [--use-local-image <app-nam> [<app-name>]*]"$ERED
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200251 exit 1
252fi
253
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100254echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100255
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100256#Temp var to check for image variable name errors
257IMAGE_ERR=0
258#Create a file with image info for later printing as a table
259image_list_file=".image-list"
260echo -e " Container\tImage\ttag" > $image_list_file
261
262# Check if image env var is set and if so export the env var with image to use (used by docker compose files)
263# arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>
264__check_image_var() {
265 if [ $# -ne 5 ]; then
266 echo "Expected arg: <image name> <script start-arg> <target-variable-name> <image-variable-name> <image-tag-variable-name>"
267 ((IMAGE_ERR++))
268 return
269 fi
270 tmp=${1}"\t"
271 #Create var from the input var names
272 image="${!4}"
273 tag="${!5}"
274
275 if [ -z $image ]; then
276 echo -e $RED"\$"$4" not set in test_env"$ERED
277 ((IMAGE_ERR++))
278 echo ""
279 tmp=$tmp"<no-image>\t"
280 else
281 tmp=$tmp$image"\t"
282 fi
283 if [ -z $tag ]; then
284 echo -e $RED"\$"$5" not set in test_env"$ERED
285 ((IMAGE_ERR++))
286 echo ""
287 tmp=$tmp"<no-tag>\t"
288 else
289 tmp=$tmp$tag
290 fi
291 echo -e "$tmp" >> $image_list_file
292 #Export the env var
293 export "${3}"=$image":"$tag
294
295 #echo " Configured image for ${1} (script start arg=${2}): "$image":"$tag
296}
297
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200298
299#Check if app local image shall override remote image
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200300__check_image_local_override() {
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200301 for im in $USE_LOCAL_IMAGES; do
302 if [ "$1" == "$im" ]; then
303 return 1
304 fi
305 done
306 return 0
307}
308
309#Check if app uses image excluded from this test run
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200310__check_excluded_image() {
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200311 for im in $EXCLUDED_IMAGES; do
312 if [ "$1" == "$im" ]; then
313 return 1
314 fi
315 done
316 return 0
317}
318
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100319# Check that image env setting are available
320echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200321
322if [ $START_ARG == "local" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100323
324 #Local agent image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200325 __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 +0100326
327 #Local Control Panel image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200328 __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 +0100329
330 #Local SNDC image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200331 __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 +0100332
333 #Local ric sim image
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200334 __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 +0100335
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200336elif [ $START_ARG == "remote" ] || [ $START_ARG == "remote-remove" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100337
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200338 __check_image_local_override 'PA'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200339 if [ $? -eq 0 ]; then
340 #Remote agent image
341 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_REMOTE_IMAGE" "POLICY_AGENT_REMOTE_IMAGE_TAG"
342 else
343 #Local agent image
344 __check_image_var " Policy Agent" $START_ARG "POLICY_AGENT_IMAGE" "POLICY_AGENT_LOCAL_IMAGE" "POLICY_AGENT_LOCAL_IMAGE_TAG"
345 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100346
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200347 __check_image_local_override 'CP'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200348 if [ $? -eq 0 ]; then
349 #Remote Control Panel image
350 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE" "CONTROL_PANEL_REMOTE_IMAGE_TAG"
351 else
352 #Local Control Panel image
353 __check_image_var " Control Panel" $START_ARG "CONTROL_PANEL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE" "CONTROL_PANEL_LOCAL_IMAGE_TAG"
354 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100355
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200356 __check_image_local_override 'SDNC'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200357 if [ $? -eq 0 ]; then
358 #Remote SDNC image
359 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE" "SDNC_A1_CONTROLLER_REMOTE_IMAGE_TAG"
360 else
361 #Local SNDC image
362 __check_image_var " SDNC A1 Controller" $START_ARG "SDNC_A1_CONTROLLER_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE" "SDNC_A1_CONTROLLER_LOCAL_IMAGE_TAG"
363 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100364
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200365 __check_image_local_override 'RICSIM'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200366 if [ $? -eq 0 ]; then
367 #Remote ric sim image
368 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_REMOTE_IMAGE" "RIC_SIM_REMOTE_IMAGE_TAG"
369 else
370 #Local ric sim image
371 __check_image_var " RIC Simulator" $START_ARG "RIC_SIM_IMAGE" "RIC_SIM_LOCAL_IMAGE" "RIC_SIM_LOCAL_IMAGE_TAG"
372 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100373
374else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200375 #Should never get here....
376 echo "Unknow args: "$@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100377 exit 1
YongchaoWu9a84f512019-12-16 22:54:11 +0100378fi
379
YongchaoWu9a84f512019-12-16 22:54:11 +0100380
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100381# 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 +0200382__check_image_var " Message Router" $START_ARG "MRSTUB_IMAGE" "MRSTUB_LOCAL_IMAGE" "MRSTUB_LOCAL_IMAGE_TAG"
383__check_image_var " Callback Receiver" $START_ARG "CR_IMAGE" "CR_LOCAL_IMAGE" "CR_LOCAL_IMAGE_TAG"
384__check_image_var " Consul" $START_ARG "CONSUL_IMAGE" "CONSUL_REMOTE_IMAGE" "CONSUL_REMOTE_IMAGE_TAG"
385__check_image_var " CBS" $START_ARG "CBS_IMAGE" "CBS_REMOTE_IMAGE" "CBS_REMOTE_IMAGE_TAG"
386__check_image_var " SDNC DB" $START_ARG "SDNC_DB_IMAGE" "SDNC_DB_REMOTE_IMAGE" "SDNC_DB_REMOTE_IMAGE_TAG"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200387__check_excluded_image 'SDNC_ONAP'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200388if [ $? -eq 0 ]; then
389 __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"
390 __check_image_var " SDNC ONAP DB" $START_ARG "SDNC_ONAP_DB_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE" "SDNC_ONAP_DB_REMOTE_IMAGE_TAG"
391fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100392
393#Errors in image setting - exit
394if [ $IMAGE_ERR -ne 0 ]; then
395 exit 1
396fi
397
398#Print a tables of the image settings
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200399echo -e $BOLD"Images configured for start arg: "$START $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100400column -t -s $'\t' $image_list_file
401
YongchaoWuf309b1b2020-01-22 13:24:48 +0100402echo ""
403
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100404
405#Set the SIM_GROUP var
406echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
407if [ -z "$SIM_GROUP" ]; then
408 SIM_GROUP=$PWD/../simulator-group
409 if [ ! -d $SIM_GROUP ]; then
410 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
411 echo -e $RED"Please set the SIM_GROUP manually in the test_env.sh"$ERED
412 exit 1
413 else
414 echo " SIM_GROUP auto set to: " $SIM_GROUP
415 fi
416elif [ $SIM_GROUP = *simulator_group ]; then
417 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
418 exit 1
419else
420 echo " SIM_GROUP env var already set to: " $SIM_GROUP
maximesson28ee8a52020-03-17 09:32:03 +0100421fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100422
423echo ""
424
425#Temp var to check for image pull errors
426IMAGE_ERR=0
427
428#Function to check if image exist and stop+remove the container+pull new images as needed
429#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag>
430__check_and_pull_image() {
431
432 echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$4$EBOLD"
433 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
434 tmp_im=$(docker images --format $format_string ${4})
435
436 if [ $1 == "local" ]; then
437 if [ -z "$tmp_im" ]; then
438 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
439 ((IMAGE_ERR++))
440 return 1
441 else
442 echo -e " "$2" (local image): \033[1m"$4"\033[0m "$GREEN"OK"$EGREEN
443 fi
444 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
445 if [ $1 == "remote-remove" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200446 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100447 tmp="$(docker ps -aq --filter name=${3})"
448 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200449 docker stop $tmp &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100450 if [ $? -ne 0 ]; then
451 ((IMAGE_ERR++))
452 echo ""
453 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200454 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100455 return 1
456 fi
457 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200458 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100459 tmp="$(docker ps -aq --filter name=${3})" &> /dev/null
460 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200461 docker rm $tmp &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100462 if [ $? -ne 0 ]; then
463 ((IMAGE_ERR++))
464 echo ""
465 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200466 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100467 return 1
468 fi
469 fi
470 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200471 echo -ne " Removing image - ${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100472 tmp="$(docker images -q ${4})" &> /dev/null
473 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200474 docker rmi $4 &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100475 if [ $? -ne 0 ]; then
476 ((IMAGE_ERR++))
477 echo ""
478 echo -e $RED" Image could not be removed - try manual removal of the image"$ERED
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200479 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100480 return 1
481 fi
482 echo -e " Removing image - "$GREEN"removed"$EGREEN
483 else
484 echo -e " Removing image - "$GREEN"image not in repository"$EGREEN
485 fi
486 tmp_im=""
487 fi
488 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200489 echo -ne " Pulling image${SAMELINE}"
490 docker pull $4 &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100491 tmp_im=$(docker images ${4} | grep -v REPOSITORY)
492 if [ -z "$tmp_im" ]; then
493 echo ""
494 echo -e " Pulling image -$RED could not be pulled"$ERED
495 ((IMAGE_ERR++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200496 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100497 return 1
498 fi
499 echo -e " Pulling image -$GREEN Pulled $EGREEN"
500 else
501 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
502 fi
503 fi
504 return 0
505}
506
507
508echo -e $BOLD"Pulling configured images, if needed"$EBOLD
509
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200510START_ARG_MOD=$START_ARG
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200511__check_image_local_override 'PA'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200512if [ $? -eq 1 ]; then
513 START_ARG_MOD="local"
514fi
515app="Policy Agent"; __check_and_pull_image $START_ARG_MOD "$app" $POLICY_AGENT_APP_NAME $POLICY_AGENT_IMAGE
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100516
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200517START_ARG_MOD=$START_ARG
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200518__check_image_local_override 'CP'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200519if [ $? -eq 1 ]; then
520 START_ARG_MOD="local"
521fi
522app="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 +0200523
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200524START_ARG_MOD=$START_ARG
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200525__check_image_local_override 'RICSIM'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200526if [ $? -eq 1 ]; then
527 START_ARG_MOD="local"
528fi
529app="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 +0100530
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200531app="Consul"; __check_and_pull_image $START_ARG "$app" $CONSUL_APP_NAME $CONSUL_IMAGE
532app="CBS"; __check_and_pull_image $START_ARG "$app" $CBS_APP_NAME $CBS_IMAGE
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200533__check_excluded_image 'SDNC'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200534if [ $? -eq 0 ]; then
535 START_ARG_MOD=$START_ARG
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200536 __check_image_local_override 'SDNC'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200537 if [ $? -eq 1 ]; then
538 START_ARG_MOD="local"
539 fi
540 app="SDNC A1 Controller"; __check_and_pull_image $START_ARG_MOD "$app" $SDNC_APP_NAME $SDNC_A1_CONTROLLER_IMAGE
541 app="SDNC DB"; __check_and_pull_image $START_ARG "$app" $SDNC_APP_NAME $SDNC_DB_IMAGE
542else
543 echo -e $YELLOW" Excluding SDNC image and related DB image from image check/pull"$EYELLOW
544fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200545__check_excluded_image 'SDNC_ONAP'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200546if [ $? -eq 0 ]; then
547 app="SDNC ONAP A1 Adapter"; __check_and_pull_image $START_ARG "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_A1_ADAPTER_IMAGE
548 app="SDNC ONAP DB"; __check_and_pull_image $START_ARG "$app" $SDNC_ONAP_APP_NAME $SDNC_ONAP_DB_IMAGE
549else
550 echo -e $YELLOW" Excluding ONAP SDNC image and related DB image from image check/pull"$EYELLOW
551fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100552# MR stub image not checked, will be built by this script - only local image
553# CR stub image not checked, will be built by this script - only local image
554
555
556#Errors in image setting - exit
557if [ $IMAGE_ERR -ne 0 ]; then
558 echo ""
559 echo "#################################################################################################"
560 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 +0200561 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100562 echo "#################################################################################################"
563 echo ""
564 exit 1
565fi
566
567echo ""
568
569echo -e $BOLD"Building images needed for test"$EBOLD
570
571curdir=$PWD
572cd $curdir
573cd ../mrstub
574echo " Building mrstub image: mrstub:latest"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200575docker build -t mrstub . &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100576if [ $? -eq 0 ]; then
577 echo -e $GREEN" Build Ok"$EGREEN
578else
579 echo -e $RED" Build Failed"$ERED
580 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200581 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100582fi
583cd $curdir
584
585cd ../cr
586echo " Building Callback Receiver image: callback-receiver:latest"
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200587docker build -t callback-receiver . &> .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100588if [ $? -eq 0 ]; then
589 echo -e $GREEN" Build Ok"$EGREEN
590else
591 echo -e $RED" Build Failed"$ERED
592 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200593 cat .dockererr
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100594fi
YongchaoWuf309b1b2020-01-22 13:24:48 +0100595cd $curdir
596
597echo ""
598
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100599# Create a table of the images used in the script
600echo -e $BOLD"Local docker registry images used in the this test script"$EBOLD
601
602docker_tmp_file=.docker-images-table
603format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}"
604echo -e " Application\tRepository\tTag\tCreated Since\tSize" > $docker_tmp_file
605echo -e " Policy Agent\t$(docker images --format $format_string $POLICY_AGENT_IMAGE)" >> $docker_tmp_file
606echo -e " Control Panel\t$(docker images --format $format_string $CONTROL_PANEL_IMAGE)" >> $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100607echo -e " RIC Simulator\t$(docker images --format $format_string $RIC_SIM_IMAGE)" >> $docker_tmp_file
608echo -e " Message Router\t$(docker images --format $format_string $MRSTUB_IMAGE)" >> $docker_tmp_file
609echo -e " Callback Receiver\t$(docker images --format $format_string $CR_IMAGE)" >> $docker_tmp_file
610echo -e " Consul\t$(docker images --format $format_string $CONSUL_IMAGE)" >> $docker_tmp_file
611echo -e " CBS\t$(docker images --format $format_string $CBS_IMAGE)" >> $docker_tmp_file
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200612__check_excluded_image 'SDNC'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200613if [ $? -eq 0 ]; then
614 echo -e " SDNC A1 Controller\t$(docker images --format $format_string $SDNC_A1_CONTROLLER_IMAGE)" >> $docker_tmp_file
615 echo -e " SDNC DB\t$(docker images --format $format_string $SDNC_DB_IMAGE)" >> $docker_tmp_file
616fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200617__check_excluded_image 'SDNC_ONAP'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200618if [ $? -eq 0 ]; then
619 echo -e " SDNC ONAP A1 Adapter\t$(docker images --format $format_string $SDNC_ONAP_A1_ADAPTER_IMAGE)" >> $docker_tmp_file
620 echo -e " SDNC ONAP DB\t$(docker images --format $format_string $SDNC_ONAP_DB_IMAGE)" >> $docker_tmp_file
621fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100622
623column -t -s $'\t' $docker_tmp_file
624
YongchaoWuf309b1b2020-01-22 13:24:48 +0100625echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100626
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100627echo -e $BOLD"======================================================="$EBOLD
628echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
629echo -e $BOLD"======================================================="$EBOLD
630echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +0100631
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100632# Function to print the test result, shall be the last cmd in a test script
633# args: -
634# (Function for test scripts)
635print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +0100636
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100637 TCTEST_END=$SECONDS
638 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +0100639
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100640 echo "-------------------------------------------------------------------------------------------------"
641 echo "------------------------------------- Test case: "$ATC
642 echo "------------------------------------- Ended: "$(date)
643 echo "-------------------------------------------------------------------------------------------------"
644 echo "-- Description: "$TC_ONELINE_DESCR
645 echo "-- Execution time: " $duration " seconds"
646 echo "-------------------------------------------------------------------------------------------------"
647 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +0100648 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +0100649
YongchaoWu21f17bb2020-03-05 12:44:08 +0100650
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200651 if [ $RES_DEVIATION -gt 0 ]; then
652 echo "Test case deviations"
653 echo "===================================="
654 cat $DEVIATION_FILE
655 fi
656 echo ""
657 echo "Timer measurement in the test script"
658 echo "===================================="
659 column -t -s $'\t' $TIMER_MEASUREMENTS
660 echo ""
661
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100662 total=$((RES_PASS+RES_FAIL))
663 if [ $RES_TEST -eq 0 ]; then
664 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
665 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
666 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
667 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
668 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
669 elif [ $total != $RES_TEST ]; then
670 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
671 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
672 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
673 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
674 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
675 elif [ $RES_CONF_FAIL -ne 0 ]; then
676 echo -e "\033[1mOne or more configure regest has failed. Check the script log....\033[0m"
677 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
678 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
679 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
680 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
681 elif [ $RES_PASS = $RES_TEST ]; then
682 echo -e "All tests \033[32m\033[1mPASS\033[0m"
683 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
684 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
685 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
686 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
687 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +0100688
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100689 # Update test suite counter
690 if [ -f .tmp_tcsuite_pass_ctr ]; then
691 tmpval=$(< .tmp_tcsuite_pass_ctr)
692 ((tmpval++))
693 echo $tmpval > .tmp_tcsuite_pass_ctr
694 fi
695 if [ -f .tmp_tcsuite_pass ]; then
696 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
697 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200698 #Create file with OK exit code
699 echo "0" > "$PWD/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100700 else
701 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
702 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
703 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
704 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
705 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
706 echo ""
707 # Update test suite counter
708 if [ -f .tmp_tcsuite_fail_ctr ]; then
709 tmpval=$(< .tmp_tcsuite_fail_ctr)
710 ((tmpval++))
711 echo $tmpval > .tmp_tcsuite_fail_ctr
712 fi
713 if [ -f .tmp_tcsuite_fail ]; then
714 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
715 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +0100716 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +0100717
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100718 echo "++++ Number of tests: "$RES_TEST
719 echo "++++ Number of passed tests: "$RES_PASS
720 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +0100721 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100722 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200723 echo ""
724 echo "++++ Number of test case deviations: "$RES_DEVIATION
725 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100726 echo "------------------------------------- Test case complete ---------------------------------"
727 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +0100728 echo ""
729}
730
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100731#####################################################################
732###### Functions for start, configuring, stoping, cleaning etc ######
733#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +0100734
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200735# Start timer for time measurement
736# args - (any args will be printed though)
737start_timer() {
738 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
739 TC_TIMER=$SECONDS
740 echo " Timer started"
741}
742
743# Print the value of the time (in seconds)
744# args - <timer message to print> - timer value and message will be printed both on screen
745# and in the timer measurement report
746print_timer() {
747 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
748 if [ $# -lt 1 ]; then
749 ((RES_CONF_FAIL++))
750 __print_err "need 1 or more args, <timer message to print>" $@
751 exit 1
752 fi
753 duration=$(($SECONDS-$TC_TIMER))
754 if [ $duration -eq 0 ]; then
755 duration="<1 second"
756 else
757 duration=$duration" seconds"
758 fi
759 echo " Timer duration :" $duration
760
761 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
762}
763
764# Print the value of the time (in seconds) and reset the timer
765# args - <timer message to print> - timer value and message will be printed both on screen
766# and in the timer measurement report
767print_and_reset_timer() {
768 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
769 if [ $# -lt 1 ]; then
770 ((RES_CONF_FAIL++))
771 __print_err "need 1 or more args, <timer message to print>" $@
772 exit 1
773 fi
774 duration=$(($SECONDS-$TC_TIMER))" seconds"
775 if [ $duration -eq 0 ]; then
776 duration="<1 second"
777 else
778 duration=$duration" seconds"
779 fi
780 echo " Timer duration :" $duration
781 TC_TIMER=$SECONDS
782 echo " Timer reset"
783
784 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
785
786}
787# Print info about a deviations from intended tests
788# Each deviation counted is also printed in the testreport
789# args <deviation message to print>
790deviation() {
791 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
792 if [ $# -lt 1 ]; then
793 ((RES_CONF_FAIL++))
794 __print_err "need 1 or more args, <deviation message to print>" $@
795 exit 1
796 fi
797 ((RES_DEVIATION++))
798 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
799 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
800 echo ""
801}
YongchaoWu21f17bb2020-03-05 12:44:08 +0100802
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200803# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
804__check_stop_at_error() {
805 if [ $STOP_AT_ERROR -eq 1 ]; then
806 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
807 store_logs "STOP_AT_ERROR"
808 exit 1
809 fi
810 return 0
811}
812
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100813# Stop and remove all containers
814# args: -
815# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +0100816clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100817
818 echo -e $BOLD"Stopping and removing all running containers, by container name"$EBOLD
819
820 CONTAINTER_NAMES=("Policy Agent " $POLICY_AGENT_APP_NAME\
821 "Non-RT RIC Simulator(s)" $RIC_SIM_PREFIX\
822 "Message Router " $MR_APP_NAME\
823 "Callback Receiver " $CR_APP_NAME\
824 "Control Panel " $CONTROL_PANEL_APP_NAME\
825 "SDNC A1 Controller " $SDNC_APP_NAME\
826 "SDNC DB " $SDNC_DB_APP_NAME\
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200827 "SDNC ONAP A1 Adapter " $SDNC_ONAP_APP_NAME\
828 "SDNC DB " $SDNC_ONAP_DB_APP_NAME\
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100829 "CBS " $CBS_APP_NAME\
830 "Consul " $CONSUL_APP_NAME)
831
832 nw=0 # Calc max width of container name, to make a nice table
833 for (( i=1; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
834 if [ ${#CONTAINTER_NAMES[i]} -gt $nw ]; then
835 nw=${#CONTAINTER_NAMES[i]}
836 fi
837 done
838
839 for (( i=0; i<${#CONTAINTER_NAMES[@]} ; i+=2 )) ; do
840 APP="${CONTAINTER_NAMES[i]}"
841 CONTR="${CONTAINTER_NAMES[i+1]}"
842 for((w=${#CONTR}; w<$nw; w=w+1)); do
843 CONTR="$CONTR "
844 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200845 echo -ne " $APP: $CONTR - ${GREEN}stopping${EGREEN}${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100846 docker stop $(docker ps -qa --filter name=${CONTR}) &> /dev/null
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200847 echo -ne " $APP: $CONTR - ${GREEN}stopped${EGREEN}${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200848 docker rm --force $(docker ps -qa --filter name=${CONTR}) &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100849 echo -e " $APP: $CONTR - ${GREEN}stopped removed${EGREEN}"
850 done
851
YongchaoWu9a84f512019-12-16 22:54:11 +0100852 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200853
854 echo -e $BOLD" Removing docker network"$EBOLD
855 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
856 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
857 docker network rm $DOCKER_SIM_NWNAME
858 if [ $? -ne 0 ]; then
859 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
860 exit 1
861 fi
862 fi
863
864 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
865 docker network prune --force #&> /dev/null
866
867 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
868 docker volume prune --force #&> /dev/null
869
870 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
871 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
872 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +0200873
874 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
875 if [ $? -eq 0 ]; then
876 if [ $CONTRS -ne 0 ]; then
877 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
878 docker ps -a
879 fi
880 fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100881}
882
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100883# Function stop and remove all container in the end of the test script, if the arg 'auto-clean' is given at test script start
884# args: -
885# (Function for test scripts)
886auto_clean_containers() {
887 echo
888 if [ "$AUTO_CLEAN" == "auto" ]; then
889 echo -e $BOLD"Initiating automatic cleaning of started containers"$EBOLD
890 clean_containers
YongchaoWu9a84f512019-12-16 22:54:11 +0100891 fi
892}
893
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100894# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200895# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100896# (Function for test scripts)
897sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +0100898
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100899 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
900 if [ $# -lt 1 ]; then
901 ((RES_CONF_FAIL++))
902 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
903 exit 1
904 fi
905 #echo "---- Sleep for " $1 " seconds ---- "$2
906 start=$SECONDS
907 duration=$((SECONDS-start))
908 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200909 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100910 sleep 1
911 duration=$((SECONDS-start))
912 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200913 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100914 echo ""
915}
916
917# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
918# Not to be called from the test script itself.
919__print_err() {
920 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
921 if [ $# -gt 1 ]; then
922 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
923 fi
924}
925
926
927# Helper function to get a the port of a specific ric simulatpor
928# args: <ric-id>
929# (Not for test scripts)
930__find_sim_port() {
931 name=$1" " #Space appended to prevent matching 10 if 1 is desired....
ecaiyanlinux99a769b2020-05-15 13:58:02 +0200932 cmdstr="docker inspect --format='{{(index (index .NetworkSettings.Ports \"$RIC_SIM_PORT/tcp\") 0).HostPort}}' ${name}"
933 res=$(eval $cmdstr)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100934 if [[ "$res" =~ ^[0-9]+$ ]]; then
935 echo $res
936 else
937 echo "0"
938 fi
939}
940
941# Function to create the docker network for the test
942# Not to be called from the test script itself.
943__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200944 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100945 if [ $? -ne 0 ]; then
946 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
947 return 1
948 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200949 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100950 echo -e "Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
951 docker network create $DOCKER_SIM_NWNAME
952 if [ $? -ne 0 ]; then
953 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
954 return 1
955 fi
956 else
957 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
958 fi
959}
960
961# Check if container is started by calling url on localhost using a port, expects response code 2XX
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200962# args: <container-name> <port> <url> https|https
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100963# Not to be called from the test script itself.
964__check_container_start() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200965 paramError=0
966 if [ $# -ne 4 ]; then
967 paramError=1
968 elif [ $4 != "http" ] && [ $4 != "https" ]; then
969 paramError=1
970 fi
971 if [ $paramError -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100972 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200973 __print_err "need 3 args, <container-name> <port> <url> https|https" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100974 return 1
975 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200976 echo -ne " Container $BOLD$1$EBOLD starting${SAMELINE}"
YongchaoWu9a84f512019-12-16 22:54:11 +0100977 appname=$1
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100978 localport=$2
979 url=$3
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200980 if [[ $appname != "STANDALONE_"* ]] ; then
981 app_started=0
982 for i in {1..10}; do
983 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
984 echo -e " Container $BOLD$1$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
985 app_started=1
986 break
987 else
988 sleep $i
989 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100990 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200991 if [ $app_started -eq 0 ]; then
992 ((RES_CONF_FAIL++))
993 echo ""
994 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
995 return 1
996 fi
997 if [ $localport -eq 0 ]; then
998 while [ $localport -eq 0 ]; do
999 echo -ne " Waiting for container ${appname} to publish its ports...${SAMELINE}"
1000 localport=$(__find_sim_port $appname)
1001 sleep 1
1002 echo -ne " Waiting for container ${appname} to publish its ports...retrying....${SAMELINE}"
1003 done
1004 echo -ne " Waiting for container ${appname} to publish its ports...retrying....$GREEN OK $EGREEN"
1005 echo ""
1006 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01001007 fi
1008
1009 pa_st=false
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001010 echo -ne " Waiting for container ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001011 TSTART=$SECONDS
1012 for i in {1..50}; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001013 if [ $4 == "https" ]; then
1014 result="$(__do_curl "-k https://localhost:"${localport}${url})"
1015 else
1016 result="$(__do_curl $LOCALHOST${localport}${url})"
1017 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01001018 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001019 if [ ${#result} -gt 15 ]; then
1020 #If response is too long, truncate
1021 result="...response text too long, omitted"
1022 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001023 echo -ne " Waiting for container $BOLD${appname}$EBOLD service status, result: $result${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001024 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 +01001025 pa_st=true
1026 break
1027 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001028 TS_TMP=$SECONDS
1029 while [ $(($TS_TMP+$i)) -gt $SECONDS ]; do
1030 echo -ne " Waiting for container ${appname} service status...retrying in $(($TS_TMP+$i-$SECONDS)) seconds ${SAMELINE}"
1031 sleep 1
1032 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001033 fi
1034 done
1035
1036 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001037 ((RES_CONF_FAIL++))
1038 echo -e $RED" Container ${appname} did not respond to service status"$ERED
1039 return 0
1040 fi
1041
1042 echo ""
1043 return 0
1044}
1045
1046
1047# Function to start a container and wait until it responds on the given port and url.
1048#args: <docker-compose-dir> NODOCKERARGS|<docker-compose-arg> <app-name> <port-number> <alive-url> [<app-name> <port-number> <alive-url>]*
1049__start_container() {
1050
1051 variableArgCount=$(($#-2))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001052 if [ $# -lt 6 ] && [ [ $(($variableArgCount%4)) -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001053 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001054 __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 +01001055 exit 1
1056 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001057
1058 __create_docker_network
1059
1060 curdir=$PWD
1061 cd $SIM_GROUP
1062 cd $1
1063
1064 if [ "$2" == "NODOCKERARGS" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001065 docker-compose up -d &> .dockererr
1066 if [ $? -ne 0 ]; then
1067 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
1068 cat .dockererr
1069 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001070 elif [ "$2" == "STANDALONE" ]; then
1071 echo "Skipping docker-compose"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001072 else
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001073 docker-compose up -d $2 &> .dockererr
1074 if [ $? -ne 0 ]; then
1075 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
1076 cat .dockererr
1077 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001078 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001079 app_prefix=""
1080 if [ "$2" == "STANDALONE" ]; then
1081 app_prefix="STANDALONE_"
1082 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001083 shift; shift;
1084 cntr=0
1085 while [ $cntr -lt $variableArgCount ]; do
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001086 app=$app_prefix$1; shift;
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001087 port=$1; shift;
1088 url=$1; shift;
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001089 httpx=$1; shift;
1090 let cntr=cntr+4
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001091
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001092 __check_container_start "$app" "$port" "$url" $httpx
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001093 done
1094
1095 cd $curdir
1096 echo ""
1097 return 0
YongchaoWu9a84f512019-12-16 22:54:11 +01001098}
1099
BjornMagnussonXAad047782020-06-08 15:54:11 +02001100# Generate a UUID to use as prefix for policy ids
1101generate_uuid() {
1102 UUID=$(python3 -c 'import sys,uuid; sys.stdout.write(uuid.uuid4().hex)')
1103 #Reduce length to make space for serial id, us 'a' as marker where the serial id is added
1104 UUID=${UUID:0:${#UUID}-4}"a"
1105}
1106
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001107####################
1108### Consul functions
1109####################
1110
1111# Function to load config from a file into consul for the Policy Agent
1112# arg: <json-config-file>
1113# (Function for test scripts)
1114consul_config_app() {
1115
1116 echo -e $BOLD"Configuring Consul"$EBOLD
1117
1118 if [ $# -ne 1 ]; then
1119 ((RES_CONF_FAIL++))
1120 __print_err "need one arg, <json-config-file>" $@
1121 exit 1
1122 fi
1123
1124 echo " Loading config for "$POLICY_AGENT_APP_NAME" from "$1
1125
1126 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
1127 if [ $? -ne 0 ]; then
1128 echo -e $RED" FAIL - json config could not be loaded to consul" $ERED
1129 ((RES_CONF_FAIL++))
1130 return 1
1131 fi
1132 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001133 echo $body > ".output"$1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001134
1135 if [ $? -ne 0 ]; then
1136 echo -e $RED" FAIL - json config could not be loaded from consul/cbs, contents cannot be checked." $ERED
1137 ((RES_CONF_FAIL++))
1138 return 1
1139 else
1140 targetJson=$(< $1)
1141 targetJson="{\"config\":"$targetJson"}"
1142 echo "TARGET JSON: $targetJson" >> $HTTPLOG
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001143 res=$(python3 ../common/compare_json.py "$targetJson" "$body")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001144 if [ $res -ne 0 ]; then
1145 echo -e $RED" FAIL - policy json config read from consul/cbs is not equal to the intended json config...." $ERED
1146 ((RES_CONF_FAIL++))
1147 return 1
1148 else
1149 echo -e $GREEN" Config loaded ok to consul"$EGREEN
1150 fi
1151 fi
1152
1153 echo ""
1154
1155}
1156
1157# Function to perpare the consul configuration according to the current simulator configuration
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001158# args: SDNC|SDNC_ONAP|NOSDNC <output-file>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001159# (Function for test scripts)
1160prepare_consul_config() {
1161 echo -e $BOLD"Prepare Consul config"$EBOLD
1162
1163 echo " Writing consul config for "$POLICY_AGENT_APP_NAME" to file: "$2
1164
1165 if [ $# != 2 ]; then
1166 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001167 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001168 exit 1
1169 fi
1170
1171 if [ $1 == "SDNC" ]; then
1172 echo -e " Config$BOLD including SDNC$EBOLD configuration"
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001173 elif [ $1 == "SDNC_ONAP" ]; then
1174 echo -e " Config$BOLD including SDNC ONAP$EBOLD configuration"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001175 elif [ $1 == "NOSDNC" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001176 echo -e " Config$BOLD excluding SDNC or SDNC ONAP$EBOLD configuration"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001177 else
1178 ((RES_CONF_FAIL++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001179 __print_err "need two args, SDNC|SDNC_ONAP|NOSDNC <output-file>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001180 exit 1
1181 fi
1182
1183 config_json="\n {"
1184 if [ $1 == "SDNC" ]; then
1185 config_json=$config_json"\n \"controller\": ["
1186 config_json=$config_json"\n {"
1187 config_json=$config_json"\n \"name\": \"$SDNC_APP_NAME\","
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001188 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1189 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://$SDNC_APP_NAME:$SDNC_PORT\","
1190 else
1191 config_json=$config_json"\n \"baseUrl\": \"$SDNC_HTTPX://localhost:$SDNC_LOCAL_PORT\","
1192 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001193 config_json=$config_json"\n \"userName\": \"$SDNC_USER\","
1194 config_json=$config_json"\n \"password\": \"$SDNC_PWD\""
1195 config_json=$config_json"\n }"
1196 config_json=$config_json"\n ],"
1197 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001198 if [ $1 == "SDNC_ONAP" ]; then
1199 config_json=$config_json"\n \"controller\": ["
1200 config_json=$config_json"\n {"
1201 config_json=$config_json"\n \"name\": \"$SDNC_ONAP_APP_NAME\","
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001202 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1203 config_json=$config_json"\n \"baseUrl\": \"http://$SDNC_ONAP_APP_NAME:$SDNC_ONAP_INTERNAL_PORT\","
1204 else
1205 config_json=$config_json"\n \"baseUrl\": \"http://localhost:$SDNC_ONAP_EXTERNAL_PORT\","
1206 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001207 config_json=$config_json"\n \"userName\": \"$SDNC_ONAP_USER\","
1208 config_json=$config_json"\n \"password\": \"$SDNC_ONAP_PWD\""
1209 config_json=$config_json"\n }"
1210 config_json=$config_json"\n ],"
1211 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001212
1213
1214 config_json=$config_json"\n \"streams_publishes\": {"
1215 config_json=$config_json"\n \"dmaap_publisher\": {"
1216 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1217 config_json=$config_json"\n \"dmaap_info\": {"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001218 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1219 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_WRITE_URL\""
1220 else
1221 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_WRITE_URL\""
1222 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001223 config_json=$config_json"\n }"
1224 config_json=$config_json"\n }"
1225 config_json=$config_json"\n },"
1226 config_json=$config_json"\n \"streams_subscribes\": {"
1227 config_json=$config_json"\n \"dmaap_subscriber\": {"
1228 config_json=$config_json"\n \"type\": \"$MR_APP_NAME\","
1229 config_json=$config_json"\n \"dmaap_info\": {"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001230 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1231 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://$MR_APP_NAME:$MR_PORT$MR_READ_URL\""
1232 else
1233 config_json=$config_json"\n \"topic_url\": \"$MR_HTTPX://localhost:$MR_LOCAL_PORT$MR_READ_URL\""
1234 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001235 config_json=$config_json"\n }"
1236 config_json=$config_json"\n }"
1237 config_json=$config_json"\n },"
1238
1239 config_json=$config_json"\n \"ric\": ["
1240
BjornMagnussonXAad047782020-06-08 15:54:11 +02001241 rics=$(docker ps | grep $RIC_SIM_PREFIX | awk '{print $NF}')
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001242
1243 if [ $? -ne 0 ] || [ -z "$rics" ]; then
1244 echo -e $RED" FAIL - the names of the running RIC Simulator cannot be retrieved." $ERED
1245 ((RES_CONF_FAIL++))
1246 return 1
1247 fi
1248
1249 cntr=0
1250 for ric in $rics; do
1251 if [ $cntr -gt 0 ]; then
1252 config_json=$config_json"\n ,"
1253 fi
1254 config_json=$config_json"\n {"
1255 config_json=$config_json"\n \"name\": \"$ric\","
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001256 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1257 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://$ric:$RIC_SIM_PORT\","
1258 else
1259 config_json=$config_json"\n \"baseUrl\": \"$RIC_SIM_HTTPX://localhost:$(__find_sim_port $ric)\","
1260 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001261 if [ $1 == "SDNC" ]; then
1262 config_json=$config_json"\n \"controller\": \"$SDNC_APP_NAME\","
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001263 elif [ $1 == "SDNC_ONAP" ]; then
1264 config_json=$config_json"\n \"controller\": \"$SDNC_ONAP_APP_NAME\","
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001265 fi
1266 config_json=$config_json"\n \"managedElementIds\": ["
1267 config_json=$config_json"\n \"me1_$ric\","
1268 config_json=$config_json"\n \"me2_$ric\""
1269 config_json=$config_json"\n ]"
1270 config_json=$config_json"\n }"
1271 let cntr=cntr+1
1272 done
1273
1274 config_json=$config_json"\n ]"
1275 config_json=$config_json"\n}"
1276
1277
1278 printf "$config_json">$2
1279
1280 echo ""
1281}
1282
1283
1284# Start Consul and CBS
1285# args: -
1286# (Function for test scripts)
1287start_consul_cbs() {
1288
1289 echo -e $BOLD"Starting Consul and CBS"$EBOLD
1290
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001291 __start_container consul_cbs NODOCKERARGS "$CONSUL_APP_NAME" "$CONSUL_EXTERNAL_PORT" "/ui/dc1/kv" "http" \
1292 "$CBS_APP_NAME" "$CBS_EXTERNAL_PORT" "/healthcheck" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001293}
1294
1295###########################
1296### RIC Simulator functions
1297###########################
1298
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001299use_simulator_http() {
1300 echo -e "Using unsecure $BOLD http $EBOLD towards the simulators"
1301 export RIC_SIM_HTTPX="http"
1302 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1303 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_PORT
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001304 echo ""
1305}
1306
1307use_simulator_https() {
1308 echo -e "Using secure $BOLD https $EBOLD towards the simulators"
1309 export RIC_SIM_HTTPX="https"
1310 export RIC_SIM_LOCALHOST=$RIC_SIM_HTTPX"://localhost:"
1311 export RIC_SIM_PORT=$RIC_SIM_INTERNAL_SECURE_PORT
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001312 echo ""
1313}
1314
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001315# Start one group (ricsim_g1, ricsim_g2 or ricsim_g3) with a number of RIC Simulators using a given A interface
BjornMagnussonXAad047782020-06-08 15:54:11 +02001316# 'ricsim' may be set on command line to other prefix
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001317# args: ricsim_g1|ricsim_g2|ricsim_g3 <count> <interface-id>
1318# (Function for test scripts)
1319start_ric_simulators() {
1320
1321 echo -e $BOLD"Starting RIC Simulators"$EBOLD
1322
BjornMagnussonXAad047782020-06-08 15:54:11 +02001323 RIC1=$RIC_SIM_PREFIX"_g1"
1324 RIC2=$RIC_SIM_PREFIX"_g2"
1325 RIC3=$RIC_SIM_PREFIX"_g3"
1326
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001327 if [ $# != 3 ]; then
1328 ((RES_CONF_FAIL++))
BjornMagnussonXAad047782020-06-08 15:54:11 +02001329 __print_err "need three args, $RIC1|$RIC2|$RIC3 <count> <interface-id>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001330 exit 1
1331 fi
1332 echo " $2 simulators using basename: $1 on interface: $3"
1333 #Set env var for simulator count and A1 interface vesion for the given group
BjornMagnussonXAad047782020-06-08 15:54:11 +02001334 if [ $1 == "$RIC1" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001335 G1_COUNT=$2
1336 G1_A1_VERSION=$3
BjornMagnussonXAad047782020-06-08 15:54:11 +02001337 elif [ $1 == "$RIC2" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001338 G2_COUNT=$2
1339 G2_A1_VERSION=$3
BjornMagnussonXAad047782020-06-08 15:54:11 +02001340 elif [ $1 == "$RIC3" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001341 G3_COUNT=$2
1342 G3_A1_VERSION=$3
1343 else
1344 ((RES_CONF_FAIL++))
BjornMagnussonXAad047782020-06-08 15:54:11 +02001345 __print_err "need three args, $RIC1|$RIC2|$RIC3 <count> <interface-id>" $@
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001346 exit 1
1347 fi
1348
1349 # Create .env file to compose project, all ric container will get this prefix
1350 echo "COMPOSE_PROJECT_NAME="$RIC_SIM_PREFIX > $SIM_GROUP/ric/.env
1351
1352 export G1_A1_VERSION
1353 export G2_A1_VERSION
1354 export G3_A1_VERSION
1355
1356 docker_args="--scale g1=$G1_COUNT --scale g2=$G2_COUNT --scale g3=$G3_COUNT"
1357 app_data=""
1358 cntr=1
1359 while [ $cntr -le $2 ]; do
1360 app=$1"_"$cntr
1361 port=0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001362 app_data="$app_data $app $port / "$RIC_SIM_HTTPX
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001363 let cntr=cntr+1
1364 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001365 __start_container ric "$docker_args" $app_data
1366
1367}
1368
1369###########################
1370### Control Panel functions
1371###########################
1372
1373# Start the Control Panel container
1374# args: -
1375# (Function for test scripts)
1376start_control_panel() {
1377
1378 echo -e $BOLD"Starting Control Panel"$EBOLD
1379
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001380 __start_container control_panel NODOCKERARGS $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001381
1382}
1383
1384##################
1385### SDNC functions
1386##################
1387
1388# Start the SDNC A1 Controller
1389# args: -
1390# (Function for test scripts)
1391start_sdnc() {
1392
1393 echo -e $BOLD"Starting SDNC A1 Controller"$EBOLD
1394
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001395 __check_excluded_image 'SDNC'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001396 if [ $? -eq 1 ]; then
1397 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
1398 echo -e $RED"SDNC will not be started"$ERED
1399 exit
1400 fi
1401
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001402 __start_container sdnc NODOCKERARGS $SDNC_APP_NAME $SDNC_EXTERNAL_PORT $SDNC_ALIVE_URL "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001403
1404}
1405
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001406use_sdnc_http() {
1407 echo -e $BOLD"Using http between agent and SDNC"$EBOLD
1408 export SDNC_HTTPX="http"
1409 export SDNC_PORT=$SDNC_INTERNAL_PORT
1410 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_PORT
1411 echo ""
1412}
1413
1414use_sdnc_https() {
1415 echo -e $BOLD"Using https between agent and SDNC"$EBOLD
1416 export SDNC_HTTPX="https"
1417 export SDNC_PORT=$SDNC_INTERNAL_SECURE_PORT
1418 export SDNC_LOCAL_PORT=$SDNC_EXTERNAL_SECURE_PORT
1419 echo ""
1420}
1421
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001422#######################
1423### SDNC ONAP functions
1424#######################
1425
1426# Start the SDNC ONAP A1 Adapter
1427# args: -
1428# (Function for test scripts)
1429start_sdnc_onap() {
1430
1431 echo -e $BOLD"Starting SDNC ONAP A1 Adapter"$EBOLD
1432
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001433 __check_excluded_image 'SDNC_ONAP'
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001434 if [ $? -eq 1 ]; then
1435 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
1436 echo -e $RED"SDNC ONAP will not be started"$ERED
1437 exit
1438 fi
1439
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001440 __start_container sdnc_onap NODOCKERARGS $SDNC_ONAP_APP_NAME $SDNC_ONAP_EXTERNAL_PORT $SDNC_ONAP_ALIVE_URL "http"
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001441
1442}
1443
1444# Configure the SDNC ONAP A1 Adapter
1445# args: -
1446# (Function for test scripts)
1447config_sdnc_onap() {
1448
1449 echo -e $BOLD"Configuring SDNC ONAP A1 Adapter"$EBOLD
1450
1451 LOCALFILE=".sdnc_onap.prop"
1452 REMOTEFILE="/tmp/.sdnc_onap.prop"
1453
1454 docker cp $SDNC_ONAP_APP_NAME:$SDNC_ONAP_PROPERTIES_FILE $LOCALFILE
1455 if [ $? -ne 0 ]; then
1456 echo -e $RED"Could not copy $SDNC_ONAP_PROPERTIES_FILE from $SDNC_ONAP_APP_NAME container"$ERED
1457 exit 1
1458 fi
1459
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001460
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001461 #Config of the prop file shall be inserted here
1462
1463 #Copy file to /tmp and then to final destination, a trick to get correct permission of the file.
1464
1465 docker cp $LOCALFILE $SDNC_ONAP_APP_NAME:$REMOTEFILE
1466 if [ $? -ne 0 ]; then
1467 echo -e $RED"Could not copy local $LOCALFILE to $REMOTEFILE in $SDNC_ONAP_APP_NAME container"$ERED
1468 exit 1
1469 fi
1470
1471 docker exec -it $SDNC_ONAP_APP_NAME cp $REMOTEFILE $SDNC_ONAP_PROPERTIES_FILE
1472 if [ $? -ne 0 ]; then
1473 echo -e $RED"Could not copy $REMOTEFILE to $SDNC_ONAP_PROPERTIES_FILE in $SDNC_ONAP_APP_NAME container"$ERED
1474 exit 1
1475 fi
1476}
1477
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001478#####################
1479### MR stub functions
1480#####################
1481
1482# Start the Message Router stub interface in the simulator group
1483# args: -
1484# (Function for test scripts)
1485start_mr() {
1486
1487 echo -e $BOLD"Starting Message Router 'mrstub'"$EBOLD
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001488 export MR_CERT_MOUNT_DIR="./cert"
1489 __start_container mr NODOCKERARGS $MR_APP_NAME $MR_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001490}
1491
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001492use_mr_http() {
1493 echo -e $BOLD"Using http between agent and MR"$EBOLD
1494 export MR_HTTPX="http"
1495 export MR_PORT=$MR_INTERNAL_PORT
1496 export MR_LOCAL_PORT=$MR_EXTERNAL_PORT
1497 echo ""
1498}
1499
1500use_mr_https() {
1501 echo -e $BOLD"Using https between agent and MR"$EBOLD
1502 export MR_HTTPX="https"
1503 export MR_PORT=$MR_INTERNAL_SECURE_PORT
1504 export MR_LOCAL_PORT=$MR_EXTERNAL_SECURE_PORT
1505 echo ""
1506}
1507
1508
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001509################
1510### CR functions
1511################
1512
1513# Start the Callback reciver in the simulator group
1514# args: -
1515# (Function for test scripts)
1516start_cr() {
1517
1518 echo -e $BOLD"Starting Callback Receiver"$EBOLD
1519
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001520 __start_container cr NODOCKERARGS $CR_APP_NAME $CR_EXTERNAL_PORT "/" "http"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001521
1522}
1523
BjornMagnussonXA496156d2020-08-10 14:16:24 +02001524use_cr_http() {
1525 echo -e $BOLD"Using http between test script and CR"$EBOLD
1526 export CR_HTTPX="http"
1527 export CR_PORT=$CR_INTERNAL_PORT
1528 export CR_LOCAL_PORT=$CR_EXTERNAL_PORT
1529 echo ""
1530}
1531
1532use_cr_https() {
1533 echo -e $BOLD"Using https between test script and CR"$EBOLD
1534 export CR_HTTPX="https"
1535 export CR_PORT=$CR_INTERNAL_SECURE_PORT
1536 export CR_LOCAL_PORT=$CR_EXTERNAL_SECURE_PORT
1537 echo ""
1538}
1539
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001540###########################
1541### Policy Agents functions
1542###########################
1543
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001544# Use an agent on the local machine instead of container
1545use_agent_stand_alone() {
1546 AGENT_STAND_ALONE=1
1547}
1548
1549# Start the policy agent
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001550# args: -
1551# (Function for test scripts)
1552start_policy_agent() {
1553
1554 echo -e $BOLD"Starting Policy Agent"$EBOLD
1555
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001556 if [ $AGENT_STAND_ALONE -eq 0 ]; then
1557 __start_container policy_agent NODOCKERARGS $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1558 else
1559 echo -e $RED"The consul config produced by this test script (filename '<fullpath-to-autotest-dir>.output<file-name>"$ERED
1560 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
1561 echo -e $RED"application.yaml"$ERED
1562 echo -e $RED"The application jar may need to be built beforefor continuing"$ERED
1563 echo -e $RED"The agent shall now be running on port $POLICY_AGENT_EXTERNAL_PORT for http"$ERED
1564
1565 read -p "<press any key to continue>"
1566 __start_container policy_agent "STANDALONE" $POLICY_AGENT_APP_NAME $POLICY_AGENT_EXTERNAL_PORT "/status" "http"
1567 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001568
1569}
1570
1571# All calls to the agent will be directed to the agent REST interface from now on
1572# args: -
1573# (Function for test scripts)
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001574use_agent_rest_http() {
1575 echo -e $BOLD"Using agent REST interface with http"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001576 export ADAPTER=$RESTBASE
1577 echo ""
1578}
1579
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001580# All calls to the agent will be directed to the agent REST interface from now on
1581# args: -
1582# (Function for test scripts)
1583use_agent_rest_https() {
1584 echo -e $BOLD"Using agent REST interface with https"$EBOLD
1585 export ADAPTER=$RESTBASE_SECURE
1586 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001587 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001588}
1589
BjornMagnussonXA496156d2020-08-10 14:16:24 +02001590# All calls to the agent will be directed to the agent dmaap interface over http from now on
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001591# args: -
1592# (Function for test scripts)
BjornMagnussonXA496156d2020-08-10 14:16:24 +02001593use_agent_dmaap_http() {
1594 echo -e $BOLD"Agent using DMAAP http interface"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001595 export ADAPTER=$DMAAPBASE
1596 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001597 return 0
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001598}
1599
BjornMagnussonXA496156d2020-08-10 14:16:24 +02001600# All calls to the agent will be directed to the agent dmaap interface over https from now on
1601# args: -
1602# (Function for test scripts)
1603use_agent_dmaap_https() {
1604 echo -e $BOLD"Agent using DMAAP https interface"$EBOLD
1605 export ADAPTER=$DMAAPBASE_SECURE
1606 echo ""
1607 return 0
1608}
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001609
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001610# Turn on debug level tracing in the agent
1611# args: -
1612# (Function for test scripts)
1613set_agent_debug() {
1614 echo -e $BOLD"Setting agent debug"$EBOLD
1615 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"debug"}' &> /dev/null
1616 if [ $? -ne 0 ]; then
1617 __print_err "could not set debug mode" $@
1618 return 1
1619 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001620 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001621 return 0
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001622}
1623
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001624# Turn on trace level tracing in the agent
1625# args: -
1626# (Function for test scripts)
1627set_agent_trace() {
1628 echo -e $BOLD"Setting agent trace"$EBOLD
1629 curl $LOCALHOST$POLICY_AGENT_EXTERNAL_PORT/actuator/loggers/org.oransc.policyagent -X POST -H 'Content-Type: application/json' -d '{"configuredLevel":"trace"}' &> /dev/null
1630 if [ $? -ne 0 ]; then
1631 __print_err "could not set trace mode" $@
1632 return 1
1633 fi
1634 echo ""
1635 return 0
1636}
1637
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001638# Perform curl retries when making direct call to the agent for the specified http response codes
1639# Speace separated list of http response codes
1640# args: [<response-code>]*
1641use_agent_retries() {
1642 echo -e $BOLD"Do curl retries to the agent REST inteface for these response codes:$@"$EBOLD
1643 AGENT_RETRY_CODES=$@
1644 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001645 return
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001646}
1647
1648#################
1649### Log functions
1650#################
1651
1652# Check the agent logs for WARNINGs and ERRORs
1653# args: -
1654# (Function for test scripts)
1655
YongchaoWu9a84f512019-12-16 22:54:11 +01001656check_policy_agent_logs() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001657 __check_container_logs "Policy Agent" $POLICY_AGENT_APP_NAME $POLICY_AGENT_LOGPATH
YongchaoWu9a84f512019-12-16 22:54:11 +01001658}
1659
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001660check_control_panel_logs() {
1661 __check_container_logs "Control Panel" $CONTROL_PANEL_APP_NAME $CONTROL_PANEL_LOGPATH
YongchaoWu9a84f512019-12-16 22:54:11 +01001662}
1663
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001664__check_container_logs() {
1665 dispname=$1
1666 appname=$2
1667 logpath=$3
1668 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
1669
1670 #tmp=$(docker ps | grep $appname)
1671 tmp=$(docker ps -q --filter name=$appname) #get the container id
1672 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
1673 echo $dispname" is not running, no check made"
1674 return
1675 fi
1676 foundentries="$(docker exec -it $tmp grep WARN $logpath | wc -l)"
1677 if [ $? -ne 0 ];then
1678 echo " Problem to search $appname log $logpath"
1679 else
1680 if [ $foundentries -eq 0 ]; then
1681 echo " No WARN entries found in $appname log $logpath"
1682 else
1683 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
1684 fi
1685 fi
1686 foundentries="$(docker exec -it $tmp grep ERR $logpath | wc -l)"
1687 if [ $? -ne 0 ];then
1688 echo " Problem to search $appname log $logpath"
1689 else
1690 if [ $foundentries -eq 0 ]; then
1691 echo " No ERR entries found in $appname log $logpath"
1692 else
1693 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
1694 fi
1695 fi
1696 echo ""
1697}
1698
1699# Store all container logs and other logs in the log dir for the script
1700# Logs are stored with a prefix in case logs should be stored several times during a test
1701# args: <logfile-prefix>
1702# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01001703store_logs() {
1704 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001705 ((RES_CONF_FAIL++))
1706 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01001707 exit 1
1708 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001709 echo -e $BOLD"Storing all container logs, Policy Agent app log and consul config using prefix: "$1$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +01001710
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001711 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001712 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_consul.log 2>&1
1713 docker logs $CBS_APP_NAME > $TESTLOGS/$ATC/$1_cbs.log 2>&1
1714 docker logs $POLICY_AGENT_APP_NAME > $TESTLOGS/$ATC/$1_policy-agent.log 2>&1
1715 docker logs $CONSUL_APP_NAME > $TESTLOGS/$ATC/$1_control-panel.log 2>&1
1716 docker logs $MR_APP_NAME > $TESTLOGS/$ATC/$1_mr.log 2>&1
1717 docker logs $CR_APP_NAME > $TESTLOGS/$ATC/$1_cr.log 2>&1
1718 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
1719
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001720 docker exec -it $SDNC_APP_NAME cat $SDNC_KARAF_LOG> $TESTLOGS/$ATC/$1_SDNC_karaf.log 2>&1
1721
1722 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 +01001723
1724 rics=$(docker ps -f "name=$RIC_SIM_PREFIX" --format "{{.Names}}")
1725 for ric in $rics; do
1726 docker logs $ric > $TESTLOGS/$ATC/$1_$ric.log 2>&1
1727 done
1728 body="$(__do_curl $LOCALHOST$CBS_EXTERNAL_PORT/service_component_all/$POLICY_AGENT_APP_NAME)"
1729 echo "$body" > $TESTLOGS/$ATC/$1_consul_config.json 2>&1
1730 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01001731}
1732
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001733###############
1734## Generic curl
1735###############
1736# Generic curl function, assumed all 200-codes are ok
1737# args: <url>
1738# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
1739# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01001740__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001741 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
1742 curlString="curl -skw %{http_code} $1"
1743 echo " CMD: $curlString" >> $HTTPLOG
1744 res=$($curlString)
1745 echo " RESP: $res" >> $HTTPLOG
YongchaoWu9a84f512019-12-16 22:54:11 +01001746 http_code="${res:${#res}-3}"
1747 if [ ${#res} -eq 3 ]; then
1748 echo "<no-response-from-server>"
1749 return 1
1750 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001751 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01001752 echo "<not found, resp:${http_code}>"
1753 return 1
1754 fi
1755 if [ $# -eq 2 ]; then
1756 echo "${res:0:${#res}-3}" | xargs
1757 else
1758 echo "${res:0:${#res}-3}"
1759 fi
1760
1761 return 0
1762 fi
1763}
1764
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001765#######################################
1766### Basic helper function for test cases
1767#######################################
1768
1769# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
1770# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
1771# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
1772# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
1773# before setting pass or fail depending on the result of comparing variable and target using the operator.
1774# 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.
1775# Not to be called from test script.
1776
1777__var_test() {
1778 checkjsonarraycount=0
1779
1780 if [ $# -eq 6 ]; then
1781 if [[ $3 == "json:"* ]]; then
1782 checkjsonarraycount=1
1783 fi
1784
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001785 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001786 ((RES_TEST++))
1787 start=$SECONDS
1788 ctr=0
1789 for (( ; ; )); do
1790 if [ $checkjsonarraycount -eq 0 ]; then
1791 result="$(__do_curl $2$3)"
1792 retcode=$?
1793 result=${result//[[:blank:]]/} #Strip blanks
1794 else
1795 path=${3:5}
1796 result="$(__do_curl $2$path)"
1797 retcode=$?
1798 echo "$result" > .tmp.curl.json
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001799 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001800 fi
1801 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001802 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001803 let ctr=ctr+1
1804 if [ $retcode -ne 0 ]; then
1805 if [ $duration -gt $6 ]; then
1806 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001807 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001808 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001809 return
1810 fi
1811 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1812 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001813 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001814 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001815 return
1816 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1817 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001818 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001819 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001820 return
1821 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1822 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001823 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001824 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001825 return
1826 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1827 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001828 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001829 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001830 return
1831 else
1832 if [ $duration -gt $6 ]; then
1833 ((RES_FAIL++))
1834 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001835 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001836 return
1837 fi
1838 fi
1839 sleep 1
1840 done
1841 elif [ $# -eq 5 ]; then
1842 if [[ $3 == "json:"* ]]; then
1843 checkjsonarraycount=1
1844 fi
1845
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001846 echo -e $BOLD"TEST(${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
1847 ((RES_TEST++))
1848 if [ $checkjsonarraycount -eq 0 ]; then
1849 result="$(__do_curl $2$3)"
1850 retcode=$?
1851 result=${result//[[:blank:]]/} #Strip blanks
1852 else
1853 path=${3:5}
1854 result="$(__do_curl $2$path)"
1855 retcode=$?
1856 echo "$result" > .tmp.curl.json
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001857 result=$(python3 ../common/count_json_elements.py ".tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001858 fi
1859 if [ $retcode -ne 0 ]; then
1860 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001861 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001862 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001863 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
1864 ((RES_PASS++))
1865 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001866 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
1867 ((RES_PASS++))
1868 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001869 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
1870 ((RES_PASS++))
1871 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001872 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
1873 ((RES_PASS++))
1874 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001875 else
1876 ((RES_FAIL++))
1877 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001878 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001879 fi
1880 else
1881 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
1882 echo "Got:" $@
1883 exit 1
1884 fi
1885}
1886
1887
1888### Generic test cases for varaible checking
1889
1890# Tests if a variable value in the CR is equal to a target value and and optional timeout.
1891# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1892# equal to the target or not.
1893# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1894# before setting pass or fail depending on if the variable value becomes equal to the target
1895# value or not.
1896# (Function for test scripts)
1897cr_equal() {
1898 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1899 __var_test "CR" "$LOCALHOST$CR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1900 else
1901 ((RES_CONF_FAIL++))
1902 __print_err "Wrong args to cr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1903 fi
1904}
1905
1906# Tests if a variable value in the MR stub is equal to a target value and and optional timeout.
1907# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1908# equal to the target or not.
1909# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1910# before setting pass or fail depending on if the variable value becomes equal to the target
1911# value or not.
1912# (Function for test scripts)
1913mr_equal() {
1914 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
1915 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 "=" $2 $3
1916 else
1917 ((RES_CONF_FAIL++))
1918 __print_err "Wrong args to mr_equal, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1919 fi
1920}
1921
1922# Tests if a variable value in the MR stub is greater than a target value and and optional timeout.
1923# Arg: <variable-name> <target-value> - This test set pass or fail depending on if the variable is
1924# greater than the target or not.
1925# Arg: <variable-name> <target-value> <timeout-in-sec> - This test waits up to the timeout seconds
1926# before setting pass or fail depending on if the variable value becomes greater than the target
1927# value or not.
1928# (Function for test scripts)
1929mr_greater() {
1930 if [ $# -eq 2 ] || [ $# -eq 3 ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001931 __var_test "MR" "$LOCALHOST$MR_EXTERNAL_PORT/counter/" $1 ">" $2 $3
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001932 else
1933 ((RES_CONF_FAIL++))
1934 __print_err "Wrong args to mr_greater, needs two or three args: <sim-param> <target-value> [ timeout ]" $@
1935 fi
1936}
1937
1938# Read a variable value from MR sim and send to stdout. Arg: <variable-name>
1939mr_read() {
1940 echo "$(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"
1941}
1942
1943# Print a variable value from the MR stub.
1944# arg: <variable-name>
1945# (Function for test scripts)
1946mr_print() {
1947 if [ $# != 1 ]; then
1948 ((RES_CONF_FAIL++))
1949 __print_err "need one arg, <mr-param>" $@
1950 exit 1
1951 fi
1952 echo -e $BOLD"INFO(${BASH_LINENO[0]}): mrstub, $1 = $(__do_curl $LOCALHOST$MR_EXTERNAL_PORT/counter/$1)"$EBOLD
1953}