blob: 66cbd96bbe079304a4ace330922b2a8b198dbf94 [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]"
BjornMagnussonXA674793d2021-05-06 19:49:17 +020030 echo " [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>]"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010031}
32
33if [ $# -eq 1 ] && [ "$1" == "help" ]; then
34
35 if [ ! -z "$TC_ONELINE_DESCR" ]; then
36 echo "Test script description:"
37 echo $TC_ONELINE_DESCR
38 echo ""
39 fi
40 __print_args
41 echo ""
42 echo "remote - Use images from remote repositories. Can be overridden for individual images using the '--use_xxx' flags"
43 echo "remote-remove - Same as 'remote' but will also try to pull fresh images from remote repositories"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010044 echo "docker - Test executed in docker environment"
45 echo "kube - Test executed in kubernetes environment - requires an already started kubernetes environment"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010046 echo "--env-file - The script will use the supplied file to read environment variables from"
47 echo "release - If this flag is given the script will use release version of the images"
48 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."
49 echo "--stop-at-error - The script will stop when the first failed test or configuration"
50 echo "--ricsim-prefix - The a1 simulator will use the supplied string as container prefix instead of 'ricsim'"
51 echo "--use-local-image - The script will use local images for the supplied apps, space separated list of app short names"
52 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"
53 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"
54 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 +020055 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 +020056 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 +020057 echo "--cluster-timeout - Optional timeout for cluster where it takes time to obtain external ip/host-name. Timeout in seconds. "
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010058 echo ""
59 echo "List of app short names supported: "$APP_SHORT_NAMES
60 exit 0
61fi
62
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010063AUTOTEST_HOME=$PWD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020064# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
65# FTC1.sh -> ATC == FTC1
66ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
67
68#Create result file (containing '1' for error) for this test case
69#Will be replaced with a file containing '0' if all test cases pass
70echo "1" > "$PWD/.result$ATC.txt"
71
BjornMagnussonXA80a92002020-03-19 14:31:06 +010072#Formatting for 'echo' cmd
73BOLD="\033[1m"
74EBOLD="\033[0m"
75RED="\033[31m\033[1m"
76ERED="\033[0m"
77GREEN="\033[32m\033[1m"
78EGREEN="\033[0m"
79YELLOW="\033[33m\033[1m"
80EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020081SAMELINE="\033[0K\r"
82
BjornMagnussonXA80a92002020-03-19 14:31:06 +010083# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020084echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +010085
BjornMagnussonXA575869c2020-09-14 21:28:54 +020086# default test environment variables
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020087TEST_ENV_VAR_FILE=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +010088
89echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
90
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010091#Localhost constants
92LOCALHOST_NAME="localhost"
93LOCALHOST_HTTP="http://localhost"
94LOCALHOST_HTTPS="https://localhost"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020095
BjornMagnussonXA80a92002020-03-19 14:31:06 +010096# Var to hold 'auto' in case containers shall be stopped when test case ends
97AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +010098
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010099# Var to hold the app names to use local images for
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200100USE_LOCAL_IMAGES=""
101
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100102# Var to hold the app names to use remote snapshot images for
103USE_SNAPSHOT_IMAGES=""
104
105# Var to hold the app names to use remote staging images for
106USE_STAGING_IMAGES=""
107
108# Var to hold the app names to use remote release images for
109USE_RELEASE_IMAGES=""
110
BjornMagnussonXAad047782020-06-08 15:54:11 +0200111
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200112# 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
113STOP_AT_ERROR=0
114
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100115# The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used.
116# The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used
117# Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE
118IMAGE_CATEGORY="DEV"
119
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200120# Function to indent cmd output with one space
121indent1() { sed 's/^/ /'; }
122
123# Function to indent cmd output with two spaces
124indent2() { sed 's/^/ /'; }
125
YongchaoWu9a84f512019-12-16 22:54:11 +0100126# Set a description string for the test case
127if [ -z "$TC_ONELINE_DESCR" ]; then
128 TC_ONELINE_DESCR="<no-description>"
129 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
130fi
131
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100132# Counter for test suites
133if [ -f .tmp_tcsuite_ctr ]; then
134 tmpval=$(< .tmp_tcsuite_ctr)
135 ((tmpval++))
136 echo $tmpval > .tmp_tcsuite_ctr
137fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100138
YongchaoWu9a84f512019-12-16 22:54:11 +0100139# Create the logs dir if not already created in the current dir
140if [ ! -d "logs" ]; then
141 mkdir logs
142fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100143TESTLOGS=$PWD/logs
144
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200145# Create the tmp dir for temporary files that is not needed after the test
146# hidden files for the test env is still stored in the current dir
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100147# files in the ./tmp is moved to ./tmp/prev when a new test is started
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200148if [ ! -d "tmp" ]; then
149 mkdir tmp
150fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100151curdir=$PWD
152cd tmp
153if [ $? -ne 0 ]; then
154 echo "Cannot cd to $PWD/tmp"
155 echo "Dir cannot be created. Exiting...."
156fi
157if [ ! -d "prev" ]; then
158 mkdir prev
159fi
160cd $curdir
161mv ./tmp/* ./tmp/prev 2> /dev/null
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200162
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100163# Create a http message log for this testcase
164HTTPLOG=$PWD"/.httplog_"$ATC".txt"
165echo "" > $HTTPLOG
166
167# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100168mkdir -p $TESTLOGS/$ATC
169
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100170# Save create for current logs
171mkdir -p $TESTLOGS/$ATC/previous
172
173rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
174rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
175rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
176
177mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
178mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
179mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
180
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100181# Clear the log dir for the test case
182rm $TESTLOGS/$ATC/*.log &> /dev/null
183rm $TESTLOGS/$ATC/*.txt &> /dev/null
184rm $TESTLOGS/$ATC/*.json &> /dev/null
185
186# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100187TCLOG=$TESTLOGS/$ATC/TC.log
188exec &> >(tee ${TCLOG})
189
190#Variables for counting tests as well as passed and failed tests
191RES_TEST=0
192RES_PASS=0
193RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100194RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200195RES_DEVIATION=0
196
197#File to keep deviation messages
198DEVIATION_FILE=".tmp_deviations"
199rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100200
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100201# Trap "command not found" and make the script fail
202trap_fnc() {
203
204 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100205 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100206 ((RES_CONF_FAIL++))
207 fi
208}
209trap trap_fnc ERR
210
211# Counter for tests
212TEST_SEQUENCE_NR=1
213
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100214# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100215__log_test_start() {
216 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
217 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
218 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
219 ((RES_TEST++))
220 ((TEST_SEQUENCE_NR++))
221}
222
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100223# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100224__log_test_fail_general() {
225 echo -e $RED" FAIL."$1 $ERED
226 ((RES_FAIL++))
227 __check_stop_at_error
228}
229
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100230# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100231__log_test_fail_status_code() {
232 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
233 ((RES_FAIL++))
234 __check_stop_at_error
235}
236
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100237# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100238__log_test_fail_body() {
239 echo -e $RED" FAIL, returned body not correct"$ERED
240 ((RES_FAIL++))
241 __check_stop_at_error
242}
243
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100244# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100245__log_test_fail_not_supported() {
246 echo -e $RED" FAIL, function not supported"$ERED
247 ((RES_FAIL++))
248 __check_stop_at_error
249}
250
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100251# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100252__log_test_pass() {
253 if [ $# -gt 0 ]; then
254 echo $@
255 fi
256 ((RES_PASS++))
257 echo -e $GREEN" PASS"$EGREEN
258}
259
260#Counter for configurations
261CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100262
263# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100264__log_conf_start() {
265 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
266 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
267 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
268 ((CONF_SEQUENCE_NR++))
269}
270
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100271# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100272__log_conf_fail_general() {
273 echo -e $RED" FAIL."$1 $ERED
274 ((RES_CONF_FAIL++))
275 __check_stop_at_error
276}
277
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100278# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100279__log_conf_fail_status_code() {
280 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
281 ((RES_CONF_FAIL++))
282 __check_stop_at_error
283}
284
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100285# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100286__log_conf_fail_body() {
287 echo -e $RED" FAIL, returned body not correct"$ERED
288 ((RES_CONF_FAIL++))
289 __check_stop_at_error
290}
291
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100292# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100293__log_conf_ok() {
294 if [ $# -gt 0 ]; then
295 echo $@
296 fi
297 echo -e $GREEN" OK"$EGREEN
298}
299
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100300#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100301TCTEST_START=$SECONDS
302
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200303#File to save timer measurement results
304TIMER_MEASUREMENTS=".timer_measurement.txt"
305echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
306
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200307# 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 +0200308IMAGE_REPO_ADR=""
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200309IMAGE_REPO_POLICY="local"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200310CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200311
YongchaoWu9a84f512019-12-16 22:54:11 +0100312echo "-------------------------------------------------------------------------------------------------"
313echo "----------------------------------- Test case: "$ATC
314echo "----------------------------------- Started: "$(date)
315echo "-------------------------------------------------------------------------------------------------"
316echo "-- Description: "$TC_ONELINE_DESCR
317echo "-------------------------------------------------------------------------------------------------"
318echo "----------------------------------- Test case setup -----------------------------------"
319
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100320echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200321START_ARG=$1
322paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100323paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200324if [ $# -lt 1 ]; then
325 paramerror=1
326fi
327if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100328 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200329 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100330 if [ -z "$paramerror_str" ]; then
331 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
332 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200333 else
334 shift;
335 fi
336fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100337if [ $paramerror -eq 0 ]; then
338 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
339 paramerror=1
340 if [ -z "$paramerror_str" ]; then
341 paramerror_str="Second arg shall be 'docker' or 'kube'"
342 fi
343 else
344 if [ $1 == "docker" ]; then
345 RUNMODE="DOCKER"
346 echo "Setting RUNMODE=DOCKER"
347 fi
348 if [ $1 == "kube" ]; then
349 RUNMODE="KUBE"
350 echo "Setting RUNMODE=KUBE"
351 fi
352 shift;
353 fi
354fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200355foundparm=0
356while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
357 foundparm=1
358 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100359 if [ "$1" == "release" ]; then
360 IMAGE_CATEGORY="RELEASE"
361 echo "Option set - Release image tags used for applicable images "
362 shift;
363 foundparm=0
364 fi
365 fi
366 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200367 if [ "$1" == "auto-clean" ]; then
368 AUTO_CLEAN="auto"
369 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200370 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200371 foundparm=0
372 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200373 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200374 if [ $paramerror -eq 0 ]; then
375 if [ "$1" == "--stop-at-error" ]; then
376 STOP_AT_ERROR=1
377 echo "Option set - Stop at first error"
378 shift;
379 foundparm=0
380 fi
381 fi
382 if [ $paramerror -eq 0 ]; then
383 if [ "$1" == "--ricsim-prefix" ]; then
384 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100385 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200386 if [ -z "$1" ]; then
387 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100388 if [ -z "$paramerror_str" ]; then
389 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
390 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200391 else
392 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
393 shift;
394 foundparm=0
395 fi
396 fi
397 fi
398 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200399 if [ "$1" == "--env-file" ]; then
400 shift;
401 TEST_ENV_VAR_FILE=$1
402 if [ -z "$1" ]; then
403 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100404 if [ -z "$paramerror_str" ]; then
405 paramerror_str="No env file found for flag: '--env-file'"
406 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200407 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200408 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200409 shift;
410 foundparm=0
411 fi
412 fi
413 fi
414 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200415 if [ "$1" == "--use-local-image" ]; then
416 USE_LOCAL_IMAGES=""
417 shift
418 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
419 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100420 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200421 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100422 if [ -z "$paramerror_str" ]; then
423 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
424 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200425 fi
426 shift;
427 done
428 foundparm=0
429 if [ -z "$USE_LOCAL_IMAGES" ]; then
430 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100431 if [ -z "$paramerror_str" ]; then
432 paramerror_str="No app name found for flag: '--use-local-image'"
433 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200434 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100435 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
436 fi
437 fi
438 fi
439 if [ $paramerror -eq 0 ]; then
440 if [ "$1" == "--use-snapshot-image" ]; then
441 USE_SNAPSHOT_IMAGES=""
442 shift
443 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
444 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
445 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
446 paramerror=1
447 if [ -z "$paramerror_str" ]; then
448 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
449 fi
450 fi
451 shift;
452 done
453 foundparm=0
454 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
455 paramerror=1
456 if [ -z "$paramerror_str" ]; then
457 paramerror_str="No app name found for flag: '--use-snapshot-image'"
458 fi
459 else
460 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
461 fi
462 fi
463 fi
464 if [ $paramerror -eq 0 ]; then
465 if [ "$1" == "--use-staging-image" ]; then
466 USE_STAGING_IMAGES=""
467 shift
468 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
469 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
470 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
471 paramerror=1
472 if [ -z "$paramerror_str" ]; then
473 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
474 fi
475 fi
476 shift;
477 done
478 foundparm=0
479 if [ -z "$USE_STAGING_IMAGES" ]; then
480 paramerror=1
481 if [ -z "$paramerror_str" ]; then
482 paramerror_str="No app name found for flag: '--use-staging-image'"
483 fi
484 else
485 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
486 fi
487 fi
488 fi
489 if [ $paramerror -eq 0 ]; then
490 if [ "$1" == "--use-release-image" ]; then
491 USE_RELEASE_IMAGES=""
492 shift
493 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
494 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
495 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
496 paramerror=1
497 if [ -z "$paramerror_str" ]; then
498 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
499 fi
500 fi
501 shift;
502 done
503 foundparm=0
504 if [ -z "$USE_RELEASE_IMAGES" ]; then
505 paramerror=1
506 if [ -z "$paramerror_str" ]; then
507 paramerror_str="No app name found for flag: '--use-release-image'"
508 fi
509 else
510 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200511 fi
512 fi
513 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200514 if [ $paramerror -eq 0 ]; then
515 if [ "$1" == "--image-repo" ]; then
516 shift;
517 IMAGE_REPO_ADR=$1
518 if [ -z "$1" ]; then
519 paramerror=1
520 if [ -z "$paramerror_str" ]; then
521 paramerror_str="No image repo url found for : '--image-repo'"
522 fi
523 else
524 echo "Option set - Image repo url: "$1
525 shift;
526 foundparm=0
527 fi
528 fi
529 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200530 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200531 if [ "$1" == "--repo-policy" ]; then
532 shift;
533 IMAGE_REPO_POLICY=$1
534 if [ -z "$1" ]; then
535 paramerror=1
536 if [ -z "$paramerror_str" ]; then
537 paramerror_str="No policy found for : '--repo-policy'"
538 fi
539 else
540 if [ "$1" == "local" ] || [ "$1" == "remote" ]; then
541 echo "Option set - Image repo policy: "$1
542 shift;
543 foundparm=0
544 else
545 paramerror=1
546 if [ -z "$paramerror_str" ]; then
547 paramerror_str="Repo policy shall be 'local' or 'remote'"
548 fi
549 fi
550 fi
551 fi
552 fi
553 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200554 if [ "$1" == "--cluster-timeout" ]; then
555 shift;
556 CLUSTER_TIME_OUT=$1
557 if [ -z "$1" ]; then
558 paramerror=1
559 if [ -z "$paramerror_str" ]; then
560 paramerror_str="No timeout value found for : '--cluster-timeout'"
561 fi
562 else
563 #Check if positive int
564 case ${CLUSTER_TIME_OUT#[+]} in
565 *[!0-9]* | '')
566 paramerror=1
567 if [ -z "$paramerror_str" ]; then
568 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
569 fi
570 ;;
571 * ) ;; # Ok
572 esac
573 echo "Option set - Cluster timeout: "$1
574 shift;
575 foundparm=0
576 fi
577 fi
578 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200579done
580echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200581
BjornMagnussonXAad047782020-06-08 15:54:11 +0200582#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200583if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
584 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100585 if [ -z "$paramerror_str" ]; then
586 paramerror_str="Unknown parameter(s): "$@
587 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200588fi
589
590if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100591 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
592 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200593 exit 1
594fi
595
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200596# sourcing the selected env variables for the test case
597if [ -f "$TEST_ENV_VAR_FILE" ]; then
598 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
599 . $TEST_ENV_VAR_FILE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100600
601 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100602 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 +0100603 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100604 found_profile=0
605 for prof in $SUPPORTED_PROFILES; do
606 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
607 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
608 found_profile=1
609 fi
610 done
611 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100612 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100613 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100614 echo -e $RED"Exiting...."$ERED
615 exit 1
616 fi
617 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200618else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200619 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200620 echo " Select one of following env var file matching the intended target of the test"
621 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100622 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200623 exit 1
624fi
625
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100626#This var need be preserved from the command line option, if set, when env var is sourced.
627if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
628 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
629fi
630
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100631if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100632 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100633 exit 1
634fi
635
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100636if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
637 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
638 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
639 exit 1
640fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100641
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100642# Choose list of included apps depending on run-mode
643if [ $RUNMODE == "KUBE" ]; then
644 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
645else
646 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
647fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200648
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100649# Check needed installed sw
650tmp=$(which python3)
651if [ $? -ne 0 ] || [ -z tmp ]; then
652 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
653 exit 1
654fi
655tmp=$(which docker)
656if [ $? -ne 0 ] || [ -z tmp ]; then
657 echo -e $RED"docker is required to run the test environment, pls install"$ERED
658 exit 1
659fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200660
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100661tmp=$(which docker-compose)
662if [ $? -ne 0 ] || [ -z tmp ]; then
663 if [ $RUNMODE == "DOCKER" ]; then
664 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
665 exit 1
666 fi
667fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200668
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100669tmp=$(which kubectl)
670if [ $? -ne 0 ] || [ -z tmp ]; then
671 if [ $RUNMODE == "KUBE" ]; then
672 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
673 exit 1
674 fi
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200675else
676 if [ $RUNMODE == "KUBE" ]; then
677 res=$(kubectl cluster-info 2>&1)
678 if [ $? -ne 0 ]; then
679 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
680 echo -e $BOLD$RED"Command 'kubectl cluster-info' returned error $ERED$EBOLD"
681 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
682 echo " "
683 echo "kubectl response:"
684 echo $res
685 echo " "
686 echo "This script may have been started with user with no permission to run kubectl"
687 echo "Try running with 'sudo' or set 'KUBECONFIG'"
688 echo "Do either 1, 2 or 3 "
689 echo " "
690 echo "1"
691 echo "Run with sudo"
692 echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD
693 echo " "
694 echo "2"
695 echo "Export KUBECONFIG and pass env to sudo - (replace user)"
696 echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD
697 echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD
698 echo " "
699 echo "3"
700 echo "Set KUBECONFIG inline (replace user)"
701 echo -e $BOLD"sudo KUBECONFIG='/home/<user>/.kube/config' <test-script-and-parameters>"$EBOLD
702
703 exit 1
704 fi
705 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100706fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100707
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100708echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100709
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100710#Temp var to check for image variable name errors
711IMAGE_ERR=0
712#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200713image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100714echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100715
716# 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 +0100717# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100718__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200719
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200720 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100721 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 +0100722 ((IMAGE_ERR++))
723 return
724 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100725
726 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200727 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100728 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200729 # Image is excluded since the corresponding app is not used in this test
730 return
731 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100732 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100733 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100734 image="${!3}"
735 tmptag=$4"_"$5
736 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100737
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200738 optional_image_repo_target=""
739
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100740 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100741 __check_ignore_image $1
742 if [ $? -eq 0 ]; then
743 app_ds=$6
744 if [ -z "$6" ]; then
745 app_ds="<app ignored>"
746 fi
747 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
748 # Image is ignored since the corresponding the images is not set in the env file
749 __remove_included_image $1 # Remove the image from the list of included images
750 return
751 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100752 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
753 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100754 echo ""
755 tmp=$tmp"<no-image>\t"
756 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200757
758 optional_image_repo_target=$image
759
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100760 #Add repo depending on image type
761 if [ "$5" == "REMOTE_RELEASE" ]; then
762 image=$NEXUS_RELEASE_REPO$image
763 fi
764 if [ "$5" == "REMOTE" ]; then
765 image=$NEXUS_STAGING_REPO$image
766 fi
767 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
768 image=$NEXUS_SNAPSHOT_REPO$image
769 fi
770 if [ "$5" == "REMOTE_PROXY" ]; then
771 image=$NEXUS_PROXY_REPO$image
772 fi
773 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
774 image=$NEXUS_RELEASE_REPO_ONAP$image
775 fi
776 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
777 image=$NEXUS_RELEASE_REPO_ORAN$image
778 fi
779 #No nexus repo added for local images, tag: LOCAL
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100780 tmp=$tmp$image"\t"
781 fi
782 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100783 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
784 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100785 echo ""
786 tmp=$tmp"<no-tag>\t"
787 else
788 tmp=$tmp$tag
789 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100790 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100791 echo -e "$tmp" >> $image_list_file
792 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200793 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 +0200794
795 remote_or_local_push=false
796 if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
797 if [ $5 == "LOCAL" ]; then
798 remote_or_local_push=true
799 fi
800 if [[ $5 == *"REMOTE"* ]]; then
801 if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
802 remote_or_local_push=true
803 fi
804 fi
805 fi
806 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 +0200807 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200808 if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
809 optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
810 optional_image_repo_target=$optional_image_repo_target_tmp
811 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200812 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
813 else
814 export "${2}_SOURCE"=""
815 export "${2}_TARGET"=""
816 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200817}
818
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200819# Check if app uses image included in this test run
820# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200821__check_included_image() {
822 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200823 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200824 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200825 fi
826 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200827 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200828}
829
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100830# Check if app uses a project image
831# Returns 0 if image is included, 1 if not
832__check_project_image() {
833 for im in $PROJECT_IMAGES; do
834 if [ "$1" == "$im" ]; then
835 return 0
836 fi
837 done
838 return 1
839}
840
841# Check if app uses image built by the test script
842# Returns 0 if image is included, 1 if not
843__check_image_local_build() {
844 for im in $LOCAL_IMAGE_BUILD; do
845 if [ "$1" == "$im" ]; then
846 return 0
847 fi
848 done
849 return 1
850}
851
852# Check if app image is conditionally ignored in this test run
853# Returns 0 if image is conditionally ignored, 1 if not
854__check_ignore_image() {
855 for im in $CONDITIONALLY_IGNORED_IMAGES; do
856 if [ "$1" == "$im" ]; then
857 return 0
858 fi
859 done
860 return 1
861}
862
863# Removed image from included list of included images
864# Used when an image is marked as conditionally ignored
865__remove_included_image() {
866 tmp_img_rem_list=""
867 for im in $INCLUDED_IMAGES; do
868 if [ "$1" != "$im" ]; then
869 tmp_img_rem_list=$tmp_img_rem_list" "$im
870 fi
871 done
872 INCLUDED_IMAGES=$tmp_img_rem_list
873 return 0
874}
875
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100876# Check if app is included in the prestarted set of apps
877# Returns 0 if image is included, 1 if not
878__check_prestarted_image() {
879 for im in $KUBE_PRESTARTED_IMAGES; do
880 if [ "$1" == "$im" ]; then
881 return 0
882 fi
883 done
884 return 1
885}
886
887# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100888__check_image_local_override() {
889 for im in $USE_LOCAL_IMAGES; do
890 if [ "$1" == "$im" ]; then
891 return 1
892 fi
893 done
894 return 0
895}
896
897# Check if app uses image override
898# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
899__check_image_override() {
900
901 for im in $ORAN_IMAGES_APP_NAMES; do
902 if [ "$1" == "$im" ]; then
903 echo "REMOTE_RELEASE_ORAN"
904 return 0
905 fi
906 done
907
908 for im in $ONAP_IMAGES_APP_NAMES; do
909 if [ "$1" == "$im" ]; then
910 echo "REMOTE_RELEASE_ONAP"
911 return 0
912 fi
913 done
914
915 found=0
916 for im in $PROJECT_IMAGES_APP_NAMES; do
917 if [ "$1" == "$im" ]; then
918 found=1
919 fi
920 done
921
922 if [ $found -eq 0 ]; then
923 echo "REMOTE_PROXY"
924 return 0
925 fi
926
927 suffix=""
928 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
929 suffix="REMOTE_RELEASE"
930 fi
931 if [ $IMAGE_CATEGORY == "DEV" ]; then
932 suffix="REMOTE"
933 fi
934 CTR=0
935 for im in $USE_STAGING_IMAGES; do
936 if [ "$1" == "$im" ]; then
937 suffix="REMOTE"
938 ((CTR++))
939 fi
940 done
941 for im in $USE_RELEASE_IMAGES; do
942 if [ "$1" == "$im" ]; then
943 suffix="REMOTE_RELEASE"
944 ((CTR++))
945 fi
946 done
947 for im in $USE_SNAPSHOT_IMAGES; do
948 if [ "$1" == "$im" ]; then
949 suffix="REMOTE_SNAPSHOT"
950 ((CTR++))
951 fi
952 done
953 for im in $USE_LOCAL_IMAGES; do
954 if [ "$1" == "$im" ]; then
955 suffix="LOCAL"
956 ((CTR++))
957 fi
958 done
959 echo $suffix
960 if [ $CTR -gt 1 ]; then
961 exit 1
962 fi
963 return 0
964}
965
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200966# Function to re-tag and image and push to another image repo
967__retag_and_push_image() {
968 if [ ! -z "$IMAGE_REPO_ADR" ]; then
969 source_image="${!1}"
970 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
971 target_image="${!trg_var_name}"
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200972
973 if [ -z $target_image ]; then
974 return 0 # Image with no target shall not be pushed
975 fi
976
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200977 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
978 tmp=$(docker image tag $source_image ${target_image} )
979 if [ $? -ne 0 ]; then
980 docker stop $tmp &> ./tmp/.dockererr
981 ((IMAGE_ERR++))
982 echo ""
983 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
984 cat ./tmp/.dockererr
985 return 1
986 else
987 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
988 fi
989 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
990 tmp=$(docker push ${target_image} )
991 if [ $? -ne 0 ]; then
992 docker stop $tmp &> ./tmp/.dockererr
993 ((IMAGE_ERR++))
994 echo ""
995 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
996 cat ./tmp/.dockererr
997 return 1
998 else
999 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1000 fi
1001 export "${1}"=$target_image
1002 fi
1003 return 0
1004}
1005
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001006#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001007#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001008__check_and_pull_image() {
1009
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001010 source_image="${!4}"
1011
1012 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 +01001013 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001014 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001015
1016 if [ $1 == "local" ]; then
1017 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001018 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 +01001019 ((IMAGE_ERR++))
1020 return 1
1021 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001022 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001023 fi
1024 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
1025 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001026 if [ $RUNMODE == "DOCKER" ]; then
1027 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001028 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001029 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1030 docker stop $tmp &> ./tmp/.dockererr
1031 if [ $? -ne 0 ]; then
1032 ((IMAGE_ERR++))
1033 echo ""
1034 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
1035 cat ./tmp/.dockererr
1036 return 1
1037 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001038 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001039 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001040 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001041 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1042 docker rm $tmp &> ./tmp/.dockererr
1043 if [ $? -ne 0 ]; then
1044 ((IMAGE_ERR++))
1045 echo ""
1046 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
1047 cat ./tmp/.dockererr
1048 return 1
1049 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001050 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001051 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
1052 tmp_im=""
1053 else
1054 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001055 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001056 fi
1057 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001058 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001059 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001060 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001061 echo ""
1062 echo -e " Pulling image -$RED could not be pulled"$ERED
1063 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001064 echo $out > ./tmp/.dockererr
1065 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001066 return 1
1067 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001068 echo $out > ./tmp/.dockererr
1069 if [[ $out == *"up to date"* ]]; then
1070 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
1071 elif [[ $out == *"Downloaded newer image"* ]]; then
1072 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
1073 else
1074 echo -e " Pulling image -$GREEN Pulled $EGREEN"
1075 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001076 else
1077 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1078 fi
1079 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001080
1081 __retag_and_push_image $4
1082
1083 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001084}
1085
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001086setup_testenvironment() {
1087 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001088 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001089
1090 # Image var setup for all project images included in the test
1091 for imagename in $APP_SHORT_NAMES; do
1092 __check_included_image $imagename
1093 incl=$?
1094 __check_project_image $imagename
1095 proj=$?
1096 if [ $incl -eq 0 ]; then
1097 if [ $proj -eq 0 ]; then
1098 IMAGE_SUFFIX=$(__check_image_override $imagename)
1099 if [ $? -ne 0 ]; then
1100 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1101 ((IMAGE_ERR++))
1102 fi
1103 else
1104 IMAGE_SUFFIX="none"
1105 fi
1106 # A function name is created from the app short name
1107 # for example app short name 'ECS' -> produce the function
1108 # name __ECS_imagesetup
1109 # This function is called and is expected to exist in the imported
1110 # file for the ecs test functions
1111 # The resulting function impl will call '__check_and_create_image_var' function
1112 # with appropriate parameters
1113 # If the image suffix is none, then the component decides the suffix
1114 function_pointer="__"$imagename"_imagesetup"
1115 $function_pointer $IMAGE_SUFFIX
1116 fi
1117 done
1118
1119 #Errors in image setting - exit
1120 if [ $IMAGE_ERR -ne 0 ]; then
1121 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001122 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001123
1124 #Print a tables of the image settings
1125 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1126 column -t -s $'\t' $image_list_file | indent1
1127
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001128 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001129
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001130 #Set the SIM_GROUP var
1131 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1132 if [ -z "$SIM_GROUP" ]; then
1133 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1134 if [ ! -d $SIM_GROUP ]; then
1135 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1136 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1137 exit 1
1138 else
1139 echo " SIM_GROUP auto set to: " $SIM_GROUP
1140 fi
1141 elif [ $SIM_GROUP = *simulator_group ]; then
1142 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
1143 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001144 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001145 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1146 fi
1147
1148 echo ""
1149
1150 #Temp var to check for image pull errors
1151 IMAGE_ERR=0
1152
1153 # The following sequence pull the configured images
1154
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001155
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001156 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001157 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
1158 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 +02001159 else
1160 for imagename in $APP_SHORT_NAMES; do
1161 __check_included_image $imagename
1162 incl=$?
1163 __check_project_image $imagename
1164 proj=$?
1165 if [ $incl -eq 0 ]; then
1166 if [ $proj -eq 0 ]; then
1167 START_ARG_MOD=$START_ARG
1168 __check_image_local_override $imagename
1169 if [ $? -eq 1 ]; then
1170 START_ARG_MOD="local"
1171 fi
1172 else
1173 START_ARG_MOD=$START_ARG
1174 fi
1175 __check_image_local_build $imagename
1176 #No pull of images built locally
1177 if [ $? -ne 0 ]; then
1178 # A function name is created from the app short name
1179 # for example app short name 'HTTPPROXY' -> produce the function
1180 # name __HTTPPROXY_imagesetup
1181 # This function is called and is expected to exist in the imported
1182 # file for the httpproxy test functions
1183 # The resulting function impl will call '__check_and_pull_image' function
1184 # with appropriate parameters
1185 function_pointer="__"$imagename"_imagepull"
1186 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001187 fi
1188 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001189 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001190 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001191 done
1192 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001193
1194 #Errors in image setting - exit
1195 if [ $IMAGE_ERR -ne 0 ]; then
1196 echo ""
1197 echo "#################################################################################################"
1198 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1199 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1200 if [ $IMAGE_CATEGORY == "DEV" ]; then
1201 echo -e $RED"Note that SNAPSHOT images may be purged from nexus after a certain period."$ERED
1202 echo -e $RED"In that case, switch to use a released image instead."$ERED
1203 fi
1204 echo "#################################################################################################"
1205 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001206 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001207 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001208
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001209 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001210
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001211 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001212
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001213 for imagename in $APP_SHORT_NAMES; do
1214 cd $AUTOTEST_HOME #Always reset to orig dir
1215 __check_image_local_build $imagename
1216 if [ $? -eq 0 ]; then
1217 __check_included_image $imagename
1218 if [ $? -eq 0 ]; then
1219 # A function name is created from the app short name
1220 # for example app short name 'MR' -> produce the function
1221 # name __MR_imagebuild
1222 # This function is called and is expected to exist in the imported
1223 # file for the mr test functions
1224 # The resulting function impl shall build the imagee
1225 function_pointer="__"$imagename"_imagebuild"
1226 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001227
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001228 else
1229 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1230 fi
1231 fi
1232 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001233
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001234 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001235
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001236 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001237
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001238 # Create a table of the images used in the script - from local repo
1239 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001240
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001241 docker_tmp_file=./tmp/.docker-images-table
1242 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1243 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001244
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001245 for imagename in $APP_SHORT_NAMES; do
1246 __check_included_image $imagename
1247 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001248 # Only print image data if image repo is null, or if image repo is set and image is local
1249 print_image_data=0
1250 if [ -z "$IMAGE_REPO_ADR" ]; then
1251 print_image_data=1
1252 else
1253 __check_image_local_build $imagename
1254 if [ $? -eq 0 ]; then
1255 print_image_data=1
1256 fi
1257 fi
1258 if [ $print_image_data -eq 1 ]; then
1259 # A function name is created from the app short name
1260 # for example app short name 'MR' -> produce the function
1261 # name __MR_imagebuild
1262 # This function is called and is expected to exist in the imported
1263 # file for the mr test functions
1264 # The resulting function impl shall build the imagee
1265 function_pointer="__"$imagename"_image_data"
1266 $function_pointer "$format_string" $docker_tmp_file
1267 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001268 fi
1269 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001270
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001271 column -t -s $'\t' $docker_tmp_file | indent1
1272
1273 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001274
1275 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1276
1277 # Create a table of the images used in the script - from remote repo
1278 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1279 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1280
1281 docker_tmp_file=./tmp/.docker-images-table
1282 format_string="{{.Repository}}\\t{{.Tag}}"
1283 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1284
1285 for imagename in $APP_SHORT_NAMES; do
1286 __check_included_image $imagename
1287 if [ $? -eq 0 ]; then
1288 # Only print image data if image repo is null, or if image repo is set and image is local
1289 __check_image_local_build $imagename
1290 if [ $? -ne 0 ]; then
1291 # A function name is created from the app short name
1292 # for example app short name 'MR' -> produce the function
1293 # name __MR_imagebuild
1294 # This function is called and is expected to exist in the imported
1295 # file for the mr test functions
1296 # The resulting function impl shall build the imagee
1297 function_pointer="__"$imagename"_image_data"
1298 $function_pointer "$format_string" $docker_tmp_file
1299 fi
1300 fi
1301 done
1302
1303 column -t -s $'\t' $docker_tmp_file | indent1
1304
1305 echo ""
1306 fi
1307
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001308 if [ $RUNMODE == "KUBE" ]; then
1309
1310 echo "================================================================================="
1311 echo "================================================================================="
1312
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001313 if [ -z "$IMAGE_REPO_ADR" ]; then
1314 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1315 echo -e " This setting only works on single node clusters on the local machine"
1316 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001317 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001318 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001319 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1320 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1321 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1322 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"
1323 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001324 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001325 CLUSTER_IP=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
1326 echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001327
1328 echo "================================================================================="
1329 echo "================================================================================="
1330 echo ""
1331 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001332
1333 echo -e $BOLD"======================================================="$EBOLD
1334 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1335 echo -e $BOLD"======================================================="$EBOLD
1336 echo ""
1337
1338}
YongchaoWu9a84f512019-12-16 22:54:11 +01001339
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001340# Function to print the test result, shall be the last cmd in a test script
1341# args: -
1342# (Function for test scripts)
1343print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001344
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001345 TCTEST_END=$SECONDS
1346 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001347
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001348 echo "-------------------------------------------------------------------------------------------------"
1349 echo "------------------------------------- Test case: "$ATC
1350 echo "------------------------------------- Ended: "$(date)
1351 echo "-------------------------------------------------------------------------------------------------"
1352 echo "-- Description: "$TC_ONELINE_DESCR
1353 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001354 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001355 echo "-------------------------------------------------------------------------------------------------"
1356 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001357 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001358
YongchaoWu21f17bb2020-03-05 12:44:08 +01001359
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001360 if [ $RES_DEVIATION -gt 0 ]; then
1361 echo "Test case deviations"
1362 echo "===================================="
1363 cat $DEVIATION_FILE
1364 fi
1365 echo ""
1366 echo "Timer measurement in the test script"
1367 echo "===================================="
1368 column -t -s $'\t' $TIMER_MEASUREMENTS
1369 echo ""
1370
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001371 total=$((RES_PASS+RES_FAIL))
1372 if [ $RES_TEST -eq 0 ]; then
1373 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1374 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1375 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1376 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1377 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1378 elif [ $total != $RES_TEST ]; then
1379 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1380 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1381 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1382 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1383 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1384 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001385 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001386 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1387 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1388 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1389 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1390 elif [ $RES_PASS = $RES_TEST ]; then
1391 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1392 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1393 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1394 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1395 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1396 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001397
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001398 # Update test suite counter
1399 if [ -f .tmp_tcsuite_pass_ctr ]; then
1400 tmpval=$(< .tmp_tcsuite_pass_ctr)
1401 ((tmpval++))
1402 echo $tmpval > .tmp_tcsuite_pass_ctr
1403 fi
1404 if [ -f .tmp_tcsuite_pass ]; then
1405 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1406 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001407 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001408 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001409 else
1410 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1411 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
1412 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
1413 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
1414 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
1415 echo ""
1416 # Update test suite counter
1417 if [ -f .tmp_tcsuite_fail_ctr ]; then
1418 tmpval=$(< .tmp_tcsuite_fail_ctr)
1419 ((tmpval++))
1420 echo $tmpval > .tmp_tcsuite_fail_ctr
1421 fi
1422 if [ -f .tmp_tcsuite_fail ]; then
1423 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1424 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001425 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001426
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001427 echo "++++ Number of tests: "$RES_TEST
1428 echo "++++ Number of passed tests: "$RES_PASS
1429 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01001430 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001431 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001432 echo ""
1433 echo "++++ Number of test case deviations: "$RES_DEVIATION
1434 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001435 echo "------------------------------------- Test case complete ---------------------------------"
1436 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01001437 echo ""
1438}
1439
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001440#####################################################################
1441###### Functions for start, configuring, stoping, cleaning etc ######
1442#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01001443
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001444# Start timer for time measurement
1445# args - (any args will be printed though)
1446start_timer() {
1447 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1448 TC_TIMER=$SECONDS
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001449 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001450}
1451
1452# Print the value of the time (in seconds)
1453# args - <timer message to print> - timer value and message will be printed both on screen
1454# and in the timer measurement report
1455print_timer() {
1456 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1457 if [ $# -lt 1 ]; then
1458 ((RES_CONF_FAIL++))
1459 __print_err "need 1 or more args, <timer message to print>" $@
1460 exit 1
1461 fi
1462 duration=$(($SECONDS-$TC_TIMER))
1463 if [ $duration -eq 0 ]; then
1464 duration="<1 second"
1465 else
1466 duration=$duration" seconds"
1467 fi
1468 echo " Timer duration :" $duration
1469
1470 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
1471}
1472
1473# Print the value of the time (in seconds) and reset the timer
1474# args - <timer message to print> - timer value and message will be printed both on screen
1475# and in the timer measurement report
1476print_and_reset_timer() {
1477 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1478 if [ $# -lt 1 ]; then
1479 ((RES_CONF_FAIL++))
1480 __print_err "need 1 or more args, <timer message to print>" $@
1481 exit 1
1482 fi
1483 duration=$(($SECONDS-$TC_TIMER))" seconds"
1484 if [ $duration -eq 0 ]; then
1485 duration="<1 second"
1486 else
1487 duration=$duration" seconds"
1488 fi
1489 echo " Timer duration :" $duration
1490 TC_TIMER=$SECONDS
1491 echo " Timer reset"
1492
1493 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
1494
1495}
1496# Print info about a deviations from intended tests
1497# Each deviation counted is also printed in the testreport
1498# args <deviation message to print>
1499deviation() {
1500 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
1501 if [ $# -lt 1 ]; then
1502 ((RES_CONF_FAIL++))
1503 __print_err "need 1 or more args, <deviation message to print>" $@
1504 exit 1
1505 fi
1506 ((RES_DEVIATION++))
1507 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
1508 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
1509 echo ""
1510}
YongchaoWu21f17bb2020-03-05 12:44:08 +01001511
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001512# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
1513__check_stop_at_error() {
1514 if [ $STOP_AT_ERROR -eq 1 ]; then
1515 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
1516 store_logs "STOP_AT_ERROR"
1517 exit 1
1518 fi
1519 return 0
1520}
1521
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001522# Stop and remove all containers
1523# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001524# (Not for test scripts)
1525__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001526
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001527 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001528
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001529 #Create empty file
1530 running_contr_file="./tmp/running_contr.txt"
1531 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001532
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001533 # Get list of all containers started by the test script
1534 for imagename in $APP_SHORT_NAMES; do
1535 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 +01001536 done
1537
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001538 tab_heading1="App display name"
1539 tab_heading2="App short name"
1540 tab_heading3="Container name"
1541
1542 tab_heading1_len=${#tab_heading1}
1543 tab_heading2_len=${#tab_heading2}
1544 tab_heading3_len=${#tab_heading3}
1545 cntr=0
1546 #Calc field lengths of each item in the list of containers
1547 while read p; do
1548 if (( $cntr % 3 == 0 ));then
1549 if [ ${#p} -gt $tab_heading1_len ]; then
1550 tab_heading1_len=${#p}
1551 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001552 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001553 if (( $cntr % 3 == 1));then
1554 if [ ${#p} -gt $tab_heading2_len ]; then
1555 tab_heading2_len=${#p}
1556 fi
1557 fi
1558 if (( $cntr % 3 == 2));then
1559 if [ ${#p} -gt $tab_heading3_len ]; then
1560 tab_heading3_len=${#p}
1561 fi
1562 fi
1563 let cntr=cntr+1
1564 done <$running_contr_file
1565
1566 let tab_heading1_len=tab_heading1_len+2
1567 while (( ${#tab_heading1} < $tab_heading1_len)); do
1568 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001569 done
1570
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001571 let tab_heading2_len=tab_heading2_len+2
1572 while (( ${#tab_heading2} < $tab_heading2_len)); do
1573 tab_heading2="$tab_heading2"" "
1574 done
1575
1576 let tab_heading3_len=tab_heading3_len+2
1577 while (( ${#tab_heading3} < $tab_heading3_len)); do
1578 tab_heading3="$tab_heading3"" "
1579 done
1580
1581 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
1582 cntr=0
1583 while read p; do
1584 if (( $cntr % 3 == 0 ));then
1585 row=""
1586 heading=$p
1587 heading_len=$tab_heading1_len
1588 fi
1589 if (( $cntr % 3 == 1));then
1590 heading=$p
1591 heading_len=$tab_heading2_len
1592 fi
1593 if (( $cntr % 3 == 2));then
1594 contr=$p
1595 heading=$p
1596 heading_len=$tab_heading3_len
1597 fi
1598 while (( ${#heading} < $heading_len)); do
1599 heading="$heading"" "
1600 done
1601 row=$row$heading
1602 if (( $cntr % 3 == 2));then
1603 echo -ne $row$SAMELINE
1604 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
1605 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1606 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
1607 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1608 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
1609 fi
1610 let cntr=cntr+1
1611 done <$running_contr_file
1612
YongchaoWu9a84f512019-12-16 22:54:11 +01001613 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001614
1615 echo -e $BOLD" Removing docker network"$EBOLD
1616 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
1617 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001618 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001619 if [ $? -ne 0 ]; then
1620 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
1621 exit 1
1622 fi
1623 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001624 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001625
1626 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001627 docker network prune --force | indent2
1628 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001629
1630 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001631 docker volume prune --force | indent2
1632 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001633
1634 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
1635 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001636 echo -e "$GREEN Done$EGREEN"
1637 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001638
1639 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
1640 if [ $? -eq 0 ]; then
1641 if [ $CONTRS -ne 0 ]; then
1642 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001643 docker ps -a | indent1
1644 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001645 fi
1646 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01001647}
1648
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001649###################################
1650### Functions for kube management
1651###################################
1652
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02001653# Get resource type for scaling
1654# args: <resource-name> <namespace>
1655__kube_get_resource_type() {
1656 kubectl get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1657 if [ $? -eq 0 ]; then
1658 echo "deployment"
1659 return 0
1660 fi
1661 kubectl get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1662 if [ $? -eq 0 ]; then
1663 echo "sts"
1664 return 0
1665 fi
1666 echo "unknown-resource-type"
1667 return 1
1668}
1669
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001670# Scale a kube resource to a specific count
1671# args: <resource-type> <resource-name> <namespace> <target-count>
1672# (Not for test scripts)
1673__kube_scale() {
1674 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
1675 kubectl scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
1676 if [ $? -ne 0 ]; then
1677 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
1678 ((RES_CONF_FAIL++))
1679 echo " Message: $(<./tmp/kubeerr)"
1680 return 1
1681 else
1682 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
1683 fi
1684
1685 TSTART=$SECONDS
1686
1687 for i in {1..500}; do
1688 count=$(kubectl get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
1689 retcode=$?
1690 if [ -z "$count" ]; then
1691 #No value is sometimes returned for some reason, in case the resource has replica 0
1692 count=0
1693 fi
1694 if [ $retcode -ne 0 ]; then
1695 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
1696 ((RES_CONF_FAIL++))
1697 return 1
1698 fi
1699 #echo ""
1700 if [ $count -ne $4 ]; then
1701 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
1702 sleep $i
1703 else
1704 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
1705 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
1706 echo ""
1707 return 0
1708 fi
1709 done
1710 echo ""
1711 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
1712 ((RES_CONF_FAIL++))
1713 return 0
1714}
1715
1716# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1717# This function does not wait for the resource to reach 0
1718# args: <namespace> <label-name> <label-id>
1719# (Not for test scripts)
1720__kube_scale_all_resources() {
1721 namespace=$1
1722 labelname=$2
1723 labelid=$3
1724 resources="deployment replicaset statefulset"
1725 for restype in $resources; do
1726 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1727 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1728 deleted_resourcetypes=$deleted_resourcetypes" "$restype
1729 for resid in $result; do
1730 echo -ne " Ordered caling $restype $resid from namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
1731 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
1732 echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
1733 done
1734 fi
1735 done
1736}
1737
1738# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1739# This function do wait for the resource to reach 0
1740# args: <namespace> <label-name> <label-id>
1741# (Not for test scripts)
1742__kube_scale_and_wait_all_resources() {
1743 namespace=$1
1744 labelname=$2
1745 labelid=$3
1746 resources="deployment replicaset statefulset"
1747 scaled_all=1
1748 while [ $scaled_all -ne 0 ]; do
1749 scaled_all=0
1750 for restype in $resources; do
1751 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1752 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1753 for resid in $result; do
1754 echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0"
1755 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
1756 count=1
1757 T_START=$SECONDS
1758 while [ $count -ne 0 ]; do
1759 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
1760 echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE
1761 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
1762 sleep 0.5
1763 else
1764 count=0
1765 fi
1766 duration=$(($SECONDS-$T_START))
1767 if [ $duration -gt 100 ]; then
1768 #Forcring count 0, to avoid hanging for failed scaling
1769 scaled_all=1
1770 count=0
1771 fi
1772 done
1773 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN"
1774 done
1775 fi
1776 done
1777 done
1778}
1779
1780# Remove all kube resources in a namespace for resources having a certain label and label-id
1781# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
1782# args: <namespace> <label-name> <label-id>
1783# (Not for test scripts)
1784__kube_delete_all_resources() {
1785 namespace=$1
1786 labelname=$2
1787 labelid=$3
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001788 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001789 deleted_resourcetypes=""
1790 for restype in $resources; do
1791 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1792 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1793 deleted_resourcetypes=$deleted_resourcetypes" "$restype
1794 for resid in $result; do
1795 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
1796 count=1
1797 while [ $count -ne 0 ]; do
1798 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
1799 echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE
1800 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
1801 sleep 0.5
1802 else
1803 count=0
1804 fi
1805 done
1806 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN"
1807 fi
1808 echo -ne " Deleting $restype $resid from namespace $namespace with label $labelname=$labelid "$SAMELINE
1809 kubectl delete $restype $resid -n $namespace 1> /dev/null 2> ./tmp/kubeerr
1810 if [ $? -eq 0 ]; then
1811 echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN OK $EGREEN"
1812 else
1813 echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
1814 fi
1815 #fi
1816 done
1817 fi
1818 done
1819 if [ ! -z "$deleted_resourcetypes" ]; then
1820 for restype in $deleted_resources; do
1821 echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted..."$SAMELINE
1822 T_START=$SECONDS
1823 result="dummy"
1824 while [ ! -z "$result" ]; do
1825 sleep 0.5
1826 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1827 echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
1828 if [ -z "$result" ]; then
1829 echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
1830 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
1831 echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
1832 result=""
1833 fi
1834 done
1835 done
1836 fi
1837}
1838
1839# Creates a namespace if it does not exists
1840# args: <namespace>
1841# (Not for test scripts)
1842__kube_create_namespace() {
1843
1844 #Check if test namespace exists, if not create it
1845 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
1846 if [ $? -ne 0 ]; then
1847 echo -ne " Creating namespace "$1 $SAMELINE
1848 kubectl create namespace $1 1> /dev/null 2> ./tmp/kubeerr
1849 if [ $? -ne 0 ]; then
1850 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
1851 ((RES_CONF_FAIL++))
1852 echo " Message: $(<./tmp/kubeerr)"
1853 return 1
1854 else
1855 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
1856 fi
1857 else
1858 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
1859 fi
1860 return 0
1861}
1862
1863# Find the host ip of an app (using the service resource)
1864# args: <app-name> <namespace>
1865# (Not for test scripts)
1866__kube_get_service_host() {
1867 if [ $# -ne 2 ]; then
1868 ((RES_CONF_FAIL++))
1869 __print_err "need 2 args, <app-name> <namespace>" $@
1870 exit 1
1871 fi
1872 for timeout in {1..60}; do
1873 host=$(kubectl get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
1874 if [ $? -eq 0 ]; then
1875 if [ ! -z "$host" ]; then
1876 echo $host
1877 return 0
1878 fi
1879 fi
1880 sleep 0.5
1881 done
1882 ((RES_CONF_FAIL++))
1883 echo "host-not-found-fatal-error"
1884 return 1
1885}
1886
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001887# Find the named port to an app (using the service resource)
1888# args: <app-name> <namespace> <port-name>
1889# (Not for test scripts)
1890__kube_get_service_port() {
1891 if [ $# -ne 3 ]; then
1892 ((RES_CONF_FAIL++))
1893 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
1894 exit 1
1895 fi
1896
1897 for timeout in {1..60}; do
1898 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
1899 if [ $? -eq 0 ]; then
1900 if [ ! -z "$port" ]; then
1901 echo $port
1902 return 0
1903 fi
1904 fi
1905 sleep 0.5
1906 done
1907 ((RES_CONF_FAIL++))
1908 echo "0"
1909 return 1
1910}
1911
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001912# Find the named node port to an app (using the service resource)
1913# args: <app-name> <namespace> <port-name>
1914# (Not for test scripts)
1915__kube_get_service_nodeport() {
1916 if [ $# -ne 3 ]; then
1917 ((RES_CONF_FAIL++))
1918 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
1919 exit 1
1920 fi
1921
1922 for timeout in {1..60}; do
1923 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
1924 if [ $? -eq 0 ]; then
1925 if [ ! -z "$port" ]; then
1926 echo $port
1927 return 0
1928 fi
1929 fi
1930 sleep 0.5
1931 done
1932 ((RES_CONF_FAIL++))
1933 echo "0"
1934 return 1
1935}
1936
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001937# Create a kube resource from a yaml template
1938# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
1939# (Not for test scripts)
1940__kube_create_instance() {
1941 echo -ne " Creating $1 $2"$SAMELINE
1942 envsubst < $3 > $4
1943 kubectl apply -f $4 1> /dev/null 2> ./tmp/kubeerr
1944 if [ $? -ne 0 ]; then
1945 ((RES_CONF_FAIL++))
1946 echo -e " Creating $1 $2 $RED Failed $ERED"
1947 echo " Message: $(<./tmp/kubeerr)"
1948 return 1
1949 else
1950 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
1951 fi
1952}
1953
1954# Function to create a configmap in kubernetes
1955# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
1956# (Not for test scripts)
1957__kube_create_configmap() {
1958 echo -ne " Creating configmap $1 "$SAMELINE
1959 envsubst < $5 > $5"_tmp"
1960 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
1961 kubectl create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
1962 if [ $? -ne 0 ]; then
1963 echo -e " Creating configmap $1 $RED Failed $ERED"
1964 ((RES_CONF_FAIL++))
1965 return 1
1966 fi
1967
1968 kubectl apply -f $6 1> /dev/null 2> ./tmp/kubeerr
1969 if [ $? -ne 0 ]; then
1970 echo -e " Creating configmap $1 $RED Apply failed $ERED"
1971 echo " Message: $(<./tmp/kubeerr)"
1972 ((RES_CONF_FAIL++))
1973 return 1
1974 fi
1975 kubectl label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
1976 if [ $? -ne 0 ]; then
1977 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
1978 echo " Message: $(<./tmp/kubeerr)"
1979 ((RES_CONF_FAIL++))
1980 return 1
1981 fi
1982 # Log the resulting map
1983 kubectl get configmap $1 -n $2 -o yaml > $6
1984
1985 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
1986 return 0
1987}
1988
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001989# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
1990# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
BjornMagnussonXAa5491572021-05-04 09:21:24 +02001991# args: <full kubectl cmd with parameters>
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001992# (Not for test scripts)
1993__kube_cmd_with_timeout() {
1994 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
1995
1996 while true; do
1997 kube_cmd_result=$($@)
1998 if [ $? -ne 0 ]; then
1999 kube_cmd_result=""
2000 fi
2001 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
2002 echo $kube_cmd_result
2003 return 0
2004 fi
2005 sleep 1
2006 done
2007}
2008
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002009# This function starts a pod that cleans a the contents of a path mounted as a pvc
2010# After this action the pod should terminate
2011# This should only be executed when the pod owning the pvc is not running
2012# args: <appname> <namespace> <pvc-name> <path-to remove>
2013# (Not for test scripts)
2014__kube_clean_pvc() {
2015
2016 export PVC_CLEANER_NAMESPACE=$2
2017 export PVC_CLEANER_CLAIMNAME=$3
2018 export PVC_CLEANER_RM_PATH=$4
2019 input_yaml=$SIM_GROUP"/pvc-cleaner/"pvc-cleaner.yaml
2020 output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml
2021
2022 envsubst < $input_yaml > $output_yaml
2023
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002024 kubectl delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002025
2026 __kube_create_instance pod pvc-cleaner $input_yaml $output_yaml
2027 if [ $? -ne 0 ]; then
2028 echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work"
2029 return 1
2030 fi
2031
2032 term_ts=$(($SECONDS+30))
2033 while [ $term_ts -gt $SECONDS ]; do
2034 pod_status=$(kubectl get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase")
2035 if [ "$pod_status" == "Succeeded" ]; then
2036 return 0
2037 fi
2038 done
2039 return 1
2040}
2041
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002042# This function scales or deletes all resources for app selected by the testcase.
2043# args: -
2044# (Not for test scripts)
2045__clean_kube() {
2046 echo -e $BOLD"Initialize kube services//pods/statefulsets/replicaset to initial state"$EBOLD
2047
2048 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002049 for imagename in $APP_SHORT_NAMES; do
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002050 # A function name is created from the app short name
2051 # for example app short name 'RICMSIM' -> produce the function
2052 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
2053 # This function is called and is expected to exist in the imported
2054 # file for the ricsim test functions
2055 # The resulting function impl shall scale the resources to 0
2056 # For prestarted apps, the function waits until the resources are 0
2057 # For included (not prestated) apps, the scaling is just ordered
2058 __check_prestarted_image $imagename
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002059 if [ $? -eq 0 ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002060 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002061 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2062 $function_pointer
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002063 else
2064 __check_included_image $imagename
2065 if [ $? -eq 0 ]; then
2066 function_pointer="__"$imagename"_kube_scale_zero"
2067 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2068 $function_pointer
2069 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002070 fi
2071 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002072
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002073 # Delete managed apps
2074 for imagename in $APP_SHORT_NAMES; do
2075 __check_included_image $imagename
2076 if [ $? -eq 0 ]; then
2077 __check_prestarted_image $imagename
2078 if [ $? -ne 0 ]; then
2079 # A function name is created from the app short name
2080 # for example app short name 'RICMSIM' -> produce the function
2081 # name __RICSIM__kube_delete_all
2082 # This function is called and is expected to exist in the imported
2083 # file for the ricsim test functions
2084 # The resulting function impl shall delete all its resources
2085 function_pointer="__"$imagename"_kube_delete_all"
2086 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
2087 $function_pointer
2088 fi
2089 fi
2090 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002091
2092 echo ""
2093}
2094
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002095# Function stop and remove all containers (docker) and services/deployments etc(kube)
2096# args: -
2097# Function for test script
2098clean_environment() {
2099 if [ $RUNMODE == "KUBE" ]; then
2100 __clean_kube
2101 else
2102 __clean_containers
2103 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002104}
2105
2106# 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 +01002107# args: -
2108# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002109auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002110 echo
2111 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002112 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
2113 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01002114 fi
2115}
2116
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002117# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002118# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002119# (Function for test scripts)
2120sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01002121
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002122 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
2123 if [ $# -lt 1 ]; then
2124 ((RES_CONF_FAIL++))
2125 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
2126 exit 1
2127 fi
2128 #echo "---- Sleep for " $1 " seconds ---- "$2
2129 start=$SECONDS
2130 duration=$((SECONDS-start))
2131 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002132 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002133 sleep 1
2134 duration=$((SECONDS-start))
2135 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002136 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002137 echo ""
2138}
2139
2140# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2141# Not to be called from the test script itself.
2142__print_err() {
2143 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2144 if [ $# -gt 1 ]; then
2145 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2146 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002147 ((RES_CONF_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002148}
2149
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002150# Function to create the docker network for the test
2151# Not to be called from the test script itself.
2152__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002153 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002154 if [ $? -ne 0 ]; then
2155 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2156 return 1
2157 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002158 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002159 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2160 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002161 if [ $? -ne 0 ]; then
2162 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2163 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002164 else
2165 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002166 fi
2167 else
2168 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2169 fi
2170}
2171
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002172# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002173# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2174#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002175# (Not for test scripts)
2176__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002177
2178 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002179 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002180 __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 +01002181 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002182 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002183
2184 __create_docker_network
2185
2186 curdir=$PWD
2187 cd $SIM_GROUP
2188 compose_dir=$1
2189 cd $1
2190 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002191 compose_file=$1
2192 if [ -z "$compose_file" ]; then
2193 compose_file="docker-compose.yml"
2194 fi
2195 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002196 compose_args=$1
2197 shift
2198 appcount=$1
2199 shift
2200
2201 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002202 docker-compose -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002203 if [ $? -ne 0 ]; then
2204 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2205 cat .dockererr
2206 echo -e $RED"Stopping script...."$ERED
2207 exit 1
2208 fi
2209 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002210 docker-compose -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002211 if [ $? -ne 0 ]; then
2212 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2213 cat .dockererr
2214 echo -e $RED"Stopping script...."$ERED
2215 exit 1
2216 fi
2217 fi
2218
2219 cd $curdir
2220
2221 appindex=0
2222 while [ $appindex -lt $appcount ]; do
2223 appname=$1
2224 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002225 app_started=0
2226 for i in {1..10}; do
2227 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002228 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 +02002229 app_started=1
2230 break
2231 else
2232 sleep $i
2233 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002234 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002235 if [ $app_started -eq 0 ]; then
2236 ((RES_CONF_FAIL++))
2237 echo ""
2238 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002239 echo -e $RED" Stopping script..."$ERED
2240 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002241 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002242 let appindex=appindex+1
2243 done
2244 return 0
2245}
2246
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002247# Function to check if container/service is responding to http/https
2248# args: <container-name>|<service-name> url
2249# (Not for test scripts)
2250__check_service_start() {
2251
2252 if [ $# -ne 2 ]; then
2253 ((RES_CONF_FAIL++))
2254 __print_err "need 2 args, <container-name>|<service-name> url" $@
2255 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002256 fi
2257
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002258 if [ $RUNMODE == "KUBE" ]; then
2259 ENTITY="service/set/deployment"
2260 else
2261 ENTITY="container"
2262 fi
2263 appname=$1
2264 url=$2
2265 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2266
2267
YongchaoWu9a84f512019-12-16 22:54:11 +01002268 pa_st=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002269 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002270 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002271 loop_ctr=0
2272 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002273 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002274 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002275 if [ ${#result} -gt 15 ]; then
2276 #If response is too long, truncate
2277 result="...response text too long, omitted"
2278 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002279 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2280 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 +01002281 pa_st=true
2282 break
2283 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002284 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002285 TS_OFFSET=$loop_ctr
2286 if (( $TS_OFFSET > 5 )); then
2287 TS_OFFSET=5
2288 fi
2289 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
2290 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 +02002291 sleep 1
2292 done
YongchaoWu9a84f512019-12-16 22:54:11 +01002293 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002294 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01002295 done
2296
2297 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002298 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002299 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 +01002300 return 1
2301 fi
2302
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002303 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002304 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002305}
2306
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002307
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002308#################
2309### Log functions
2310#################
2311
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002312__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002313
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002314 dispname=$1
2315 appname=$2
2316 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002317 warning=$4
2318 error=$5
2319
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002320 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
2321
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002322 if [ $RUNMODE == "KUBE" ]; then
2323 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
2324 return
2325 fi
2326
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002327 #tmp=$(docker ps | grep $appname)
2328 tmp=$(docker ps -q --filter name=$appname) #get the container id
2329 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002330 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002331 return
2332 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002333 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002334 if [ $? -ne 0 ];then
2335 echo " Problem to search $appname log $logpath"
2336 else
2337 if [ $foundentries -eq 0 ]; then
2338 echo " No WARN entries found in $appname log $logpath"
2339 else
2340 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
2341 fi
2342 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002343 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002344 if [ $? -ne 0 ];then
2345 echo " Problem to search $appname log $logpath"
2346 else
2347 if [ $foundentries -eq 0 ]; then
2348 echo " No ERR entries found in $appname log $logpath"
2349 else
2350 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
2351 fi
2352 fi
2353 echo ""
2354}
2355
2356# Store all container logs and other logs in the log dir for the script
2357# Logs are stored with a prefix in case logs should be stored several times during a test
2358# args: <logfile-prefix>
2359# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01002360store_logs() {
2361 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002362 ((RES_CONF_FAIL++))
2363 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01002364 exit 1
2365 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002366 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 +01002367
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002368 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002369
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002370 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002371
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002372 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
2373
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002374 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002375
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002376 # Store docker logs for all container
2377 for imagename in $APP_SHORT_NAMES; do
2378 __check_included_image $imagename
2379 if [ $? -eq 0 ]; then
2380 # A function name is created from the app short name
2381 # for example app short name 'RICMSIM' -> produce the function
2382 # name __RICSIM__store_docker_logs
2383 # This function is called and is expected to exist in the imported
2384 # file for the ricsim test functions
2385 # The resulting function impl shall store the docker logs for each container
2386 function_pointer="__"$imagename"_store_docker_logs"
2387 $function_pointer "$TESTLOGS/$ATC/" $1
2388 fi
2389 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002390 fi
2391 if [ $RUNMODE == "KUBE" ]; then
2392 namespaces=$(kubectl get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
2393 for nsid in $namespaces; do
2394 pods=$(kubectl get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
2395 for podid in $pods; do
2396 kubectl logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
2397 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002398 done
2399 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002400 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01002401}
2402
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002403###############
2404## Generic curl
2405###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002406# Generic curl function, assumes all 200-codes are ok
2407# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002408# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
2409# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01002410__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002411 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002412 proxyflag=""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002413 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002414 if [ ! -z "$KUBE_PROXY_PATH" ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +02002415 if [ $KUBE_PROXY_HTTPX == "http" ]; then
2416 proxyflag=" --proxy $KUBE_PROXY_PATH"
2417 else
2418 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
2419 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002420 fi
2421 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002422 curlString="curl -skw %{http_code} $proxyflag $@"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002423 echo " CMD: $curlString" >> $HTTPLOG
2424 res=$($curlString)
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002425 retcode=$?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002426 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002427 echo " RETCODE: $retcode" >> $HTTPLOG
2428 if [ $retcode -ne 0 ]; then
2429 echo "<no-response-from-server>"
2430 return 1
2431 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002432 http_code="${res:${#res}-3}"
2433 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002434 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
2435 echo "<no-response-from-server>"
2436 return 1
2437 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002438 return 0
2439 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002440 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002441 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01002442 echo "<not found, resp:${http_code}>"
2443 return 1
2444 fi
2445 if [ $# -eq 2 ]; then
2446 echo "${res:0:${#res}-3}" | xargs
2447 else
2448 echo "${res:0:${#res}-3}"
2449 fi
2450
2451 return 0
2452 fi
2453}
2454
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002455#######################################
2456### Basic helper function for test cases
2457#######################################
2458
2459# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
2460# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
2461# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
2462# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
2463# before setting pass or fail depending on the result of comparing variable and target using the operator.
2464# 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.
2465# Not to be called from test script.
2466
2467__var_test() {
2468 checkjsonarraycount=0
2469
2470 if [ $# -eq 6 ]; then
2471 if [[ $3 == "json:"* ]]; then
2472 checkjsonarraycount=1
2473 fi
2474
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002475 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002476 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002477 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002478 start=$SECONDS
2479 ctr=0
2480 for (( ; ; )); do
2481 if [ $checkjsonarraycount -eq 0 ]; then
2482 result="$(__do_curl $2$3)"
2483 retcode=$?
2484 result=${result//[[:blank:]]/} #Strip blanks
2485 else
2486 path=${3:5}
2487 result="$(__do_curl $2$path)"
2488 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002489 echo "$result" > ./tmp/.tmp.curl.json
2490 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002491 fi
2492 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002493 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002494 let ctr=ctr+1
2495 if [ $retcode -ne 0 ]; then
2496 if [ $duration -gt $6 ]; then
2497 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002498 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002499 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002500 return
2501 fi
2502 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2503 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002504 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002505 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002506 return
2507 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2508 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002509 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002510 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002511 return
2512 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2513 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002514 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002515 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002516 return
2517 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2518 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002519 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002520 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002521 return
2522 else
2523 if [ $duration -gt $6 ]; then
2524 ((RES_FAIL++))
2525 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002526 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002527 return
2528 fi
2529 fi
2530 sleep 1
2531 done
2532 elif [ $# -eq 5 ]; then
2533 if [[ $3 == "json:"* ]]; then
2534 checkjsonarraycount=1
2535 fi
2536
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002537 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002538 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002539 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002540 if [ $checkjsonarraycount -eq 0 ]; then
2541 result="$(__do_curl $2$3)"
2542 retcode=$?
2543 result=${result//[[:blank:]]/} #Strip blanks
2544 else
2545 path=${3:5}
2546 result="$(__do_curl $2$path)"
2547 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002548 echo "$result" > ./tmp/.tmp.curl.json
2549 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002550 fi
2551 if [ $retcode -ne 0 ]; then
2552 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002553 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002554 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002555 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2556 ((RES_PASS++))
2557 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002558 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2559 ((RES_PASS++))
2560 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002561 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2562 ((RES_PASS++))
2563 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002564 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2565 ((RES_PASS++))
2566 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002567 else
2568 ((RES_FAIL++))
2569 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002570 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002571 fi
2572 else
2573 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
2574 echo "Got:" $@
2575 exit 1
2576 fi
2577}