blob: b13b479f013303cf2b3182d7ce116a9eceb9875e [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>+]"
BjornMagnussonXAe85223d2022-06-15 10:19:00 +020029 echo " [--use-staging-image <app-nam>+] [--use-release-image <app-nam>+] [--use-external-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]"
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +010032 echo " [--delete-containers] [--endpoint-stats] [--kubeconfig <config-file>] [--host-path-dir <local-host-dir>]"
BjornMagnussonXA145762b2022-03-22 10:35:10 +010033 echo " [--kubecontext <context-name>] [--docker-host <docker-host-url>] [--docker-proxy <host-or-ip>]"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010034}
35
36if [ $# -eq 1 ] && [ "$1" == "help" ]; then
37
38 if [ ! -z "$TC_ONELINE_DESCR" ]; then
39 echo "Test script description:"
40 echo $TC_ONELINE_DESCR
41 echo ""
42 fi
43 __print_args
44 echo ""
45 echo "remote - Use images from remote repositories. Can be overridden for individual images using the '--use_xxx' flags"
46 echo "remote-remove - Same as 'remote' but will also try to pull fresh images from remote repositories"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010047 echo "docker - Test executed in docker environment"
48 echo "kube - Test executed in kubernetes environment - requires an already started kubernetes environment"
BjornMagnussonXA663566c2021-11-08 10:25:07 +010049 echo "--env-file <file> - The script will use the supplied file to read environment variables from"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010050 echo "release - If this flag is given the script will use release version of the images"
51 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."
52 echo "--stop-at-error - The script will stop when the first failed test or configuration"
53 echo "--ricsim-prefix - The a1 simulator will use the supplied string as container prefix instead of 'ricsim'"
54 echo "--use-local-image - The script will use local images for the supplied apps, space separated list of app short names"
55 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"
56 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"
57 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"
BjornMagnussonXAe85223d2022-06-15 10:19:00 +020058 echo "--use-external-image - The script will use images from the external (non oran/onap) repo for the supplied apps, space separated list of app short names"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +020059 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 +020060 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 +020061 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 +020062 echo "--print-stats - Print current test stats after each test."
BjornMagnussonXA663566c2021-11-08 10:25:07 +010063 echo "--override <file> - Override setting from the file supplied by --env-file"
64 echo "--pre-clean - Will clean kube resouces when running docker and vice versa"
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +010065 echo "--gen-stats - Collect container/pod runtime statistics"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +010066 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."
67 echo "--delete-containers - Delete docker containers before starting tests - but only those created by the test scripts. Docker mode only."
68 echo "--endpoint-stats - Collect endpoint statistics"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +010069 echo "--kubeconfig - Configure kubectl to use cluster specific cluster config file"
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +010070 echo "--host-path-dir - (Base-)path on local-hostmounted to all VMs (nodes), for hostpath volumes in kube"
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +010071 echo "--kubecontext - Configure kubectl to use a certain context, e.g 'minikube'"
BjornMagnussonXA145762b2022-03-22 10:35:10 +010072 echo "--docker-host - Configure docker to use docker in e.g. a VM"
73 echo "--docker-proxy - Configure ip/host to docker when docker is running in a VM"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010074 echo ""
75 echo "List of app short names supported: "$APP_SHORT_NAMES
76 exit 0
77fi
78
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010079AUTOTEST_HOME=$PWD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020080# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
81# FTC1.sh -> ATC == FTC1
82ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
83
84#Create result file (containing '1' for error) for this test case
85#Will be replaced with a file containing '0' if all test cases pass
86echo "1" > "$PWD/.result$ATC.txt"
87
BjornMagnussonXA80a92002020-03-19 14:31:06 +010088#Formatting for 'echo' cmd
89BOLD="\033[1m"
90EBOLD="\033[0m"
91RED="\033[31m\033[1m"
92ERED="\033[0m"
93GREEN="\033[32m\033[1m"
94EGREEN="\033[0m"
95YELLOW="\033[33m\033[1m"
96EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020097SAMELINE="\033[0K\r"
98
BjornMagnussonXA80a92002020-03-19 14:31:06 +010099# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200100echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100101
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200102# default test environment variables
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200103TEST_ENV_VAR_FILE=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100104#Override env file, will be added on top of the above file
105TEST_ENV_VAR_FILE_OVERRIDE=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100106
107echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
108
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100109# Var to hold 'auto' in case containers shall be stopped when test case ends
110AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +0100111
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100112# Var to indicate pre clean, if flag --pre-clean is set the script will clean kube resouces when running docker and vice versa
113PRE_CLEAN="0"
114
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100115# Var to hold the app names to use local images for
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200116USE_LOCAL_IMAGES=""
117
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100118# Var to hold the app names to use remote snapshot images for
119USE_SNAPSHOT_IMAGES=""
120
121# Var to hold the app names to use remote staging images for
122USE_STAGING_IMAGES=""
123
124# Var to hold the app names to use remote release images for
125USE_RELEASE_IMAGES=""
126
BjornMagnussonXAe85223d2022-06-15 10:19:00 +0200127# Var to hold the app names to use external release images for
128USE_EXTERNAL_IMAGES=""
BjornMagnussonXAad047782020-06-08 15:54:11 +0200129
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200130# 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
131STOP_AT_ERROR=0
132
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100133# The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used.
134# The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used
135# Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE
136IMAGE_CATEGORY="DEV"
137
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100138#Var to indicate docker-compose version, V1 or V2
139#V1 names replicated containers <proj-name>_<service-name>_<index>
140#V2 names replicated containers <proj-name>-<service-name>-<index>
BjornMagnussonXAcd938442022-05-11 10:01:24 +0200141DOCKER_COMPOSE_VERSION="V1"
BjornMagnussonXA51f04f02021-11-23 09:22:35 +0100142
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200143# Function to indent cmd output with one space
144indent1() { sed 's/^/ /'; }
145
146# Function to indent cmd output with two spaces
147indent2() { sed 's/^/ /'; }
148
YongchaoWu9a84f512019-12-16 22:54:11 +0100149# Set a description string for the test case
150if [ -z "$TC_ONELINE_DESCR" ]; then
151 TC_ONELINE_DESCR="<no-description>"
152 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
153fi
154
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100155# Counter for test suites
156if [ -f .tmp_tcsuite_ctr ]; then
157 tmpval=$(< .tmp_tcsuite_ctr)
158 ((tmpval++))
159 echo $tmpval > .tmp_tcsuite_ctr
160fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100161
YongchaoWu9a84f512019-12-16 22:54:11 +0100162# Create the logs dir if not already created in the current dir
163if [ ! -d "logs" ]; then
164 mkdir logs
165fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100166TESTLOGS=$PWD/logs
167
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200168# Create the tmp dir for temporary files that is not needed after the test
169# hidden files for the test env is still stored in the current dir
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100170# files in the ./tmp is moved to ./tmp/prev when a new test is started
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200171if [ ! -d "tmp" ]; then
172 mkdir tmp
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100173 if [ $? -ne 0 ]; then
174 echo "Cannot create dir for temp files, $PWD/tmp"
175 echo "Exiting...."
176 exit 1
177 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200178fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100179curdir=$PWD
180cd tmp
181if [ $? -ne 0 ]; then
182 echo "Cannot cd to $PWD/tmp"
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100183 echo "Exiting...."
184 exit 1
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100185fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100186
187TESTENV_TEMP_FILES=$PWD
188
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100189if [ ! -d "prev" ]; then
190 mkdir prev
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100191 if [ $? -ne 0 ]; then
192 echo "Cannot create dir for previous temp files, $PWD/prev"
193 echo "Exiting...."
194 exit 1
195 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100196fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100197
198TMPFILES=$(ls -A | grep -vw prev)
199if [ ! -z "$TMPFILES" ]; then
200 cp -r $TMPFILES prev #Move all temp files to prev dir
201 if [ $? -ne 0 ]; then
202 echo "Cannot move temp files in $PWD to previous temp files in, $PWD/prev"
203 echo "Exiting...."
204 exit 1
205 fi
206 if [ $(pwd | xargs basename) == "tmp" ]; then #Check that current dir is tmp...for safety
207
208 rm -rf $TMPFILES # Remove all temp files
209 fi
210fi
211
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100212cd $curdir
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +0100213if [ $? -ne 0 ]; then
214 echo "Cannot cd to $curdir"
215 echo "Exiting...."
216 exit 1
217fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200218
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100219
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100220# Create a http message log for this testcase
221HTTPLOG=$PWD"/.httplog_"$ATC".txt"
222echo "" > $HTTPLOG
223
224# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100225mkdir -p $TESTLOGS/$ATC
226
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100227# Save create for current logs
228mkdir -p $TESTLOGS/$ATC/previous
229
230rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
231rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
232rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
233
234mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
235mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
236mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
237
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100238# Clear the log dir for the test case
239rm $TESTLOGS/$ATC/*.log &> /dev/null
240rm $TESTLOGS/$ATC/*.txt &> /dev/null
241rm $TESTLOGS/$ATC/*.json &> /dev/null
242
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100243#Create result file in the log dir
244echo "1" > "$TESTLOGS/$ATC/.result$ATC.txt"
245
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100246# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100247TCLOG=$TESTLOGS/$ATC/TC.log
248exec &> >(tee ${TCLOG})
249
BjornMagnussonXAa27463f2022-11-21 15:34:30 +0100250echo $(date) > $TESTLOGS/$ATC/endpoint_tc_start.log
251echo "Test failed" > $TESTLOGS/$ATC/endpoint_tc_end.log # Will be overritten if test is ok
252
YongchaoWu9a84f512019-12-16 22:54:11 +0100253#Variables for counting tests as well as passed and failed tests
254RES_TEST=0
255RES_PASS=0
256RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100257RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200258RES_DEVIATION=0
259
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200260#Var to control if current stats shall be printed
261PRINT_CURRENT_STATS=0
262
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100263#Var to control if container/pod runtim statistics shall be collected
264COLLECT_RUNTIME_STATS=0
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +0100265COLLECT_RUNTIME_STATS_PID=0
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100266
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100267#Var to control if endpoint statistics shall be collected
268COLLECT_ENDPOINT_STATS=0
269
270#Var to control if namespaces shall be delete before test setup
271DELETE_KUBE_NAMESPACES=0
272
273#Var to control if containers shall be delete before test setup
274DELETE_CONTAINERS=0
275
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100276#Var to configure kubectl from a config file or context
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +0100277KUBECONF=""
278
BjornMagnussonXA145762b2022-03-22 10:35:10 +0100279#Localhost, may be set to another host/ip by cmd parameter
280LOCALHOST_NAME="localhost"
281
282#Reseting vars related to token/keys used by kubeproxy when istio is enabled
283#The vars are populated if istio is used in the testcase
284KUBE_PROXY_CURL_JWT=""
285KUBE_PROXY_ISTIO_JWKS_KEYS=""
286
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +0100287#Var pointing to dir mounted to each kubernetes node (master and workers)
288#Persistent volumes using "hostpath" are allocated beneath the point.
289#Typically it is a dir on local host mounted to each VM running the master and worker.
290#So the intention is make this dir available so the PODs can be restarted on any
291#node and still access the persistent data
292#If not set from cmd line, the path is defaults to "/tmp"
293HOST_PATH_BASE_DIR=""
294
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200295#File to keep deviation messages
296DEVIATION_FILE=".tmp_deviations"
297rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100298
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100299# Trap "command not found" and make the script fail
300trap_fnc() {
301
302 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100303 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100304 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200305 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100306 fi
307}
308trap trap_fnc ERR
309
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +0100310# Trap to kill subprocess for stats collection (if running)
311trap_fnc2() {
312 if [ $COLLECT_RUNTIME_STATS_PID -ne 0 ]; then
313 kill $COLLECT_RUNTIME_STATS_PID
314 fi
315}
316trap trap_fnc2 EXIT
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100317
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100318# Counter for tests
319TEST_SEQUENCE_NR=1
320
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100321# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100322__log_test_start() {
323 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
324 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
325 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
326 ((RES_TEST++))
327 ((TEST_SEQUENCE_NR++))
328}
329
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200330# Function to print current statistics
331__print_current_stats() {
332 if [ $PRINT_CURRENT_STATS -ne 0 ]; then
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +0200333 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 +0200334 fi
335}
336
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100337# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100338__log_test_fail_general() {
339 echo -e $RED" FAIL."$1 $ERED
340 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200341 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100342 __check_stop_at_error
343}
344
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100345# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100346__log_test_fail_status_code() {
347 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
348 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200349 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100350 __check_stop_at_error
351}
352
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100353# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100354__log_test_fail_body() {
355 echo -e $RED" FAIL, returned body not correct"$ERED
356 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200357 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100358 __check_stop_at_error
359}
360
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100361# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100362__log_test_fail_not_supported() {
363 echo -e $RED" FAIL, function not supported"$ERED
364 ((RES_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200365 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100366 __check_stop_at_error
367}
368
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100369# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100370__log_test_pass() {
371 if [ $# -gt 0 ]; then
372 echo $@
373 fi
374 ((RES_PASS++))
375 echo -e $GREEN" PASS"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200376 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100377}
378
379#Counter for configurations
380CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100381
382# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100383__log_conf_start() {
384 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
385 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
386 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
387 ((CONF_SEQUENCE_NR++))
388}
389
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100390# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100391__log_conf_fail_general() {
392 echo -e $RED" FAIL."$1 $ERED
393 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200394 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100395 __check_stop_at_error
396}
397
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100398# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100399__log_conf_fail_status_code() {
400 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
401 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200402 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100403 __check_stop_at_error
404}
405
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100406# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100407__log_conf_fail_body() {
408 echo -e $RED" FAIL, returned body not correct"$ERED
409 ((RES_CONF_FAIL++))
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200410 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100411 __check_stop_at_error
412}
413
BjornMagnussonXA83a750f2021-09-21 20:39:58 +0200414# Function to log a configuration that is not supported
415__log_conf_fail_not_supported() {
416 echo -e $RED" FAIL, function not supported"$ERED$@
417 ((RES_CONF_FAIL++))
418 __print_current_stats
419 __check_stop_at_error
420}
421
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100422# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100423__log_conf_ok() {
424 if [ $# -gt 0 ]; then
425 echo $@
426 fi
427 echo -e $GREEN" OK"$EGREEN
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200428 __print_current_stats
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100429}
430
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100431# Function to collect stats on endpoints
432# args: <app-id> <end-point-no> <http-operation> <end-point-url> <http-status> [<count>]
433__collect_endpoint_stats() {
434 if [ $COLLECT_ENDPOINT_STATS -eq 0 ]; then
435 return
436 fi
437 ENDPOINT_COUNT=1
438 if [ $# -gt 5 ]; then
439 ENDPOINT_COUNT=$6
440 fi
441 ENDPOINT_STAT_FILE=$TESTLOGS/$ATC/endpoint_$ATC_$1_$2".log"
442 ENDPOINT_POS=0
443 ENDPOINT_NEG=0
444 if [ -f $ENDPOINT_STAT_FILE ]; then
445 ENDPOINT_VAL=$(< $ENDPOINT_STAT_FILE)
446 ENDPOINT_POS=$(echo $ENDPOINT_VAL | cut -f4 -d ' ' | cut -f1 -d '/')
447 ENDPOINT_NEG=$(echo $ENDPOINT_VAL | cut -f5 -d ' ' | cut -f1 -d '/')
448 fi
449
450 if [ $5 -ge 200 ] && [ $5 -lt 300 ]; then
451 let ENDPOINT_POS=ENDPOINT_POS+$ENDPOINT_COUNT
452 else
453 let ENDPOINT_NEG=ENDPOINT_NEG+$ENDPOINT_COUNT
454 fi
455
456 printf '%-2s %-10s %-45s %-16s %-16s' "#" "$3" "$4" "$ENDPOINT_POS/$ENDPOINT_POS" "$ENDPOINT_NEG/$ENDPOINT_NEG" > $ENDPOINT_STAT_FILE
457}
458
459# Function to collect stats on endpoints
460# args: <app-id> <image-info>
461__collect_endpoint_stats_image_info() {
462 if [ $COLLECT_ENDPOINT_STATS -eq 0 ]; then
463 return
464 fi
465 ENDPOINT_STAT_FILE=$TESTLOGS/$ATC/imageinfo_$ATC_$1".log"
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100466 echo $A1PMS_IMAGE > $ENDPOINT_STAT_FILE
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100467}
468
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100469#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100470TCTEST_START=$SECONDS
471
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100472#Vars to hold the start time and timer text for a custom timer
473TC_TIMER_STARTTIME=""
474TC_TIMER_TIMER_TEXT=""
475TC_TIMER_CURRENT_FAILS="" # Then numer of failed test when timer starts.
476 # Compared with the current number of fails at timer stop
477 # to judge the measurement reliability
478
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200479#File to save timer measurement results
480TIMER_MEASUREMENTS=".timer_measurement.txt"
BjornMagnussonXA79e37002021-11-22 13:36:04 +0100481echo -e "Activity \t Duration \t Info" > $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200482
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100483# 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 +0200484IMAGE_REPO_ADR=""
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200485IMAGE_REPO_POLICY="local"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200486CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200487
YongchaoWu9a84f512019-12-16 22:54:11 +0100488echo "-------------------------------------------------------------------------------------------------"
489echo "----------------------------------- Test case: "$ATC
490echo "----------------------------------- Started: "$(date)
491echo "-------------------------------------------------------------------------------------------------"
492echo "-- Description: "$TC_ONELINE_DESCR
493echo "-------------------------------------------------------------------------------------------------"
494echo "----------------------------------- Test case setup -----------------------------------"
495
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100496echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200497START_ARG=$1
498paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100499paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200500if [ $# -lt 1 ]; then
501 paramerror=1
502fi
503if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100504 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200505 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100506 if [ -z "$paramerror_str" ]; then
507 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
508 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200509 else
510 shift;
511 fi
512fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100513if [ $paramerror -eq 0 ]; then
514 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
515 paramerror=1
516 if [ -z "$paramerror_str" ]; then
517 paramerror_str="Second arg shall be 'docker' or 'kube'"
518 fi
519 else
520 if [ $1 == "docker" ]; then
521 RUNMODE="DOCKER"
522 echo "Setting RUNMODE=DOCKER"
523 fi
524 if [ $1 == "kube" ]; then
525 RUNMODE="KUBE"
526 echo "Setting RUNMODE=KUBE"
527 fi
528 shift;
529 fi
530fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200531foundparm=0
532while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
533 foundparm=1
534 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100535 if [ "$1" == "release" ]; then
536 IMAGE_CATEGORY="RELEASE"
537 echo "Option set - Release image tags used for applicable images "
538 shift;
539 foundparm=0
540 fi
541 fi
542 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200543 if [ "$1" == "auto-clean" ]; then
544 AUTO_CLEAN="auto"
545 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200546 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200547 foundparm=0
548 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200549 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200550 if [ $paramerror -eq 0 ]; then
551 if [ "$1" == "--stop-at-error" ]; then
552 STOP_AT_ERROR=1
553 echo "Option set - Stop at first error"
554 shift;
555 foundparm=0
556 fi
557 fi
558 if [ $paramerror -eq 0 ]; then
559 if [ "$1" == "--ricsim-prefix" ]; then
560 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100561 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200562 if [ -z "$1" ]; then
563 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100564 if [ -z "$paramerror_str" ]; then
565 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
566 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200567 else
568 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
569 shift;
570 foundparm=0
571 fi
572 fi
573 fi
574 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200575 if [ "$1" == "--env-file" ]; then
576 shift;
577 TEST_ENV_VAR_FILE=$1
578 if [ -z "$1" ]; then
579 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100580 if [ -z "$paramerror_str" ]; then
581 paramerror_str="No env file found for flag: '--env-file'"
582 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200583 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200584 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200585 shift;
586 foundparm=0
587 fi
588 fi
589 fi
590 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200591 if [ "$1" == "--use-local-image" ]; then
592 USE_LOCAL_IMAGES=""
593 shift
594 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
595 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100596 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200597 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100598 if [ -z "$paramerror_str" ]; then
599 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
600 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200601 fi
602 shift;
603 done
604 foundparm=0
605 if [ -z "$USE_LOCAL_IMAGES" ]; then
606 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100607 if [ -z "$paramerror_str" ]; then
608 paramerror_str="No app name found for flag: '--use-local-image'"
609 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200610 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100611 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
612 fi
613 fi
614 fi
615 if [ $paramerror -eq 0 ]; then
616 if [ "$1" == "--use-snapshot-image" ]; then
617 USE_SNAPSHOT_IMAGES=""
618 shift
619 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
620 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
621 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
622 paramerror=1
623 if [ -z "$paramerror_str" ]; then
624 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
625 fi
626 fi
627 shift;
628 done
629 foundparm=0
630 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
631 paramerror=1
632 if [ -z "$paramerror_str" ]; then
633 paramerror_str="No app name found for flag: '--use-snapshot-image'"
634 fi
635 else
636 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
637 fi
638 fi
639 fi
640 if [ $paramerror -eq 0 ]; then
641 if [ "$1" == "--use-staging-image" ]; then
642 USE_STAGING_IMAGES=""
643 shift
644 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
645 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
646 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
647 paramerror=1
648 if [ -z "$paramerror_str" ]; then
649 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
650 fi
651 fi
652 shift;
653 done
654 foundparm=0
655 if [ -z "$USE_STAGING_IMAGES" ]; then
656 paramerror=1
657 if [ -z "$paramerror_str" ]; then
658 paramerror_str="No app name found for flag: '--use-staging-image'"
659 fi
660 else
661 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
662 fi
663 fi
664 fi
665 if [ $paramerror -eq 0 ]; then
666 if [ "$1" == "--use-release-image" ]; then
667 USE_RELEASE_IMAGES=""
668 shift
669 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
670 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
671 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
672 paramerror=1
673 if [ -z "$paramerror_str" ]; then
674 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
675 fi
676 fi
677 shift;
678 done
679 foundparm=0
680 if [ -z "$USE_RELEASE_IMAGES" ]; then
681 paramerror=1
682 if [ -z "$paramerror_str" ]; then
683 paramerror_str="No app name found for flag: '--use-release-image'"
684 fi
685 else
686 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200687 fi
688 fi
689 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200690 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAe85223d2022-06-15 10:19:00 +0200691 if [ "$1" == "--use-external-image" ]; then
692 USE_EXTERNAL_IMAGES=""
693 shift
694 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
695 USE_EXTERNAL_IMAGES=$USE_EXTERNAL_IMAGES" "$1
696 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
697 paramerror=1
698 if [ -z "$paramerror_str" ]; then
699 paramerror_str="App name $1 is not available for release override for flag: '--use-external-image'"
700 fi
701 fi
702 shift;
703 done
704 foundparm=0
705 if [ -z "$USE_EXTERNAL_IMAGES" ]; then
706 paramerror=1
707 if [ -z "$paramerror_str" ]; then
708 paramerror_str="No app name found for flag: '--use-use-external-image'"
709 fi
710 else
711 echo "Option set - Overriding with external images for app(s):"$USE_EXTERNAL_IMAGES
712 fi
713 fi
714 fi
715 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200716 if [ "$1" == "--image-repo" ]; then
717 shift;
718 IMAGE_REPO_ADR=$1
719 if [ -z "$1" ]; then
720 paramerror=1
721 if [ -z "$paramerror_str" ]; then
722 paramerror_str="No image repo url found for : '--image-repo'"
723 fi
724 else
725 echo "Option set - Image repo url: "$1
726 shift;
727 foundparm=0
728 fi
729 fi
730 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200731 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA674793d2021-05-06 19:49:17 +0200732 if [ "$1" == "--repo-policy" ]; then
733 shift;
734 IMAGE_REPO_POLICY=$1
735 if [ -z "$1" ]; then
736 paramerror=1
737 if [ -z "$paramerror_str" ]; then
738 paramerror_str="No policy found for : '--repo-policy'"
739 fi
740 else
741 if [ "$1" == "local" ] || [ "$1" == "remote" ]; then
742 echo "Option set - Image repo policy: "$1
743 shift;
744 foundparm=0
745 else
746 paramerror=1
747 if [ -z "$paramerror_str" ]; then
748 paramerror_str="Repo policy shall be 'local' or 'remote'"
749 fi
750 fi
751 fi
752 fi
753 fi
754 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200755 if [ "$1" == "--cluster-timeout" ]; then
756 shift;
757 CLUSTER_TIME_OUT=$1
758 if [ -z "$1" ]; then
759 paramerror=1
760 if [ -z "$paramerror_str" ]; then
761 paramerror_str="No timeout value found for : '--cluster-timeout'"
762 fi
763 else
764 #Check if positive int
765 case ${CLUSTER_TIME_OUT#[+]} in
766 *[!0-9]* | '')
767 paramerror=1
768 if [ -z "$paramerror_str" ]; then
769 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
770 fi
771 ;;
772 * ) ;; # Ok
773 esac
774 echo "Option set - Cluster timeout: "$1
775 shift;
776 foundparm=0
777 fi
778 fi
779 fi
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200780 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +0100781 if [ "$1" == "--override" ]; then
782 shift;
783 TEST_ENV_VAR_FILE_OVERRIDE=$1
784 if [ -z "$1" ]; then
785 paramerror=1
786 if [ -z "$paramerror_str" ]; then
787 paramerror_str="No env file found for flag: '--override'"
788 fi
789 else
790 if [ ! -f $TEST_ENV_VAR_FILE_OVERRIDE ]; then
791 paramerror=1
792 if [ -z "$paramerror_str" ]; then
793 paramerror_str="File for '--override' does not exist : "$TEST_ENV_VAR_FILE_OVERRIDE
794 fi
795 fi
796 echo "Option set - Override env from: "$1
797 shift;
798 foundparm=0
799 fi
800 fi
801 fi
802 if [ $paramerror -eq 0 ]; then
803 if [ "$1" == "--pre-clean" ]; then
804 PRE_CLEAN=1
805 echo "Option set - Pre-clean of kube/docker resouces"
806 shift;
807 foundparm=0
808 fi
809 fi
810 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200811 if [ "$1" == "--print-stats" ]; then
812 PRINT_CURRENT_STATS=1
BjornMagnussonXA007b6452021-11-29 08:03:38 +0100813 echo "Option set - Print stats after every test-case and config"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +0200814 shift;
815 foundparm=0
816 fi
817 fi
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +0100818 if [ $paramerror -eq 0 ]; then
819 if [ "$1" == "--gen-stats" ]; then
820 COLLECT_RUNTIME_STATS=1
821 echo "Option set - Collect runtime statistics"
822 shift;
823 foundparm=0
824 fi
825 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +0100826 if [ $paramerror -eq 0 ]; then
827 if [ "$1" == "--delete-namespaces" ]; then
828 if [ $RUNMODE == "DOCKER" ]; then
829 DELETE_KUBE_NAMESPACES=0
830 echo "Option ignored - Delete namespaces (ignored when running docker)"
831 else
832 if [ -z "KUBE_PRESTARTED_IMAGES" ]; then
833 DELETE_KUBE_NAMESPACES=0
834 echo "Option ignored - Delete namespaces (ignored when using prestarted apps)"
835 else
836 DELETE_KUBE_NAMESPACES=1
837 echo "Option set - Delete namespaces"
838 fi
839 fi
840 shift;
841 foundparm=0
842 fi
843 fi
844 if [ $paramerror -eq 0 ]; then
845 if [ "$1" == "--delete-containers" ]; then
846 if [ $RUNMODE == "DOCKER" ]; then
847 DELETE_CONTAINERS=1
848 echo "Option set - Delete containers started by previous test(s)"
849 else
850 echo "Option ignored - Delete containers (ignored when running kube)"
851 fi
852 shift;
853 foundparm=0
854 fi
855 fi
856 if [ $paramerror -eq 0 ]; then
857 if [ "$1" == "--endpoint-stats" ]; then
858 COLLECT_ENDPOINT_STATS=1
859 echo "Option set - Collect endpoint statistics"
860 shift;
861 foundparm=0
862 fi
863 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +0100864 if [ $paramerror -eq 0 ]; then
865 if [ "$1" == "--kubeconfig" ]; then
866 shift;
867 if [ -z "$1" ]; then
868 paramerror=1
869 if [ -z "$paramerror_str" ]; then
870 paramerror_str="No path found for : '--kubeconfig'"
871 fi
872 else
873 if [ -f $1 ]; then
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100874 if [ ! -z "$KUBECONF" ]; then
875 paramerror=1
876 if [ -z "$paramerror_str" ]; then
877 paramerror_str="Only one of --kubeconfig/--kubecontext can be set"
878 fi
879 else
880 KUBECONF="--kubeconfig $1"
881 echo "Option set - Kubeconfig path: "$1
882 shift;
883 foundparm=0
884 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +0100885 else
886 paramerror=1
887 if [ -z "$paramerror_str" ]; then
888 paramerror_str="File $1 for --kubeconfig not found"
889 fi
890 fi
891 fi
892 fi
893 fi
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +0100894 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100895 if [ "$1" == "--kubecontext" ]; then
896 shift;
897 if [ -z "$1" ]; then
898 paramerror=1
899 if [ -z "$paramerror_str" ]; then
900 paramerror_str="No context-name found for : '--kubecontext'"
901 fi
902 else
903 if [ ! -z "$KUBECONF" ]; then
904 paramerror=1
905 if [ -z "$paramerror_str" ]; then
906 paramerror_str="Only one of --kubeconfig or --kubecontext can be set"
907 fi
908 else
909 KUBECONF="--context $1"
910 echo "Option set - Kubecontext name: "$1
911 shift;
912 foundparm=0
913 fi
914 fi
915 fi
916 fi
917 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +0100918 if [ "$1" == "--host-path-dir" ]; then
919 shift;
920 if [ -z "$1" ]; then
921 paramerror=1
922 if [ -z "$paramerror_str" ]; then
923 paramerror_str="No path found for : '--host-path-dir'"
924 fi
925 else
926 HOST_PATH_BASE_DIR=$1
BjornMagnussonXA145762b2022-03-22 10:35:10 +0100927 echo "Option set - Host path for kube set to: "$1
928 shift
929 foundparm=0
930 fi
931 fi
932 fi
933 if [ $paramerror -eq 0 ]; then
934 if [ "$1" == "--docker-host" ]; then
935 shift;
936 if [ -z "$1" ]; then
937 paramerror=1
938 if [ -z "$paramerror_str" ]; then
939 paramerror_str="No url found for : '--docker-host'"
940 fi
941 else
942 export DOCKER_HOST="$1"
943 echo "Option set - DOCKER_HOST set to: "$1
944 shift
945 foundparm=0
946 fi
947 fi
948 fi
949 if [ $paramerror -eq 0 ]; then
950 if [ "$1" == "--docker-host" ]; then
951 shift;
952 if [ -z "$1" ]; then
953 paramerror=1
954 if [ -z "$paramerror_str" ]; then
955 paramerror_str="No url found for : '--docker-host'"
956 fi
957 else
958 export DOCKER_HOST="$1"
959 echo "Option set - DOCKER_HOST set to: "$1
960 shift
961 foundparm=0
962 fi
963 fi
964 fi
965 if [ $paramerror -eq 0 ]; then
966 if [ "$1" == "--docker-proxy" ]; then
967 shift;
968 if [ -z "$1" ]; then
969 paramerror=1
970 if [ -z "$paramerror_str" ]; then
971 paramerror_str="No ip/host found for : '--docker-proxy'"
972 fi
973 else
974 export LOCALHOST_NAME=$1
975 echo "Option set - docker proxy set to: "$1
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +0100976 shift
977 foundparm=0
978 fi
979 fi
980 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200981done
982echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200983
BjornMagnussonXAad047782020-06-08 15:54:11 +0200984#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200985if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
986 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100987 if [ -z "$paramerror_str" ]; then
988 paramerror_str="Unknown parameter(s): "$@
989 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200990fi
991
992if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100993 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
994 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200995 exit 1
996fi
997
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +0100998LOCALHOST_HTTP="http://$LOCALHOST_NAME"
999LOCALHOST_HTTPS="https://$LOCALHOST_NAME"
1000
BjornMagnussonXA575869c2020-09-14 21:28:54 +02001001# sourcing the selected env variables for the test case
1002if [ -f "$TEST_ENV_VAR_FILE" ]; then
1003 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
1004 . $TEST_ENV_VAR_FILE
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001005 if [ ! -z "$TEST_ENV_VAR_FILE_OVERRIDE" ]; then
1006 echo -e $BOLD"Sourcing override env vars from: "$TEST_ENV_VAR_FILE_OVERRIDE$EBOLD
1007 . $TEST_ENV_VAR_FILE_OVERRIDE
1008 fi
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001009
1010 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001011 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 +01001012 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001013 found_profile=0
1014 for prof in $SUPPORTED_PROFILES; do
1015 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
1016 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
1017 found_profile=1
1018 fi
1019 done
1020 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001021 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001022 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01001023 echo -e $RED"Exiting...."$ERED
1024 exit 1
1025 fi
1026 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +02001027else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001028 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +02001029 echo " Select one of following env var file matching the intended target of the test"
1030 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001031 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +02001032 exit 1
1033fi
1034
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001035#This var need be preserved from the command line option, if set, when env var is sourced.
1036if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
1037 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
1038fi
1039
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001040if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001041 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001042 exit 1
1043fi
1044
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001045if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
1046 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
1047 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
1048 exit 1
1049fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001050
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001051# Choose list of included apps depending on run-mode
1052if [ $RUNMODE == "KUBE" ]; then
1053 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
1054else
1055 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
1056fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +02001057
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001058echo ""
1059# auto adding system apps
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01001060__added_apps=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001061echo -e $BOLD"Auto adding system apps"$EBOLD
1062if [ $RUNMODE == "KUBE" ]; then
1063 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_KUBE_SYSTEM_APPS
1064 TMP_APPS=$TESTENV_KUBE_SYSTEM_APPS
1065else
1066 INCLUDED_IMAGES=$INCLUDED_IMAGES" "$TESTENV_DOCKER_SYSTEM_APPS
1067 TMP_APPS=$TESTENV_DOCKER_SYSTEM_APPS
1068fi
1069if [ ! -z "$TMP_APPS" ]; then
1070 for iapp in "$TMP_APPS"; do
1071 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
1072 file_pointer="../common/"$file_pointer"_api_functions.sh"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01001073 padded_iapp=$iapp
1074 while [ ${#padded_iapp} -lt 16 ]; do
1075 padded_iapp=$padded_iapp" "
1076 done
1077 echo " Auto-adding system app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001078 . $file_pointer
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01001079 if [ $? -ne 0 ]; then
1080 echo " Include file $file_pointer contain errors. Exiting..."
1081 exit 1
1082 fi
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01001083 __added_apps=" $iapp "$__added_apps
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001084 done
1085else
1086 echo " None"
1087fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001088
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01001089if [ $RUNMODE == "KUBE" ]; then
1090 TMP_APPS=$INCLUDED_IMAGES" "$KUBE_PRESTARTED_IMAGES
1091else
1092 TMP_APPS=$INCLUDED_IMAGES
1093fi
1094
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001095echo -e $BOLD"Auto adding included apps"$EBOLD
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01001096 for iapp in $TMP_APPS; do
1097 if [[ "$__added_apps" != *"$iapp"* ]]; then
1098 file_pointer=$(echo $iapp | tr '[:upper:]' '[:lower:]')
1099 file_pointer="../common/"$file_pointer"_api_functions.sh"
1100 padded_iapp=$iapp
1101 while [ ${#padded_iapp} -lt 16 ]; do
1102 padded_iapp=$padded_iapp" "
1103 done
1104 echo " Auto-adding included app $padded_iapp Sourcing $file_pointer"
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01001105 if [ ! -f "$file_pointer" ]; then
1106 echo " Include file $file_pointer for app $iapp does not exist"
1107 exit 1
1108 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01001109 . $file_pointer
1110 if [ $? -ne 0 ]; then
1111 echo " Include file $file_pointer contain errors. Exiting..."
1112 exit 1
1113 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001114 fi
1115 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001116echo ""
1117
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001118echo -e $BOLD"Test environment info"$EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001119
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001120# Check needed installed sw
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001121
1122tmp=$(which bash)
1123if [ $? -ne 0 ] || [ -z "$tmp" ]; then
1124 echo -e $RED"bash is required to run the test environment, pls install"$ERED
1125 exit 1
1126fi
1127echo " bash is installed and using version:"
1128echo "$(bash --version)" | indent2
1129
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001130tmp=$(which python3)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001131if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001132 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
1133 exit 1
1134fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001135echo " python3 is installed and using version: $(python3 --version)"
1136
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001137tmp=$(which docker)
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001138if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001139 echo -e $RED"docker is required to run the test environment, pls install"$ERED
1140 exit 1
1141fi
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001142echo " docker is installed and using versions:"
1143echo " $(docker version --format 'Client version {{.Client.Version}} Server version {{.Server.Version}}')"
BjornMagnussonXA575869c2020-09-14 21:28:54 +02001144
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01001145if [ $RUNMODE == "DOCKER" ]; then
1146 tmp=$(which docker-compose)
1147 if [ $? -ne 0 ] || [ -z "$tmp" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001148 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
1149 exit 1
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01001150 else
BjornMagnussonXA8fbc6362022-04-19 09:39:22 +02001151 tmp=$(docker-compose version)
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01001152 echo " docker-compose installed and using version $tmp"
1153 if [[ "$tmp" == *'v2'* ]]; then
BjornMagnussonXAcd938442022-05-11 10:01:24 +02001154 DOCKER_COMPOSE_VERSION="V2"
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01001155 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001156 fi
1157fi
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01001158if [ $RUNMODE == "KUBE" ]; then
1159 tmp=$(which kubectl)
1160 if [ $? -ne 0 ] || [ -z tmp ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001161 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
1162 exit 1
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01001163 else
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001164 echo " kubectl is installed and using versions:"
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +01001165 echo $(kubectl $KUBECONF version --short=true) | indent2
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001166 res=$(kubectl $KUBECONF cluster-info 2>&1)
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001167 if [ $? -ne 0 ]; then
1168 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001169 echo -e $BOLD$RED"Command 'kubectl '$KUBECONF' cluster-info' returned error $ERED$EBOLD"
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001170 echo -e "$BOLD$RED############################################# $ERED$EBOLD"
1171 echo " "
1172 echo "kubectl response:"
1173 echo $res
1174 echo " "
1175 echo "This script may have been started with user with no permission to run kubectl"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001176 echo "Try running with 'sudo', set env KUBECONFIG or set '--kubeconfig' parameter"
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001177 echo "Do either 1, 2 or 3 "
1178 echo " "
1179 echo "1"
1180 echo "Run with sudo"
1181 echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD
1182 echo " "
1183 echo "2"
1184 echo "Export KUBECONFIG and pass env to sudo - (replace user)"
1185 echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD
1186 echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD
1187 echo " "
1188 echo "3"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001189 echo "Set KUBECONFIG via script parameter"
1190 echo -e $BOLD"sudo ... --kubeconfig /home/<user>/.kube/<config-file> ...."$EBOLD
1191 echo "The config file need to downloaded from the cluster"
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001192
1193 exit 1
1194 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001195 echo " Node(s) and container runtime config"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001196 kubectl $KUBECONF get nodes -o wide | indent2
BjornMagnussonXAc8f92e92022-02-28 15:16:37 +01001197 echo ""
1198 if [ -z "$HOST_PATH_BASE_DIR" ]; then
1199 HOST_PATH_BASE_DIR="/tmp"
1200 echo " Persistent volumes will be mounted to $HOST_PATH_BASE_DIR on applicable node"
1201 echo " No guarantee that persistent volume data is available on all nodes in the cluster"
1202 else
1203 echo "Persistent volumes will be mounted to base dir: $HOST_PATH_BASE_DIR"
1204 echo "Assuming this dir is mounted from each node to a dir on the localhost or other"
1205 echo "file system available to all nodes"
1206 fi
BjornMagnussonXA9ef79da2021-06-15 00:08:11 +02001207 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001208fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +01001209
BjornMagnussonXA51f04f02021-11-23 09:22:35 +01001210echo ""
1211
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001212echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +01001213
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001214#Temp var to check for image variable name errors
1215IMAGE_ERR=0
1216#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001217image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001218echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001219
1220# 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 +01001221# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001222__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001223
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001224 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001225 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 +01001226 ((IMAGE_ERR++))
1227 return
1228 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001229
1230 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001231 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001232 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001233 # Image is excluded since the corresponding app is not used in this test
1234 return
1235 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001236 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001237 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001238 image="${!3}"
1239 tmptag=$4"_"$5
1240 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001241
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001242 optional_image_repo_target=""
1243
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001244 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001245 __check_ignore_image $1
1246 if [ $? -eq 0 ]; then
1247 app_ds=$6
1248 if [ -z "$6" ]; then
1249 app_ds="<app ignored>"
1250 fi
1251 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
1252 # Image is ignored since the corresponding the images is not set in the env file
1253 __remove_included_image $1 # Remove the image from the list of included images
1254 return
1255 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001256 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
1257 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001258 echo ""
1259 tmp=$tmp"<no-image>\t"
1260 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001261
1262 optional_image_repo_target=$image
1263
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001264 #Add repo depending on image type
1265 if [ "$5" == "REMOTE_RELEASE" ]; then
1266 image=$NEXUS_RELEASE_REPO$image
1267 fi
1268 if [ "$5" == "REMOTE" ]; then
1269 image=$NEXUS_STAGING_REPO$image
1270 fi
1271 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
1272 image=$NEXUS_SNAPSHOT_REPO$image
1273 fi
1274 if [ "$5" == "REMOTE_PROXY" ]; then
1275 image=$NEXUS_PROXY_REPO$image
1276 fi
1277 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
1278 image=$NEXUS_RELEASE_REPO_ONAP$image
1279 fi
1280 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
1281 image=$NEXUS_RELEASE_REPO_ORAN$image
1282 fi
BjornMagnussonXAe85223d2022-06-15 10:19:00 +02001283 #No nexus repo added for local images, tag: LOCAL and other tags
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001284 tmp=$tmp$image"\t"
1285 fi
1286 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001287 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
1288 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001289 echo ""
1290 tmp=$tmp"<no-tag>\t"
1291 else
1292 tmp=$tmp$tag
1293 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001294 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001295 echo -e "$tmp" >> $image_list_file
1296 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001297 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 +02001298
1299 remote_or_local_push=false
1300 if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then
1301 if [ $5 == "LOCAL" ]; then
1302 remote_or_local_push=true
1303 fi
1304 if [[ $5 == *"REMOTE"* ]]; then
1305 if [ "$IMAGE_REPO_POLICY" == "remote" ]; then
1306 remote_or_local_push=true
1307 fi
1308 fi
1309 fi
1310 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 +02001311 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001312 if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo
1313 optional_image_repo_target_tmp=${optional_image_repo_target//\//_}
1314 optional_image_repo_target=$optional_image_repo_target_tmp
1315 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001316 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
1317 else
1318 export "${2}_SOURCE"=""
1319 export "${2}_TARGET"=""
1320 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001321}
1322
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001323# Check if app uses image included in this test run
1324# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001325__check_included_image() {
1326 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001327 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001328 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001329 fi
1330 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001331 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001332}
1333
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001334# Check if app uses a project image
1335# Returns 0 if image is included, 1 if not
1336__check_project_image() {
1337 for im in $PROJECT_IMAGES; do
1338 if [ "$1" == "$im" ]; then
1339 return 0
1340 fi
1341 done
1342 return 1
1343}
1344
1345# Check if app uses image built by the test script
1346# Returns 0 if image is included, 1 if not
1347__check_image_local_build() {
1348 for im in $LOCAL_IMAGE_BUILD; do
1349 if [ "$1" == "$im" ]; then
1350 return 0
1351 fi
1352 done
1353 return 1
1354}
1355
1356# Check if app image is conditionally ignored in this test run
1357# Returns 0 if image is conditionally ignored, 1 if not
1358__check_ignore_image() {
1359 for im in $CONDITIONALLY_IGNORED_IMAGES; do
1360 if [ "$1" == "$im" ]; then
1361 return 0
1362 fi
1363 done
1364 return 1
1365}
1366
1367# Removed image from included list of included images
1368# Used when an image is marked as conditionally ignored
1369__remove_included_image() {
1370 tmp_img_rem_list=""
1371 for im in $INCLUDED_IMAGES; do
1372 if [ "$1" != "$im" ]; then
1373 tmp_img_rem_list=$tmp_img_rem_list" "$im
1374 fi
1375 done
1376 INCLUDED_IMAGES=$tmp_img_rem_list
1377 return 0
1378}
1379
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001380# Check if app is included in the prestarted set of apps
1381# Returns 0 if image is included, 1 if not
1382__check_prestarted_image() {
1383 for im in $KUBE_PRESTARTED_IMAGES; do
1384 if [ "$1" == "$im" ]; then
1385 return 0
1386 fi
1387 done
1388 return 1
1389}
1390
1391# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001392__check_image_local_override() {
1393 for im in $USE_LOCAL_IMAGES; do
1394 if [ "$1" == "$im" ]; then
1395 return 1
1396 fi
1397 done
1398 return 0
1399}
1400
1401# Check if app uses image override
1402# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
1403__check_image_override() {
1404
1405 for im in $ORAN_IMAGES_APP_NAMES; do
1406 if [ "$1" == "$im" ]; then
1407 echo "REMOTE_RELEASE_ORAN"
1408 return 0
1409 fi
1410 done
1411
1412 for im in $ONAP_IMAGES_APP_NAMES; do
1413 if [ "$1" == "$im" ]; then
1414 echo "REMOTE_RELEASE_ONAP"
1415 return 0
1416 fi
1417 done
1418
1419 found=0
1420 for im in $PROJECT_IMAGES_APP_NAMES; do
1421 if [ "$1" == "$im" ]; then
1422 found=1
1423 fi
1424 done
1425
1426 if [ $found -eq 0 ]; then
1427 echo "REMOTE_PROXY"
1428 return 0
1429 fi
1430
1431 suffix=""
1432 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
1433 suffix="REMOTE_RELEASE"
1434 fi
1435 if [ $IMAGE_CATEGORY == "DEV" ]; then
1436 suffix="REMOTE"
1437 fi
1438 CTR=0
1439 for im in $USE_STAGING_IMAGES; do
1440 if [ "$1" == "$im" ]; then
1441 suffix="REMOTE"
1442 ((CTR++))
1443 fi
1444 done
1445 for im in $USE_RELEASE_IMAGES; do
1446 if [ "$1" == "$im" ]; then
1447 suffix="REMOTE_RELEASE"
1448 ((CTR++))
1449 fi
1450 done
1451 for im in $USE_SNAPSHOT_IMAGES; do
1452 if [ "$1" == "$im" ]; then
1453 suffix="REMOTE_SNAPSHOT"
1454 ((CTR++))
1455 fi
1456 done
1457 for im in $USE_LOCAL_IMAGES; do
1458 if [ "$1" == "$im" ]; then
1459 suffix="LOCAL"
1460 ((CTR++))
1461 fi
1462 done
BjornMagnussonXAe85223d2022-06-15 10:19:00 +02001463 for im in $USE_EXTERNAL_IMAGES; do
1464 if [ "$1" == "$im" ]; then
1465 suffix="EXTERNAL"
1466 ((CTR++))
1467 fi
1468 done
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001469 echo $suffix
1470 if [ $CTR -gt 1 ]; then
1471 exit 1
1472 fi
1473 return 0
1474}
1475
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001476# Function to re-tag and image and push to another image repo
1477__retag_and_push_image() {
1478 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1479 source_image="${!1}"
1480 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
1481 target_image="${!trg_var_name}"
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001482
1483 if [ -z $target_image ]; then
1484 return 0 # Image with no target shall not be pushed
1485 fi
1486
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001487 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1488 tmp=$(docker image tag $source_image ${target_image} )
1489 if [ $? -ne 0 ]; then
1490 docker stop $tmp &> ./tmp/.dockererr
1491 ((IMAGE_ERR++))
1492 echo ""
1493 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1494 cat ./tmp/.dockererr
1495 return 1
1496 else
1497 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1498 fi
1499 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
1500 tmp=$(docker push ${target_image} )
1501 if [ $? -ne 0 ]; then
1502 docker stop $tmp &> ./tmp/.dockererr
1503 ((IMAGE_ERR++))
1504 echo ""
1505 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
1506 cat ./tmp/.dockererr
1507 return 1
1508 else
1509 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
1510 fi
1511 export "${1}"=$target_image
1512 fi
1513 return 0
1514}
1515
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001516#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001517#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001518__check_and_pull_image() {
1519
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001520 source_image="${!4}"
1521
1522 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 +01001523 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001524 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001525
1526 if [ $1 == "local" ]; then
1527 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001528 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 +01001529 ((IMAGE_ERR++))
1530 return 1
1531 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001532 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001533 fi
1534 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
1535 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001536 if [ $RUNMODE == "DOCKER" ]; then
1537 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001538 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001539 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1540 docker stop $tmp &> ./tmp/.dockererr
1541 if [ $? -ne 0 ]; then
1542 ((IMAGE_ERR++))
1543 echo ""
1544 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
1545 cat ./tmp/.dockererr
1546 return 1
1547 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001548 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001549 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001550 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001551 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
1552 docker rm $tmp &> ./tmp/.dockererr
1553 if [ $? -ne 0 ]; then
1554 ((IMAGE_ERR++))
1555 echo ""
1556 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
1557 cat ./tmp/.dockererr
1558 return 1
1559 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001560 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001561 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
1562 tmp_im=""
1563 else
1564 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001565 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001566 fi
1567 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +02001568 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001569 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001570 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001571 echo ""
1572 echo -e " Pulling image -$RED could not be pulled"$ERED
1573 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001574 echo $out > ./tmp/.dockererr
1575 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001576 return 1
1577 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001578 echo $out > ./tmp/.dockererr
1579 if [[ $out == *"up to date"* ]]; then
1580 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
1581 elif [[ $out == *"Downloaded newer image"* ]]; then
1582 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
1583 else
1584 echo -e " Pulling image -$GREEN Pulled $EGREEN"
1585 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001586 else
1587 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1588 fi
1589 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001590
1591 __retag_and_push_image $4
1592
1593 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001594}
1595
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001596setup_testenvironment() {
1597 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001598 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001599
1600 # Image var setup for all project images included in the test
1601 for imagename in $APP_SHORT_NAMES; do
1602 __check_included_image $imagename
1603 incl=$?
1604 __check_project_image $imagename
1605 proj=$?
1606 if [ $incl -eq 0 ]; then
1607 if [ $proj -eq 0 ]; then
1608 IMAGE_SUFFIX=$(__check_image_override $imagename)
1609 if [ $? -ne 0 ]; then
1610 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1611 ((IMAGE_ERR++))
1612 fi
1613 else
1614 IMAGE_SUFFIX="none"
1615 fi
1616 # A function name is created from the app short name
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001617 # for example app short name 'ICS' -> produce the function
1618 # name __ICS_imagesetup
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001619 # This function is called and is expected to exist in the imported
BjornMagnussonXA007b6452021-11-29 08:03:38 +01001620 # file for the ics test functions
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001621 # The resulting function impl will call '__check_and_create_image_var' function
1622 # with appropriate parameters
1623 # If the image suffix is none, then the component decides the suffix
1624 function_pointer="__"$imagename"_imagesetup"
1625 $function_pointer $IMAGE_SUFFIX
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01001626
1627 function_pointer="__"$imagename"_test_requirements"
1628 $function_pointer
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001629 fi
1630 done
1631
1632 #Errors in image setting - exit
1633 if [ $IMAGE_ERR -ne 0 ]; then
1634 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001635 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001636
1637 #Print a tables of the image settings
1638 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1639 column -t -s $'\t' $image_list_file | indent1
1640
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001641 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001642
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001643 #Set the SIM_GROUP var
1644 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1645 if [ -z "$SIM_GROUP" ]; then
1646 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1647 if [ ! -d $SIM_GROUP ]; then
1648 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1649 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1650 exit 1
1651 else
1652 echo " SIM_GROUP auto set to: " $SIM_GROUP
1653 fi
1654 elif [ $SIM_GROUP = *simulator_group ]; then
1655 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
1656 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001657 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001658 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1659 fi
1660
1661 echo ""
1662
1663 #Temp var to check for image pull errors
1664 IMAGE_ERR=0
1665
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001666 # Delete namespaces
1667 echo -e $BOLD"Deleting namespaces"$EBOLD
1668
1669
1670 if [ "$DELETE_KUBE_NAMESPACES" -eq 1 ]; then
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001671 test_env_namespaces=$(kubectl $KUBECONF get ns --no-headers -o custom-columns=":metadata.name" -l autotest=engine) #Get list of ns created by the test env
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001672 if [ $? -ne 0 ]; then
1673 echo " Cannot get list of namespaces...ignoring delete"
1674 else
1675 for test_env_ns in $test_env_namespaces; do
1676 __kube_delete_namespace $test_env_ns
1677 done
1678 fi
1679 else
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001680 echo " Namespace delete option not set or ignored"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001681 fi
1682 echo ""
1683
1684 # Delete containers
1685 echo -e $BOLD"Deleting containers"$EBOLD
1686
1687 if [ "$DELETE_CONTAINERS" -eq 1 ]; then
1688 echo " Stopping containers label 'nrttest_app'..."
1689 docker stop $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
1690 echo " Removing stopped containers..."
1691 docker rm $(docker ps -qa --filter "label=nrttest_app") 2> /dev/null
1692 else
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01001693 echo " Contatiner delete option not set or ignored"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001694 fi
1695 echo ""
1696
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001697 # The following sequence pull the configured images
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001698 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
BjornMagnussonXA674793d2021-05-06 19:49:17 +02001699 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then
1700 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 +02001701 else
1702 for imagename in $APP_SHORT_NAMES; do
1703 __check_included_image $imagename
1704 incl=$?
1705 __check_project_image $imagename
1706 proj=$?
1707 if [ $incl -eq 0 ]; then
1708 if [ $proj -eq 0 ]; then
1709 START_ARG_MOD=$START_ARG
1710 __check_image_local_override $imagename
1711 if [ $? -eq 1 ]; then
1712 START_ARG_MOD="local"
1713 fi
1714 else
1715 START_ARG_MOD=$START_ARG
1716 fi
1717 __check_image_local_build $imagename
1718 #No pull of images built locally
1719 if [ $? -ne 0 ]; then
1720 # A function name is created from the app short name
1721 # for example app short name 'HTTPPROXY' -> produce the function
1722 # name __HTTPPROXY_imagesetup
1723 # This function is called and is expected to exist in the imported
1724 # file for the httpproxy test functions
1725 # The resulting function impl will call '__check_and_pull_image' function
1726 # with appropriate parameters
1727 function_pointer="__"$imagename"_imagepull"
1728 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001729 fi
1730 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001731 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001732 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001733 done
1734 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001735
1736 #Errors in image setting - exit
1737 if [ $IMAGE_ERR -ne 0 ]; then
1738 echo ""
1739 echo "#################################################################################################"
1740 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1741 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1742 if [ $IMAGE_CATEGORY == "DEV" ]; then
BjornMagnussonXA720f5d72021-08-13 10:37:23 +02001743 echo ""
1744 echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED
1745 echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED
1746 echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED
1747 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 +01001748 fi
1749 echo "#################################################################################################"
1750 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001751 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001752 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001753
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001754 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001755
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001756 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001757
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001758 for imagename in $APP_SHORT_NAMES; do
1759 cd $AUTOTEST_HOME #Always reset to orig dir
1760 __check_image_local_build $imagename
1761 if [ $? -eq 0 ]; then
1762 __check_included_image $imagename
1763 if [ $? -eq 0 ]; then
1764 # A function name is created from the app short name
1765 # for example app short name 'MR' -> produce the function
1766 # name __MR_imagebuild
1767 # This function is called and is expected to exist in the imported
1768 # file for the mr test functions
1769 # The resulting function impl shall build the imagee
1770 function_pointer="__"$imagename"_imagebuild"
1771 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001772
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001773 else
1774 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1775 fi
1776 fi
1777 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001778
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001779 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001780
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001781 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001782
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001783 # Create a table of the images used in the script - from local repo
1784 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001785
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001786 docker_tmp_file=./tmp/.docker-images-table
1787 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1788 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001789
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001790 for imagename in $APP_SHORT_NAMES; do
1791 __check_included_image $imagename
1792 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001793 # Only print image data if image repo is null, or if image repo is set and image is local
1794 print_image_data=0
1795 if [ -z "$IMAGE_REPO_ADR" ]; then
1796 print_image_data=1
1797 else
1798 __check_image_local_build $imagename
1799 if [ $? -eq 0 ]; then
1800 print_image_data=1
1801 fi
1802 fi
1803 if [ $print_image_data -eq 1 ]; then
1804 # A function name is created from the app short name
1805 # for example app short name 'MR' -> produce the function
1806 # name __MR_imagebuild
1807 # This function is called and is expected to exist in the imported
1808 # file for the mr test functions
1809 # The resulting function impl shall build the imagee
1810 function_pointer="__"$imagename"_image_data"
1811 $function_pointer "$format_string" $docker_tmp_file
1812 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001813 fi
1814 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001815
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001816 column -t -s $'\t' $docker_tmp_file | indent1
1817
1818 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001819
1820 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1821
1822 # Create a table of the images used in the script - from remote repo
1823 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1824 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1825
1826 docker_tmp_file=./tmp/.docker-images-table
1827 format_string="{{.Repository}}\\t{{.Tag}}"
1828 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1829
1830 for imagename in $APP_SHORT_NAMES; do
1831 __check_included_image $imagename
1832 if [ $? -eq 0 ]; then
1833 # Only print image data if image repo is null, or if image repo is set and image is local
1834 __check_image_local_build $imagename
1835 if [ $? -ne 0 ]; then
1836 # A function name is created from the app short name
1837 # for example app short name 'MR' -> produce the function
1838 # name __MR_imagebuild
1839 # This function is called and is expected to exist in the imported
1840 # file for the mr test functions
1841 # The resulting function impl shall build the imagee
1842 function_pointer="__"$imagename"_image_data"
1843 $function_pointer "$format_string" $docker_tmp_file
1844 fi
1845 fi
1846 done
1847
1848 column -t -s $'\t' $docker_tmp_file | indent1
1849
1850 echo ""
1851 fi
1852
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001853 if [ $RUNMODE == "KUBE" ]; then
1854
1855 echo "================================================================================="
1856 echo "================================================================================="
1857
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001858 if [ -z "$IMAGE_REPO_ADR" ]; then
1859 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1860 echo -e " This setting only works on single node clusters on the local machine"
1861 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001862 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001863 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001864 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1865 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1866 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1867 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"
1868 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001869 fi
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01001870 #CLUSTER_IP=$(kubectl $KUBECONF config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
1871 #echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001872
1873 echo "================================================================================="
1874 echo "================================================================================="
1875 echo ""
1876 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001877
1878 echo -e $BOLD"======================================================="$EBOLD
1879 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1880 echo -e $BOLD"======================================================="$EBOLD
1881 echo ""
1882
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001883 LOG_STAT_ARGS=""
1884
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001885 for imagename in $APP_SHORT_NAMES; do
1886 __check_included_image $imagename
1887 retcode_i=$?
1888 __check_prestarted_image $imagename
1889 retcode_p=$?
1890 if [ $retcode_i -eq 0 ] || [ $retcode_p -eq 0 ]; then
1891 # A function name is created from the app short name
1892 # for example app short name 'RICMSIM' -> produce the function
1893 # name __RICSIM__initial_setup
1894 # This function is called and is expected to exist in the imported
1895 # file for the ricsim test functions
1896 # The resulting function impl shall perform initial setup of port, host etc
1897
1898 function_pointer="__"$imagename"_initial_setup"
1899 $function_pointer
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001900
1901 function_pointer="__"$imagename"_statisics_setup"
1902 LOG_STAT_ARGS=$LOG_STAT_ARGS" "$($function_pointer)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01001903 fi
1904 done
1905
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001906 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1907 ../common/genstat.sh $RUNMODE $SECONDS $TESTLOGS/$ATC/stat_data.csv $LOG_STAT_ARGS &
BjornMagnussonXA0730e0f2021-12-29 17:13:23 +01001908 COLLECT_RUNTIME_STATS_PID=$!
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001909 fi
1910
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001911}
YongchaoWu9a84f512019-12-16 22:54:11 +01001912
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001913# Function to print the test result, shall be the last cmd in a test script
1914# args: -
1915# (Function for test scripts)
1916print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001917
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001918 TCTEST_END=$SECONDS
1919 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001920
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001921 echo "-------------------------------------------------------------------------------------------------"
1922 echo "------------------------------------- Test case: "$ATC
1923 echo "------------------------------------- Ended: "$(date)
1924 echo "-------------------------------------------------------------------------------------------------"
1925 echo "-- Description: "$TC_ONELINE_DESCR
1926 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001927 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001928 echo "-------------------------------------------------------------------------------------------------"
1929 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001930 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001931
YongchaoWu21f17bb2020-03-05 12:44:08 +01001932
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001933 if [ $RES_DEVIATION -gt 0 ]; then
1934 echo "Test case deviations"
1935 echo "===================================="
1936 cat $DEVIATION_FILE
1937 fi
1938 echo ""
1939 echo "Timer measurement in the test script"
1940 echo "===================================="
1941 column -t -s $'\t' $TIMER_MEASUREMENTS
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001942 if [ $RES_PASS != $RES_TEST ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01001943 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 +01001944 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001945 echo ""
1946
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01001947 if [ $COLLECT_RUNTIME_STATS -eq 1 ]; then
1948 echo "Runtime statistics collected in file: "$TESTLOGS/$ATC/stat_data.csv
1949 echo ""
1950 fi
BjornMagnussonXAf4a7e422022-09-01 16:35:17 +02001951 TMP_FLAG_FAIL_PASS=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001952 total=$((RES_PASS+RES_FAIL))
1953 if [ $RES_TEST -eq 0 ]; then
BjornMagnussonXAf4a7e422022-09-01 16:35:17 +02001954 TMP_FLAG_FAIL_PASS=1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001955 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1956 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1957 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1958 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1959 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1960 elif [ $total != $RES_TEST ]; then
BjornMagnussonXAf4a7e422022-09-01 16:35:17 +02001961 TMP_FLAG_FAIL_PASS=1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001962 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1963 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1964 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1965 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1966 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1967 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAf4a7e422022-09-01 16:35:17 +02001968 TMP_FLAG_FAIL_PASS=1
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001969 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001970 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1971 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1972 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1973 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1974 elif [ $RES_PASS = $RES_TEST ]; then
BjornMagnussonXAf4a7e422022-09-01 16:35:17 +02001975 TMP_FLAG_FAIL_PASS=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001976 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1977 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1978 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1979 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1980 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1981 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001982
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001983 # Update test suite counter
1984 if [ -f .tmp_tcsuite_pass_ctr ]; then
1985 tmpval=$(< .tmp_tcsuite_pass_ctr)
1986 ((tmpval++))
1987 echo $tmpval > .tmp_tcsuite_pass_ctr
1988 fi
1989 if [ -f .tmp_tcsuite_pass ]; then
1990 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1991 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001992 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001993 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01001994 echo "0" > "$TESTLOGS/$ATC/.result$ATC.txt"
BjornMagnussonXAa27463f2022-11-21 15:34:30 +01001995 echo $(date) > $TESTLOGS/$ATC/endpoint_tc_end.log
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001996 else
BjornMagnussonXAf4a7e422022-09-01 16:35:17 +02001997 TMP_FLAG_FAIL_PASS=1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001998 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1999 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
2000 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
2001 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
2002 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
2003 echo ""
BjornMagnussonXAf4a7e422022-09-01 16:35:17 +02002004 fi
2005
2006 if [ $TMP_FLAG_FAIL_PASS -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002007 # Update test suite counter
2008 if [ -f .tmp_tcsuite_fail_ctr ]; then
2009 tmpval=$(< .tmp_tcsuite_fail_ctr)
2010 ((tmpval++))
2011 echo $tmpval > .tmp_tcsuite_fail_ctr
2012 fi
2013 if [ -f .tmp_tcsuite_fail ]; then
2014 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
2015 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01002016 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01002017
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002018 echo "++++ Number of tests: "$RES_TEST
2019 echo "++++ Number of passed tests: "$RES_PASS
2020 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01002021 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002022 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002023 echo ""
2024 echo "++++ Number of test case deviations: "$RES_DEVIATION
2025 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002026 echo "------------------------------------- Test case complete ---------------------------------"
2027 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01002028 echo ""
2029}
2030
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002031#####################################################################
2032###### Functions for start, configuring, stoping, cleaning etc ######
2033#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01002034
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002035# Start timer for time measurement
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002036# args: <timer message to print> - timer value and message will be printed both on screen
2037# and in the timer measurement report - if at least one "print_timer is called"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002038start_timer() {
2039 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002040 TC_TIMER_STARTTIME=$SECONDS
2041 TC_TIMER_TIMER_TEXT="${@:1}"
2042 if [ $# -ne 1 ]; then
2043 __print_err "need 1 arg, <timer message to print>" $@
2044 TC_TIMER_TIMER_TEXT=${FUNCNAME[0]}":"${BASH_LINENO[0]}
2045 echo " Assigning timer name: "$TC_TIMER_TIMER_TEXT
2046 fi
2047 TC_TIMER_CURRENT_FAILS=$(($RES_FAIL+$RES_CONF_FAIL))
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002048 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002049}
2050
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002051# Print the running timer the value of the time (in seconds)
2052# Timer value and message will be printed both on screen and in the timer measurement report
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002053print_timer() {
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002054 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $TC_TIMER_TIMER_TEXT $EBOLD
2055 if [ -z "$TC_TIMER_STARTTIME" ]; then
2056 __print_err "timer not started" $@
2057 return 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002058 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002059 duration=$(($SECONDS-$TC_TIMER_STARTTIME))
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002060 if [ $duration -eq 0 ]; then
2061 duration="<1 second"
2062 else
2063 duration=$duration" seconds"
2064 fi
2065 echo " Timer duration :" $duration
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002066 res="-"
2067 if [ $(($RES_FAIL+$RES_CONF_FAIL)) -ne $TC_TIMER_CURRENT_FAILS ]; then
2068 res="Failures occured during test - timer not reliabled"
2069 fi
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002070
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002071 echo -e "$TC_TIMER_TIMER_TEXT \t $duration \t $res" >> $TIMER_MEASUREMENTS
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002072}
2073
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002074# Print info about a deviations from intended tests
2075# Each deviation counted is also printed in the testreport
2076# args <deviation message to print>
2077deviation() {
2078 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
2079 if [ $# -lt 1 ]; then
2080 ((RES_CONF_FAIL++))
2081 __print_err "need 1 or more args, <deviation message to print>" $@
2082 exit 1
2083 fi
2084 ((RES_DEVIATION++))
2085 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
2086 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02002087 __print_current_stats
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002088 echo ""
2089}
YongchaoWu21f17bb2020-03-05 12:44:08 +01002090
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002091# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
2092__check_stop_at_error() {
2093 if [ $STOP_AT_ERROR -eq 1 ]; then
2094 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
2095 store_logs "STOP_AT_ERROR"
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01002096
2097 # Update test suite counter
2098 if [ -f .tmp_tcsuite_fail_ctr ]; then
2099 tmpval=$(< .tmp_tcsuite_fail_ctr)
2100 ((tmpval++))
2101 echo $tmpval > .tmp_tcsuite_fail_ctr
2102 fi
2103 if [ -f .tmp_tcsuite_fail ]; then
2104 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution stopped due to error" >> .tmp_tcsuite_fail
2105 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002106 exit 1
2107 fi
2108 return 0
2109}
2110
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002111# Stop and remove all containers
2112# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002113# (Not for test scripts)
2114__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002115
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002116 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002117
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002118 #Create empty file
2119 running_contr_file="./tmp/running_contr.txt"
2120 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002121
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002122 # Get list of all containers started by the test script
2123 for imagename in $APP_SHORT_NAMES; do
2124 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 +01002125 done
BjornMagnussonXAecf34532021-11-24 08:25:15 +01002126 running_contr_file_empty="No docker containers running, started by previous test execution"
2127 if [ -s $running_contr_file ]; then
2128 running_contr_file_empty=""
2129 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002130
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002131 # Kill all containers started by the test env - to speed up shut down
2132 docker kill $(docker ps -a --filter "label=nrttest_app" --format '{{.Names}}') &> /dev/null
2133
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002134 tab_heading1="App display name"
2135 tab_heading2="App short name"
2136 tab_heading3="Container name"
2137
2138 tab_heading1_len=${#tab_heading1}
2139 tab_heading2_len=${#tab_heading2}
2140 tab_heading3_len=${#tab_heading3}
2141 cntr=0
2142 #Calc field lengths of each item in the list of containers
2143 while read p; do
2144 if (( $cntr % 3 == 0 ));then
2145 if [ ${#p} -gt $tab_heading1_len ]; then
2146 tab_heading1_len=${#p}
2147 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002148 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002149 if (( $cntr % 3 == 1));then
2150 if [ ${#p} -gt $tab_heading2_len ]; then
2151 tab_heading2_len=${#p}
2152 fi
2153 fi
2154 if (( $cntr % 3 == 2));then
2155 if [ ${#p} -gt $tab_heading3_len ]; then
2156 tab_heading3_len=${#p}
2157 fi
2158 fi
2159 let cntr=cntr+1
2160 done <$running_contr_file
2161
2162 let tab_heading1_len=tab_heading1_len+2
2163 while (( ${#tab_heading1} < $tab_heading1_len)); do
2164 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002165 done
2166
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002167 let tab_heading2_len=tab_heading2_len+2
2168 while (( ${#tab_heading2} < $tab_heading2_len)); do
2169 tab_heading2="$tab_heading2"" "
2170 done
2171
2172 let tab_heading3_len=tab_heading3_len+2
2173 while (( ${#tab_heading3} < $tab_heading3_len)); do
2174 tab_heading3="$tab_heading3"" "
2175 done
2176
BjornMagnussonXAecf34532021-11-24 08:25:15 +01002177 if [ ! -z "$running_contr_file_empty" ]; then
2178 echo $running_contr_file_empty | indent1
2179 else
2180 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
2181 cntr=0
2182 while read p; do
2183 if (( $cntr % 3 == 0 ));then
2184 row=""
2185 heading=$p
2186 heading_len=$tab_heading1_len
2187 fi
2188 if (( $cntr % 3 == 1));then
2189 heading=$p
2190 heading_len=$tab_heading2_len
2191 fi
2192 if (( $cntr % 3 == 2));then
2193 contr=$p
2194 heading=$p
2195 heading_len=$tab_heading3_len
2196 fi
2197 while (( ${#heading} < $heading_len)); do
2198 heading="$heading"" "
2199 done
2200 row=$row$heading
2201 if (( $cntr % 3 == 2));then
2202 echo -ne $row$SAMELINE
2203 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
2204 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
2205 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
2206 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
2207 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
2208 fi
2209 let cntr=cntr+1
2210 done <$running_contr_file
2211 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002212
YongchaoWu9a84f512019-12-16 22:54:11 +01002213 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002214
2215 echo -e $BOLD" Removing docker network"$EBOLD
2216 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
2217 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002218 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002219 if [ $? -ne 0 ]; then
2220 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
2221 exit 1
2222 fi
2223 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002224 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002225
2226 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002227 docker network prune --force | indent2
2228 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002229
2230 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002231 docker volume prune --force | indent2
2232 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002233
2234 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
2235 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002236 echo -e "$GREEN Done$EGREEN"
2237 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02002238
2239 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
2240 if [ $? -eq 0 ]; then
2241 if [ $CONTRS -ne 0 ]; then
2242 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002243 docker ps -a | indent1
2244 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02002245 fi
2246 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002247}
2248
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002249###################################
2250### Functions for kube management
2251###################################
2252
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002253# Get resource type for scaling
2254# args: <resource-name> <namespace>
2255__kube_get_resource_type() {
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002256 kubectl $KUBECONF get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002257 if [ $? -eq 0 ]; then
2258 echo "deployment"
2259 return 0
2260 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002261 kubectl $KUBECONF get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA6f9c2b22021-06-11 16:31:40 +02002262 if [ $? -eq 0 ]; then
2263 echo "sts"
2264 return 0
2265 fi
2266 echo "unknown-resource-type"
2267 return 1
2268}
2269
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002270# Scale a kube resource to a specific count
2271# args: <resource-type> <resource-name> <namespace> <target-count>
2272# (Not for test scripts)
2273__kube_scale() {
2274 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002275 kubectl $KUBECONF scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002276 if [ $? -ne 0 ]; then
2277 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
2278 ((RES_CONF_FAIL++))
2279 echo " Message: $(<./tmp/kubeerr)"
2280 return 1
2281 else
2282 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
2283 fi
2284
2285 TSTART=$SECONDS
2286
2287 for i in {1..500}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002288 count=$(kubectl $KUBECONF get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002289 retcode=$?
2290 if [ -z "$count" ]; then
2291 #No value is sometimes returned for some reason, in case the resource has replica 0
2292 count=0
2293 fi
2294 if [ $retcode -ne 0 ]; then
2295 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
2296 ((RES_CONF_FAIL++))
2297 return 1
2298 fi
2299 #echo ""
2300 if [ $count -ne $4 ]; then
2301 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
2302 sleep $i
2303 else
2304 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
2305 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
2306 echo ""
2307 return 0
2308 fi
2309 done
2310 echo ""
2311 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
2312 ((RES_CONF_FAIL++))
2313 return 0
2314}
2315
2316# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
2317# This function does not wait for the resource to reach 0
2318# args: <namespace> <label-name> <label-id>
2319# (Not for test scripts)
2320__kube_scale_all_resources() {
2321 namespace=$1
2322 labelname=$2
2323 labelid=$3
2324 resources="deployment replicaset statefulset"
2325 for restype in $resources; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002326 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002327 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002328 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002329 echo -ne " Ordered caling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002330 kubectl $KUBECONF scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002331 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002332 done
2333 fi
2334 done
2335}
2336
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002337# 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 +01002338# This function do wait for the resource to reach 0
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002339# args: <namespace> <label-name> [ <label-id> ]
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002340# (Not for test scripts)
2341__kube_scale_and_wait_all_resources() {
2342 namespace=$1
2343 labelname=$2
2344 labelid=$3
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002345 if [ -z "$3" ]; then
2346 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname"
2347 else
2348 echo " Attempt to scale - deployment replicaset statefulset - in namespace $namespace with label $labelname=$labelid"
2349 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002350 resources="deployment replicaset statefulset"
2351 scaled_all=1
2352 while [ $scaled_all -ne 0 ]; do
2353 scaled_all=0
2354 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002355 if [ -z "$3" ]; then
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002356 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname')].metadata.name}')
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002357 else
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002358 result=$(kubectl $KUBECONF get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002359 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002360 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2361 for resid in $result; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002362 echo -e " Ordered scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0"
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002363 kubectl $KUBECONF scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002364 count=1
2365 T_START=$SECONDS
2366 while [ $count -ne 0 ]; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002367 count=$(kubectl $KUBECONF get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002368 echo -ne " Scaling $restype $resid in namespace $namespace with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002369 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2370 sleep 0.5
2371 else
2372 count=0
2373 fi
2374 duration=$(($SECONDS-$T_START))
2375 if [ $duration -gt 100 ]; then
2376 #Forcring count 0, to avoid hanging for failed scaling
2377 scaled_all=1
2378 count=0
2379 fi
2380 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002381 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 +01002382 done
2383 fi
2384 done
2385 done
2386}
2387
2388# Remove all kube resources in a namespace for resources having a certain label and label-id
2389# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
2390# args: <namespace> <label-name> <label-id>
2391# (Not for test scripts)
2392__kube_delete_all_resources() {
2393 namespace=$1
2394 labelname=$2
2395 labelid=$3
BjornMagnussonXA145762b2022-03-22 10:35:10 +01002396 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes serviceaccounts clusterrolebindings secrets authorizationpolicies requestauthentications"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002397 deleted_resourcetypes=""
2398 for restype in $resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002399 ns_flag="-n $namespace"
2400 ns_text="in namespace $namespace"
2401 if [ $restype == "persistentvolumes" ]; then
2402 ns_flag=""
2403 ns_text=""
2404 fi
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002405 if [ $restype == "clusterrolebindings" ]; then
2406 ns_flag=""
2407 ns_text=""
2408 fi
BjornMagnussonXA8fbc6362022-04-19 09:39:22 +02002409 result=$(kubectl $KUBECONF get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}' 2> /dev/null)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002410 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
2411 deleted_resourcetypes=$deleted_resourcetypes" "$restype
2412 for resid in $result; do
2413 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
2414 count=1
2415 while [ $count -ne 0 ]; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002416 count=$(kubectl $KUBECONF get $restype $resid $ns_flag -o jsonpath='{.status.replicas}' 2> /dev/null)
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002417 echo -ne " Scaling $restype $resid $ns_text with label $labelname=$labelid to 0, current count=$count"$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002418 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
2419 sleep 0.5
2420 else
2421 count=0
2422 fi
2423 done
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002424 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 +01002425 fi
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002426 echo -ne " Deleting $restype $resid $ns_text with label $labelname=$labelid "$SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002427 kubectl $KUBECONF delete --grace-period=1 $restype $resid $ns_flag 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002428 if [ $? -eq 0 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002429 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002430 else
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002431 echo -e " Deleted $restype $resid $ns_text with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002432 fi
2433 #fi
2434 done
2435 fi
2436 done
2437 if [ ! -z "$deleted_resourcetypes" ]; then
2438 for restype in $deleted_resources; do
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002439 ns_flag="-n $namespace"
2440 ns_text="in namespace $namespace"
2441 if [ $restype == "persistentvolumes" ]; then
2442 ns_flag=""
2443 ns_text=""
2444 fi
2445 echo -ne " Waiting for $restype $ns_text with label $labelname=$labelid to be deleted..."$SAMELINE
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002446 T_START=$SECONDS
2447 result="dummy"
2448 while [ ! -z "$result" ]; do
2449 sleep 0.5
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002450 result=$(kubectl $KUBECONF get $restype $ns_flag -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002451 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 +01002452 if [ -z "$result" ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002453 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 +01002454 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002455 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 +01002456 result=""
2457 fi
2458 done
2459 done
2460 fi
2461}
2462
2463# Creates a namespace if it does not exists
2464# args: <namespace>
2465# (Not for test scripts)
2466__kube_create_namespace() {
2467
2468 #Check if test namespace exists, if not create it
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002469 kubectl $KUBECONF get namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002470 if [ $? -ne 0 ]; then
2471 echo -ne " Creating namespace "$1 $SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002472 kubectl $KUBECONF create namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002473 if [ $? -ne 0 ]; then
2474 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2475 ((RES_CONF_FAIL++))
2476 echo " Message: $(<./tmp/kubeerr)"
2477 return 1
2478 else
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01002479 kubectl $KUBECONF label ns $1 autotest=engine > /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002480 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2481 fi
2482 else
2483 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
2484 fi
2485 return 0
2486}
2487
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002488# Removes a namespace if it exists
2489# args: <namespace>
2490# (Not for test scripts)
2491__kube_delete_namespace() {
2492
2493 #Check if test namespace exists, if so remove it
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002494 kubectl $KUBECONF get namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002495 if [ $? -eq 0 ]; then
2496 echo -ne " Removing namespace "$1 $SAMELINE
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002497 kubectl $KUBECONF delete namespace $1 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002498 if [ $? -ne 0 ]; then
2499 echo -e " Removing namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
2500 ((RES_CONF_FAIL++))
2501 echo " Message: $(<./tmp/kubeerr)"
2502 return 1
2503 else
2504 echo -e " Removing namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
2505 fi
2506 else
2507 echo -e " Namespace $1 $GREEN$BOLD does not exist, OK $EBOLD$EGREEN"
2508 fi
2509 return 0
2510}
2511
BjornMagnussonXA145762b2022-03-22 10:35:10 +01002512# Removes and re-create a namespace
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002513# args: <namespace>
2514# (Not for test scripts)
2515clean_and_create_namespace() {
2516 __log_conf_start $@
2517
2518 if [ $# -ne 1 ]; then
2519 __print_err "<namespace>" $@
2520 return 1
2521 fi
2522 __kube_delete_namespace $1
2523 if [ $? -ne 0 ]; then
2524 return 1
2525 fi
2526 __kube_create_namespace $1
2527 if [ $? -ne 0 ]; then
2528 return 1
2529 fi
BjornMagnussonXA145762b2022-03-22 10:35:10 +01002530}
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002531
BjornMagnussonXA145762b2022-03-22 10:35:10 +01002532# Add/remove label on non-namespaced kube object
2533# args: <api> <instance> <label>
2534# (Not for test scripts)
2535__kube_label_non_ns_instance() {
2536 kubectl $KUBECONF label $1 $2 "$3" 1> /dev/null 2> ./tmp/kubeerr
2537 return $?
2538}
2539
2540# Add/remove label on namespaced kube object
2541# args: <api> <instance> <namespace> <label>
2542# (Not for test scripts)
2543__kube_label_ns_instance() {
2544 kubectl $KUBECONF label $1 $2 -n $3 "$4" 1> /dev/null 2> ./tmp/kubeerr
2545 return $?
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002546}
2547
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002548# Find the host ip of an app (using the service resource)
2549# args: <app-name> <namespace>
2550# (Not for test scripts)
2551__kube_get_service_host() {
2552 if [ $# -ne 2 ]; then
2553 ((RES_CONF_FAIL++))
2554 __print_err "need 2 args, <app-name> <namespace>" $@
2555 exit 1
2556 fi
2557 for timeout in {1..60}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002558 host=$(kubectl $KUBECONF get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002559 if [ $? -eq 0 ]; then
2560 if [ ! -z "$host" ]; then
2561 echo $host
2562 return 0
2563 fi
2564 fi
2565 sleep 0.5
2566 done
2567 ((RES_CONF_FAIL++))
2568 echo "host-not-found-fatal-error"
2569 return 1
2570}
2571
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002572# Find the named port to an app (using the service resource)
2573# args: <app-name> <namespace> <port-name>
2574# (Not for test scripts)
2575__kube_get_service_port() {
2576 if [ $# -ne 3 ]; then
2577 ((RES_CONF_FAIL++))
2578 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2579 exit 1
2580 fi
2581
2582 for timeout in {1..60}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002583 port=$(kubectl $KUBECONF get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002584 if [ $? -eq 0 ]; then
2585 if [ ! -z "$port" ]; then
2586 echo $port
2587 return 0
2588 fi
2589 fi
2590 sleep 0.5
2591 done
2592 ((RES_CONF_FAIL++))
2593 echo "0"
2594 return 1
2595}
2596
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002597# Find the named node port to an app (using the service resource)
2598# args: <app-name> <namespace> <port-name>
2599# (Not for test scripts)
2600__kube_get_service_nodeport() {
2601 if [ $# -ne 3 ]; then
2602 ((RES_CONF_FAIL++))
2603 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
2604 exit 1
2605 fi
2606
2607 for timeout in {1..60}; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002608 port=$(kubectl $KUBECONF get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002609 if [ $? -eq 0 ]; then
2610 if [ ! -z "$port" ]; then
2611 echo $port
2612 return 0
2613 fi
2614 fi
2615 sleep 0.5
2616 done
2617 ((RES_CONF_FAIL++))
2618 echo "0"
2619 return 1
2620}
2621
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002622# Create a kube resource from a yaml template
2623# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
2624# (Not for test scripts)
2625__kube_create_instance() {
2626 echo -ne " Creating $1 $2"$SAMELINE
2627 envsubst < $3 > $4
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002628 kubectl $KUBECONF apply -f $4 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002629 if [ $? -ne 0 ]; then
2630 ((RES_CONF_FAIL++))
2631 echo -e " Creating $1 $2 $RED Failed $ERED"
2632 echo " Message: $(<./tmp/kubeerr)"
2633 return 1
2634 else
2635 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
2636 fi
2637}
2638
2639# Function to create a configmap in kubernetes
2640# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
2641# (Not for test scripts)
2642__kube_create_configmap() {
2643 echo -ne " Creating configmap $1 "$SAMELINE
2644 envsubst < $5 > $5"_tmp"
2645 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002646 kubectl $KUBECONF create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002647 if [ $? -ne 0 ]; then
2648 echo -e " Creating configmap $1 $RED Failed $ERED"
2649 ((RES_CONF_FAIL++))
2650 return 1
2651 fi
2652
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002653 kubectl $KUBECONF apply -f $6 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002654 if [ $? -ne 0 ]; then
2655 echo -e " Creating configmap $1 $RED Apply failed $ERED"
2656 echo " Message: $(<./tmp/kubeerr)"
2657 ((RES_CONF_FAIL++))
2658 return 1
2659 fi
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002660 kubectl $KUBECONF label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002661 if [ $? -ne 0 ]; then
2662 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
2663 echo " Message: $(<./tmp/kubeerr)"
2664 ((RES_CONF_FAIL++))
2665 return 1
2666 fi
2667 # Log the resulting map
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002668 kubectl $KUBECONF get configmap $1 -n $2 -o yaml > $6
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002669
2670 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
2671 return 0
2672}
2673
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002674# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
2675# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002676# args: <full kubectl cmd with parameters>
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002677# (Not for test scripts)
2678__kube_cmd_with_timeout() {
2679 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
2680
2681 while true; do
2682 kube_cmd_result=$($@)
2683 if [ $? -ne 0 ]; then
2684 kube_cmd_result=""
2685 fi
2686 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
2687 echo $kube_cmd_result
2688 return 0
2689 fi
2690 sleep 1
2691 done
2692}
2693
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002694# This function starts a pod that cleans a the contents of a path mounted as a pvc
2695# After this action the pod should terminate
2696# This should only be executed when the pod owning the pvc is not running
2697# args: <appname> <namespace> <pvc-name> <path-to remove>
2698# (Not for test scripts)
2699__kube_clean_pvc() {
2700
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002701 #using env vars setup in pvccleaner_api_functions.sh
2702
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002703 export PVC_CLEANER_NAMESPACE=$2
2704 export PVC_CLEANER_CLAIMNAME=$3
2705 export PVC_CLEANER_RM_PATH=$4
BjornMagnussonXAdfdca182021-12-10 10:43:32 +01002706 export PVC_CLEANER_APP_NAME
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002707 input_yaml=$SIM_GROUP"/"$PVC_CLEANER_COMPOSE_DIR"/"pvc-cleaner.yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002708 output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml
2709
2710 envsubst < $input_yaml > $output_yaml
2711
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002712 kubectl $KUBECONF delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002713
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002714 __kube_create_instance pod $PVC_CLEANER_APP_NAME $input_yaml $output_yaml
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002715 if [ $? -ne 0 ]; then
2716 echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work"
2717 return 1
2718 fi
2719
2720 term_ts=$(($SECONDS+30))
2721 while [ $term_ts -gt $SECONDS ]; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01002722 pod_status=$(kubectl $KUBECONF get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase")
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002723 if [ "$pod_status" == "Succeeded" ]; then
2724 return 0
2725 fi
2726 done
2727 return 1
2728}
2729
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002730# This function scales or deletes all resources for app selected by the testcase.
2731# args: -
2732# (Not for test scripts)
2733__clean_kube() {
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002734 echo -e $BOLD"Initialize kube pods/statefulsets/replicaset to initial state"$EBOLD
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002735
2736 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002737 for imagename in $APP_SHORT_NAMES; do
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002738 # A function name is created from the app short name
2739 # for example app short name 'RICMSIM' -> produce the function
2740 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
2741 # This function is called and is expected to exist in the imported
2742 # file for the ricsim test functions
2743 # The resulting function impl shall scale the resources to 0
2744 # For prestarted apps, the function waits until the resources are 0
2745 # For included (not prestated) apps, the scaling is just ordered
2746 __check_prestarted_image $imagename
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002747 if [ $? -eq 0 ]; then
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002748 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002749 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2750 $function_pointer
BjornMagnussonXAa5491572021-05-04 09:21:24 +02002751 else
2752 __check_included_image $imagename
2753 if [ $? -eq 0 ]; then
2754 function_pointer="__"$imagename"_kube_scale_zero"
2755 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
2756 $function_pointer
2757 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002758 fi
2759 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002760
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002761 # Delete managed apps
2762 for imagename in $APP_SHORT_NAMES; do
2763 __check_included_image $imagename
2764 if [ $? -eq 0 ]; then
2765 __check_prestarted_image $imagename
2766 if [ $? -ne 0 ]; then
2767 # A function name is created from the app short name
2768 # for example app short name 'RICMSIM' -> produce the function
2769 # name __RICSIM__kube_delete_all
2770 # This function is called and is expected to exist in the imported
2771 # file for the ricsim test functions
2772 # The resulting function impl shall delete all its resources
2773 function_pointer="__"$imagename"_kube_delete_all"
2774 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
2775 $function_pointer
2776 fi
2777 fi
2778 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002779
BjornMagnussonXAcd938442022-05-11 10:01:24 +02002780 # Remove istio label on namespaces
2781 test_env_namespaces=$(kubectl $KUBECONF get ns --no-headers -o custom-columns=":metadata.name" -l autotest=engine -l istio-injection=enabled) #Get list of ns created by the test env
2782 if [ $? -ne 0 ]; then
2783 echo " Cannot get list of namespaces...continues.."
2784 else
2785 for test_env_ns in $test_env_namespaces; do
2786 echo " Removing istio label on ns: "$test_env_ns
2787 __kube_label_non_ns_instance ns $test_env_ns "istio-injection-"
2788 done
2789 fi
2790
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002791 echo ""
2792}
2793
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002794# Function stop and remove all containers (docker) and services/deployments etc(kube)
2795# args: -
2796# Function for test script
2797clean_environment() {
2798 if [ $RUNMODE == "KUBE" ]; then
2799 __clean_kube
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002800 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002801 echo " Cleaning docker resouces to free up resources, may take time..."
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01002802 ../common/clean_docker.sh 2>&1 > /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002803 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002804 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002805 else
2806 __clean_containers
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002807 if [ $PRE_CLEAN -eq 1 ]; then
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002808 echo " Cleaning kubernetes resouces to free up resources, may take time..."
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01002809 ../common/clean_kube.sh $KUBECONF 2>&1 > /dev/null
BjornMagnussonXA6fc58fd2021-11-18 08:19:45 +01002810 echo ""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01002811 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002812 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002813}
2814
2815# 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 +01002816# args: -
2817# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002818auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002819 echo
2820 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002821 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
2822 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01002823 fi
2824}
2825
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002826# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002827# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002828# (Function for test scripts)
2829sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01002830
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002831 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
2832 if [ $# -lt 1 ]; then
2833 ((RES_CONF_FAIL++))
2834 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
2835 exit 1
2836 fi
2837 #echo "---- Sleep for " $1 " seconds ---- "$2
2838 start=$SECONDS
2839 duration=$((SECONDS-start))
2840 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002841 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002842 sleep 1
2843 duration=$((SECONDS-start))
2844 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002845 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002846 echo ""
2847}
2848
2849# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2850# Not to be called from the test script itself.
2851__print_err() {
2852 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2853 if [ $# -gt 1 ]; then
2854 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2855 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002856 ((RES_CONF_FAIL++))
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01002857 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002858}
2859
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002860# Function to create the docker network for the test
2861# Not to be called from the test script itself.
2862__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002863 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002864 if [ $? -ne 0 ]; then
2865 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2866 return 1
2867 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002868 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002869 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2870 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002871 if [ $? -ne 0 ]; then
2872 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2873 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002874 else
2875 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002876 fi
2877 else
2878 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2879 fi
2880}
2881
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002882# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002883# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2884#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002885# (Not for test scripts)
2886__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002887
2888 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002889 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002890 __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 +01002891 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002892 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002893
2894 __create_docker_network
2895
2896 curdir=$PWD
2897 cd $SIM_GROUP
2898 compose_dir=$1
2899 cd $1
2900 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002901 compose_file=$1
2902 if [ -z "$compose_file" ]; then
2903 compose_file="docker-compose.yml"
2904 fi
2905 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002906 compose_args=$1
2907 shift
2908 appcount=$1
2909 shift
2910
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002911 envsubst < $compose_file > "gen_"$compose_file
2912 compose_file="gen_"$compose_file
BjornMagnussonXAcd938442022-05-11 10:01:24 +02002913 if [ $DOCKER_COMPOSE_VERSION == "V1" ]; then
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002914 docker_compose_cmd="docker-compose"
2915 else
2916 docker_compose_cmd="docker compose"
2917 fi
BjornMagnussonXA79e37002021-11-22 13:36:04 +01002918
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002919 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002920 $docker_compose_cmd -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002921 if [ $? -ne 0 ]; then
2922 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2923 cat .dockererr
2924 echo -e $RED"Stopping script...."$ERED
2925 exit 1
2926 fi
2927 else
BjornMagnussonXAe60d04e2021-12-27 13:38:01 +01002928 $docker_compose_cmd -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002929 if [ $? -ne 0 ]; then
2930 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2931 cat .dockererr
2932 echo -e $RED"Stopping script...."$ERED
2933 exit 1
2934 fi
2935 fi
2936
2937 cd $curdir
2938
2939 appindex=0
2940 while [ $appindex -lt $appcount ]; do
2941 appname=$1
2942 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002943 app_started=0
2944 for i in {1..10}; do
2945 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002946 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 +02002947 app_started=1
2948 break
2949 else
2950 sleep $i
2951 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002952 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002953 if [ $app_started -eq 0 ]; then
2954 ((RES_CONF_FAIL++))
2955 echo ""
2956 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002957 echo -e $RED" Stopping script..."$ERED
2958 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002959 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002960 let appindex=appindex+1
2961 done
2962 return 0
2963}
2964
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002965# Function to check if container/service is responding to http/https
2966# args: <container-name>|<service-name> url
2967# (Not for test scripts)
2968__check_service_start() {
2969
2970 if [ $# -ne 2 ]; then
2971 ((RES_CONF_FAIL++))
2972 __print_err "need 2 args, <container-name>|<service-name> url" $@
2973 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002974 fi
2975
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002976 if [ $RUNMODE == "KUBE" ]; then
2977 ENTITY="service/set/deployment"
2978 else
2979 ENTITY="container"
2980 fi
2981 appname=$1
2982 url=$2
2983 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2984
2985
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01002986 a1pmsst=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002987 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002988 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002989 loop_ctr=0
2990 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002991 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002992 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002993 if [ ${#result} -gt 15 ]; then
2994 #If response is too long, truncate
2995 result="...response text too long, omitted"
2996 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002997 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2998 echo -ne " The ${ENTITY} $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN on ${url} after $(($SECONDS-$TSTART)) seconds"
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01002999 a1pmsst=true
YongchaoWu9a84f512019-12-16 22:54:11 +01003000 break
3001 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02003002 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01003003 TS_OFFSET=$loop_ctr
3004 if (( $TS_OFFSET > 5 )); then
3005 TS_OFFSET=5
3006 fi
3007 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
3008 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 +02003009 sleep 1
3010 done
YongchaoWu9a84f512019-12-16 22:54:11 +01003011 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01003012 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01003013 done
3014
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01003015 if [ "$a1pmsst" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003016 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003017 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 +01003018 return 1
3019 fi
3020
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003021 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02003022 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003023}
3024
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02003025
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003026#################
3027### Log functions
3028#################
3029
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003030__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003031
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003032 dispname=$1
3033 appname=$2
3034 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003035 warning=$4
3036 error=$5
3037
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003038 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
3039
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003040 if [ $RUNMODE == "KUBE" ]; then
3041 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
3042 return
3043 fi
3044
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003045 #tmp=$(docker ps | grep $appname)
3046 tmp=$(docker ps -q --filter name=$appname) #get the container id
BjornMagnussonXAd2aeca82022-03-07 11:04:55 +01003047 if [ -z "$tmp" ]; then #Only check logs for running A1PMS apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01003048 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003049 return
3050 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003051 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003052 if [ $? -ne 0 ];then
3053 echo " Problem to search $appname log $logpath"
3054 else
3055 if [ $foundentries -eq 0 ]; then
3056 echo " No WARN entries found in $appname log $logpath"
3057 else
3058 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
3059 fi
3060 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003061 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003062 if [ $? -ne 0 ];then
3063 echo " Problem to search $appname log $logpath"
3064 else
3065 if [ $foundentries -eq 0 ]; then
3066 echo " No ERR entries found in $appname log $logpath"
3067 else
3068 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
3069 fi
3070 fi
3071 echo ""
3072}
3073
3074# Store all container logs and other logs in the log dir for the script
3075# Logs are stored with a prefix in case logs should be stored several times during a test
3076# args: <logfile-prefix>
3077# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01003078store_logs() {
3079 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003080 ((RES_CONF_FAIL++))
3081 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01003082 exit 1
3083 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01003084 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 +01003085
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02003086 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003087
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003088 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003089
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003090 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
3091
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003092 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003093
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01003094 # Store docker logs for all container
3095 for imagename in $APP_SHORT_NAMES; do
3096 __check_included_image $imagename
3097 if [ $? -eq 0 ]; then
3098 # A function name is created from the app short name
3099 # for example app short name 'RICMSIM' -> produce the function
3100 # name __RICSIM__store_docker_logs
3101 # This function is called and is expected to exist in the imported
3102 # file for the ricsim test functions
3103 # The resulting function impl shall store the docker logs for each container
3104 function_pointer="__"$imagename"_store_docker_logs"
3105 $function_pointer "$TESTLOGS/$ATC/" $1
3106 fi
3107 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003108 fi
3109 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01003110 namespaces=$(kubectl $KUBECONF get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003111 for nsid in $namespaces; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01003112 pods=$(kubectl $KUBECONF get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003113 for podid in $pods; do
BjornMagnussonXAcb6113e2022-02-17 15:01:28 +01003114 kubectl $KUBECONF logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01003115 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003116 done
3117 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003118 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01003119}
3120
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003121###############
3122## Generic curl
3123###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003124# Generic curl function, assumes all 200-codes are ok
BjornMagnussonXA0a882452022-03-10 08:28:57 +01003125# Used proxy, set
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003126# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003127# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
3128# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01003129__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003130 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02003131 proxyflag=""
BjornMagnussonXA663566c2021-11-08 10:25:07 +01003132 if [ ! -z "$KUBE_PROXY_PATH" ]; then
3133 if [ $KUBE_PROXY_HTTPX == "http" ]; then
3134 proxyflag=" --proxy $KUBE_PROXY_PATH"
3135 else
3136 proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01003137 fi
3138 fi
BjornMagnussonXA145762b2022-03-22 10:35:10 +01003139
3140 if [ ! -z "$KUBE_PROXY_CURL_JWT" ]; then
3141 jwt="-H "\""Authorization: Bearer $KUBE_PROXY_CURL_JWT"\"
3142 curlString="curl -skw %{http_code} $proxyflag $@"
3143 echo " CMD: $curlString $jwt" >> $HTTPLOG
3144 res=$($curlString -H "Authorization: Bearer $KUBE_PROXY_CURL_JWT")
3145 retcode=$?
3146 else
3147 curlString="curl -skw %{http_code} $proxyflag $@"
3148 echo " CMD: $curlString" >> $HTTPLOG
3149 res=$($curlString)
3150 retcode=$?
3151 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003152 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02003153 echo " RETCODE: $retcode" >> $HTTPLOG
3154 if [ $retcode -ne 0 ]; then
3155 echo "<no-response-from-server>"
3156 return 1
3157 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01003158 http_code="${res:${#res}-3}"
3159 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003160 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
3161 echo "<no-response-from-server>"
3162 return 1
3163 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003164 return 0
3165 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01003166 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003167 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01003168 echo "<not found, resp:${http_code}>"
3169 return 1
3170 fi
3171 if [ $# -eq 2 ]; then
3172 echo "${res:0:${#res}-3}" | xargs
3173 else
3174 echo "${res:0:${#res}-3}"
3175 fi
3176
3177 return 0
3178 fi
BjornMagnussonXA145762b2022-03-22 10:35:10 +01003179
YongchaoWu9a84f512019-12-16 22:54:11 +01003180}
3181
BjornMagnussonXA0a882452022-03-10 08:28:57 +01003182# Generic curl function, assumes all 200-codes are ok
3183# Uses no proxy, even if it is set
3184# args: <valid-curl-args-including full url>
3185# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
3186# returns: The return code is 0 for ok and 1 for not ok
3187__do_curl_no_proxy() {
3188 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
3189 curlString="curl -skw %{http_code} $@"
3190 echo " CMD: $curlString" >> $HTTPLOG
3191 res=$($curlString)
3192 retcode=$?
3193 echo " RESP: $res" >> $HTTPLOG
3194 echo " RETCODE: $retcode" >> $HTTPLOG
3195 if [ $retcode -ne 0 ]; then
3196 echo "<no-response-from-server>"
3197 return 1
3198 fi
3199 http_code="${res:${#res}-3}"
3200 if [ ${#res} -eq 3 ]; then
3201 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
3202 echo "<no-response-from-server>"
3203 return 1
3204 else
3205 return 0
3206 fi
3207 else
3208 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
3209 echo "<not found, resp:${http_code}>"
3210 return 1
3211 fi
3212 if [ $# -eq 2 ]; then
3213 echo "${res:0:${#res}-3}" | xargs
3214 else
3215 echo "${res:0:${#res}-3}"
3216 fi
3217
3218 return 0
3219 fi
3220}
3221
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003222#######################################
3223### Basic helper function for test cases
3224#######################################
3225
3226# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
3227# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
3228# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
3229# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
3230# before setting pass or fail depending on the result of comparing variable and target using the operator.
3231# 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.
3232# Not to be called from test script.
3233
3234__var_test() {
3235 checkjsonarraycount=0
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02003236 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003237 if [ $# -eq 6 ]; then
3238 if [[ $3 == "json:"* ]]; then
3239 checkjsonarraycount=1
3240 fi
3241
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003242 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02003243 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG
3244
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003245 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003246 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003247 start=$SECONDS
3248 ctr=0
3249 for (( ; ; )); do
3250 if [ $checkjsonarraycount -eq 0 ]; then
3251 result="$(__do_curl $2$3)"
3252 retcode=$?
3253 result=${result//[[:blank:]]/} #Strip blanks
3254 else
3255 path=${3:5}
3256 result="$(__do_curl $2$path)"
3257 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003258 echo "$result" > ./tmp/.tmp.curl.json
3259 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003260 fi
3261 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003262 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003263 let ctr=ctr+1
3264 if [ $retcode -ne 0 ]; then
3265 if [ $duration -gt $6 ]; then
3266 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003267 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003268 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003269 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003270 return
3271 fi
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003272 elif [ "$4" == "=" ] && [ "$result" -eq $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003273 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003274 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003275 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003276 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003277 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003278 elif [ "$4" == ">" ] && [ "$result" -gt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003279 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003280 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003281 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003282 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003283 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003284 elif [ "$4" == "<" ] && [ "$result" -lt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003285 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003286 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003287 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003288 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003289 return
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003290 elif [ "$4" == ">=" ] && [ "$result" -ge $5 ]; then
3291 ((RES_PASS++))
3292 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
3293 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
3294 __print_current_stats
3295 return
3296 elif [ "$4" == "contain_str" ] && [[ $result =~ $5 ]]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003297 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02003298 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003299 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003300 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003301 return
3302 else
3303 if [ $duration -gt $6 ]; then
3304 ((RES_FAIL++))
3305 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003306 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003307 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003308 return
3309 fi
3310 fi
3311 sleep 1
3312 done
3313 elif [ $# -eq 5 ]; then
3314 if [[ $3 == "json:"* ]]; then
3315 checkjsonarraycount=1
3316 fi
3317
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003318 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA3cc0b582021-08-30 10:46:41 +02003319 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003320 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01003321 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003322 if [ $checkjsonarraycount -eq 0 ]; then
3323 result="$(__do_curl $2$3)"
3324 retcode=$?
3325 result=${result//[[:blank:]]/} #Strip blanks
3326 else
3327 path=${3:5}
3328 result="$(__do_curl $2$path)"
3329 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02003330 echo "$result" > ./tmp/.tmp.curl.json
3331 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003332 fi
3333 if [ $retcode -ne 0 ]; then
3334 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003335 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003336 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003337 __check_stop_at_error
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003338 elif [ "$4" == "=" ] && [ "$result" -eq $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003339 ((RES_PASS++))
3340 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003341 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003342 elif [ "$4" == ">" ] && [ "$result" -gt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003343 ((RES_PASS++))
3344 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003345 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003346 elif [ "$4" == "<" ] && [ "$result" -lt $5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003347 ((RES_PASS++))
3348 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003349 __print_current_stats
BjornMagnussonXA8fbb2262022-01-24 15:20:15 +01003350 elif [ "$4" == ">=" ] && [ "$result" -ge $5 ]; then
3351 ((RES_PASS++))
3352 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
3353 __print_current_stats
3354 elif [ "$4" == "contain_str" ] && [[ $result =~ $5 ]]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003355 ((RES_PASS++))
3356 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003357 __print_current_stats
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003358 else
3359 ((RES_FAIL++))
3360 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXAfec823b2021-08-03 14:14:05 +02003361 __print_current_stats
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02003362 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003363 fi
3364 else
3365 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
3366 echo "Got:" $@
3367 exit 1
3368 fi
3369}