blob: b63d4d129e7d55b1f09d1c485ab6d7ba0f5a765d [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/bin/bash
YongchaoWu9a84f512019-12-16 22:54:11 +01002
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003# ============LICENSE_START===============================================
4# Copyright (C) 2020 Nordix Foundation. All rights reserved.
5# ========================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ============LICENSE_END=================================================
18#
YongchaoWu9a84f512019-12-16 22:54:11 +010019
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010020# This is a script that contains all the common functions needed for auto test.
21# Specific test function are defined in scripts XXXX_functions.sh
22
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010023. ../common/api_curl.sh
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010024. ../common/testengine_config.sh
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010025
26__print_args() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010027 echo "Args: remote|remote-remove docker|kube --env-file <environment-filename> [release] [auto-clean] [--stop-at-error] "
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010028 echo " [--ricsim-prefix <prefix> ] [--use-local-image <app-nam>+] [--use-snapshot-image <app-nam>+]"
BjornMagnussonXA483ee332021-04-08 01:35:24 +020029 echo " [--use-staging-image <app-nam>+] [--use-release-image <app-nam>+] [--image-repo <repo-address]"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +020030 echo " [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>] [--print-stats]"
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. "
BjornMagnussonXAfec823b2021-08-03 14:14:05 +020058 echo "--print-stats - Print current test stats after each test."
59
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010060 echo ""
61 echo "List of app short names supported: "$APP_SHORT_NAMES
62 exit 0
63fi
64
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010065AUTOTEST_HOME=$PWD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020066# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
67# FTC1.sh -> ATC == FTC1
68ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
69
70#Create result file (containing '1' for error) for this test case
71#Will be replaced with a file containing '0' if all test cases pass
72echo "1" > "$PWD/.result$ATC.txt"
73
BjornMagnussonXA80a92002020-03-19 14:31:06 +010074#Formatting for 'echo' cmd
75BOLD="\033[1m"
76EBOLD="\033[0m"
77RED="\033[31m\033[1m"
78ERED="\033[0m"
79GREEN="\033[32m\033[1m"
80EGREEN="\033[0m"
81YELLOW="\033[33m\033[1m"
82EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020083SAMELINE="\033[0K\r"
84
BjornMagnussonXA80a92002020-03-19 14:31:06 +010085# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020086echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +010087
BjornMagnussonXA575869c2020-09-14 21:28:54 +020088# default test environment variables
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020089TEST_ENV_VAR_FILE=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +010090
91echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
92
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010093#Localhost constants
94LOCALHOST_NAME="localhost"
95LOCALHOST_HTTP="http://localhost"
96LOCALHOST_HTTPS="https://localhost"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020097
BjornMagnussonXA80a92002020-03-19 14:31:06 +010098# Var to hold 'auto' in case containers shall be stopped when test case ends
99AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +0100100
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100101# Var to hold the app names to use local images for
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200102USE_LOCAL_IMAGES=""
103
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100104# Var to hold the app names to use remote snapshot images for
105USE_SNAPSHOT_IMAGES=""
106
107# Var to hold the app names to use remote staging images for
108USE_STAGING_IMAGES=""
109
110# Var to hold the app names to use remote release images for
111USE_RELEASE_IMAGES=""
112
BjornMagnussonXAad047782020-06-08 15:54:11 +0200113
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200114# 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
115STOP_AT_ERROR=0
116
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100117# The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used.
118# The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used
119# Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE
120IMAGE_CATEGORY="DEV"
121
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200122# Function to indent cmd output with one space
123indent1() { sed 's/^/ /'; }
124
125# Function to indent cmd output with two spaces
126indent2() { sed 's/^/ /'; }
127
YongchaoWu9a84f512019-12-16 22:54:11 +0100128# Set a description string for the test case
129if [ -z "$TC_ONELINE_DESCR" ]; then
130 TC_ONELINE_DESCR="<no-description>"
131 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
132fi
133
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100134# Counter for test suites
135if [ -f .tmp_tcsuite_ctr ]; then
136 tmpval=$(< .tmp_tcsuite_ctr)
137 ((tmpval++))
138 echo $tmpval > .tmp_tcsuite_ctr
139fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100140
YongchaoWu9a84f512019-12-16 22:54:11 +0100141# Create the logs dir if not already created in the current dir
142if [ ! -d "logs" ]; then
143 mkdir logs
144fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100145TESTLOGS=$PWD/logs
146
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200147# Create the tmp dir for temporary files that is not needed after the test
148# hidden files for the test env is still stored in the current dir
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100149# files in the ./tmp is moved to ./tmp/prev when a new test is started
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200150if [ ! -d "tmp" ]; then
151 mkdir tmp
152fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100153curdir=$PWD
154cd tmp
155if [ $? -ne 0 ]; then
156 echo "Cannot cd to $PWD/tmp"
157 echo "Dir cannot be created. Exiting...."
158fi
159if [ ! -d "prev" ]; then
160 mkdir prev
161fi
162cd $curdir
163mv ./tmp/* ./tmp/prev 2> /dev/null
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200164
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100165# Create a http message log for this testcase
166HTTPLOG=$PWD"/.httplog_"$ATC".txt"
167echo "" > $HTTPLOG
168
169# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100170mkdir -p $TESTLOGS/$ATC
171
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100172# Save create for current logs
173mkdir -p $TESTLOGS/$ATC/previous
174
175rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
176rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
177rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
178
179mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
180mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
181mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
182
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100183# Clear the log dir for the test case
184rm $TESTLOGS/$ATC/*.log &> /dev/null
185rm $TESTLOGS/$ATC/*.txt &> /dev/null
186rm $TESTLOGS/$ATC/*.json &> /dev/null
187
188# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100189TCLOG=$TESTLOGS/$ATC/TC.log
190exec &> >(tee ${TCLOG})
191
192#Variables for counting tests as well as passed and failed tests
193RES_TEST=0
194RES_PASS=0
195RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100196RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200197RES_DEVIATION=0
198
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200199#Var to control if current stats shall be printed
200PRINT_CURRENT_STATS=0
201
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200202#File to keep deviation messages
203DEVIATION_FILE=".tmp_deviations"
204rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100205
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100206# Trap "command not found" and make the script fail
207trap_fnc() {
208
209 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100210 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100211 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200212 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100213 fi
214}
215trap trap_fnc ERR
216
217# Counter for tests
218TEST_SEQUENCE_NR=1
219
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100220# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100221__log_test_start() {
222 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
223 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
224 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
225 ((RES_TEST++))
226 ((TEST_SEQUENCE_NR++))
227}
228
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200229# Function to print current statistics
230__print_current_stats() {
231 if [ $PRINT_CURRENT_STATS -ne 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200232 echo " Current stats - exe-time, tests, passes, fails, conf fails, deviations: $(($SECONDS-$TCTEST_START)), $RES_TEST, $RES_PASS, $RES_FAIL, $RES_CONF_FAIL, $RES_DEVIATION"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200233 fi
234}
235
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100236# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100237__log_test_fail_general() {
238 echo -e $RED" FAIL."$1 $ERED
239 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200240 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100241 __check_stop_at_error
242}
243
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100244# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100245__log_test_fail_status_code() {
246 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
247 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200248 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100249 __check_stop_at_error
250}
251
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100252# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100253__log_test_fail_body() {
254 echo -e $RED" FAIL, returned body not correct"$ERED
255 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200256 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100257 __check_stop_at_error
258}
259
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100260# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100261__log_test_fail_not_supported() {
262 echo -e $RED" FAIL, function not supported"$ERED
263 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200264 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100265 __check_stop_at_error
266}
267
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100268# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100269__log_test_pass() {
270 if [ $# -gt 0 ]; then
271 echo $@
272 fi
273 ((RES_PASS++))
274 echo -e $GREEN" PASS"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200275 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100276}
277
278#Counter for configurations
279CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100280
281# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100282__log_conf_start() {
283 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
284 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
285 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
286 ((CONF_SEQUENCE_NR++))
287}
288
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100289# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100290__log_conf_fail_general() {
291 echo -e $RED" FAIL."$1 $ERED
292 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200293 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100294 __check_stop_at_error
295}
296
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100297# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100298__log_conf_fail_status_code() {
299 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
300 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200301 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100302 __check_stop_at_error
303}
304
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100305# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100306__log_conf_fail_body() {
307 echo -e $RED" FAIL, returned body not correct"$ERED
308 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200309 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100310 __check_stop_at_error
311}
312
BjornMagnussonXA83a750f2021-09-21 20:39:58 +0200313# Function to log a configuration that is not supported
314__log_conf_fail_not_supported() {
315 echo -e $RED" FAIL, function not supported"$ERED$@
316 ((RES_CONF_FAIL++))
317 __print_current_stats
318 __check_stop_at_error
319}
320
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100321# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100322__log_conf_ok() {
323 if [ $# -gt 0 ]; then
324 echo $@
325 fi
326 echo -e $GREEN" OK"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200327 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100328}
329
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100330#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100331TCTEST_START=$SECONDS
332
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200333#File to save timer measurement results
334TIMER_MEASUREMENTS=".timer_measurement.txt"
335echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
336
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200337# 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 +0200338IMAGE_REPO_ADR=""
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200339IMAGE_REPO_POLICY="local"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200340CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200341
YongchaoWu9a84f512019-12-16 22:54:11 +0100342echo "-------------------------------------------------------------------------------------------------"
343echo "----------------------------------- Test case: "$ATC
344echo "----------------------------------- Started: "$(date)
345echo "-------------------------------------------------------------------------------------------------"
346echo "-- Description: "$TC_ONELINE_DESCR
347echo "-------------------------------------------------------------------------------------------------"
348echo "----------------------------------- Test case setup -----------------------------------"
349
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100350echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200351START_ARG=$1
352paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100353paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200354if [ $# -lt 1 ]; then
355 paramerror=1
356fi
357if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100358 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200359 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100360 if [ -z "$paramerror_str" ]; then
361 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
362 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200363 else
364 shift;
365 fi
366fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100367if [ $paramerror -eq 0 ]; then
368 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
369 paramerror=1
370 if [ -z "$paramerror_str" ]; then
371 paramerror_str="Second arg shall be 'docker' or 'kube'"
372 fi
373 else
374 if [ $1 == "docker" ]; then
375 RUNMODE="DOCKER"
376 echo "Setting RUNMODE=DOCKER"
377 fi
378 if [ $1 == "kube" ]; then
379 RUNMODE="KUBE"
380 echo "Setting RUNMODE=KUBE"
381 fi
382 shift;
383 fi
384fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200385foundparm=0
386while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
387 foundparm=1
388 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100389 if [ "$1" == "release" ]; then
390 IMAGE_CATEGORY="RELEASE"
391 echo "Option set - Release image tags used for applicable images "
392 shift;
393 foundparm=0
394 fi
395 fi
396 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200397 if [ "$1" == "auto-clean" ]; then
398 AUTO_CLEAN="auto"
399 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200400 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200401 foundparm=0
402 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200403 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200404 if [ $paramerror -eq 0 ]; then
405 if [ "$1" == "--stop-at-error" ]; then
406 STOP_AT_ERROR=1
407 echo "Option set - Stop at first error"
408 shift;
409 foundparm=0
410 fi
411 fi
412 if [ $paramerror -eq 0 ]; then
413 if [ "$1" == "--ricsim-prefix" ]; then
414 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100415 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200416 if [ -z "$1" ]; then
417 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100418 if [ -z "$paramerror_str" ]; then
419 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
420 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200421 else
422 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
423 shift;
424 foundparm=0
425 fi
426 fi
427 fi
428 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200429 if [ "$1" == "--env-file" ]; then
430 shift;
431 TEST_ENV_VAR_FILE=$1
432 if [ -z "$1" ]; then
433 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100434 if [ -z "$paramerror_str" ]; then
435 paramerror_str="No env file found for flag: '--env-file'"
436 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200437 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200438 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200439 shift;
440 foundparm=0
441 fi
442 fi
443 fi
444 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200445 if [ "$1" == "--use-local-image" ]; then
446 USE_LOCAL_IMAGES=""
447 shift
448 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
449 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100450 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200451 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100452 if [ -z "$paramerror_str" ]; then
453 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
454 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200455 fi
456 shift;
457 done
458 foundparm=0
459 if [ -z "$USE_LOCAL_IMAGES" ]; then
460 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100461 if [ -z "$paramerror_str" ]; then
462 paramerror_str="No app name found for flag: '--use-local-image'"
463 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200464 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100465 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
466 fi
467 fi
468 fi
469 if [ $paramerror -eq 0 ]; then
470 if [ "$1" == "--use-snapshot-image" ]; then
471 USE_SNAPSHOT_IMAGES=""
472 shift
473 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
474 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
475 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
476 paramerror=1
477 if [ -z "$paramerror_str" ]; then
478 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
479 fi
480 fi
481 shift;
482 done
483 foundparm=0
484 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
485 paramerror=1
486 if [ -z "$paramerror_str" ]; then
487 paramerror_str="No app name found for flag: '--use-snapshot-image'"
488 fi
489 else
490 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
491 fi
492 fi
493 fi
494 if [ $paramerror -eq 0 ]; then
495 if [ "$1" == "--use-staging-image" ]; then
496 USE_STAGING_IMAGES=""
497 shift
498 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
499 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
500 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
501 paramerror=1
502 if [ -z "$paramerror_str" ]; then
503 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
504 fi
505 fi
506 shift;
507 done
508 foundparm=0
509 if [ -z "$USE_STAGING_IMAGES" ]; then
510 paramerror=1
511 if [ -z "$paramerror_str" ]; then
512 paramerror_str="No app name found for flag: '--use-staging-image'"
513 fi
514 else
515 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
516 fi
517 fi
518 fi
519 if [ $paramerror -eq 0 ]; then
520 if [ "$1" == "--use-release-image" ]; then
521 USE_RELEASE_IMAGES=""
522 shift
523 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
524 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
525 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
526 paramerror=1
527 if [ -z "$paramerror_str" ]; then
528 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
529 fi
530 fi
531 shift;
532 done
533 foundparm=0
534 if [ -z "$USE_RELEASE_IMAGES" ]; then
535 paramerror=1
536 if [ -z "$paramerror_str" ]; then
537 paramerror_str="No app name found for flag: '--use-release-image'"
538 fi
539 else
540 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200541 fi
542 fi
543 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200544 if [ $paramerror -eq 0 ]; then
545 if [ "$1" == "--image-repo" ]; then
546 shift;
547 IMAGE_REPO_ADR=$1
548 if [ -z "$1" ]; then
549 paramerror=1
550 if [ -z "$paramerror_str" ]; then
551 paramerror_str="No image repo url found for : '--image-repo'"
552 fi
553 else
554 echo "Option set - Image repo url: "$1
555 shift;
556 foundparm=0
557 fi
558 fi
559 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200560 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200561 if [ "$1" == "--repo-policy" ]; then
562 shift;
563 IMAGE_REPO_POLICY=$1
564 if [ -z "$1" ]; then
565 paramerror=1
566 if [ -z "$paramerror_str" ]; then
567 paramerror_str="No policy found for : '--repo-policy'"
568 fi
569 else
570 if [ "$1" == "local" ] || [ "$1" == "remote" ]; then
571 echo "Option set - Image repo policy: "$1
572 shift;
573 foundparm=0
574 else
575 paramerror=1
576 if [ -z "$paramerror_str" ]; then
577 paramerror_str="Repo policy shall be 'local' or 'remote'"
578 fi
579 fi
580 fi
581 fi
582 fi
583 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200584 if [ "$1" == "--cluster-timeout" ]; then
585 shift;
586 CLUSTER_TIME_OUT=$1
587 if [ -z "$1" ]; then
588 paramerror=1
589 if [ -z "$paramerror_str" ]; then
590 paramerror_str="No timeout value found for : '--cluster-timeout'"
591 fi
592 else
593 #Check if positive int
594 case ${CLUSTER_TIME_OUT#[+]} in
595 *[!0-9]* | '')
596 paramerror=1
597 if [ -z "$paramerror_str" ]; then
598 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
599 fi
600 ;;
601 * ) ;; # Ok
602 esac
603 echo "Option set - Cluster timeout: "$1
604 shift;
605 foundparm=0
606 fi
607 fi
608 fi
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200609 if [ $paramerror -eq 0 ]; then
610 if [ "$1" == "--print-stats" ]; then
611 PRINT_CURRENT_STATS=1
612 echo "Option set - Print stats"
613 shift;
614 foundparm=0
615 fi
616 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200617done
618echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200619
BjornMagnussonXAad047782020-06-08 15:54:11 +0200620#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200621if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
622 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100623 if [ -z "$paramerror_str" ]; then
624 paramerror_str="Unknown parameter(s): "$@
625 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200626fi
627
628if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100629 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
630 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200631 exit 1
632fi
633
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200634# sourcing the selected env variables for the test case
635if [ -f "$TEST_ENV_VAR_FILE" ]; then
636 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
637 . $TEST_ENV_VAR_FILE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100638
639 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100640 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 +0100641 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100642 found_profile=0
643 for prof in $SUPPORTED_PROFILES; do
644 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
645 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
646 found_profile=1
647 fi
648 done
649 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100650 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100651 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100652 echo -e $RED"Exiting...."$ERED
653 exit 1
654 fi
655 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200656else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200657 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200658 echo " Select one of following env var file matching the intended target of the test"
659 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100660 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200661 exit 1
662fi
663
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100664#This var need be preserved from the command line option, if set, when env var is sourced.
665if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
666 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
667fi
668
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100669if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100670 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100671 exit 1
672fi
673
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100674if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
675 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
676 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
677 exit 1
678fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100679
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100680# Choose list of included apps depending on run-mode
681if [ $RUNMODE == "KUBE" ]; then
682 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
683else
684 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
685fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200686
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100687# Check needed installed sw
688tmp=$(which python3)
689if [ $? -ne 0 ] || [ -z tmp ]; then
690 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
691 exit 1
692fi
693tmp=$(which docker)
694if [ $? -ne 0 ] || [ -z tmp ]; then
695 echo -e $RED"docker is required to run the test environment, pls install"$ERED
696 exit 1
697fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200698
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100699tmp=$(which docker-compose)
700if [ $? -ne 0 ] || [ -z tmp ]; then
701 if [ $RUNMODE == "DOCKER" ]; then
702 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
703 exit 1
704 fi
705fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200706
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100707tmp=$(which kubectl)
708if [ $? -ne 0 ] || [ -z tmp ]; then
709 if [ $RUNMODE == "KUBE" ]; then
710 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
711 exit 1
712 fi
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200713else
714 if [ $RUNMODE == "KUBE" ]; then
715 res=$(kubectl cluster-info 2>&1)
716 if [ $? -ne 0 ]; then
717 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
718 echo -e $BOLD$RED"Command 'kubectl cluster-info' returned error $ERED$EBOLD"
719 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
720 echo " "
721 echo "kubectl response:"
722 echo $res
723 echo " "
724 echo "This script may have been started with user with no permission to run kubectl"
725 echo "Try running with 'sudo' or set 'KUBECONFIG'"
726 echo "Do either 1, 2 or 3 "
727 echo " "
728 echo "1"
729 echo "Run with sudo"
730 echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD
731 echo " "
732 echo "2"
733 echo "Export KUBECONFIG and pass env to sudo - (replace user)"
734 echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD
735 echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD
736 echo " "
737 echo "3"
738 echo "Set KUBECONFIG inline (replace user)"
739 echo -e $BOLD"sudo KUBECONFIG='/home/<user>/.kube/config' <test-script-and-parameters>"$EBOLD
740
741 exit 1
742 fi
743 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100744fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100745
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100746echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100747
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100748#Temp var to check for image variable name errors
749IMAGE_ERR=0
750#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200751image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100752echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100753
754# 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 +0100755# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100756__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200757
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200758 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100759 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 +0100760 ((IMAGE_ERR++))
761 return
762 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100763
764 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200765 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100766 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200767 # Image is excluded since the corresponding app is not used in this test
768 return
769 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100770 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100771 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100772 image="${!3}"
773 tmptag=$4"_"$5
774 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100775
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200776 optional_image_repo_target=""
777
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100778 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100779 __check_ignore_image $1
780 if [ $? -eq 0 ]; then
781 app_ds=$6
782 if [ -z "$6" ]; then
783 app_ds="<app ignored>"
784 fi
785 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
786 # Image is ignored since the corresponding the images is not set in the env file
787 __remove_included_image $1 # Remove the image from the list of included images
788 return
789 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100790 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
791 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100792 echo ""
793 tmp=$tmp"<no-image>\t"
794 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200795
796 optional_image_repo_target=$image
797
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100798 #Add repo depending on image type
799 if [ "$5" == "REMOTE_RELEASE" ]; then
800 image=$NEXUS_RELEASE_REPO$image
801 fi
802 if [ "$5" == "REMOTE" ]; then
803 image=$NEXUS_STAGING_REPO$image
804 fi
805 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
806 image=$NEXUS_SNAPSHOT_REPO$image
807 fi
808 if [ "$5" == "REMOTE_PROXY" ]; then
809 image=$NEXUS_PROXY_REPO$image
810 fi
811 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
812 image=$NEXUS_RELEASE_REPO_ONAP$image
813 fi
814 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
815 image=$NEXUS_RELEASE_REPO_ORAN$image
816 fi
817 #No nexus repo added for local images, tag: LOCAL
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100818 tmp=$tmp$image"\t"
819 fi
820 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100821 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
822 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100823 echo ""
824 tmp=$tmp"<no-tag>\t"
825 else
826 tmp=$tmp$tag
827 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100828 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100829 echo -e "$tmp" >> $image_list_file
830 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200831 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 +0200832
833 remote_or_local_push=false
834 if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
835 if [ $5 == "LOCAL" ]; then
836 remote_or_local_push=true
837 fi
838 if [[ $5 == *"REMOTE"* ]]; then
839 if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
840 remote_or_local_push=true
841 fi
842 fi
843 fi
844 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 +0200845 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200846 if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
847 optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
848 optional_image_repo_target=$optional_image_repo_target_tmp
849 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200850 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
851 else
852 export "${2}_SOURCE"=""
853 export "${2}_TARGET"=""
854 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200855}
856
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200857# Check if app uses image included in this test run
858# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200859__check_included_image() {
860 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200861 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200862 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200863 fi
864 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200865 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200866}
867
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100868# Check if app uses a project image
869# Returns 0 if image is included, 1 if not
870__check_project_image() {
871 for im in $PROJECT_IMAGES; do
872 if [ "$1" == "$im" ]; then
873 return 0
874 fi
875 done
876 return 1
877}
878
879# Check if app uses image built by the test script
880# Returns 0 if image is included, 1 if not
881__check_image_local_build() {
882 for im in $LOCAL_IMAGE_BUILD; do
883 if [ "$1" == "$im" ]; then
884 return 0
885 fi
886 done
887 return 1
888}
889
890# Check if app image is conditionally ignored in this test run
891# Returns 0 if image is conditionally ignored, 1 if not
892__check_ignore_image() {
893 for im in $CONDITIONALLY_IGNORED_IMAGES; do
894 if [ "$1" == "$im" ]; then
895 return 0
896 fi
897 done
898 return 1
899}
900
901# Removed image from included list of included images
902# Used when an image is marked as conditionally ignored
903__remove_included_image() {
904 tmp_img_rem_list=""
905 for im in $INCLUDED_IMAGES; do
906 if [ "$1" != "$im" ]; then
907 tmp_img_rem_list=$tmp_img_rem_list" "$im
908 fi
909 done
910 INCLUDED_IMAGES=$tmp_img_rem_list
911 return 0
912}
913
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100914# Check if app is included in the prestarted set of apps
915# Returns 0 if image is included, 1 if not
916__check_prestarted_image() {
917 for im in $KUBE_PRESTARTED_IMAGES; do
918 if [ "$1" == "$im" ]; then
919 return 0
920 fi
921 done
922 return 1
923}
924
925# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100926__check_image_local_override() {
927 for im in $USE_LOCAL_IMAGES; do
928 if [ "$1" == "$im" ]; then
929 return 1
930 fi
931 done
932 return 0
933}
934
935# Check if app uses image override
936# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
937__check_image_override() {
938
939 for im in $ORAN_IMAGES_APP_NAMES; do
940 if [ "$1" == "$im" ]; then
941 echo "REMOTE_RELEASE_ORAN"
942 return 0
943 fi
944 done
945
946 for im in $ONAP_IMAGES_APP_NAMES; do
947 if [ "$1" == "$im" ]; then
948 echo "REMOTE_RELEASE_ONAP"
949 return 0
950 fi
951 done
952
953 found=0
954 for im in $PROJECT_IMAGES_APP_NAMES; do
955 if [ "$1" == "$im" ]; then
956 found=1
957 fi
958 done
959
960 if [ $found -eq 0 ]; then
961 echo "REMOTE_PROXY"
962 return 0
963 fi
964
965 suffix=""
966 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
967 suffix="REMOTE_RELEASE"
968 fi
969 if [ $IMAGE_CATEGORY == "DEV" ]; then
970 suffix="REMOTE"
971 fi
972 CTR=0
973 for im in $USE_STAGING_IMAGES; do
974 if [ "$1" == "$im" ]; then
975 suffix="REMOTE"
976 ((CTR++))
977 fi
978 done
979 for im in $USE_RELEASE_IMAGES; do
980 if [ "$1" == "$im" ]; then
981 suffix="REMOTE_RELEASE"
982 ((CTR++))
983 fi
984 done
985 for im in $USE_SNAPSHOT_IMAGES; do
986 if [ "$1" == "$im" ]; then
987 suffix="REMOTE_SNAPSHOT"
988 ((CTR++))
989 fi
990 done
991 for im in $USE_LOCAL_IMAGES; do
992 if [ "$1" == "$im" ]; then
993 suffix="LOCAL"
994 ((CTR++))
995 fi
996 done
997 echo $suffix
998 if [ $CTR -gt 1 ]; then
999 exit 1
1000 fi
1001 return 0
1002}
1003
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001004# Function to re-tag and image and push to another image repo
1005__retag_and_push_image() {
1006 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1007 source_image="${!1}"
1008 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
1009 target_image="${!trg_var_name}"
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001010
1011 if [ -z $target_image ]; then
1012 return 0 # Image with no target shall not be pushed
1013 fi
1014
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001015 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1016 tmp=$(docker image tag $source_image ${target_image} )
1017 if [ $? -ne 0 ]; then
1018 docker stop $tmp &> ./tmp/.dockererr
1019 ((IMAGE_ERR++))
1020 echo ""
1021 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1022 cat ./tmp/.dockererr
1023 return 1
1024 else
1025 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1026 fi
1027 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1028 tmp=$(docker push ${target_image} )
1029 if [ $? -ne 0 ]; then
1030 docker stop $tmp &> ./tmp/.dockererr
1031 ((IMAGE_ERR++))
1032 echo ""
1033 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1034 cat ./tmp/.dockererr
1035 return 1
1036 else
1037 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1038 fi
1039 export "${1}"=$target_image
1040 fi
1041 return 0
1042}
1043
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001044#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001045#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001046__check_and_pull_image() {
1047
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001048 source_image="${!4}"
1049
1050 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 +01001051 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001052 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001053
1054 if [ $1 == "local" ]; then
1055 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001056 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 +01001057 ((IMAGE_ERR++))
1058 return 1
1059 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001060 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001061 fi
1062 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
1063 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001064 if [ $RUNMODE == "DOCKER" ]; then
1065 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001066 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001067 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1068 docker stop $tmp &> ./tmp/.dockererr
1069 if [ $? -ne 0 ]; then
1070 ((IMAGE_ERR++))
1071 echo ""
1072 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
1073 cat ./tmp/.dockererr
1074 return 1
1075 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001076 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001077 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001078 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001079 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1080 docker rm $tmp &> ./tmp/.dockererr
1081 if [ $? -ne 0 ]; then
1082 ((IMAGE_ERR++))
1083 echo ""
1084 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
1085 cat ./tmp/.dockererr
1086 return 1
1087 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001088 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001089 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
1090 tmp_im=""
1091 else
1092 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001093 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001094 fi
1095 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001096 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001097 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001098 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001099 echo ""
1100 echo -e " Pulling image -$RED could not be pulled"$ERED
1101 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001102 echo $out > ./tmp/.dockererr
1103 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001104 return 1
1105 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001106 echo $out > ./tmp/.dockererr
1107 if [[ $out == *"up to date"* ]]; then
1108 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
1109 elif [[ $out == *"Downloaded newer image"* ]]; then
1110 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
1111 else
1112 echo -e " Pulling image -$GREEN Pulled $EGREEN"
1113 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001114 else
1115 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1116 fi
1117 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001118
1119 __retag_and_push_image $4
1120
1121 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001122}
1123
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001124setup_testenvironment() {
1125 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001126 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001127
1128 # Image var setup for all project images included in the test
1129 for imagename in $APP_SHORT_NAMES; do
1130 __check_included_image $imagename
1131 incl=$?
1132 __check_project_image $imagename
1133 proj=$?
1134 if [ $incl -eq 0 ]; then
1135 if [ $proj -eq 0 ]; then
1136 IMAGE_SUFFIX=$(__check_image_override $imagename)
1137 if [ $? -ne 0 ]; then
1138 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1139 ((IMAGE_ERR++))
1140 fi
1141 else
1142 IMAGE_SUFFIX="none"
1143 fi
1144 # A function name is created from the app short name
1145 # for example app short name 'ECS' -> produce the function
1146 # name __ECS_imagesetup
1147 # This function is called and is expected to exist in the imported
1148 # file for the ecs test functions
1149 # The resulting function impl will call '__check_and_create_image_var' function
1150 # with appropriate parameters
1151 # If the image suffix is none, then the component decides the suffix
1152 function_pointer="__"$imagename"_imagesetup"
1153 $function_pointer $IMAGE_SUFFIX
1154 fi
1155 done
1156
1157 #Errors in image setting - exit
1158 if [ $IMAGE_ERR -ne 0 ]; then
1159 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001160 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001161
1162 #Print a tables of the image settings
1163 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1164 column -t -s $'\t' $image_list_file | indent1
1165
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001166 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001167
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001168 #Set the SIM_GROUP var
1169 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1170 if [ -z "$SIM_GROUP" ]; then
1171 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1172 if [ ! -d $SIM_GROUP ]; then
1173 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1174 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1175 exit 1
1176 else
1177 echo " SIM_GROUP auto set to: " $SIM_GROUP
1178 fi
1179 elif [ $SIM_GROUP = *simulator_group ]; then
1180 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
1181 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001182 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001183 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1184 fi
1185
1186 echo ""
1187
1188 #Temp var to check for image pull errors
1189 IMAGE_ERR=0
1190
1191 # The following sequence pull the configured images
1192
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001193
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001194 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001195 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
1196 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 +02001197 else
1198 for imagename in $APP_SHORT_NAMES; do
1199 __check_included_image $imagename
1200 incl=$?
1201 __check_project_image $imagename
1202 proj=$?
1203 if [ $incl -eq 0 ]; then
1204 if [ $proj -eq 0 ]; then
1205 START_ARG_MOD=$START_ARG
1206 __check_image_local_override $imagename
1207 if [ $? -eq 1 ]; then
1208 START_ARG_MOD="local"
1209 fi
1210 else
1211 START_ARG_MOD=$START_ARG
1212 fi
1213 __check_image_local_build $imagename
1214 #No pull of images built locally
1215 if [ $? -ne 0 ]; then
1216 # A function name is created from the app short name
1217 # for example app short name 'HTTPPROXY' -> produce the function
1218 # name __HTTPPROXY_imagesetup
1219 # This function is called and is expected to exist in the imported
1220 # file for the httpproxy test functions
1221 # The resulting function impl will call '__check_and_pull_image' function
1222 # with appropriate parameters
1223 function_pointer="__"$imagename"_imagepull"
1224 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001225 fi
1226 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001227 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001228 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001229 done
1230 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001231
1232 #Errors in image setting - exit
1233 if [ $IMAGE_ERR -ne 0 ]; then
1234 echo ""
1235 echo "#################################################################################################"
1236 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1237 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1238 if [ $IMAGE_CATEGORY == "DEV" ]; then
BjornMagnussonXA720f5d72021-08-13 10:37:23 +02001239 echo ""
1240 echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED
1241 echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED
1242 echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED
1243 echo -e $RED"Use the 'App-short-name' for the applicable image from the above table: 'Images configured for start arg'."$ERED
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001244 fi
1245 echo "#################################################################################################"
1246 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001247 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001248 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001249
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001250 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001251
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001252 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001253
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001254 for imagename in $APP_SHORT_NAMES; do
1255 cd $AUTOTEST_HOME #Always reset to orig dir
1256 __check_image_local_build $imagename
1257 if [ $? -eq 0 ]; then
1258 __check_included_image $imagename
1259 if [ $? -eq 0 ]; then
1260 # A function name is created from the app short name
1261 # for example app short name 'MR' -> produce the function
1262 # name __MR_imagebuild
1263 # This function is called and is expected to exist in the imported
1264 # file for the mr test functions
1265 # The resulting function impl shall build the imagee
1266 function_pointer="__"$imagename"_imagebuild"
1267 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001268
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001269 else
1270 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1271 fi
1272 fi
1273 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001274
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001275 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001276
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001277 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001278
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001279 # Create a table of the images used in the script - from local repo
1280 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001281
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001282 docker_tmp_file=./tmp/.docker-images-table
1283 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1284 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001285
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001286 for imagename in $APP_SHORT_NAMES; do
1287 __check_included_image $imagename
1288 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001289 # Only print image data if image repo is null, or if image repo is set and image is local
1290 print_image_data=0
1291 if [ -z "$IMAGE_REPO_ADR" ]; then
1292 print_image_data=1
1293 else
1294 __check_image_local_build $imagename
1295 if [ $? -eq 0 ]; then
1296 print_image_data=1
1297 fi
1298 fi
1299 if [ $print_image_data -eq 1 ]; then
1300 # A function name is created from the app short name
1301 # for example app short name 'MR' -> produce the function
1302 # name __MR_imagebuild
1303 # This function is called and is expected to exist in the imported
1304 # file for the mr test functions
1305 # The resulting function impl shall build the imagee
1306 function_pointer="__"$imagename"_image_data"
1307 $function_pointer "$format_string" $docker_tmp_file
1308 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001309 fi
1310 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001311
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001312 column -t -s $'\t' $docker_tmp_file | indent1
1313
1314 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001315
1316 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1317
1318 # Create a table of the images used in the script - from remote repo
1319 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1320 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1321
1322 docker_tmp_file=./tmp/.docker-images-table
1323 format_string="{{.Repository}}\\t{{.Tag}}"
1324 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1325
1326 for imagename in $APP_SHORT_NAMES; do
1327 __check_included_image $imagename
1328 if [ $? -eq 0 ]; then
1329 # Only print image data if image repo is null, or if image repo is set and image is local
1330 __check_image_local_build $imagename
1331 if [ $? -ne 0 ]; then
1332 # A function name is created from the app short name
1333 # for example app short name 'MR' -> produce the function
1334 # name __MR_imagebuild
1335 # This function is called and is expected to exist in the imported
1336 # file for the mr test functions
1337 # The resulting function impl shall build the imagee
1338 function_pointer="__"$imagename"_image_data"
1339 $function_pointer "$format_string" $docker_tmp_file
1340 fi
1341 fi
1342 done
1343
1344 column -t -s $'\t' $docker_tmp_file | indent1
1345
1346 echo ""
1347 fi
1348
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001349 if [ $RUNMODE == "KUBE" ]; then
1350
1351 echo "================================================================================="
1352 echo "================================================================================="
1353
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001354 if [ -z "$IMAGE_REPO_ADR" ]; then
1355 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1356 echo -e " This setting only works on single node clusters on the local machine"
1357 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001358 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001359 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001360 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1361 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1362 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1363 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"
1364 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001365 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001366 CLUSTER_IP=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
1367 echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001368
1369 echo "================================================================================="
1370 echo "================================================================================="
1371 echo ""
1372 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001373
1374 echo -e $BOLD"======================================================="$EBOLD
1375 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1376 echo -e $BOLD"======================================================="$EBOLD
1377 echo ""
1378
1379}
YongchaoWu9a84f512019-12-16 22:54:11 +01001380
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001381# Function to print the test result, shall be the last cmd in a test script
1382# args: -
1383# (Function for test scripts)
1384print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001385
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001386 TCTEST_END=$SECONDS
1387 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001388
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001389 echo "-------------------------------------------------------------------------------------------------"
1390 echo "------------------------------------- Test case: "$ATC
1391 echo "------------------------------------- Ended: "$(date)
1392 echo "-------------------------------------------------------------------------------------------------"
1393 echo "-- Description: "$TC_ONELINE_DESCR
1394 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001395 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001396 echo "-------------------------------------------------------------------------------------------------"
1397 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001398 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001399
YongchaoWu21f17bb2020-03-05 12:44:08 +01001400
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001401 if [ $RES_DEVIATION -gt 0 ]; then
1402 echo "Test case deviations"
1403 echo "===================================="
1404 cat $DEVIATION_FILE
1405 fi
1406 echo ""
1407 echo "Timer measurement in the test script"
1408 echo "===================================="
1409 column -t -s $'\t' $TIMER_MEASUREMENTS
1410 echo ""
1411
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001412 total=$((RES_PASS+RES_FAIL))
1413 if [ $RES_TEST -eq 0 ]; then
1414 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1415 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1416 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1417 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1418 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1419 elif [ $total != $RES_TEST ]; then
1420 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1421 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1422 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1423 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1424 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1425 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001426 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001427 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1428 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1429 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1430 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1431 elif [ $RES_PASS = $RES_TEST ]; then
1432 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1433 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1434 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1435 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1436 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1437 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001438
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001439 # Update test suite counter
1440 if [ -f .tmp_tcsuite_pass_ctr ]; then
1441 tmpval=$(< .tmp_tcsuite_pass_ctr)
1442 ((tmpval++))
1443 echo $tmpval > .tmp_tcsuite_pass_ctr
1444 fi
1445 if [ -f .tmp_tcsuite_pass ]; then
1446 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1447 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001448 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001449 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001450 else
1451 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1452 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
1453 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
1454 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
1455 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
1456 echo ""
1457 # Update test suite counter
1458 if [ -f .tmp_tcsuite_fail_ctr ]; then
1459 tmpval=$(< .tmp_tcsuite_fail_ctr)
1460 ((tmpval++))
1461 echo $tmpval > .tmp_tcsuite_fail_ctr
1462 fi
1463 if [ -f .tmp_tcsuite_fail ]; then
1464 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1465 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001466 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001467
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001468 echo "++++ Number of tests: "$RES_TEST
1469 echo "++++ Number of passed tests: "$RES_PASS
1470 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01001471 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001472 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001473 echo ""
1474 echo "++++ Number of test case deviations: "$RES_DEVIATION
1475 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001476 echo "------------------------------------- Test case complete ---------------------------------"
1477 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01001478 echo ""
1479}
1480
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001481#####################################################################
1482###### Functions for start, configuring, stoping, cleaning etc ######
1483#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01001484
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001485# Start timer for time measurement
1486# args - (any args will be printed though)
1487start_timer() {
1488 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1489 TC_TIMER=$SECONDS
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001490 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001491}
1492
1493# Print the value of the time (in seconds)
1494# args - <timer message to print> - timer value and message will be printed both on screen
1495# and in the timer measurement report
1496print_timer() {
1497 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1498 if [ $# -lt 1 ]; then
1499 ((RES_CONF_FAIL++))
1500 __print_err "need 1 or more args, <timer message to print>" $@
1501 exit 1
1502 fi
1503 duration=$(($SECONDS-$TC_TIMER))
1504 if [ $duration -eq 0 ]; then
1505 duration="<1 second"
1506 else
1507 duration=$duration" seconds"
1508 fi
1509 echo " Timer duration :" $duration
1510
1511 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
1512}
1513
1514# Print the value of the time (in seconds) and reset the timer
1515# args - <timer message to print> - timer value and message will be printed both on screen
1516# and in the timer measurement report
1517print_and_reset_timer() {
1518 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1519 if [ $# -lt 1 ]; then
1520 ((RES_CONF_FAIL++))
1521 __print_err "need 1 or more args, <timer message to print>" $@
1522 exit 1
1523 fi
1524 duration=$(($SECONDS-$TC_TIMER))" seconds"
1525 if [ $duration -eq 0 ]; then
1526 duration="<1 second"
1527 else
1528 duration=$duration" seconds"
1529 fi
1530 echo " Timer duration :" $duration
1531 TC_TIMER=$SECONDS
1532 echo " Timer reset"
1533
1534 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
1535
1536}
1537# Print info about a deviations from intended tests
1538# Each deviation counted is also printed in the testreport
1539# args <deviation message to print>
1540deviation() {
1541 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
1542 if [ $# -lt 1 ]; then
1543 ((RES_CONF_FAIL++))
1544 __print_err "need 1 or more args, <deviation message to print>" $@
1545 exit 1
1546 fi
1547 ((RES_DEVIATION++))
1548 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
1549 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02001550 __print_current_stats
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001551 echo ""
1552}
YongchaoWu21f17bb2020-03-05 12:44:08 +01001553
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001554# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
1555__check_stop_at_error() {
1556 if [ $STOP_AT_ERROR -eq 1 ]; then
1557 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
1558 store_logs "STOP_AT_ERROR"
1559 exit 1
1560 fi
1561 return 0
1562}
1563
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001564# Stop and remove all containers
1565# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001566# (Not for test scripts)
1567__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001568
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001569 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001570
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001571 #Create empty file
1572 running_contr_file="./tmp/running_contr.txt"
1573 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001574
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001575 # Get list of all containers started by the test script
1576 for imagename in $APP_SHORT_NAMES; do
1577 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 +01001578 done
1579
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001580 tab_heading1="App display name"
1581 tab_heading2="App short name"
1582 tab_heading3="Container name"
1583
1584 tab_heading1_len=${#tab_heading1}
1585 tab_heading2_len=${#tab_heading2}
1586 tab_heading3_len=${#tab_heading3}
1587 cntr=0
1588 #Calc field lengths of each item in the list of containers
1589 while read p; do
1590 if (( $cntr % 3 == 0 ));then
1591 if [ ${#p} -gt $tab_heading1_len ]; then
1592 tab_heading1_len=${#p}
1593 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001594 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001595 if (( $cntr % 3 == 1));then
1596 if [ ${#p} -gt $tab_heading2_len ]; then
1597 tab_heading2_len=${#p}
1598 fi
1599 fi
1600 if (( $cntr % 3 == 2));then
1601 if [ ${#p} -gt $tab_heading3_len ]; then
1602 tab_heading3_len=${#p}
1603 fi
1604 fi
1605 let cntr=cntr+1
1606 done <$running_contr_file
1607
1608 let tab_heading1_len=tab_heading1_len+2
1609 while (( ${#tab_heading1} < $tab_heading1_len)); do
1610 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001611 done
1612
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001613 let tab_heading2_len=tab_heading2_len+2
1614 while (( ${#tab_heading2} < $tab_heading2_len)); do
1615 tab_heading2="$tab_heading2"" "
1616 done
1617
1618 let tab_heading3_len=tab_heading3_len+2
1619 while (( ${#tab_heading3} < $tab_heading3_len)); do
1620 tab_heading3="$tab_heading3"" "
1621 done
1622
1623 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
1624 cntr=0
1625 while read p; do
1626 if (( $cntr % 3 == 0 ));then
1627 row=""
1628 heading=$p
1629 heading_len=$tab_heading1_len
1630 fi
1631 if (( $cntr % 3 == 1));then
1632 heading=$p
1633 heading_len=$tab_heading2_len
1634 fi
1635 if (( $cntr % 3 == 2));then
1636 contr=$p
1637 heading=$p
1638 heading_len=$tab_heading3_len
1639 fi
1640 while (( ${#heading} < $heading_len)); do
1641 heading="$heading"" "
1642 done
1643 row=$row$heading
1644 if (( $cntr % 3 == 2));then
1645 echo -ne $row$SAMELINE
1646 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
1647 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1648 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
1649 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1650 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
1651 fi
1652 let cntr=cntr+1
1653 done <$running_contr_file
1654
YongchaoWu9a84f512019-12-16 22:54:11 +01001655 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001656
1657 echo -e $BOLD" Removing docker network"$EBOLD
1658 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
1659 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001660 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001661 if [ $? -ne 0 ]; then
1662 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
1663 exit 1
1664 fi
1665 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001666 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001667
1668 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001669 docker network prune --force | indent2
1670 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001671
1672 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001673 docker volume prune --force | indent2
1674 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001675
1676 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
1677 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001678 echo -e "$GREEN Done$EGREEN"
1679 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001680
1681 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
1682 if [ $? -eq 0 ]; then
1683 if [ $CONTRS -ne 0 ]; then
1684 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001685 docker ps -a | indent1
1686 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001687 fi
1688 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01001689}
1690
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001691###################################
1692### Functions for kube management
1693###################################
1694
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02001695# Get resource type for scaling
1696# args: <resource-name> <namespace>
1697__kube_get_resource_type() {
1698 kubectl get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1699 if [ $? -eq 0 ]; then
1700 echo "deployment"
1701 return 0
1702 fi
1703 kubectl get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
1704 if [ $? -eq 0 ]; then
1705 echo "sts"
1706 return 0
1707 fi
1708 echo "unknown-resource-type"
1709 return 1
1710}
1711
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001712# Scale a kube resource to a specific count
1713# args: <resource-type> <resource-name> <namespace> <target-count>
1714# (Not for test scripts)
1715__kube_scale() {
1716 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
1717 kubectl scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
1718 if [ $? -ne 0 ]; then
1719 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
1720 ((RES_CONF_FAIL++))
1721 echo " Message: $(<./tmp/kubeerr)"
1722 return 1
1723 else
1724 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
1725 fi
1726
1727 TSTART=$SECONDS
1728
1729 for i in {1..500}; do
1730 count=$(kubectl get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
1731 retcode=$?
1732 if [ -z "$count" ]; then
1733 #No value is sometimes returned for some reason, in case the resource has replica 0
1734 count=0
1735 fi
1736 if [ $retcode -ne 0 ]; then
1737 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
1738 ((RES_CONF_FAIL++))
1739 return 1
1740 fi
1741 #echo ""
1742 if [ $count -ne $4 ]; then
1743 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
1744 sleep $i
1745 else
1746 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
1747 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
1748 echo ""
1749 return 0
1750 fi
1751 done
1752 echo ""
1753 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
1754 ((RES_CONF_FAIL++))
1755 return 0
1756}
1757
1758# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1759# This function does not wait for the resource to reach 0
1760# args: <namespace> <label-name> <label-id>
1761# (Not for test scripts)
1762__kube_scale_all_resources() {
1763 namespace=$1
1764 labelname=$2
1765 labelid=$3
1766 resources="deployment replicaset statefulset"
1767 for restype in $resources; do
1768 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1769 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1770 deleted_resourcetypes=$deleted_resourcetypes" "$restype
1771 for resid in $result; do
1772 echo -ne " Ordered caling $restype $resid from namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
1773 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
1774 echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
1775 done
1776 fi
1777 done
1778}
1779
1780# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1781# This function do wait for the resource to reach 0
1782# args: <namespace> <label-name> <label-id>
1783# (Not for test scripts)
1784__kube_scale_and_wait_all_resources() {
1785 namespace=$1
1786 labelname=$2
1787 labelid=$3
1788 resources="deployment replicaset statefulset"
1789 scaled_all=1
1790 while [ $scaled_all -ne 0 ]; do
1791 scaled_all=0
1792 for restype in $resources; do
1793 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1794 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1795 for resid in $result; do
1796 echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0"
1797 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
1798 count=1
1799 T_START=$SECONDS
1800 while [ $count -ne 0 ]; do
1801 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
1802 echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE
1803 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
1804 sleep 0.5
1805 else
1806 count=0
1807 fi
1808 duration=$(($SECONDS-$T_START))
1809 if [ $duration -gt 100 ]; then
1810 #Forcring count 0, to avoid hanging for failed scaling
1811 scaled_all=1
1812 count=0
1813 fi
1814 done
1815 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN"
1816 done
1817 fi
1818 done
1819 done
1820}
1821
1822# Remove all kube resources in a namespace for resources having a certain label and label-id
1823# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
1824# args: <namespace> <label-name> <label-id>
1825# (Not for test scripts)
1826__kube_delete_all_resources() {
1827 namespace=$1
1828 labelname=$2
1829 labelid=$3
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001830 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001831 deleted_resourcetypes=""
1832 for restype in $resources; do
1833 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1834 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1835 deleted_resourcetypes=$deleted_resourcetypes" "$restype
1836 for resid in $result; do
1837 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
1838 count=1
1839 while [ $count -ne 0 ]; do
1840 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
1841 echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE
1842 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
1843 sleep 0.5
1844 else
1845 count=0
1846 fi
1847 done
1848 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN"
1849 fi
1850 echo -ne " Deleting $restype $resid from namespace $namespace with label $labelname=$labelid "$SAMELINE
1851 kubectl delete $restype $resid -n $namespace 1> /dev/null 2> ./tmp/kubeerr
1852 if [ $? -eq 0 ]; then
1853 echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN OK $EGREEN"
1854 else
1855 echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
1856 fi
1857 #fi
1858 done
1859 fi
1860 done
1861 if [ ! -z "$deleted_resourcetypes" ]; then
1862 for restype in $deleted_resources; do
1863 echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted..."$SAMELINE
1864 T_START=$SECONDS
1865 result="dummy"
1866 while [ ! -z "$result" ]; do
1867 sleep 0.5
1868 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1869 echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
1870 if [ -z "$result" ]; then
1871 echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
1872 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
1873 echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
1874 result=""
1875 fi
1876 done
1877 done
1878 fi
1879}
1880
1881# Creates a namespace if it does not exists
1882# args: <namespace>
1883# (Not for test scripts)
1884__kube_create_namespace() {
1885
1886 #Check if test namespace exists, if not create it
1887 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
1888 if [ $? -ne 0 ]; then
1889 echo -ne " Creating namespace "$1 $SAMELINE
1890 kubectl create namespace $1 1> /dev/null 2> ./tmp/kubeerr
1891 if [ $? -ne 0 ]; then
1892 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
1893 ((RES_CONF_FAIL++))
1894 echo " Message: $(<./tmp/kubeerr)"
1895 return 1
1896 else
1897 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
1898 fi
1899 else
1900 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
1901 fi
1902 return 0
1903}
1904
1905# Find the host ip of an app (using the service resource)
1906# args: <app-name> <namespace>
1907# (Not for test scripts)
1908__kube_get_service_host() {
1909 if [ $# -ne 2 ]; then
1910 ((RES_CONF_FAIL++))
1911 __print_err "need 2 args, <app-name> <namespace>" $@
1912 exit 1
1913 fi
1914 for timeout in {1..60}; do
1915 host=$(kubectl get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
1916 if [ $? -eq 0 ]; then
1917 if [ ! -z "$host" ]; then
1918 echo $host
1919 return 0
1920 fi
1921 fi
1922 sleep 0.5
1923 done
1924 ((RES_CONF_FAIL++))
1925 echo "host-not-found-fatal-error"
1926 return 1
1927}
1928
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001929# Find the named port to an app (using the service resource)
1930# args: <app-name> <namespace> <port-name>
1931# (Not for test scripts)
1932__kube_get_service_port() {
1933 if [ $# -ne 3 ]; then
1934 ((RES_CONF_FAIL++))
1935 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
1936 exit 1
1937 fi
1938
1939 for timeout in {1..60}; do
1940 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
1941 if [ $? -eq 0 ]; then
1942 if [ ! -z "$port" ]; then
1943 echo $port
1944 return 0
1945 fi
1946 fi
1947 sleep 0.5
1948 done
1949 ((RES_CONF_FAIL++))
1950 echo "0"
1951 return 1
1952}
1953
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001954# Find the named node port to an app (using the service resource)
1955# args: <app-name> <namespace> <port-name>
1956# (Not for test scripts)
1957__kube_get_service_nodeport() {
1958 if [ $# -ne 3 ]; then
1959 ((RES_CONF_FAIL++))
1960 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
1961 exit 1
1962 fi
1963
1964 for timeout in {1..60}; do
1965 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
1966 if [ $? -eq 0 ]; then
1967 if [ ! -z "$port" ]; then
1968 echo $port
1969 return 0
1970 fi
1971 fi
1972 sleep 0.5
1973 done
1974 ((RES_CONF_FAIL++))
1975 echo "0"
1976 return 1
1977}
1978
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001979# Create a kube resource from a yaml template
1980# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
1981# (Not for test scripts)
1982__kube_create_instance() {
1983 echo -ne " Creating $1 $2"$SAMELINE
1984 envsubst < $3 > $4
1985 kubectl apply -f $4 1> /dev/null 2> ./tmp/kubeerr
1986 if [ $? -ne 0 ]; then
1987 ((RES_CONF_FAIL++))
1988 echo -e " Creating $1 $2 $RED Failed $ERED"
1989 echo " Message: $(<./tmp/kubeerr)"
1990 return 1
1991 else
1992 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
1993 fi
1994}
1995
1996# Function to create a configmap in kubernetes
1997# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
1998# (Not for test scripts)
1999__kube_create_configmap() {
2000 echo -ne " Creating configmap $1 "$SAMELINE
2001 envsubst < $5 > $5"_tmp"
2002 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
2003 kubectl create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
2004 if [ $? -ne 0 ]; then
2005 echo -e " Creating configmap $1 $RED Failed $ERED"
2006 ((RES_CONF_FAIL++))
2007 return 1
2008 fi
2009
2010 kubectl apply -f $6 1> /dev/null 2> ./tmp/kubeerr
2011 if [ $? -ne 0 ]; then
2012 echo -e " Creating configmap $1 $RED Apply failed $ERED"
2013 echo " Message: $(<./tmp/kubeerr)"
2014 ((RES_CONF_FAIL++))
2015 return 1
2016 fi
2017 kubectl label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
2018 if [ $? -ne 0 ]; then
2019 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
2020 echo " Message: $(<./tmp/kubeerr)"
2021 ((RES_CONF_FAIL++))
2022 return 1
2023 fi
2024 # Log the resulting map
2025 kubectl get configmap $1 -n $2 -o yaml > $6
2026
2027 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
2028 return 0
2029}
2030
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002031# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
2032# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002033# args: <full kubectl cmd with parameters>
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002034# (Not for test scripts)
2035__kube_cmd_with_timeout() {
2036 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
2037
2038 while true; do
2039 kube_cmd_result=$($@)
2040 if [ $? -ne 0 ]; then
2041 kube_cmd_result=""
2042 fi
2043 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
2044 echo $kube_cmd_result
2045 return 0
2046 fi
2047 sleep 1
2048 done
2049}
2050
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002051# This function starts a pod that cleans a the contents of a path mounted as a pvc
2052# After this action the pod should terminate
2053# This should only be executed when the pod owning the pvc is not running
2054# args: <appname> <namespace> <pvc-name> <path-to remove>
2055# (Not for test scripts)
2056__kube_clean_pvc() {
2057
2058 export PVC_CLEANER_NAMESPACE=$2
2059 export PVC_CLEANER_CLAIMNAME=$3
2060 export PVC_CLEANER_RM_PATH=$4
2061 input_yaml=$SIM_GROUP"/pvc-cleaner/"pvc-cleaner.yaml
2062 output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml
2063
2064 envsubst < $input_yaml > $output_yaml
2065
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002066 kubectl delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002067
2068 __kube_create_instance pod pvc-cleaner $input_yaml $output_yaml
2069 if [ $? -ne 0 ]; then
2070 echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work"
2071 return 1
2072 fi
2073
2074 term_ts=$(($SECONDS+30))
2075 while [ $term_ts -gt $SECONDS ]; do
2076 pod_status=$(kubectl get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase")
2077 if [ "$pod_status" == "Succeeded" ]; then
2078 return 0
2079 fi
2080 done
2081 return 1
2082}
2083
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002084# This function scales or deletes all resources for app selected by the testcase.
2085# args: -
2086# (Not for test scripts)
2087__clean_kube() {
2088 echo -e $BOLD"Initialize kube services//pods/statefulsets/replicaset to initial state"$EBOLD
2089
2090 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002091 for imagename in $APP_SHORT_NAMES; do
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002092 # A function name is created from the app short name
2093 # for example app short name 'RICMSIM' -> produce the function
2094 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
2095 # This function is called and is expected to exist in the imported
2096 # file for the ricsim test functions
2097 # The resulting function impl shall scale the resources to 0
2098 # For prestarted apps, the function waits until the resources are 0
2099 # For included (not prestated) apps, the scaling is just ordered
2100 __check_prestarted_image $imagename
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002101 if [ $? -eq 0 ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002102 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002103 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2104 $function_pointer
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002105 else
2106 __check_included_image $imagename
2107 if [ $? -eq 0 ]; then
2108 function_pointer="__"$imagename"_kube_scale_zero"
2109 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2110 $function_pointer
2111 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002112 fi
2113 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002114
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002115 # Delete managed apps
2116 for imagename in $APP_SHORT_NAMES; do
2117 __check_included_image $imagename
2118 if [ $? -eq 0 ]; then
2119 __check_prestarted_image $imagename
2120 if [ $? -ne 0 ]; then
2121 # A function name is created from the app short name
2122 # for example app short name 'RICMSIM' -> produce the function
2123 # name __RICSIM__kube_delete_all
2124 # This function is called and is expected to exist in the imported
2125 # file for the ricsim test functions
2126 # The resulting function impl shall delete all its resources
2127 function_pointer="__"$imagename"_kube_delete_all"
2128 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
2129 $function_pointer
2130 fi
2131 fi
2132 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002133
2134 echo ""
2135}
2136
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002137# Function stop and remove all containers (docker) and services/deployments etc(kube)
2138# args: -
2139# Function for test script
2140clean_environment() {
2141 if [ $RUNMODE == "KUBE" ]; then
2142 __clean_kube
2143 else
2144 __clean_containers
2145 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002146}
2147
2148# 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 +01002149# args: -
2150# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002151auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002152 echo
2153 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002154 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
2155 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01002156 fi
2157}
2158
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002159# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002160# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002161# (Function for test scripts)
2162sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01002163
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002164 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
2165 if [ $# -lt 1 ]; then
2166 ((RES_CONF_FAIL++))
2167 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
2168 exit 1
2169 fi
2170 #echo "---- Sleep for " $1 " seconds ---- "$2
2171 start=$SECONDS
2172 duration=$((SECONDS-start))
2173 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002174 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002175 sleep 1
2176 duration=$((SECONDS-start))
2177 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002178 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002179 echo ""
2180}
2181
2182# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2183# Not to be called from the test script itself.
2184__print_err() {
2185 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2186 if [ $# -gt 1 ]; then
2187 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2188 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002189 ((RES_CONF_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002190}
2191
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002192# Function to create the docker network for the test
2193# Not to be called from the test script itself.
2194__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002195 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002196 if [ $? -ne 0 ]; then
2197 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2198 return 1
2199 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002200 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002201 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2202 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002203 if [ $? -ne 0 ]; then
2204 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2205 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002206 else
2207 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002208 fi
2209 else
2210 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2211 fi
2212}
2213
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002214# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002215# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2216#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002217# (Not for test scripts)
2218__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002219
2220 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002221 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002222 __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 +01002223 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002224 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002225
2226 __create_docker_network
2227
2228 curdir=$PWD
2229 cd $SIM_GROUP
2230 compose_dir=$1
2231 cd $1
2232 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002233 compose_file=$1
2234 if [ -z "$compose_file" ]; then
2235 compose_file="docker-compose.yml"
2236 fi
2237 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002238 compose_args=$1
2239 shift
2240 appcount=$1
2241 shift
2242
2243 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002244 docker-compose -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002245 if [ $? -ne 0 ]; then
2246 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2247 cat .dockererr
2248 echo -e $RED"Stopping script...."$ERED
2249 exit 1
2250 fi
2251 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002252 docker-compose -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002253 if [ $? -ne 0 ]; then
2254 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2255 cat .dockererr
2256 echo -e $RED"Stopping script...."$ERED
2257 exit 1
2258 fi
2259 fi
2260
2261 cd $curdir
2262
2263 appindex=0
2264 while [ $appindex -lt $appcount ]; do
2265 appname=$1
2266 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002267 app_started=0
2268 for i in {1..10}; do
2269 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002270 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 +02002271 app_started=1
2272 break
2273 else
2274 sleep $i
2275 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002276 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002277 if [ $app_started -eq 0 ]; then
2278 ((RES_CONF_FAIL++))
2279 echo ""
2280 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002281 echo -e $RED" Stopping script..."$ERED
2282 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002283 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002284 let appindex=appindex+1
2285 done
2286 return 0
2287}
2288
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002289# Function to check if container/service is responding to http/https
2290# args: <container-name>|<service-name> url
2291# (Not for test scripts)
2292__check_service_start() {
2293
2294 if [ $# -ne 2 ]; then
2295 ((RES_CONF_FAIL++))
2296 __print_err "need 2 args, <container-name>|<service-name> url" $@
2297 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002298 fi
2299
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002300 if [ $RUNMODE == "KUBE" ]; then
2301 ENTITY="service/set/deployment"
2302 else
2303 ENTITY="container"
2304 fi
2305 appname=$1
2306 url=$2
2307 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2308
2309
YongchaoWu9a84f512019-12-16 22:54:11 +01002310 pa_st=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002311 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002312 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002313 loop_ctr=0
2314 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002315 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002316 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002317 if [ ${#result} -gt 15 ]; then
2318 #If response is too long, truncate
2319 result="...response text too long, omitted"
2320 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002321 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2322 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 +01002323 pa_st=true
2324 break
2325 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002326 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002327 TS_OFFSET=$loop_ctr
2328 if (( $TS_OFFSET > 5 )); then
2329 TS_OFFSET=5
2330 fi
2331 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
2332 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 +02002333 sleep 1
2334 done
YongchaoWu9a84f512019-12-16 22:54:11 +01002335 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002336 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01002337 done
2338
2339 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002340 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002341 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 +01002342 return 1
2343 fi
2344
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002345 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002346 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002347}
2348
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002349
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002350#################
2351### Log functions
2352#################
2353
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002354__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002355
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002356 dispname=$1
2357 appname=$2
2358 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002359 warning=$4
2360 error=$5
2361
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002362 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
2363
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002364 if [ $RUNMODE == "KUBE" ]; then
2365 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
2366 return
2367 fi
2368
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002369 #tmp=$(docker ps | grep $appname)
2370 tmp=$(docker ps -q --filter name=$appname) #get the container id
2371 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002372 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002373 return
2374 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002375 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002376 if [ $? -ne 0 ];then
2377 echo " Problem to search $appname log $logpath"
2378 else
2379 if [ $foundentries -eq 0 ]; then
2380 echo " No WARN entries found in $appname log $logpath"
2381 else
2382 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
2383 fi
2384 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002385 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002386 if [ $? -ne 0 ];then
2387 echo " Problem to search $appname log $logpath"
2388 else
2389 if [ $foundentries -eq 0 ]; then
2390 echo " No ERR entries found in $appname log $logpath"
2391 else
2392 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
2393 fi
2394 fi
2395 echo ""
2396}
2397
2398# Store all container logs and other logs in the log dir for the script
2399# Logs are stored with a prefix in case logs should be stored several times during a test
2400# args: <logfile-prefix>
2401# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01002402store_logs() {
2403 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002404 ((RES_CONF_FAIL++))
2405 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01002406 exit 1
2407 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002408 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 +01002409
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002410 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002411
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002412 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002413
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002414 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
2415
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002416 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002417
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002418 # Store docker logs for all container
2419 for imagename in $APP_SHORT_NAMES; do
2420 __check_included_image $imagename
2421 if [ $? -eq 0 ]; then
2422 # A function name is created from the app short name
2423 # for example app short name 'RICMSIM' -> produce the function
2424 # name __RICSIM__store_docker_logs
2425 # This function is called and is expected to exist in the imported
2426 # file for the ricsim test functions
2427 # The resulting function impl shall store the docker logs for each container
2428 function_pointer="__"$imagename"_store_docker_logs"
2429 $function_pointer "$TESTLOGS/$ATC/" $1
2430 fi
2431 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002432 fi
2433 if [ $RUNMODE == "KUBE" ]; then
2434 namespaces=$(kubectl get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
2435 for nsid in $namespaces; do
2436 pods=$(kubectl get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
2437 for podid in $pods; do
2438 kubectl logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
2439 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002440 done
2441 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002442 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01002443}
2444
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002445###############
2446## Generic curl
2447###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002448# Generic curl function, assumes all 200-codes are ok
2449# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002450# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
2451# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01002452__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002453 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002454 proxyflag=""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002455 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002456 if [ ! -z "$KUBE_PROXY_PATH" ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +02002457 if [ $KUBE_PROXY_HTTPX == "http" ]; then
2458 proxyflag=" --proxy $KUBE_PROXY_PATH"
2459 else
2460 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
2461 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002462 fi
2463 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002464 curlString="curl -skw %{http_code} $proxyflag $@"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002465 echo " CMD: $curlString" >> $HTTPLOG
2466 res=$($curlString)
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002467 retcode=$?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002468 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002469 echo " RETCODE: $retcode" >> $HTTPLOG
2470 if [ $retcode -ne 0 ]; then
2471 echo "<no-response-from-server>"
2472 return 1
2473 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002474 http_code="${res:${#res}-3}"
2475 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002476 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
2477 echo "<no-response-from-server>"
2478 return 1
2479 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002480 return 0
2481 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002482 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002483 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01002484 echo "<not found, resp:${http_code}>"
2485 return 1
2486 fi
2487 if [ $# -eq 2 ]; then
2488 echo "${res:0:${#res}-3}" | xargs
2489 else
2490 echo "${res:0:${#res}-3}"
2491 fi
2492
2493 return 0
2494 fi
2495}
2496
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002497#######################################
2498### Basic helper function for test cases
2499#######################################
2500
2501# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
2502# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
2503# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
2504# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
2505# before setting pass or fail depending on the result of comparing variable and target using the operator.
2506# 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.
2507# Not to be called from test script.
2508
2509__var_test() {
2510 checkjsonarraycount=0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002511 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002512 if [ $# -eq 6 ]; then
2513 if [[ $3 == "json:"* ]]; then
2514 checkjsonarraycount=1
2515 fi
2516
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002517 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002518 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG
2519
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002520 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002521 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002522 start=$SECONDS
2523 ctr=0
2524 for (( ; ; )); do
2525 if [ $checkjsonarraycount -eq 0 ]; then
2526 result="$(__do_curl $2$3)"
2527 retcode=$?
2528 result=${result//[[:blank:]]/} #Strip blanks
2529 else
2530 path=${3:5}
2531 result="$(__do_curl $2$path)"
2532 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002533 echo "$result" > ./tmp/.tmp.curl.json
2534 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002535 fi
2536 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002537 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002538 let ctr=ctr+1
2539 if [ $retcode -ne 0 ]; then
2540 if [ $duration -gt $6 ]; then
2541 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002542 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002543 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002544 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002545 return
2546 fi
2547 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2548 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002549 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002550 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002551 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002552 return
2553 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2554 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002555 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002556 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002557 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002558 return
2559 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2560 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002561 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002562 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002563 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002564 return
2565 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2566 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002567 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002568 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002569 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002570 return
2571 else
2572 if [ $duration -gt $6 ]; then
2573 ((RES_FAIL++))
2574 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002575 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002576 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002577 return
2578 fi
2579 fi
2580 sleep 1
2581 done
2582 elif [ $# -eq 5 ]; then
2583 if [[ $3 == "json:"* ]]; then
2584 checkjsonarraycount=1
2585 fi
2586
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002587 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002588 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002589 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002590 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002591 if [ $checkjsonarraycount -eq 0 ]; then
2592 result="$(__do_curl $2$3)"
2593 retcode=$?
2594 result=${result//[[:blank:]]/} #Strip blanks
2595 else
2596 path=${3:5}
2597 result="$(__do_curl $2$path)"
2598 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002599 echo "$result" > ./tmp/.tmp.curl.json
2600 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002601 fi
2602 if [ $retcode -ne 0 ]; then
2603 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002604 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002605 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002606 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002607 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2608 ((RES_PASS++))
2609 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002610 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002611 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2612 ((RES_PASS++))
2613 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002614 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002615 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2616 ((RES_PASS++))
2617 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002618 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002619 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2620 ((RES_PASS++))
2621 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002622 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002623 else
2624 ((RES_FAIL++))
2625 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002626 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002627 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002628 fi
2629 else
2630 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
2631 echo "Got:" $@
2632 exit 1
2633 fi
2634}