blob: c9374cf80597bf37c0a146b45311325b9d2a16d9 [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/bin/bash
YongchaoWu9a84f512019-12-16 22:54:11 +01002
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003# ============LICENSE_START===============================================
4# Copyright (C) 2020 Nordix Foundation. All rights reserved.
5# ========================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ============LICENSE_END=================================================
18#
YongchaoWu9a84f512019-12-16 22:54:11 +010019
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010020# This is a script that contains all the common functions needed for auto test.
21# Specific test function are defined in scripts XXXX_functions.sh
22
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010023. ../common/api_curl.sh
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010024. ../common/testengine_config.sh
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010025
26__print_args() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010027 echo "Args: remote|remote-remove docker|kube --env-file <environment-filename> [release] [auto-clean] [--stop-at-error] "
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010028 echo " [--ricsim-prefix <prefix> ] [--use-local-image <app-nam>+] [--use-snapshot-image <app-nam>+]"
BjornMagnussonXA483ee332021-04-08 01:35:24 +020029 echo " [--use-staging-image <app-nam>+] [--use-release-image <app-nam>+] [--image-repo <repo-address]"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +020030 echo " [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>] [--print-stats]"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +010031 echo " [--override <override-environment-filename>] [--pre-clean] [--gen-stats] [--delete-namespaces]"
32 echo " [--delete-containers] [--endpoint-stats]"
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"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010067 echo ""
68 echo "List of app short names supported: "$APP_SHORT_NAMES
69 exit 0
70fi
71
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010072AUTOTEST_HOME=$PWD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020073# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
74# FTC1.sh -> ATC == FTC1
75ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
76
77#Create result file (containing '1' for error) for this test case
78#Will be replaced with a file containing '0' if all test cases pass
79echo "1" > "$PWD/.result$ATC.txt"
80
BjornMagnussonXA80a92002020-03-19 14:31:06 +010081#Formatting for 'echo' cmd
82BOLD="\033[1m"
83EBOLD="\033[0m"
84RED="\033[31m\033[1m"
85ERED="\033[0m"
86GREEN="\033[32m\033[1m"
87EGREEN="\033[0m"
88YELLOW="\033[33m\033[1m"
89EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020090SAMELINE="\033[0K\r"
91
BjornMagnussonXA80a92002020-03-19 14:31:06 +010092# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020093echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +010094
BjornMagnussonXA575869c2020-09-14 21:28:54 +020095# default test environment variables
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020096TEST_ENV_VAR_FILE=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +010097#Override env file, will be added on top of the above file
98TEST_ENV_VAR_FILE_OVERRIDE=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +010099
100echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
101
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100102#Localhost constants
103LOCALHOST_NAME="localhost"
104LOCALHOST_HTTP="http://localhost"
105LOCALHOST_HTTPS="https://localhost"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200106
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100107# Var to hold 'auto' in case containers shall be stopped when test case ends
108AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +0100109
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100110# Var to indicate pre clean, if flag --pre-clean is set the script will clean kube resouces when running docker and vice versa
111PRE_CLEAN="0"
112
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100113# Var to hold the app names to use local images for
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200114USE_LOCAL_IMAGES=""
115
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100116# Var to hold the app names to use remote snapshot images for
117USE_SNAPSHOT_IMAGES=""
118
119# Var to hold the app names to use remote staging images for
120USE_STAGING_IMAGES=""
121
122# Var to hold the app names to use remote release images for
123USE_RELEASE_IMAGES=""
124
BjornMagnussonXAad047782020-06-08 15:54:11 +0200125
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200126# 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
127STOP_AT_ERROR=0
128
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100129# The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used.
130# The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used
131# Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE
132IMAGE_CATEGORY="DEV"
133
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100134#Var to indicate docker-compose version, V1 or V2
135#V1 names replicated containers <proj-name>_<service-name>_<index>
136#V2 names replicated containers <proj-name>-<service-name>-<index>
137DOCKER_COMPOSE_VERION="V1"
138
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200139# Function to indent cmd output with one space
140indent1() { sed 's/^/ /'; }
141
142# Function to indent cmd output with two spaces
143indent2() { sed 's/^/ /'; }
144
YongchaoWu9a84f512019-12-16 22:54:11 +0100145# Set a description string for the test case
146if [ -z "$TC_ONELINE_DESCR" ]; then
147 TC_ONELINE_DESCR="<no-description>"
148 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
149fi
150
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100151# Counter for test suites
152if [ -f .tmp_tcsuite_ctr ]; then
153 tmpval=$(< .tmp_tcsuite_ctr)
154 ((tmpval++))
155 echo $tmpval > .tmp_tcsuite_ctr
156fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100157
YongchaoWu9a84f512019-12-16 22:54:11 +0100158# Create the logs dir if not already created in the current dir
159if [ ! -d "logs" ]; then
160 mkdir logs
161fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100162TESTLOGS=$PWD/logs
163
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200164# Create the tmp dir for temporary files that is not needed after the test
165# hidden files for the test env is still stored in the current dir
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100166# files in the ./tmp is moved to ./tmp/prev when a new test is started
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200167if [ ! -d "tmp" ]; then
168 mkdir tmp
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100169 if [ $? -ne 0 ]; then
170 echo "Cannot create dir for temp files, $PWD/tmp"
171 echo "Exiting...."
172 exit 1
173 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200174fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100175curdir=$PWD
176cd tmp
177if [ $? -ne 0 ]; then
178 echo "Cannot cd to $PWD/tmp"
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100179 echo "Exiting...."
180 exit 1
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100181fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100182
183TESTENV_TEMP_FILES=$PWD
184
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100185if [ ! -d "prev" ]; then
186 mkdir prev
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100187 if [ $? -ne 0 ]; then
188 echo "Cannot create dir for previous temp files, $PWD/prev"
189 echo "Exiting...."
190 exit 1
191 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100192fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100193
194TMPFILES=$(ls -A | grep -vw prev)
195if [ ! -z "$TMPFILES" ]; then
196 cp -r $TMPFILES prev #Move all temp files to prev dir
197 if [ $? -ne 0 ]; then
198 echo "Cannot move temp files in $PWD to previous temp files in, $PWD/prev"
199 echo "Exiting...."
200 exit 1
201 fi
202 if [ $(pwd | xargs basename) == "tmp" ]; then #Check that current dir is tmp...for safety
203
204 rm -rf $TMPFILES # Remove all temp files
205 fi
206fi
207
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100208cd $curdir
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100209if [ $? -ne 0 ]; then
210 echo "Cannot cd to $curdir"
211 echo "Exiting...."
212 exit 1
213fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200214
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100215
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100216# Create a http message log for this testcase
217HTTPLOG=$PWD"/.httplog_"$ATC".txt"
218echo "" > $HTTPLOG
219
220# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100221mkdir -p $TESTLOGS/$ATC
222
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100223# Save create for current logs
224mkdir -p $TESTLOGS/$ATC/previous
225
226rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
227rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
228rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
229
230mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
231mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
232mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
233
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100234# Clear the log dir for the test case
235rm $TESTLOGS/$ATC/*.log &> /dev/null
236rm $TESTLOGS/$ATC/*.txt &> /dev/null
237rm $TESTLOGS/$ATC/*.json &> /dev/null
238
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100239#Create result file in the log dir
240echo "1" > "$TESTLOGS/$ATC/.result$ATC.txt"
241
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100242# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100243TCLOG=$TESTLOGS/$ATC/TC.log
244exec &> >(tee ${TCLOG})
245
246#Variables for counting tests as well as passed and failed tests
247RES_TEST=0
248RES_PASS=0
249RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100250RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200251RES_DEVIATION=0
252
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200253#Var to control if current stats shall be printed
254PRINT_CURRENT_STATS=0
255
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100256#Var to control if container/pod runtim statistics shall be collected
257COLLECT_RUNTIME_STATS=0
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +0100258COLLECT_RUNTIME_STATS_PID=0
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100259
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100260#Var to control if endpoint statistics shall be collected
261COLLECT_ENDPOINT_STATS=0
262
263#Var to control if namespaces shall be delete before test setup
264DELETE_KUBE_NAMESPACES=0
265
266#Var to control if containers shall be delete before test setup
267DELETE_CONTAINERS=0
268
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200269#File to keep deviation messages
270DEVIATION_FILE=".tmp_deviations"
271rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100272
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100273# Trap "command not found" and make the script fail
274trap_fnc() {
275
276 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100277 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100278 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200279 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100280 fi
281}
282trap trap_fnc ERR
283
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +0100284# Trap to kill subprocess for stats collection (if running)
285trap_fnc2() {
286 if [ $COLLECT_RUNTIME_STATS_PID -ne 0 ]; then
287 kill $COLLECT_RUNTIME_STATS_PID
288 fi
289}
290trap trap_fnc2 EXIT
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100291
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100292# Counter for tests
293TEST_SEQUENCE_NR=1
294
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100295# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100296__log_test_start() {
297 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
298 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
299 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
300 ((RES_TEST++))
301 ((TEST_SEQUENCE_NR++))
302}
303
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200304# Function to print current statistics
305__print_current_stats() {
306 if [ $PRINT_CURRENT_STATS -ne 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200307 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 +0200308 fi
309}
310
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100311# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100312__log_test_fail_general() {
313 echo -e $RED" FAIL."$1 $ERED
314 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200315 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100316 __check_stop_at_error
317}
318
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100319# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100320__log_test_fail_status_code() {
321 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
322 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200323 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100324 __check_stop_at_error
325}
326
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100327# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100328__log_test_fail_body() {
329 echo -e $RED" FAIL, returned body not correct"$ERED
330 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200331 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100332 __check_stop_at_error
333}
334
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100335# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100336__log_test_fail_not_supported() {
337 echo -e $RED" FAIL, function not supported"$ERED
338 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200339 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100340 __check_stop_at_error
341}
342
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100343# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100344__log_test_pass() {
345 if [ $# -gt 0 ]; then
346 echo $@
347 fi
348 ((RES_PASS++))
349 echo -e $GREEN" PASS"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200350 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100351}
352
353#Counter for configurations
354CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100355
356# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100357__log_conf_start() {
358 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
359 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
360 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
361 ((CONF_SEQUENCE_NR++))
362}
363
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100364# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100365__log_conf_fail_general() {
366 echo -e $RED" FAIL."$1 $ERED
367 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200368 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100369 __check_stop_at_error
370}
371
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100372# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100373__log_conf_fail_status_code() {
374 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
375 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200376 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100377 __check_stop_at_error
378}
379
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100380# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100381__log_conf_fail_body() {
382 echo -e $RED" FAIL, returned body not correct"$ERED
383 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200384 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100385 __check_stop_at_error
386}
387
BjornMagnussonXA83a750f2021-09-21 20:39:58 +0200388# Function to log a configuration that is not supported
389__log_conf_fail_not_supported() {
390 echo -e $RED" FAIL, function not supported"$ERED$@
391 ((RES_CONF_FAIL++))
392 __print_current_stats
393 __check_stop_at_error
394}
395
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100396# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100397__log_conf_ok() {
398 if [ $# -gt 0 ]; then
399 echo $@
400 fi
401 echo -e $GREEN" OK"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200402 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100403}
404
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100405# Function to collect stats on endpoints
406# args: <app-id> <end-point-no> <http-operation> <end-point-url> <http-status> [<count>]
407__collect_endpoint_stats() {
408 if [ $COLLECT_ENDPOINT_STATS -eq 0 ]; then
409 return
410 fi
411 ENDPOINT_COUNT=1
412 if [ $# -gt 5 ]; then
413 ENDPOINT_COUNT=$6
414 fi
415 ENDPOINT_STAT_FILE=$TESTLOGS/$ATC/endpoint_$ATC_$1_$2".log"
416 ENDPOINT_POS=0
417 ENDPOINT_NEG=0
418 if [ -f $ENDPOINT_STAT_FILE ]; then
419 ENDPOINT_VAL=$(< $ENDPOINT_STAT_FILE)
420 ENDPOINT_POS=$(echo $ENDPOINT_VAL | cut -f4 -d ' ' | cut -f1 -d '/')
421 ENDPOINT_NEG=$(echo $ENDPOINT_VAL | cut -f5 -d ' ' | cut -f1 -d '/')
422 fi
423
424 if [ $5 -ge 200 ] && [ $5 -lt 300 ]; then
425 let ENDPOINT_POS=ENDPOINT_POS+$ENDPOINT_COUNT
426 else
427 let ENDPOINT_NEG=ENDPOINT_NEG+$ENDPOINT_COUNT
428 fi
429
430 printf '%-2s %-10s %-45s %-16s %-16s' "#" "$3" "$4" "$ENDPOINT_POS/$ENDPOINT_POS" "$ENDPOINT_NEG/$ENDPOINT_NEG" > $ENDPOINT_STAT_FILE
431}
432
433# Function to collect stats on endpoints
434# args: <app-id> <image-info>
435__collect_endpoint_stats_image_info() {
436 if [ $COLLECT_ENDPOINT_STATS -eq 0 ]; then
437 return
438 fi
439 ENDPOINT_STAT_FILE=$TESTLOGS/$ATC/imageinfo_$ATC_$1".log"
440 echo $POLICY_AGENT_IMAGE > $ENDPOINT_STAT_FILE
441}
442
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100443#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100444TCTEST_START=$SECONDS
445
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100446#Vars to hold the start time and timer text for a custom timer
447TC_TIMER_STARTTIME=""
448TC_TIMER_TIMER_TEXT=""
449TC_TIMER_CURRENT_FAILS="" # Then numer of failed test when timer starts.
450 # Compared with the current number of fails at timer stop
451 # to judge the measurement reliability
452
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200453#File to save timer measurement results
454TIMER_MEASUREMENTS=".timer_measurement.txt"
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100455echo -e "Activity \t Duration \t Info" > $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200456
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100457# 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 +0200458IMAGE_REPO_ADR=""
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200459IMAGE_REPO_POLICY="local"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200460CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200461
YongchaoWu9a84f512019-12-16 22:54:11 +0100462echo "-------------------------------------------------------------------------------------------------"
463echo "----------------------------------- Test case: "$ATC
464echo "----------------------------------- Started: "$(date)
465echo "-------------------------------------------------------------------------------------------------"
466echo "-- Description: "$TC_ONELINE_DESCR
467echo "-------------------------------------------------------------------------------------------------"
468echo "----------------------------------- Test case setup -----------------------------------"
469
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100470echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200471START_ARG=$1
472paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100473paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200474if [ $# -lt 1 ]; then
475 paramerror=1
476fi
477if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100478 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200479 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100480 if [ -z "$paramerror_str" ]; then
481 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
482 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200483 else
484 shift;
485 fi
486fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100487if [ $paramerror -eq 0 ]; then
488 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
489 paramerror=1
490 if [ -z "$paramerror_str" ]; then
491 paramerror_str="Second arg shall be 'docker' or 'kube'"
492 fi
493 else
494 if [ $1 == "docker" ]; then
495 RUNMODE="DOCKER"
496 echo "Setting RUNMODE=DOCKER"
497 fi
498 if [ $1 == "kube" ]; then
499 RUNMODE="KUBE"
500 echo "Setting RUNMODE=KUBE"
501 fi
502 shift;
503 fi
504fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200505foundparm=0
506while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
507 foundparm=1
508 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100509 if [ "$1" == "release" ]; then
510 IMAGE_CATEGORY="RELEASE"
511 echo "Option set - Release image tags used for applicable images "
512 shift;
513 foundparm=0
514 fi
515 fi
516 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200517 if [ "$1" == "auto-clean" ]; then
518 AUTO_CLEAN="auto"
519 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200520 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200521 foundparm=0
522 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200523 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200524 if [ $paramerror -eq 0 ]; then
525 if [ "$1" == "--stop-at-error" ]; then
526 STOP_AT_ERROR=1
527 echo "Option set - Stop at first error"
528 shift;
529 foundparm=0
530 fi
531 fi
532 if [ $paramerror -eq 0 ]; then
533 if [ "$1" == "--ricsim-prefix" ]; then
534 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100535 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200536 if [ -z "$1" ]; then
537 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100538 if [ -z "$paramerror_str" ]; then
539 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
540 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200541 else
542 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
543 shift;
544 foundparm=0
545 fi
546 fi
547 fi
548 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200549 if [ "$1" == "--env-file" ]; then
550 shift;
551 TEST_ENV_VAR_FILE=$1
552 if [ -z "$1" ]; then
553 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100554 if [ -z "$paramerror_str" ]; then
555 paramerror_str="No env file found for flag: '--env-file'"
556 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200557 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200558 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200559 shift;
560 foundparm=0
561 fi
562 fi
563 fi
564 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200565 if [ "$1" == "--use-local-image" ]; then
566 USE_LOCAL_IMAGES=""
567 shift
568 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
569 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100570 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200571 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100572 if [ -z "$paramerror_str" ]; then
573 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
574 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200575 fi
576 shift;
577 done
578 foundparm=0
579 if [ -z "$USE_LOCAL_IMAGES" ]; then
580 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100581 if [ -z "$paramerror_str" ]; then
582 paramerror_str="No app name found for flag: '--use-local-image'"
583 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200584 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100585 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
586 fi
587 fi
588 fi
589 if [ $paramerror -eq 0 ]; then
590 if [ "$1" == "--use-snapshot-image" ]; then
591 USE_SNAPSHOT_IMAGES=""
592 shift
593 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
594 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
595 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
596 paramerror=1
597 if [ -z "$paramerror_str" ]; then
598 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
599 fi
600 fi
601 shift;
602 done
603 foundparm=0
604 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
605 paramerror=1
606 if [ -z "$paramerror_str" ]; then
607 paramerror_str="No app name found for flag: '--use-snapshot-image'"
608 fi
609 else
610 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
611 fi
612 fi
613 fi
614 if [ $paramerror -eq 0 ]; then
615 if [ "$1" == "--use-staging-image" ]; then
616 USE_STAGING_IMAGES=""
617 shift
618 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
619 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
620 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
621 paramerror=1
622 if [ -z "$paramerror_str" ]; then
623 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
624 fi
625 fi
626 shift;
627 done
628 foundparm=0
629 if [ -z "$USE_STAGING_IMAGES" ]; then
630 paramerror=1
631 if [ -z "$paramerror_str" ]; then
632 paramerror_str="No app name found for flag: '--use-staging-image'"
633 fi
634 else
635 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
636 fi
637 fi
638 fi
639 if [ $paramerror -eq 0 ]; then
640 if [ "$1" == "--use-release-image" ]; then
641 USE_RELEASE_IMAGES=""
642 shift
643 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
644 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
645 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
646 paramerror=1
647 if [ -z "$paramerror_str" ]; then
648 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
649 fi
650 fi
651 shift;
652 done
653 foundparm=0
654 if [ -z "$USE_RELEASE_IMAGES" ]; then
655 paramerror=1
656 if [ -z "$paramerror_str" ]; then
657 paramerror_str="No app name found for flag: '--use-release-image'"
658 fi
659 else
660 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200661 fi
662 fi
663 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200664 if [ $paramerror -eq 0 ]; then
665 if [ "$1" == "--image-repo" ]; then
666 shift;
667 IMAGE_REPO_ADR=$1
668 if [ -z "$1" ]; then
669 paramerror=1
670 if [ -z "$paramerror_str" ]; then
671 paramerror_str="No image repo url found for : '--image-repo'"
672 fi
673 else
674 echo "Option set - Image repo url: "$1
675 shift;
676 foundparm=0
677 fi
678 fi
679 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200680 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200681 if [ "$1" == "--repo-policy" ]; then
682 shift;
683 IMAGE_REPO_POLICY=$1
684 if [ -z "$1" ]; then
685 paramerror=1
686 if [ -z "$paramerror_str" ]; then
687 paramerror_str="No policy found for : '--repo-policy'"
688 fi
689 else
690 if [ "$1" == "local" ] || [ "$1" == "remote" ]; then
691 echo "Option set - Image repo policy: "$1
692 shift;
693 foundparm=0
694 else
695 paramerror=1
696 if [ -z "$paramerror_str" ]; then
697 paramerror_str="Repo policy shall be 'local' or 'remote'"
698 fi
699 fi
700 fi
701 fi
702 fi
703 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200704 if [ "$1" == "--cluster-timeout" ]; then
705 shift;
706 CLUSTER_TIME_OUT=$1
707 if [ -z "$1" ]; then
708 paramerror=1
709 if [ -z "$paramerror_str" ]; then
710 paramerror_str="No timeout value found for : '--cluster-timeout'"
711 fi
712 else
713 #Check if positive int
714 case ${CLUSTER_TIME_OUT#[+]} in
715 *[!0-9]* | '')
716 paramerror=1
717 if [ -z "$paramerror_str" ]; then
718 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
719 fi
720 ;;
721 * ) ;; # Ok
722 esac
723 echo "Option set - Cluster timeout: "$1
724 shift;
725 foundparm=0
726 fi
727 fi
728 fi
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200729 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100730 if [ "$1" == "--override" ]; then
731 shift;
732 TEST_ENV_VAR_FILE_OVERRIDE=$1
733 if [ -z "$1" ]; then
734 paramerror=1
735 if [ -z "$paramerror_str" ]; then
736 paramerror_str="No env file found for flag: '--override'"
737 fi
738 else
739 if [ ! -f $TEST_ENV_VAR_FILE_OVERRIDE ]; then
740 paramerror=1
741 if [ -z "$paramerror_str" ]; then
742 paramerror_str="File for '--override' does not exist : "$TEST_ENV_VAR_FILE_OVERRIDE
743 fi
744 fi
745 echo "Option set - Override env from: "$1
746 shift;
747 foundparm=0
748 fi
749 fi
750 fi
751 if [ $paramerror -eq 0 ]; then
752 if [ "$1" == "--pre-clean" ]; then
753 PRE_CLEAN=1
754 echo "Option set - Pre-clean of kube/docker resouces"
755 shift;
756 foundparm=0
757 fi
758 fi
759 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200760 if [ "$1" == "--print-stats" ]; then
761 PRINT_CURRENT_STATS=1
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100762 echo "Option set - Print stats after every test-case and config"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200763 shift;
764 foundparm=0
765 fi
766 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100767 if [ $paramerror -eq 0 ]; then
768 if [ "$1" == "--gen-stats" ]; then
769 COLLECT_RUNTIME_STATS=1
770 echo "Option set - Collect runtime statistics"
771 shift;
772 foundparm=0
773 fi
774 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100775 if [ $paramerror -eq 0 ]; then
776 if [ "$1" == "--delete-namespaces" ]; then
777 if [ $RUNMODE == "DOCKER" ]; then
778 DELETE_KUBE_NAMESPACES=0
779 echo "Option ignored - Delete namespaces (ignored when running docker)"
780 else
781 if [ -z "KUBE_PRESTARTED_IMAGES" ]; then
782 DELETE_KUBE_NAMESPACES=0
783 echo "Option ignored - Delete namespaces (ignored when using prestarted apps)"
784 else
785 DELETE_KUBE_NAMESPACES=1
786 echo "Option set - Delete namespaces"
787 fi
788 fi
789 shift;
790 foundparm=0
791 fi
792 fi
793 if [ $paramerror -eq 0 ]; then
794 if [ "$1" == "--delete-containers" ]; then
795 if [ $RUNMODE == "DOCKER" ]; then
796 DELETE_CONTAINERS=1
797 echo "Option set - Delete containers started by previous test(s)"
798 else
799 echo "Option ignored - Delete containers (ignored when running kube)"
800 fi
801 shift;
802 foundparm=0
803 fi
804 fi
805 if [ $paramerror -eq 0 ]; then
806 if [ "$1" == "--endpoint-stats" ]; then
807 COLLECT_ENDPOINT_STATS=1
808 echo "Option set - Collect endpoint statistics"
809 shift;
810 foundparm=0
811 fi
812 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100813
BjornMagnussonXAad047782020-06-08 15:54:11 +0200814done
815echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200816
BjornMagnussonXAad047782020-06-08 15:54:11 +0200817#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200818if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
819 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100820 if [ -z "$paramerror_str" ]; then
821 paramerror_str="Unknown parameter(s): "$@
822 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200823fi
824
825if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100826 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
827 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200828 exit 1
829fi
830
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200831# sourcing the selected env variables for the test case
832if [ -f "$TEST_ENV_VAR_FILE" ]; then
833 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
834 . $TEST_ENV_VAR_FILE
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100835 if [ ! -z "$TEST_ENV_VAR_FILE_OVERRIDE" ]; then
836 echo -e $BOLD"Sourcing override env vars from: "$TEST_ENV_VAR_FILE_OVERRIDE$EBOLD
837 . $TEST_ENV_VAR_FILE_OVERRIDE
838 fi
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100839
840 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100841 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 +0100842 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100843 found_profile=0
844 for prof in $SUPPORTED_PROFILES; do
845 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
846 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
847 found_profile=1
848 fi
849 done
850 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100851 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100852 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100853 echo -e $RED"Exiting...."$ERED
854 exit 1
855 fi
856 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200857else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200858 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200859 echo " Select one of following env var file matching the intended target of the test"
860 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100861 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200862 exit 1
863fi
864
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100865#This var need be preserved from the command line option, if set, when env var is sourced.
866if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
867 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
868fi
869
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100870if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100871 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100872 exit 1
873fi
874
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100875if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
876 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
877 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
878 exit 1
879fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100880
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100881# Choose list of included apps depending on run-mode
882if [ $RUNMODE == "KUBE" ]; then
883 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
884else
885 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
886fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200887
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100888echo ""
889# auto adding system apps
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100890__added_apps=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100891echo -e $BOLD"Auto adding system apps"$EBOLD
892if [ $RUNMODE == "KUBE" ]; then
893 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_KUBE_SYSTEM_APPS
894 TMP_APPS=$TESTENV_KUBE_SYSTEM_APPS
895else
896 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_DOCKER_SYSTEM_APPS
897 TMP_APPS=$TESTENV_DOCKER_SYSTEM_APPS
898fi
899if [ ! -z "$TMP_APPS" ]; then
900 for iapp in "$TMP_APPS"; do
901 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
902 file_pointer="../common/"$file_pointer"_api_functions.sh"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100903 padded_iapp=$iapp
904 while [ ${#padded_iapp} -lt 16 ]; do
905 padded_iapp=$padded_iapp" "
906 done
907 echo " Auto-adding system app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100908 . $file_pointer
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100909 if [ $? -ne 0 ]; then
910 echo " Include file $file_pointer contain errors. Exiting..."
911 exit 1
912 fi
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100913 __added_apps=" $iapp "$__added_apps
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100914 done
915else
916 echo " None"
917fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100918
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100919if [ $RUNMODE == "KUBE" ]; then
920 TMP_APPS=$INCLUDED_IMAGES" "$KUBE_PRESTARTED_IMAGES
921else
922 TMP_APPS=$INCLUDED_IMAGES
923fi
924
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100925echo -e $BOLD"Auto adding included apps"$EBOLD
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100926 for iapp in $TMP_APPS; do
927 if [[ "$__added_apps" != *"$iapp"* ]]; then
928 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
929 file_pointer="../common/"$file_pointer"_api_functions.sh"
930 padded_iapp=$iapp
931 while [ ${#padded_iapp} -lt 16 ]; do
932 padded_iapp=$padded_iapp" "
933 done
934 echo " Auto-adding included app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +0100935 if [ ! -f "$file_pointer" ]; then
936 echo " Include file $file_pointer for app $iapp does not exist"
937 exit 1
938 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100939 . $file_pointer
940 if [ $? -ne 0 ]; then
941 echo " Include file $file_pointer contain errors. Exiting..."
942 exit 1
943 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100944 fi
945 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100946echo ""
947
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100948echo -e $BOLD"Test environment info"$EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100949
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100950# Check needed installed sw
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100951
952tmp=$(which bash)
953if [ $? -ne 0 ] || [ -z "$tmp" ]; then
954 echo -e $RED"bash is required to run the test environment, pls install"$ERED
955 exit 1
956fi
957echo " bash is installed and using version:"
958echo "$(bash --version)" | indent2
959
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100960tmp=$(which python3)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100961if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100962 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
963 exit 1
964fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100965echo " python3 is installed and using version: $(python3 --version)"
966
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100967tmp=$(which docker)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100968if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100969 echo -e $RED"docker is required to run the test environment, pls install"$ERED
970 exit 1
971fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100972echo " docker is installed and using versions:"
973echo " $(docker version --format 'Client version {{.Client.Version}} Server version {{.Server.Version}}')"
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200974
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100975tmp=$(which docker-compose)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100976if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100977 if [ $RUNMODE == "DOCKER" ]; then
978 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
979 exit 1
980 fi
981fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100982tmp=$(docker-compose version --short)
983echo " docker-compose installed and using version $tmp"
984if [[ "$tmp" == *'v2'* ]]; then
985 DOCKER_COMPOSE_VERION="V2"
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100986fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200987
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100988tmp=$(which kubectl)
989if [ $? -ne 0 ] || [ -z tmp ]; then
990 if [ $RUNMODE == "KUBE" ]; then
991 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
992 exit 1
993 fi
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200994else
995 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100996 echo " kubectl is installed and using versions:"
997 echo $(kubectl version --short=true) | indent2
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +0200998 res=$(kubectl cluster-info 2>&1)
999 if [ $? -ne 0 ]; then
1000 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
1001 echo -e $BOLD$RED"Command 'kubectl cluster-info' returned error $ERED$EBOLD"
1002 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
1003 echo " "
1004 echo "kubectl response:"
1005 echo $res
1006 echo " "
1007 echo "This script may have been started with user with no permission to run kubectl"
1008 echo "Try running with 'sudo' or set 'KUBECONFIG'"
1009 echo "Do either 1, 2 or 3 "
1010 echo " "
1011 echo "1"
1012 echo "Run with sudo"
1013 echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD
1014 echo " "
1015 echo "2"
1016 echo "Export KUBECONFIG and pass env to sudo - (replace user)"
1017 echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD
1018 echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD
1019 echo " "
1020 echo "3"
1021 echo "Set KUBECONFIG inline (replace user)"
1022 echo -e $BOLD"sudo KUBECONFIG='/home/<user>/.kube/config' <test-script-and-parameters>"$EBOLD
1023
1024 exit 1
1025 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001026 echo " Node(s) and container runtime config"
BjornMagnussonXA6dc3bd22021-12-20 08:58:10 +01001027 kubectl get nodes -o wide | indent2
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001028 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001029fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +01001030
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001031echo ""
1032
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001033echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +01001034
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001035#Temp var to check for image variable name errors
1036IMAGE_ERR=0
1037#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001038image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001039echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001040
1041# 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 +01001042# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001043__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001044
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001045 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001046 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 +01001047 ((IMAGE_ERR++))
1048 return
1049 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001050
1051 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001052 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001053 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001054 # Image is excluded since the corresponding app is not used in this test
1055 return
1056 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001057 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001058 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001059 image="${!3}"
1060 tmptag=$4"_"$5
1061 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001062
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001063 optional_image_repo_target=""
1064
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001065 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001066 __check_ignore_image $1
1067 if [ $? -eq 0 ]; then
1068 app_ds=$6
1069 if [ -z "$6" ]; then
1070 app_ds="<app ignored>"
1071 fi
1072 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
1073 # Image is ignored since the corresponding the images is not set in the env file
1074 __remove_included_image $1 # Remove the image from the list of included images
1075 return
1076 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001077 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
1078 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001079 echo ""
1080 tmp=$tmp"<no-image>\t"
1081 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001082
1083 optional_image_repo_target=$image
1084
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001085 #Add repo depending on image type
1086 if [ "$5" == "REMOTE_RELEASE" ]; then
1087 image=$NEXUS_RELEASE_REPO$image
1088 fi
1089 if [ "$5" == "REMOTE" ]; then
1090 image=$NEXUS_STAGING_REPO$image
1091 fi
1092 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
1093 image=$NEXUS_SNAPSHOT_REPO$image
1094 fi
1095 if [ "$5" == "REMOTE_PROXY" ]; then
1096 image=$NEXUS_PROXY_REPO$image
1097 fi
1098 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
1099 image=$NEXUS_RELEASE_REPO_ONAP$image
1100 fi
1101 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
1102 image=$NEXUS_RELEASE_REPO_ORAN$image
1103 fi
1104 #No nexus repo added for local images, tag: LOCAL
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001105 tmp=$tmp$image"\t"
1106 fi
1107 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001108 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
1109 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001110 echo ""
1111 tmp=$tmp"<no-tag>\t"
1112 else
1113 tmp=$tmp$tag
1114 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001115 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001116 echo -e "$tmp" >> $image_list_file
1117 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001118 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 +02001119
1120 remote_or_local_push=false
1121 if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
1122 if [ $5 == "LOCAL" ]; then
1123 remote_or_local_push=true
1124 fi
1125 if [[ $5 == *"REMOTE"* ]]; then
1126 if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
1127 remote_or_local_push=true
1128 fi
1129 fi
1130 fi
1131 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 +02001132 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001133 if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
1134 optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
1135 optional_image_repo_target=$optional_image_repo_target_tmp
1136 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001137 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
1138 else
1139 export "${2}_SOURCE"=""
1140 export "${2}_TARGET"=""
1141 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001142}
1143
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001144# Check if app uses image included in this test run
1145# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001146__check_included_image() {
1147 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001148 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001149 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001150 fi
1151 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001152 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001153}
1154
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001155# Check if app uses a project image
1156# Returns 0 if image is included, 1 if not
1157__check_project_image() {
1158 for im in $PROJECT_IMAGES; do
1159 if [ "$1" == "$im" ]; then
1160 return 0
1161 fi
1162 done
1163 return 1
1164}
1165
1166# Check if app uses image built by the test script
1167# Returns 0 if image is included, 1 if not
1168__check_image_local_build() {
1169 for im in $LOCAL_IMAGE_BUILD; do
1170 if [ "$1" == "$im" ]; then
1171 return 0
1172 fi
1173 done
1174 return 1
1175}
1176
1177# Check if app image is conditionally ignored in this test run
1178# Returns 0 if image is conditionally ignored, 1 if not
1179__check_ignore_image() {
1180 for im in $CONDITIONALLY_IGNORED_IMAGES; do
1181 if [ "$1" == "$im" ]; then
1182 return 0
1183 fi
1184 done
1185 return 1
1186}
1187
1188# Removed image from included list of included images
1189# Used when an image is marked as conditionally ignored
1190__remove_included_image() {
1191 tmp_img_rem_list=""
1192 for im in $INCLUDED_IMAGES; do
1193 if [ "$1" != "$im" ]; then
1194 tmp_img_rem_list=$tmp_img_rem_list" "$im
1195 fi
1196 done
1197 INCLUDED_IMAGES=$tmp_img_rem_list
1198 return 0
1199}
1200
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001201# Check if app is included in the prestarted set of apps
1202# Returns 0 if image is included, 1 if not
1203__check_prestarted_image() {
1204 for im in $KUBE_PRESTARTED_IMAGES; do
1205 if [ "$1" == "$im" ]; then
1206 return 0
1207 fi
1208 done
1209 return 1
1210}
1211
1212# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001213__check_image_local_override() {
1214 for im in $USE_LOCAL_IMAGES; do
1215 if [ "$1" == "$im" ]; then
1216 return 1
1217 fi
1218 done
1219 return 0
1220}
1221
1222# Check if app uses image override
1223# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
1224__check_image_override() {
1225
1226 for im in $ORAN_IMAGES_APP_NAMES; do
1227 if [ "$1" == "$im" ]; then
1228 echo "REMOTE_RELEASE_ORAN"
1229 return 0
1230 fi
1231 done
1232
1233 for im in $ONAP_IMAGES_APP_NAMES; do
1234 if [ "$1" == "$im" ]; then
1235 echo "REMOTE_RELEASE_ONAP"
1236 return 0
1237 fi
1238 done
1239
1240 found=0
1241 for im in $PROJECT_IMAGES_APP_NAMES; do
1242 if [ "$1" == "$im" ]; then
1243 found=1
1244 fi
1245 done
1246
1247 if [ $found -eq 0 ]; then
1248 echo "REMOTE_PROXY"
1249 return 0
1250 fi
1251
1252 suffix=""
1253 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
1254 suffix="REMOTE_RELEASE"
1255 fi
1256 if [ $IMAGE_CATEGORY == "DEV" ]; then
1257 suffix="REMOTE"
1258 fi
1259 CTR=0
1260 for im in $USE_STAGING_IMAGES; do
1261 if [ "$1" == "$im" ]; then
1262 suffix="REMOTE"
1263 ((CTR++))
1264 fi
1265 done
1266 for im in $USE_RELEASE_IMAGES; do
1267 if [ "$1" == "$im" ]; then
1268 suffix="REMOTE_RELEASE"
1269 ((CTR++))
1270 fi
1271 done
1272 for im in $USE_SNAPSHOT_IMAGES; do
1273 if [ "$1" == "$im" ]; then
1274 suffix="REMOTE_SNAPSHOT"
1275 ((CTR++))
1276 fi
1277 done
1278 for im in $USE_LOCAL_IMAGES; do
1279 if [ "$1" == "$im" ]; then
1280 suffix="LOCAL"
1281 ((CTR++))
1282 fi
1283 done
1284 echo $suffix
1285 if [ $CTR -gt 1 ]; then
1286 exit 1
1287 fi
1288 return 0
1289}
1290
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001291# Function to re-tag and image and push to another image repo
1292__retag_and_push_image() {
1293 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1294 source_image="${!1}"
1295 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
1296 target_image="${!trg_var_name}"
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001297
1298 if [ -z $target_image ]; then
1299 return 0 # Image with no target shall not be pushed
1300 fi
1301
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001302 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1303 tmp=$(docker image tag $source_image ${target_image} )
1304 if [ $? -ne 0 ]; then
1305 docker stop $tmp &> ./tmp/.dockererr
1306 ((IMAGE_ERR++))
1307 echo ""
1308 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1309 cat ./tmp/.dockererr
1310 return 1
1311 else
1312 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1313 fi
1314 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1315 tmp=$(docker push ${target_image} )
1316 if [ $? -ne 0 ]; then
1317 docker stop $tmp &> ./tmp/.dockererr
1318 ((IMAGE_ERR++))
1319 echo ""
1320 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1321 cat ./tmp/.dockererr
1322 return 1
1323 else
1324 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1325 fi
1326 export "${1}"=$target_image
1327 fi
1328 return 0
1329}
1330
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001331#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001332#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001333__check_and_pull_image() {
1334
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001335 source_image="${!4}"
1336
1337 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 +01001338 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001339 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001340
1341 if [ $1 == "local" ]; then
1342 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001343 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 +01001344 ((IMAGE_ERR++))
1345 return 1
1346 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001347 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001348 fi
1349 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
1350 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001351 if [ $RUNMODE == "DOCKER" ]; then
1352 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001353 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001354 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1355 docker stop $tmp &> ./tmp/.dockererr
1356 if [ $? -ne 0 ]; then
1357 ((IMAGE_ERR++))
1358 echo ""
1359 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
1360 cat ./tmp/.dockererr
1361 return 1
1362 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001363 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001364 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001365 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001366 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1367 docker rm $tmp &> ./tmp/.dockererr
1368 if [ $? -ne 0 ]; then
1369 ((IMAGE_ERR++))
1370 echo ""
1371 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
1372 cat ./tmp/.dockererr
1373 return 1
1374 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001375 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001376 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
1377 tmp_im=""
1378 else
1379 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001380 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001381 fi
1382 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001383 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001384 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001385 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001386 echo ""
1387 echo -e " Pulling image -$RED could not be pulled"$ERED
1388 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001389 echo $out > ./tmp/.dockererr
1390 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001391 return 1
1392 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001393 echo $out > ./tmp/.dockererr
1394 if [[ $out == *"up to date"* ]]; then
1395 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
1396 elif [[ $out == *"Downloaded newer image"* ]]; then
1397 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
1398 else
1399 echo -e " Pulling image -$GREEN Pulled $EGREEN"
1400 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001401 else
1402 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1403 fi
1404 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001405
1406 __retag_and_push_image $4
1407
1408 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001409}
1410
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001411setup_testenvironment() {
1412 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001413 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001414
1415 # Image var setup for all project images included in the test
1416 for imagename in $APP_SHORT_NAMES; do
1417 __check_included_image $imagename
1418 incl=$?
1419 __check_project_image $imagename
1420 proj=$?
1421 if [ $incl -eq 0 ]; then
1422 if [ $proj -eq 0 ]; then
1423 IMAGE_SUFFIX=$(__check_image_override $imagename)
1424 if [ $? -ne 0 ]; then
1425 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1426 ((IMAGE_ERR++))
1427 fi
1428 else
1429 IMAGE_SUFFIX="none"
1430 fi
1431 # A function name is created from the app short name
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001432 # for example app short name 'ICS' -> produce the function
1433 # name __ICS_imagesetup
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001434 # This function is called and is expected to exist in the imported
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001435 # file for the ics test functions
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001436 # The resulting function impl will call '__check_and_create_image_var' function
1437 # with appropriate parameters
1438 # If the image suffix is none, then the component decides the suffix
1439 function_pointer="__"$imagename"_imagesetup"
1440 $function_pointer $IMAGE_SUFFIX
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01001441
1442 function_pointer="__"$imagename"_test_requirements"
1443 $function_pointer
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001444 fi
1445 done
1446
1447 #Errors in image setting - exit
1448 if [ $IMAGE_ERR -ne 0 ]; then
1449 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001450 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001451
1452 #Print a tables of the image settings
1453 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1454 column -t -s $'\t' $image_list_file | indent1
1455
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001456 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001457
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001458 #Set the SIM_GROUP var
1459 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1460 if [ -z "$SIM_GROUP" ]; then
1461 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1462 if [ ! -d $SIM_GROUP ]; then
1463 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1464 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1465 exit 1
1466 else
1467 echo " SIM_GROUP auto set to: " $SIM_GROUP
1468 fi
1469 elif [ $SIM_GROUP = *simulator_group ]; then
1470 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
1471 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001472 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001473 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1474 fi
1475
1476 echo ""
1477
1478 #Temp var to check for image pull errors
1479 IMAGE_ERR=0
1480
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001481 # Delete namespaces
1482 echo -e $BOLD"Deleting namespaces"$EBOLD
1483
1484
1485 if [ "$DELETE_KUBE_NAMESPACES" -eq 1 ]; then
1486 test_env_namespaces=$(kubectl get ns --no-headers -o custom-columns=":metadata.name" -l autotest=engine) #Get list of ns created by the test env
1487 if [ $? -ne 0 ]; then
1488 echo " Cannot get list of namespaces...ignoring delete"
1489 else
1490 for test_env_ns in $test_env_namespaces; do
1491 __kube_delete_namespace $test_env_ns
1492 done
1493 fi
1494 else
1495 echo " Namespace delete option not set"
1496 fi
1497 echo ""
1498
1499 # Delete containers
1500 echo -e $BOLD"Deleting containers"$EBOLD
1501
1502 if [ "$DELETE_CONTAINERS" -eq 1 ]; then
1503 echo " Stopping containers label 'nrttest_app'..."
1504 docker stop $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
1505 echo " Removing stopped containers..."
1506 docker rm $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
1507 else
1508 echo " Contatiner delete option not set"
1509 fi
1510 echo ""
1511
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001512 # The following sequence pull the configured images
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001513 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001514 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
1515 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 +02001516 else
1517 for imagename in $APP_SHORT_NAMES; do
1518 __check_included_image $imagename
1519 incl=$?
1520 __check_project_image $imagename
1521 proj=$?
1522 if [ $incl -eq 0 ]; then
1523 if [ $proj -eq 0 ]; then
1524 START_ARG_MOD=$START_ARG
1525 __check_image_local_override $imagename
1526 if [ $? -eq 1 ]; then
1527 START_ARG_MOD="local"
1528 fi
1529 else
1530 START_ARG_MOD=$START_ARG
1531 fi
1532 __check_image_local_build $imagename
1533 #No pull of images built locally
1534 if [ $? -ne 0 ]; then
1535 # A function name is created from the app short name
1536 # for example app short name 'HTTPPROXY' -> produce the function
1537 # name __HTTPPROXY_imagesetup
1538 # This function is called and is expected to exist in the imported
1539 # file for the httpproxy test functions
1540 # The resulting function impl will call '__check_and_pull_image' function
1541 # with appropriate parameters
1542 function_pointer="__"$imagename"_imagepull"
1543 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001544 fi
1545 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001546 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001547 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001548 done
1549 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001550
1551 #Errors in image setting - exit
1552 if [ $IMAGE_ERR -ne 0 ]; then
1553 echo ""
1554 echo "#################################################################################################"
1555 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1556 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1557 if [ $IMAGE_CATEGORY == "DEV" ]; then
BjornMagnussonXA720f5d72021-08-13 10:37:23 +02001558 echo ""
1559 echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED
1560 echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED
1561 echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED
1562 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 +01001563 fi
1564 echo "#################################################################################################"
1565 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001566 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001567 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001568
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001569 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001570
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001571 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001572
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001573 for imagename in $APP_SHORT_NAMES; do
1574 cd $AUTOTEST_HOME #Always reset to orig dir
1575 __check_image_local_build $imagename
1576 if [ $? -eq 0 ]; then
1577 __check_included_image $imagename
1578 if [ $? -eq 0 ]; then
1579 # A function name is created from the app short name
1580 # for example app short name 'MR' -> produce the function
1581 # name __MR_imagebuild
1582 # This function is called and is expected to exist in the imported
1583 # file for the mr test functions
1584 # The resulting function impl shall build the imagee
1585 function_pointer="__"$imagename"_imagebuild"
1586 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001587
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001588 else
1589 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1590 fi
1591 fi
1592 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001593
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001594 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001595
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001596 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001597
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001598 # Create a table of the images used in the script - from local repo
1599 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001600
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001601 docker_tmp_file=./tmp/.docker-images-table
1602 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1603 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001604
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001605 for imagename in $APP_SHORT_NAMES; do
1606 __check_included_image $imagename
1607 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001608 # Only print image data if image repo is null, or if image repo is set and image is local
1609 print_image_data=0
1610 if [ -z "$IMAGE_REPO_ADR" ]; then
1611 print_image_data=1
1612 else
1613 __check_image_local_build $imagename
1614 if [ $? -eq 0 ]; then
1615 print_image_data=1
1616 fi
1617 fi
1618 if [ $print_image_data -eq 1 ]; then
1619 # A function name is created from the app short name
1620 # for example app short name 'MR' -> produce the function
1621 # name __MR_imagebuild
1622 # This function is called and is expected to exist in the imported
1623 # file for the mr test functions
1624 # The resulting function impl shall build the imagee
1625 function_pointer="__"$imagename"_image_data"
1626 $function_pointer "$format_string" $docker_tmp_file
1627 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001628 fi
1629 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001630
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001631 column -t -s $'\t' $docker_tmp_file | indent1
1632
1633 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001634
1635 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1636
1637 # Create a table of the images used in the script - from remote repo
1638 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1639 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1640
1641 docker_tmp_file=./tmp/.docker-images-table
1642 format_string="{{.Repository}}\\t{{.Tag}}"
1643 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1644
1645 for imagename in $APP_SHORT_NAMES; do
1646 __check_included_image $imagename
1647 if [ $? -eq 0 ]; then
1648 # Only print image data if image repo is null, or if image repo is set and image is local
1649 __check_image_local_build $imagename
1650 if [ $? -ne 0 ]; then
1651 # A function name is created from the app short name
1652 # for example app short name 'MR' -> produce the function
1653 # name __MR_imagebuild
1654 # This function is called and is expected to exist in the imported
1655 # file for the mr test functions
1656 # The resulting function impl shall build the imagee
1657 function_pointer="__"$imagename"_image_data"
1658 $function_pointer "$format_string" $docker_tmp_file
1659 fi
1660 fi
1661 done
1662
1663 column -t -s $'\t' $docker_tmp_file | indent1
1664
1665 echo ""
1666 fi
1667
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001668 if [ $RUNMODE == "KUBE" ]; then
1669
1670 echo "================================================================================="
1671 echo "================================================================================="
1672
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001673 if [ -z "$IMAGE_REPO_ADR" ]; then
1674 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1675 echo -e " This setting only works on single node clusters on the local machine"
1676 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001677 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001678 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001679 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1680 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1681 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1682 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"
1683 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001684 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001685 CLUSTER_IP=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
1686 echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001687
1688 echo "================================================================================="
1689 echo "================================================================================="
1690 echo ""
1691 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001692
1693 echo -e $BOLD"======================================================="$EBOLD
1694 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1695 echo -e $BOLD"======================================================="$EBOLD
1696 echo ""
1697
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001698 LOG_STAT_ARGS=""
1699
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001700 for imagename in $APP_SHORT_NAMES; do
1701 __check_included_image $imagename
1702 retcode_i=$?
1703 __check_prestarted_image $imagename
1704 retcode_p=$?
1705 if [ $retcode_i -eq 0 ] || [ $retcode_p -eq 0 ]; then
1706 # A function name is created from the app short name
1707 # for example app short name 'RICMSIM' -> produce the function
1708 # name __RICSIM__initial_setup
1709 # This function is called and is expected to exist in the imported
1710 # file for the ricsim test functions
1711 # The resulting function impl shall perform initial setup of port, host etc
1712
1713 function_pointer="__"$imagename"_initial_setup"
1714 $function_pointer
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001715
1716 function_pointer="__"$imagename"_statisics_setup"
1717 LOG_STAT_ARGS=$LOG_STAT_ARGS" "$($function_pointer)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001718 fi
1719 done
1720
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001721 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1722 ../common/genstat.sh $RUNMODE $SECONDS $TESTLOGS/$ATC/stat_data.csv $LOG_STAT_ARGS &
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +01001723 COLLECT_RUNTIME_STATS_PID=$!
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001724 fi
1725
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001726}
YongchaoWu9a84f512019-12-16 22:54:11 +01001727
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001728# Function to print the test result, shall be the last cmd in a test script
1729# args: -
1730# (Function for test scripts)
1731print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001732
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001733 TCTEST_END=$SECONDS
1734 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001735
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001736 echo "-------------------------------------------------------------------------------------------------"
1737 echo "------------------------------------- Test case: "$ATC
1738 echo "------------------------------------- Ended: "$(date)
1739 echo "-------------------------------------------------------------------------------------------------"
1740 echo "-- Description: "$TC_ONELINE_DESCR
1741 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001742 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001743 echo "-------------------------------------------------------------------------------------------------"
1744 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001745 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001746
YongchaoWu21f17bb2020-03-05 12:44:08 +01001747
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001748 if [ $RES_DEVIATION -gt 0 ]; then
1749 echo "Test case deviations"
1750 echo "===================================="
1751 cat $DEVIATION_FILE
1752 fi
1753 echo ""
1754 echo "Timer measurement in the test script"
1755 echo "===================================="
1756 column -t -s $'\t' $TIMER_MEASUREMENTS
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001757 if [ $RES_PASS != $RES_TEST ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001758 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 +01001759 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001760 echo ""
1761
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001762 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1763 echo "Runtime statistics collected in file: "$TESTLOGS/$ATC/stat_data.csv
1764 echo ""
1765 fi
1766
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001767 total=$((RES_PASS+RES_FAIL))
1768 if [ $RES_TEST -eq 0 ]; then
1769 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1770 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1771 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1772 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1773 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1774 elif [ $total != $RES_TEST ]; then
1775 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1776 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1777 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1778 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1779 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1780 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001781 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001782 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1783 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1784 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1785 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1786 elif [ $RES_PASS = $RES_TEST ]; then
1787 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1788 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1789 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1790 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1791 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1792 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001793
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001794 # Update test suite counter
1795 if [ -f .tmp_tcsuite_pass_ctr ]; then
1796 tmpval=$(< .tmp_tcsuite_pass_ctr)
1797 ((tmpval++))
1798 echo $tmpval > .tmp_tcsuite_pass_ctr
1799 fi
1800 if [ -f .tmp_tcsuite_pass ]; then
1801 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1802 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001803 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001804 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001805 echo "0" > "$TESTLOGS/$ATC/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001806 else
1807 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1808 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
1809 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 ""
1813 # Update test suite counter
1814 if [ -f .tmp_tcsuite_fail_ctr ]; then
1815 tmpval=$(< .tmp_tcsuite_fail_ctr)
1816 ((tmpval++))
1817 echo $tmpval > .tmp_tcsuite_fail_ctr
1818 fi
1819 if [ -f .tmp_tcsuite_fail ]; then
1820 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1821 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001822 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001823
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001824 echo "++++ Number of tests: "$RES_TEST
1825 echo "++++ Number of passed tests: "$RES_PASS
1826 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01001827 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001828 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001829 echo ""
1830 echo "++++ Number of test case deviations: "$RES_DEVIATION
1831 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001832 echo "------------------------------------- Test case complete ---------------------------------"
1833 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01001834 echo ""
1835}
1836
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001837#####################################################################
1838###### Functions for start, configuring, stoping, cleaning etc ######
1839#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01001840
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001841# Start timer for time measurement
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001842# args: <timer message to print> - timer value and message will be printed both on screen
1843# and in the timer measurement report - if at least one "print_timer is called"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001844start_timer() {
1845 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001846 TC_TIMER_STARTTIME=$SECONDS
1847 TC_TIMER_TIMER_TEXT="${@:1}"
1848 if [ $# -ne 1 ]; then
1849 __print_err "need 1 arg, <timer message to print>" $@
1850 TC_TIMER_TIMER_TEXT=${FUNCNAME[0]}":"${BASH_LINENO[0]}
1851 echo " Assigning timer name: "$TC_TIMER_TIMER_TEXT
1852 fi
1853 TC_TIMER_CURRENT_FAILS=$(($RES_FAIL+$RES_CONF_FAIL))
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001854 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001855}
1856
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001857# Print the running timer the value of the time (in seconds)
1858# Timer value and message will be printed both on screen and in the timer measurement report
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001859print_timer() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001860 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $TC_TIMER_TIMER_TEXT $EBOLD
1861 if [ -z "$TC_TIMER_STARTTIME" ]; then
1862 __print_err "timer not started" $@
1863 return 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001864 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001865 duration=$(($SECONDS-$TC_TIMER_STARTTIME))
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001866 if [ $duration -eq 0 ]; then
1867 duration="<1 second"
1868 else
1869 duration=$duration" seconds"
1870 fi
1871 echo " Timer duration :" $duration
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001872 res="-"
1873 if [ $(($RES_FAIL+$RES_CONF_FAIL)) -ne $TC_TIMER_CURRENT_FAILS ]; then
1874 res="Failures occured during test - timer not reliabled"
1875 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001876
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001877 echo -e "$TC_TIMER_TIMER_TEXT \t $duration \t $res" >> $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001878}
1879
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001880# Print info about a deviations from intended tests
1881# Each deviation counted is also printed in the testreport
1882# args <deviation message to print>
1883deviation() {
1884 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
1885 if [ $# -lt 1 ]; then
1886 ((RES_CONF_FAIL++))
1887 __print_err "need 1 or more args, <deviation message to print>" $@
1888 exit 1
1889 fi
1890 ((RES_DEVIATION++))
1891 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
1892 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02001893 __print_current_stats
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001894 echo ""
1895}
YongchaoWu21f17bb2020-03-05 12:44:08 +01001896
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001897# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
1898__check_stop_at_error() {
1899 if [ $STOP_AT_ERROR -eq 1 ]; then
1900 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
1901 store_logs "STOP_AT_ERROR"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001902
1903 # Update test suite counter
1904 if [ -f .tmp_tcsuite_fail_ctr ]; then
1905 tmpval=$(< .tmp_tcsuite_fail_ctr)
1906 ((tmpval++))
1907 echo $tmpval > .tmp_tcsuite_fail_ctr
1908 fi
1909 if [ -f .tmp_tcsuite_fail ]; then
1910 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution stopped due to error" >> .tmp_tcsuite_fail
1911 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001912 exit 1
1913 fi
1914 return 0
1915}
1916
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001917# Stop and remove all containers
1918# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001919# (Not for test scripts)
1920__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001921
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001922 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001923
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001924 #Create empty file
1925 running_contr_file="./tmp/running_contr.txt"
1926 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001927
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001928 # Get list of all containers started by the test script
1929 for imagename in $APP_SHORT_NAMES; do
1930 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 +01001931 done
BjornMagnussonXAecf34532021-11-24 08:25:15 +01001932 running_contr_file_empty="No docker containers running, started by previous test execution"
1933 if [ -s $running_contr_file ]; then
1934 running_contr_file_empty=""
1935 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001936
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001937 # Kill all containers started by the test env - to speed up shut down
1938 docker kill $(docker ps -a --filter "label=nrttest_app" --format '{{.Names}}') &> /dev/null
1939
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001940 tab_heading1="App display name"
1941 tab_heading2="App short name"
1942 tab_heading3="Container name"
1943
1944 tab_heading1_len=${#tab_heading1}
1945 tab_heading2_len=${#tab_heading2}
1946 tab_heading3_len=${#tab_heading3}
1947 cntr=0
1948 #Calc field lengths of each item in the list of containers
1949 while read p; do
1950 if (( $cntr % 3 == 0 ));then
1951 if [ ${#p} -gt $tab_heading1_len ]; then
1952 tab_heading1_len=${#p}
1953 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001954 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001955 if (( $cntr % 3 == 1));then
1956 if [ ${#p} -gt $tab_heading2_len ]; then
1957 tab_heading2_len=${#p}
1958 fi
1959 fi
1960 if (( $cntr % 3 == 2));then
1961 if [ ${#p} -gt $tab_heading3_len ]; then
1962 tab_heading3_len=${#p}
1963 fi
1964 fi
1965 let cntr=cntr+1
1966 done <$running_contr_file
1967
1968 let tab_heading1_len=tab_heading1_len+2
1969 while (( ${#tab_heading1} < $tab_heading1_len)); do
1970 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001971 done
1972
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001973 let tab_heading2_len=tab_heading2_len+2
1974 while (( ${#tab_heading2} < $tab_heading2_len)); do
1975 tab_heading2="$tab_heading2"" "
1976 done
1977
1978 let tab_heading3_len=tab_heading3_len+2
1979 while (( ${#tab_heading3} < $tab_heading3_len)); do
1980 tab_heading3="$tab_heading3"" "
1981 done
1982
BjornMagnussonXAecf34532021-11-24 08:25:15 +01001983 if [ ! -z "$running_contr_file_empty" ]; then
1984 echo $running_contr_file_empty | indent1
1985 else
1986 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
1987 cntr=0
1988 while read p; do
1989 if (( $cntr % 3 == 0 ));then
1990 row=""
1991 heading=$p
1992 heading_len=$tab_heading1_len
1993 fi
1994 if (( $cntr % 3 == 1));then
1995 heading=$p
1996 heading_len=$tab_heading2_len
1997 fi
1998 if (( $cntr % 3 == 2));then
1999 contr=$p
2000 heading=$p
2001 heading_len=$tab_heading3_len
2002 fi
2003 while (( ${#heading} < $heading_len)); do
2004 heading="$heading"" "
2005 done
2006 row=$row$heading
2007 if (( $cntr % 3 == 2));then
2008 echo -ne $row$SAMELINE
2009 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
2010 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
2011 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
2012 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
2013 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
2014 fi
2015 let cntr=cntr+1
2016 done <$running_contr_file
2017 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002018
YongchaoWu9a84f512019-12-16 22:54:11 +01002019 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002020
2021 echo -e $BOLD" Removing docker network"$EBOLD
2022 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
2023 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002024 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002025 if [ $? -ne 0 ]; then
2026 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
2027 exit 1
2028 fi
2029 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002030 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002031
2032 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002033 docker network prune --force | indent2
2034 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002035
2036 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002037 docker volume prune --force | indent2
2038 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002039
2040 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
2041 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002042 echo -e "$GREEN Done$EGREEN"
2043 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02002044
2045 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
2046 if [ $? -eq 0 ]; then
2047 if [ $CONTRS -ne 0 ]; then
2048 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002049 docker ps -a | indent1
2050 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02002051 fi
2052 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002053}
2054
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002055###################################
2056### Functions for kube management
2057###################################
2058
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002059# Get resource type for scaling
2060# args: <resource-name> <namespace>
2061__kube_get_resource_type() {
2062 kubectl get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
2063 if [ $? -eq 0 ]; then
2064 echo "deployment"
2065 return 0
2066 fi
2067 kubectl get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
2068 if [ $? -eq 0 ]; then
2069 echo "sts"
2070 return 0
2071 fi
2072 echo "unknown-resource-type"
2073 return 1
2074}
2075
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002076# Scale a kube resource to a specific count
2077# args: <resource-type> <resource-name> <namespace> <target-count>
2078# (Not for test scripts)
2079__kube_scale() {
2080 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
2081 kubectl scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
2082 if [ $? -ne 0 ]; then
2083 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
2084 ((RES_CONF_FAIL++))
2085 echo " Message: $(<./tmp/kubeerr)"
2086 return 1
2087 else
2088 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
2089 fi
2090
2091 TSTART=$SECONDS
2092
2093 for i in {1..500}; do
2094 count=$(kubectl get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
2095 retcode=$?
2096 if [ -z "$count" ]; then
2097 #No value is sometimes returned for some reason, in case the resource has replica 0
2098 count=0
2099 fi
2100 if [ $retcode -ne 0 ]; then
2101 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
2102 ((RES_CONF_FAIL++))
2103 return 1
2104 fi
2105 #echo ""
2106 if [ $count -ne $4 ]; then
2107 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
2108 sleep $i
2109 else
2110 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
2111 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
2112 echo ""
2113 return 0
2114 fi
2115 done
2116 echo ""
2117 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
2118 ((RES_CONF_FAIL++))
2119 return 0
2120}
2121
2122# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
2123# This function does not wait for the resource to reach 0
2124# args: <namespace> <label-name> <label-id>
2125# (Not for test scripts)
2126__kube_scale_all_resources() {
2127 namespace=$1
2128 labelname=$2
2129 labelid=$3
2130 resources="deployment replicaset statefulset"
2131 for restype in $resources; do
2132 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
2133 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002134 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002135 echo -ne " Ordered caling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002136 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002137 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002138 done
2139 fi
2140 done
2141}
2142
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002143# 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 +01002144# This function do wait for the resource to reach 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002145# args: <namespace> <label-name> [ <label-id> ]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002146# (Not for test scripts)
2147__kube_scale_and_wait_all_resources() {
2148 namespace=$1
2149 labelname=$2
2150 labelid=$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002151 if [ -z "$3" ]; then
2152 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname"
2153 else
2154 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname=$labelid"
2155 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002156 resources="deployment replicaset statefulset"
2157 scaled_all=1
2158 while [ $scaled_all -ne 0 ]; do
2159 scaled_all=0
2160 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002161 if [ -z "$3" ]; then
2162 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname')].metadata.name}')
2163 else
2164 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
2165 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002166 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2167 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002168 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002169 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
2170 count=1
2171 T_START=$SECONDS
2172 while [ $count -ne 0 ]; do
2173 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002174 echo -ne " Scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002175 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2176 sleep 0.5
2177 else
2178 count=0
2179 fi
2180 duration=$(($SECONDS-$T_START))
2181 if [ $duration -gt 100 ]; then
2182 #Forcring count 0, to avoid hanging for failed scaling
2183 scaled_all=1
2184 count=0
2185 fi
2186 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002187 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 +01002188 done
2189 fi
2190 done
2191 done
2192}
2193
2194# Remove all kube resources in a namespace for resources having a certain label and label-id
2195# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
2196# args: <namespace> <label-name> <label-id>
2197# (Not for test scripts)
2198__kube_delete_all_resources() {
2199 namespace=$1
2200 labelname=$2
2201 labelid=$3
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002202 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes serviceaccounts clusterrolebindings"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002203 deleted_resourcetypes=""
2204 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002205 ns_flag="-n $namespace"
2206 ns_text="in namespace $namespace"
2207 if [ $restype == "persistentvolumes" ]; then
2208 ns_flag=""
2209 ns_text=""
2210 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002211 if [ $restype == "clusterrolebindings" ]; then
2212 ns_flag=""
2213 ns_text=""
2214 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002215 result=$(kubectl get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002216 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2217 deleted_resourcetypes=$deleted_resourcetypes" "$restype
2218 for resid in $result; do
2219 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
2220 count=1
2221 while [ $count -ne 0 ]; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002222 count=$(kubectl get $restype $resid $ns_flag -o jsonpath='{.status.replicas}' 2> /dev/null)
2223 echo -ne " Scaling $restype $resid $ns_text with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002224 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2225 sleep 0.5
2226 else
2227 count=0
2228 fi
2229 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002230 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 +01002231 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002232 echo -ne " Deleting $restype $resid $ns_text with label $labelname=$labelid "$SAMELINE
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002233 kubectl delete --grace-period=1 $restype $resid $ns_flag 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002234 if [ $? -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002235 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002236 else
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002237 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002238 fi
2239 #fi
2240 done
2241 fi
2242 done
2243 if [ ! -z "$deleted_resourcetypes" ]; then
2244 for restype in $deleted_resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002245 ns_flag="-n $namespace"
2246 ns_text="in namespace $namespace"
2247 if [ $restype == "persistentvolumes" ]; then
2248 ns_flag=""
2249 ns_text=""
2250 fi
2251 echo -ne " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted..."$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002252 T_START=$SECONDS
2253 result="dummy"
2254 while [ ! -z "$result" ]; do
2255 sleep 0.5
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002256 result=$(kubectl get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
2257 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 +01002258 if [ -z "$result" ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002259 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 +01002260 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002261 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 +01002262 result=""
2263 fi
2264 done
2265 done
2266 fi
2267}
2268
2269# Creates a namespace if it does not exists
2270# args: <namespace>
2271# (Not for test scripts)
2272__kube_create_namespace() {
2273
2274 #Check if test namespace exists, if not create it
2275 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
2276 if [ $? -ne 0 ]; then
2277 echo -ne " Creating namespace "$1 $SAMELINE
2278 kubectl create namespace $1 1> /dev/null 2> ./tmp/kubeerr
2279 if [ $? -ne 0 ]; then
2280 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2281 ((RES_CONF_FAIL++))
2282 echo " Message: $(<./tmp/kubeerr)"
2283 return 1
2284 else
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01002285 kubectl label ns $1 autotest=engine
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002286 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2287 fi
2288 else
2289 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
2290 fi
2291 return 0
2292}
2293
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002294# Removes a namespace if it exists
2295# args: <namespace>
2296# (Not for test scripts)
2297__kube_delete_namespace() {
2298
2299 #Check if test namespace exists, if so remove it
2300 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
2301 if [ $? -eq 0 ]; then
2302 echo -ne " Removing namespace "$1 $SAMELINE
2303 kubectl delete namespace $1 1> /dev/null 2> ./tmp/kubeerr
2304 if [ $? -ne 0 ]; then
2305 echo -e " Removing namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2306 ((RES_CONF_FAIL++))
2307 echo " Message: $(<./tmp/kubeerr)"
2308 return 1
2309 else
2310 echo -e " Removing namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2311 fi
2312 else
2313 echo -e " Namespace $1 $GREEN$BOLD does not exist, OK $EBOLD$EGREEN"
2314 fi
2315 return 0
2316}
2317
2318# Removes a namespace
2319# args: <namespace>
2320# (Not for test scripts)
2321clean_and_create_namespace() {
2322 __log_conf_start $@
2323
2324 if [ $# -ne 1 ]; then
2325 __print_err "<namespace>" $@
2326 return 1
2327 fi
2328 __kube_delete_namespace $1
2329 if [ $? -ne 0 ]; then
2330 return 1
2331 fi
2332 __kube_create_namespace $1
2333 if [ $? -ne 0 ]; then
2334 return 1
2335 fi
2336
2337}
2338
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002339# Find the host ip of an app (using the service resource)
2340# args: <app-name> <namespace>
2341# (Not for test scripts)
2342__kube_get_service_host() {
2343 if [ $# -ne 2 ]; then
2344 ((RES_CONF_FAIL++))
2345 __print_err "need 2 args, <app-name> <namespace>" $@
2346 exit 1
2347 fi
2348 for timeout in {1..60}; do
2349 host=$(kubectl get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
2350 if [ $? -eq 0 ]; then
2351 if [ ! -z "$host" ]; then
2352 echo $host
2353 return 0
2354 fi
2355 fi
2356 sleep 0.5
2357 done
2358 ((RES_CONF_FAIL++))
2359 echo "host-not-found-fatal-error"
2360 return 1
2361}
2362
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002363# Find the named port to an app (using the service resource)
2364# args: <app-name> <namespace> <port-name>
2365# (Not for test scripts)
2366__kube_get_service_port() {
2367 if [ $# -ne 3 ]; then
2368 ((RES_CONF_FAIL++))
2369 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2370 exit 1
2371 fi
2372
2373 for timeout in {1..60}; do
2374 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
2375 if [ $? -eq 0 ]; then
2376 if [ ! -z "$port" ]; then
2377 echo $port
2378 return 0
2379 fi
2380 fi
2381 sleep 0.5
2382 done
2383 ((RES_CONF_FAIL++))
2384 echo "0"
2385 return 1
2386}
2387
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002388# Find the named node port to an app (using the service resource)
2389# args: <app-name> <namespace> <port-name>
2390# (Not for test scripts)
2391__kube_get_service_nodeport() {
2392 if [ $# -ne 3 ]; then
2393 ((RES_CONF_FAIL++))
2394 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2395 exit 1
2396 fi
2397
2398 for timeout in {1..60}; do
2399 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
2400 if [ $? -eq 0 ]; then
2401 if [ ! -z "$port" ]; then
2402 echo $port
2403 return 0
2404 fi
2405 fi
2406 sleep 0.5
2407 done
2408 ((RES_CONF_FAIL++))
2409 echo "0"
2410 return 1
2411}
2412
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002413# Create a kube resource from a yaml template
2414# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
2415# (Not for test scripts)
2416__kube_create_instance() {
2417 echo -ne " Creating $1 $2"$SAMELINE
2418 envsubst < $3 > $4
2419 kubectl apply -f $4 1> /dev/null 2> ./tmp/kubeerr
2420 if [ $? -ne 0 ]; then
2421 ((RES_CONF_FAIL++))
2422 echo -e " Creating $1 $2 $RED Failed $ERED"
2423 echo " Message: $(<./tmp/kubeerr)"
2424 return 1
2425 else
2426 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
2427 fi
2428}
2429
2430# Function to create a configmap in kubernetes
2431# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
2432# (Not for test scripts)
2433__kube_create_configmap() {
2434 echo -ne " Creating configmap $1 "$SAMELINE
2435 envsubst < $5 > $5"_tmp"
2436 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
2437 kubectl create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
2438 if [ $? -ne 0 ]; then
2439 echo -e " Creating configmap $1 $RED Failed $ERED"
2440 ((RES_CONF_FAIL++))
2441 return 1
2442 fi
2443
2444 kubectl apply -f $6 1> /dev/null 2> ./tmp/kubeerr
2445 if [ $? -ne 0 ]; then
2446 echo -e " Creating configmap $1 $RED Apply failed $ERED"
2447 echo " Message: $(<./tmp/kubeerr)"
2448 ((RES_CONF_FAIL++))
2449 return 1
2450 fi
2451 kubectl label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
2452 if [ $? -ne 0 ]; then
2453 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
2454 echo " Message: $(<./tmp/kubeerr)"
2455 ((RES_CONF_FAIL++))
2456 return 1
2457 fi
2458 # Log the resulting map
2459 kubectl get configmap $1 -n $2 -o yaml > $6
2460
2461 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
2462 return 0
2463}
2464
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002465# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
2466# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002467# args: <full kubectl cmd with parameters>
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002468# (Not for test scripts)
2469__kube_cmd_with_timeout() {
2470 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
2471
2472 while true; do
2473 kube_cmd_result=$($@)
2474 if [ $? -ne 0 ]; then
2475 kube_cmd_result=""
2476 fi
2477 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
2478 echo $kube_cmd_result
2479 return 0
2480 fi
2481 sleep 1
2482 done
2483}
2484
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002485# This function starts a pod that cleans a the contents of a path mounted as a pvc
2486# After this action the pod should terminate
2487# This should only be executed when the pod owning the pvc is not running
2488# args: <appname> <namespace> <pvc-name> <path-to remove>
2489# (Not for test scripts)
2490__kube_clean_pvc() {
2491
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002492 #using env vars setup in pvccleaner_api_functions.sh
2493
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002494 export PVC_CLEANER_NAMESPACE=$2
2495 export PVC_CLEANER_CLAIMNAME=$3
2496 export PVC_CLEANER_RM_PATH=$4
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01002497 export PVC_CLEANER_APP_NAME
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002498 input_yaml=$SIM_GROUP"/"$PVC_CLEANER_COMPOSE_DIR"/"pvc-cleaner.yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002499 output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml
2500
2501 envsubst < $input_yaml > $output_yaml
2502
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002503 kubectl delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002504
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002505 __kube_create_instance pod $PVC_CLEANER_APP_NAME $input_yaml $output_yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002506 if [ $? -ne 0 ]; then
2507 echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work"
2508 return 1
2509 fi
2510
2511 term_ts=$(($SECONDS+30))
2512 while [ $term_ts -gt $SECONDS ]; do
2513 pod_status=$(kubectl get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase")
2514 if [ "$pod_status" == "Succeeded" ]; then
2515 return 0
2516 fi
2517 done
2518 return 1
2519}
2520
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002521# This function scales or deletes all resources for app selected by the testcase.
2522# args: -
2523# (Not for test scripts)
2524__clean_kube() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002525 echo -e $BOLD"Initialize kube pods/statefulsets/replicaset to initial state"$EBOLD
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002526
2527 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002528 for imagename in $APP_SHORT_NAMES; do
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002529 # A function name is created from the app short name
2530 # for example app short name 'RICMSIM' -> produce the function
2531 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
2532 # This function is called and is expected to exist in the imported
2533 # file for the ricsim test functions
2534 # The resulting function impl shall scale the resources to 0
2535 # For prestarted apps, the function waits until the resources are 0
2536 # For included (not prestated) apps, the scaling is just ordered
2537 __check_prestarted_image $imagename
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002538 if [ $? -eq 0 ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002539 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002540 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2541 $function_pointer
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002542 else
2543 __check_included_image $imagename
2544 if [ $? -eq 0 ]; then
2545 function_pointer="__"$imagename"_kube_scale_zero"
2546 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2547 $function_pointer
2548 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002549 fi
2550 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002551
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002552 # Delete managed apps
2553 for imagename in $APP_SHORT_NAMES; do
2554 __check_included_image $imagename
2555 if [ $? -eq 0 ]; then
2556 __check_prestarted_image $imagename
2557 if [ $? -ne 0 ]; then
2558 # A function name is created from the app short name
2559 # for example app short name 'RICMSIM' -> produce the function
2560 # name __RICSIM__kube_delete_all
2561 # This function is called and is expected to exist in the imported
2562 # file for the ricsim test functions
2563 # The resulting function impl shall delete all its resources
2564 function_pointer="__"$imagename"_kube_delete_all"
2565 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
2566 $function_pointer
2567 fi
2568 fi
2569 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002570
2571 echo ""
2572}
2573
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002574# Function stop and remove all containers (docker) and services/deployments etc(kube)
2575# args: -
2576# Function for test script
2577clean_environment() {
2578 if [ $RUNMODE == "KUBE" ]; then
2579 __clean_kube
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002580 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002581 echo " Cleaning docker resouces to free up resources, may take time..."
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002582 ../common/clean_docker.sh 2>&1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002583 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002584 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002585 else
2586 __clean_containers
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002587 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002588 echo " Cleaning kubernetes resouces to free up resources, may take time..."
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002589 ../common/clean_kube.sh 2>&1 /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002590 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002591 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002592 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002593}
2594
2595# 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 +01002596# args: -
2597# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002598auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002599 echo
2600 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002601 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
2602 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01002603 fi
2604}
2605
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002606# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002607# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002608# (Function for test scripts)
2609sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01002610
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002611 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
2612 if [ $# -lt 1 ]; then
2613 ((RES_CONF_FAIL++))
2614 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
2615 exit 1
2616 fi
2617 #echo "---- Sleep for " $1 " seconds ---- "$2
2618 start=$SECONDS
2619 duration=$((SECONDS-start))
2620 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002621 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002622 sleep 1
2623 duration=$((SECONDS-start))
2624 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002625 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002626 echo ""
2627}
2628
2629# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2630# Not to be called from the test script itself.
2631__print_err() {
2632 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2633 if [ $# -gt 1 ]; then
2634 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2635 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002636 ((RES_CONF_FAIL++))
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01002637 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002638}
2639
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002640# Function to create the docker network for the test
2641# Not to be called from the test script itself.
2642__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002643 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002644 if [ $? -ne 0 ]; then
2645 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2646 return 1
2647 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002648 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002649 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2650 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002651 if [ $? -ne 0 ]; then
2652 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2653 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002654 else
2655 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002656 fi
2657 else
2658 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2659 fi
2660}
2661
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002662# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002663# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2664#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002665# (Not for test scripts)
2666__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002667
2668 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002669 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002670 __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 +01002671 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002672 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002673
2674 __create_docker_network
2675
2676 curdir=$PWD
2677 cd $SIM_GROUP
2678 compose_dir=$1
2679 cd $1
2680 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002681 compose_file=$1
2682 if [ -z "$compose_file" ]; then
2683 compose_file="docker-compose.yml"
2684 fi
2685 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002686 compose_args=$1
2687 shift
2688 appcount=$1
2689 shift
2690
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002691 envsubst < $compose_file > "gen_"$compose_file
2692 compose_file="gen_"$compose_file
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002693 if [ $DOCKER_COMPOSE_VERION == "V1" ]; then
2694 docker_compose_cmd="docker-compose"
2695 else
2696 docker_compose_cmd="docker compose"
2697 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002698
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002699 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002700 $docker_compose_cmd -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002701 if [ $? -ne 0 ]; then
2702 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2703 cat .dockererr
2704 echo -e $RED"Stopping script...."$ERED
2705 exit 1
2706 fi
2707 else
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002708 $docker_compose_cmd -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002709 if [ $? -ne 0 ]; then
2710 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2711 cat .dockererr
2712 echo -e $RED"Stopping script...."$ERED
2713 exit 1
2714 fi
2715 fi
2716
2717 cd $curdir
2718
2719 appindex=0
2720 while [ $appindex -lt $appcount ]; do
2721 appname=$1
2722 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002723 app_started=0
2724 for i in {1..10}; do
2725 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002726 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 +02002727 app_started=1
2728 break
2729 else
2730 sleep $i
2731 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002732 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002733 if [ $app_started -eq 0 ]; then
2734 ((RES_CONF_FAIL++))
2735 echo ""
2736 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002737 echo -e $RED" Stopping script..."$ERED
2738 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002739 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002740 let appindex=appindex+1
2741 done
2742 return 0
2743}
2744
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002745# Function to check if container/service is responding to http/https
2746# args: <container-name>|<service-name> url
2747# (Not for test scripts)
2748__check_service_start() {
2749
2750 if [ $# -ne 2 ]; then
2751 ((RES_CONF_FAIL++))
2752 __print_err "need 2 args, <container-name>|<service-name> url" $@
2753 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002754 fi
2755
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002756 if [ $RUNMODE == "KUBE" ]; then
2757 ENTITY="service/set/deployment"
2758 else
2759 ENTITY="container"
2760 fi
2761 appname=$1
2762 url=$2
2763 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2764
2765
YongchaoWu9a84f512019-12-16 22:54:11 +01002766 pa_st=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002767 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002768 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002769 loop_ctr=0
2770 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002771 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002772 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002773 if [ ${#result} -gt 15 ]; then
2774 #If response is too long, truncate
2775 result="...response text too long, omitted"
2776 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002777 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2778 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 +01002779 pa_st=true
2780 break
2781 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002782 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002783 TS_OFFSET=$loop_ctr
2784 if (( $TS_OFFSET > 5 )); then
2785 TS_OFFSET=5
2786 fi
2787 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
2788 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 +02002789 sleep 1
2790 done
YongchaoWu9a84f512019-12-16 22:54:11 +01002791 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002792 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01002793 done
2794
2795 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002796 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002797 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 +01002798 return 1
2799 fi
2800
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002801 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002802 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002803}
2804
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002805
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002806#################
2807### Log functions
2808#################
2809
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002810__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002811
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002812 dispname=$1
2813 appname=$2
2814 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002815 warning=$4
2816 error=$5
2817
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002818 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
2819
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002820 if [ $RUNMODE == "KUBE" ]; then
2821 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
2822 return
2823 fi
2824
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002825 #tmp=$(docker ps | grep $appname)
2826 tmp=$(docker ps -q --filter name=$appname) #get the container id
2827 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002828 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002829 return
2830 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002831 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002832 if [ $? -ne 0 ];then
2833 echo " Problem to search $appname log $logpath"
2834 else
2835 if [ $foundentries -eq 0 ]; then
2836 echo " No WARN entries found in $appname log $logpath"
2837 else
2838 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
2839 fi
2840 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002841 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002842 if [ $? -ne 0 ];then
2843 echo " Problem to search $appname log $logpath"
2844 else
2845 if [ $foundentries -eq 0 ]; then
2846 echo " No ERR entries found in $appname log $logpath"
2847 else
2848 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
2849 fi
2850 fi
2851 echo ""
2852}
2853
2854# Store all container logs and other logs in the log dir for the script
2855# Logs are stored with a prefix in case logs should be stored several times during a test
2856# args: <logfile-prefix>
2857# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01002858store_logs() {
2859 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002860 ((RES_CONF_FAIL++))
2861 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01002862 exit 1
2863 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002864 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 +01002865
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002866 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002867
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002868 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002869
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002870 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
2871
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002872 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002873
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002874 # Store docker logs for all container
2875 for imagename in $APP_SHORT_NAMES; do
2876 __check_included_image $imagename
2877 if [ $? -eq 0 ]; then
2878 # A function name is created from the app short name
2879 # for example app short name 'RICMSIM' -> produce the function
2880 # name __RICSIM__store_docker_logs
2881 # This function is called and is expected to exist in the imported
2882 # file for the ricsim test functions
2883 # The resulting function impl shall store the docker logs for each container
2884 function_pointer="__"$imagename"_store_docker_logs"
2885 $function_pointer "$TESTLOGS/$ATC/" $1
2886 fi
2887 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002888 fi
2889 if [ $RUNMODE == "KUBE" ]; then
2890 namespaces=$(kubectl get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
2891 for nsid in $namespaces; do
2892 pods=$(kubectl get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
2893 for podid in $pods; do
2894 kubectl logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
2895 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002896 done
2897 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002898 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01002899}
2900
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002901###############
2902## Generic curl
2903###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002904# Generic curl function, assumes all 200-codes are ok
2905# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002906# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
2907# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01002908__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002909 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002910 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002911 if [ ! -z "$KUBE_PROXY_PATH" ]; then
2912 if [ $KUBE_PROXY_HTTPX == "http" ]; then
2913 proxyflag=" --proxy $KUBE_PROXY_PATH"
2914 else
2915 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002916 fi
2917 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002918 curlString="curl -skw %{http_code} $proxyflag $@"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002919 echo " CMD: $curlString" >> $HTTPLOG
2920 res=$($curlString)
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002921 retcode=$?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002922 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002923 echo " RETCODE: $retcode" >> $HTTPLOG
2924 if [ $retcode -ne 0 ]; then
2925 echo "<no-response-from-server>"
2926 return 1
2927 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002928 http_code="${res:${#res}-3}"
2929 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002930 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
2931 echo "<no-response-from-server>"
2932 return 1
2933 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002934 return 0
2935 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002936 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002937 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01002938 echo "<not found, resp:${http_code}>"
2939 return 1
2940 fi
2941 if [ $# -eq 2 ]; then
2942 echo "${res:0:${#res}-3}" | xargs
2943 else
2944 echo "${res:0:${#res}-3}"
2945 fi
2946
2947 return 0
2948 fi
2949}
2950
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002951#######################################
2952### Basic helper function for test cases
2953#######################################
2954
2955# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
2956# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
2957# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
2958# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
2959# before setting pass or fail depending on the result of comparing variable and target using the operator.
2960# 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.
2961# Not to be called from test script.
2962
2963__var_test() {
2964 checkjsonarraycount=0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002965 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002966 if [ $# -eq 6 ]; then
2967 if [[ $3 == "json:"* ]]; then
2968 checkjsonarraycount=1
2969 fi
2970
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002971 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02002972 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG
2973
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002974 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002975 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002976 start=$SECONDS
2977 ctr=0
2978 for (( ; ; )); do
2979 if [ $checkjsonarraycount -eq 0 ]; then
2980 result="$(__do_curl $2$3)"
2981 retcode=$?
2982 result=${result//[[:blank:]]/} #Strip blanks
2983 else
2984 path=${3:5}
2985 result="$(__do_curl $2$path)"
2986 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002987 echo "$result" > ./tmp/.tmp.curl.json
2988 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002989 fi
2990 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002991 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002992 let ctr=ctr+1
2993 if [ $retcode -ne 0 ]; then
2994 if [ $duration -gt $6 ]; then
2995 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002996 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002997 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002998 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002999 return
3000 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003001 elif [ "$4" == "=" ] && [ "$result" -eq $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003002 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003003 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003004 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003005 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003006 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003007 elif [ "$4" == ">" ] && [ "$result" -gt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003008 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003009 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003010 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003011 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003012 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003013 elif [ "$4" == "<" ] && [ "$result" -lt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003014 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003015 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003016 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003017 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003018 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003019 elif [ "$4" == ">=" ] && [ "$result" -ge $5 ]; then
3020 ((RES_PASS++))
3021 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
3022 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
3023 __print_current_stats
3024 return
3025 elif [ "$4" == "contain_str" ] && [[ $result =~ $5 ]]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003026 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003027 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003028 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003029 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003030 return
3031 else
3032 if [ $duration -gt $6 ]; then
3033 ((RES_FAIL++))
3034 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003035 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003036 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003037 return
3038 fi
3039 fi
3040 sleep 1
3041 done
3042 elif [ $# -eq 5 ]; then
3043 if [[ $3 == "json:"* ]]; then
3044 checkjsonarraycount=1
3045 fi
3046
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003047 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02003048 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003049 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003050 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003051 if [ $checkjsonarraycount -eq 0 ]; then
3052 result="$(__do_curl $2$3)"
3053 retcode=$?
3054 result=${result//[[:blank:]]/} #Strip blanks
3055 else
3056 path=${3:5}
3057 result="$(__do_curl $2$path)"
3058 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003059 echo "$result" > ./tmp/.tmp.curl.json
3060 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003061 fi
3062 if [ $retcode -ne 0 ]; then
3063 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003064 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003065 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003066 __check_stop_at_error
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003067 elif [ "$4" == "=" ] && [ "$result" -eq $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003068 ((RES_PASS++))
3069 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003070 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003071 elif [ "$4" == ">" ] && [ "$result" -gt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003072 ((RES_PASS++))
3073 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003074 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003075 elif [ "$4" == "<" ] && [ "$result" -lt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003076 ((RES_PASS++))
3077 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003078 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003079 elif [ "$4" == ">=" ] && [ "$result" -ge $5 ]; then
3080 ((RES_PASS++))
3081 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
3082 __print_current_stats
3083 elif [ "$4" == "contain_str" ] && [[ $result =~ $5 ]]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003084 ((RES_PASS++))
3085 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003086 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003087 else
3088 ((RES_FAIL++))
3089 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003090 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003091 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003092 fi
3093 else
3094 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
3095 echo "Got:" $@
3096 exit 1
3097 fi
3098}