blob: da2abf5e4aa3ac09758a52bec4cd8e8c353ed424 [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
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010020# This is a script that contains all the common functions needed for auto test.
21# Specific test function are defined in scripts XXXX_functions.sh
22
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010023. ../common/api_curl.sh
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010024. ../common/testengine_config.sh
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010025
26__print_args() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010027 echo "Args: remote|remote-remove docker|kube --env-file <environment-filename> [release] [auto-clean] [--stop-at-error] "
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010028 echo " [--ricsim-prefix <prefix> ] [--use-local-image <app-nam>+] [--use-snapshot-image <app-nam>+]"
BjornMagnussonXA483ee332021-04-08 01:35:24 +020029 echo " [--use-staging-image <app-nam>+] [--use-release-image <app-nam>+] [--image-repo <repo-address]"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +020030 echo " [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>] [--print-stats]"
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010031 echo " [--override <override-environment-filename> --pre-clean --gen-stats]"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010032}
33
34if [ $# -eq 1 ] && [ "$1" == "help" ]; then
35
36 if [ ! -z "$TC_ONELINE_DESCR" ]; then
37 echo "Test script description:"
38 echo $TC_ONELINE_DESCR
39 echo ""
40 fi
41 __print_args
42 echo ""
43 echo "remote - Use images from remote repositories. Can be overridden for individual images using the '--use_xxx' flags"
44 echo "remote-remove - Same as 'remote' but will also try to pull fresh images from remote repositories"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010045 echo "docker - Test executed in docker environment"
46 echo "kube - Test executed in kubernetes environment - requires an already started kubernetes environment"
BjornMagnussonXA663566c2021-11-08 10:25:07 +010047 echo "--env-file <file> - The script will use the supplied file to read environment variables from"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010048 echo "release - If this flag is given the script will use release version of the images"
49 echo "auto-clean - If the function 'auto_clean_containers' is present in the end of the test script then all containers will be stopped and removed. If 'auto-clean' is not given then the function has no effect."
50 echo "--stop-at-error - The script will stop when the first failed test or configuration"
51 echo "--ricsim-prefix - The a1 simulator will use the supplied string as container prefix instead of 'ricsim'"
52 echo "--use-local-image - The script will use local images for the supplied apps, space separated list of app short names"
53 echo "--use-snapshot-image - The script will use images from the nexus snapshot repo for the supplied apps, space separated list of app short names"
54 echo "--use-staging-image - The script will use images from the nexus staging repo for the supplied apps, space separated list of app short names"
55 echo "--use-release-image - The script will use images from the nexus release repo for the supplied apps, space separated list of app short names"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +020056 echo "--image-repo - Url to optional image repo. Only locally built images will be re-tagged and pushed to this repo"
BjornMagnussonXA674793d2021-05-06 19:49:17 +020057 echo "--repo-policy - Policy controlling which images to re-tag and push if param --image-repo is set. Default is 'local'"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +020058 echo "--cluster-timeout - Optional timeout for cluster where it takes time to obtain external ip/host-name. Timeout in seconds. "
BjornMagnussonXAfec823b2021-08-03 14:14:05 +020059 echo "--print-stats - Print current test stats after each test."
BjornMagnussonXA663566c2021-11-08 10:25:07 +010060 echo "--override <file> - Override setting from the file supplied by --env-file"
61 echo "--pre-clean - Will clean kube resouces when running docker and vice versa"
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010062 echo "--gen-stats - Collect container/pod runtime statistics"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +020063
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010064 echo ""
65 echo "List of app short names supported: "$APP_SHORT_NAMES
66 exit 0
67fi
68
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010069AUTOTEST_HOME=$PWD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020070# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
71# FTC1.sh -> ATC == FTC1
72ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
73
74#Create result file (containing '1' for error) for this test case
75#Will be replaced with a file containing '0' if all test cases pass
76echo "1" > "$PWD/.result$ATC.txt"
77
BjornMagnussonXA80a92002020-03-19 14:31:06 +010078#Formatting for 'echo' cmd
79BOLD="\033[1m"
80EBOLD="\033[0m"
81RED="\033[31m\033[1m"
82ERED="\033[0m"
83GREEN="\033[32m\033[1m"
84EGREEN="\033[0m"
85YELLOW="\033[33m\033[1m"
86EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020087SAMELINE="\033[0K\r"
88
BjornMagnussonXA80a92002020-03-19 14:31:06 +010089# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020090echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +010091
BjornMagnussonXA575869c2020-09-14 21:28:54 +020092# default test environment variables
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020093TEST_ENV_VAR_FILE=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +010094#Override env file, will be added on top of the above file
95TEST_ENV_VAR_FILE_OVERRIDE=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +010096
97echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
98
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010099#Localhost constants
100LOCALHOST_NAME="localhost"
101LOCALHOST_HTTP="http://localhost"
102LOCALHOST_HTTPS="https://localhost"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200103
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100104# Var to hold 'auto' in case containers shall be stopped when test case ends
105AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +0100106
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100107# Var to indicate pre clean, if flag --pre-clean is set the script will clean kube resouces when running docker and vice versa
108PRE_CLEAN="0"
109
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100110# Var to hold the app names to use local images for
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200111USE_LOCAL_IMAGES=""
112
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100113# Var to hold the app names to use remote snapshot images for
114USE_SNAPSHOT_IMAGES=""
115
116# Var to hold the app names to use remote staging images for
117USE_STAGING_IMAGES=""
118
119# Var to hold the app names to use remote release images for
120USE_RELEASE_IMAGES=""
121
BjornMagnussonXAad047782020-06-08 15:54:11 +0200122
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200123# 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
124STOP_AT_ERROR=0
125
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100126# The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used.
127# The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used
128# Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE
129IMAGE_CATEGORY="DEV"
130
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100131#Var to indicate docker-compose version, V1 or V2
132#V1 names replicated containers <proj-name>_<service-name>_<index>
133#V2 names replicated containers <proj-name>-<service-name>-<index>
134DOCKER_COMPOSE_VERION="V1"
135
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200136# Function to indent cmd output with one space
137indent1() { sed 's/^/ /'; }
138
139# Function to indent cmd output with two spaces
140indent2() { sed 's/^/ /'; }
141
YongchaoWu9a84f512019-12-16 22:54:11 +0100142# Set a description string for the test case
143if [ -z "$TC_ONELINE_DESCR" ]; then
144 TC_ONELINE_DESCR="<no-description>"
145 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
146fi
147
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100148# Counter for test suites
149if [ -f .tmp_tcsuite_ctr ]; then
150 tmpval=$(< .tmp_tcsuite_ctr)
151 ((tmpval++))
152 echo $tmpval > .tmp_tcsuite_ctr
153fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100154
YongchaoWu9a84f512019-12-16 22:54:11 +0100155# Create the logs dir if not already created in the current dir
156if [ ! -d "logs" ]; then
157 mkdir logs
158fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100159TESTLOGS=$PWD/logs
160
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200161# Create the tmp dir for temporary files that is not needed after the test
162# hidden files for the test env is still stored in the current dir
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100163# files in the ./tmp is moved to ./tmp/prev when a new test is started
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200164if [ ! -d "tmp" ]; then
165 mkdir tmp
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100166 if [ $? -ne 0 ]; then
167 echo "Cannot create dir for temp files, $PWD/tmp"
168 echo "Exiting...."
169 exit 1
170 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200171fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100172curdir=$PWD
173cd tmp
174if [ $? -ne 0 ]; then
175 echo "Cannot cd to $PWD/tmp"
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100176 echo "Exiting...."
177 exit 1
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100178fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100179
180TESTENV_TEMP_FILES=$PWD
181
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100182if [ ! -d "prev" ]; then
183 mkdir prev
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100184 if [ $? -ne 0 ]; then
185 echo "Cannot create dir for previous temp files, $PWD/prev"
186 echo "Exiting...."
187 exit 1
188 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100189fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100190
191TMPFILES=$(ls -A | grep -vw prev)
192if [ ! -z "$TMPFILES" ]; then
193 cp -r $TMPFILES prev #Move all temp files to prev dir
194 if [ $? -ne 0 ]; then
195 echo "Cannot move temp files in $PWD to previous temp files in, $PWD/prev"
196 echo "Exiting...."
197 exit 1
198 fi
199 if [ $(pwd | xargs basename) == "tmp" ]; then #Check that current dir is tmp...for safety
200
201 rm -rf $TMPFILES # Remove all temp files
202 fi
203fi
204
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100205cd $curdir
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100206if [ $? -ne 0 ]; then
207 echo "Cannot cd to $curdir"
208 echo "Exiting...."
209 exit 1
210fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200211
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100212# Create a http message log for this testcase
213HTTPLOG=$PWD"/.httplog_"$ATC".txt"
214echo "" > $HTTPLOG
215
216# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100217mkdir -p $TESTLOGS/$ATC
218
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100219# Save create for current logs
220mkdir -p $TESTLOGS/$ATC/previous
221
222rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
223rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
224rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
225
226mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
227mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
228mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
229
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100230# Clear the log dir for the test case
231rm $TESTLOGS/$ATC/*.log &> /dev/null
232rm $TESTLOGS/$ATC/*.txt &> /dev/null
233rm $TESTLOGS/$ATC/*.json &> /dev/null
234
235# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100236TCLOG=$TESTLOGS/$ATC/TC.log
237exec &> >(tee ${TCLOG})
238
239#Variables for counting tests as well as passed and failed tests
240RES_TEST=0
241RES_PASS=0
242RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100243RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200244RES_DEVIATION=0
245
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200246#Var to control if current stats shall be printed
247PRINT_CURRENT_STATS=0
248
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100249#Var to control if container/pod runtim statistics shall be collected
250COLLECT_RUNTIME_STATS=0
251
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200252#File to keep deviation messages
253DEVIATION_FILE=".tmp_deviations"
254rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100255
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100256# Trap "command not found" and make the script fail
257trap_fnc() {
258
259 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100260 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100261 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200262 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100263 fi
264}
265trap trap_fnc ERR
266
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100267# Trap to kill subprocesses
268trap "kill 0" EXIT
269
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100270# Counter for tests
271TEST_SEQUENCE_NR=1
272
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100273# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100274__log_test_start() {
275 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
276 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
277 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
278 ((RES_TEST++))
279 ((TEST_SEQUENCE_NR++))
280}
281
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200282# Function to print current statistics
283__print_current_stats() {
284 if [ $PRINT_CURRENT_STATS -ne 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200285 echo " Current stats - exe-time, tests, passes, fails, conf fails, deviations: $(($SECONDS-$TCTEST_START)), $RES_TEST, $RES_PASS, $RES_FAIL, $RES_CONF_FAIL, $RES_DEVIATION"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200286 fi
287}
288
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100289# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100290__log_test_fail_general() {
291 echo -e $RED" FAIL."$1 $ERED
292 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200293 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100294 __check_stop_at_error
295}
296
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100297# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100298__log_test_fail_status_code() {
299 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
300 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200301 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100302 __check_stop_at_error
303}
304
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100305# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100306__log_test_fail_body() {
307 echo -e $RED" FAIL, returned body not correct"$ERED
308 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200309 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100310 __check_stop_at_error
311}
312
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100313# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100314__log_test_fail_not_supported() {
315 echo -e $RED" FAIL, function not supported"$ERED
316 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200317 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100318 __check_stop_at_error
319}
320
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100321# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100322__log_test_pass() {
323 if [ $# -gt 0 ]; then
324 echo $@
325 fi
326 ((RES_PASS++))
327 echo -e $GREEN" PASS"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200328 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100329}
330
331#Counter for configurations
332CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100333
334# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100335__log_conf_start() {
336 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
337 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
338 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
339 ((CONF_SEQUENCE_NR++))
340}
341
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100342# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100343__log_conf_fail_general() {
344 echo -e $RED" FAIL."$1 $ERED
345 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200346 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100347 __check_stop_at_error
348}
349
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100350# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100351__log_conf_fail_status_code() {
352 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
353 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200354 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100355 __check_stop_at_error
356}
357
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100358# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100359__log_conf_fail_body() {
360 echo -e $RED" FAIL, returned body not correct"$ERED
361 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200362 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100363 __check_stop_at_error
364}
365
BjornMagnussonXA83a750f2021-09-21 20:39:58 +0200366# Function to log a configuration that is not supported
367__log_conf_fail_not_supported() {
368 echo -e $RED" FAIL, function not supported"$ERED$@
369 ((RES_CONF_FAIL++))
370 __print_current_stats
371 __check_stop_at_error
372}
373
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100374# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100375__log_conf_ok() {
376 if [ $# -gt 0 ]; then
377 echo $@
378 fi
379 echo -e $GREEN" OK"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200380 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100381}
382
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100383#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100384TCTEST_START=$SECONDS
385
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100386#Vars to hold the start time and timer text for a custom timer
387TC_TIMER_STARTTIME=""
388TC_TIMER_TIMER_TEXT=""
389TC_TIMER_CURRENT_FAILS="" # Then numer of failed test when timer starts.
390 # Compared with the current number of fails at timer stop
391 # to judge the measurement reliability
392
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200393#File to save timer measurement results
394TIMER_MEASUREMENTS=".timer_measurement.txt"
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100395echo -e "Activity \t Duration \t Info" > $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200396
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200397# If this is set, some images (control by the parameter repo-polcy) will be re-tagged and pushed to this repo before any
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200398IMAGE_REPO_ADR=""
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200399IMAGE_REPO_POLICY="local"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200400CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200401
YongchaoWu9a84f512019-12-16 22:54:11 +0100402echo "-------------------------------------------------------------------------------------------------"
403echo "----------------------------------- Test case: "$ATC
404echo "----------------------------------- Started: "$(date)
405echo "-------------------------------------------------------------------------------------------------"
406echo "-- Description: "$TC_ONELINE_DESCR
407echo "-------------------------------------------------------------------------------------------------"
408echo "----------------------------------- Test case setup -----------------------------------"
409
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100410echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200411START_ARG=$1
412paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100413paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200414if [ $# -lt 1 ]; then
415 paramerror=1
416fi
417if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100418 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200419 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100420 if [ -z "$paramerror_str" ]; then
421 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
422 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200423 else
424 shift;
425 fi
426fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100427if [ $paramerror -eq 0 ]; then
428 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
429 paramerror=1
430 if [ -z "$paramerror_str" ]; then
431 paramerror_str="Second arg shall be 'docker' or 'kube'"
432 fi
433 else
434 if [ $1 == "docker" ]; then
435 RUNMODE="DOCKER"
436 echo "Setting RUNMODE=DOCKER"
437 fi
438 if [ $1 == "kube" ]; then
439 RUNMODE="KUBE"
440 echo "Setting RUNMODE=KUBE"
441 fi
442 shift;
443 fi
444fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200445foundparm=0
446while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
447 foundparm=1
448 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100449 if [ "$1" == "release" ]; then
450 IMAGE_CATEGORY="RELEASE"
451 echo "Option set - Release image tags used for applicable images "
452 shift;
453 foundparm=0
454 fi
455 fi
456 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200457 if [ "$1" == "auto-clean" ]; then
458 AUTO_CLEAN="auto"
459 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200460 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200461 foundparm=0
462 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200463 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200464 if [ $paramerror -eq 0 ]; then
465 if [ "$1" == "--stop-at-error" ]; then
466 STOP_AT_ERROR=1
467 echo "Option set - Stop at first error"
468 shift;
469 foundparm=0
470 fi
471 fi
472 if [ $paramerror -eq 0 ]; then
473 if [ "$1" == "--ricsim-prefix" ]; then
474 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100475 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200476 if [ -z "$1" ]; then
477 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100478 if [ -z "$paramerror_str" ]; then
479 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
480 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200481 else
482 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
483 shift;
484 foundparm=0
485 fi
486 fi
487 fi
488 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200489 if [ "$1" == "--env-file" ]; then
490 shift;
491 TEST_ENV_VAR_FILE=$1
492 if [ -z "$1" ]; then
493 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100494 if [ -z "$paramerror_str" ]; then
495 paramerror_str="No env file found for flag: '--env-file'"
496 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200497 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200498 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200499 shift;
500 foundparm=0
501 fi
502 fi
503 fi
504 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200505 if [ "$1" == "--use-local-image" ]; then
506 USE_LOCAL_IMAGES=""
507 shift
508 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
509 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100510 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200511 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100512 if [ -z "$paramerror_str" ]; then
513 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
514 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200515 fi
516 shift;
517 done
518 foundparm=0
519 if [ -z "$USE_LOCAL_IMAGES" ]; then
520 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100521 if [ -z "$paramerror_str" ]; then
522 paramerror_str="No app name found for flag: '--use-local-image'"
523 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200524 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100525 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
526 fi
527 fi
528 fi
529 if [ $paramerror -eq 0 ]; then
530 if [ "$1" == "--use-snapshot-image" ]; then
531 USE_SNAPSHOT_IMAGES=""
532 shift
533 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
534 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
535 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
536 paramerror=1
537 if [ -z "$paramerror_str" ]; then
538 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
539 fi
540 fi
541 shift;
542 done
543 foundparm=0
544 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
545 paramerror=1
546 if [ -z "$paramerror_str" ]; then
547 paramerror_str="No app name found for flag: '--use-snapshot-image'"
548 fi
549 else
550 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
551 fi
552 fi
553 fi
554 if [ $paramerror -eq 0 ]; then
555 if [ "$1" == "--use-staging-image" ]; then
556 USE_STAGING_IMAGES=""
557 shift
558 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
559 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
560 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
561 paramerror=1
562 if [ -z "$paramerror_str" ]; then
563 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
564 fi
565 fi
566 shift;
567 done
568 foundparm=0
569 if [ -z "$USE_STAGING_IMAGES" ]; then
570 paramerror=1
571 if [ -z "$paramerror_str" ]; then
572 paramerror_str="No app name found for flag: '--use-staging-image'"
573 fi
574 else
575 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
576 fi
577 fi
578 fi
579 if [ $paramerror -eq 0 ]; then
580 if [ "$1" == "--use-release-image" ]; then
581 USE_RELEASE_IMAGES=""
582 shift
583 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
584 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
585 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
586 paramerror=1
587 if [ -z "$paramerror_str" ]; then
588 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
589 fi
590 fi
591 shift;
592 done
593 foundparm=0
594 if [ -z "$USE_RELEASE_IMAGES" ]; then
595 paramerror=1
596 if [ -z "$paramerror_str" ]; then
597 paramerror_str="No app name found for flag: '--use-release-image'"
598 fi
599 else
600 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200601 fi
602 fi
603 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200604 if [ $paramerror -eq 0 ]; then
605 if [ "$1" == "--image-repo" ]; then
606 shift;
607 IMAGE_REPO_ADR=$1
608 if [ -z "$1" ]; then
609 paramerror=1
610 if [ -z "$paramerror_str" ]; then
611 paramerror_str="No image repo url found for : '--image-repo'"
612 fi
613 else
614 echo "Option set - Image repo url: "$1
615 shift;
616 foundparm=0
617 fi
618 fi
619 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200620 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200621 if [ "$1" == "--repo-policy" ]; then
622 shift;
623 IMAGE_REPO_POLICY=$1
624 if [ -z "$1" ]; then
625 paramerror=1
626 if [ -z "$paramerror_str" ]; then
627 paramerror_str="No policy found for : '--repo-policy'"
628 fi
629 else
630 if [ "$1" == "local" ] || [ "$1" == "remote" ]; then
631 echo "Option set - Image repo policy: "$1
632 shift;
633 foundparm=0
634 else
635 paramerror=1
636 if [ -z "$paramerror_str" ]; then
637 paramerror_str="Repo policy shall be 'local' or 'remote'"
638 fi
639 fi
640 fi
641 fi
642 fi
643 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200644 if [ "$1" == "--cluster-timeout" ]; then
645 shift;
646 CLUSTER_TIME_OUT=$1
647 if [ -z "$1" ]; then
648 paramerror=1
649 if [ -z "$paramerror_str" ]; then
650 paramerror_str="No timeout value found for : '--cluster-timeout'"
651 fi
652 else
653 #Check if positive int
654 case ${CLUSTER_TIME_OUT#[+]} in
655 *[!0-9]* | '')
656 paramerror=1
657 if [ -z "$paramerror_str" ]; then
658 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
659 fi
660 ;;
661 * ) ;; # Ok
662 esac
663 echo "Option set - Cluster timeout: "$1
664 shift;
665 foundparm=0
666 fi
667 fi
668 fi
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200669 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100670 if [ "$1" == "--override" ]; then
671 shift;
672 TEST_ENV_VAR_FILE_OVERRIDE=$1
673 if [ -z "$1" ]; then
674 paramerror=1
675 if [ -z "$paramerror_str" ]; then
676 paramerror_str="No env file found for flag: '--override'"
677 fi
678 else
679 if [ ! -f $TEST_ENV_VAR_FILE_OVERRIDE ]; then
680 paramerror=1
681 if [ -z "$paramerror_str" ]; then
682 paramerror_str="File for '--override' does not exist : "$TEST_ENV_VAR_FILE_OVERRIDE
683 fi
684 fi
685 echo "Option set - Override env from: "$1
686 shift;
687 foundparm=0
688 fi
689 fi
690 fi
691 if [ $paramerror -eq 0 ]; then
692 if [ "$1" == "--pre-clean" ]; then
693 PRE_CLEAN=1
694 echo "Option set - Pre-clean of kube/docker resouces"
695 shift;
696 foundparm=0
697 fi
698 fi
699 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200700 if [ "$1" == "--print-stats" ]; then
701 PRINT_CURRENT_STATS=1
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100702 echo "Option set - Print stats after every test-case and config"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200703 shift;
704 foundparm=0
705 fi
706 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100707 if [ $paramerror -eq 0 ]; then
708 if [ "$1" == "--gen-stats" ]; then
709 COLLECT_RUNTIME_STATS=1
710 echo "Option set - Collect runtime statistics"
711 shift;
712 foundparm=0
713 fi
714 fi
715
BjornMagnussonXAad047782020-06-08 15:54:11 +0200716done
717echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200718
BjornMagnussonXAad047782020-06-08 15:54:11 +0200719#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200720if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
721 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100722 if [ -z "$paramerror_str" ]; then
723 paramerror_str="Unknown parameter(s): "$@
724 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200725fi
726
727if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100728 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
729 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200730 exit 1
731fi
732
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200733# sourcing the selected env variables for the test case
734if [ -f "$TEST_ENV_VAR_FILE" ]; then
735 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
736 . $TEST_ENV_VAR_FILE
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100737 if [ ! -z "$TEST_ENV_VAR_FILE_OVERRIDE" ]; then
738 echo -e $BOLD"Sourcing override env vars from: "$TEST_ENV_VAR_FILE_OVERRIDE$EBOLD
739 . $TEST_ENV_VAR_FILE_OVERRIDE
740 fi
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100741
742 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100743 echo -e $YELLOW"This test case may not work with selected test env file. TEST_ENV_PROFILE is missing in test_env file or SUPPORTED_PROFILES is missing in test case file"$EYELLOW
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100744 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100745 found_profile=0
746 for prof in $SUPPORTED_PROFILES; do
747 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
748 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
749 found_profile=1
750 fi
751 done
752 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100753 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100754 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100755 echo -e $RED"Exiting...."$ERED
756 exit 1
757 fi
758 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200759else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200760 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200761 echo " Select one of following env var file matching the intended target of the test"
762 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100763 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200764 exit 1
765fi
766
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100767#This var need be preserved from the command line option, if set, when env var is sourced.
768if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
769 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
770fi
771
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100772if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100773 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100774 exit 1
775fi
776
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100777if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
778 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
779 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
780 exit 1
781fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100782
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100783# Choose list of included apps depending on run-mode
784if [ $RUNMODE == "KUBE" ]; then
785 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
786else
787 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
788fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200789
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100790echo ""
791# auto adding system apps
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100792__added_apps=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100793echo -e $BOLD"Auto adding system apps"$EBOLD
794if [ $RUNMODE == "KUBE" ]; then
795 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_KUBE_SYSTEM_APPS
796 TMP_APPS=$TESTENV_KUBE_SYSTEM_APPS
797else
798 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_DOCKER_SYSTEM_APPS
799 TMP_APPS=$TESTENV_DOCKER_SYSTEM_APPS
800fi
801if [ ! -z "$TMP_APPS" ]; then
802 for iapp in "$TMP_APPS"; do
803 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
804 file_pointer="../common/"$file_pointer"_api_functions.sh"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100805 padded_iapp=$iapp
806 while [ ${#padded_iapp} -lt 16 ]; do
807 padded_iapp=$padded_iapp" "
808 done
809 echo " Auto-adding system app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100810 . $file_pointer
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100811 if [ $? -ne 0 ]; then
812 echo " Include file $file_pointer contain errors. Exiting..."
813 exit 1
814 fi
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100815 __added_apps=" $iapp "$__added_apps
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100816 done
817else
818 echo " None"
819fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100820
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100821if [ $RUNMODE == "KUBE" ]; then
822 TMP_APPS=$INCLUDED_IMAGES" "$KUBE_PRESTARTED_IMAGES
823else
824 TMP_APPS=$INCLUDED_IMAGES
825fi
826
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100827echo -e $BOLD"Auto adding included apps"$EBOLD
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100828 for iapp in $TMP_APPS; do
829 if [[ "$__added_apps" != *"$iapp"* ]]; then
830 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
831 file_pointer="../common/"$file_pointer"_api_functions.sh"
832 padded_iapp=$iapp
833 while [ ${#padded_iapp} -lt 16 ]; do
834 padded_iapp=$padded_iapp" "
835 done
836 echo " Auto-adding included app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100837 if [ ! -f "$file_pointer" ]; then
838 echo " Include file $file_pointer for app $iapp does not exist"
839 exit 1
840 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100841 . $file_pointer
842 if [ $? -ne 0 ]; then
843 echo " Include file $file_pointer contain errors. Exiting..."
844 exit 1
845 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100846 fi
847 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100848echo ""
849
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100850echo -e $BOLD"Test environment info"$EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100851
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100852# Check needed installed sw
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100853
854tmp=$(which bash)
855if [ $? -ne 0 ] || [ -z "$tmp" ]; then
856 echo -e $RED"bash is required to run the test environment, pls install"$ERED
857 exit 1
858fi
859echo " bash is installed and using version:"
860echo "$(bash --version)" | indent2
861
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100862tmp=$(which python3)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100863if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100864 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
865 exit 1
866fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100867echo " python3 is installed and using version: $(python3 --version)"
868
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100869tmp=$(which docker)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100870if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100871 echo -e $RED"docker is required to run the test environment, pls install"$ERED
872 exit 1
873fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100874echo " docker is installed and using versions:"
875echo " $(docker version --format 'Client version {{.Client.Version}} Server version {{.Server.Version}}')"
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200876
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100877tmp=$(which docker-compose)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100878if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100879 if [ $RUNMODE == "DOCKER" ]; then
880 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
881 exit 1
882 fi
883fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100884tmp=$(docker-compose version --short)
885echo " docker-compose installed and using version $tmp"
886if [[ "$tmp" == *'v2'* ]]; then
887 DOCKER_COMPOSE_VERION="V2"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100888fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200889
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100890tmp=$(which kubectl)
891if [ $? -ne 0 ] || [ -z tmp ]; then
892 if [ $RUNMODE == "KUBE" ]; then
893 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
894 exit 1
895 fi
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200896else
897 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100898 echo " kubectl is installed and using versions:"
899 echo $(kubectl version --short=true) | indent2
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200900 res=$(kubectl cluster-info 2>&1)
901 if [ $? -ne 0 ]; then
902 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
903 echo -e $BOLD$RED"Command 'kubectl cluster-info' returned error $ERED$EBOLD"
904 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
905 echo " "
906 echo "kubectl response:"
907 echo $res
908 echo " "
909 echo "This script may have been started with user with no permission to run kubectl"
910 echo "Try running with 'sudo' or set 'KUBECONFIG'"
911 echo "Do either 1, 2 or 3 "
912 echo " "
913 echo "1"
914 echo "Run with sudo"
915 echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD
916 echo " "
917 echo "2"
918 echo "Export KUBECONFIG and pass env to sudo - (replace user)"
919 echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD
920 echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD
921 echo " "
922 echo "3"
923 echo "Set KUBECONFIG inline (replace user)"
924 echo -e $BOLD"sudo KUBECONFIG='/home/<user>/.kube/config' <test-script-and-parameters>"$EBOLD
925
926 exit 1
927 fi
BjornMagnussonXA6dc3bd22021-12-20 08:58:10 +0100928 echo " Node(s) and container container runtime config"
929 kubectl get nodes -o wide | indent2
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200930 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100931fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100932
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100933echo ""
934
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100935echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100936
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100937#Temp var to check for image variable name errors
938IMAGE_ERR=0
939#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200940image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100941echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100942
943# Check if image env var is set and if so export the env var with image to use (used by docker compose files)
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100944# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100945__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200946
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200947 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100948 echo "Expected arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100949 ((IMAGE_ERR++))
950 return
951 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100952
953 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200954 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100955 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200956 # Image is excluded since the corresponding app is not used in this test
957 return
958 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100959 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100960 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100961 image="${!3}"
962 tmptag=$4"_"$5
963 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100964
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200965 optional_image_repo_target=""
966
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100967 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100968 __check_ignore_image $1
969 if [ $? -eq 0 ]; then
970 app_ds=$6
971 if [ -z "$6" ]; then
972 app_ds="<app ignored>"
973 fi
974 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
975 # Image is ignored since the corresponding the images is not set in the env file
976 __remove_included_image $1 # Remove the image from the list of included images
977 return
978 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100979 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
980 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100981 echo ""
982 tmp=$tmp"<no-image>\t"
983 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200984
985 optional_image_repo_target=$image
986
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100987 #Add repo depending on image type
988 if [ "$5" == "REMOTE_RELEASE" ]; then
989 image=$NEXUS_RELEASE_REPO$image
990 fi
991 if [ "$5" == "REMOTE" ]; then
992 image=$NEXUS_STAGING_REPO$image
993 fi
994 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
995 image=$NEXUS_SNAPSHOT_REPO$image
996 fi
997 if [ "$5" == "REMOTE_PROXY" ]; then
998 image=$NEXUS_PROXY_REPO$image
999 fi
1000 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
1001 image=$NEXUS_RELEASE_REPO_ONAP$image
1002 fi
1003 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
1004 image=$NEXUS_RELEASE_REPO_ORAN$image
1005 fi
1006 #No nexus repo added for local images, tag: LOCAL
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001007 tmp=$tmp$image"\t"
1008 fi
1009 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001010 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
1011 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001012 echo ""
1013 tmp=$tmp"<no-tag>\t"
1014 else
1015 tmp=$tmp$tag
1016 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001017 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001018 echo -e "$tmp" >> $image_list_file
1019 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001020 export "${2}"=$image":"$tag #Note, this var may be set to the value of the target value below in __check_and_pull_image
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001021
1022 remote_or_local_push=false
1023 if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
1024 if [ $5 == "LOCAL" ]; then
1025 remote_or_local_push=true
1026 fi
1027 if [[ $5 == *"REMOTE"* ]]; then
1028 if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
1029 remote_or_local_push=true
1030 fi
1031 fi
1032 fi
1033 if $remote_or_local_push; then # Only re-tag and push images according to policy, if repo is given
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001034 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001035 if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
1036 optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
1037 optional_image_repo_target=$optional_image_repo_target_tmp
1038 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001039 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
1040 else
1041 export "${2}_SOURCE"=""
1042 export "${2}_TARGET"=""
1043 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001044}
1045
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001046# Check if app uses image included in this test run
1047# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001048__check_included_image() {
1049 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001050 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001051 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001052 fi
1053 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001054 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001055}
1056
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001057# Check if app uses a project image
1058# Returns 0 if image is included, 1 if not
1059__check_project_image() {
1060 for im in $PROJECT_IMAGES; do
1061 if [ "$1" == "$im" ]; then
1062 return 0
1063 fi
1064 done
1065 return 1
1066}
1067
1068# Check if app uses image built by the test script
1069# Returns 0 if image is included, 1 if not
1070__check_image_local_build() {
1071 for im in $LOCAL_IMAGE_BUILD; do
1072 if [ "$1" == "$im" ]; then
1073 return 0
1074 fi
1075 done
1076 return 1
1077}
1078
1079# Check if app image is conditionally ignored in this test run
1080# Returns 0 if image is conditionally ignored, 1 if not
1081__check_ignore_image() {
1082 for im in $CONDITIONALLY_IGNORED_IMAGES; do
1083 if [ "$1" == "$im" ]; then
1084 return 0
1085 fi
1086 done
1087 return 1
1088}
1089
1090# Removed image from included list of included images
1091# Used when an image is marked as conditionally ignored
1092__remove_included_image() {
1093 tmp_img_rem_list=""
1094 for im in $INCLUDED_IMAGES; do
1095 if [ "$1" != "$im" ]; then
1096 tmp_img_rem_list=$tmp_img_rem_list" "$im
1097 fi
1098 done
1099 INCLUDED_IMAGES=$tmp_img_rem_list
1100 return 0
1101}
1102
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001103# Check if app is included in the prestarted set of apps
1104# Returns 0 if image is included, 1 if not
1105__check_prestarted_image() {
1106 for im in $KUBE_PRESTARTED_IMAGES; do
1107 if [ "$1" == "$im" ]; then
1108 return 0
1109 fi
1110 done
1111 return 1
1112}
1113
1114# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001115__check_image_local_override() {
1116 for im in $USE_LOCAL_IMAGES; do
1117 if [ "$1" == "$im" ]; then
1118 return 1
1119 fi
1120 done
1121 return 0
1122}
1123
1124# Check if app uses image override
1125# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
1126__check_image_override() {
1127
1128 for im in $ORAN_IMAGES_APP_NAMES; do
1129 if [ "$1" == "$im" ]; then
1130 echo "REMOTE_RELEASE_ORAN"
1131 return 0
1132 fi
1133 done
1134
1135 for im in $ONAP_IMAGES_APP_NAMES; do
1136 if [ "$1" == "$im" ]; then
1137 echo "REMOTE_RELEASE_ONAP"
1138 return 0
1139 fi
1140 done
1141
1142 found=0
1143 for im in $PROJECT_IMAGES_APP_NAMES; do
1144 if [ "$1" == "$im" ]; then
1145 found=1
1146 fi
1147 done
1148
1149 if [ $found -eq 0 ]; then
1150 echo "REMOTE_PROXY"
1151 return 0
1152 fi
1153
1154 suffix=""
1155 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
1156 suffix="REMOTE_RELEASE"
1157 fi
1158 if [ $IMAGE_CATEGORY == "DEV" ]; then
1159 suffix="REMOTE"
1160 fi
1161 CTR=0
1162 for im in $USE_STAGING_IMAGES; do
1163 if [ "$1" == "$im" ]; then
1164 suffix="REMOTE"
1165 ((CTR++))
1166 fi
1167 done
1168 for im in $USE_RELEASE_IMAGES; do
1169 if [ "$1" == "$im" ]; then
1170 suffix="REMOTE_RELEASE"
1171 ((CTR++))
1172 fi
1173 done
1174 for im in $USE_SNAPSHOT_IMAGES; do
1175 if [ "$1" == "$im" ]; then
1176 suffix="REMOTE_SNAPSHOT"
1177 ((CTR++))
1178 fi
1179 done
1180 for im in $USE_LOCAL_IMAGES; do
1181 if [ "$1" == "$im" ]; then
1182 suffix="LOCAL"
1183 ((CTR++))
1184 fi
1185 done
1186 echo $suffix
1187 if [ $CTR -gt 1 ]; then
1188 exit 1
1189 fi
1190 return 0
1191}
1192
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001193# Function to re-tag and image and push to another image repo
1194__retag_and_push_image() {
1195 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1196 source_image="${!1}"
1197 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
1198 target_image="${!trg_var_name}"
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001199
1200 if [ -z $target_image ]; then
1201 return 0 # Image with no target shall not be pushed
1202 fi
1203
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001204 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1205 tmp=$(docker image tag $source_image ${target_image} )
1206 if [ $? -ne 0 ]; then
1207 docker stop $tmp &> ./tmp/.dockererr
1208 ((IMAGE_ERR++))
1209 echo ""
1210 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1211 cat ./tmp/.dockererr
1212 return 1
1213 else
1214 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1215 fi
1216 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1217 tmp=$(docker push ${target_image} )
1218 if [ $? -ne 0 ]; then
1219 docker stop $tmp &> ./tmp/.dockererr
1220 ((IMAGE_ERR++))
1221 echo ""
1222 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1223 cat ./tmp/.dockererr
1224 return 1
1225 else
1226 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1227 fi
1228 export "${1}"=$target_image
1229 fi
1230 return 0
1231}
1232
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001233#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001234#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001235__check_and_pull_image() {
1236
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001237 source_image="${!4}"
1238
1239 echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$source_image$EBOLD"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001240 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001241 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001242
1243 if [ $1 == "local" ]; then
1244 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001245 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m $RED does not exist in local registry, need to be built (or manually pulled)"$ERED
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001246 ((IMAGE_ERR++))
1247 return 1
1248 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001249 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001250 fi
1251 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
1252 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001253 if [ $RUNMODE == "DOCKER" ]; then
1254 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001255 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001256 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1257 docker stop $tmp &> ./tmp/.dockererr
1258 if [ $? -ne 0 ]; then
1259 ((IMAGE_ERR++))
1260 echo ""
1261 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
1262 cat ./tmp/.dockererr
1263 return 1
1264 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001265 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001266 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001267 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001268 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1269 docker rm $tmp &> ./tmp/.dockererr
1270 if [ $? -ne 0 ]; then
1271 ((IMAGE_ERR++))
1272 echo ""
1273 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
1274 cat ./tmp/.dockererr
1275 return 1
1276 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001277 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001278 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
1279 tmp_im=""
1280 else
1281 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001282 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001283 fi
1284 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001285 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001286 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001287 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001288 echo ""
1289 echo -e " Pulling image -$RED could not be pulled"$ERED
1290 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001291 echo $out > ./tmp/.dockererr
1292 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001293 return 1
1294 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001295 echo $out > ./tmp/.dockererr
1296 if [[ $out == *"up to date"* ]]; then
1297 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
1298 elif [[ $out == *"Downloaded newer image"* ]]; then
1299 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
1300 else
1301 echo -e " Pulling image -$GREEN Pulled $EGREEN"
1302 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001303 else
1304 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1305 fi
1306 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001307
1308 __retag_and_push_image $4
1309
1310 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001311}
1312
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001313setup_testenvironment() {
1314 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001315 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001316
1317 # Image var setup for all project images included in the test
1318 for imagename in $APP_SHORT_NAMES; do
1319 __check_included_image $imagename
1320 incl=$?
1321 __check_project_image $imagename
1322 proj=$?
1323 if [ $incl -eq 0 ]; then
1324 if [ $proj -eq 0 ]; then
1325 IMAGE_SUFFIX=$(__check_image_override $imagename)
1326 if [ $? -ne 0 ]; then
1327 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1328 ((IMAGE_ERR++))
1329 fi
1330 else
1331 IMAGE_SUFFIX="none"
1332 fi
1333 # A function name is created from the app short name
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001334 # for example app short name 'ICS' -> produce the function
1335 # name __ICS_imagesetup
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001336 # This function is called and is expected to exist in the imported
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001337 # file for the ics test functions
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001338 # The resulting function impl will call '__check_and_create_image_var' function
1339 # with appropriate parameters
1340 # If the image suffix is none, then the component decides the suffix
1341 function_pointer="__"$imagename"_imagesetup"
1342 $function_pointer $IMAGE_SUFFIX
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01001343
1344 function_pointer="__"$imagename"_test_requirements"
1345 $function_pointer
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001346 fi
1347 done
1348
1349 #Errors in image setting - exit
1350 if [ $IMAGE_ERR -ne 0 ]; then
1351 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001352 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001353
1354 #Print a tables of the image settings
1355 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1356 column -t -s $'\t' $image_list_file | indent1
1357
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001358 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001359
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001360 #Set the SIM_GROUP var
1361 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1362 if [ -z "$SIM_GROUP" ]; then
1363 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1364 if [ ! -d $SIM_GROUP ]; then
1365 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1366 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1367 exit 1
1368 else
1369 echo " SIM_GROUP auto set to: " $SIM_GROUP
1370 fi
1371 elif [ $SIM_GROUP = *simulator_group ]; then
1372 echo -e $RED"Env var SIM_GROUP does not seem to point to dir 'simulator-group' in the repo, check $TEST_ENV_VAR_FILE"$ERED
1373 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001374 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001375 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1376 fi
1377
1378 echo ""
1379
1380 #Temp var to check for image pull errors
1381 IMAGE_ERR=0
1382
1383 # The following sequence pull the configured images
1384
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001385
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001386 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001387 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
1388 echo -e $YELLOW" Excluding all remote image check/pull when running with image repo: $IMAGE_REPO_ADR and image policy $IMAGE_REPO_POLICY"$EYELLOW
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001389 else
1390 for imagename in $APP_SHORT_NAMES; do
1391 __check_included_image $imagename
1392 incl=$?
1393 __check_project_image $imagename
1394 proj=$?
1395 if [ $incl -eq 0 ]; then
1396 if [ $proj -eq 0 ]; then
1397 START_ARG_MOD=$START_ARG
1398 __check_image_local_override $imagename
1399 if [ $? -eq 1 ]; then
1400 START_ARG_MOD="local"
1401 fi
1402 else
1403 START_ARG_MOD=$START_ARG
1404 fi
1405 __check_image_local_build $imagename
1406 #No pull of images built locally
1407 if [ $? -ne 0 ]; then
1408 # A function name is created from the app short name
1409 # for example app short name 'HTTPPROXY' -> produce the function
1410 # name __HTTPPROXY_imagesetup
1411 # This function is called and is expected to exist in the imported
1412 # file for the httpproxy test functions
1413 # The resulting function impl will call '__check_and_pull_image' function
1414 # with appropriate parameters
1415 function_pointer="__"$imagename"_imagepull"
1416 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001417 fi
1418 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001419 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001420 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001421 done
1422 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001423
1424 #Errors in image setting - exit
1425 if [ $IMAGE_ERR -ne 0 ]; then
1426 echo ""
1427 echo "#################################################################################################"
1428 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1429 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1430 if [ $IMAGE_CATEGORY == "DEV" ]; then
BjornMagnussonXA720f5d72021-08-13 10:37:23 +02001431 echo ""
1432 echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED
1433 echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED
1434 echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED
1435 echo -e $RED"Use the 'App-short-name' for the applicable image from the above table: 'Images configured for start arg'."$ERED
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001436 fi
1437 echo "#################################################################################################"
1438 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001439 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001440 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001441
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001442 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001443
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001444 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001445
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001446 for imagename in $APP_SHORT_NAMES; do
1447 cd $AUTOTEST_HOME #Always reset to orig dir
1448 __check_image_local_build $imagename
1449 if [ $? -eq 0 ]; then
1450 __check_included_image $imagename
1451 if [ $? -eq 0 ]; then
1452 # A function name is created from the app short name
1453 # for example app short name 'MR' -> produce the function
1454 # name __MR_imagebuild
1455 # This function is called and is expected to exist in the imported
1456 # file for the mr test functions
1457 # The resulting function impl shall build the imagee
1458 function_pointer="__"$imagename"_imagebuild"
1459 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001460
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001461 else
1462 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1463 fi
1464 fi
1465 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001466
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001467 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001468
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001469 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001470
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001471 # Create a table of the images used in the script - from local repo
1472 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001473
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001474 docker_tmp_file=./tmp/.docker-images-table
1475 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1476 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001477
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001478 for imagename in $APP_SHORT_NAMES; do
1479 __check_included_image $imagename
1480 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001481 # Only print image data if image repo is null, or if image repo is set and image is local
1482 print_image_data=0
1483 if [ -z "$IMAGE_REPO_ADR" ]; then
1484 print_image_data=1
1485 else
1486 __check_image_local_build $imagename
1487 if [ $? -eq 0 ]; then
1488 print_image_data=1
1489 fi
1490 fi
1491 if [ $print_image_data -eq 1 ]; then
1492 # A function name is created from the app short name
1493 # for example app short name 'MR' -> produce the function
1494 # name __MR_imagebuild
1495 # This function is called and is expected to exist in the imported
1496 # file for the mr test functions
1497 # The resulting function impl shall build the imagee
1498 function_pointer="__"$imagename"_image_data"
1499 $function_pointer "$format_string" $docker_tmp_file
1500 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001501 fi
1502 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001503
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001504 column -t -s $'\t' $docker_tmp_file | indent1
1505
1506 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001507
1508 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1509
1510 # Create a table of the images used in the script - from remote repo
1511 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1512 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1513
1514 docker_tmp_file=./tmp/.docker-images-table
1515 format_string="{{.Repository}}\\t{{.Tag}}"
1516 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1517
1518 for imagename in $APP_SHORT_NAMES; do
1519 __check_included_image $imagename
1520 if [ $? -eq 0 ]; then
1521 # Only print image data if image repo is null, or if image repo is set and image is local
1522 __check_image_local_build $imagename
1523 if [ $? -ne 0 ]; then
1524 # A function name is created from the app short name
1525 # for example app short name 'MR' -> produce the function
1526 # name __MR_imagebuild
1527 # This function is called and is expected to exist in the imported
1528 # file for the mr test functions
1529 # The resulting function impl shall build the imagee
1530 function_pointer="__"$imagename"_image_data"
1531 $function_pointer "$format_string" $docker_tmp_file
1532 fi
1533 fi
1534 done
1535
1536 column -t -s $'\t' $docker_tmp_file | indent1
1537
1538 echo ""
1539 fi
1540
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001541 if [ $RUNMODE == "KUBE" ]; then
1542
1543 echo "================================================================================="
1544 echo "================================================================================="
1545
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001546 if [ -z "$IMAGE_REPO_ADR" ]; then
1547 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1548 echo -e " This setting only works on single node clusters on the local machine"
1549 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001550 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001551 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001552 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1553 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1554 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1555 echo -e " Pulling remote snapshot or staging images my in some case result in pulling newer image versions outside the control of the test engine"
1556 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001557 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001558 CLUSTER_IP=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
1559 echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001560
1561 echo "================================================================================="
1562 echo "================================================================================="
1563 echo ""
1564 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001565
1566 echo -e $BOLD"======================================================="$EBOLD
1567 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1568 echo -e $BOLD"======================================================="$EBOLD
1569 echo ""
1570
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001571 LOG_STAT_ARGS=""
1572
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001573 for imagename in $APP_SHORT_NAMES; do
1574 __check_included_image $imagename
1575 retcode_i=$?
1576 __check_prestarted_image $imagename
1577 retcode_p=$?
1578 if [ $retcode_i -eq 0 ] || [ $retcode_p -eq 0 ]; then
1579 # A function name is created from the app short name
1580 # for example app short name 'RICMSIM' -> produce the function
1581 # name __RICSIM__initial_setup
1582 # This function is called and is expected to exist in the imported
1583 # file for the ricsim test functions
1584 # The resulting function impl shall perform initial setup of port, host etc
1585
1586 function_pointer="__"$imagename"_initial_setup"
1587 $function_pointer
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001588
1589 function_pointer="__"$imagename"_statisics_setup"
1590 LOG_STAT_ARGS=$LOG_STAT_ARGS" "$($function_pointer)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001591 fi
1592 done
1593
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001594 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1595 ../common/genstat.sh $RUNMODE $SECONDS $TESTLOGS/$ATC/stat_data.csv $LOG_STAT_ARGS &
1596 fi
1597
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001598}
YongchaoWu9a84f512019-12-16 22:54:11 +01001599
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001600# Function to print the test result, shall be the last cmd in a test script
1601# args: -
1602# (Function for test scripts)
1603print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001604
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001605 TCTEST_END=$SECONDS
1606 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001607
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001608 echo "-------------------------------------------------------------------------------------------------"
1609 echo "------------------------------------- Test case: "$ATC
1610 echo "------------------------------------- Ended: "$(date)
1611 echo "-------------------------------------------------------------------------------------------------"
1612 echo "-- Description: "$TC_ONELINE_DESCR
1613 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001614 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001615 echo "-------------------------------------------------------------------------------------------------"
1616 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001617 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001618
YongchaoWu21f17bb2020-03-05 12:44:08 +01001619
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001620 if [ $RES_DEVIATION -gt 0 ]; then
1621 echo "Test case deviations"
1622 echo "===================================="
1623 cat $DEVIATION_FILE
1624 fi
1625 echo ""
1626 echo "Timer measurement in the test script"
1627 echo "===================================="
1628 column -t -s $'\t' $TIMER_MEASUREMENTS
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001629 if [ $RES_PASS != $RES_TEST ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001630 echo -e $RED"Measurement may not be reliable when there are failed test - failures may cause long measurement values due to timeouts etc."$ERED
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001631 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001632 echo ""
1633
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001634 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1635 echo "Runtime statistics collected in file: "$TESTLOGS/$ATC/stat_data.csv
1636 echo ""
1637 fi
1638
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001639 total=$((RES_PASS+RES_FAIL))
1640 if [ $RES_TEST -eq 0 ]; then
1641 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1642 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1643 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1644 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1645 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1646 elif [ $total != $RES_TEST ]; then
1647 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1648 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1649 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1650 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1651 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1652 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001653 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001654 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1655 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1656 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1657 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1658 elif [ $RES_PASS = $RES_TEST ]; then
1659 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1660 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1661 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1662 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1663 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1664 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001665
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001666 # Update test suite counter
1667 if [ -f .tmp_tcsuite_pass_ctr ]; then
1668 tmpval=$(< .tmp_tcsuite_pass_ctr)
1669 ((tmpval++))
1670 echo $tmpval > .tmp_tcsuite_pass_ctr
1671 fi
1672 if [ -f .tmp_tcsuite_pass ]; then
1673 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1674 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001675 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001676 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001677 else
1678 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1679 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
1680 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
1681 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
1682 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
1683 echo ""
1684 # Update test suite counter
1685 if [ -f .tmp_tcsuite_fail_ctr ]; then
1686 tmpval=$(< .tmp_tcsuite_fail_ctr)
1687 ((tmpval++))
1688 echo $tmpval > .tmp_tcsuite_fail_ctr
1689 fi
1690 if [ -f .tmp_tcsuite_fail ]; then
1691 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1692 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001693 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001694
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001695 echo "++++ Number of tests: "$RES_TEST
1696 echo "++++ Number of passed tests: "$RES_PASS
1697 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01001698 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001699 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001700 echo ""
1701 echo "++++ Number of test case deviations: "$RES_DEVIATION
1702 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001703 echo "------------------------------------- Test case complete ---------------------------------"
1704 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01001705 echo ""
1706}
1707
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001708#####################################################################
1709###### Functions for start, configuring, stoping, cleaning etc ######
1710#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01001711
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001712# Start timer for time measurement
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001713# args: <timer message to print> - timer value and message will be printed both on screen
1714# and in the timer measurement report - if at least one "print_timer is called"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001715start_timer() {
1716 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001717 TC_TIMER_STARTTIME=$SECONDS
1718 TC_TIMER_TIMER_TEXT="${@:1}"
1719 if [ $# -ne 1 ]; then
1720 __print_err "need 1 arg, <timer message to print>" $@
1721 TC_TIMER_TIMER_TEXT=${FUNCNAME[0]}":"${BASH_LINENO[0]}
1722 echo " Assigning timer name: "$TC_TIMER_TIMER_TEXT
1723 fi
1724 TC_TIMER_CURRENT_FAILS=$(($RES_FAIL+$RES_CONF_FAIL))
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001725 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001726}
1727
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001728# Print the running timer the value of the time (in seconds)
1729# Timer value and message will be printed both on screen and in the timer measurement report
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001730print_timer() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001731 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $TC_TIMER_TIMER_TEXT $EBOLD
1732 if [ -z "$TC_TIMER_STARTTIME" ]; then
1733 __print_err "timer not started" $@
1734 return 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001735 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001736 duration=$(($SECONDS-$TC_TIMER_STARTTIME))
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001737 if [ $duration -eq 0 ]; then
1738 duration="<1 second"
1739 else
1740 duration=$duration" seconds"
1741 fi
1742 echo " Timer duration :" $duration
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001743 res="-"
1744 if [ $(($RES_FAIL+$RES_CONF_FAIL)) -ne $TC_TIMER_CURRENT_FAILS ]; then
1745 res="Failures occured during test - timer not reliabled"
1746 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001747
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001748 echo -e "$TC_TIMER_TIMER_TEXT \t $duration \t $res" >> $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001749}
1750
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001751# Print info about a deviations from intended tests
1752# Each deviation counted is also printed in the testreport
1753# args <deviation message to print>
1754deviation() {
1755 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
1756 if [ $# -lt 1 ]; then
1757 ((RES_CONF_FAIL++))
1758 __print_err "need 1 or more args, <deviation message to print>" $@
1759 exit 1
1760 fi
1761 ((RES_DEVIATION++))
1762 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
1763 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02001764 __print_current_stats
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001765 echo ""
1766}
YongchaoWu21f17bb2020-03-05 12:44:08 +01001767
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001768# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
1769__check_stop_at_error() {
1770 if [ $STOP_AT_ERROR -eq 1 ]; then
1771 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
1772 store_logs "STOP_AT_ERROR"
1773 exit 1
1774 fi
1775 return 0
1776}
1777
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001778# Stop and remove all containers
1779# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001780# (Not for test scripts)
1781__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001782
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001783 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001784
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001785 #Create empty file
1786 running_contr_file="./tmp/running_contr.txt"
1787 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001788
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001789 # Get list of all containers started by the test script
1790 for imagename in $APP_SHORT_NAMES; do
1791 docker ps -a --filter "label=nrttest_app=$imagename" --filter "network=$DOCKER_SIM_NWNAME" --format ' {{.Label "nrttest_dp"}}\n{{.Label "nrttest_app"}}\n{{.Names}}' >> $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001792 done
BjornMagnussonXAecf34532021-11-24 08:25:15 +01001793 running_contr_file_empty="No docker containers running, started by previous test execution"
1794 if [ -s $running_contr_file ]; then
1795 running_contr_file_empty=""
1796 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001797
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001798 # Kill all containers started by the test env - to speed up shut down
1799 docker kill $(docker ps -a --filter "label=nrttest_app" --format '{{.Names}}') &> /dev/null
1800
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001801 tab_heading1="App display name"
1802 tab_heading2="App short name"
1803 tab_heading3="Container name"
1804
1805 tab_heading1_len=${#tab_heading1}
1806 tab_heading2_len=${#tab_heading2}
1807 tab_heading3_len=${#tab_heading3}
1808 cntr=0
1809 #Calc field lengths of each item in the list of containers
1810 while read p; do
1811 if (( $cntr % 3 == 0 ));then
1812 if [ ${#p} -gt $tab_heading1_len ]; then
1813 tab_heading1_len=${#p}
1814 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001815 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001816 if (( $cntr % 3 == 1));then
1817 if [ ${#p} -gt $tab_heading2_len ]; then
1818 tab_heading2_len=${#p}
1819 fi
1820 fi
1821 if (( $cntr % 3 == 2));then
1822 if [ ${#p} -gt $tab_heading3_len ]; then
1823 tab_heading3_len=${#p}
1824 fi
1825 fi
1826 let cntr=cntr+1
1827 done <$running_contr_file
1828
1829 let tab_heading1_len=tab_heading1_len+2
1830 while (( ${#tab_heading1} < $tab_heading1_len)); do
1831 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001832 done
1833
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001834 let tab_heading2_len=tab_heading2_len+2
1835 while (( ${#tab_heading2} < $tab_heading2_len)); do
1836 tab_heading2="$tab_heading2"" "
1837 done
1838
1839 let tab_heading3_len=tab_heading3_len+2
1840 while (( ${#tab_heading3} < $tab_heading3_len)); do
1841 tab_heading3="$tab_heading3"" "
1842 done
1843
BjornMagnussonXAecf34532021-11-24 08:25:15 +01001844 if [ ! -z "$running_contr_file_empty" ]; then
1845 echo $running_contr_file_empty | indent1
1846 else
1847 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
1848 cntr=0
1849 while read p; do
1850 if (( $cntr % 3 == 0 ));then
1851 row=""
1852 heading=$p
1853 heading_len=$tab_heading1_len
1854 fi
1855 if (( $cntr % 3 == 1));then
1856 heading=$p
1857 heading_len=$tab_heading2_len
1858 fi
1859 if (( $cntr % 3 == 2));then
1860 contr=$p
1861 heading=$p
1862 heading_len=$tab_heading3_len
1863 fi
1864 while (( ${#heading} < $heading_len)); do
1865 heading="$heading"" "
1866 done
1867 row=$row$heading
1868 if (( $cntr % 3 == 2));then
1869 echo -ne $row$SAMELINE
1870 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
1871 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1872 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
1873 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1874 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
1875 fi
1876 let cntr=cntr+1
1877 done <$running_contr_file
1878 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001879
YongchaoWu9a84f512019-12-16 22:54:11 +01001880 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001881
1882 echo -e $BOLD" Removing docker network"$EBOLD
1883 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
1884 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001885 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001886 if [ $? -ne 0 ]; then
1887 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
1888 exit 1
1889 fi
1890 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001891 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001892
1893 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001894 docker network prune --force | indent2
1895 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001896
1897 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001898 docker volume prune --force | indent2
1899 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001900
1901 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
1902 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001903 echo -e "$GREEN Done$EGREEN"
1904 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001905
1906 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
1907 if [ $? -eq 0 ]; then
1908 if [ $CONTRS -ne 0 ]; then
1909 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001910 docker ps -a | indent1
1911 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001912 fi
1913 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01001914}
1915
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001916###################################
1917### Functions for kube management
1918###################################
1919
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02001920# Get resource type for scaling
1921# args: <resource-name> <namespace>
1922__kube_get_resource_type() {
1923 kubectl get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1924 if [ $? -eq 0 ]; then
1925 echo "deployment"
1926 return 0
1927 fi
1928 kubectl get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1929 if [ $? -eq 0 ]; then
1930 echo "sts"
1931 return 0
1932 fi
1933 echo "unknown-resource-type"
1934 return 1
1935}
1936
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001937# Scale a kube resource to a specific count
1938# args: <resource-type> <resource-name> <namespace> <target-count>
1939# (Not for test scripts)
1940__kube_scale() {
1941 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
1942 kubectl scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
1943 if [ $? -ne 0 ]; then
1944 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
1945 ((RES_CONF_FAIL++))
1946 echo " Message: $(<./tmp/kubeerr)"
1947 return 1
1948 else
1949 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
1950 fi
1951
1952 TSTART=$SECONDS
1953
1954 for i in {1..500}; do
1955 count=$(kubectl get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
1956 retcode=$?
1957 if [ -z "$count" ]; then
1958 #No value is sometimes returned for some reason, in case the resource has replica 0
1959 count=0
1960 fi
1961 if [ $retcode -ne 0 ]; then
1962 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
1963 ((RES_CONF_FAIL++))
1964 return 1
1965 fi
1966 #echo ""
1967 if [ $count -ne $4 ]; then
1968 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
1969 sleep $i
1970 else
1971 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
1972 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
1973 echo ""
1974 return 0
1975 fi
1976 done
1977 echo ""
1978 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
1979 ((RES_CONF_FAIL++))
1980 return 0
1981}
1982
1983# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1984# This function does not wait for the resource to reach 0
1985# args: <namespace> <label-name> <label-id>
1986# (Not for test scripts)
1987__kube_scale_all_resources() {
1988 namespace=$1
1989 labelname=$2
1990 labelid=$3
1991 resources="deployment replicaset statefulset"
1992 for restype in $resources; do
1993 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1994 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001995 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001996 echo -ne " Ordered caling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001997 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001998 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001999 done
2000 fi
2001 done
2002}
2003
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002004# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and an optional label-id
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002005# This function do wait for the resource to reach 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002006# args: <namespace> <label-name> [ <label-id> ]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002007# (Not for test scripts)
2008__kube_scale_and_wait_all_resources() {
2009 namespace=$1
2010 labelname=$2
2011 labelid=$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002012 if [ -z "$3" ]; then
2013 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname"
2014 else
2015 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname=$labelid"
2016 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002017 resources="deployment replicaset statefulset"
2018 scaled_all=1
2019 while [ $scaled_all -ne 0 ]; do
2020 scaled_all=0
2021 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002022 if [ -z "$3" ]; then
2023 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname')].metadata.name}')
2024 else
2025 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
2026 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002027 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2028 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002029 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002030 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
2031 count=1
2032 T_START=$SECONDS
2033 while [ $count -ne 0 ]; do
2034 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002035 echo -ne " Scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002036 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2037 sleep 0.5
2038 else
2039 count=0
2040 fi
2041 duration=$(($SECONDS-$T_START))
2042 if [ $duration -gt 100 ]; then
2043 #Forcring count 0, to avoid hanging for failed scaling
2044 scaled_all=1
2045 count=0
2046 fi
2047 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002048 echo -e " Scaled $restype $resid in namespace $namespace with label $labelname=$labelid to 0, current count=$count $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002049 done
2050 fi
2051 done
2052 done
2053}
2054
2055# Remove all kube resources in a namespace for resources having a certain label and label-id
2056# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
2057# args: <namespace> <label-name> <label-id>
2058# (Not for test scripts)
2059__kube_delete_all_resources() {
2060 namespace=$1
2061 labelname=$2
2062 labelid=$3
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002063 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes serviceaccounts clusterrolebindings"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002064 deleted_resourcetypes=""
2065 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002066 ns_flag="-n $namespace"
2067 ns_text="in namespace $namespace"
2068 if [ $restype == "persistentvolumes" ]; then
2069 ns_flag=""
2070 ns_text=""
2071 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002072 if [ $restype == "clusterrolebindings" ]; then
2073 ns_flag=""
2074 ns_text=""
2075 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002076 result=$(kubectl get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002077 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2078 deleted_resourcetypes=$deleted_resourcetypes" "$restype
2079 for resid in $result; do
2080 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
2081 count=1
2082 while [ $count -ne 0 ]; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002083 count=$(kubectl get $restype $resid $ns_flag -o jsonpath='{.status.replicas}' 2> /dev/null)
2084 echo -ne " Scaling $restype $resid $ns_text with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002085 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2086 sleep 0.5
2087 else
2088 count=0
2089 fi
2090 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002091 echo -e " Scaled $restype $resid $ns_text with label $labelname=$labelid to 0, current count=$count $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002092 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002093 echo -ne " Deleting $restype $resid $ns_text with label $labelname=$labelid "$SAMELINE
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002094 kubectl delete --grace-period=1 $restype $resid $ns_flag 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002095 if [ $? -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002096 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002097 else
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002098 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002099 fi
2100 #fi
2101 done
2102 fi
2103 done
2104 if [ ! -z "$deleted_resourcetypes" ]; then
2105 for restype in $deleted_resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002106 ns_flag="-n $namespace"
2107 ns_text="in namespace $namespace"
2108 if [ $restype == "persistentvolumes" ]; then
2109 ns_flag=""
2110 ns_text=""
2111 fi
2112 echo -ne " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted..."$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002113 T_START=$SECONDS
2114 result="dummy"
2115 while [ ! -z "$result" ]; do
2116 sleep 0.5
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002117 result=$(kubectl get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
2118 echo -ne " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002119 if [ -z "$result" ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002120 echo -e " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002121 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002122 echo -e " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002123 result=""
2124 fi
2125 done
2126 done
2127 fi
2128}
2129
2130# Creates a namespace if it does not exists
2131# args: <namespace>
2132# (Not for test scripts)
2133__kube_create_namespace() {
2134
2135 #Check if test namespace exists, if not create it
2136 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
2137 if [ $? -ne 0 ]; then
2138 echo -ne " Creating namespace "$1 $SAMELINE
2139 kubectl create namespace $1 1> /dev/null 2> ./tmp/kubeerr
2140 if [ $? -ne 0 ]; then
2141 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2142 ((RES_CONF_FAIL++))
2143 echo " Message: $(<./tmp/kubeerr)"
2144 return 1
2145 else
2146 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2147 fi
2148 else
2149 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
2150 fi
2151 return 0
2152}
2153
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002154# Removes a namespace if it exists
2155# args: <namespace>
2156# (Not for test scripts)
2157__kube_delete_namespace() {
2158
2159 #Check if test namespace exists, if so remove it
2160 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
2161 if [ $? -eq 0 ]; then
2162 echo -ne " Removing namespace "$1 $SAMELINE
2163 kubectl delete namespace $1 1> /dev/null 2> ./tmp/kubeerr
2164 if [ $? -ne 0 ]; then
2165 echo -e " Removing namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2166 ((RES_CONF_FAIL++))
2167 echo " Message: $(<./tmp/kubeerr)"
2168 return 1
2169 else
2170 echo -e " Removing namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2171 fi
2172 else
2173 echo -e " Namespace $1 $GREEN$BOLD does not exist, OK $EBOLD$EGREEN"
2174 fi
2175 return 0
2176}
2177
2178# Removes a namespace
2179# args: <namespace>
2180# (Not for test scripts)
2181clean_and_create_namespace() {
2182 __log_conf_start $@
2183
2184 if [ $# -ne 1 ]; then
2185 __print_err "<namespace>" $@
2186 return 1
2187 fi
2188 __kube_delete_namespace $1
2189 if [ $? -ne 0 ]; then
2190 return 1
2191 fi
2192 __kube_create_namespace $1
2193 if [ $? -ne 0 ]; then
2194 return 1
2195 fi
2196
2197}
2198
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002199# Find the host ip of an app (using the service resource)
2200# args: <app-name> <namespace>
2201# (Not for test scripts)
2202__kube_get_service_host() {
2203 if [ $# -ne 2 ]; then
2204 ((RES_CONF_FAIL++))
2205 __print_err "need 2 args, <app-name> <namespace>" $@
2206 exit 1
2207 fi
2208 for timeout in {1..60}; do
2209 host=$(kubectl get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
2210 if [ $? -eq 0 ]; then
2211 if [ ! -z "$host" ]; then
2212 echo $host
2213 return 0
2214 fi
2215 fi
2216 sleep 0.5
2217 done
2218 ((RES_CONF_FAIL++))
2219 echo "host-not-found-fatal-error"
2220 return 1
2221}
2222
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002223# Find the named port to an app (using the service resource)
2224# args: <app-name> <namespace> <port-name>
2225# (Not for test scripts)
2226__kube_get_service_port() {
2227 if [ $# -ne 3 ]; then
2228 ((RES_CONF_FAIL++))
2229 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2230 exit 1
2231 fi
2232
2233 for timeout in {1..60}; do
2234 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
2235 if [ $? -eq 0 ]; then
2236 if [ ! -z "$port" ]; then
2237 echo $port
2238 return 0
2239 fi
2240 fi
2241 sleep 0.5
2242 done
2243 ((RES_CONF_FAIL++))
2244 echo "0"
2245 return 1
2246}
2247
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002248# Find the named node port to an app (using the service resource)
2249# args: <app-name> <namespace> <port-name>
2250# (Not for test scripts)
2251__kube_get_service_nodeport() {
2252 if [ $# -ne 3 ]; then
2253 ((RES_CONF_FAIL++))
2254 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2255 exit 1
2256 fi
2257
2258 for timeout in {1..60}; do
2259 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
2260 if [ $? -eq 0 ]; then
2261 if [ ! -z "$port" ]; then
2262 echo $port
2263 return 0
2264 fi
2265 fi
2266 sleep 0.5
2267 done
2268 ((RES_CONF_FAIL++))
2269 echo "0"
2270 return 1
2271}
2272
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002273# Create a kube resource from a yaml template
2274# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
2275# (Not for test scripts)
2276__kube_create_instance() {
2277 echo -ne " Creating $1 $2"$SAMELINE
2278 envsubst < $3 > $4
2279 kubectl apply -f $4 1> /dev/null 2> ./tmp/kubeerr
2280 if [ $? -ne 0 ]; then
2281 ((RES_CONF_FAIL++))
2282 echo -e " Creating $1 $2 $RED Failed $ERED"
2283 echo " Message: $(<./tmp/kubeerr)"
2284 return 1
2285 else
2286 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
2287 fi
2288}
2289
2290# Function to create a configmap in kubernetes
2291# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
2292# (Not for test scripts)
2293__kube_create_configmap() {
2294 echo -ne " Creating configmap $1 "$SAMELINE
2295 envsubst < $5 > $5"_tmp"
2296 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
2297 kubectl create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
2298 if [ $? -ne 0 ]; then
2299 echo -e " Creating configmap $1 $RED Failed $ERED"
2300 ((RES_CONF_FAIL++))
2301 return 1
2302 fi
2303
2304 kubectl apply -f $6 1> /dev/null 2> ./tmp/kubeerr
2305 if [ $? -ne 0 ]; then
2306 echo -e " Creating configmap $1 $RED Apply failed $ERED"
2307 echo " Message: $(<./tmp/kubeerr)"
2308 ((RES_CONF_FAIL++))
2309 return 1
2310 fi
2311 kubectl label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
2312 if [ $? -ne 0 ]; then
2313 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
2314 echo " Message: $(<./tmp/kubeerr)"
2315 ((RES_CONF_FAIL++))
2316 return 1
2317 fi
2318 # Log the resulting map
2319 kubectl get configmap $1 -n $2 -o yaml > $6
2320
2321 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
2322 return 0
2323}
2324
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002325# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
2326# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002327# args: <full kubectl cmd with parameters>
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002328# (Not for test scripts)
2329__kube_cmd_with_timeout() {
2330 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
2331
2332 while true; do
2333 kube_cmd_result=$($@)
2334 if [ $? -ne 0 ]; then
2335 kube_cmd_result=""
2336 fi
2337 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
2338 echo $kube_cmd_result
2339 return 0
2340 fi
2341 sleep 1
2342 done
2343}
2344
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002345# This function starts a pod that cleans a the contents of a path mounted as a pvc
2346# After this action the pod should terminate
2347# This should only be executed when the pod owning the pvc is not running
2348# args: <appname> <namespace> <pvc-name> <path-to remove>
2349# (Not for test scripts)
2350__kube_clean_pvc() {
2351
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002352 #using env vars setup in pvccleaner_api_functions.sh
2353
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002354 export PVC_CLEANER_NAMESPACE=$2
2355 export PVC_CLEANER_CLAIMNAME=$3
2356 export PVC_CLEANER_RM_PATH=$4
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01002357 export PVC_CLEANER_APP_NAME
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002358 input_yaml=$SIM_GROUP"/"$PVC_CLEANER_COMPOSE_DIR"/"pvc-cleaner.yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002359 output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml
2360
2361 envsubst < $input_yaml > $output_yaml
2362
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002363 kubectl delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002364
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002365 __kube_create_instance pod $PVC_CLEANER_APP_NAME $input_yaml $output_yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002366 if [ $? -ne 0 ]; then
2367 echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work"
2368 return 1
2369 fi
2370
2371 term_ts=$(($SECONDS+30))
2372 while [ $term_ts -gt $SECONDS ]; do
2373 pod_status=$(kubectl get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase")
2374 if [ "$pod_status" == "Succeeded" ]; then
2375 return 0
2376 fi
2377 done
2378 return 1
2379}
2380
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002381# This function scales or deletes all resources for app selected by the testcase.
2382# args: -
2383# (Not for test scripts)
2384__clean_kube() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002385 echo -e $BOLD"Initialize kube pods/statefulsets/replicaset to initial state"$EBOLD
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002386
2387 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002388 for imagename in $APP_SHORT_NAMES; do
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002389 # A function name is created from the app short name
2390 # for example app short name 'RICMSIM' -> produce the function
2391 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
2392 # This function is called and is expected to exist in the imported
2393 # file for the ricsim test functions
2394 # The resulting function impl shall scale the resources to 0
2395 # For prestarted apps, the function waits until the resources are 0
2396 # For included (not prestated) apps, the scaling is just ordered
2397 __check_prestarted_image $imagename
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002398 if [ $? -eq 0 ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002399 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002400 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2401 $function_pointer
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002402 else
2403 __check_included_image $imagename
2404 if [ $? -eq 0 ]; then
2405 function_pointer="__"$imagename"_kube_scale_zero"
2406 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2407 $function_pointer
2408 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002409 fi
2410 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002411
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002412 # Delete managed apps
2413 for imagename in $APP_SHORT_NAMES; do
2414 __check_included_image $imagename
2415 if [ $? -eq 0 ]; then
2416 __check_prestarted_image $imagename
2417 if [ $? -ne 0 ]; then
2418 # A function name is created from the app short name
2419 # for example app short name 'RICMSIM' -> produce the function
2420 # name __RICSIM__kube_delete_all
2421 # This function is called and is expected to exist in the imported
2422 # file for the ricsim test functions
2423 # The resulting function impl shall delete all its resources
2424 function_pointer="__"$imagename"_kube_delete_all"
2425 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
2426 $function_pointer
2427 fi
2428 fi
2429 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002430
2431 echo ""
2432}
2433
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002434# Function stop and remove all containers (docker) and services/deployments etc(kube)
2435# args: -
2436# Function for test script
2437clean_environment() {
2438 if [ $RUNMODE == "KUBE" ]; then
2439 __clean_kube
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002440 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002441 echo " Cleaning docker resouces to free up resources, may take time..."
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002442 ../common/clean_docker.sh 2>&1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002443 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002444 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002445 else
2446 __clean_containers
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002447 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002448 echo " Cleaning kubernetes resouces to free up resources, may take time..."
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002449 ../common/clean_kube.sh 2>&1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002450 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002451 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002452 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002453}
2454
2455# Function stop and remove all containers (docker) and services/deployments etc(kube) in the end of the test script, if the arg 'auto-clean' is given at test script start
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002456# args: -
2457# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002458auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002459 echo
2460 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002461 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
2462 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01002463 fi
2464}
2465
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002466# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002467# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002468# (Function for test scripts)
2469sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01002470
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002471 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
2472 if [ $# -lt 1 ]; then
2473 ((RES_CONF_FAIL++))
2474 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
2475 exit 1
2476 fi
2477 #echo "---- Sleep for " $1 " seconds ---- "$2
2478 start=$SECONDS
2479 duration=$((SECONDS-start))
2480 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002481 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002482 sleep 1
2483 duration=$((SECONDS-start))
2484 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002485 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002486 echo ""
2487}
2488
2489# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2490# Not to be called from the test script itself.
2491__print_err() {
2492 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2493 if [ $# -gt 1 ]; then
2494 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2495 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002496 ((RES_CONF_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002497}
2498
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002499# Function to create the docker network for the test
2500# Not to be called from the test script itself.
2501__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002502 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002503 if [ $? -ne 0 ]; then
2504 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2505 return 1
2506 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002507 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002508 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2509 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002510 if [ $? -ne 0 ]; then
2511 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2512 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002513 else
2514 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002515 fi
2516 else
2517 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2518 fi
2519}
2520
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002521# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002522# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2523#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002524# (Not for test scripts)
2525__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002526
2527 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002528 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002529 __print_err "need 5 or more args, <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+" $@
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002530 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002531 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002532
2533 __create_docker_network
2534
2535 curdir=$PWD
2536 cd $SIM_GROUP
2537 compose_dir=$1
2538 cd $1
2539 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002540 compose_file=$1
2541 if [ -z "$compose_file" ]; then
2542 compose_file="docker-compose.yml"
2543 fi
2544 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002545 compose_args=$1
2546 shift
2547 appcount=$1
2548 shift
2549
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002550 envsubst < $compose_file > "gen_"$compose_file
2551 compose_file="gen_"$compose_file
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002552 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
2553 docker_compose_cmd="docker-compose"
2554 else
2555 docker_compose_cmd="docker compose"
2556 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002557
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002558 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002559 $docker_compose_cmd -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002560 if [ $? -ne 0 ]; then
2561 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2562 cat .dockererr
2563 echo -e $RED"Stopping script...."$ERED
2564 exit 1
2565 fi
2566 else
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002567 $docker_compose_cmd -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002568 if [ $? -ne 0 ]; then
2569 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2570 cat .dockererr
2571 echo -e $RED"Stopping script...."$ERED
2572 exit 1
2573 fi
2574 fi
2575
2576 cd $curdir
2577
2578 appindex=0
2579 while [ $appindex -lt $appcount ]; do
2580 appname=$1
2581 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002582 app_started=0
2583 for i in {1..10}; do
2584 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002585 echo -e " Container $BOLD${appname}$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002586 app_started=1
2587 break
2588 else
2589 sleep $i
2590 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002591 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002592 if [ $app_started -eq 0 ]; then
2593 ((RES_CONF_FAIL++))
2594 echo ""
2595 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002596 echo -e $RED" Stopping script..."$ERED
2597 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002598 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002599 let appindex=appindex+1
2600 done
2601 return 0
2602}
2603
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002604# Function to check if container/service is responding to http/https
2605# args: <container-name>|<service-name> url
2606# (Not for test scripts)
2607__check_service_start() {
2608
2609 if [ $# -ne 2 ]; then
2610 ((RES_CONF_FAIL++))
2611 __print_err "need 2 args, <container-name>|<service-name> url" $@
2612 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002613 fi
2614
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002615 if [ $RUNMODE == "KUBE" ]; then
2616 ENTITY="service/set/deployment"
2617 else
2618 ENTITY="container"
2619 fi
2620 appname=$1
2621 url=$2
2622 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2623
2624
YongchaoWu9a84f512019-12-16 22:54:11 +01002625 pa_st=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002626 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002627 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002628 loop_ctr=0
2629 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002630 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002631 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002632 if [ ${#result} -gt 15 ]; then
2633 #If response is too long, truncate
2634 result="...response text too long, omitted"
2635 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002636 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2637 echo -ne " The ${ENTITY} $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN on ${url} after $(($SECONDS-$TSTART)) seconds"
YongchaoWu9a84f512019-12-16 22:54:11 +01002638 pa_st=true
2639 break
2640 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002641 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002642 TS_OFFSET=$loop_ctr
2643 if (( $TS_OFFSET > 5 )); then
2644 TS_OFFSET=5
2645 fi
2646 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
2647 echo -ne " Waiting for ${ENTITY} ${appname} service status on ${url}...$(($SECONDS-$TSTART)) seconds, retrying in $(($TS_TMP+$TS_OFFSET-$SECONDS)) seconds ${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002648 sleep 1
2649 done
YongchaoWu9a84f512019-12-16 22:54:11 +01002650 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002651 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01002652 done
2653
2654 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002655 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002656 echo -e $RED" The ${ENTITY} ${appname} did not respond to service status on ${url} in $(($SECONDS-$TSTART)) seconds"$ERED
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002657 return 1
2658 fi
2659
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002660 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002661 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002662}
2663
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002664
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002665#################
2666### Log functions
2667#################
2668
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002669__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002670
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002671 dispname=$1
2672 appname=$2
2673 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002674 warning=$4
2675 error=$5
2676
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002677 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
2678
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002679 if [ $RUNMODE == "KUBE" ]; then
2680 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
2681 return
2682 fi
2683
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002684 #tmp=$(docker ps | grep $appname)
2685 tmp=$(docker ps -q --filter name=$appname) #get the container id
2686 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002687 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002688 return
2689 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002690 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002691 if [ $? -ne 0 ];then
2692 echo " Problem to search $appname log $logpath"
2693 else
2694 if [ $foundentries -eq 0 ]; then
2695 echo " No WARN entries found in $appname log $logpath"
2696 else
2697 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
2698 fi
2699 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002700 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002701 if [ $? -ne 0 ];then
2702 echo " Problem to search $appname log $logpath"
2703 else
2704 if [ $foundentries -eq 0 ]; then
2705 echo " No ERR entries found in $appname log $logpath"
2706 else
2707 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
2708 fi
2709 fi
2710 echo ""
2711}
2712
2713# Store all container logs and other logs in the log dir for the script
2714# Logs are stored with a prefix in case logs should be stored several times during a test
2715# args: <logfile-prefix>
2716# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01002717store_logs() {
2718 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002719 ((RES_CONF_FAIL++))
2720 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01002721 exit 1
2722 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002723 echo -e $BOLD"Storing all docker/kube container logs and other test logs in $TESTLOGS/$ATC using prefix: "$1$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +01002724
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002725 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002726
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002727 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002728
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002729 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
2730
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002731 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002732
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002733 # Store docker logs for all container
2734 for imagename in $APP_SHORT_NAMES; do
2735 __check_included_image $imagename
2736 if [ $? -eq 0 ]; then
2737 # A function name is created from the app short name
2738 # for example app short name 'RICMSIM' -> produce the function
2739 # name __RICSIM__store_docker_logs
2740 # This function is called and is expected to exist in the imported
2741 # file for the ricsim test functions
2742 # The resulting function impl shall store the docker logs for each container
2743 function_pointer="__"$imagename"_store_docker_logs"
2744 $function_pointer "$TESTLOGS/$ATC/" $1
2745 fi
2746 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002747 fi
2748 if [ $RUNMODE == "KUBE" ]; then
2749 namespaces=$(kubectl get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
2750 for nsid in $namespaces; do
2751 pods=$(kubectl get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
2752 for podid in $pods; do
2753 kubectl logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
2754 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002755 done
2756 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002757 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01002758}
2759
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002760###############
2761## Generic curl
2762###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002763# Generic curl function, assumes all 200-codes are ok
2764# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002765# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
2766# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01002767__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002768 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002769 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002770 if [ ! -z "$KUBE_PROXY_PATH" ]; then
2771 if [ $KUBE_PROXY_HTTPX == "http" ]; then
2772 proxyflag=" --proxy $KUBE_PROXY_PATH"
2773 else
2774 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002775 fi
2776 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002777 curlString="curl -skw %{http_code} $proxyflag $@"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002778 echo " CMD: $curlString" >> $HTTPLOG
2779 res=$($curlString)
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002780 retcode=$?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002781 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002782 echo " RETCODE: $retcode" >> $HTTPLOG
2783 if [ $retcode -ne 0 ]; then
2784 echo "<no-response-from-server>"
2785 return 1
2786 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002787 http_code="${res:${#res}-3}"
2788 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002789 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
2790 echo "<no-response-from-server>"
2791 return 1
2792 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002793 return 0
2794 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002795 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002796 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01002797 echo "<not found, resp:${http_code}>"
2798 return 1
2799 fi
2800 if [ $# -eq 2 ]; then
2801 echo "${res:0:${#res}-3}" | xargs
2802 else
2803 echo "${res:0:${#res}-3}"
2804 fi
2805
2806 return 0
2807 fi
2808}
2809
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002810#######################################
2811### Basic helper function for test cases
2812#######################################
2813
2814# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
2815# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
2816# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
2817# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
2818# before setting pass or fail depending on the result of comparing variable and target using the operator.
2819# 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.
2820# Not to be called from test script.
2821
2822__var_test() {
2823 checkjsonarraycount=0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002824 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002825 if [ $# -eq 6 ]; then
2826 if [[ $3 == "json:"* ]]; then
2827 checkjsonarraycount=1
2828 fi
2829
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002830 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002831 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG
2832
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002833 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002834 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002835 start=$SECONDS
2836 ctr=0
2837 for (( ; ; )); do
2838 if [ $checkjsonarraycount -eq 0 ]; then
2839 result="$(__do_curl $2$3)"
2840 retcode=$?
2841 result=${result//[[:blank:]]/} #Strip blanks
2842 else
2843 path=${3:5}
2844 result="$(__do_curl $2$path)"
2845 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002846 echo "$result" > ./tmp/.tmp.curl.json
2847 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002848 fi
2849 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002850 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002851 let ctr=ctr+1
2852 if [ $retcode -ne 0 ]; then
2853 if [ $duration -gt $6 ]; then
2854 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002855 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002856 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002857 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002858 return
2859 fi
2860 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2861 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002862 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002863 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002864 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002865 return
2866 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2867 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002868 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002869 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002870 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002871 return
2872 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2873 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002874 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002875 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002876 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002877 return
2878 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2879 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002880 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002881 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002882 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002883 return
2884 else
2885 if [ $duration -gt $6 ]; then
2886 ((RES_FAIL++))
2887 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002888 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002889 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002890 return
2891 fi
2892 fi
2893 sleep 1
2894 done
2895 elif [ $# -eq 5 ]; then
2896 if [[ $3 == "json:"* ]]; then
2897 checkjsonarraycount=1
2898 fi
2899
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002900 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002901 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002902 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002903 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002904 if [ $checkjsonarraycount -eq 0 ]; then
2905 result="$(__do_curl $2$3)"
2906 retcode=$?
2907 result=${result//[[:blank:]]/} #Strip blanks
2908 else
2909 path=${3:5}
2910 result="$(__do_curl $2$path)"
2911 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002912 echo "$result" > ./tmp/.tmp.curl.json
2913 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002914 fi
2915 if [ $retcode -ne 0 ]; then
2916 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002917 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002918 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002919 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002920 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2921 ((RES_PASS++))
2922 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002923 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002924 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2925 ((RES_PASS++))
2926 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002927 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002928 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2929 ((RES_PASS++))
2930 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002931 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002932 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2933 ((RES_PASS++))
2934 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002935 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002936 else
2937 ((RES_FAIL++))
2938 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002939 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002940 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002941 fi
2942 else
2943 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
2944 echo "Got:" $@
2945 exit 1
2946 fi
2947}