blob: 80059a1671ab32a0e0c51698a697b8e62df38398 [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
166fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100167curdir=$PWD
168cd tmp
169if [ $? -ne 0 ]; then
170 echo "Cannot cd to $PWD/tmp"
171 echo "Dir cannot be created. Exiting...."
172fi
173if [ ! -d "prev" ]; then
174 mkdir prev
175fi
176cd $curdir
177mv ./tmp/* ./tmp/prev 2> /dev/null
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200178
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100179# Create a http message log for this testcase
180HTTPLOG=$PWD"/.httplog_"$ATC".txt"
181echo "" > $HTTPLOG
182
183# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100184mkdir -p $TESTLOGS/$ATC
185
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100186# Save create for current logs
187mkdir -p $TESTLOGS/$ATC/previous
188
189rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
190rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
191rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
192
193mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
194mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
195mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
196
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100197# Clear the log dir for the test case
198rm $TESTLOGS/$ATC/*.log &> /dev/null
199rm $TESTLOGS/$ATC/*.txt &> /dev/null
200rm $TESTLOGS/$ATC/*.json &> /dev/null
201
202# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100203TCLOG=$TESTLOGS/$ATC/TC.log
204exec &> >(tee ${TCLOG})
205
206#Variables for counting tests as well as passed and failed tests
207RES_TEST=0
208RES_PASS=0
209RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100210RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200211RES_DEVIATION=0
212
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200213#Var to control if current stats shall be printed
214PRINT_CURRENT_STATS=0
215
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100216#Var to control if container/pod runtim statistics shall be collected
217COLLECT_RUNTIME_STATS=0
218
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200219#File to keep deviation messages
220DEVIATION_FILE=".tmp_deviations"
221rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100222
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100223# Trap "command not found" and make the script fail
224trap_fnc() {
225
226 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100227 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100228 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200229 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100230 fi
231}
232trap trap_fnc ERR
233
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100234# Trap to kill subprocesses
235trap "kill 0" EXIT
236
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100237# Counter for tests
238TEST_SEQUENCE_NR=1
239
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100240# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100241__log_test_start() {
242 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
243 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
244 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
245 ((RES_TEST++))
246 ((TEST_SEQUENCE_NR++))
247}
248
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200249# Function to print current statistics
250__print_current_stats() {
251 if [ $PRINT_CURRENT_STATS -ne 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200252 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 +0200253 fi
254}
255
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100256# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100257__log_test_fail_general() {
258 echo -e $RED" FAIL."$1 $ERED
259 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200260 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100261 __check_stop_at_error
262}
263
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100264# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100265__log_test_fail_status_code() {
266 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
267 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200268 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100269 __check_stop_at_error
270}
271
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100272# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100273__log_test_fail_body() {
274 echo -e $RED" FAIL, returned body not correct"$ERED
275 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200276 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100277 __check_stop_at_error
278}
279
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100280# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100281__log_test_fail_not_supported() {
282 echo -e $RED" FAIL, function not supported"$ERED
283 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200284 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100285 __check_stop_at_error
286}
287
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100288# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100289__log_test_pass() {
290 if [ $# -gt 0 ]; then
291 echo $@
292 fi
293 ((RES_PASS++))
294 echo -e $GREEN" PASS"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200295 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100296}
297
298#Counter for configurations
299CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100300
301# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100302__log_conf_start() {
303 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
304 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
305 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
306 ((CONF_SEQUENCE_NR++))
307}
308
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100309# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100310__log_conf_fail_general() {
311 echo -e $RED" FAIL."$1 $ERED
312 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200313 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100314 __check_stop_at_error
315}
316
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100317# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100318__log_conf_fail_status_code() {
319 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
320 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200321 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100322 __check_stop_at_error
323}
324
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100325# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100326__log_conf_fail_body() {
327 echo -e $RED" FAIL, returned body not correct"$ERED
328 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200329 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100330 __check_stop_at_error
331}
332
BjornMagnussonXA83a750f2021-09-21 20:39:58 +0200333# Function to log a configuration that is not supported
334__log_conf_fail_not_supported() {
335 echo -e $RED" FAIL, function not supported"$ERED$@
336 ((RES_CONF_FAIL++))
337 __print_current_stats
338 __check_stop_at_error
339}
340
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100341# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100342__log_conf_ok() {
343 if [ $# -gt 0 ]; then
344 echo $@
345 fi
346 echo -e $GREEN" OK"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200347 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100348}
349
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100350#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100351TCTEST_START=$SECONDS
352
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100353#Vars to hold the start time and timer text for a custom timer
354TC_TIMER_STARTTIME=""
355TC_TIMER_TIMER_TEXT=""
356TC_TIMER_CURRENT_FAILS="" # Then numer of failed test when timer starts.
357 # Compared with the current number of fails at timer stop
358 # to judge the measurement reliability
359
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200360#File to save timer measurement results
361TIMER_MEASUREMENTS=".timer_measurement.txt"
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100362echo -e "Activity \t Duration \t Info" > $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200363
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200364# 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 +0200365IMAGE_REPO_ADR=""
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200366IMAGE_REPO_POLICY="local"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200367CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200368
YongchaoWu9a84f512019-12-16 22:54:11 +0100369echo "-------------------------------------------------------------------------------------------------"
370echo "----------------------------------- Test case: "$ATC
371echo "----------------------------------- Started: "$(date)
372echo "-------------------------------------------------------------------------------------------------"
373echo "-- Description: "$TC_ONELINE_DESCR
374echo "-------------------------------------------------------------------------------------------------"
375echo "----------------------------------- Test case setup -----------------------------------"
376
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100377echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200378START_ARG=$1
379paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100380paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200381if [ $# -lt 1 ]; then
382 paramerror=1
383fi
384if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100385 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200386 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100387 if [ -z "$paramerror_str" ]; then
388 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
389 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200390 else
391 shift;
392 fi
393fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100394if [ $paramerror -eq 0 ]; then
395 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
396 paramerror=1
397 if [ -z "$paramerror_str" ]; then
398 paramerror_str="Second arg shall be 'docker' or 'kube'"
399 fi
400 else
401 if [ $1 == "docker" ]; then
402 RUNMODE="DOCKER"
403 echo "Setting RUNMODE=DOCKER"
404 fi
405 if [ $1 == "kube" ]; then
406 RUNMODE="KUBE"
407 echo "Setting RUNMODE=KUBE"
408 fi
409 shift;
410 fi
411fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200412foundparm=0
413while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
414 foundparm=1
415 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100416 if [ "$1" == "release" ]; then
417 IMAGE_CATEGORY="RELEASE"
418 echo "Option set - Release image tags used for applicable images "
419 shift;
420 foundparm=0
421 fi
422 fi
423 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200424 if [ "$1" == "auto-clean" ]; then
425 AUTO_CLEAN="auto"
426 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200427 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200428 foundparm=0
429 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200430 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200431 if [ $paramerror -eq 0 ]; then
432 if [ "$1" == "--stop-at-error" ]; then
433 STOP_AT_ERROR=1
434 echo "Option set - Stop at first error"
435 shift;
436 foundparm=0
437 fi
438 fi
439 if [ $paramerror -eq 0 ]; then
440 if [ "$1" == "--ricsim-prefix" ]; then
441 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100442 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200443 if [ -z "$1" ]; then
444 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100445 if [ -z "$paramerror_str" ]; then
446 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
447 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200448 else
449 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
450 shift;
451 foundparm=0
452 fi
453 fi
454 fi
455 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200456 if [ "$1" == "--env-file" ]; then
457 shift;
458 TEST_ENV_VAR_FILE=$1
459 if [ -z "$1" ]; then
460 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100461 if [ -z "$paramerror_str" ]; then
462 paramerror_str="No env file found for flag: '--env-file'"
463 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200464 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200465 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200466 shift;
467 foundparm=0
468 fi
469 fi
470 fi
471 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200472 if [ "$1" == "--use-local-image" ]; then
473 USE_LOCAL_IMAGES=""
474 shift
475 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
476 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100477 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200478 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100479 if [ -z "$paramerror_str" ]; then
480 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
481 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200482 fi
483 shift;
484 done
485 foundparm=0
486 if [ -z "$USE_LOCAL_IMAGES" ]; then
487 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100488 if [ -z "$paramerror_str" ]; then
489 paramerror_str="No app name found for flag: '--use-local-image'"
490 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200491 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100492 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
493 fi
494 fi
495 fi
496 if [ $paramerror -eq 0 ]; then
497 if [ "$1" == "--use-snapshot-image" ]; then
498 USE_SNAPSHOT_IMAGES=""
499 shift
500 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
501 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
502 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
503 paramerror=1
504 if [ -z "$paramerror_str" ]; then
505 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
506 fi
507 fi
508 shift;
509 done
510 foundparm=0
511 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
512 paramerror=1
513 if [ -z "$paramerror_str" ]; then
514 paramerror_str="No app name found for flag: '--use-snapshot-image'"
515 fi
516 else
517 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
518 fi
519 fi
520 fi
521 if [ $paramerror -eq 0 ]; then
522 if [ "$1" == "--use-staging-image" ]; then
523 USE_STAGING_IMAGES=""
524 shift
525 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
526 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
527 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
528 paramerror=1
529 if [ -z "$paramerror_str" ]; then
530 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
531 fi
532 fi
533 shift;
534 done
535 foundparm=0
536 if [ -z "$USE_STAGING_IMAGES" ]; then
537 paramerror=1
538 if [ -z "$paramerror_str" ]; then
539 paramerror_str="No app name found for flag: '--use-staging-image'"
540 fi
541 else
542 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
543 fi
544 fi
545 fi
546 if [ $paramerror -eq 0 ]; then
547 if [ "$1" == "--use-release-image" ]; then
548 USE_RELEASE_IMAGES=""
549 shift
550 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
551 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
552 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
553 paramerror=1
554 if [ -z "$paramerror_str" ]; then
555 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
556 fi
557 fi
558 shift;
559 done
560 foundparm=0
561 if [ -z "$USE_RELEASE_IMAGES" ]; then
562 paramerror=1
563 if [ -z "$paramerror_str" ]; then
564 paramerror_str="No app name found for flag: '--use-release-image'"
565 fi
566 else
567 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200568 fi
569 fi
570 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200571 if [ $paramerror -eq 0 ]; then
572 if [ "$1" == "--image-repo" ]; then
573 shift;
574 IMAGE_REPO_ADR=$1
575 if [ -z "$1" ]; then
576 paramerror=1
577 if [ -z "$paramerror_str" ]; then
578 paramerror_str="No image repo url found for : '--image-repo'"
579 fi
580 else
581 echo "Option set - Image repo url: "$1
582 shift;
583 foundparm=0
584 fi
585 fi
586 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200587 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200588 if [ "$1" == "--repo-policy" ]; then
589 shift;
590 IMAGE_REPO_POLICY=$1
591 if [ -z "$1" ]; then
592 paramerror=1
593 if [ -z "$paramerror_str" ]; then
594 paramerror_str="No policy found for : '--repo-policy'"
595 fi
596 else
597 if [ "$1" == "local" ] || [ "$1" == "remote" ]; then
598 echo "Option set - Image repo policy: "$1
599 shift;
600 foundparm=0
601 else
602 paramerror=1
603 if [ -z "$paramerror_str" ]; then
604 paramerror_str="Repo policy shall be 'local' or 'remote'"
605 fi
606 fi
607 fi
608 fi
609 fi
610 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200611 if [ "$1" == "--cluster-timeout" ]; then
612 shift;
613 CLUSTER_TIME_OUT=$1
614 if [ -z "$1" ]; then
615 paramerror=1
616 if [ -z "$paramerror_str" ]; then
617 paramerror_str="No timeout value found for : '--cluster-timeout'"
618 fi
619 else
620 #Check if positive int
621 case ${CLUSTER_TIME_OUT#[+]} in
622 *[!0-9]* | '')
623 paramerror=1
624 if [ -z "$paramerror_str" ]; then
625 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
626 fi
627 ;;
628 * ) ;; # Ok
629 esac
630 echo "Option set - Cluster timeout: "$1
631 shift;
632 foundparm=0
633 fi
634 fi
635 fi
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200636 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100637 if [ "$1" == "--override" ]; then
638 shift;
639 TEST_ENV_VAR_FILE_OVERRIDE=$1
640 if [ -z "$1" ]; then
641 paramerror=1
642 if [ -z "$paramerror_str" ]; then
643 paramerror_str="No env file found for flag: '--override'"
644 fi
645 else
646 if [ ! -f $TEST_ENV_VAR_FILE_OVERRIDE ]; then
647 paramerror=1
648 if [ -z "$paramerror_str" ]; then
649 paramerror_str="File for '--override' does not exist : "$TEST_ENV_VAR_FILE_OVERRIDE
650 fi
651 fi
652 echo "Option set - Override env from: "$1
653 shift;
654 foundparm=0
655 fi
656 fi
657 fi
658 if [ $paramerror -eq 0 ]; then
659 if [ "$1" == "--pre-clean" ]; then
660 PRE_CLEAN=1
661 echo "Option set - Pre-clean of kube/docker resouces"
662 shift;
663 foundparm=0
664 fi
665 fi
666 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200667 if [ "$1" == "--print-stats" ]; then
668 PRINT_CURRENT_STATS=1
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100669 echo "Option set - Print stats after every test-case and config"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200670 shift;
671 foundparm=0
672 fi
673 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100674 if [ $paramerror -eq 0 ]; then
675 if [ "$1" == "--gen-stats" ]; then
676 COLLECT_RUNTIME_STATS=1
677 echo "Option set - Collect runtime statistics"
678 shift;
679 foundparm=0
680 fi
681 fi
682
BjornMagnussonXAad047782020-06-08 15:54:11 +0200683done
684echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200685
BjornMagnussonXAad047782020-06-08 15:54:11 +0200686#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200687if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
688 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100689 if [ -z "$paramerror_str" ]; then
690 paramerror_str="Unknown parameter(s): "$@
691 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200692fi
693
694if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100695 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
696 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200697 exit 1
698fi
699
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200700# sourcing the selected env variables for the test case
701if [ -f "$TEST_ENV_VAR_FILE" ]; then
702 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
703 . $TEST_ENV_VAR_FILE
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100704 if [ ! -z "$TEST_ENV_VAR_FILE_OVERRIDE" ]; then
705 echo -e $BOLD"Sourcing override env vars from: "$TEST_ENV_VAR_FILE_OVERRIDE$EBOLD
706 . $TEST_ENV_VAR_FILE_OVERRIDE
707 fi
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100708
709 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100710 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 +0100711 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100712 found_profile=0
713 for prof in $SUPPORTED_PROFILES; do
714 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
715 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
716 found_profile=1
717 fi
718 done
719 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100720 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100721 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100722 echo -e $RED"Exiting...."$ERED
723 exit 1
724 fi
725 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200726else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200727 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200728 echo " Select one of following env var file matching the intended target of the test"
729 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100730 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200731 exit 1
732fi
733
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100734#This var need be preserved from the command line option, if set, when env var is sourced.
735if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
736 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
737fi
738
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100739if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100740 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100741 exit 1
742fi
743
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100744if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
745 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
746 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
747 exit 1
748fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100749
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100750# Choose list of included apps depending on run-mode
751if [ $RUNMODE == "KUBE" ]; then
752 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
753else
754 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
755fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200756
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100757echo ""
758# auto adding system apps
759echo -e $BOLD"Auto adding system apps"$EBOLD
760if [ $RUNMODE == "KUBE" ]; then
761 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_KUBE_SYSTEM_APPS
762 TMP_APPS=$TESTENV_KUBE_SYSTEM_APPS
763else
764 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_DOCKER_SYSTEM_APPS
765 TMP_APPS=$TESTENV_DOCKER_SYSTEM_APPS
766fi
767if [ ! -z "$TMP_APPS" ]; then
768 for iapp in "$TMP_APPS"; do
769 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
770 file_pointer="../common/"$file_pointer"_api_functions.sh"
771 echo " Auto-adding system app $iapp. Sourcing $file_pointer"
772 . $file_pointer
773 done
774else
775 echo " None"
776fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100777
778echo -e $BOLD"Auto adding included apps"$EBOLD
779 for iapp in $INCLUDED_IMAGES; do
780 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
781 file_pointer="../common/"$file_pointer"_api_functions.sh"
782 echo " Auto-adding included app $iapp. Sourcing $file_pointer"
783 . $file_pointer
784 if [ ! -f "$file_pointer" ]; then
785 echo " Include file $file_pointer for app $iapp does not exist"
786 exit 1
787 fi
788 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100789echo ""
790
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100791echo -e $BOLD"Test environment info"$EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100792
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100793# Check needed installed sw
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100794
795tmp=$(which bash)
796if [ $? -ne 0 ] || [ -z "$tmp" ]; then
797 echo -e $RED"bash is required to run the test environment, pls install"$ERED
798 exit 1
799fi
800echo " bash is installed and using version:"
801echo "$(bash --version)" | indent2
802
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100803tmp=$(which python3)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100804if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100805 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
806 exit 1
807fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100808echo " python3 is installed and using version: $(python3 --version)"
809
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100810tmp=$(which docker)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100811if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100812 echo -e $RED"docker is required to run the test environment, pls install"$ERED
813 exit 1
814fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100815echo " docker is installed and using versions:"
816echo " $(docker version --format 'Client version {{.Client.Version}} Server version {{.Server.Version}}')"
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200817
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100818tmp=$(which docker-compose)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100819if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100820 if [ $RUNMODE == "DOCKER" ]; then
821 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
822 exit 1
823 fi
824fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100825tmp=$(docker-compose version --short)
826echo " docker-compose installed and using version $tmp"
827if [[ "$tmp" == *'v2'* ]]; then
828 DOCKER_COMPOSE_VERION="V2"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100829fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200830
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100831tmp=$(which kubectl)
832if [ $? -ne 0 ] || [ -z tmp ]; then
833 if [ $RUNMODE == "KUBE" ]; then
834 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
835 exit 1
836 fi
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200837else
838 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100839 echo " kubectl is installed and using versions:"
840 echo $(kubectl version --short=true) | indent2
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200841 res=$(kubectl cluster-info 2>&1)
842 if [ $? -ne 0 ]; then
843 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
844 echo -e $BOLD$RED"Command 'kubectl cluster-info' returned error $ERED$EBOLD"
845 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
846 echo " "
847 echo "kubectl response:"
848 echo $res
849 echo " "
850 echo "This script may have been started with user with no permission to run kubectl"
851 echo "Try running with 'sudo' or set 'KUBECONFIG'"
852 echo "Do either 1, 2 or 3 "
853 echo " "
854 echo "1"
855 echo "Run with sudo"
856 echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD
857 echo " "
858 echo "2"
859 echo "Export KUBECONFIG and pass env to sudo - (replace user)"
860 echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD
861 echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD
862 echo " "
863 echo "3"
864 echo "Set KUBECONFIG inline (replace user)"
865 echo -e $BOLD"sudo KUBECONFIG='/home/<user>/.kube/config' <test-script-and-parameters>"$EBOLD
866
867 exit 1
868 fi
869 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100870fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100871
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100872echo ""
873
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100874echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100875
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100876#Temp var to check for image variable name errors
877IMAGE_ERR=0
878#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200879image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100880echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100881
882# 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 +0100883# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100884__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200885
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200886 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100887 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 +0100888 ((IMAGE_ERR++))
889 return
890 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100891
892 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200893 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100894 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200895 # Image is excluded since the corresponding app is not used in this test
896 return
897 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100898 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100899 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100900 image="${!3}"
901 tmptag=$4"_"$5
902 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100903
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200904 optional_image_repo_target=""
905
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100906 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100907 __check_ignore_image $1
908 if [ $? -eq 0 ]; then
909 app_ds=$6
910 if [ -z "$6" ]; then
911 app_ds="<app ignored>"
912 fi
913 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
914 # Image is ignored since the corresponding the images is not set in the env file
915 __remove_included_image $1 # Remove the image from the list of included images
916 return
917 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100918 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
919 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100920 echo ""
921 tmp=$tmp"<no-image>\t"
922 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200923
924 optional_image_repo_target=$image
925
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100926 #Add repo depending on image type
927 if [ "$5" == "REMOTE_RELEASE" ]; then
928 image=$NEXUS_RELEASE_REPO$image
929 fi
930 if [ "$5" == "REMOTE" ]; then
931 image=$NEXUS_STAGING_REPO$image
932 fi
933 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
934 image=$NEXUS_SNAPSHOT_REPO$image
935 fi
936 if [ "$5" == "REMOTE_PROXY" ]; then
937 image=$NEXUS_PROXY_REPO$image
938 fi
939 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
940 image=$NEXUS_RELEASE_REPO_ONAP$image
941 fi
942 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
943 image=$NEXUS_RELEASE_REPO_ORAN$image
944 fi
945 #No nexus repo added for local images, tag: LOCAL
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100946 tmp=$tmp$image"\t"
947 fi
948 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100949 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
950 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100951 echo ""
952 tmp=$tmp"<no-tag>\t"
953 else
954 tmp=$tmp$tag
955 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100956 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100957 echo -e "$tmp" >> $image_list_file
958 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200959 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 +0200960
961 remote_or_local_push=false
962 if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
963 if [ $5 == "LOCAL" ]; then
964 remote_or_local_push=true
965 fi
966 if [[ $5 == *"REMOTE"* ]]; then
967 if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
968 remote_or_local_push=true
969 fi
970 fi
971 fi
972 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 +0200973 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200974 if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
975 optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
976 optional_image_repo_target=$optional_image_repo_target_tmp
977 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200978 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
979 else
980 export "${2}_SOURCE"=""
981 export "${2}_TARGET"=""
982 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200983}
984
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200985# Check if app uses image included in this test run
986# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200987__check_included_image() {
988 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200989 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200990 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200991 fi
992 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200993 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200994}
995
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100996# Check if app uses a project image
997# Returns 0 if image is included, 1 if not
998__check_project_image() {
999 for im in $PROJECT_IMAGES; do
1000 if [ "$1" == "$im" ]; then
1001 return 0
1002 fi
1003 done
1004 return 1
1005}
1006
1007# Check if app uses image built by the test script
1008# Returns 0 if image is included, 1 if not
1009__check_image_local_build() {
1010 for im in $LOCAL_IMAGE_BUILD; do
1011 if [ "$1" == "$im" ]; then
1012 return 0
1013 fi
1014 done
1015 return 1
1016}
1017
1018# Check if app image is conditionally ignored in this test run
1019# Returns 0 if image is conditionally ignored, 1 if not
1020__check_ignore_image() {
1021 for im in $CONDITIONALLY_IGNORED_IMAGES; do
1022 if [ "$1" == "$im" ]; then
1023 return 0
1024 fi
1025 done
1026 return 1
1027}
1028
1029# Removed image from included list of included images
1030# Used when an image is marked as conditionally ignored
1031__remove_included_image() {
1032 tmp_img_rem_list=""
1033 for im in $INCLUDED_IMAGES; do
1034 if [ "$1" != "$im" ]; then
1035 tmp_img_rem_list=$tmp_img_rem_list" "$im
1036 fi
1037 done
1038 INCLUDED_IMAGES=$tmp_img_rem_list
1039 return 0
1040}
1041
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001042# Check if app is included in the prestarted set of apps
1043# Returns 0 if image is included, 1 if not
1044__check_prestarted_image() {
1045 for im in $KUBE_PRESTARTED_IMAGES; do
1046 if [ "$1" == "$im" ]; then
1047 return 0
1048 fi
1049 done
1050 return 1
1051}
1052
1053# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001054__check_image_local_override() {
1055 for im in $USE_LOCAL_IMAGES; do
1056 if [ "$1" == "$im" ]; then
1057 return 1
1058 fi
1059 done
1060 return 0
1061}
1062
1063# Check if app uses image override
1064# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
1065__check_image_override() {
1066
1067 for im in $ORAN_IMAGES_APP_NAMES; do
1068 if [ "$1" == "$im" ]; then
1069 echo "REMOTE_RELEASE_ORAN"
1070 return 0
1071 fi
1072 done
1073
1074 for im in $ONAP_IMAGES_APP_NAMES; do
1075 if [ "$1" == "$im" ]; then
1076 echo "REMOTE_RELEASE_ONAP"
1077 return 0
1078 fi
1079 done
1080
1081 found=0
1082 for im in $PROJECT_IMAGES_APP_NAMES; do
1083 if [ "$1" == "$im" ]; then
1084 found=1
1085 fi
1086 done
1087
1088 if [ $found -eq 0 ]; then
1089 echo "REMOTE_PROXY"
1090 return 0
1091 fi
1092
1093 suffix=""
1094 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
1095 suffix="REMOTE_RELEASE"
1096 fi
1097 if [ $IMAGE_CATEGORY == "DEV" ]; then
1098 suffix="REMOTE"
1099 fi
1100 CTR=0
1101 for im in $USE_STAGING_IMAGES; do
1102 if [ "$1" == "$im" ]; then
1103 suffix="REMOTE"
1104 ((CTR++))
1105 fi
1106 done
1107 for im in $USE_RELEASE_IMAGES; do
1108 if [ "$1" == "$im" ]; then
1109 suffix="REMOTE_RELEASE"
1110 ((CTR++))
1111 fi
1112 done
1113 for im in $USE_SNAPSHOT_IMAGES; do
1114 if [ "$1" == "$im" ]; then
1115 suffix="REMOTE_SNAPSHOT"
1116 ((CTR++))
1117 fi
1118 done
1119 for im in $USE_LOCAL_IMAGES; do
1120 if [ "$1" == "$im" ]; then
1121 suffix="LOCAL"
1122 ((CTR++))
1123 fi
1124 done
1125 echo $suffix
1126 if [ $CTR -gt 1 ]; then
1127 exit 1
1128 fi
1129 return 0
1130}
1131
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001132# Function to re-tag and image and push to another image repo
1133__retag_and_push_image() {
1134 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1135 source_image="${!1}"
1136 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
1137 target_image="${!trg_var_name}"
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001138
1139 if [ -z $target_image ]; then
1140 return 0 # Image with no target shall not be pushed
1141 fi
1142
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001143 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1144 tmp=$(docker image tag $source_image ${target_image} )
1145 if [ $? -ne 0 ]; then
1146 docker stop $tmp &> ./tmp/.dockererr
1147 ((IMAGE_ERR++))
1148 echo ""
1149 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1150 cat ./tmp/.dockererr
1151 return 1
1152 else
1153 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1154 fi
1155 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1156 tmp=$(docker push ${target_image} )
1157 if [ $? -ne 0 ]; then
1158 docker stop $tmp &> ./tmp/.dockererr
1159 ((IMAGE_ERR++))
1160 echo ""
1161 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1162 cat ./tmp/.dockererr
1163 return 1
1164 else
1165 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1166 fi
1167 export "${1}"=$target_image
1168 fi
1169 return 0
1170}
1171
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001172#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001173#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001174__check_and_pull_image() {
1175
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001176 source_image="${!4}"
1177
1178 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 +01001179 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001180 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001181
1182 if [ $1 == "local" ]; then
1183 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001184 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 +01001185 ((IMAGE_ERR++))
1186 return 1
1187 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001188 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001189 fi
1190 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
1191 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001192 if [ $RUNMODE == "DOCKER" ]; then
1193 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001194 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001195 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1196 docker stop $tmp &> ./tmp/.dockererr
1197 if [ $? -ne 0 ]; then
1198 ((IMAGE_ERR++))
1199 echo ""
1200 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
1201 cat ./tmp/.dockererr
1202 return 1
1203 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001204 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001205 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001206 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001207 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1208 docker rm $tmp &> ./tmp/.dockererr
1209 if [ $? -ne 0 ]; then
1210 ((IMAGE_ERR++))
1211 echo ""
1212 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
1213 cat ./tmp/.dockererr
1214 return 1
1215 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001216 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001217 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
1218 tmp_im=""
1219 else
1220 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001221 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001222 fi
1223 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001224 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001225 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001226 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001227 echo ""
1228 echo -e " Pulling image -$RED could not be pulled"$ERED
1229 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001230 echo $out > ./tmp/.dockererr
1231 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001232 return 1
1233 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001234 echo $out > ./tmp/.dockererr
1235 if [[ $out == *"up to date"* ]]; then
1236 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
1237 elif [[ $out == *"Downloaded newer image"* ]]; then
1238 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
1239 else
1240 echo -e " Pulling image -$GREEN Pulled $EGREEN"
1241 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001242 else
1243 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1244 fi
1245 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001246
1247 __retag_and_push_image $4
1248
1249 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001250}
1251
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001252setup_testenvironment() {
1253 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001254 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001255
1256 # Image var setup for all project images included in the test
1257 for imagename in $APP_SHORT_NAMES; do
1258 __check_included_image $imagename
1259 incl=$?
1260 __check_project_image $imagename
1261 proj=$?
1262 if [ $incl -eq 0 ]; then
1263 if [ $proj -eq 0 ]; then
1264 IMAGE_SUFFIX=$(__check_image_override $imagename)
1265 if [ $? -ne 0 ]; then
1266 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1267 ((IMAGE_ERR++))
1268 fi
1269 else
1270 IMAGE_SUFFIX="none"
1271 fi
1272 # A function name is created from the app short name
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001273 # for example app short name 'ICS' -> produce the function
1274 # name __ICS_imagesetup
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001275 # This function is called and is expected to exist in the imported
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001276 # file for the ics test functions
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001277 # The resulting function impl will call '__check_and_create_image_var' function
1278 # with appropriate parameters
1279 # If the image suffix is none, then the component decides the suffix
1280 function_pointer="__"$imagename"_imagesetup"
1281 $function_pointer $IMAGE_SUFFIX
1282 fi
1283 done
1284
1285 #Errors in image setting - exit
1286 if [ $IMAGE_ERR -ne 0 ]; then
1287 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001288 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001289
1290 #Print a tables of the image settings
1291 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1292 column -t -s $'\t' $image_list_file | indent1
1293
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001294 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001295
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001296 #Set the SIM_GROUP var
1297 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1298 if [ -z "$SIM_GROUP" ]; then
1299 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1300 if [ ! -d $SIM_GROUP ]; then
1301 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1302 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1303 exit 1
1304 else
1305 echo " SIM_GROUP auto set to: " $SIM_GROUP
1306 fi
1307 elif [ $SIM_GROUP = *simulator_group ]; then
1308 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
1309 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001310 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001311 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1312 fi
1313
1314 echo ""
1315
1316 #Temp var to check for image pull errors
1317 IMAGE_ERR=0
1318
1319 # The following sequence pull the configured images
1320
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001321
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001322 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001323 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
1324 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 +02001325 else
1326 for imagename in $APP_SHORT_NAMES; do
1327 __check_included_image $imagename
1328 incl=$?
1329 __check_project_image $imagename
1330 proj=$?
1331 if [ $incl -eq 0 ]; then
1332 if [ $proj -eq 0 ]; then
1333 START_ARG_MOD=$START_ARG
1334 __check_image_local_override $imagename
1335 if [ $? -eq 1 ]; then
1336 START_ARG_MOD="local"
1337 fi
1338 else
1339 START_ARG_MOD=$START_ARG
1340 fi
1341 __check_image_local_build $imagename
1342 #No pull of images built locally
1343 if [ $? -ne 0 ]; then
1344 # A function name is created from the app short name
1345 # for example app short name 'HTTPPROXY' -> produce the function
1346 # name __HTTPPROXY_imagesetup
1347 # This function is called and is expected to exist in the imported
1348 # file for the httpproxy test functions
1349 # The resulting function impl will call '__check_and_pull_image' function
1350 # with appropriate parameters
1351 function_pointer="__"$imagename"_imagepull"
1352 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001353 fi
1354 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001355 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001356 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001357 done
1358 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001359
1360 #Errors in image setting - exit
1361 if [ $IMAGE_ERR -ne 0 ]; then
1362 echo ""
1363 echo "#################################################################################################"
1364 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1365 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1366 if [ $IMAGE_CATEGORY == "DEV" ]; then
BjornMagnussonXA720f5d72021-08-13 10:37:23 +02001367 echo ""
1368 echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED
1369 echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED
1370 echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED
1371 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 +01001372 fi
1373 echo "#################################################################################################"
1374 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001375 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001376 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001377
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001378 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001379
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001380 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001381
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001382 for imagename in $APP_SHORT_NAMES; do
1383 cd $AUTOTEST_HOME #Always reset to orig dir
1384 __check_image_local_build $imagename
1385 if [ $? -eq 0 ]; then
1386 __check_included_image $imagename
1387 if [ $? -eq 0 ]; then
1388 # A function name is created from the app short name
1389 # for example app short name 'MR' -> produce the function
1390 # name __MR_imagebuild
1391 # This function is called and is expected to exist in the imported
1392 # file for the mr test functions
1393 # The resulting function impl shall build the imagee
1394 function_pointer="__"$imagename"_imagebuild"
1395 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001396
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001397 else
1398 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1399 fi
1400 fi
1401 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001402
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001403 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001404
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001405 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001406
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001407 # Create a table of the images used in the script - from local repo
1408 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001409
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001410 docker_tmp_file=./tmp/.docker-images-table
1411 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1412 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001413
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001414 for imagename in $APP_SHORT_NAMES; do
1415 __check_included_image $imagename
1416 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001417 # Only print image data if image repo is null, or if image repo is set and image is local
1418 print_image_data=0
1419 if [ -z "$IMAGE_REPO_ADR" ]; then
1420 print_image_data=1
1421 else
1422 __check_image_local_build $imagename
1423 if [ $? -eq 0 ]; then
1424 print_image_data=1
1425 fi
1426 fi
1427 if [ $print_image_data -eq 1 ]; then
1428 # A function name is created from the app short name
1429 # for example app short name 'MR' -> produce the function
1430 # name __MR_imagebuild
1431 # This function is called and is expected to exist in the imported
1432 # file for the mr test functions
1433 # The resulting function impl shall build the imagee
1434 function_pointer="__"$imagename"_image_data"
1435 $function_pointer "$format_string" $docker_tmp_file
1436 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001437 fi
1438 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001439
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001440 column -t -s $'\t' $docker_tmp_file | indent1
1441
1442 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001443
1444 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1445
1446 # Create a table of the images used in the script - from remote repo
1447 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1448 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1449
1450 docker_tmp_file=./tmp/.docker-images-table
1451 format_string="{{.Repository}}\\t{{.Tag}}"
1452 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1453
1454 for imagename in $APP_SHORT_NAMES; do
1455 __check_included_image $imagename
1456 if [ $? -eq 0 ]; then
1457 # Only print image data if image repo is null, or if image repo is set and image is local
1458 __check_image_local_build $imagename
1459 if [ $? -ne 0 ]; then
1460 # A function name is created from the app short name
1461 # for example app short name 'MR' -> produce the function
1462 # name __MR_imagebuild
1463 # This function is called and is expected to exist in the imported
1464 # file for the mr test functions
1465 # The resulting function impl shall build the imagee
1466 function_pointer="__"$imagename"_image_data"
1467 $function_pointer "$format_string" $docker_tmp_file
1468 fi
1469 fi
1470 done
1471
1472 column -t -s $'\t' $docker_tmp_file | indent1
1473
1474 echo ""
1475 fi
1476
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001477 if [ $RUNMODE == "KUBE" ]; then
1478
1479 echo "================================================================================="
1480 echo "================================================================================="
1481
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001482 if [ -z "$IMAGE_REPO_ADR" ]; then
1483 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1484 echo -e " This setting only works on single node clusters on the local machine"
1485 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001486 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001487 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001488 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1489 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1490 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1491 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"
1492 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001493 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001494 CLUSTER_IP=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
1495 echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001496
1497 echo "================================================================================="
1498 echo "================================================================================="
1499 echo ""
1500 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001501
1502 echo -e $BOLD"======================================================="$EBOLD
1503 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1504 echo -e $BOLD"======================================================="$EBOLD
1505 echo ""
1506
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001507 LOG_STAT_ARGS=""
1508
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001509 for imagename in $APP_SHORT_NAMES; do
1510 __check_included_image $imagename
1511 retcode_i=$?
1512 __check_prestarted_image $imagename
1513 retcode_p=$?
1514 if [ $retcode_i -eq 0 ] || [ $retcode_p -eq 0 ]; then
1515 # A function name is created from the app short name
1516 # for example app short name 'RICMSIM' -> produce the function
1517 # name __RICSIM__initial_setup
1518 # This function is called and is expected to exist in the imported
1519 # file for the ricsim test functions
1520 # The resulting function impl shall perform initial setup of port, host etc
1521
1522 function_pointer="__"$imagename"_initial_setup"
1523 $function_pointer
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001524
1525 function_pointer="__"$imagename"_statisics_setup"
1526 LOG_STAT_ARGS=$LOG_STAT_ARGS" "$($function_pointer)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001527 fi
1528 done
1529
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001530 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1531 ../common/genstat.sh $RUNMODE $SECONDS $TESTLOGS/$ATC/stat_data.csv $LOG_STAT_ARGS &
1532 fi
1533
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001534}
YongchaoWu9a84f512019-12-16 22:54:11 +01001535
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001536# Function to print the test result, shall be the last cmd in a test script
1537# args: -
1538# (Function for test scripts)
1539print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001540
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001541 TCTEST_END=$SECONDS
1542 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001543
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001544 echo "-------------------------------------------------------------------------------------------------"
1545 echo "------------------------------------- Test case: "$ATC
1546 echo "------------------------------------- Ended: "$(date)
1547 echo "-------------------------------------------------------------------------------------------------"
1548 echo "-- Description: "$TC_ONELINE_DESCR
1549 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001550 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001551 echo "-------------------------------------------------------------------------------------------------"
1552 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001553 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001554
YongchaoWu21f17bb2020-03-05 12:44:08 +01001555
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001556 if [ $RES_DEVIATION -gt 0 ]; then
1557 echo "Test case deviations"
1558 echo "===================================="
1559 cat $DEVIATION_FILE
1560 fi
1561 echo ""
1562 echo "Timer measurement in the test script"
1563 echo "===================================="
1564 column -t -s $'\t' $TIMER_MEASUREMENTS
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001565 if [ $RES_PASS != $RES_TEST ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001566 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 +01001567 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001568 echo ""
1569
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001570 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1571 echo "Runtime statistics collected in file: "$TESTLOGS/$ATC/stat_data.csv
1572 echo ""
1573 fi
1574
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001575 total=$((RES_PASS+RES_FAIL))
1576 if [ $RES_TEST -eq 0 ]; then
1577 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1578 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1579 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1580 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1581 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1582 elif [ $total != $RES_TEST ]; then
1583 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1584 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1585 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1586 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1587 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1588 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001589 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001590 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1591 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1592 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1593 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1594 elif [ $RES_PASS = $RES_TEST ]; then
1595 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1596 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1597 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1598 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1599 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1600 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001601
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001602 # Update test suite counter
1603 if [ -f .tmp_tcsuite_pass_ctr ]; then
1604 tmpval=$(< .tmp_tcsuite_pass_ctr)
1605 ((tmpval++))
1606 echo $tmpval > .tmp_tcsuite_pass_ctr
1607 fi
1608 if [ -f .tmp_tcsuite_pass ]; then
1609 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1610 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001611 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001612 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001613 else
1614 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1615 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
1616 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
1617 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
1618 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
1619 echo ""
1620 # Update test suite counter
1621 if [ -f .tmp_tcsuite_fail_ctr ]; then
1622 tmpval=$(< .tmp_tcsuite_fail_ctr)
1623 ((tmpval++))
1624 echo $tmpval > .tmp_tcsuite_fail_ctr
1625 fi
1626 if [ -f .tmp_tcsuite_fail ]; then
1627 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1628 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001629 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001630
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001631 echo "++++ Number of tests: "$RES_TEST
1632 echo "++++ Number of passed tests: "$RES_PASS
1633 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01001634 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001635 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001636 echo ""
1637 echo "++++ Number of test case deviations: "$RES_DEVIATION
1638 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001639 echo "------------------------------------- Test case complete ---------------------------------"
1640 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01001641 echo ""
1642}
1643
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001644#####################################################################
1645###### Functions for start, configuring, stoping, cleaning etc ######
1646#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01001647
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001648# Start timer for time measurement
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001649# args: <timer message to print> - timer value and message will be printed both on screen
1650# and in the timer measurement report - if at least one "print_timer is called"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001651start_timer() {
1652 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001653 TC_TIMER_STARTTIME=$SECONDS
1654 TC_TIMER_TIMER_TEXT="${@:1}"
1655 if [ $# -ne 1 ]; then
1656 __print_err "need 1 arg, <timer message to print>" $@
1657 TC_TIMER_TIMER_TEXT=${FUNCNAME[0]}":"${BASH_LINENO[0]}
1658 echo " Assigning timer name: "$TC_TIMER_TIMER_TEXT
1659 fi
1660 TC_TIMER_CURRENT_FAILS=$(($RES_FAIL+$RES_CONF_FAIL))
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001661 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001662}
1663
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001664# Print the running timer the value of the time (in seconds)
1665# Timer value and message will be printed both on screen and in the timer measurement report
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001666print_timer() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001667 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $TC_TIMER_TIMER_TEXT $EBOLD
1668 if [ -z "$TC_TIMER_STARTTIME" ]; then
1669 __print_err "timer not started" $@
1670 return 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001671 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001672 duration=$(($SECONDS-$TC_TIMER_STARTTIME))
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001673 if [ $duration -eq 0 ]; then
1674 duration="<1 second"
1675 else
1676 duration=$duration" seconds"
1677 fi
1678 echo " Timer duration :" $duration
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001679 res="-"
1680 if [ $(($RES_FAIL+$RES_CONF_FAIL)) -ne $TC_TIMER_CURRENT_FAILS ]; then
1681 res="Failures occured during test - timer not reliabled"
1682 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001683
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001684 echo -e "$TC_TIMER_TIMER_TEXT \t $duration \t $res" >> $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001685}
1686
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001687# Print info about a deviations from intended tests
1688# Each deviation counted is also printed in the testreport
1689# args <deviation message to print>
1690deviation() {
1691 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
1692 if [ $# -lt 1 ]; then
1693 ((RES_CONF_FAIL++))
1694 __print_err "need 1 or more args, <deviation message to print>" $@
1695 exit 1
1696 fi
1697 ((RES_DEVIATION++))
1698 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
1699 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02001700 __print_current_stats
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001701 echo ""
1702}
YongchaoWu21f17bb2020-03-05 12:44:08 +01001703
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001704# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
1705__check_stop_at_error() {
1706 if [ $STOP_AT_ERROR -eq 1 ]; then
1707 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
1708 store_logs "STOP_AT_ERROR"
1709 exit 1
1710 fi
1711 return 0
1712}
1713
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001714# Stop and remove all containers
1715# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001716# (Not for test scripts)
1717__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001718
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001719 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001720
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001721 #Create empty file
1722 running_contr_file="./tmp/running_contr.txt"
1723 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001724
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001725 # Get list of all containers started by the test script
1726 for imagename in $APP_SHORT_NAMES; do
1727 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 +01001728 done
BjornMagnussonXAecf34532021-11-24 08:25:15 +01001729 running_contr_file_empty="No docker containers running, started by previous test execution"
1730 if [ -s $running_contr_file ]; then
1731 running_contr_file_empty=""
1732 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001733
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001734 # Kill all containers started by the test env - to speed up shut down
1735 docker kill $(docker ps -a --filter "label=nrttest_app" --format '{{.Names}}') &> /dev/null
1736
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001737 tab_heading1="App display name"
1738 tab_heading2="App short name"
1739 tab_heading3="Container name"
1740
1741 tab_heading1_len=${#tab_heading1}
1742 tab_heading2_len=${#tab_heading2}
1743 tab_heading3_len=${#tab_heading3}
1744 cntr=0
1745 #Calc field lengths of each item in the list of containers
1746 while read p; do
1747 if (( $cntr % 3 == 0 ));then
1748 if [ ${#p} -gt $tab_heading1_len ]; then
1749 tab_heading1_len=${#p}
1750 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001751 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001752 if (( $cntr % 3 == 1));then
1753 if [ ${#p} -gt $tab_heading2_len ]; then
1754 tab_heading2_len=${#p}
1755 fi
1756 fi
1757 if (( $cntr % 3 == 2));then
1758 if [ ${#p} -gt $tab_heading3_len ]; then
1759 tab_heading3_len=${#p}
1760 fi
1761 fi
1762 let cntr=cntr+1
1763 done <$running_contr_file
1764
1765 let tab_heading1_len=tab_heading1_len+2
1766 while (( ${#tab_heading1} < $tab_heading1_len)); do
1767 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001768 done
1769
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001770 let tab_heading2_len=tab_heading2_len+2
1771 while (( ${#tab_heading2} < $tab_heading2_len)); do
1772 tab_heading2="$tab_heading2"" "
1773 done
1774
1775 let tab_heading3_len=tab_heading3_len+2
1776 while (( ${#tab_heading3} < $tab_heading3_len)); do
1777 tab_heading3="$tab_heading3"" "
1778 done
1779
BjornMagnussonXAecf34532021-11-24 08:25:15 +01001780 if [ ! -z "$running_contr_file_empty" ]; then
1781 echo $running_contr_file_empty | indent1
1782 else
1783 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
1784 cntr=0
1785 while read p; do
1786 if (( $cntr % 3 == 0 ));then
1787 row=""
1788 heading=$p
1789 heading_len=$tab_heading1_len
1790 fi
1791 if (( $cntr % 3 == 1));then
1792 heading=$p
1793 heading_len=$tab_heading2_len
1794 fi
1795 if (( $cntr % 3 == 2));then
1796 contr=$p
1797 heading=$p
1798 heading_len=$tab_heading3_len
1799 fi
1800 while (( ${#heading} < $heading_len)); do
1801 heading="$heading"" "
1802 done
1803 row=$row$heading
1804 if (( $cntr % 3 == 2));then
1805 echo -ne $row$SAMELINE
1806 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
1807 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1808 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
1809 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1810 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
1811 fi
1812 let cntr=cntr+1
1813 done <$running_contr_file
1814 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001815
YongchaoWu9a84f512019-12-16 22:54:11 +01001816 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001817
1818 echo -e $BOLD" Removing docker network"$EBOLD
1819 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
1820 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001821 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001822 if [ $? -ne 0 ]; then
1823 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
1824 exit 1
1825 fi
1826 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001827 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001828
1829 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001830 docker network prune --force | indent2
1831 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001832
1833 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001834 docker volume prune --force | indent2
1835 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001836
1837 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
1838 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001839 echo -e "$GREEN Done$EGREEN"
1840 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001841
1842 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
1843 if [ $? -eq 0 ]; then
1844 if [ $CONTRS -ne 0 ]; then
1845 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001846 docker ps -a | indent1
1847 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001848 fi
1849 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01001850}
1851
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001852###################################
1853### Functions for kube management
1854###################################
1855
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02001856# Get resource type for scaling
1857# args: <resource-name> <namespace>
1858__kube_get_resource_type() {
1859 kubectl get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1860 if [ $? -eq 0 ]; then
1861 echo "deployment"
1862 return 0
1863 fi
1864 kubectl get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1865 if [ $? -eq 0 ]; then
1866 echo "sts"
1867 return 0
1868 fi
1869 echo "unknown-resource-type"
1870 return 1
1871}
1872
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001873# Scale a kube resource to a specific count
1874# args: <resource-type> <resource-name> <namespace> <target-count>
1875# (Not for test scripts)
1876__kube_scale() {
1877 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
1878 kubectl scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
1879 if [ $? -ne 0 ]; then
1880 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
1881 ((RES_CONF_FAIL++))
1882 echo " Message: $(<./tmp/kubeerr)"
1883 return 1
1884 else
1885 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
1886 fi
1887
1888 TSTART=$SECONDS
1889
1890 for i in {1..500}; do
1891 count=$(kubectl get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
1892 retcode=$?
1893 if [ -z "$count" ]; then
1894 #No value is sometimes returned for some reason, in case the resource has replica 0
1895 count=0
1896 fi
1897 if [ $retcode -ne 0 ]; then
1898 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
1899 ((RES_CONF_FAIL++))
1900 return 1
1901 fi
1902 #echo ""
1903 if [ $count -ne $4 ]; then
1904 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
1905 sleep $i
1906 else
1907 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
1908 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
1909 echo ""
1910 return 0
1911 fi
1912 done
1913 echo ""
1914 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
1915 ((RES_CONF_FAIL++))
1916 return 0
1917}
1918
1919# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1920# This function does not wait for the resource to reach 0
1921# args: <namespace> <label-name> <label-id>
1922# (Not for test scripts)
1923__kube_scale_all_resources() {
1924 namespace=$1
1925 labelname=$2
1926 labelid=$3
1927 resources="deployment replicaset statefulset"
1928 for restype in $resources; do
1929 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1930 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001931 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001932 echo -ne " Ordered caling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001933 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001934 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001935 done
1936 fi
1937 done
1938}
1939
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001940# 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 +01001941# This function do wait for the resource to reach 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001942# args: <namespace> <label-name> [ <label-id> ]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001943# (Not for test scripts)
1944__kube_scale_and_wait_all_resources() {
1945 namespace=$1
1946 labelname=$2
1947 labelid=$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001948 if [ -z "$3" ]; then
1949 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname"
1950 else
1951 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname=$labelid"
1952 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001953 resources="deployment replicaset statefulset"
1954 scaled_all=1
1955 while [ $scaled_all -ne 0 ]; do
1956 scaled_all=0
1957 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001958 if [ -z "$3" ]; then
1959 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname')].metadata.name}')
1960 else
1961 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1962 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001963 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1964 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001965 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001966 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
1967 count=1
1968 T_START=$SECONDS
1969 while [ $count -ne 0 ]; do
1970 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001971 echo -ne " Scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001972 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
1973 sleep 0.5
1974 else
1975 count=0
1976 fi
1977 duration=$(($SECONDS-$T_START))
1978 if [ $duration -gt 100 ]; then
1979 #Forcring count 0, to avoid hanging for failed scaling
1980 scaled_all=1
1981 count=0
1982 fi
1983 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001984 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 +01001985 done
1986 fi
1987 done
1988 done
1989}
1990
1991# Remove all kube resources in a namespace for resources having a certain label and label-id
1992# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
1993# args: <namespace> <label-name> <label-id>
1994# (Not for test scripts)
1995__kube_delete_all_resources() {
1996 namespace=$1
1997 labelname=$2
1998 labelid=$3
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001999 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002000 deleted_resourcetypes=""
2001 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002002 ns_flag="-n $namespace"
2003 ns_text="in namespace $namespace"
2004 if [ $restype == "persistentvolumes" ]; then
2005 ns_flag=""
2006 ns_text=""
2007 fi
2008 result=$(kubectl get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002009 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2010 deleted_resourcetypes=$deleted_resourcetypes" "$restype
2011 for resid in $result; do
2012 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
2013 count=1
2014 while [ $count -ne 0 ]; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002015 count=$(kubectl get $restype $resid $ns_flag -o jsonpath='{.status.replicas}' 2> /dev/null)
2016 echo -ne " Scaling $restype $resid $ns_text with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002017 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2018 sleep 0.5
2019 else
2020 count=0
2021 fi
2022 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002023 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 +01002024 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002025 echo -ne " Deleting $restype $resid $ns_text with label $labelname=$labelid "$SAMELINE
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002026 kubectl delete --grace-period=1 $restype $resid $ns_flag 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002027 if [ $? -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002028 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002029 else
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002030 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002031 fi
2032 #fi
2033 done
2034 fi
2035 done
2036 if [ ! -z "$deleted_resourcetypes" ]; then
2037 for restype in $deleted_resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002038 ns_flag="-n $namespace"
2039 ns_text="in namespace $namespace"
2040 if [ $restype == "persistentvolumes" ]; then
2041 ns_flag=""
2042 ns_text=""
2043 fi
2044 echo -ne " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted..."$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002045 T_START=$SECONDS
2046 result="dummy"
2047 while [ ! -z "$result" ]; do
2048 sleep 0.5
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002049 result=$(kubectl get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
2050 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 +01002051 if [ -z "$result" ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002052 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 +01002053 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002054 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 +01002055 result=""
2056 fi
2057 done
2058 done
2059 fi
2060}
2061
2062# Creates a namespace if it does not exists
2063# args: <namespace>
2064# (Not for test scripts)
2065__kube_create_namespace() {
2066
2067 #Check if test namespace exists, if not create it
2068 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
2069 if [ $? -ne 0 ]; then
2070 echo -ne " Creating namespace "$1 $SAMELINE
2071 kubectl create namespace $1 1> /dev/null 2> ./tmp/kubeerr
2072 if [ $? -ne 0 ]; then
2073 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2074 ((RES_CONF_FAIL++))
2075 echo " Message: $(<./tmp/kubeerr)"
2076 return 1
2077 else
2078 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2079 fi
2080 else
2081 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
2082 fi
2083 return 0
2084}
2085
2086# Find the host ip of an app (using the service resource)
2087# args: <app-name> <namespace>
2088# (Not for test scripts)
2089__kube_get_service_host() {
2090 if [ $# -ne 2 ]; then
2091 ((RES_CONF_FAIL++))
2092 __print_err "need 2 args, <app-name> <namespace>" $@
2093 exit 1
2094 fi
2095 for timeout in {1..60}; do
2096 host=$(kubectl get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
2097 if [ $? -eq 0 ]; then
2098 if [ ! -z "$host" ]; then
2099 echo $host
2100 return 0
2101 fi
2102 fi
2103 sleep 0.5
2104 done
2105 ((RES_CONF_FAIL++))
2106 echo "host-not-found-fatal-error"
2107 return 1
2108}
2109
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002110# Find the named port to an app (using the service resource)
2111# args: <app-name> <namespace> <port-name>
2112# (Not for test scripts)
2113__kube_get_service_port() {
2114 if [ $# -ne 3 ]; then
2115 ((RES_CONF_FAIL++))
2116 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2117 exit 1
2118 fi
2119
2120 for timeout in {1..60}; do
2121 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
2122 if [ $? -eq 0 ]; then
2123 if [ ! -z "$port" ]; then
2124 echo $port
2125 return 0
2126 fi
2127 fi
2128 sleep 0.5
2129 done
2130 ((RES_CONF_FAIL++))
2131 echo "0"
2132 return 1
2133}
2134
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002135# Find the named node port to an app (using the service resource)
2136# args: <app-name> <namespace> <port-name>
2137# (Not for test scripts)
2138__kube_get_service_nodeport() {
2139 if [ $# -ne 3 ]; then
2140 ((RES_CONF_FAIL++))
2141 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2142 exit 1
2143 fi
2144
2145 for timeout in {1..60}; do
2146 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
2147 if [ $? -eq 0 ]; then
2148 if [ ! -z "$port" ]; then
2149 echo $port
2150 return 0
2151 fi
2152 fi
2153 sleep 0.5
2154 done
2155 ((RES_CONF_FAIL++))
2156 echo "0"
2157 return 1
2158}
2159
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002160# Create a kube resource from a yaml template
2161# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
2162# (Not for test scripts)
2163__kube_create_instance() {
2164 echo -ne " Creating $1 $2"$SAMELINE
2165 envsubst < $3 > $4
2166 kubectl apply -f $4 1> /dev/null 2> ./tmp/kubeerr
2167 if [ $? -ne 0 ]; then
2168 ((RES_CONF_FAIL++))
2169 echo -e " Creating $1 $2 $RED Failed $ERED"
2170 echo " Message: $(<./tmp/kubeerr)"
2171 return 1
2172 else
2173 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
2174 fi
2175}
2176
2177# Function to create a configmap in kubernetes
2178# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
2179# (Not for test scripts)
2180__kube_create_configmap() {
2181 echo -ne " Creating configmap $1 "$SAMELINE
2182 envsubst < $5 > $5"_tmp"
2183 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
2184 kubectl create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
2185 if [ $? -ne 0 ]; then
2186 echo -e " Creating configmap $1 $RED Failed $ERED"
2187 ((RES_CONF_FAIL++))
2188 return 1
2189 fi
2190
2191 kubectl apply -f $6 1> /dev/null 2> ./tmp/kubeerr
2192 if [ $? -ne 0 ]; then
2193 echo -e " Creating configmap $1 $RED Apply failed $ERED"
2194 echo " Message: $(<./tmp/kubeerr)"
2195 ((RES_CONF_FAIL++))
2196 return 1
2197 fi
2198 kubectl label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
2199 if [ $? -ne 0 ]; then
2200 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
2201 echo " Message: $(<./tmp/kubeerr)"
2202 ((RES_CONF_FAIL++))
2203 return 1
2204 fi
2205 # Log the resulting map
2206 kubectl get configmap $1 -n $2 -o yaml > $6
2207
2208 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
2209 return 0
2210}
2211
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002212# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
2213# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002214# args: <full kubectl cmd with parameters>
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002215# (Not for test scripts)
2216__kube_cmd_with_timeout() {
2217 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
2218
2219 while true; do
2220 kube_cmd_result=$($@)
2221 if [ $? -ne 0 ]; then
2222 kube_cmd_result=""
2223 fi
2224 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
2225 echo $kube_cmd_result
2226 return 0
2227 fi
2228 sleep 1
2229 done
2230}
2231
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002232# This function starts a pod that cleans a the contents of a path mounted as a pvc
2233# After this action the pod should terminate
2234# This should only be executed when the pod owning the pvc is not running
2235# args: <appname> <namespace> <pvc-name> <path-to remove>
2236# (Not for test scripts)
2237__kube_clean_pvc() {
2238
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002239 #using env vars setup in pvccleaner_api_functions.sh
2240
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002241 export PVC_CLEANER_NAMESPACE=$2
2242 export PVC_CLEANER_CLAIMNAME=$3
2243 export PVC_CLEANER_RM_PATH=$4
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002244 input_yaml=$SIM_GROUP"/"$PVC_CLEANER_COMPOSE_DIR"/"pvc-cleaner.yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002245 output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml
2246
2247 envsubst < $input_yaml > $output_yaml
2248
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002249 kubectl delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002250
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002251 __kube_create_instance pod $PVC_CLEANER_APP_NAME $input_yaml $output_yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002252 if [ $? -ne 0 ]; then
2253 echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work"
2254 return 1
2255 fi
2256
2257 term_ts=$(($SECONDS+30))
2258 while [ $term_ts -gt $SECONDS ]; do
2259 pod_status=$(kubectl get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase")
2260 if [ "$pod_status" == "Succeeded" ]; then
2261 return 0
2262 fi
2263 done
2264 return 1
2265}
2266
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002267# This function scales or deletes all resources for app selected by the testcase.
2268# args: -
2269# (Not for test scripts)
2270__clean_kube() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002271 echo -e $BOLD"Initialize kube pods/statefulsets/replicaset to initial state"$EBOLD
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002272
2273 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002274 for imagename in $APP_SHORT_NAMES; do
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002275 # A function name is created from the app short name
2276 # for example app short name 'RICMSIM' -> produce the function
2277 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
2278 # This function is called and is expected to exist in the imported
2279 # file for the ricsim test functions
2280 # The resulting function impl shall scale the resources to 0
2281 # For prestarted apps, the function waits until the resources are 0
2282 # For included (not prestated) apps, the scaling is just ordered
2283 __check_prestarted_image $imagename
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002284 if [ $? -eq 0 ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002285 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002286 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2287 $function_pointer
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002288 else
2289 __check_included_image $imagename
2290 if [ $? -eq 0 ]; then
2291 function_pointer="__"$imagename"_kube_scale_zero"
2292 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2293 $function_pointer
2294 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002295 fi
2296 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002297
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002298 # Delete managed apps
2299 for imagename in $APP_SHORT_NAMES; do
2300 __check_included_image $imagename
2301 if [ $? -eq 0 ]; then
2302 __check_prestarted_image $imagename
2303 if [ $? -ne 0 ]; then
2304 # A function name is created from the app short name
2305 # for example app short name 'RICMSIM' -> produce the function
2306 # name __RICSIM__kube_delete_all
2307 # This function is called and is expected to exist in the imported
2308 # file for the ricsim test functions
2309 # The resulting function impl shall delete all its resources
2310 function_pointer="__"$imagename"_kube_delete_all"
2311 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
2312 $function_pointer
2313 fi
2314 fi
2315 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002316
2317 echo ""
2318}
2319
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002320# Function stop and remove all containers (docker) and services/deployments etc(kube)
2321# args: -
2322# Function for test script
2323clean_environment() {
2324 if [ $RUNMODE == "KUBE" ]; then
2325 __clean_kube
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002326 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002327 echo " Cleaning docker resouces to free up resources, may take time..."
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002328 ../common/clean_docker.sh 2&>1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002329 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002330 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002331 else
2332 __clean_containers
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002333 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002334 echo " Cleaning kubernetes resouces to free up resources, may take time..."
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002335 ../common/clean_kube.sh 2&>1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002336 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002337 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002338 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002339}
2340
2341# 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 +01002342# args: -
2343# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002344auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002345 echo
2346 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002347 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
2348 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01002349 fi
2350}
2351
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002352# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002353# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002354# (Function for test scripts)
2355sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01002356
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002357 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
2358 if [ $# -lt 1 ]; then
2359 ((RES_CONF_FAIL++))
2360 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
2361 exit 1
2362 fi
2363 #echo "---- Sleep for " $1 " seconds ---- "$2
2364 start=$SECONDS
2365 duration=$((SECONDS-start))
2366 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002367 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002368 sleep 1
2369 duration=$((SECONDS-start))
2370 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002371 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002372 echo ""
2373}
2374
2375# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2376# Not to be called from the test script itself.
2377__print_err() {
2378 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2379 if [ $# -gt 1 ]; then
2380 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2381 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002382 ((RES_CONF_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002383}
2384
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002385# Function to create the docker network for the test
2386# Not to be called from the test script itself.
2387__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002388 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002389 if [ $? -ne 0 ]; then
2390 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2391 return 1
2392 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002393 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002394 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2395 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002396 if [ $? -ne 0 ]; then
2397 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2398 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002399 else
2400 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002401 fi
2402 else
2403 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2404 fi
2405}
2406
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002407# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002408# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2409#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002410# (Not for test scripts)
2411__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002412
2413 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002414 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002415 __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 +01002416 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002417 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002418
2419 __create_docker_network
2420
2421 curdir=$PWD
2422 cd $SIM_GROUP
2423 compose_dir=$1
2424 cd $1
2425 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002426 compose_file=$1
2427 if [ -z "$compose_file" ]; then
2428 compose_file="docker-compose.yml"
2429 fi
2430 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002431 compose_args=$1
2432 shift
2433 appcount=$1
2434 shift
2435
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002436 envsubst < $compose_file > "gen_"$compose_file
2437 compose_file="gen_"$compose_file
2438
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002439 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002440 docker-compose -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002441 if [ $? -ne 0 ]; then
2442 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2443 cat .dockererr
2444 echo -e $RED"Stopping script...."$ERED
2445 exit 1
2446 fi
2447 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002448 docker-compose -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002449 if [ $? -ne 0 ]; then
2450 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2451 cat .dockererr
2452 echo -e $RED"Stopping script...."$ERED
2453 exit 1
2454 fi
2455 fi
2456
2457 cd $curdir
2458
2459 appindex=0
2460 while [ $appindex -lt $appcount ]; do
2461 appname=$1
2462 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002463 app_started=0
2464 for i in {1..10}; do
2465 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002466 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 +02002467 app_started=1
2468 break
2469 else
2470 sleep $i
2471 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002472 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002473 if [ $app_started -eq 0 ]; then
2474 ((RES_CONF_FAIL++))
2475 echo ""
2476 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002477 echo -e $RED" Stopping script..."$ERED
2478 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002479 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002480 let appindex=appindex+1
2481 done
2482 return 0
2483}
2484
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002485# Function to check if container/service is responding to http/https
2486# args: <container-name>|<service-name> url
2487# (Not for test scripts)
2488__check_service_start() {
2489
2490 if [ $# -ne 2 ]; then
2491 ((RES_CONF_FAIL++))
2492 __print_err "need 2 args, <container-name>|<service-name> url" $@
2493 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002494 fi
2495
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002496 if [ $RUNMODE == "KUBE" ]; then
2497 ENTITY="service/set/deployment"
2498 else
2499 ENTITY="container"
2500 fi
2501 appname=$1
2502 url=$2
2503 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2504
2505
YongchaoWu9a84f512019-12-16 22:54:11 +01002506 pa_st=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002507 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002508 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002509 loop_ctr=0
2510 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002511 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002512 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002513 if [ ${#result} -gt 15 ]; then
2514 #If response is too long, truncate
2515 result="...response text too long, omitted"
2516 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002517 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2518 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 +01002519 pa_st=true
2520 break
2521 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002522 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002523 TS_OFFSET=$loop_ctr
2524 if (( $TS_OFFSET > 5 )); then
2525 TS_OFFSET=5
2526 fi
2527 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
2528 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 +02002529 sleep 1
2530 done
YongchaoWu9a84f512019-12-16 22:54:11 +01002531 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002532 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01002533 done
2534
2535 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002536 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002537 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 +01002538 return 1
2539 fi
2540
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002541 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002542 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002543}
2544
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002545
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002546#################
2547### Log functions
2548#################
2549
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002550__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002551
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002552 dispname=$1
2553 appname=$2
2554 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002555 warning=$4
2556 error=$5
2557
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002558 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
2559
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002560 if [ $RUNMODE == "KUBE" ]; then
2561 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
2562 return
2563 fi
2564
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002565 #tmp=$(docker ps | grep $appname)
2566 tmp=$(docker ps -q --filter name=$appname) #get the container id
2567 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002568 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002569 return
2570 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002571 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002572 if [ $? -ne 0 ];then
2573 echo " Problem to search $appname log $logpath"
2574 else
2575 if [ $foundentries -eq 0 ]; then
2576 echo " No WARN entries found in $appname log $logpath"
2577 else
2578 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
2579 fi
2580 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002581 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002582 if [ $? -ne 0 ];then
2583 echo " Problem to search $appname log $logpath"
2584 else
2585 if [ $foundentries -eq 0 ]; then
2586 echo " No ERR entries found in $appname log $logpath"
2587 else
2588 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
2589 fi
2590 fi
2591 echo ""
2592}
2593
2594# Store all container logs and other logs in the log dir for the script
2595# Logs are stored with a prefix in case logs should be stored several times during a test
2596# args: <logfile-prefix>
2597# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01002598store_logs() {
2599 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002600 ((RES_CONF_FAIL++))
2601 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01002602 exit 1
2603 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002604 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 +01002605
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002606 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002607
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002608 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002609
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002610 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
2611
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002612 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002613
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002614 # Store docker logs for all container
2615 for imagename in $APP_SHORT_NAMES; do
2616 __check_included_image $imagename
2617 if [ $? -eq 0 ]; then
2618 # A function name is created from the app short name
2619 # for example app short name 'RICMSIM' -> produce the function
2620 # name __RICSIM__store_docker_logs
2621 # This function is called and is expected to exist in the imported
2622 # file for the ricsim test functions
2623 # The resulting function impl shall store the docker logs for each container
2624 function_pointer="__"$imagename"_store_docker_logs"
2625 $function_pointer "$TESTLOGS/$ATC/" $1
2626 fi
2627 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002628 fi
2629 if [ $RUNMODE == "KUBE" ]; then
2630 namespaces=$(kubectl get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
2631 for nsid in $namespaces; do
2632 pods=$(kubectl get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
2633 for podid in $pods; do
2634 kubectl logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
2635 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002636 done
2637 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002638 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01002639}
2640
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002641###############
2642## Generic curl
2643###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002644# Generic curl function, assumes all 200-codes are ok
2645# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002646# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
2647# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01002648__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002649 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002650 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002651 if [ ! -z "$KUBE_PROXY_PATH" ]; then
2652 if [ $KUBE_PROXY_HTTPX == "http" ]; then
2653 proxyflag=" --proxy $KUBE_PROXY_PATH"
2654 else
2655 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002656 fi
2657 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002658 curlString="curl -skw %{http_code} $proxyflag $@"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002659 echo " CMD: $curlString" >> $HTTPLOG
2660 res=$($curlString)
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002661 retcode=$?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002662 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002663 echo " RETCODE: $retcode" >> $HTTPLOG
2664 if [ $retcode -ne 0 ]; then
2665 echo "<no-response-from-server>"
2666 return 1
2667 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002668 http_code="${res:${#res}-3}"
2669 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002670 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
2671 echo "<no-response-from-server>"
2672 return 1
2673 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002674 return 0
2675 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002676 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002677 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01002678 echo "<not found, resp:${http_code}>"
2679 return 1
2680 fi
2681 if [ $# -eq 2 ]; then
2682 echo "${res:0:${#res}-3}" | xargs
2683 else
2684 echo "${res:0:${#res}-3}"
2685 fi
2686
2687 return 0
2688 fi
2689}
2690
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002691#######################################
2692### Basic helper function for test cases
2693#######################################
2694
2695# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
2696# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
2697# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
2698# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
2699# before setting pass or fail depending on the result of comparing variable and target using the operator.
2700# 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.
2701# Not to be called from test script.
2702
2703__var_test() {
2704 checkjsonarraycount=0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002705 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002706 if [ $# -eq 6 ]; then
2707 if [[ $3 == "json:"* ]]; then
2708 checkjsonarraycount=1
2709 fi
2710
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002711 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002712 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG
2713
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002714 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002715 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002716 start=$SECONDS
2717 ctr=0
2718 for (( ; ; )); do
2719 if [ $checkjsonarraycount -eq 0 ]; then
2720 result="$(__do_curl $2$3)"
2721 retcode=$?
2722 result=${result//[[:blank:]]/} #Strip blanks
2723 else
2724 path=${3:5}
2725 result="$(__do_curl $2$path)"
2726 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002727 echo "$result" > ./tmp/.tmp.curl.json
2728 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002729 fi
2730 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002731 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002732 let ctr=ctr+1
2733 if [ $retcode -ne 0 ]; then
2734 if [ $duration -gt $6 ]; then
2735 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002736 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002737 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002738 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002739 return
2740 fi
2741 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2742 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002743 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002744 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002745 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002746 return
2747 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2748 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002749 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002750 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002751 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002752 return
2753 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2754 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002755 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002756 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002757 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002758 return
2759 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2760 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002761 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002762 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002763 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002764 return
2765 else
2766 if [ $duration -gt $6 ]; then
2767 ((RES_FAIL++))
2768 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002769 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002770 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002771 return
2772 fi
2773 fi
2774 sleep 1
2775 done
2776 elif [ $# -eq 5 ]; then
2777 if [[ $3 == "json:"* ]]; then
2778 checkjsonarraycount=1
2779 fi
2780
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002781 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002782 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002783 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002784 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002785 if [ $checkjsonarraycount -eq 0 ]; then
2786 result="$(__do_curl $2$3)"
2787 retcode=$?
2788 result=${result//[[:blank:]]/} #Strip blanks
2789 else
2790 path=${3:5}
2791 result="$(__do_curl $2$path)"
2792 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002793 echo "$result" > ./tmp/.tmp.curl.json
2794 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002795 fi
2796 if [ $retcode -ne 0 ]; then
2797 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002798 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002799 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002800 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002801 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2802 ((RES_PASS++))
2803 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002804 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002805 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2806 ((RES_PASS++))
2807 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002808 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002809 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2810 ((RES_PASS++))
2811 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002812 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002813 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2814 ((RES_PASS++))
2815 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002816 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002817 else
2818 ((RES_FAIL++))
2819 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002820 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002821 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002822 fi
2823 else
2824 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
2825 echo "Got:" $@
2826 exit 1
2827 fi
2828}