blob: a94d3e2274b0b6d7e2e43bc5567e6b5eaff29084 [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>+]"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +010029 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]"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +010031 echo " [--override <override-environment-filename>] [--pre-clean] [--gen-stats] [--delete-namespaces]"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +010032 echo " [--delete-containers] [--endpoint-stats] [--kubeconfig <config-file>]"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010033}
34
35if [ $# -eq 1 ] && [ "$1" == "help" ]; then
36
37 if [ ! -z "$TC_ONELINE_DESCR" ]; then
38 echo "Test script description:"
39 echo $TC_ONELINE_DESCR
40 echo ""
41 fi
42 __print_args
43 echo ""
44 echo "remote - Use images from remote repositories. Can be overridden for individual images using the '--use_xxx' flags"
45 echo "remote-remove - Same as 'remote' but will also try to pull fresh images from remote repositories"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010046 echo "docker - Test executed in docker environment"
47 echo "kube - Test executed in kubernetes environment - requires an already started kubernetes environment"
BjornMagnussonXA663566c2021-11-08 10:25:07 +010048 echo "--env-file <file> - The script will use the supplied file to read environment variables from"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010049 echo "release - If this flag is given the script will use release version of the images"
50 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."
51 echo "--stop-at-error - The script will stop when the first failed test or configuration"
52 echo "--ricsim-prefix - The a1 simulator will use the supplied string as container prefix instead of 'ricsim'"
53 echo "--use-local-image - The script will use local images for the supplied apps, space separated list of app short names"
54 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"
55 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"
56 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 +020057 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 +020058 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 +020059 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 +020060 echo "--print-stats - Print current test stats after each test."
BjornMagnussonXA663566c2021-11-08 10:25:07 +010061 echo "--override <file> - Override setting from the file supplied by --env-file"
62 echo "--pre-clean - Will clean kube resouces when running docker and vice versa"
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010063 echo "--gen-stats - Collect container/pod runtime statistics"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +010064 echo "--delete-namespaces - Delete kubernetes namespaces before starting tests - but only those created by the test scripts. Kube mode only. Ignored if running with prestarted apps."
65 echo "--delete-containers - Delete docker containers before starting tests - but only those created by the test scripts. Docker mode only."
66 echo "--endpoint-stats - Collect endpoint statistics"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +010067 echo "--kubeconfig - Configure kubectl to use cluster specific cluster config file"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010068 echo ""
69 echo "List of app short names supported: "$APP_SHORT_NAMES
70 exit 0
71fi
72
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010073AUTOTEST_HOME=$PWD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020074# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
75# FTC1.sh -> ATC == FTC1
76ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
77
78#Create result file (containing '1' for error) for this test case
79#Will be replaced with a file containing '0' if all test cases pass
80echo "1" > "$PWD/.result$ATC.txt"
81
BjornMagnussonXA80a92002020-03-19 14:31:06 +010082#Formatting for 'echo' cmd
83BOLD="\033[1m"
84EBOLD="\033[0m"
85RED="\033[31m\033[1m"
86ERED="\033[0m"
87GREEN="\033[32m\033[1m"
88EGREEN="\033[0m"
89YELLOW="\033[33m\033[1m"
90EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020091SAMELINE="\033[0K\r"
92
BjornMagnussonXA80a92002020-03-19 14:31:06 +010093# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020094echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +010095
BjornMagnussonXA575869c2020-09-14 21:28:54 +020096# default test environment variables
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020097TEST_ENV_VAR_FILE=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +010098#Override env file, will be added on top of the above file
99TEST_ENV_VAR_FILE_OVERRIDE=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100100
101echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
102
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100103#Localhost constants
104LOCALHOST_NAME="localhost"
105LOCALHOST_HTTP="http://localhost"
106LOCALHOST_HTTPS="https://localhost"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200107
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100108# Var to hold 'auto' in case containers shall be stopped when test case ends
109AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +0100110
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100111# Var to indicate pre clean, if flag --pre-clean is set the script will clean kube resouces when running docker and vice versa
112PRE_CLEAN="0"
113
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100114# Var to hold the app names to use local images for
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200115USE_LOCAL_IMAGES=""
116
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100117# Var to hold the app names to use remote snapshot images for
118USE_SNAPSHOT_IMAGES=""
119
120# Var to hold the app names to use remote staging images for
121USE_STAGING_IMAGES=""
122
123# Var to hold the app names to use remote release images for
124USE_RELEASE_IMAGES=""
125
BjornMagnussonXAad047782020-06-08 15:54:11 +0200126
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200127# 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
128STOP_AT_ERROR=0
129
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100130# The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used.
131# The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used
132# Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE
133IMAGE_CATEGORY="DEV"
134
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100135#Var to indicate docker-compose version, V1 or V2
136#V1 names replicated containers <proj-name>_<service-name>_<index>
137#V2 names replicated containers <proj-name>-<service-name>-<index>
138DOCKER_COMPOSE_VERION="V1"
139
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200140# Function to indent cmd output with one space
141indent1() { sed 's/^/ /'; }
142
143# Function to indent cmd output with two spaces
144indent2() { sed 's/^/ /'; }
145
YongchaoWu9a84f512019-12-16 22:54:11 +0100146# Set a description string for the test case
147if [ -z "$TC_ONELINE_DESCR" ]; then
148 TC_ONELINE_DESCR="<no-description>"
149 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
150fi
151
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100152# Counter for test suites
153if [ -f .tmp_tcsuite_ctr ]; then
154 tmpval=$(< .tmp_tcsuite_ctr)
155 ((tmpval++))
156 echo $tmpval > .tmp_tcsuite_ctr
157fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100158
YongchaoWu9a84f512019-12-16 22:54:11 +0100159# Create the logs dir if not already created in the current dir
160if [ ! -d "logs" ]; then
161 mkdir logs
162fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100163TESTLOGS=$PWD/logs
164
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200165# Create the tmp dir for temporary files that is not needed after the test
166# hidden files for the test env is still stored in the current dir
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100167# files in the ./tmp is moved to ./tmp/prev when a new test is started
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200168if [ ! -d "tmp" ]; then
169 mkdir tmp
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100170 if [ $? -ne 0 ]; then
171 echo "Cannot create dir for temp files, $PWD/tmp"
172 echo "Exiting...."
173 exit 1
174 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200175fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100176curdir=$PWD
177cd tmp
178if [ $? -ne 0 ]; then
179 echo "Cannot cd to $PWD/tmp"
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100180 echo "Exiting...."
181 exit 1
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100182fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100183
184TESTENV_TEMP_FILES=$PWD
185
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100186if [ ! -d "prev" ]; then
187 mkdir prev
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100188 if [ $? -ne 0 ]; then
189 echo "Cannot create dir for previous temp files, $PWD/prev"
190 echo "Exiting...."
191 exit 1
192 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100193fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100194
195TMPFILES=$(ls -A | grep -vw prev)
196if [ ! -z "$TMPFILES" ]; then
197 cp -r $TMPFILES prev #Move all temp files to prev dir
198 if [ $? -ne 0 ]; then
199 echo "Cannot move temp files in $PWD to previous temp files in, $PWD/prev"
200 echo "Exiting...."
201 exit 1
202 fi
203 if [ $(pwd | xargs basename) == "tmp" ]; then #Check that current dir is tmp...for safety
204
205 rm -rf $TMPFILES # Remove all temp files
206 fi
207fi
208
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100209cd $curdir
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100210if [ $? -ne 0 ]; then
211 echo "Cannot cd to $curdir"
212 echo "Exiting...."
213 exit 1
214fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200215
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100216
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100217# Create a http message log for this testcase
218HTTPLOG=$PWD"/.httplog_"$ATC".txt"
219echo "" > $HTTPLOG
220
221# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100222mkdir -p $TESTLOGS/$ATC
223
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100224# Save create for current logs
225mkdir -p $TESTLOGS/$ATC/previous
226
227rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
228rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
229rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
230
231mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
232mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
233mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
234
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100235# Clear the log dir for the test case
236rm $TESTLOGS/$ATC/*.log &> /dev/null
237rm $TESTLOGS/$ATC/*.txt &> /dev/null
238rm $TESTLOGS/$ATC/*.json &> /dev/null
239
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100240#Create result file in the log dir
241echo "1" > "$TESTLOGS/$ATC/.result$ATC.txt"
242
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100243# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100244TCLOG=$TESTLOGS/$ATC/TC.log
245exec &> >(tee ${TCLOG})
246
247#Variables for counting tests as well as passed and failed tests
248RES_TEST=0
249RES_PASS=0
250RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100251RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200252RES_DEVIATION=0
253
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200254#Var to control if current stats shall be printed
255PRINT_CURRENT_STATS=0
256
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100257#Var to control if container/pod runtim statistics shall be collected
258COLLECT_RUNTIME_STATS=0
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +0100259COLLECT_RUNTIME_STATS_PID=0
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100260
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100261#Var to control if endpoint statistics shall be collected
262COLLECT_ENDPOINT_STATS=0
263
264#Var to control if namespaces shall be delete before test setup
265DELETE_KUBE_NAMESPACES=0
266
267#Var to control if containers shall be delete before test setup
268DELETE_CONTAINERS=0
269
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +0100270#Var to configure kubectl from a config file.
271KUBECONF=""
272
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200273#File to keep deviation messages
274DEVIATION_FILE=".tmp_deviations"
275rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100276
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100277# Trap "command not found" and make the script fail
278trap_fnc() {
279
280 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100281 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100282 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200283 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100284 fi
285}
286trap trap_fnc ERR
287
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +0100288# Trap to kill subprocess for stats collection (if running)
289trap_fnc2() {
290 if [ $COLLECT_RUNTIME_STATS_PID -ne 0 ]; then
291 kill $COLLECT_RUNTIME_STATS_PID
292 fi
293}
294trap trap_fnc2 EXIT
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100295
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100296# Counter for tests
297TEST_SEQUENCE_NR=1
298
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100299# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100300__log_test_start() {
301 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
302 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
303 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
304 ((RES_TEST++))
305 ((TEST_SEQUENCE_NR++))
306}
307
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200308# Function to print current statistics
309__print_current_stats() {
310 if [ $PRINT_CURRENT_STATS -ne 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200311 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 +0200312 fi
313}
314
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100315# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100316__log_test_fail_general() {
317 echo -e $RED" FAIL."$1 $ERED
318 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200319 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100320 __check_stop_at_error
321}
322
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100323# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100324__log_test_fail_status_code() {
325 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
326 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200327 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100328 __check_stop_at_error
329}
330
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100331# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100332__log_test_fail_body() {
333 echo -e $RED" FAIL, returned body not correct"$ERED
334 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200335 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100336 __check_stop_at_error
337}
338
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100339# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100340__log_test_fail_not_supported() {
341 echo -e $RED" FAIL, function not supported"$ERED
342 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200343 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100344 __check_stop_at_error
345}
346
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100347# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100348__log_test_pass() {
349 if [ $# -gt 0 ]; then
350 echo $@
351 fi
352 ((RES_PASS++))
353 echo -e $GREEN" PASS"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200354 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100355}
356
357#Counter for configurations
358CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100359
360# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100361__log_conf_start() {
362 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
363 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
364 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
365 ((CONF_SEQUENCE_NR++))
366}
367
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100368# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100369__log_conf_fail_general() {
370 echo -e $RED" FAIL."$1 $ERED
371 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200372 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100373 __check_stop_at_error
374}
375
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100376# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100377__log_conf_fail_status_code() {
378 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
379 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200380 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100381 __check_stop_at_error
382}
383
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100384# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100385__log_conf_fail_body() {
386 echo -e $RED" FAIL, returned body not correct"$ERED
387 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200388 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100389 __check_stop_at_error
390}
391
BjornMagnussonXA83a750f2021-09-21 20:39:58 +0200392# Function to log a configuration that is not supported
393__log_conf_fail_not_supported() {
394 echo -e $RED" FAIL, function not supported"$ERED$@
395 ((RES_CONF_FAIL++))
396 __print_current_stats
397 __check_stop_at_error
398}
399
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100400# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100401__log_conf_ok() {
402 if [ $# -gt 0 ]; then
403 echo $@
404 fi
405 echo -e $GREEN" OK"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200406 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100407}
408
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100409# Function to collect stats on endpoints
410# args: <app-id> <end-point-no> <http-operation> <end-point-url> <http-status> [<count>]
411__collect_endpoint_stats() {
412 if [ $COLLECT_ENDPOINT_STATS -eq 0 ]; then
413 return
414 fi
415 ENDPOINT_COUNT=1
416 if [ $# -gt 5 ]; then
417 ENDPOINT_COUNT=$6
418 fi
419 ENDPOINT_STAT_FILE=$TESTLOGS/$ATC/endpoint_$ATC_$1_$2".log"
420 ENDPOINT_POS=0
421 ENDPOINT_NEG=0
422 if [ -f $ENDPOINT_STAT_FILE ]; then
423 ENDPOINT_VAL=$(< $ENDPOINT_STAT_FILE)
424 ENDPOINT_POS=$(echo $ENDPOINT_VAL | cut -f4 -d ' ' | cut -f1 -d '/')
425 ENDPOINT_NEG=$(echo $ENDPOINT_VAL | cut -f5 -d ' ' | cut -f1 -d '/')
426 fi
427
428 if [ $5 -ge 200 ] && [ $5 -lt 300 ]; then
429 let ENDPOINT_POS=ENDPOINT_POS+$ENDPOINT_COUNT
430 else
431 let ENDPOINT_NEG=ENDPOINT_NEG+$ENDPOINT_COUNT
432 fi
433
434 printf '%-2s %-10s %-45s %-16s %-16s' "#" "$3" "$4" "$ENDPOINT_POS/$ENDPOINT_POS" "$ENDPOINT_NEG/$ENDPOINT_NEG" > $ENDPOINT_STAT_FILE
435}
436
437# Function to collect stats on endpoints
438# args: <app-id> <image-info>
439__collect_endpoint_stats_image_info() {
440 if [ $COLLECT_ENDPOINT_STATS -eq 0 ]; then
441 return
442 fi
443 ENDPOINT_STAT_FILE=$TESTLOGS/$ATC/imageinfo_$ATC_$1".log"
444 echo $POLICY_AGENT_IMAGE > $ENDPOINT_STAT_FILE
445}
446
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100447#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100448TCTEST_START=$SECONDS
449
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100450#Vars to hold the start time and timer text for a custom timer
451TC_TIMER_STARTTIME=""
452TC_TIMER_TIMER_TEXT=""
453TC_TIMER_CURRENT_FAILS="" # Then numer of failed test when timer starts.
454 # Compared with the current number of fails at timer stop
455 # to judge the measurement reliability
456
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200457#File to save timer measurement results
458TIMER_MEASUREMENTS=".timer_measurement.txt"
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100459echo -e "Activity \t Duration \t Info" > $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200460
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100461# If this is set, some images (controlled by the parameter repo-policy) will be re-tagged and pushed to this repo before any
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200462IMAGE_REPO_ADR=""
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200463IMAGE_REPO_POLICY="local"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200464CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200465
YongchaoWu9a84f512019-12-16 22:54:11 +0100466echo "-------------------------------------------------------------------------------------------------"
467echo "----------------------------------- Test case: "$ATC
468echo "----------------------------------- Started: "$(date)
469echo "-------------------------------------------------------------------------------------------------"
470echo "-- Description: "$TC_ONELINE_DESCR
471echo "-------------------------------------------------------------------------------------------------"
472echo "----------------------------------- Test case setup -----------------------------------"
473
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100474echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200475START_ARG=$1
476paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100477paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200478if [ $# -lt 1 ]; then
479 paramerror=1
480fi
481if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100482 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200483 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100484 if [ -z "$paramerror_str" ]; then
485 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
486 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200487 else
488 shift;
489 fi
490fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100491if [ $paramerror -eq 0 ]; then
492 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
493 paramerror=1
494 if [ -z "$paramerror_str" ]; then
495 paramerror_str="Second arg shall be 'docker' or 'kube'"
496 fi
497 else
498 if [ $1 == "docker" ]; then
499 RUNMODE="DOCKER"
500 echo "Setting RUNMODE=DOCKER"
501 fi
502 if [ $1 == "kube" ]; then
503 RUNMODE="KUBE"
504 echo "Setting RUNMODE=KUBE"
505 fi
506 shift;
507 fi
508fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200509foundparm=0
510while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
511 foundparm=1
512 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100513 if [ "$1" == "release" ]; then
514 IMAGE_CATEGORY="RELEASE"
515 echo "Option set - Release image tags used for applicable images "
516 shift;
517 foundparm=0
518 fi
519 fi
520 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200521 if [ "$1" == "auto-clean" ]; then
522 AUTO_CLEAN="auto"
523 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200524 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200525 foundparm=0
526 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200527 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200528 if [ $paramerror -eq 0 ]; then
529 if [ "$1" == "--stop-at-error" ]; then
530 STOP_AT_ERROR=1
531 echo "Option set - Stop at first error"
532 shift;
533 foundparm=0
534 fi
535 fi
536 if [ $paramerror -eq 0 ]; then
537 if [ "$1" == "--ricsim-prefix" ]; then
538 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100539 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200540 if [ -z "$1" ]; then
541 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100542 if [ -z "$paramerror_str" ]; then
543 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
544 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200545 else
546 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
547 shift;
548 foundparm=0
549 fi
550 fi
551 fi
552 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200553 if [ "$1" == "--env-file" ]; then
554 shift;
555 TEST_ENV_VAR_FILE=$1
556 if [ -z "$1" ]; then
557 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100558 if [ -z "$paramerror_str" ]; then
559 paramerror_str="No env file found for flag: '--env-file'"
560 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200561 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200562 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200563 shift;
564 foundparm=0
565 fi
566 fi
567 fi
568 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200569 if [ "$1" == "--use-local-image" ]; then
570 USE_LOCAL_IMAGES=""
571 shift
572 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
573 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100574 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200575 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100576 if [ -z "$paramerror_str" ]; then
577 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
578 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200579 fi
580 shift;
581 done
582 foundparm=0
583 if [ -z "$USE_LOCAL_IMAGES" ]; then
584 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100585 if [ -z "$paramerror_str" ]; then
586 paramerror_str="No app name found for flag: '--use-local-image'"
587 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200588 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100589 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
590 fi
591 fi
592 fi
593 if [ $paramerror -eq 0 ]; then
594 if [ "$1" == "--use-snapshot-image" ]; then
595 USE_SNAPSHOT_IMAGES=""
596 shift
597 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
598 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
599 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
600 paramerror=1
601 if [ -z "$paramerror_str" ]; then
602 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
603 fi
604 fi
605 shift;
606 done
607 foundparm=0
608 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
609 paramerror=1
610 if [ -z "$paramerror_str" ]; then
611 paramerror_str="No app name found for flag: '--use-snapshot-image'"
612 fi
613 else
614 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
615 fi
616 fi
617 fi
618 if [ $paramerror -eq 0 ]; then
619 if [ "$1" == "--use-staging-image" ]; then
620 USE_STAGING_IMAGES=""
621 shift
622 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
623 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
624 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
625 paramerror=1
626 if [ -z "$paramerror_str" ]; then
627 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
628 fi
629 fi
630 shift;
631 done
632 foundparm=0
633 if [ -z "$USE_STAGING_IMAGES" ]; then
634 paramerror=1
635 if [ -z "$paramerror_str" ]; then
636 paramerror_str="No app name found for flag: '--use-staging-image'"
637 fi
638 else
639 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
640 fi
641 fi
642 fi
643 if [ $paramerror -eq 0 ]; then
644 if [ "$1" == "--use-release-image" ]; then
645 USE_RELEASE_IMAGES=""
646 shift
647 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
648 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
649 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
650 paramerror=1
651 if [ -z "$paramerror_str" ]; then
652 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
653 fi
654 fi
655 shift;
656 done
657 foundparm=0
658 if [ -z "$USE_RELEASE_IMAGES" ]; then
659 paramerror=1
660 if [ -z "$paramerror_str" ]; then
661 paramerror_str="No app name found for flag: '--use-release-image'"
662 fi
663 else
664 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200665 fi
666 fi
667 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200668 if [ $paramerror -eq 0 ]; then
669 if [ "$1" == "--image-repo" ]; then
670 shift;
671 IMAGE_REPO_ADR=$1
672 if [ -z "$1" ]; then
673 paramerror=1
674 if [ -z "$paramerror_str" ]; then
675 paramerror_str="No image repo url found for : '--image-repo'"
676 fi
677 else
678 echo "Option set - Image repo url: "$1
679 shift;
680 foundparm=0
681 fi
682 fi
683 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200684 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200685 if [ "$1" == "--repo-policy" ]; then
686 shift;
687 IMAGE_REPO_POLICY=$1
688 if [ -z "$1" ]; then
689 paramerror=1
690 if [ -z "$paramerror_str" ]; then
691 paramerror_str="No policy found for : '--repo-policy'"
692 fi
693 else
694 if [ "$1" == "local" ] || [ "$1" == "remote" ]; then
695 echo "Option set - Image repo policy: "$1
696 shift;
697 foundparm=0
698 else
699 paramerror=1
700 if [ -z "$paramerror_str" ]; then
701 paramerror_str="Repo policy shall be 'local' or 'remote'"
702 fi
703 fi
704 fi
705 fi
706 fi
707 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200708 if [ "$1" == "--cluster-timeout" ]; then
709 shift;
710 CLUSTER_TIME_OUT=$1
711 if [ -z "$1" ]; then
712 paramerror=1
713 if [ -z "$paramerror_str" ]; then
714 paramerror_str="No timeout value found for : '--cluster-timeout'"
715 fi
716 else
717 #Check if positive int
718 case ${CLUSTER_TIME_OUT#[+]} in
719 *[!0-9]* | '')
720 paramerror=1
721 if [ -z "$paramerror_str" ]; then
722 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
723 fi
724 ;;
725 * ) ;; # Ok
726 esac
727 echo "Option set - Cluster timeout: "$1
728 shift;
729 foundparm=0
730 fi
731 fi
732 fi
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200733 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100734 if [ "$1" == "--override" ]; then
735 shift;
736 TEST_ENV_VAR_FILE_OVERRIDE=$1
737 if [ -z "$1" ]; then
738 paramerror=1
739 if [ -z "$paramerror_str" ]; then
740 paramerror_str="No env file found for flag: '--override'"
741 fi
742 else
743 if [ ! -f $TEST_ENV_VAR_FILE_OVERRIDE ]; then
744 paramerror=1
745 if [ -z "$paramerror_str" ]; then
746 paramerror_str="File for '--override' does not exist : "$TEST_ENV_VAR_FILE_OVERRIDE
747 fi
748 fi
749 echo "Option set - Override env from: "$1
750 shift;
751 foundparm=0
752 fi
753 fi
754 fi
755 if [ $paramerror -eq 0 ]; then
756 if [ "$1" == "--pre-clean" ]; then
757 PRE_CLEAN=1
758 echo "Option set - Pre-clean of kube/docker resouces"
759 shift;
760 foundparm=0
761 fi
762 fi
763 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200764 if [ "$1" == "--print-stats" ]; then
765 PRINT_CURRENT_STATS=1
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100766 echo "Option set - Print stats after every test-case and config"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200767 shift;
768 foundparm=0
769 fi
770 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100771 if [ $paramerror -eq 0 ]; then
772 if [ "$1" == "--gen-stats" ]; then
773 COLLECT_RUNTIME_STATS=1
774 echo "Option set - Collect runtime statistics"
775 shift;
776 foundparm=0
777 fi
778 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100779 if [ $paramerror -eq 0 ]; then
780 if [ "$1" == "--delete-namespaces" ]; then
781 if [ $RUNMODE == "DOCKER" ]; then
782 DELETE_KUBE_NAMESPACES=0
783 echo "Option ignored - Delete namespaces (ignored when running docker)"
784 else
785 if [ -z "KUBE_PRESTARTED_IMAGES" ]; then
786 DELETE_KUBE_NAMESPACES=0
787 echo "Option ignored - Delete namespaces (ignored when using prestarted apps)"
788 else
789 DELETE_KUBE_NAMESPACES=1
790 echo "Option set - Delete namespaces"
791 fi
792 fi
793 shift;
794 foundparm=0
795 fi
796 fi
797 if [ $paramerror -eq 0 ]; then
798 if [ "$1" == "--delete-containers" ]; then
799 if [ $RUNMODE == "DOCKER" ]; then
800 DELETE_CONTAINERS=1
801 echo "Option set - Delete containers started by previous test(s)"
802 else
803 echo "Option ignored - Delete containers (ignored when running kube)"
804 fi
805 shift;
806 foundparm=0
807 fi
808 fi
809 if [ $paramerror -eq 0 ]; then
810 if [ "$1" == "--endpoint-stats" ]; then
811 COLLECT_ENDPOINT_STATS=1
812 echo "Option set - Collect endpoint statistics"
813 shift;
814 foundparm=0
815 fi
816 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +0100817 if [ $paramerror -eq 0 ]; then
818 if [ "$1" == "--kubeconfig" ]; then
819 shift;
820 if [ -z "$1" ]; then
821 paramerror=1
822 if [ -z "$paramerror_str" ]; then
823 paramerror_str="No path found for : '--kubeconfig'"
824 fi
825 else
826 if [ -f $1 ]; then
827 KUBECONF="--kubeconfig $1"
828 echo "Option set - Kubeconfig path: "$1
829 shift;
830 foundparm=0
831 else
832 paramerror=1
833 if [ -z "$paramerror_str" ]; then
834 paramerror_str="File $1 for --kubeconfig not found"
835 fi
836 fi
837 fi
838 fi
839 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200840done
841echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200842
BjornMagnussonXAad047782020-06-08 15:54:11 +0200843#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200844if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
845 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100846 if [ -z "$paramerror_str" ]; then
847 paramerror_str="Unknown parameter(s): "$@
848 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200849fi
850
851if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100852 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
853 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200854 exit 1
855fi
856
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200857# sourcing the selected env variables for the test case
858if [ -f "$TEST_ENV_VAR_FILE" ]; then
859 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
860 . $TEST_ENV_VAR_FILE
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100861 if [ ! -z "$TEST_ENV_VAR_FILE_OVERRIDE" ]; then
862 echo -e $BOLD"Sourcing override env vars from: "$TEST_ENV_VAR_FILE_OVERRIDE$EBOLD
863 . $TEST_ENV_VAR_FILE_OVERRIDE
864 fi
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100865
866 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100867 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 +0100868 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100869 found_profile=0
870 for prof in $SUPPORTED_PROFILES; do
871 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
872 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
873 found_profile=1
874 fi
875 done
876 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100877 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100878 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100879 echo -e $RED"Exiting...."$ERED
880 exit 1
881 fi
882 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200883else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200884 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200885 echo " Select one of following env var file matching the intended target of the test"
886 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100887 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200888 exit 1
889fi
890
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100891#This var need be preserved from the command line option, if set, when env var is sourced.
892if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
893 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
894fi
895
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100896if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100897 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100898 exit 1
899fi
900
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100901if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
902 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
903 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
904 exit 1
905fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100906
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100907# Choose list of included apps depending on run-mode
908if [ $RUNMODE == "KUBE" ]; then
909 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
910else
911 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
912fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200913
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100914echo ""
915# auto adding system apps
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100916__added_apps=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100917echo -e $BOLD"Auto adding system apps"$EBOLD
918if [ $RUNMODE == "KUBE" ]; then
919 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_KUBE_SYSTEM_APPS
920 TMP_APPS=$TESTENV_KUBE_SYSTEM_APPS
921else
922 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_DOCKER_SYSTEM_APPS
923 TMP_APPS=$TESTENV_DOCKER_SYSTEM_APPS
924fi
925if [ ! -z "$TMP_APPS" ]; then
926 for iapp in "$TMP_APPS"; do
927 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
928 file_pointer="../common/"$file_pointer"_api_functions.sh"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100929 padded_iapp=$iapp
930 while [ ${#padded_iapp} -lt 16 ]; do
931 padded_iapp=$padded_iapp" "
932 done
933 echo " Auto-adding system app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100934 . $file_pointer
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100935 if [ $? -ne 0 ]; then
936 echo " Include file $file_pointer contain errors. Exiting..."
937 exit 1
938 fi
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100939 __added_apps=" $iapp "$__added_apps
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100940 done
941else
942 echo " None"
943fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100944
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100945if [ $RUNMODE == "KUBE" ]; then
946 TMP_APPS=$INCLUDED_IMAGES" "$KUBE_PRESTARTED_IMAGES
947else
948 TMP_APPS=$INCLUDED_IMAGES
949fi
950
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100951echo -e $BOLD"Auto adding included apps"$EBOLD
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100952 for iapp in $TMP_APPS; do
953 if [[ "$__added_apps" != *"$iapp"* ]]; then
954 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
955 file_pointer="../common/"$file_pointer"_api_functions.sh"
956 padded_iapp=$iapp
957 while [ ${#padded_iapp} -lt 16 ]; do
958 padded_iapp=$padded_iapp" "
959 done
960 echo " Auto-adding included app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100961 if [ ! -f "$file_pointer" ]; then
962 echo " Include file $file_pointer for app $iapp does not exist"
963 exit 1
964 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100965 . $file_pointer
966 if [ $? -ne 0 ]; then
967 echo " Include file $file_pointer contain errors. Exiting..."
968 exit 1
969 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100970 fi
971 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100972echo ""
973
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100974echo -e $BOLD"Test environment info"$EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100975
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100976# Check needed installed sw
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100977
978tmp=$(which bash)
979if [ $? -ne 0 ] || [ -z "$tmp" ]; then
980 echo -e $RED"bash is required to run the test environment, pls install"$ERED
981 exit 1
982fi
983echo " bash is installed and using version:"
984echo "$(bash --version)" | indent2
985
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100986tmp=$(which python3)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100987if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100988 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
989 exit 1
990fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100991echo " python3 is installed and using version: $(python3 --version)"
992
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100993tmp=$(which docker)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100994if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100995 echo -e $RED"docker is required to run the test environment, pls install"$ERED
996 exit 1
997fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100998echo " docker is installed and using versions:"
999echo " $(docker version --format 'Client version {{.Client.Version}} Server version {{.Server.Version}}')"
BjornMagnussonXA575869c2020-09-14 21:28:54 +02001000
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001001tmp=$(which docker-compose)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001002if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001003 if [ $RUNMODE == "DOCKER" ]; then
1004 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
1005 exit 1
1006 fi
1007fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001008tmp=$(docker-compose version --short)
1009echo " docker-compose installed and using version $tmp"
1010if [[ "$tmp" == *'v2'* ]]; then
1011 DOCKER_COMPOSE_VERION="V2"
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001012fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +02001013
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001014tmp=$(which kubectl)
1015if [ $? -ne 0 ] || [ -z tmp ]; then
1016 if [ $RUNMODE == "KUBE" ]; then
1017 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
1018 exit 1
1019 fi
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001020else
1021 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001022 echo " kubectl is installed and using versions:"
1023 echo $(kubectl version --short=true) | indent2
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001024 res=$(kubectl $KUBECONF cluster-info 2>&1)
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001025 if [ $? -ne 0 ]; then
1026 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001027 echo -e $BOLD$RED"Command 'kubectl '$KUBECONF' cluster-info' returned error $ERED$EBOLD"
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001028 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
1029 echo " "
1030 echo "kubectl response:"
1031 echo $res
1032 echo " "
1033 echo "This script may have been started with user with no permission to run kubectl"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001034 echo "Try running with 'sudo', set env KUBECONFIG or set '--kubeconfig' parameter"
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001035 echo "Do either 1, 2 or 3 "
1036 echo " "
1037 echo "1"
1038 echo "Run with sudo"
1039 echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD
1040 echo " "
1041 echo "2"
1042 echo "Export KUBECONFIG and pass env to sudo - (replace user)"
1043 echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD
1044 echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD
1045 echo " "
1046 echo "3"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001047 echo "Set KUBECONFIG via script parameter"
1048 echo -e $BOLD"sudo ... --kubeconfig /home/<user>/.kube/<config-file> ...."$EBOLD
1049 echo "The config file need to downloaded from the cluster"
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001050
1051 exit 1
1052 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001053 echo " Node(s) and container runtime config"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001054 kubectl $KUBECONF get nodes -o wide | indent2
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001055 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001056fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +01001057
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001058echo ""
1059
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001060echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +01001061
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001062#Temp var to check for image variable name errors
1063IMAGE_ERR=0
1064#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001065image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001066echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001067
1068# 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 +01001069# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001070__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001071
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001072 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001073 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 +01001074 ((IMAGE_ERR++))
1075 return
1076 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001077
1078 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001079 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001080 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001081 # Image is excluded since the corresponding app is not used in this test
1082 return
1083 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001084 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001085 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001086 image="${!3}"
1087 tmptag=$4"_"$5
1088 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001089
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001090 optional_image_repo_target=""
1091
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001092 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001093 __check_ignore_image $1
1094 if [ $? -eq 0 ]; then
1095 app_ds=$6
1096 if [ -z "$6" ]; then
1097 app_ds="<app ignored>"
1098 fi
1099 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
1100 # Image is ignored since the corresponding the images is not set in the env file
1101 __remove_included_image $1 # Remove the image from the list of included images
1102 return
1103 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001104 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
1105 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001106 echo ""
1107 tmp=$tmp"<no-image>\t"
1108 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001109
1110 optional_image_repo_target=$image
1111
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001112 #Add repo depending on image type
1113 if [ "$5" == "REMOTE_RELEASE" ]; then
1114 image=$NEXUS_RELEASE_REPO$image
1115 fi
1116 if [ "$5" == "REMOTE" ]; then
1117 image=$NEXUS_STAGING_REPO$image
1118 fi
1119 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
1120 image=$NEXUS_SNAPSHOT_REPO$image
1121 fi
1122 if [ "$5" == "REMOTE_PROXY" ]; then
1123 image=$NEXUS_PROXY_REPO$image
1124 fi
1125 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
1126 image=$NEXUS_RELEASE_REPO_ONAP$image
1127 fi
1128 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
1129 image=$NEXUS_RELEASE_REPO_ORAN$image
1130 fi
1131 #No nexus repo added for local images, tag: LOCAL
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001132 tmp=$tmp$image"\t"
1133 fi
1134 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001135 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
1136 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001137 echo ""
1138 tmp=$tmp"<no-tag>\t"
1139 else
1140 tmp=$tmp$tag
1141 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001142 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001143 echo -e "$tmp" >> $image_list_file
1144 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001145 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 +02001146
1147 remote_or_local_push=false
1148 if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
1149 if [ $5 == "LOCAL" ]; then
1150 remote_or_local_push=true
1151 fi
1152 if [[ $5 == *"REMOTE"* ]]; then
1153 if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
1154 remote_or_local_push=true
1155 fi
1156 fi
1157 fi
1158 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 +02001159 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001160 if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
1161 optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
1162 optional_image_repo_target=$optional_image_repo_target_tmp
1163 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001164 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
1165 else
1166 export "${2}_SOURCE"=""
1167 export "${2}_TARGET"=""
1168 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001169}
1170
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001171# Check if app uses image included in this test run
1172# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001173__check_included_image() {
1174 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001175 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001176 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001177 fi
1178 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001179 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001180}
1181
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001182# Check if app uses a project image
1183# Returns 0 if image is included, 1 if not
1184__check_project_image() {
1185 for im in $PROJECT_IMAGES; do
1186 if [ "$1" == "$im" ]; then
1187 return 0
1188 fi
1189 done
1190 return 1
1191}
1192
1193# Check if app uses image built by the test script
1194# Returns 0 if image is included, 1 if not
1195__check_image_local_build() {
1196 for im in $LOCAL_IMAGE_BUILD; do
1197 if [ "$1" == "$im" ]; then
1198 return 0
1199 fi
1200 done
1201 return 1
1202}
1203
1204# Check if app image is conditionally ignored in this test run
1205# Returns 0 if image is conditionally ignored, 1 if not
1206__check_ignore_image() {
1207 for im in $CONDITIONALLY_IGNORED_IMAGES; do
1208 if [ "$1" == "$im" ]; then
1209 return 0
1210 fi
1211 done
1212 return 1
1213}
1214
1215# Removed image from included list of included images
1216# Used when an image is marked as conditionally ignored
1217__remove_included_image() {
1218 tmp_img_rem_list=""
1219 for im in $INCLUDED_IMAGES; do
1220 if [ "$1" != "$im" ]; then
1221 tmp_img_rem_list=$tmp_img_rem_list" "$im
1222 fi
1223 done
1224 INCLUDED_IMAGES=$tmp_img_rem_list
1225 return 0
1226}
1227
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001228# Check if app is included in the prestarted set of apps
1229# Returns 0 if image is included, 1 if not
1230__check_prestarted_image() {
1231 for im in $KUBE_PRESTARTED_IMAGES; do
1232 if [ "$1" == "$im" ]; then
1233 return 0
1234 fi
1235 done
1236 return 1
1237}
1238
1239# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001240__check_image_local_override() {
1241 for im in $USE_LOCAL_IMAGES; do
1242 if [ "$1" == "$im" ]; then
1243 return 1
1244 fi
1245 done
1246 return 0
1247}
1248
1249# Check if app uses image override
1250# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
1251__check_image_override() {
1252
1253 for im in $ORAN_IMAGES_APP_NAMES; do
1254 if [ "$1" == "$im" ]; then
1255 echo "REMOTE_RELEASE_ORAN"
1256 return 0
1257 fi
1258 done
1259
1260 for im in $ONAP_IMAGES_APP_NAMES; do
1261 if [ "$1" == "$im" ]; then
1262 echo "REMOTE_RELEASE_ONAP"
1263 return 0
1264 fi
1265 done
1266
1267 found=0
1268 for im in $PROJECT_IMAGES_APP_NAMES; do
1269 if [ "$1" == "$im" ]; then
1270 found=1
1271 fi
1272 done
1273
1274 if [ $found -eq 0 ]; then
1275 echo "REMOTE_PROXY"
1276 return 0
1277 fi
1278
1279 suffix=""
1280 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
1281 suffix="REMOTE_RELEASE"
1282 fi
1283 if [ $IMAGE_CATEGORY == "DEV" ]; then
1284 suffix="REMOTE"
1285 fi
1286 CTR=0
1287 for im in $USE_STAGING_IMAGES; do
1288 if [ "$1" == "$im" ]; then
1289 suffix="REMOTE"
1290 ((CTR++))
1291 fi
1292 done
1293 for im in $USE_RELEASE_IMAGES; do
1294 if [ "$1" == "$im" ]; then
1295 suffix="REMOTE_RELEASE"
1296 ((CTR++))
1297 fi
1298 done
1299 for im in $USE_SNAPSHOT_IMAGES; do
1300 if [ "$1" == "$im" ]; then
1301 suffix="REMOTE_SNAPSHOT"
1302 ((CTR++))
1303 fi
1304 done
1305 for im in $USE_LOCAL_IMAGES; do
1306 if [ "$1" == "$im" ]; then
1307 suffix="LOCAL"
1308 ((CTR++))
1309 fi
1310 done
1311 echo $suffix
1312 if [ $CTR -gt 1 ]; then
1313 exit 1
1314 fi
1315 return 0
1316}
1317
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001318# Function to re-tag and image and push to another image repo
1319__retag_and_push_image() {
1320 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1321 source_image="${!1}"
1322 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
1323 target_image="${!trg_var_name}"
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001324
1325 if [ -z $target_image ]; then
1326 return 0 # Image with no target shall not be pushed
1327 fi
1328
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001329 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1330 tmp=$(docker image tag $source_image ${target_image} )
1331 if [ $? -ne 0 ]; then
1332 docker stop $tmp &> ./tmp/.dockererr
1333 ((IMAGE_ERR++))
1334 echo ""
1335 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1336 cat ./tmp/.dockererr
1337 return 1
1338 else
1339 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1340 fi
1341 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1342 tmp=$(docker push ${target_image} )
1343 if [ $? -ne 0 ]; then
1344 docker stop $tmp &> ./tmp/.dockererr
1345 ((IMAGE_ERR++))
1346 echo ""
1347 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1348 cat ./tmp/.dockererr
1349 return 1
1350 else
1351 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1352 fi
1353 export "${1}"=$target_image
1354 fi
1355 return 0
1356}
1357
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001358#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001359#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001360__check_and_pull_image() {
1361
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001362 source_image="${!4}"
1363
1364 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 +01001365 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001366 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001367
1368 if [ $1 == "local" ]; then
1369 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001370 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 +01001371 ((IMAGE_ERR++))
1372 return 1
1373 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001374 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001375 fi
1376 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
1377 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001378 if [ $RUNMODE == "DOCKER" ]; then
1379 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001380 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001381 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1382 docker stop $tmp &> ./tmp/.dockererr
1383 if [ $? -ne 0 ]; then
1384 ((IMAGE_ERR++))
1385 echo ""
1386 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
1387 cat ./tmp/.dockererr
1388 return 1
1389 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001390 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001391 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001392 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001393 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1394 docker rm $tmp &> ./tmp/.dockererr
1395 if [ $? -ne 0 ]; then
1396 ((IMAGE_ERR++))
1397 echo ""
1398 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
1399 cat ./tmp/.dockererr
1400 return 1
1401 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001402 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001403 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
1404 tmp_im=""
1405 else
1406 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001407 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001408 fi
1409 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001410 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001411 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001412 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001413 echo ""
1414 echo -e " Pulling image -$RED could not be pulled"$ERED
1415 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001416 echo $out > ./tmp/.dockererr
1417 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001418 return 1
1419 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001420 echo $out > ./tmp/.dockererr
1421 if [[ $out == *"up to date"* ]]; then
1422 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
1423 elif [[ $out == *"Downloaded newer image"* ]]; then
1424 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
1425 else
1426 echo -e " Pulling image -$GREEN Pulled $EGREEN"
1427 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001428 else
1429 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1430 fi
1431 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001432
1433 __retag_and_push_image $4
1434
1435 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001436}
1437
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001438setup_testenvironment() {
1439 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001440 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001441
1442 # Image var setup for all project images included in the test
1443 for imagename in $APP_SHORT_NAMES; do
1444 __check_included_image $imagename
1445 incl=$?
1446 __check_project_image $imagename
1447 proj=$?
1448 if [ $incl -eq 0 ]; then
1449 if [ $proj -eq 0 ]; then
1450 IMAGE_SUFFIX=$(__check_image_override $imagename)
1451 if [ $? -ne 0 ]; then
1452 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1453 ((IMAGE_ERR++))
1454 fi
1455 else
1456 IMAGE_SUFFIX="none"
1457 fi
1458 # A function name is created from the app short name
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001459 # for example app short name 'ICS' -> produce the function
1460 # name __ICS_imagesetup
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001461 # This function is called and is expected to exist in the imported
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001462 # file for the ics test functions
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001463 # The resulting function impl will call '__check_and_create_image_var' function
1464 # with appropriate parameters
1465 # If the image suffix is none, then the component decides the suffix
1466 function_pointer="__"$imagename"_imagesetup"
1467 $function_pointer $IMAGE_SUFFIX
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01001468
1469 function_pointer="__"$imagename"_test_requirements"
1470 $function_pointer
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001471 fi
1472 done
1473
1474 #Errors in image setting - exit
1475 if [ $IMAGE_ERR -ne 0 ]; then
1476 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001477 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001478
1479 #Print a tables of the image settings
1480 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1481 column -t -s $'\t' $image_list_file | indent1
1482
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001483 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001484
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001485 #Set the SIM_GROUP var
1486 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1487 if [ -z "$SIM_GROUP" ]; then
1488 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1489 if [ ! -d $SIM_GROUP ]; then
1490 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1491 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1492 exit 1
1493 else
1494 echo " SIM_GROUP auto set to: " $SIM_GROUP
1495 fi
1496 elif [ $SIM_GROUP = *simulator_group ]; then
1497 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
1498 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001499 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001500 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1501 fi
1502
1503 echo ""
1504
1505 #Temp var to check for image pull errors
1506 IMAGE_ERR=0
1507
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001508 # Delete namespaces
1509 echo -e $BOLD"Deleting namespaces"$EBOLD
1510
1511
1512 if [ "$DELETE_KUBE_NAMESPACES" -eq 1 ]; then
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001513 test_env_namespaces=$(kubectl $KUBECONF get ns --no-headers -o custom-columns=":metadata.name" -l autotest=engine) #Get list of ns created by the test env
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001514 if [ $? -ne 0 ]; then
1515 echo " Cannot get list of namespaces...ignoring delete"
1516 else
1517 for test_env_ns in $test_env_namespaces; do
1518 __kube_delete_namespace $test_env_ns
1519 done
1520 fi
1521 else
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001522 echo " Namespace delete option not set or ignored"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001523 fi
1524 echo ""
1525
1526 # Delete containers
1527 echo -e $BOLD"Deleting containers"$EBOLD
1528
1529 if [ "$DELETE_CONTAINERS" -eq 1 ]; then
1530 echo " Stopping containers label 'nrttest_app'..."
1531 docker stop $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
1532 echo " Removing stopped containers..."
1533 docker rm $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
1534 else
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001535 echo " Contatiner delete option not set or ignored"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001536 fi
1537 echo ""
1538
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001539 # The following sequence pull the configured images
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001540 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001541 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
1542 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 +02001543 else
1544 for imagename in $APP_SHORT_NAMES; do
1545 __check_included_image $imagename
1546 incl=$?
1547 __check_project_image $imagename
1548 proj=$?
1549 if [ $incl -eq 0 ]; then
1550 if [ $proj -eq 0 ]; then
1551 START_ARG_MOD=$START_ARG
1552 __check_image_local_override $imagename
1553 if [ $? -eq 1 ]; then
1554 START_ARG_MOD="local"
1555 fi
1556 else
1557 START_ARG_MOD=$START_ARG
1558 fi
1559 __check_image_local_build $imagename
1560 #No pull of images built locally
1561 if [ $? -ne 0 ]; then
1562 # A function name is created from the app short name
1563 # for example app short name 'HTTPPROXY' -> produce the function
1564 # name __HTTPPROXY_imagesetup
1565 # This function is called and is expected to exist in the imported
1566 # file for the httpproxy test functions
1567 # The resulting function impl will call '__check_and_pull_image' function
1568 # with appropriate parameters
1569 function_pointer="__"$imagename"_imagepull"
1570 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001571 fi
1572 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001573 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001574 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001575 done
1576 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001577
1578 #Errors in image setting - exit
1579 if [ $IMAGE_ERR -ne 0 ]; then
1580 echo ""
1581 echo "#################################################################################################"
1582 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1583 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1584 if [ $IMAGE_CATEGORY == "DEV" ]; then
BjornMagnussonXA720f5d72021-08-13 10:37:23 +02001585 echo ""
1586 echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED
1587 echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED
1588 echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED
1589 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 +01001590 fi
1591 echo "#################################################################################################"
1592 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001593 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001594 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001595
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001596 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001597
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001598 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001599
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001600 for imagename in $APP_SHORT_NAMES; do
1601 cd $AUTOTEST_HOME #Always reset to orig dir
1602 __check_image_local_build $imagename
1603 if [ $? -eq 0 ]; then
1604 __check_included_image $imagename
1605 if [ $? -eq 0 ]; then
1606 # A function name is created from the app short name
1607 # for example app short name 'MR' -> produce the function
1608 # name __MR_imagebuild
1609 # This function is called and is expected to exist in the imported
1610 # file for the mr test functions
1611 # The resulting function impl shall build the imagee
1612 function_pointer="__"$imagename"_imagebuild"
1613 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001614
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001615 else
1616 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1617 fi
1618 fi
1619 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001620
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001621 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001622
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001623 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001624
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001625 # Create a table of the images used in the script - from local repo
1626 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001627
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001628 docker_tmp_file=./tmp/.docker-images-table
1629 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1630 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001631
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001632 for imagename in $APP_SHORT_NAMES; do
1633 __check_included_image $imagename
1634 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001635 # Only print image data if image repo is null, or if image repo is set and image is local
1636 print_image_data=0
1637 if [ -z "$IMAGE_REPO_ADR" ]; then
1638 print_image_data=1
1639 else
1640 __check_image_local_build $imagename
1641 if [ $? -eq 0 ]; then
1642 print_image_data=1
1643 fi
1644 fi
1645 if [ $print_image_data -eq 1 ]; then
1646 # A function name is created from the app short name
1647 # for example app short name 'MR' -> produce the function
1648 # name __MR_imagebuild
1649 # This function is called and is expected to exist in the imported
1650 # file for the mr test functions
1651 # The resulting function impl shall build the imagee
1652 function_pointer="__"$imagename"_image_data"
1653 $function_pointer "$format_string" $docker_tmp_file
1654 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001655 fi
1656 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001657
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001658 column -t -s $'\t' $docker_tmp_file | indent1
1659
1660 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001661
1662 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1663
1664 # Create a table of the images used in the script - from remote repo
1665 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1666 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1667
1668 docker_tmp_file=./tmp/.docker-images-table
1669 format_string="{{.Repository}}\\t{{.Tag}}"
1670 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1671
1672 for imagename in $APP_SHORT_NAMES; do
1673 __check_included_image $imagename
1674 if [ $? -eq 0 ]; then
1675 # Only print image data if image repo is null, or if image repo is set and image is local
1676 __check_image_local_build $imagename
1677 if [ $? -ne 0 ]; then
1678 # A function name is created from the app short name
1679 # for example app short name 'MR' -> produce the function
1680 # name __MR_imagebuild
1681 # This function is called and is expected to exist in the imported
1682 # file for the mr test functions
1683 # The resulting function impl shall build the imagee
1684 function_pointer="__"$imagename"_image_data"
1685 $function_pointer "$format_string" $docker_tmp_file
1686 fi
1687 fi
1688 done
1689
1690 column -t -s $'\t' $docker_tmp_file | indent1
1691
1692 echo ""
1693 fi
1694
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001695 if [ $RUNMODE == "KUBE" ]; then
1696
1697 echo "================================================================================="
1698 echo "================================================================================="
1699
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001700 if [ -z "$IMAGE_REPO_ADR" ]; then
1701 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1702 echo -e " This setting only works on single node clusters on the local machine"
1703 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001704 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001705 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001706 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1707 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1708 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1709 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"
1710 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001711 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001712 CLUSTER_IP=$(kubectl $KUBECONF config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001713 echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001714
1715 echo "================================================================================="
1716 echo "================================================================================="
1717 echo ""
1718 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001719
1720 echo -e $BOLD"======================================================="$EBOLD
1721 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1722 echo -e $BOLD"======================================================="$EBOLD
1723 echo ""
1724
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001725 LOG_STAT_ARGS=""
1726
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001727 for imagename in $APP_SHORT_NAMES; do
1728 __check_included_image $imagename
1729 retcode_i=$?
1730 __check_prestarted_image $imagename
1731 retcode_p=$?
1732 if [ $retcode_i -eq 0 ] || [ $retcode_p -eq 0 ]; then
1733 # A function name is created from the app short name
1734 # for example app short name 'RICMSIM' -> produce the function
1735 # name __RICSIM__initial_setup
1736 # This function is called and is expected to exist in the imported
1737 # file for the ricsim test functions
1738 # The resulting function impl shall perform initial setup of port, host etc
1739
1740 function_pointer="__"$imagename"_initial_setup"
1741 $function_pointer
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001742
1743 function_pointer="__"$imagename"_statisics_setup"
1744 LOG_STAT_ARGS=$LOG_STAT_ARGS" "$($function_pointer)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001745 fi
1746 done
1747
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001748 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1749 ../common/genstat.sh $RUNMODE $SECONDS $TESTLOGS/$ATC/stat_data.csv $LOG_STAT_ARGS &
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +01001750 COLLECT_RUNTIME_STATS_PID=$!
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001751 fi
1752
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001753}
YongchaoWu9a84f512019-12-16 22:54:11 +01001754
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001755# Function to print the test result, shall be the last cmd in a test script
1756# args: -
1757# (Function for test scripts)
1758print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001759
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001760 TCTEST_END=$SECONDS
1761 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001762
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001763 echo "-------------------------------------------------------------------------------------------------"
1764 echo "------------------------------------- Test case: "$ATC
1765 echo "------------------------------------- Ended: "$(date)
1766 echo "-------------------------------------------------------------------------------------------------"
1767 echo "-- Description: "$TC_ONELINE_DESCR
1768 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001769 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001770 echo "-------------------------------------------------------------------------------------------------"
1771 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001772 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001773
YongchaoWu21f17bb2020-03-05 12:44:08 +01001774
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001775 if [ $RES_DEVIATION -gt 0 ]; then
1776 echo "Test case deviations"
1777 echo "===================================="
1778 cat $DEVIATION_FILE
1779 fi
1780 echo ""
1781 echo "Timer measurement in the test script"
1782 echo "===================================="
1783 column -t -s $'\t' $TIMER_MEASUREMENTS
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001784 if [ $RES_PASS != $RES_TEST ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001785 echo -e $RED"Measurement may not be reliable when there are failed test - failures may cause long measurement values due to timeouts etc."$ERED
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001786 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001787 echo ""
1788
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001789 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1790 echo "Runtime statistics collected in file: "$TESTLOGS/$ATC/stat_data.csv
1791 echo ""
1792 fi
1793
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001794 total=$((RES_PASS+RES_FAIL))
1795 if [ $RES_TEST -eq 0 ]; then
1796 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1797 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1798 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1799 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1800 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1801 elif [ $total != $RES_TEST ]; then
1802 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1803 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1804 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1805 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1806 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1807 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001808 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001809 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1810 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1811 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1812 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1813 elif [ $RES_PASS = $RES_TEST ]; then
1814 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1815 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1816 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1817 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1818 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1819 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001820
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001821 # Update test suite counter
1822 if [ -f .tmp_tcsuite_pass_ctr ]; then
1823 tmpval=$(< .tmp_tcsuite_pass_ctr)
1824 ((tmpval++))
1825 echo $tmpval > .tmp_tcsuite_pass_ctr
1826 fi
1827 if [ -f .tmp_tcsuite_pass ]; then
1828 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1829 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001830 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001831 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001832 echo "0" > "$TESTLOGS/$ATC/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001833 else
1834 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1835 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
1836 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
1837 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
1838 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
1839 echo ""
1840 # Update test suite counter
1841 if [ -f .tmp_tcsuite_fail_ctr ]; then
1842 tmpval=$(< .tmp_tcsuite_fail_ctr)
1843 ((tmpval++))
1844 echo $tmpval > .tmp_tcsuite_fail_ctr
1845 fi
1846 if [ -f .tmp_tcsuite_fail ]; then
1847 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1848 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001849 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001850
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001851 echo "++++ Number of tests: "$RES_TEST
1852 echo "++++ Number of passed tests: "$RES_PASS
1853 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01001854 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001855 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001856 echo ""
1857 echo "++++ Number of test case deviations: "$RES_DEVIATION
1858 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001859 echo "------------------------------------- Test case complete ---------------------------------"
1860 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01001861 echo ""
1862}
1863
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001864#####################################################################
1865###### Functions for start, configuring, stoping, cleaning etc ######
1866#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01001867
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001868# Start timer for time measurement
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001869# args: <timer message to print> - timer value and message will be printed both on screen
1870# and in the timer measurement report - if at least one "print_timer is called"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001871start_timer() {
1872 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001873 TC_TIMER_STARTTIME=$SECONDS
1874 TC_TIMER_TIMER_TEXT="${@:1}"
1875 if [ $# -ne 1 ]; then
1876 __print_err "need 1 arg, <timer message to print>" $@
1877 TC_TIMER_TIMER_TEXT=${FUNCNAME[0]}":"${BASH_LINENO[0]}
1878 echo " Assigning timer name: "$TC_TIMER_TIMER_TEXT
1879 fi
1880 TC_TIMER_CURRENT_FAILS=$(($RES_FAIL+$RES_CONF_FAIL))
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001881 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001882}
1883
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001884# Print the running timer the value of the time (in seconds)
1885# Timer value and message will be printed both on screen and in the timer measurement report
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001886print_timer() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001887 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $TC_TIMER_TIMER_TEXT $EBOLD
1888 if [ -z "$TC_TIMER_STARTTIME" ]; then
1889 __print_err "timer not started" $@
1890 return 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001891 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001892 duration=$(($SECONDS-$TC_TIMER_STARTTIME))
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001893 if [ $duration -eq 0 ]; then
1894 duration="<1 second"
1895 else
1896 duration=$duration" seconds"
1897 fi
1898 echo " Timer duration :" $duration
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001899 res="-"
1900 if [ $(($RES_FAIL+$RES_CONF_FAIL)) -ne $TC_TIMER_CURRENT_FAILS ]; then
1901 res="Failures occured during test - timer not reliabled"
1902 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001903
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001904 echo -e "$TC_TIMER_TIMER_TEXT \t $duration \t $res" >> $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001905}
1906
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001907# Print info about a deviations from intended tests
1908# Each deviation counted is also printed in the testreport
1909# args <deviation message to print>
1910deviation() {
1911 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
1912 if [ $# -lt 1 ]; then
1913 ((RES_CONF_FAIL++))
1914 __print_err "need 1 or more args, <deviation message to print>" $@
1915 exit 1
1916 fi
1917 ((RES_DEVIATION++))
1918 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
1919 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02001920 __print_current_stats
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001921 echo ""
1922}
YongchaoWu21f17bb2020-03-05 12:44:08 +01001923
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001924# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
1925__check_stop_at_error() {
1926 if [ $STOP_AT_ERROR -eq 1 ]; then
1927 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
1928 store_logs "STOP_AT_ERROR"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001929
1930 # Update test suite counter
1931 if [ -f .tmp_tcsuite_fail_ctr ]; then
1932 tmpval=$(< .tmp_tcsuite_fail_ctr)
1933 ((tmpval++))
1934 echo $tmpval > .tmp_tcsuite_fail_ctr
1935 fi
1936 if [ -f .tmp_tcsuite_fail ]; then
1937 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution stopped due to error" >> .tmp_tcsuite_fail
1938 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001939 exit 1
1940 fi
1941 return 0
1942}
1943
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001944# Stop and remove all containers
1945# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001946# (Not for test scripts)
1947__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001948
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001949 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001950
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001951 #Create empty file
1952 running_contr_file="./tmp/running_contr.txt"
1953 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001954
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001955 # Get list of all containers started by the test script
1956 for imagename in $APP_SHORT_NAMES; do
1957 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 +01001958 done
BjornMagnussonXAecf34532021-11-24 08:25:15 +01001959 running_contr_file_empty="No docker containers running, started by previous test execution"
1960 if [ -s $running_contr_file ]; then
1961 running_contr_file_empty=""
1962 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001963
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001964 # Kill all containers started by the test env - to speed up shut down
1965 docker kill $(docker ps -a --filter "label=nrttest_app" --format '{{.Names}}') &> /dev/null
1966
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001967 tab_heading1="App display name"
1968 tab_heading2="App short name"
1969 tab_heading3="Container name"
1970
1971 tab_heading1_len=${#tab_heading1}
1972 tab_heading2_len=${#tab_heading2}
1973 tab_heading3_len=${#tab_heading3}
1974 cntr=0
1975 #Calc field lengths of each item in the list of containers
1976 while read p; do
1977 if (( $cntr % 3 == 0 ));then
1978 if [ ${#p} -gt $tab_heading1_len ]; then
1979 tab_heading1_len=${#p}
1980 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001981 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001982 if (( $cntr % 3 == 1));then
1983 if [ ${#p} -gt $tab_heading2_len ]; then
1984 tab_heading2_len=${#p}
1985 fi
1986 fi
1987 if (( $cntr % 3 == 2));then
1988 if [ ${#p} -gt $tab_heading3_len ]; then
1989 tab_heading3_len=${#p}
1990 fi
1991 fi
1992 let cntr=cntr+1
1993 done <$running_contr_file
1994
1995 let tab_heading1_len=tab_heading1_len+2
1996 while (( ${#tab_heading1} < $tab_heading1_len)); do
1997 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001998 done
1999
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002000 let tab_heading2_len=tab_heading2_len+2
2001 while (( ${#tab_heading2} < $tab_heading2_len)); do
2002 tab_heading2="$tab_heading2"" "
2003 done
2004
2005 let tab_heading3_len=tab_heading3_len+2
2006 while (( ${#tab_heading3} < $tab_heading3_len)); do
2007 tab_heading3="$tab_heading3"" "
2008 done
2009
BjornMagnussonXAecf34532021-11-24 08:25:15 +01002010 if [ ! -z "$running_contr_file_empty" ]; then
2011 echo $running_contr_file_empty | indent1
2012 else
2013 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
2014 cntr=0
2015 while read p; do
2016 if (( $cntr % 3 == 0 ));then
2017 row=""
2018 heading=$p
2019 heading_len=$tab_heading1_len
2020 fi
2021 if (( $cntr % 3 == 1));then
2022 heading=$p
2023 heading_len=$tab_heading2_len
2024 fi
2025 if (( $cntr % 3 == 2));then
2026 contr=$p
2027 heading=$p
2028 heading_len=$tab_heading3_len
2029 fi
2030 while (( ${#heading} < $heading_len)); do
2031 heading="$heading"" "
2032 done
2033 row=$row$heading
2034 if (( $cntr % 3 == 2));then
2035 echo -ne $row$SAMELINE
2036 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
2037 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
2038 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
2039 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
2040 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
2041 fi
2042 let cntr=cntr+1
2043 done <$running_contr_file
2044 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002045
YongchaoWu9a84f512019-12-16 22:54:11 +01002046 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002047
2048 echo -e $BOLD" Removing docker network"$EBOLD
2049 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
2050 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002051 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002052 if [ $? -ne 0 ]; then
2053 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
2054 exit 1
2055 fi
2056 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002057 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002058
2059 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002060 docker network prune --force | indent2
2061 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002062
2063 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002064 docker volume prune --force | indent2
2065 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002066
2067 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
2068 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002069 echo -e "$GREEN Done$EGREEN"
2070 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02002071
2072 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
2073 if [ $? -eq 0 ]; then
2074 if [ $CONTRS -ne 0 ]; then
2075 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002076 docker ps -a | indent1
2077 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02002078 fi
2079 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002080}
2081
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002082###################################
2083### Functions for kube management
2084###################################
2085
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002086# Get resource type for scaling
2087# args: <resource-name> <namespace>
2088__kube_get_resource_type() {
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002089 kubectl $KUBECONF get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002090 if [ $? -eq 0 ]; then
2091 echo "deployment"
2092 return 0
2093 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002094 kubectl $KUBECONF get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002095 if [ $? -eq 0 ]; then
2096 echo "sts"
2097 return 0
2098 fi
2099 echo "unknown-resource-type"
2100 return 1
2101}
2102
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002103# Scale a kube resource to a specific count
2104# args: <resource-type> <resource-name> <namespace> <target-count>
2105# (Not for test scripts)
2106__kube_scale() {
2107 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002108 kubectl $KUBECONF scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002109 if [ $? -ne 0 ]; then
2110 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
2111 ((RES_CONF_FAIL++))
2112 echo " Message: $(<./tmp/kubeerr)"
2113 return 1
2114 else
2115 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
2116 fi
2117
2118 TSTART=$SECONDS
2119
2120 for i in {1..500}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002121 count=$(kubectl $KUBECONF get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002122 retcode=$?
2123 if [ -z "$count" ]; then
2124 #No value is sometimes returned for some reason, in case the resource has replica 0
2125 count=0
2126 fi
2127 if [ $retcode -ne 0 ]; then
2128 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
2129 ((RES_CONF_FAIL++))
2130 return 1
2131 fi
2132 #echo ""
2133 if [ $count -ne $4 ]; then
2134 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
2135 sleep $i
2136 else
2137 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
2138 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
2139 echo ""
2140 return 0
2141 fi
2142 done
2143 echo ""
2144 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
2145 ((RES_CONF_FAIL++))
2146 return 0
2147}
2148
2149# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
2150# This function does not wait for the resource to reach 0
2151# args: <namespace> <label-name> <label-id>
2152# (Not for test scripts)
2153__kube_scale_all_resources() {
2154 namespace=$1
2155 labelname=$2
2156 labelid=$3
2157 resources="deployment replicaset statefulset"
2158 for restype in $resources; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002159 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002160 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002161 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002162 echo -ne " Ordered caling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002163 kubectl $KUBECONF scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002164 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002165 done
2166 fi
2167 done
2168}
2169
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002170# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and an optional label-id
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002171# This function do wait for the resource to reach 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002172# args: <namespace> <label-name> [ <label-id> ]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002173# (Not for test scripts)
2174__kube_scale_and_wait_all_resources() {
2175 namespace=$1
2176 labelname=$2
2177 labelid=$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002178 if [ -z "$3" ]; then
2179 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname"
2180 else
2181 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname=$labelid"
2182 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002183 resources="deployment replicaset statefulset"
2184 scaled_all=1
2185 while [ $scaled_all -ne 0 ]; do
2186 scaled_all=0
2187 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002188 if [ -z "$3" ]; then
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002189 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname')].metadata.name}')
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002190 else
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002191 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002192 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002193 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2194 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002195 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002196 kubectl $KUBECONF scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002197 count=1
2198 T_START=$SECONDS
2199 while [ $count -ne 0 ]; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002200 count=$(kubectl $KUBECONF get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002201 echo -ne " Scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002202 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2203 sleep 0.5
2204 else
2205 count=0
2206 fi
2207 duration=$(($SECONDS-$T_START))
2208 if [ $duration -gt 100 ]; then
2209 #Forcring count 0, to avoid hanging for failed scaling
2210 scaled_all=1
2211 count=0
2212 fi
2213 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002214 echo -e " Scaled $restype $resid in namespace $namespace with label $labelname=$labelid to 0, current count=$count $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002215 done
2216 fi
2217 done
2218 done
2219}
2220
2221# Remove all kube resources in a namespace for resources having a certain label and label-id
2222# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
2223# args: <namespace> <label-name> <label-id>
2224# (Not for test scripts)
2225__kube_delete_all_resources() {
2226 namespace=$1
2227 labelname=$2
2228 labelid=$3
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002229 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes serviceaccounts clusterrolebindings"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002230 deleted_resourcetypes=""
2231 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002232 ns_flag="-n $namespace"
2233 ns_text="in namespace $namespace"
2234 if [ $restype == "persistentvolumes" ]; then
2235 ns_flag=""
2236 ns_text=""
2237 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002238 if [ $restype == "clusterrolebindings" ]; then
2239 ns_flag=""
2240 ns_text=""
2241 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002242 result=$(kubectl $KUBECONF get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002243 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2244 deleted_resourcetypes=$deleted_resourcetypes" "$restype
2245 for resid in $result; do
2246 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
2247 count=1
2248 while [ $count -ne 0 ]; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002249 count=$(kubectl $KUBECONF get $restype $resid $ns_flag -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002250 echo -ne " Scaling $restype $resid $ns_text with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002251 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2252 sleep 0.5
2253 else
2254 count=0
2255 fi
2256 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002257 echo -e " Scaled $restype $resid $ns_text with label $labelname=$labelid to 0, current count=$count $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002258 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002259 echo -ne " Deleting $restype $resid $ns_text with label $labelname=$labelid "$SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002260 kubectl $KUBECONF delete --grace-period=1 $restype $resid $ns_flag 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002261 if [ $? -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002262 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002263 else
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002264 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002265 fi
2266 #fi
2267 done
2268 fi
2269 done
2270 if [ ! -z "$deleted_resourcetypes" ]; then
2271 for restype in $deleted_resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002272 ns_flag="-n $namespace"
2273 ns_text="in namespace $namespace"
2274 if [ $restype == "persistentvolumes" ]; then
2275 ns_flag=""
2276 ns_text=""
2277 fi
2278 echo -ne " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted..."$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002279 T_START=$SECONDS
2280 result="dummy"
2281 while [ ! -z "$result" ]; do
2282 sleep 0.5
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002283 result=$(kubectl $KUBECONF get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002284 echo -ne " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002285 if [ -z "$result" ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002286 echo -e " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002287 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002288 echo -e " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002289 result=""
2290 fi
2291 done
2292 done
2293 fi
2294}
2295
2296# Creates a namespace if it does not exists
2297# args: <namespace>
2298# (Not for test scripts)
2299__kube_create_namespace() {
2300
2301 #Check if test namespace exists, if not create it
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002302 kubectl $KUBECONF get namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002303 if [ $? -ne 0 ]; then
2304 echo -ne " Creating namespace "$1 $SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002305 kubectl $KUBECONF create namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002306 if [ $? -ne 0 ]; then
2307 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2308 ((RES_CONF_FAIL++))
2309 echo " Message: $(<./tmp/kubeerr)"
2310 return 1
2311 else
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002312 kubectl $KUBECONF label ns $1 autotest=engine
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002313 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2314 fi
2315 else
2316 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
2317 fi
2318 return 0
2319}
2320
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002321# Removes a namespace if it exists
2322# args: <namespace>
2323# (Not for test scripts)
2324__kube_delete_namespace() {
2325
2326 #Check if test namespace exists, if so remove it
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002327 kubectl $KUBECONF get namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002328 if [ $? -eq 0 ]; then
2329 echo -ne " Removing namespace "$1 $SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002330 kubectl $KUBECONF delete namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002331 if [ $? -ne 0 ]; then
2332 echo -e " Removing namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2333 ((RES_CONF_FAIL++))
2334 echo " Message: $(<./tmp/kubeerr)"
2335 return 1
2336 else
2337 echo -e " Removing namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2338 fi
2339 else
2340 echo -e " Namespace $1 $GREEN$BOLD does not exist, OK $EBOLD$EGREEN"
2341 fi
2342 return 0
2343}
2344
2345# Removes a namespace
2346# args: <namespace>
2347# (Not for test scripts)
2348clean_and_create_namespace() {
2349 __log_conf_start $@
2350
2351 if [ $# -ne 1 ]; then
2352 __print_err "<namespace>" $@
2353 return 1
2354 fi
2355 __kube_delete_namespace $1
2356 if [ $? -ne 0 ]; then
2357 return 1
2358 fi
2359 __kube_create_namespace $1
2360 if [ $? -ne 0 ]; then
2361 return 1
2362 fi
2363
2364}
2365
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002366# Find the host ip of an app (using the service resource)
2367# args: <app-name> <namespace>
2368# (Not for test scripts)
2369__kube_get_service_host() {
2370 if [ $# -ne 2 ]; then
2371 ((RES_CONF_FAIL++))
2372 __print_err "need 2 args, <app-name> <namespace>" $@
2373 exit 1
2374 fi
2375 for timeout in {1..60}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002376 host=$(kubectl $KUBECONF get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002377 if [ $? -eq 0 ]; then
2378 if [ ! -z "$host" ]; then
2379 echo $host
2380 return 0
2381 fi
2382 fi
2383 sleep 0.5
2384 done
2385 ((RES_CONF_FAIL++))
2386 echo "host-not-found-fatal-error"
2387 return 1
2388}
2389
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002390# Find the named port to an app (using the service resource)
2391# args: <app-name> <namespace> <port-name>
2392# (Not for test scripts)
2393__kube_get_service_port() {
2394 if [ $# -ne 3 ]; then
2395 ((RES_CONF_FAIL++))
2396 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2397 exit 1
2398 fi
2399
2400 for timeout in {1..60}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002401 port=$(kubectl $KUBECONF get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002402 if [ $? -eq 0 ]; then
2403 if [ ! -z "$port" ]; then
2404 echo $port
2405 return 0
2406 fi
2407 fi
2408 sleep 0.5
2409 done
2410 ((RES_CONF_FAIL++))
2411 echo "0"
2412 return 1
2413}
2414
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002415# Find the named node port to an app (using the service resource)
2416# args: <app-name> <namespace> <port-name>
2417# (Not for test scripts)
2418__kube_get_service_nodeport() {
2419 if [ $# -ne 3 ]; then
2420 ((RES_CONF_FAIL++))
2421 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2422 exit 1
2423 fi
2424
2425 for timeout in {1..60}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002426 port=$(kubectl $KUBECONF get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002427 if [ $? -eq 0 ]; then
2428 if [ ! -z "$port" ]; then
2429 echo $port
2430 return 0
2431 fi
2432 fi
2433 sleep 0.5
2434 done
2435 ((RES_CONF_FAIL++))
2436 echo "0"
2437 return 1
2438}
2439
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002440# Create a kube resource from a yaml template
2441# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
2442# (Not for test scripts)
2443__kube_create_instance() {
2444 echo -ne " Creating $1 $2"$SAMELINE
2445 envsubst < $3 > $4
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002446 kubectl $KUBECONF apply -f $4 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002447 if [ $? -ne 0 ]; then
2448 ((RES_CONF_FAIL++))
2449 echo -e " Creating $1 $2 $RED Failed $ERED"
2450 echo " Message: $(<./tmp/kubeerr)"
2451 return 1
2452 else
2453 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
2454 fi
2455}
2456
2457# Function to create a configmap in kubernetes
2458# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
2459# (Not for test scripts)
2460__kube_create_configmap() {
2461 echo -ne " Creating configmap $1 "$SAMELINE
2462 envsubst < $5 > $5"_tmp"
2463 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002464 kubectl $KUBECONF create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002465 if [ $? -ne 0 ]; then
2466 echo -e " Creating configmap $1 $RED Failed $ERED"
2467 ((RES_CONF_FAIL++))
2468 return 1
2469 fi
2470
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002471 kubectl $KUBECONF apply -f $6 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002472 if [ $? -ne 0 ]; then
2473 echo -e " Creating configmap $1 $RED Apply failed $ERED"
2474 echo " Message: $(<./tmp/kubeerr)"
2475 ((RES_CONF_FAIL++))
2476 return 1
2477 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002478 kubectl $KUBECONF label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002479 if [ $? -ne 0 ]; then
2480 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
2481 echo " Message: $(<./tmp/kubeerr)"
2482 ((RES_CONF_FAIL++))
2483 return 1
2484 fi
2485 # Log the resulting map
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002486 kubectl $KUBECONF get configmap $1 -n $2 -o yaml > $6
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002487
2488 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
2489 return 0
2490}
2491
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002492# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
2493# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002494# args: <full kubectl cmd with parameters>
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002495# (Not for test scripts)
2496__kube_cmd_with_timeout() {
2497 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
2498
2499 while true; do
2500 kube_cmd_result=$($@)
2501 if [ $? -ne 0 ]; then
2502 kube_cmd_result=""
2503 fi
2504 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
2505 echo $kube_cmd_result
2506 return 0
2507 fi
2508 sleep 1
2509 done
2510}
2511
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002512# This function starts a pod that cleans a the contents of a path mounted as a pvc
2513# After this action the pod should terminate
2514# This should only be executed when the pod owning the pvc is not running
2515# args: <appname> <namespace> <pvc-name> <path-to remove>
2516# (Not for test scripts)
2517__kube_clean_pvc() {
2518
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002519 #using env vars setup in pvccleaner_api_functions.sh
2520
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002521 export PVC_CLEANER_NAMESPACE=$2
2522 export PVC_CLEANER_CLAIMNAME=$3
2523 export PVC_CLEANER_RM_PATH=$4
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01002524 export PVC_CLEANER_APP_NAME
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002525 input_yaml=$SIM_GROUP"/"$PVC_CLEANER_COMPOSE_DIR"/"pvc-cleaner.yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002526 output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml
2527
2528 envsubst < $input_yaml > $output_yaml
2529
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002530 kubectl $KUBECONF delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002531
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002532 __kube_create_instance pod $PVC_CLEANER_APP_NAME $input_yaml $output_yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002533 if [ $? -ne 0 ]; then
2534 echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work"
2535 return 1
2536 fi
2537
2538 term_ts=$(($SECONDS+30))
2539 while [ $term_ts -gt $SECONDS ]; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002540 pod_status=$(kubectl $KUBECONF get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase")
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002541 if [ "$pod_status" == "Succeeded" ]; then
2542 return 0
2543 fi
2544 done
2545 return 1
2546}
2547
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002548# This function scales or deletes all resources for app selected by the testcase.
2549# args: -
2550# (Not for test scripts)
2551__clean_kube() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002552 echo -e $BOLD"Initialize kube pods/statefulsets/replicaset to initial state"$EBOLD
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002553
2554 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002555 for imagename in $APP_SHORT_NAMES; do
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002556 # A function name is created from the app short name
2557 # for example app short name 'RICMSIM' -> produce the function
2558 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
2559 # This function is called and is expected to exist in the imported
2560 # file for the ricsim test functions
2561 # The resulting function impl shall scale the resources to 0
2562 # For prestarted apps, the function waits until the resources are 0
2563 # For included (not prestated) apps, the scaling is just ordered
2564 __check_prestarted_image $imagename
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002565 if [ $? -eq 0 ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002566 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002567 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2568 $function_pointer
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002569 else
2570 __check_included_image $imagename
2571 if [ $? -eq 0 ]; then
2572 function_pointer="__"$imagename"_kube_scale_zero"
2573 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2574 $function_pointer
2575 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002576 fi
2577 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002578
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002579 # Delete managed apps
2580 for imagename in $APP_SHORT_NAMES; do
2581 __check_included_image $imagename
2582 if [ $? -eq 0 ]; then
2583 __check_prestarted_image $imagename
2584 if [ $? -ne 0 ]; then
2585 # A function name is created from the app short name
2586 # for example app short name 'RICMSIM' -> produce the function
2587 # name __RICSIM__kube_delete_all
2588 # This function is called and is expected to exist in the imported
2589 # file for the ricsim test functions
2590 # The resulting function impl shall delete all its resources
2591 function_pointer="__"$imagename"_kube_delete_all"
2592 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
2593 $function_pointer
2594 fi
2595 fi
2596 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002597
2598 echo ""
2599}
2600
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002601# Function stop and remove all containers (docker) and services/deployments etc(kube)
2602# args: -
2603# Function for test script
2604clean_environment() {
2605 if [ $RUNMODE == "KUBE" ]; then
2606 __clean_kube
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002607 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002608 echo " Cleaning docker resouces to free up resources, may take time..."
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002609 ../common/clean_docker.sh 2>&1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002610 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002611 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002612 else
2613 __clean_containers
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002614 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002615 echo " Cleaning kubernetes resouces to free up resources, may take time..."
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002616 ../common/clean_kube.sh $KUBECONF 2>&1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002617 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002618 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002619 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002620}
2621
2622# 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 +01002623# args: -
2624# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002625auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002626 echo
2627 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002628 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
2629 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01002630 fi
2631}
2632
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002633# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002634# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002635# (Function for test scripts)
2636sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01002637
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002638 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
2639 if [ $# -lt 1 ]; then
2640 ((RES_CONF_FAIL++))
2641 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
2642 exit 1
2643 fi
2644 #echo "---- Sleep for " $1 " seconds ---- "$2
2645 start=$SECONDS
2646 duration=$((SECONDS-start))
2647 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002648 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002649 sleep 1
2650 duration=$((SECONDS-start))
2651 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002652 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002653 echo ""
2654}
2655
2656# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2657# Not to be called from the test script itself.
2658__print_err() {
2659 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2660 if [ $# -gt 1 ]; then
2661 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2662 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002663 ((RES_CONF_FAIL++))
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01002664 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002665}
2666
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002667# Function to create the docker network for the test
2668# Not to be called from the test script itself.
2669__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002670 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002671 if [ $? -ne 0 ]; then
2672 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2673 return 1
2674 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002675 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002676 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2677 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002678 if [ $? -ne 0 ]; then
2679 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2680 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002681 else
2682 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002683 fi
2684 else
2685 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2686 fi
2687}
2688
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002689# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002690# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2691#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002692# (Not for test scripts)
2693__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002694
2695 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002696 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002697 __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 +01002698 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002699 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002700
2701 __create_docker_network
2702
2703 curdir=$PWD
2704 cd $SIM_GROUP
2705 compose_dir=$1
2706 cd $1
2707 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002708 compose_file=$1
2709 if [ -z "$compose_file" ]; then
2710 compose_file="docker-compose.yml"
2711 fi
2712 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002713 compose_args=$1
2714 shift
2715 appcount=$1
2716 shift
2717
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002718 envsubst < $compose_file > "gen_"$compose_file
2719 compose_file="gen_"$compose_file
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002720 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
2721 docker_compose_cmd="docker-compose"
2722 else
2723 docker_compose_cmd="docker compose"
2724 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002725
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002726 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002727 $docker_compose_cmd -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002728 if [ $? -ne 0 ]; then
2729 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2730 cat .dockererr
2731 echo -e $RED"Stopping script...."$ERED
2732 exit 1
2733 fi
2734 else
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002735 $docker_compose_cmd -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002736 if [ $? -ne 0 ]; then
2737 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2738 cat .dockererr
2739 echo -e $RED"Stopping script...."$ERED
2740 exit 1
2741 fi
2742 fi
2743
2744 cd $curdir
2745
2746 appindex=0
2747 while [ $appindex -lt $appcount ]; do
2748 appname=$1
2749 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002750 app_started=0
2751 for i in {1..10}; do
2752 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002753 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 +02002754 app_started=1
2755 break
2756 else
2757 sleep $i
2758 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002759 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002760 if [ $app_started -eq 0 ]; then
2761 ((RES_CONF_FAIL++))
2762 echo ""
2763 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002764 echo -e $RED" Stopping script..."$ERED
2765 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002766 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002767 let appindex=appindex+1
2768 done
2769 return 0
2770}
2771
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002772# Function to check if container/service is responding to http/https
2773# args: <container-name>|<service-name> url
2774# (Not for test scripts)
2775__check_service_start() {
2776
2777 if [ $# -ne 2 ]; then
2778 ((RES_CONF_FAIL++))
2779 __print_err "need 2 args, <container-name>|<service-name> url" $@
2780 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002781 fi
2782
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002783 if [ $RUNMODE == "KUBE" ]; then
2784 ENTITY="service/set/deployment"
2785 else
2786 ENTITY="container"
2787 fi
2788 appname=$1
2789 url=$2
2790 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2791
2792
YongchaoWu9a84f512019-12-16 22:54:11 +01002793 pa_st=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002794 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002795 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002796 loop_ctr=0
2797 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002798 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002799 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002800 if [ ${#result} -gt 15 ]; then
2801 #If response is too long, truncate
2802 result="...response text too long, omitted"
2803 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002804 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2805 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 +01002806 pa_st=true
2807 break
2808 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002809 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002810 TS_OFFSET=$loop_ctr
2811 if (( $TS_OFFSET > 5 )); then
2812 TS_OFFSET=5
2813 fi
2814 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
2815 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 +02002816 sleep 1
2817 done
YongchaoWu9a84f512019-12-16 22:54:11 +01002818 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002819 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01002820 done
2821
2822 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002823 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002824 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 +01002825 return 1
2826 fi
2827
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002828 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002829 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002830}
2831
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002832
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002833#################
2834### Log functions
2835#################
2836
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002837__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002838
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002839 dispname=$1
2840 appname=$2
2841 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002842 warning=$4
2843 error=$5
2844
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002845 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
2846
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002847 if [ $RUNMODE == "KUBE" ]; then
2848 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
2849 return
2850 fi
2851
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002852 #tmp=$(docker ps | grep $appname)
2853 tmp=$(docker ps -q --filter name=$appname) #get the container id
2854 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002855 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002856 return
2857 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002858 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002859 if [ $? -ne 0 ];then
2860 echo " Problem to search $appname log $logpath"
2861 else
2862 if [ $foundentries -eq 0 ]; then
2863 echo " No WARN entries found in $appname log $logpath"
2864 else
2865 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
2866 fi
2867 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002868 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002869 if [ $? -ne 0 ];then
2870 echo " Problem to search $appname log $logpath"
2871 else
2872 if [ $foundentries -eq 0 ]; then
2873 echo " No ERR entries found in $appname log $logpath"
2874 else
2875 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
2876 fi
2877 fi
2878 echo ""
2879}
2880
2881# Store all container logs and other logs in the log dir for the script
2882# Logs are stored with a prefix in case logs should be stored several times during a test
2883# args: <logfile-prefix>
2884# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01002885store_logs() {
2886 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002887 ((RES_CONF_FAIL++))
2888 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01002889 exit 1
2890 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002891 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 +01002892
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002893 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002894
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002895 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002896
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002897 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
2898
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002899 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002900
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002901 # Store docker logs for all container
2902 for imagename in $APP_SHORT_NAMES; do
2903 __check_included_image $imagename
2904 if [ $? -eq 0 ]; then
2905 # A function name is created from the app short name
2906 # for example app short name 'RICMSIM' -> produce the function
2907 # name __RICSIM__store_docker_logs
2908 # This function is called and is expected to exist in the imported
2909 # file for the ricsim test functions
2910 # The resulting function impl shall store the docker logs for each container
2911 function_pointer="__"$imagename"_store_docker_logs"
2912 $function_pointer "$TESTLOGS/$ATC/" $1
2913 fi
2914 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002915 fi
2916 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002917 namespaces=$(kubectl $KUBECONF get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002918 for nsid in $namespaces; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002919 pods=$(kubectl $KUBECONF get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002920 for podid in $pods; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002921 kubectl $KUBECONF logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002922 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002923 done
2924 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002925 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01002926}
2927
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002928###############
2929## Generic curl
2930###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002931# Generic curl function, assumes all 200-codes are ok
2932# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002933# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
2934# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01002935__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002936 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002937 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002938 if [ ! -z "$KUBE_PROXY_PATH" ]; then
2939 if [ $KUBE_PROXY_HTTPX == "http" ]; then
2940 proxyflag=" --proxy $KUBE_PROXY_PATH"
2941 else
2942 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002943 fi
2944 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002945 curlString="curl -skw %{http_code} $proxyflag $@"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002946 echo " CMD: $curlString" >> $HTTPLOG
2947 res=$($curlString)
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002948 retcode=$?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002949 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002950 echo " RETCODE: $retcode" >> $HTTPLOG
2951 if [ $retcode -ne 0 ]; then
2952 echo "<no-response-from-server>"
2953 return 1
2954 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002955 http_code="${res:${#res}-3}"
2956 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002957 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
2958 echo "<no-response-from-server>"
2959 return 1
2960 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002961 return 0
2962 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002963 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002964 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01002965 echo "<not found, resp:${http_code}>"
2966 return 1
2967 fi
2968 if [ $# -eq 2 ]; then
2969 echo "${res:0:${#res}-3}" | xargs
2970 else
2971 echo "${res:0:${#res}-3}"
2972 fi
2973
2974 return 0
2975 fi
2976}
2977
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002978#######################################
2979### Basic helper function for test cases
2980#######################################
2981
2982# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
2983# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
2984# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
2985# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
2986# before setting pass or fail depending on the result of comparing variable and target using the operator.
2987# 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.
2988# Not to be called from test script.
2989
2990__var_test() {
2991 checkjsonarraycount=0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002992 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002993 if [ $# -eq 6 ]; then
2994 if [[ $3 == "json:"* ]]; then
2995 checkjsonarraycount=1
2996 fi
2997
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002998 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002999 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG
3000
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003001 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003002 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003003 start=$SECONDS
3004 ctr=0
3005 for (( ; ; )); do
3006 if [ $checkjsonarraycount -eq 0 ]; then
3007 result="$(__do_curl $2$3)"
3008 retcode=$?
3009 result=${result//[[:blank:]]/} #Strip blanks
3010 else
3011 path=${3:5}
3012 result="$(__do_curl $2$path)"
3013 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003014 echo "$result" > ./tmp/.tmp.curl.json
3015 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003016 fi
3017 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003018 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003019 let ctr=ctr+1
3020 if [ $retcode -ne 0 ]; then
3021 if [ $duration -gt $6 ]; then
3022 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003023 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003024 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003025 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003026 return
3027 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003028 elif [ "$4" == "=" ] && [ "$result" -eq $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003029 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003030 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003031 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003032 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003033 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003034 elif [ "$4" == ">" ] && [ "$result" -gt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003035 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003036 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003037 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003038 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003039 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003040 elif [ "$4" == "<" ] && [ "$result" -lt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003041 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003042 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003043 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003044 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003045 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003046 elif [ "$4" == ">=" ] && [ "$result" -ge $5 ]; then
3047 ((RES_PASS++))
3048 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
3049 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
3050 __print_current_stats
3051 return
3052 elif [ "$4" == "contain_str" ] && [[ $result =~ $5 ]]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003053 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003054 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003055 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003056 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003057 return
3058 else
3059 if [ $duration -gt $6 ]; then
3060 ((RES_FAIL++))
3061 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003062 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003063 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003064 return
3065 fi
3066 fi
3067 sleep 1
3068 done
3069 elif [ $# -eq 5 ]; then
3070 if [[ $3 == "json:"* ]]; then
3071 checkjsonarraycount=1
3072 fi
3073
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003074 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02003075 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003076 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003077 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003078 if [ $checkjsonarraycount -eq 0 ]; then
3079 result="$(__do_curl $2$3)"
3080 retcode=$?
3081 result=${result//[[:blank:]]/} #Strip blanks
3082 else
3083 path=${3:5}
3084 result="$(__do_curl $2$path)"
3085 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003086 echo "$result" > ./tmp/.tmp.curl.json
3087 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003088 fi
3089 if [ $retcode -ne 0 ]; then
3090 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003091 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003092 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003093 __check_stop_at_error
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003094 elif [ "$4" == "=" ] && [ "$result" -eq $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003095 ((RES_PASS++))
3096 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003097 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003098 elif [ "$4" == ">" ] && [ "$result" -gt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003099 ((RES_PASS++))
3100 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003101 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003102 elif [ "$4" == "<" ] && [ "$result" -lt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003103 ((RES_PASS++))
3104 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003105 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003106 elif [ "$4" == ">=" ] && [ "$result" -ge $5 ]; then
3107 ((RES_PASS++))
3108 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
3109 __print_current_stats
3110 elif [ "$4" == "contain_str" ] && [[ $result =~ $5 ]]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003111 ((RES_PASS++))
3112 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003113 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003114 else
3115 ((RES_FAIL++))
3116 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003117 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003118 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003119 fi
3120 else
3121 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
3122 echo "Got:" $@
3123 exit 1
3124 fi
3125}