blob: 1f6d135f726f3fe6d25df92a37fbf289ad327c2d [file] [log] [blame]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001#!/bin/bash
YongchaoWu9a84f512019-12-16 22:54:11 +01002
BjornMagnussonXA80a92002020-03-19 14:31:06 +01003# ============LICENSE_START===============================================
4# Copyright (C) 2020 Nordix Foundation. All rights reserved.
5# ========================================================================
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ============LICENSE_END=================================================
18#
YongchaoWu9a84f512019-12-16 22:54:11 +010019
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010020# This is a script that contains all the common functions needed for auto test.
21# Specific test function are defined in scripts XXXX_functions.sh
22
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010023. ../common/api_curl.sh
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010024. ../common/testengine_config.sh
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010025
26__print_args() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010027 echo "Args: remote|remote-remove docker|kube --env-file <environment-filename> [release] [auto-clean] [--stop-at-error] "
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010028 echo " [--ricsim-prefix <prefix> ] [--use-local-image <app-nam>+] [--use-snapshot-image <app-nam>+]"
BjornMagnussonXA483ee332021-04-08 01:35:24 +020029 echo " [--use-staging-image <app-nam>+] [--use-release-image <app-nam>+] [--image-repo <repo-address]"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +020030 echo " [--cluster-timeout <timeout-in seconds>]"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010031}
32
33if [ $# -eq 1 ] && [ "$1" == "help" ]; then
34
35 if [ ! -z "$TC_ONELINE_DESCR" ]; then
36 echo "Test script description:"
37 echo $TC_ONELINE_DESCR
38 echo ""
39 fi
40 __print_args
41 echo ""
42 echo "remote - Use images from remote repositories. Can be overridden for individual images using the '--use_xxx' flags"
43 echo "remote-remove - Same as 'remote' but will also try to pull fresh images from remote repositories"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010044 echo "docker - Test executed in docker environment"
45 echo "kube - Test executed in kubernetes environment - requires an already started kubernetes environment"
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010046 echo "--env-file - The script will use the supplied file to read environment variables from"
47 echo "release - If this flag is given the script will use release version of the images"
48 echo "auto-clean - If the function 'auto_clean_containers' is present in the end of the test script then all containers will be stopped and removed. If 'auto-clean' is not given then the function has no effect."
49 echo "--stop-at-error - The script will stop when the first failed test or configuration"
50 echo "--ricsim-prefix - The a1 simulator will use the supplied string as container prefix instead of 'ricsim'"
51 echo "--use-local-image - The script will use local images for the supplied apps, space separated list of app short names"
52 echo "--use-snapshot-image - The script will use images from the nexus snapshot repo for the supplied apps, space separated list of app short names"
53 echo "--use-staging-image - The script will use images from the nexus staging repo for the supplied apps, space separated list of app short names"
54 echo "--use-release-image - The script will use images from the nexus release repo for the supplied apps, space separated list of app short names"
BjornMagnussonXAa69cd902021-04-22 23:46:10 +020055 echo "--image-repo - Url to optional image repo. Only locally built images will be re-tagged and pushed to this repo"
56 echo "--cluster-timeout - Optional timeout for cluster where it takes time to obtain external ip/host-name. Timeout in seconds. "
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010057 echo ""
58 echo "List of app short names supported: "$APP_SHORT_NAMES
59 exit 0
60fi
61
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010062AUTOTEST_HOME=$PWD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +020063# Create a test case id, ATC (Auto Test Case), from the name of the test case script.
64# FTC1.sh -> ATC == FTC1
65ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh)
66
67#Create result file (containing '1' for error) for this test case
68#Will be replaced with a file containing '0' if all test cases pass
69echo "1" > "$PWD/.result$ATC.txt"
70
BjornMagnussonXA80a92002020-03-19 14:31:06 +010071#Formatting for 'echo' cmd
72BOLD="\033[1m"
73EBOLD="\033[0m"
74RED="\033[31m\033[1m"
75ERED="\033[0m"
76GREEN="\033[32m\033[1m"
77EGREEN="\033[0m"
78YELLOW="\033[33m\033[1m"
79EYELLOW="\033[0m"
BjornMagnussonXA72667f12020-04-24 09:20:18 +020080SAMELINE="\033[0K\r"
81
BjornMagnussonXA80a92002020-03-19 14:31:06 +010082# Just resetting any previous echo formatting...
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020083echo -ne $EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +010084
BjornMagnussonXA575869c2020-09-14 21:28:54 +020085# default test environment variables
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +020086TEST_ENV_VAR_FILE=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +010087
88echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@
89
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +010090#Localhost constants
91LOCALHOST_NAME="localhost"
92LOCALHOST_HTTP="http://localhost"
93LOCALHOST_HTTPS="https://localhost"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +020094
BjornMagnussonXA80a92002020-03-19 14:31:06 +010095# Var to hold 'auto' in case containers shall be stopped when test case ends
96AUTO_CLEAN=""
YongchaoWu9a84f512019-12-16 22:54:11 +010097
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +010098# Var to hold the app names to use local images for
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020099USE_LOCAL_IMAGES=""
100
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100101# Var to hold the app names to use remote snapshot images for
102USE_SNAPSHOT_IMAGES=""
103
104# Var to hold the app names to use remote staging images for
105USE_STAGING_IMAGES=""
106
107# Var to hold the app names to use remote release images for
108USE_RELEASE_IMAGES=""
109
BjornMagnussonXAad047782020-06-08 15:54:11 +0200110
BjornMagnussonXA048aaa12020-06-04 07:48:37 +0200111# 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
112STOP_AT_ERROR=0
113
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100114# The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used.
115# The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used
116# Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE
117IMAGE_CATEGORY="DEV"
118
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200119# Function to indent cmd output with one space
120indent1() { sed 's/^/ /'; }
121
122# Function to indent cmd output with two spaces
123indent2() { sed 's/^/ /'; }
124
YongchaoWu9a84f512019-12-16 22:54:11 +0100125# Set a description string for the test case
126if [ -z "$TC_ONELINE_DESCR" ]; then
127 TC_ONELINE_DESCR="<no-description>"
128 echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR
129fi
130
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100131# Counter for test suites
132if [ -f .tmp_tcsuite_ctr ]; then
133 tmpval=$(< .tmp_tcsuite_ctr)
134 ((tmpval++))
135 echo $tmpval > .tmp_tcsuite_ctr
136fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100137
YongchaoWu9a84f512019-12-16 22:54:11 +0100138# Create the logs dir if not already created in the current dir
139if [ ! -d "logs" ]; then
140 mkdir logs
141fi
YongchaoWu9a84f512019-12-16 22:54:11 +0100142TESTLOGS=$PWD/logs
143
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200144# Create the tmp dir for temporary files that is not needed after the test
145# hidden files for the test env is still stored in the current dir
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100146# files in the ./tmp is moved to ./tmp/prev when a new test is started
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200147if [ ! -d "tmp" ]; then
148 mkdir tmp
149fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100150curdir=$PWD
151cd tmp
152if [ $? -ne 0 ]; then
153 echo "Cannot cd to $PWD/tmp"
154 echo "Dir cannot be created. Exiting...."
155fi
156if [ ! -d "prev" ]; then
157 mkdir prev
158fi
159cd $curdir
160mv ./tmp/* ./tmp/prev 2> /dev/null
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200161
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100162# Create a http message log for this testcase
163HTTPLOG=$PWD"/.httplog_"$ATC".txt"
164echo "" > $HTTPLOG
165
166# Create a log dir for the test case
YongchaoWu9a84f512019-12-16 22:54:11 +0100167mkdir -p $TESTLOGS/$ATC
168
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100169# Save create for current logs
170mkdir -p $TESTLOGS/$ATC/previous
171
172rm $TESTLOGS/$ATC/previous/*.log &> /dev/null
173rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null
174rm $TESTLOGS/$ATC/previous/*.json &> /dev/null
175
176mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null
177mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
178mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null
179
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100180# Clear the log dir for the test case
181rm $TESTLOGS/$ATC/*.log &> /dev/null
182rm $TESTLOGS/$ATC/*.txt &> /dev/null
183rm $TESTLOGS/$ATC/*.json &> /dev/null
184
185# Log all output from the test case to a TC log
YongchaoWu9a84f512019-12-16 22:54:11 +0100186TCLOG=$TESTLOGS/$ATC/TC.log
187exec &> >(tee ${TCLOG})
188
189#Variables for counting tests as well as passed and failed tests
190RES_TEST=0
191RES_PASS=0
192RES_FAIL=0
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100193RES_CONF_FAIL=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200194RES_DEVIATION=0
195
196#File to keep deviation messages
197DEVIATION_FILE=".tmp_deviations"
198rm $DEVIATION_FILE &> /dev/null
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100199
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100200# Trap "command not found" and make the script fail
201trap_fnc() {
202
203 if [ $? -eq 127 ]; then
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100204 echo -e $RED"Function not found, setting script to FAIL"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100205 ((RES_CONF_FAIL++))
206 fi
207}
208trap trap_fnc ERR
209
210# Counter for tests
211TEST_SEQUENCE_NR=1
212
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100213# Function to log the start of a test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100214__log_test_start() {
215 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
216 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD
217 echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG
218 ((RES_TEST++))
219 ((TEST_SEQUENCE_NR++))
220}
221
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100222# General function to log a failed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100223__log_test_fail_general() {
224 echo -e $RED" FAIL."$1 $ERED
225 ((RES_FAIL++))
226 __check_stop_at_error
227}
228
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100229# Function to log a test case failed due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100230__log_test_fail_status_code() {
231 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
232 ((RES_FAIL++))
233 __check_stop_at_error
234}
235
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100236# Function to log a test case failed due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100237__log_test_fail_body() {
238 echo -e $RED" FAIL, returned body not correct"$ERED
239 ((RES_FAIL++))
240 __check_stop_at_error
241}
242
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100243# Function to log a test case that is not supported
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100244__log_test_fail_not_supported() {
245 echo -e $RED" FAIL, function not supported"$ERED
246 ((RES_FAIL++))
247 __check_stop_at_error
248}
249
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100250# General function to log a passed test case
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100251__log_test_pass() {
252 if [ $# -gt 0 ]; then
253 echo $@
254 fi
255 ((RES_PASS++))
256 echo -e $GREEN" PASS"$EGREEN
257}
258
259#Counter for configurations
260CONF_SEQUENCE_NR=1
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100261
262# Function to log the start of a configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100263__log_conf_start() {
264 TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
265 echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD
266 echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG
267 ((CONF_SEQUENCE_NR++))
268}
269
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100270# Function to log a failed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100271__log_conf_fail_general() {
272 echo -e $RED" FAIL."$1 $ERED
273 ((RES_CONF_FAIL++))
274 __check_stop_at_error
275}
276
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100277# Function to log a failed configuration setup due to incorrect response code
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100278__log_conf_fail_status_code() {
279 echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED
280 ((RES_CONF_FAIL++))
281 __check_stop_at_error
282}
283
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100284# Function to log a failed configuration setup due to incorrect response body
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100285__log_conf_fail_body() {
286 echo -e $RED" FAIL, returned body not correct"$ERED
287 ((RES_CONF_FAIL++))
288 __check_stop_at_error
289}
290
BjornMagnussonXA366e36a2021-01-27 11:48:56 +0100291# Function to log a passed configuration setup
BjornMagnussonXA7b36db62020-11-23 10:57:57 +0100292__log_conf_ok() {
293 if [ $# -gt 0 ]; then
294 echo $@
295 fi
296 echo -e $GREEN" OK"$EGREEN
297}
298
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100299#Var for measuring execution time
YongchaoWu9a84f512019-12-16 22:54:11 +0100300TCTEST_START=$SECONDS
301
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200302#File to save timer measurement results
303TIMER_MEASUREMENTS=".timer_measurement.txt"
304echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS
305
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200306# If this is set, all used images will be re-tagged and pushed to this repo before any
307IMAGE_REPO_ADR=""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200308CLUSTER_TIME_OUT=0
BjornMagnussonXA70e878f2020-05-11 14:11:30 +0200309
YongchaoWu9a84f512019-12-16 22:54:11 +0100310echo "-------------------------------------------------------------------------------------------------"
311echo "----------------------------------- Test case: "$ATC
312echo "----------------------------------- Started: "$(date)
313echo "-------------------------------------------------------------------------------------------------"
314echo "-- Description: "$TC_ONELINE_DESCR
315echo "-------------------------------------------------------------------------------------------------"
316echo "----------------------------------- Test case setup -----------------------------------"
317
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100318echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200319START_ARG=$1
320paramerror=0
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100321paramerror_str=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200322if [ $# -lt 1 ]; then
323 paramerror=1
324fi
325if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100326 if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200327 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100328 if [ -z "$paramerror_str" ]; then
329 paramerror_str="First arg shall be 'remote' or 'remote-remove'"
330 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200331 else
332 shift;
333 fi
334fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100335if [ $paramerror -eq 0 ]; then
336 if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then
337 paramerror=1
338 if [ -z "$paramerror_str" ]; then
339 paramerror_str="Second arg shall be 'docker' or 'kube'"
340 fi
341 else
342 if [ $1 == "docker" ]; then
343 RUNMODE="DOCKER"
344 echo "Setting RUNMODE=DOCKER"
345 fi
346 if [ $1 == "kube" ]; then
347 RUNMODE="KUBE"
348 echo "Setting RUNMODE=KUBE"
349 fi
350 shift;
351 fi
352fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200353foundparm=0
354while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do
355 foundparm=1
356 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100357 if [ "$1" == "release" ]; then
358 IMAGE_CATEGORY="RELEASE"
359 echo "Option set - Release image tags used for applicable images "
360 shift;
361 foundparm=0
362 fi
363 fi
364 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200365 if [ "$1" == "auto-clean" ]; then
366 AUTO_CLEAN="auto"
367 echo "Option set - Auto clean at end of test script"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200368 shift;
BjornMagnussonXAad047782020-06-08 15:54:11 +0200369 foundparm=0
370 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200371 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200372 if [ $paramerror -eq 0 ]; then
373 if [ "$1" == "--stop-at-error" ]; then
374 STOP_AT_ERROR=1
375 echo "Option set - Stop at first error"
376 shift;
377 foundparm=0
378 fi
379 fi
380 if [ $paramerror -eq 0 ]; then
381 if [ "$1" == "--ricsim-prefix" ]; then
382 shift;
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100383 TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file
BjornMagnussonXAad047782020-06-08 15:54:11 +0200384 if [ -z "$1" ]; then
385 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100386 if [ -z "$paramerror_str" ]; then
387 paramerror_str="No prefix found for flag: '--ricsim-prefix'"
388 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200389 else
390 echo "Option set - Overriding RIC_SIM_PREFIX with: "$1
391 shift;
392 foundparm=0
393 fi
394 fi
395 fi
396 if [ $paramerror -eq 0 ]; then
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200397 if [ "$1" == "--env-file" ]; then
398 shift;
399 TEST_ENV_VAR_FILE=$1
400 if [ -z "$1" ]; then
401 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100402 if [ -z "$paramerror_str" ]; then
403 paramerror_str="No env file found for flag: '--env-file'"
404 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200405 else
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200406 echo "Option set - Reading test env from: "$1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200407 shift;
408 foundparm=0
409 fi
410 fi
411 fi
412 if [ $paramerror -eq 0 ]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200413 if [ "$1" == "--use-local-image" ]; then
414 USE_LOCAL_IMAGES=""
415 shift
416 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
417 USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100418 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
BjornMagnussonXAad047782020-06-08 15:54:11 +0200419 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100420 if [ -z "$paramerror_str" ]; then
421 paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'"
422 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200423 fi
424 shift;
425 done
426 foundparm=0
427 if [ -z "$USE_LOCAL_IMAGES" ]; then
428 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100429 if [ -z "$paramerror_str" ]; then
430 paramerror_str="No app name found for flag: '--use-local-image'"
431 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200432 else
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100433 echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES
434 fi
435 fi
436 fi
437 if [ $paramerror -eq 0 ]; then
438 if [ "$1" == "--use-snapshot-image" ]; then
439 USE_SNAPSHOT_IMAGES=""
440 shift
441 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
442 USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1
443 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
444 paramerror=1
445 if [ -z "$paramerror_str" ]; then
446 paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'"
447 fi
448 fi
449 shift;
450 done
451 foundparm=0
452 if [ -z "$USE_SNAPSHOT_IMAGES" ]; then
453 paramerror=1
454 if [ -z "$paramerror_str" ]; then
455 paramerror_str="No app name found for flag: '--use-snapshot-image'"
456 fi
457 else
458 echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES
459 fi
460 fi
461 fi
462 if [ $paramerror -eq 0 ]; then
463 if [ "$1" == "--use-staging-image" ]; then
464 USE_STAGING_IMAGES=""
465 shift
466 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
467 USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1
468 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
469 paramerror=1
470 if [ -z "$paramerror_str" ]; then
471 paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'"
472 fi
473 fi
474 shift;
475 done
476 foundparm=0
477 if [ -z "$USE_STAGING_IMAGES" ]; then
478 paramerror=1
479 if [ -z "$paramerror_str" ]; then
480 paramerror_str="No app name found for flag: '--use-staging-image'"
481 fi
482 else
483 echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES
484 fi
485 fi
486 fi
487 if [ $paramerror -eq 0 ]; then
488 if [ "$1" == "--use-release-image" ]; then
489 USE_RELEASE_IMAGES=""
490 shift
491 while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do
492 USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1
493 if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then
494 paramerror=1
495 if [ -z "$paramerror_str" ]; then
496 paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'"
497 fi
498 fi
499 shift;
500 done
501 foundparm=0
502 if [ -z "$USE_RELEASE_IMAGES" ]; then
503 paramerror=1
504 if [ -z "$paramerror_str" ]; then
505 paramerror_str="No app name found for flag: '--use-release-image'"
506 fi
507 else
508 echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES
BjornMagnussonXAad047782020-06-08 15:54:11 +0200509 fi
510 fi
511 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200512 if [ $paramerror -eq 0 ]; then
513 if [ "$1" == "--image-repo" ]; then
514 shift;
515 IMAGE_REPO_ADR=$1
516 if [ -z "$1" ]; then
517 paramerror=1
518 if [ -z "$paramerror_str" ]; then
519 paramerror_str="No image repo url found for : '--image-repo'"
520 fi
521 else
522 echo "Option set - Image repo url: "$1
523 shift;
524 foundparm=0
525 fi
526 fi
527 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200528 if [ $paramerror -eq 0 ]; then
529 if [ "$1" == "--cluster-timeout" ]; then
530 shift;
531 CLUSTER_TIME_OUT=$1
532 if [ -z "$1" ]; then
533 paramerror=1
534 if [ -z "$paramerror_str" ]; then
535 paramerror_str="No timeout value found for : '--cluster-timeout'"
536 fi
537 else
538 #Check if positive int
539 case ${CLUSTER_TIME_OUT#[+]} in
540 *[!0-9]* | '')
541 paramerror=1
542 if [ -z "$paramerror_str" ]; then
543 paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT
544 fi
545 ;;
546 * ) ;; # Ok
547 esac
548 echo "Option set - Cluster timeout: "$1
549 shift;
550 foundparm=0
551 fi
552 fi
553 fi
BjornMagnussonXAad047782020-06-08 15:54:11 +0200554done
555echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200556
BjornMagnussonXAad047782020-06-08 15:54:11 +0200557#Still params left?
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200558if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then
559 paramerror=1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100560 if [ -z "$paramerror_str" ]; then
561 paramerror_str="Unknown parameter(s): "$@
562 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200563fi
564
565if [ $paramerror -eq 1 ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100566 echo -e $RED"Incorrect arg list: "$paramerror_str$ERED
567 __print_args
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200568 exit 1
569fi
570
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200571# sourcing the selected env variables for the test case
572if [ -f "$TEST_ENV_VAR_FILE" ]; then
573 echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD
574 . $TEST_ENV_VAR_FILE
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100575
576 if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100577 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 +0100578 else
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100579 found_profile=0
580 for prof in $SUPPORTED_PROFILES; do
581 if [ "$TEST_ENV_PROFILE" == "$prof" ]; then
582 echo -e $GREEN"Test case supports the selected test env file"$EGREEN
583 found_profile=1
584 fi
585 done
586 if [ $found_profile -ne 1 ]; then
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100587 echo -e $RED"Test case does not support the selected test env file"$ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100588 echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +0100589 echo -e $RED"Exiting...."$ERED
590 exit 1
591 fi
592 fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200593else
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200594 echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED
BjornMagnussonXA39ad50e2020-10-22 09:55:25 +0200595 echo " Select one of following env var file matching the intended target of the test"
596 echo " Restart the test using the flag '--env-file <path-to-env-file>"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100597 ls $AUTOTEST_HOME/../common/test_env* | indent1
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200598 exit 1
599fi
600
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100601#This var need be preserved from the command line option, if set, when env var is sourced.
602if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then
603 RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX
604fi
605
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100606if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100607 echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100608 exit 1
609fi
610
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100611if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then
612 echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED
613 echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES
614 exit 1
615fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100616
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100617# Choose list of included apps depending on run-mode
618if [ $RUNMODE == "KUBE" ]; then
619 INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES
620else
621 INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES
622fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200623
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100624# Check needed installed sw
625tmp=$(which python3)
626if [ $? -ne 0 ] || [ -z tmp ]; then
627 echo -e $RED"python3 is required to run the test environment, pls install"$ERED
628 exit 1
629fi
630tmp=$(which docker)
631if [ $? -ne 0 ] || [ -z tmp ]; then
632 echo -e $RED"docker is required to run the test environment, pls install"$ERED
633 exit 1
634fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200635
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100636tmp=$(which docker-compose)
637if [ $? -ne 0 ] || [ -z tmp ]; then
638 if [ $RUNMODE == "DOCKER" ]; then
639 echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED
640 exit 1
641 fi
642fi
BjornMagnussonXA575869c2020-09-14 21:28:54 +0200643
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100644tmp=$(which kubectl)
645if [ $? -ne 0 ] || [ -z tmp ]; then
646 if [ $RUNMODE == "KUBE" ]; then
647 echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED
648 exit 1
649 fi
650fi
BjornMagnussonXAde4d0f82020-11-29 16:04:06 +0100651
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100652echo -e $BOLD"Checking configured image setting for this test case"$EBOLD
YongchaoWu9a84f512019-12-16 22:54:11 +0100653
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100654#Temp var to check for image variable name errors
655IMAGE_ERR=0
656#Create a file with image info for later printing as a table
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200657image_list_file="./tmp/.image-list"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100658echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100659
660# 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 +0100661# arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100662__check_and_create_image_var() {
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200663
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200664 if [ $# -ne 6 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100665 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 +0100666 ((IMAGE_ERR++))
667 return
668 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100669
670 __check_included_image $1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200671 if [ $? -ne 0 ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100672 echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +0200673 # Image is excluded since the corresponding app is not used in this test
674 return
675 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100676 tmp=${6}"\t"${1}"\t"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100677 #Create var from the input var names
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100678 image="${!3}"
679 tmptag=$4"_"$5
680 tag="${!tmptag}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100681
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200682 optional_image_repo_target=""
683
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100684 if [ -z $image ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100685 __check_ignore_image $1
686 if [ $? -eq 0 ]; then
687 app_ds=$6
688 if [ -z "$6" ]; then
689 app_ds="<app ignored>"
690 fi
691 echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file
692 # Image is ignored since the corresponding the images is not set in the env file
693 __remove_included_image $1 # Remove the image from the list of included images
694 return
695 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100696 echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED
697 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100698 echo ""
699 tmp=$tmp"<no-image>\t"
700 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200701
702 optional_image_repo_target=$image
703
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100704 #Add repo depending on image type
705 if [ "$5" == "REMOTE_RELEASE" ]; then
706 image=$NEXUS_RELEASE_REPO$image
707 fi
708 if [ "$5" == "REMOTE" ]; then
709 image=$NEXUS_STAGING_REPO$image
710 fi
711 if [ "$5" == "REMOTE_SNAPSHOT" ]; then
712 image=$NEXUS_SNAPSHOT_REPO$image
713 fi
714 if [ "$5" == "REMOTE_PROXY" ]; then
715 image=$NEXUS_PROXY_REPO$image
716 fi
717 if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then
718 image=$NEXUS_RELEASE_REPO_ONAP$image
719 fi
720 if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then
721 image=$NEXUS_RELEASE_REPO_ORAN$image
722 fi
723 #No nexus repo added for local images, tag: LOCAL
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100724 tmp=$tmp$image"\t"
725 fi
726 if [ -z $tag ]; then
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100727 echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED
728 ((IMAGE_ERR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100729 echo ""
730 tmp=$tmp"<no-tag>\t"
731 else
732 tmp=$tmp$tag
733 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100734 tmp=$tmp"\t"$5
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100735 echo -e "$tmp" >> $image_list_file
736 #Export the env var
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200737 export "${2}"=$image":"$tag #Note, this var may be set to the value of the target value below in __check_and_pull_image
BjornMagnussonXAa69cd902021-04-22 23:46:10 +0200738 if [ ! -z "$IMAGE_REPO_ADR" ] && [ $5 == "LOCAL" ]; then # Only push local images if repo is given
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200739 export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image
740 export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed
741 else
742 export "${2}_SOURCE"=""
743 export "${2}_TARGET"=""
744 fi
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200745}
746
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200747# Check if app uses image included in this test run
748# Returns 0 if image is included, 1 if not
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200749__check_included_image() {
750 for im in $INCLUDED_IMAGES; do
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200751 if [ "$1" == "$im" ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200752 return 0
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200753 fi
754 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +0200755 return 1
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200756}
757
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100758# Check if app uses a project image
759# Returns 0 if image is included, 1 if not
760__check_project_image() {
761 for im in $PROJECT_IMAGES; do
762 if [ "$1" == "$im" ]; then
763 return 0
764 fi
765 done
766 return 1
767}
768
769# Check if app uses image built by the test script
770# Returns 0 if image is included, 1 if not
771__check_image_local_build() {
772 for im in $LOCAL_IMAGE_BUILD; do
773 if [ "$1" == "$im" ]; then
774 return 0
775 fi
776 done
777 return 1
778}
779
780# Check if app image is conditionally ignored in this test run
781# Returns 0 if image is conditionally ignored, 1 if not
782__check_ignore_image() {
783 for im in $CONDITIONALLY_IGNORED_IMAGES; do
784 if [ "$1" == "$im" ]; then
785 return 0
786 fi
787 done
788 return 1
789}
790
791# Removed image from included list of included images
792# Used when an image is marked as conditionally ignored
793__remove_included_image() {
794 tmp_img_rem_list=""
795 for im in $INCLUDED_IMAGES; do
796 if [ "$1" != "$im" ]; then
797 tmp_img_rem_list=$tmp_img_rem_list" "$im
798 fi
799 done
800 INCLUDED_IMAGES=$tmp_img_rem_list
801 return 0
802}
803
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100804# Check if app is included in the prestarted set of apps
805# Returns 0 if image is included, 1 if not
806__check_prestarted_image() {
807 for im in $KUBE_PRESTARTED_IMAGES; do
808 if [ "$1" == "$im" ]; then
809 return 0
810 fi
811 done
812 return 1
813}
814
815# Check if an app shall use a local image, based on the cmd parameters
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100816__check_image_local_override() {
817 for im in $USE_LOCAL_IMAGES; do
818 if [ "$1" == "$im" ]; then
819 return 1
820 fi
821 done
822 return 0
823}
824
825# Check if app uses image override
826# Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image
827__check_image_override() {
828
829 for im in $ORAN_IMAGES_APP_NAMES; do
830 if [ "$1" == "$im" ]; then
831 echo "REMOTE_RELEASE_ORAN"
832 return 0
833 fi
834 done
835
836 for im in $ONAP_IMAGES_APP_NAMES; do
837 if [ "$1" == "$im" ]; then
838 echo "REMOTE_RELEASE_ONAP"
839 return 0
840 fi
841 done
842
843 found=0
844 for im in $PROJECT_IMAGES_APP_NAMES; do
845 if [ "$1" == "$im" ]; then
846 found=1
847 fi
848 done
849
850 if [ $found -eq 0 ]; then
851 echo "REMOTE_PROXY"
852 return 0
853 fi
854
855 suffix=""
856 if [ $IMAGE_CATEGORY == "RELEASE" ]; then
857 suffix="REMOTE_RELEASE"
858 fi
859 if [ $IMAGE_CATEGORY == "DEV" ]; then
860 suffix="REMOTE"
861 fi
862 CTR=0
863 for im in $USE_STAGING_IMAGES; do
864 if [ "$1" == "$im" ]; then
865 suffix="REMOTE"
866 ((CTR++))
867 fi
868 done
869 for im in $USE_RELEASE_IMAGES; do
870 if [ "$1" == "$im" ]; then
871 suffix="REMOTE_RELEASE"
872 ((CTR++))
873 fi
874 done
875 for im in $USE_SNAPSHOT_IMAGES; do
876 if [ "$1" == "$im" ]; then
877 suffix="REMOTE_SNAPSHOT"
878 ((CTR++))
879 fi
880 done
881 for im in $USE_LOCAL_IMAGES; do
882 if [ "$1" == "$im" ]; then
883 suffix="LOCAL"
884 ((CTR++))
885 fi
886 done
887 echo $suffix
888 if [ $CTR -gt 1 ]; then
889 exit 1
890 fi
891 return 0
892}
893
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200894# Function to re-tag and image and push to another image repo
895__retag_and_push_image() {
896 if [ ! -z "$IMAGE_REPO_ADR" ]; then
897 source_image="${!1}"
898 trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var
899 target_image="${!trg_var_name}"
900 echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
901 tmp=$(docker image tag $source_image ${target_image} )
902 if [ $? -ne 0 ]; then
903 docker stop $tmp &> ./tmp/.dockererr
904 ((IMAGE_ERR++))
905 echo ""
906 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
907 cat ./tmp/.dockererr
908 return 1
909 else
910 echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
911 fi
912 echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}"
913 tmp=$(docker push ${target_image} )
914 if [ $? -ne 0 ]; then
915 docker stop $tmp &> ./tmp/.dockererr
916 ((IMAGE_ERR++))
917 echo ""
918 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}"
919 cat ./tmp/.dockererr
920 return 1
921 else
922 echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}"
923 fi
924 export "${1}"=$target_image
925 fi
926 return 0
927}
928
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100929#Function to check if image exist and stop+remove the container+pull new images as needed
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200930#args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name>
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100931__check_and_pull_image() {
932
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200933 source_image="${!4}"
934
935 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 +0100936 format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\""
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200937 tmp_im=$(docker images --format $format_string $source_image)
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100938
939 if [ $1 == "local" ]; then
940 if [ -z "$tmp_im" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200941 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 +0100942 ((IMAGE_ERR++))
943 return 1
944 else
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200945 echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100946 fi
947 elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then
948 if [ $1 == "remote-remove" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100949 if [ $RUNMODE == "DOCKER" ]; then
950 echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100951 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME})
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100952 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
953 docker stop $tmp &> ./tmp/.dockererr
954 if [ $? -ne 0 ]; then
955 ((IMAGE_ERR++))
956 echo ""
957 echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED
958 cat ./tmp/.dockererr
959 return 1
960 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100961 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100962 echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +0100963 tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100964 if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then
965 docker rm $tmp &> ./tmp/.dockererr
966 if [ $? -ne 0 ]; then
967 ((IMAGE_ERR++))
968 echo ""
969 echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED
970 cat ./tmp/.dockererr
971 return 1
972 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100973 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +0100974 echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN
975 tmp_im=""
976 else
977 tmp_im=""
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100978 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100979 fi
980 if [ -z "$tmp_im" ]; then
BjornMagnussonXA72667f12020-04-24 09:20:18 +0200981 echo -ne " Pulling image${SAMELINE}"
BjornMagnussonXA483ee332021-04-08 01:35:24 +0200982 out=$(docker pull $source_image)
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100983 if [ $? -ne 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100984 echo ""
985 echo -e " Pulling image -$RED could not be pulled"$ERED
986 ((IMAGE_ERR++))
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100987 echo $out > ./tmp/.dockererr
988 echo $out
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100989 return 1
990 fi
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +0100991 echo $out > ./tmp/.dockererr
992 if [[ $out == *"up to date"* ]]; then
993 echo -e " Pulling image -$GREEN Image is up to date $EGREEN"
994 elif [[ $out == *"Downloaded newer image"* ]]; then
995 echo -e " Pulling image -$GREEN Newer image pulled $EGREEN"
996 else
997 echo -e " Pulling image -$GREEN Pulled $EGREEN"
998 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +0100999 else
1000 echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)"
1001 fi
1002 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001003
1004 __retag_and_push_image $4
1005
1006 return $?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001007}
1008
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001009setup_testenvironment() {
1010 # Check that image env setting are available
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001011 echo ""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001012
1013 # Image var setup for all project images included in the test
1014 for imagename in $APP_SHORT_NAMES; do
1015 __check_included_image $imagename
1016 incl=$?
1017 __check_project_image $imagename
1018 proj=$?
1019 if [ $incl -eq 0 ]; then
1020 if [ $proj -eq 0 ]; then
1021 IMAGE_SUFFIX=$(__check_image_override $imagename)
1022 if [ $? -ne 0 ]; then
1023 echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED
1024 ((IMAGE_ERR++))
1025 fi
1026 else
1027 IMAGE_SUFFIX="none"
1028 fi
1029 # A function name is created from the app short name
1030 # for example app short name 'ECS' -> produce the function
1031 # name __ECS_imagesetup
1032 # This function is called and is expected to exist in the imported
1033 # file for the ecs test functions
1034 # The resulting function impl will call '__check_and_create_image_var' function
1035 # with appropriate parameters
1036 # If the image suffix is none, then the component decides the suffix
1037 function_pointer="__"$imagename"_imagesetup"
1038 $function_pointer $IMAGE_SUFFIX
1039 fi
1040 done
1041
1042 #Errors in image setting - exit
1043 if [ $IMAGE_ERR -ne 0 ]; then
1044 exit 1
BjornMagnussonXA89b64ab2020-12-16 09:21:01 +01001045 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001046
1047 #Print a tables of the image settings
1048 echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD
1049 column -t -s $'\t' $image_list_file | indent1
1050
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001051 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001052
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001053 #Set the SIM_GROUP var
1054 echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD
1055 if [ -z "$SIM_GROUP" ]; then
1056 SIM_GROUP=$AUTOTEST_HOME/../simulator-group
1057 if [ ! -d $SIM_GROUP ]; then
1058 echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed."
1059 echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED
1060 exit 1
1061 else
1062 echo " SIM_GROUP auto set to: " $SIM_GROUP
1063 fi
1064 elif [ $SIM_GROUP = *simulator_group ]; then
1065 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
1066 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001067 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001068 echo " SIM_GROUP env var already set to: " $SIM_GROUP
1069 fi
1070
1071 echo ""
1072
1073 #Temp var to check for image pull errors
1074 IMAGE_ERR=0
1075
1076 # The following sequence pull the configured images
1077
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001078
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001079 echo -e $BOLD"Pulling configured images, if needed"$EBOLD
1080 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1081 echo -e $YELLOW" Excluding all remote image check/pull when running with image repo: $IMAGE_REPO_ADR"$EYELLOW
1082 else
1083 for imagename in $APP_SHORT_NAMES; do
1084 __check_included_image $imagename
1085 incl=$?
1086 __check_project_image $imagename
1087 proj=$?
1088 if [ $incl -eq 0 ]; then
1089 if [ $proj -eq 0 ]; then
1090 START_ARG_MOD=$START_ARG
1091 __check_image_local_override $imagename
1092 if [ $? -eq 1 ]; then
1093 START_ARG_MOD="local"
1094 fi
1095 else
1096 START_ARG_MOD=$START_ARG
1097 fi
1098 __check_image_local_build $imagename
1099 #No pull of images built locally
1100 if [ $? -ne 0 ]; then
1101 # A function name is created from the app short name
1102 # for example app short name 'HTTPPROXY' -> produce the function
1103 # name __HTTPPROXY_imagesetup
1104 # This function is called and is expected to exist in the imported
1105 # file for the httpproxy test functions
1106 # The resulting function impl will call '__check_and_pull_image' function
1107 # with appropriate parameters
1108 function_pointer="__"$imagename"_imagepull"
1109 $function_pointer $START_ARG_MOD $START_ARG
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001110 fi
1111 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001112 echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001113 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001114 done
1115 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001116
1117 #Errors in image setting - exit
1118 if [ $IMAGE_ERR -ne 0 ]; then
1119 echo ""
1120 echo "#################################################################################################"
1121 echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED
1122 echo -e $RED"Or local image, overriding remote image, does not exist"$ERED
1123 if [ $IMAGE_CATEGORY == "DEV" ]; then
1124 echo -e $RED"Note that SNAPSHOT images may be purged from nexus after a certain period."$ERED
1125 echo -e $RED"In that case, switch to use a released image instead."$ERED
1126 fi
1127 echo "#################################################################################################"
1128 echo ""
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001129 exit 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001130 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001131
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001132 echo ""
YongchaoWuf309b1b2020-01-22 13:24:48 +01001133
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001134 echo -e $BOLD"Building images needed for test"$EBOLD
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02001135
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001136 for imagename in $APP_SHORT_NAMES; do
1137 cd $AUTOTEST_HOME #Always reset to orig dir
1138 __check_image_local_build $imagename
1139 if [ $? -eq 0 ]; then
1140 __check_included_image $imagename
1141 if [ $? -eq 0 ]; then
1142 # A function name is created from the app short name
1143 # for example app short name 'MR' -> produce the function
1144 # name __MR_imagebuild
1145 # This function is called and is expected to exist in the imported
1146 # file for the mr test functions
1147 # The resulting function impl shall build the imagee
1148 function_pointer="__"$imagename"_imagebuild"
1149 $function_pointer
YongchaoWuf309b1b2020-01-22 13:24:48 +01001150
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001151 else
1152 echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW
1153 fi
1154 fi
1155 done
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001156
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001157 cd $AUTOTEST_HOME # Just to make sure...
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001158
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001159 echo ""
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001160
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001161 # Create a table of the images used in the script - from local repo
1162 echo -e $BOLD"Local docker registry images used in this test script"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001163
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001164 docker_tmp_file=./tmp/.docker-images-table
1165 format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}"
1166 echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001167
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001168 for imagename in $APP_SHORT_NAMES; do
1169 __check_included_image $imagename
1170 if [ $? -eq 0 ]; then
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001171 # Only print image data if image repo is null, or if image repo is set and image is local
1172 print_image_data=0
1173 if [ -z "$IMAGE_REPO_ADR" ]; then
1174 print_image_data=1
1175 else
1176 __check_image_local_build $imagename
1177 if [ $? -eq 0 ]; then
1178 print_image_data=1
1179 fi
1180 fi
1181 if [ $print_image_data -eq 1 ]; then
1182 # A function name is created from the app short name
1183 # for example app short name 'MR' -> produce the function
1184 # name __MR_imagebuild
1185 # This function is called and is expected to exist in the imported
1186 # file for the mr test functions
1187 # The resulting function impl shall build the imagee
1188 function_pointer="__"$imagename"_image_data"
1189 $function_pointer "$format_string" $docker_tmp_file
1190 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001191 fi
1192 done
YongchaoWu9a84f512019-12-16 22:54:11 +01001193
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001194 column -t -s $'\t' $docker_tmp_file | indent1
1195
1196 echo ""
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001197
1198 if [ ! -z "$IMAGE_REPO_ADR" ]; then
1199
1200 # Create a table of the images used in the script - from remote repo
1201 echo -e $BOLD"Remote repo images used in this test script"$EBOLD
1202 echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW
1203
1204 docker_tmp_file=./tmp/.docker-images-table
1205 format_string="{{.Repository}}\\t{{.Tag}}"
1206 echo -e "Application\tRepository\tTag" > $docker_tmp_file
1207
1208 for imagename in $APP_SHORT_NAMES; do
1209 __check_included_image $imagename
1210 if [ $? -eq 0 ]; then
1211 # Only print image data if image repo is null, or if image repo is set and image is local
1212 __check_image_local_build $imagename
1213 if [ $? -ne 0 ]; then
1214 # A function name is created from the app short name
1215 # for example app short name 'MR' -> produce the function
1216 # name __MR_imagebuild
1217 # This function is called and is expected to exist in the imported
1218 # file for the mr test functions
1219 # The resulting function impl shall build the imagee
1220 function_pointer="__"$imagename"_image_data"
1221 $function_pointer "$format_string" $docker_tmp_file
1222 fi
1223 fi
1224 done
1225
1226 column -t -s $'\t' $docker_tmp_file | indent1
1227
1228 echo ""
1229 fi
1230
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001231 if [ $RUNMODE == "KUBE" ]; then
1232
1233 echo "================================================================================="
1234 echo "================================================================================="
1235
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001236 if [ -z "$IMAGE_REPO_ADR" ]; then
1237 echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW
1238 echo -e " This setting only works on single node clusters on the local machine"
1239 echo -e " It does not work with multi-node clusters or remote clusters. "
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001240 export KUBE_IMAGE_PULL_POLICY="Never"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001241 else
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001242 echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW
1243 echo -e " This setting work on local clusters, multi-node clusters and remote cluster. "
1244 echo -e " Only locally built images are managed. Remote images are always pulled from remote repos"
1245 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"
1246 export KUBE_IMAGE_PULL_POLICY="Always"
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001247 fi
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001248 CLUSTER_IP=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}')
1249 echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW
BjornMagnussonXA483ee332021-04-08 01:35:24 +02001250
1251 echo "================================================================================="
1252 echo "================================================================================="
1253 echo ""
1254 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001255
1256 echo -e $BOLD"======================================================="$EBOLD
1257 echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD
1258 echo -e $BOLD"======================================================="$EBOLD
1259 echo ""
1260
1261}
YongchaoWu9a84f512019-12-16 22:54:11 +01001262
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001263# Function to print the test result, shall be the last cmd in a test script
1264# args: -
1265# (Function for test scripts)
1266print_result() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001267
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001268 TCTEST_END=$SECONDS
1269 duration=$((TCTEST_END-TCTEST_START))
YongchaoWu9a84f512019-12-16 22:54:11 +01001270
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001271 echo "-------------------------------------------------------------------------------------------------"
1272 echo "------------------------------------- Test case: "$ATC
1273 echo "------------------------------------- Ended: "$(date)
1274 echo "-------------------------------------------------------------------------------------------------"
1275 echo "-- Description: "$TC_ONELINE_DESCR
1276 echo "-- Execution time: " $duration " seconds"
BjornMagnussonXA2791e082020-11-12 00:52:08 +01001277 echo "-- Used env file: "$TEST_ENV_VAR_FILE
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001278 echo "-------------------------------------------------------------------------------------------------"
1279 echo "------------------------------------- RESULTS"
YongchaoWu4e489b02020-02-24 09:18:16 +01001280 echo ""
YongchaoWu4e489b02020-02-24 09:18:16 +01001281
YongchaoWu21f17bb2020-03-05 12:44:08 +01001282
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001283 if [ $RES_DEVIATION -gt 0 ]; then
1284 echo "Test case deviations"
1285 echo "===================================="
1286 cat $DEVIATION_FILE
1287 fi
1288 echo ""
1289 echo "Timer measurement in the test script"
1290 echo "===================================="
1291 column -t -s $'\t' $TIMER_MEASUREMENTS
1292 echo ""
1293
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001294 total=$((RES_PASS+RES_FAIL))
1295 if [ $RES_TEST -eq 0 ]; then
1296 echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m"
1297 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1298 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1299 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1300 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1301 elif [ $total != $RES_TEST ]; then
1302 echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m"
1303 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1304 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1305 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1306 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1307 elif [ $RES_CONF_FAIL -ne 0 ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001308 echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001309 echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m"
1310 echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m"
1311 echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m"
1312 echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m"
1313 elif [ $RES_PASS = $RES_TEST ]; then
1314 echo -e "All tests \033[32m\033[1mPASS\033[0m"
1315 echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m"
1316 echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m"
1317 echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m"
1318 echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m"
1319 echo ""
YongchaoWu21f17bb2020-03-05 12:44:08 +01001320
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001321 # Update test suite counter
1322 if [ -f .tmp_tcsuite_pass_ctr ]; then
1323 tmpval=$(< .tmp_tcsuite_pass_ctr)
1324 ((tmpval++))
1325 echo $tmpval > .tmp_tcsuite_pass_ctr
1326 fi
1327 if [ -f .tmp_tcsuite_pass ]; then
1328 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass
1329 fi
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001330 #Create file with OK exit code
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001331 echo "0" > "$AUTOTEST_HOME/.result$ATC.txt"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001332 else
1333 echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m "
1334 echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m"
1335 echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m"
1336 echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m"
1337 echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m"
1338 echo ""
1339 # Update test suite counter
1340 if [ -f .tmp_tcsuite_fail_ctr ]; then
1341 tmpval=$(< .tmp_tcsuite_fail_ctr)
1342 ((tmpval++))
1343 echo $tmpval > .tmp_tcsuite_fail_ctr
1344 fi
1345 if [ -f .tmp_tcsuite_fail ]; then
1346 echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail
1347 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001348 fi
YongchaoWu21f17bb2020-03-05 12:44:08 +01001349
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001350 echo "++++ Number of tests: "$RES_TEST
1351 echo "++++ Number of passed tests: "$RES_PASS
1352 echo "++++ Number of failed tests: "$RES_FAIL
YongchaoWu4e489b02020-02-24 09:18:16 +01001353 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001354 echo "++++ Number of failed configs: "$RES_CONF_FAIL
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001355 echo ""
1356 echo "++++ Number of test case deviations: "$RES_DEVIATION
1357 echo ""
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001358 echo "------------------------------------- Test case complete ---------------------------------"
1359 echo "-------------------------------------------------------------------------------------------------"
YongchaoWu9a84f512019-12-16 22:54:11 +01001360 echo ""
1361}
1362
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001363#####################################################################
1364###### Functions for start, configuring, stoping, cleaning etc ######
1365#####################################################################
YongchaoWu21f17bb2020-03-05 12:44:08 +01001366
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001367# Start timer for time measurement
1368# args - (any args will be printed though)
1369start_timer() {
1370 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1371 TC_TIMER=$SECONDS
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001372 echo " Timer started: $(date)"
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02001373}
1374
1375# Print the value of the time (in seconds)
1376# args - <timer message to print> - timer value and message will be printed both on screen
1377# and in the timer measurement report
1378print_timer() {
1379 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1380 if [ $# -lt 1 ]; then
1381 ((RES_CONF_FAIL++))
1382 __print_err "need 1 or more args, <timer message to print>" $@
1383 exit 1
1384 fi
1385 duration=$(($SECONDS-$TC_TIMER))
1386 if [ $duration -eq 0 ]; then
1387 duration="<1 second"
1388 else
1389 duration=$duration" seconds"
1390 fi
1391 echo " Timer duration :" $duration
1392
1393 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
1394}
1395
1396# Print the value of the time (in seconds) and reset the timer
1397# args - <timer message to print> - timer value and message will be printed both on screen
1398# and in the timer measurement report
1399print_and_reset_timer() {
1400 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1401 if [ $# -lt 1 ]; then
1402 ((RES_CONF_FAIL++))
1403 __print_err "need 1 or more args, <timer message to print>" $@
1404 exit 1
1405 fi
1406 duration=$(($SECONDS-$TC_TIMER))" seconds"
1407 if [ $duration -eq 0 ]; then
1408 duration="<1 second"
1409 else
1410 duration=$duration" seconds"
1411 fi
1412 echo " Timer duration :" $duration
1413 TC_TIMER=$SECONDS
1414 echo " Timer reset"
1415
1416 echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS
1417
1418}
1419# Print info about a deviations from intended tests
1420# Each deviation counted is also printed in the testreport
1421# args <deviation message to print>
1422deviation() {
1423 echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD
1424 if [ $# -lt 1 ]; then
1425 ((RES_CONF_FAIL++))
1426 __print_err "need 1 or more args, <deviation message to print>" $@
1427 exit 1
1428 fi
1429 ((RES_DEVIATION++))
1430 echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD
1431 echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE
1432 echo ""
1433}
YongchaoWu21f17bb2020-03-05 12:44:08 +01001434
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001435# Stop at first FAIL test case and take all logs - only for debugging/trouble shooting
1436__check_stop_at_error() {
1437 if [ $STOP_AT_ERROR -eq 1 ]; then
1438 echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED
1439 store_logs "STOP_AT_ERROR"
1440 exit 1
1441 fi
1442 return 0
1443}
1444
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001445# Stop and remove all containers
1446# args: -
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001447# (Not for test scripts)
1448__clean_containers() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001449
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001450 echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001451
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001452 #Create empty file
1453 running_contr_file="./tmp/running_contr.txt"
1454 > $running_contr_file
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001455
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001456 # Get list of all containers started by the test script
1457 for imagename in $APP_SHORT_NAMES; do
1458 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 +01001459 done
1460
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001461 tab_heading1="App display name"
1462 tab_heading2="App short name"
1463 tab_heading3="Container name"
1464
1465 tab_heading1_len=${#tab_heading1}
1466 tab_heading2_len=${#tab_heading2}
1467 tab_heading3_len=${#tab_heading3}
1468 cntr=0
1469 #Calc field lengths of each item in the list of containers
1470 while read p; do
1471 if (( $cntr % 3 == 0 ));then
1472 if [ ${#p} -gt $tab_heading1_len ]; then
1473 tab_heading1_len=${#p}
1474 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001475 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001476 if (( $cntr % 3 == 1));then
1477 if [ ${#p} -gt $tab_heading2_len ]; then
1478 tab_heading2_len=${#p}
1479 fi
1480 fi
1481 if (( $cntr % 3 == 2));then
1482 if [ ${#p} -gt $tab_heading3_len ]; then
1483 tab_heading3_len=${#p}
1484 fi
1485 fi
1486 let cntr=cntr+1
1487 done <$running_contr_file
1488
1489 let tab_heading1_len=tab_heading1_len+2
1490 while (( ${#tab_heading1} < $tab_heading1_len)); do
1491 tab_heading1="$tab_heading1"" "
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001492 done
1493
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001494 let tab_heading2_len=tab_heading2_len+2
1495 while (( ${#tab_heading2} < $tab_heading2_len)); do
1496 tab_heading2="$tab_heading2"" "
1497 done
1498
1499 let tab_heading3_len=tab_heading3_len+2
1500 while (( ${#tab_heading3} < $tab_heading3_len)); do
1501 tab_heading3="$tab_heading3"" "
1502 done
1503
1504 echo " $tab_heading1$tab_heading2$tab_heading3"" Actions"
1505 cntr=0
1506 while read p; do
1507 if (( $cntr % 3 == 0 ));then
1508 row=""
1509 heading=$p
1510 heading_len=$tab_heading1_len
1511 fi
1512 if (( $cntr % 3 == 1));then
1513 heading=$p
1514 heading_len=$tab_heading2_len
1515 fi
1516 if (( $cntr % 3 == 2));then
1517 contr=$p
1518 heading=$p
1519 heading_len=$tab_heading3_len
1520 fi
1521 while (( ${#heading} < $heading_len)); do
1522 heading="$heading"" "
1523 done
1524 row=$row$heading
1525 if (( $cntr % 3 == 2));then
1526 echo -ne $row$SAMELINE
1527 echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}"
1528 docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1529 echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}"
1530 docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null
1531 echo -e " $row ${GREEN}stopped removed ${EGREEN}"
1532 fi
1533 let cntr=cntr+1
1534 done <$running_contr_file
1535
YongchaoWu9a84f512019-12-16 22:54:11 +01001536 echo ""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001537
1538 echo -e $BOLD" Removing docker network"$EBOLD
1539 TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME)
1540 if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001541 docker network rm $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001542 if [ $? -ne 0 ]; then
1543 echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED
1544 exit 1
1545 fi
1546 fi
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001547 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001548
1549 echo -e $BOLD" Removing all unused docker neworks"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001550 docker network prune --force | indent2
1551 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001552
1553 echo -e $BOLD" Removing all unused docker volumes"$EBOLD
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001554 docker volume prune --force | indent2
1555 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001556
1557 echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD
1558 docker rmi --force $(docker images -q -f dangling=true) &> /dev/null
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02001559 echo -e "$GREEN Done$EGREEN"
1560 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001561
1562 CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }')
1563 if [ $? -eq 0 ]; then
1564 if [ $CONTRS -ne 0 ]; then
1565 echo -e $RED"Containers running, may cause distubance to the test case"$ERED
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01001566 docker ps -a | indent1
1567 echo ""
BjornMagnussonXAad047782020-06-08 15:54:11 +02001568 fi
1569 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01001570}
1571
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001572###################################
1573### Functions for kube management
1574###################################
1575
1576# Scale a kube resource to a specific count
1577# args: <resource-type> <resource-name> <namespace> <target-count>
1578# (Not for test scripts)
1579__kube_scale() {
1580 echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE
1581 kubectl scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr
1582 if [ $? -ne 0 ]; then
1583 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED"
1584 ((RES_CONF_FAIL++))
1585 echo " Message: $(<./tmp/kubeerr)"
1586 return 1
1587 else
1588 echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN"
1589 fi
1590
1591 TSTART=$SECONDS
1592
1593 for i in {1..500}; do
1594 count=$(kubectl get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null)
1595 retcode=$?
1596 if [ -z "$count" ]; then
1597 #No value is sometimes returned for some reason, in case the resource has replica 0
1598 count=0
1599 fi
1600 if [ $retcode -ne 0 ]; then
1601 echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED"
1602 ((RES_CONF_FAIL++))
1603 return 1
1604 fi
1605 #echo ""
1606 if [ $count -ne $4 ]; then
1607 echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE"
1608 sleep $i
1609 else
1610 echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds"
1611 echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN"
1612 echo ""
1613 return 0
1614 fi
1615 done
1616 echo ""
1617 echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED"
1618 ((RES_CONF_FAIL++))
1619 return 0
1620}
1621
1622# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1623# This function does not wait for the resource to reach 0
1624# args: <namespace> <label-name> <label-id>
1625# (Not for test scripts)
1626__kube_scale_all_resources() {
1627 namespace=$1
1628 labelname=$2
1629 labelid=$3
1630 resources="deployment replicaset statefulset"
1631 for restype in $resources; do
1632 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1633 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1634 deleted_resourcetypes=$deleted_resourcetypes" "$restype
1635 for resid in $result; do
1636 echo -ne " Ordered caling $restype $resid from namespace $namespace with label $labelname=$labelid to 0"$SAMELINE
1637 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
1638 echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN"
1639 done
1640 fi
1641 done
1642}
1643
1644# Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id
1645# This function do wait for the resource to reach 0
1646# args: <namespace> <label-name> <label-id>
1647# (Not for test scripts)
1648__kube_scale_and_wait_all_resources() {
1649 namespace=$1
1650 labelname=$2
1651 labelid=$3
1652 resources="deployment replicaset statefulset"
1653 scaled_all=1
1654 while [ $scaled_all -ne 0 ]; do
1655 scaled_all=0
1656 for restype in $resources; do
1657 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1658 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1659 for resid in $result; do
1660 echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0"
1661 kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr
1662 count=1
1663 T_START=$SECONDS
1664 while [ $count -ne 0 ]; do
1665 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
1666 echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE
1667 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
1668 sleep 0.5
1669 else
1670 count=0
1671 fi
1672 duration=$(($SECONDS-$T_START))
1673 if [ $duration -gt 100 ]; then
1674 #Forcring count 0, to avoid hanging for failed scaling
1675 scaled_all=1
1676 count=0
1677 fi
1678 done
1679 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN"
1680 done
1681 fi
1682 done
1683 done
1684}
1685
1686# Remove all kube resources in a namespace for resources having a certain label and label-id
1687# This function wait until the resources are gone. Scaling to 0 must have been ordered previously
1688# args: <namespace> <label-name> <label-id>
1689# (Not for test scripts)
1690__kube_delete_all_resources() {
1691 namespace=$1
1692 labelname=$2
1693 labelid=$3
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001694 resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes"
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001695 deleted_resourcetypes=""
1696 for restype in $resources; do
1697 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1698 if [ $? -eq 0 ] && [ ! -z "$result" ]; then
1699 deleted_resourcetypes=$deleted_resourcetypes" "$restype
1700 for resid in $result; do
1701 if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then
1702 count=1
1703 while [ $count -ne 0 ]; do
1704 count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null)
1705 echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE
1706 if [ $? -eq 0 ] && [ ! -z "$count" ]; then
1707 sleep 0.5
1708 else
1709 count=0
1710 fi
1711 done
1712 echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN"
1713 fi
1714 echo -ne " Deleting $restype $resid from namespace $namespace with label $labelname=$labelid "$SAMELINE
1715 kubectl delete $restype $resid -n $namespace 1> /dev/null 2> ./tmp/kubeerr
1716 if [ $? -eq 0 ]; then
1717 echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN OK $EGREEN"
1718 else
1719 echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN"
1720 fi
1721 #fi
1722 done
1723 fi
1724 done
1725 if [ ! -z "$deleted_resourcetypes" ]; then
1726 for restype in $deleted_resources; do
1727 echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted..."$SAMELINE
1728 T_START=$SECONDS
1729 result="dummy"
1730 while [ ! -z "$result" ]; do
1731 sleep 0.5
1732 result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}')
1733 echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE
1734 if [ -z "$result" ]; then
1735 echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN"
1736 elif [ $(($SECONDS-$T_START)) -gt 300 ]; then
1737 echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED"
1738 result=""
1739 fi
1740 done
1741 done
1742 fi
1743}
1744
1745# Creates a namespace if it does not exists
1746# args: <namespace>
1747# (Not for test scripts)
1748__kube_create_namespace() {
1749
1750 #Check if test namespace exists, if not create it
1751 kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr
1752 if [ $? -ne 0 ]; then
1753 echo -ne " Creating namespace "$1 $SAMELINE
1754 kubectl create namespace $1 1> /dev/null 2> ./tmp/kubeerr
1755 if [ $? -ne 0 ]; then
1756 echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED"
1757 ((RES_CONF_FAIL++))
1758 echo " Message: $(<./tmp/kubeerr)"
1759 return 1
1760 else
1761 echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN"
1762 fi
1763 else
1764 echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN"
1765 fi
1766 return 0
1767}
1768
1769# Find the host ip of an app (using the service resource)
1770# args: <app-name> <namespace>
1771# (Not for test scripts)
1772__kube_get_service_host() {
1773 if [ $# -ne 2 ]; then
1774 ((RES_CONF_FAIL++))
1775 __print_err "need 2 args, <app-name> <namespace>" $@
1776 exit 1
1777 fi
1778 for timeout in {1..60}; do
1779 host=$(kubectl get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}')
1780 if [ $? -eq 0 ]; then
1781 if [ ! -z "$host" ]; then
1782 echo $host
1783 return 0
1784 fi
1785 fi
1786 sleep 0.5
1787 done
1788 ((RES_CONF_FAIL++))
1789 echo "host-not-found-fatal-error"
1790 return 1
1791}
1792
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001793# Find the named port to an app (using the service resource)
1794# args: <app-name> <namespace> <port-name>
1795# (Not for test scripts)
1796__kube_get_service_port() {
1797 if [ $# -ne 3 ]; then
1798 ((RES_CONF_FAIL++))
1799 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
1800 exit 1
1801 fi
1802
1803 for timeout in {1..60}; do
1804 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}')
1805 if [ $? -eq 0 ]; then
1806 if [ ! -z "$port" ]; then
1807 echo $port
1808 return 0
1809 fi
1810 fi
1811 sleep 0.5
1812 done
1813 ((RES_CONF_FAIL++))
1814 echo "0"
1815 return 1
1816}
1817
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001818# Find the named node port to an app (using the service resource)
1819# args: <app-name> <namespace> <port-name>
1820# (Not for test scripts)
1821__kube_get_service_nodeport() {
1822 if [ $# -ne 3 ]; then
1823 ((RES_CONF_FAIL++))
1824 __print_err "need 3 args, <app-name> <namespace> <port-name>" $@
1825 exit 1
1826 fi
1827
1828 for timeout in {1..60}; do
1829 port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}')
1830 if [ $? -eq 0 ]; then
1831 if [ ! -z "$port" ]; then
1832 echo $port
1833 return 0
1834 fi
1835 fi
1836 sleep 0.5
1837 done
1838 ((RES_CONF_FAIL++))
1839 echo "0"
1840 return 1
1841}
1842
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001843# Create a kube resource from a yaml template
1844# args: <resource-type> <resource-name> <template-yaml> <output-yaml>
1845# (Not for test scripts)
1846__kube_create_instance() {
1847 echo -ne " Creating $1 $2"$SAMELINE
1848 envsubst < $3 > $4
1849 kubectl apply -f $4 1> /dev/null 2> ./tmp/kubeerr
1850 if [ $? -ne 0 ]; then
1851 ((RES_CONF_FAIL++))
1852 echo -e " Creating $1 $2 $RED Failed $ERED"
1853 echo " Message: $(<./tmp/kubeerr)"
1854 return 1
1855 else
1856 echo -e " Creating $1 $2 $GREEN OK $EGREEN"
1857 fi
1858}
1859
1860# Function to create a configmap in kubernetes
1861# args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml>
1862# (Not for test scripts)
1863__kube_create_configmap() {
1864 echo -ne " Creating configmap $1 "$SAMELINE
1865 envsubst < $5 > $5"_tmp"
1866 cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name
1867 kubectl create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6
1868 if [ $? -ne 0 ]; then
1869 echo -e " Creating configmap $1 $RED Failed $ERED"
1870 ((RES_CONF_FAIL++))
1871 return 1
1872 fi
1873
1874 kubectl apply -f $6 1> /dev/null 2> ./tmp/kubeerr
1875 if [ $? -ne 0 ]; then
1876 echo -e " Creating configmap $1 $RED Apply failed $ERED"
1877 echo " Message: $(<./tmp/kubeerr)"
1878 ((RES_CONF_FAIL++))
1879 return 1
1880 fi
1881 kubectl label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr
1882 if [ $? -ne 0 ]; then
1883 echo -e " Creating configmap $1 $RED Labeling failed $ERED"
1884 echo " Message: $(<./tmp/kubeerr)"
1885 ((RES_CONF_FAIL++))
1886 return 1
1887 fi
1888 # Log the resulting map
1889 kubectl get configmap $1 -n $2 -o yaml > $6
1890
1891 echo -e " Creating configmap $1 $GREEN OK $EGREEN"
1892 return 0
1893}
1894
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02001895# This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter.
1896# The function retries up to the timeout given in the cmd flag '--cluster-timeout'
1897# args: <full kubectl cmd with parameters
1898# (Not for test scripts)
1899__kube_cmd_with_timeout() {
1900 TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT))
1901
1902 while true; do
1903 kube_cmd_result=$($@)
1904 if [ $? -ne 0 ]; then
1905 kube_cmd_result=""
1906 fi
1907 if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then
1908 echo $kube_cmd_result
1909 return 0
1910 fi
1911 sleep 1
1912 done
1913}
1914
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001915# This function scales or deletes all resources for app selected by the testcase.
1916# args: -
1917# (Not for test scripts)
1918__clean_kube() {
1919 echo -e $BOLD"Initialize kube services//pods/statefulsets/replicaset to initial state"$EBOLD
1920
1921 # Scale prestarted or managed apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001922 for imagename in $APP_SHORT_NAMES; do
1923 __check_included_image $imagename
1924 if [ $? -eq 0 ]; then
1925 # A function name is created from the app short name
1926 # for example app short name 'RICMSIM' -> produce the function
1927 # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait
1928 # This function is called and is expected to exist in the imported
1929 # file for the ricsim test functions
1930 # The resulting function impl shall scale the resources to 0
1931 __check_prestarted_image $imagename
1932 if [ $? -eq 0 ]; then
1933 function_pointer="__"$imagename"_kube_scale_zero_and_wait"
1934 else
1935 function_pointer="__"$imagename"_kube_scale_zero"
1936 fi
1937 echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0"
1938 $function_pointer
1939 fi
1940 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001941
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01001942 # Delete managed apps
1943 for imagename in $APP_SHORT_NAMES; do
1944 __check_included_image $imagename
1945 if [ $? -eq 0 ]; then
1946 __check_prestarted_image $imagename
1947 if [ $? -ne 0 ]; then
1948 # A function name is created from the app short name
1949 # for example app short name 'RICMSIM' -> produce the function
1950 # name __RICSIM__kube_delete_all
1951 # This function is called and is expected to exist in the imported
1952 # file for the ricsim test functions
1953 # The resulting function impl shall delete all its resources
1954 function_pointer="__"$imagename"_kube_delete_all"
1955 echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD"
1956 $function_pointer
1957 fi
1958 fi
1959 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001960
1961 echo ""
1962}
1963
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001964# Function stop and remove all containers (docker) and services/deployments etc(kube)
1965# args: -
1966# Function for test script
1967clean_environment() {
1968 if [ $RUNMODE == "KUBE" ]; then
1969 __clean_kube
1970 else
1971 __clean_containers
1972 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001973}
1974
1975# 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 +01001976# args: -
1977# (Function for test scripts)
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001978auto_clean_environment() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001979 echo
1980 if [ "$AUTO_CLEAN" == "auto" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01001981 echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD
1982 clean_environment
YongchaoWu9a84f512019-12-16 22:54:11 +01001983 fi
1984}
1985
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001986# Function to sleep a test case for a numner of seconds. Prints the optional text args as info
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02001987# args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed]
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001988# (Function for test scripts)
1989sleep_wait() {
YongchaoWu9a84f512019-12-16 22:54:11 +01001990
BjornMagnussonXA80a92002020-03-19 14:31:06 +01001991 echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD
1992 if [ $# -lt 1 ]; then
1993 ((RES_CONF_FAIL++))
1994 __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@
1995 exit 1
1996 fi
1997 #echo "---- Sleep for " $1 " seconds ---- "$2
1998 start=$SECONDS
1999 duration=$((SECONDS-start))
2000 while [ $duration -lt $1 ]; do
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002001 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002002 sleep 1
2003 duration=$((SECONDS-start))
2004 done
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002005 echo -ne " Slept for ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002006 echo ""
2007}
2008
2009# Print error info for the call in the parent script (test case). Arg: <error-message-to-print>
2010# Not to be called from the test script itself.
2011__print_err() {
2012 echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED
2013 if [ $# -gt 1 ]; then
2014 echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED
2015 fi
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002016 ((RES_CONF_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002017}
2018
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002019# Function to create the docker network for the test
2020# Not to be called from the test script itself.
2021__create_docker_network() {
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002022 tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME)
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002023 if [ $? -ne 0 ]; then
2024 echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED
2025 return 1
2026 fi
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002027 if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002028 echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD"
2029 docker network create $DOCKER_SIM_NWNAME | indent2
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002030 if [ $? -ne 0 ]; then
2031 echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED
2032 return 1
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002033 else
2034 echo -e "$GREEN Done$EGREEN"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002035 fi
2036 else
2037 echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN"
2038 fi
2039}
2040
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002041# Function to start container with docker-compose and wait until all are in state running.
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002042# If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed.
2043#args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002044# (Not for test scripts)
2045__start_container() {
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002046
2047 if [ $# -lt 5 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002048 ((RES_CONF_FAIL++))
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002049 __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 +01002050 exit 1
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002051 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002052
2053 __create_docker_network
2054
2055 curdir=$PWD
2056 cd $SIM_GROUP
2057 compose_dir=$1
2058 cd $1
2059 shift
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002060 compose_file=$1
2061 if [ -z "$compose_file" ]; then
2062 compose_file="docker-compose.yml"
2063 fi
2064 shift
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002065 compose_args=$1
2066 shift
2067 appcount=$1
2068 shift
2069
BjornMagnussonXAbb648282021-03-19 09:12:42 +01002070 os_version=$(uname -a 2> /dev/null | awk '{print tolower($0)}' | grep "microsoft")
2071 if [[ "$os_version" == *"microsoft"* ]]; then
BjornMagnussonXA004424d2021-03-17 14:22:20 +01002072 echo -e $YELLOW" Workaround for Linux on Win - delay container start, 1 sec, to make sure files mounted in the container are available on disk - WLS problem"$EYELLOW
2073 sleep 1
2074 fi
2075
2076
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002077 if [ "$compose_args" == "NODOCKERARGS" ]; then
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002078 docker-compose -f $compose_file up -d &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002079 if [ $? -ne 0 ]; then
2080 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2081 cat .dockererr
2082 echo -e $RED"Stopping script...."$ERED
2083 exit 1
2084 fi
2085 else
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002086 docker-compose -f $compose_file up -d $compose_args &> .dockererr
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002087 if [ $? -ne 0 ]; then
2088 echo -e $RED"Problem to launch container(s) with docker-compose"$ERED
2089 cat .dockererr
2090 echo -e $RED"Stopping script...."$ERED
2091 exit 1
2092 fi
2093 fi
2094
2095 cd $curdir
2096
2097 appindex=0
2098 while [ $appindex -lt $appcount ]; do
2099 appname=$1
2100 shift
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002101 app_started=0
2102 for i in {1..10}; do
2103 if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002104 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 +02002105 app_started=1
2106 break
2107 else
2108 sleep $i
2109 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002110 done
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002111 if [ $app_started -eq 0 ]; then
2112 ((RES_CONF_FAIL++))
2113 echo ""
2114 echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +01002115 echo -e $RED" Stopping script..."$ERED
2116 exit 1
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002117 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002118 let appindex=appindex+1
2119 done
2120 return 0
2121}
2122
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002123# Function to check if container/service is responding to http/https
2124# args: <container-name>|<service-name> url
2125# (Not for test scripts)
2126__check_service_start() {
2127
2128 if [ $# -ne 2 ]; then
2129 ((RES_CONF_FAIL++))
2130 __print_err "need 2 args, <container-name>|<service-name> url" $@
2131 return 1
YongchaoWu9a84f512019-12-16 22:54:11 +01002132 fi
2133
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002134 if [ $RUNMODE == "KUBE" ]; then
2135 ENTITY="service/set/deployment"
2136 else
2137 ENTITY="container"
2138 fi
2139 appname=$1
2140 url=$2
2141 echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}"
2142
2143
YongchaoWu9a84f512019-12-16 22:54:11 +01002144 pa_st=false
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002145 echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}"
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002146 TSTART=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002147 loop_ctr=0
2148 while (( $TSTART+600 > $SECONDS )); do
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002149 result="$(__do_curl -m 10 $url)"
YongchaoWu9a84f512019-12-16 22:54:11 +01002150 if [ $? -eq 0 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002151 if [ ${#result} -gt 15 ]; then
2152 #If response is too long, truncate
2153 result="...response text too long, omitted"
2154 fi
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002155 echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}"
2156 echo -ne " The ${ENTITY} $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN on ${url} after $(($SECONDS-$TSTART)) seconds"
YongchaoWu9a84f512019-12-16 22:54:11 +01002157 pa_st=true
2158 break
2159 else
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002160 TS_TMP=$SECONDS
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002161 TS_OFFSET=$loop_ctr
2162 if (( $TS_OFFSET > 5 )); then
2163 TS_OFFSET=5
2164 fi
2165 while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do
2166 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 +02002167 sleep 1
2168 done
YongchaoWu9a84f512019-12-16 22:54:11 +01002169 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002170 let loop_ctr=loop_ctr+1
YongchaoWu9a84f512019-12-16 22:54:11 +01002171 done
2172
2173 if [ "$pa_st" = "false" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002174 ((RES_CONF_FAIL++))
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002175 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 +01002176 return 1
2177 fi
2178
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002179 echo ""
BjornMagnussonXA70e878f2020-05-11 14:11:30 +02002180 return 0
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002181}
2182
BjornMagnussonXAbf3700b2020-10-05 08:39:40 +02002183
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002184#################
2185### Log functions
2186#################
2187
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002188__check_container_logs() {
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002189
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002190 dispname=$1
2191 appname=$2
2192 logpath=$3
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002193 warning=$4
2194 error=$5
2195
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002196 echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD
2197
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002198 if [ $RUNMODE == "KUBE" ]; then
2199 echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW
2200 return
2201 fi
2202
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002203 #tmp=$(docker ps | grep $appname)
2204 tmp=$(docker ps -q --filter name=$appname) #get the container id
2205 if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002206 echo " "$dispname" is not running, no check made"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002207 return
2208 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002209 foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002210 if [ $? -ne 0 ];then
2211 echo " Problem to search $appname log $logpath"
2212 else
2213 if [ $foundentries -eq 0 ]; then
2214 echo " No WARN entries found in $appname log $logpath"
2215 else
2216 echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath"
2217 fi
2218 fi
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002219 foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002220 if [ $? -ne 0 ];then
2221 echo " Problem to search $appname log $logpath"
2222 else
2223 if [ $foundentries -eq 0 ]; then
2224 echo " No ERR entries found in $appname log $logpath"
2225 else
2226 echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED
2227 fi
2228 fi
2229 echo ""
2230}
2231
2232# Store all container logs and other logs in the log dir for the script
2233# Logs are stored with a prefix in case logs should be stored several times during a test
2234# args: <logfile-prefix>
2235# (Function for test scripts)
YongchaoWu9a84f512019-12-16 22:54:11 +01002236store_logs() {
2237 if [ $# != 1 ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002238 ((RES_CONF_FAIL++))
2239 __print_err "need one arg, <file-prefix>" $@
YongchaoWu9a84f512019-12-16 22:54:11 +01002240 exit 1
2241 fi
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002242 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 +01002243
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02002244 docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002245
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002246 docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002247
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002248 cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1
2249
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002250 if [ $RUNMODE == "DOCKER" ]; then
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002251
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002252 # Store docker logs for all container
2253 for imagename in $APP_SHORT_NAMES; do
2254 __check_included_image $imagename
2255 if [ $? -eq 0 ]; then
2256 # A function name is created from the app short name
2257 # for example app short name 'RICMSIM' -> produce the function
2258 # name __RICSIM__store_docker_logs
2259 # This function is called and is expected to exist in the imported
2260 # file for the ricsim test functions
2261 # The resulting function impl shall store the docker logs for each container
2262 function_pointer="__"$imagename"_store_docker_logs"
2263 $function_pointer "$TESTLOGS/$ATC/" $1
2264 fi
2265 done
BjornMagnussonXAe0b665e2021-01-08 22:19:18 +01002266 fi
2267 if [ $RUNMODE == "KUBE" ]; then
2268 namespaces=$(kubectl get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}')
2269 for nsid in $namespaces; do
2270 pods=$(kubectl get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}')
2271 for podid in $pods; do
2272 kubectl logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log
2273 done
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002274 done
2275 fi
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002276 echo ""
YongchaoWu9a84f512019-12-16 22:54:11 +01002277}
2278
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002279###############
2280## Generic curl
2281###############
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002282# Generic curl function, assumes all 200-codes are ok
2283# args: <valid-curl-args-including full url>
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002284# returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>""
2285# returns: The return code is 0 for ok and 1 for not ok
YongchaoWu9a84f512019-12-16 22:54:11 +01002286__do_curl() {
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002287 echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002288 proxyflag=""
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002289 if [ $RUNMODE == "KUBE" ]; then
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002290 if [ ! -z "$KUBE_PROXY_PATH" ]; then
2291 proxyflag=" --proxy $KUBE_PROXY_PATH"
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +01002292 fi
2293 fi
BjornMagnussonXA483ee332021-04-08 01:35:24 +02002294 curlString="curl -skw %{http_code} $proxyflag $@"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002295 echo " CMD: $curlString" >> $HTTPLOG
2296 res=$($curlString)
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002297 retcode=$?
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002298 echo " RESP: $res" >> $HTTPLOG
BjornMagnussonXAa69cd902021-04-22 23:46:10 +02002299 echo " RETCODE: $retcode" >> $HTTPLOG
2300 if [ $retcode -ne 0 ]; then
2301 echo "<no-response-from-server>"
2302 return 1
2303 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002304 http_code="${res:${#res}-3}"
2305 if [ ${#res} -eq 3 ]; then
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002306 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
2307 echo "<no-response-from-server>"
2308 return 1
2309 else
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002310 return 0
2311 fi
YongchaoWu9a84f512019-12-16 22:54:11 +01002312 else
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002313 if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then
YongchaoWu9a84f512019-12-16 22:54:11 +01002314 echo "<not found, resp:${http_code}>"
2315 return 1
2316 fi
2317 if [ $# -eq 2 ]; then
2318 echo "${res:0:${#res}-3}" | xargs
2319 else
2320 echo "${res:0:${#res}-3}"
2321 fi
2322
2323 return 0
2324 fi
2325}
2326
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002327#######################################
2328### Basic helper function for test cases
2329#######################################
2330
2331# Test a simulator container variable value towards target value using an condition operator with an optional timeout.
2332# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done
2333# immediately and sets pass or fail depending on the result of comparing variable and target using the operator.
2334# Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout
2335# before setting pass or fail depending on the result of comparing variable and target using the operator.
2336# 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.
2337# Not to be called from test script.
2338
2339__var_test() {
2340 checkjsonarraycount=0
2341
2342 if [ $# -eq 6 ]; then
2343 if [[ $3 == "json:"* ]]; then
2344 checkjsonarraycount=1
2345 fi
2346
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002347 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002348 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002349 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002350 start=$SECONDS
2351 ctr=0
2352 for (( ; ; )); do
2353 if [ $checkjsonarraycount -eq 0 ]; then
2354 result="$(__do_curl $2$3)"
2355 retcode=$?
2356 result=${result//[[:blank:]]/} #Strip blanks
2357 else
2358 path=${3:5}
2359 result="$(__do_curl $2$path)"
2360 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002361 echo "$result" > ./tmp/.tmp.curl.json
2362 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002363 fi
2364 duration=$((SECONDS-start))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002365 echo -ne " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002366 let ctr=ctr+1
2367 if [ $retcode -ne 0 ]; then
2368 if [ $duration -gt $6 ]; then
2369 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002370 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002371 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002372 return
2373 fi
2374 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2375 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002376 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002377 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002378 return
2379 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2380 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002381 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002382 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002383 return
2384 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2385 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002386 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002387 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002388 return
2389 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2390 ((RES_PASS++))
BjornMagnussonXA72667f12020-04-24 09:20:18 +02002391 echo -e " Result=${result} after ${duration} seconds${SAMELINE}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002392 echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002393 return
2394 else
2395 if [ $duration -gt $6 ]; then
2396 ((RES_FAIL++))
2397 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002398 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002399 return
2400 fi
2401 fi
2402 sleep 1
2403 done
2404 elif [ $# -eq 5 ]; then
2405 if [[ $3 == "json:"* ]]; then
2406 checkjsonarraycount=1
2407 fi
2408
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002409 echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002410 ((RES_TEST++))
BjornMagnussonXA7b36db62020-11-23 10:57:57 +01002411 ((TEST_SEQUENCE_NR++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002412 if [ $checkjsonarraycount -eq 0 ]; then
2413 result="$(__do_curl $2$3)"
2414 retcode=$?
2415 result=${result//[[:blank:]]/} #Strip blanks
2416 else
2417 path=${3:5}
2418 result="$(__do_curl $2$path)"
2419 retcode=$?
BjornMagnussonXAf38e1e82020-10-11 23:05:02 +02002420 echo "$result" > ./tmp/.tmp.curl.json
2421 result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json")
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002422 fi
2423 if [ $retcode -ne 0 ]; then
2424 ((RES_FAIL++))
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002425 echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002426 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002427 elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then
2428 ((RES_PASS++))
2429 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002430 elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then
2431 ((RES_PASS++))
2432 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002433 elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then
2434 ((RES_PASS++))
2435 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002436 elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then
2437 ((RES_PASS++))
2438 echo -e $GREEN" PASS${EGREEN} - Result=${result}"
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002439 else
2440 ((RES_FAIL++))
2441 echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}"
BjornMagnussonXA048aaa12020-06-04 07:48:37 +02002442 __check_stop_at_error
BjornMagnussonXA80a92002020-03-19 14:31:06 +01002443 fi
2444 else
2445 echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]"
2446 echo "Got:" $@
2447 exit 1
2448 fi
2449}