BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1 | #!/bin/bash |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 3 | # ============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 | # |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 19 | |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 20 | # 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 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 23 | . ../common/api_curl.sh |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 24 | . ../common/testengine_config.sh |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 25 | |
| 26 | __print_args() { |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 27 | echo "Args: remote|remote-remove docker|kube --env-file <environment-filename> [release] [auto-clean] [--stop-at-error] " |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 28 | echo " [--ricsim-prefix <prefix> ] [--use-local-image <app-nam>+] [--use-snapshot-image <app-nam>+]" |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 29 | echo " [--use-staging-image <app-nam>+] [--use-release-image <app-nam>+] [--image-repo <repo-address]" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 30 | echo " [--repo-policy local|remote] [--cluster-timeout <timeout-in seconds>] [--print-stats]" |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | if [ $# -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" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 44 | echo "docker - Test executed in docker environment" |
| 45 | echo "kube - Test executed in kubernetes environment - requires an already started kubernetes environment" |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 46 | 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" |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 55 | echo "--image-repo - Url to optional image repo. Only locally built images will be re-tagged and pushed to this repo" |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 56 | echo "--repo-policy - Policy controlling which images to re-tag and push if param --image-repo is set. Default is 'local'" |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 57 | echo "--cluster-timeout - Optional timeout for cluster where it takes time to obtain external ip/host-name. Timeout in seconds. " |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 58 | echo "--print-stats - Print current test stats after each test." |
| 59 | |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 60 | echo "" |
| 61 | echo "List of app short names supported: "$APP_SHORT_NAMES |
| 62 | exit 0 |
| 63 | fi |
| 64 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 65 | AUTOTEST_HOME=$PWD |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 66 | # Create a test case id, ATC (Auto Test Case), from the name of the test case script. |
| 67 | # FTC1.sh -> ATC == FTC1 |
| 68 | ATC=$(basename "${BASH_SOURCE[$i+1]}" .sh) |
| 69 | |
| 70 | #Create result file (containing '1' for error) for this test case |
| 71 | #Will be replaced with a file containing '0' if all test cases pass |
| 72 | echo "1" > "$PWD/.result$ATC.txt" |
| 73 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 74 | #Formatting for 'echo' cmd |
| 75 | BOLD="\033[1m" |
| 76 | EBOLD="\033[0m" |
| 77 | RED="\033[31m\033[1m" |
| 78 | ERED="\033[0m" |
| 79 | GREEN="\033[32m\033[1m" |
| 80 | EGREEN="\033[0m" |
| 81 | YELLOW="\033[33m\033[1m" |
| 82 | EYELLOW="\033[0m" |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 83 | SAMELINE="\033[0K\r" |
| 84 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 85 | # Just resetting any previous echo formatting... |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 86 | echo -ne $EBOLD |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 87 | |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 88 | # default test environment variables |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 89 | TEST_ENV_VAR_FILE="" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 90 | |
| 91 | echo "Test case started as: ${BASH_SOURCE[$i+1]} "$@ |
| 92 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 93 | #Localhost constants |
| 94 | LOCALHOST_NAME="localhost" |
| 95 | LOCALHOST_HTTP="http://localhost" |
| 96 | LOCALHOST_HTTPS="https://localhost" |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 97 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 98 | # Var to hold 'auto' in case containers shall be stopped when test case ends |
| 99 | AUTO_CLEAN="" |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 100 | |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 101 | # Var to hold the app names to use local images for |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 102 | USE_LOCAL_IMAGES="" |
| 103 | |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 104 | # Var to hold the app names to use remote snapshot images for |
| 105 | USE_SNAPSHOT_IMAGES="" |
| 106 | |
| 107 | # Var to hold the app names to use remote staging images for |
| 108 | USE_STAGING_IMAGES="" |
| 109 | |
| 110 | # Var to hold the app names to use remote release images for |
| 111 | USE_RELEASE_IMAGES="" |
| 112 | |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 113 | |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 114 | # 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 |
| 115 | STOP_AT_ERROR=0 |
| 116 | |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 117 | # The default value "DEV" indicate that development image tags (SNAPSHOT) and nexus repos (nexus port 10002) are used. |
| 118 | # The value "RELEASE" indicate that relase image tag and nexus repos (nexus port) are used |
| 119 | # Applies only to images defined in the test-env files with image names and tags defined as XXXX_RELEASE |
| 120 | IMAGE_CATEGORY="DEV" |
| 121 | |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 122 | # Function to indent cmd output with one space |
| 123 | indent1() { sed 's/^/ /'; } |
| 124 | |
| 125 | # Function to indent cmd output with two spaces |
| 126 | indent2() { sed 's/^/ /'; } |
| 127 | |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 128 | # Set a description string for the test case |
| 129 | if [ -z "$TC_ONELINE_DESCR" ]; then |
| 130 | TC_ONELINE_DESCR="<no-description>" |
| 131 | echo "No test case description found, TC_ONELINE_DESCR should be set on in the test script , using "$TC_ONELINE_DESCR |
| 132 | fi |
| 133 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 134 | # Counter for test suites |
| 135 | if [ -f .tmp_tcsuite_ctr ]; then |
| 136 | tmpval=$(< .tmp_tcsuite_ctr) |
| 137 | ((tmpval++)) |
| 138 | echo $tmpval > .tmp_tcsuite_ctr |
| 139 | fi |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 140 | |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 141 | # Create the logs dir if not already created in the current dir |
| 142 | if [ ! -d "logs" ]; then |
| 143 | mkdir logs |
| 144 | fi |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 145 | TESTLOGS=$PWD/logs |
| 146 | |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 147 | # Create the tmp dir for temporary files that is not needed after the test |
| 148 | # hidden files for the test env is still stored in the current dir |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 149 | # files in the ./tmp is moved to ./tmp/prev when a new test is started |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 150 | if [ ! -d "tmp" ]; then |
| 151 | mkdir tmp |
| 152 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 153 | curdir=$PWD |
| 154 | cd tmp |
| 155 | if [ $? -ne 0 ]; then |
| 156 | echo "Cannot cd to $PWD/tmp" |
| 157 | echo "Dir cannot be created. Exiting...." |
| 158 | fi |
| 159 | if [ ! -d "prev" ]; then |
| 160 | mkdir prev |
| 161 | fi |
| 162 | cd $curdir |
| 163 | mv ./tmp/* ./tmp/prev 2> /dev/null |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 164 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 165 | # Create a http message log for this testcase |
| 166 | HTTPLOG=$PWD"/.httplog_"$ATC".txt" |
| 167 | echo "" > $HTTPLOG |
| 168 | |
| 169 | # Create a log dir for the test case |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 170 | mkdir -p $TESTLOGS/$ATC |
| 171 | |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 172 | # Save create for current logs |
| 173 | mkdir -p $TESTLOGS/$ATC/previous |
| 174 | |
| 175 | rm $TESTLOGS/$ATC/previous/*.log &> /dev/null |
| 176 | rm $TESTLOGS/$ATC/previous/*.txt &> /dev/null |
| 177 | rm $TESTLOGS/$ATC/previous/*.json &> /dev/null |
| 178 | |
| 179 | mv $TESTLOGS/$ATC/*.log $TESTLOGS/$ATC/previous &> /dev/null |
| 180 | mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null |
| 181 | mv $TESTLOGS/$ATC/*.txt $TESTLOGS/$ATC/previous &> /dev/null |
| 182 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 183 | # Clear the log dir for the test case |
| 184 | rm $TESTLOGS/$ATC/*.log &> /dev/null |
| 185 | rm $TESTLOGS/$ATC/*.txt &> /dev/null |
| 186 | rm $TESTLOGS/$ATC/*.json &> /dev/null |
| 187 | |
| 188 | # Log all output from the test case to a TC log |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 189 | TCLOG=$TESTLOGS/$ATC/TC.log |
| 190 | exec &> >(tee ${TCLOG}) |
| 191 | |
| 192 | #Variables for counting tests as well as passed and failed tests |
| 193 | RES_TEST=0 |
| 194 | RES_PASS=0 |
| 195 | RES_FAIL=0 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 196 | RES_CONF_FAIL=0 |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 197 | RES_DEVIATION=0 |
| 198 | |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 199 | #Var to control if current stats shall be printed |
| 200 | PRINT_CURRENT_STATS=0 |
| 201 | |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 202 | #File to keep deviation messages |
| 203 | DEVIATION_FILE=".tmp_deviations" |
| 204 | rm $DEVIATION_FILE &> /dev/null |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 205 | |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 206 | # Trap "command not found" and make the script fail |
| 207 | trap_fnc() { |
| 208 | |
| 209 | if [ $? -eq 127 ]; then |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 210 | echo -e $RED"Function not found, setting script to FAIL"$ERED |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 211 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 212 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 213 | fi |
| 214 | } |
| 215 | trap trap_fnc ERR |
| 216 | |
| 217 | # Counter for tests |
| 218 | TEST_SEQUENCE_NR=1 |
| 219 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 220 | # Function to log the start of a test case |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 221 | __log_test_start() { |
| 222 | TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") |
| 223 | echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ $EBOLD |
| 224 | echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${FUNCNAME[1]}" $@ >> $HTTPLOG |
| 225 | ((RES_TEST++)) |
| 226 | ((TEST_SEQUENCE_NR++)) |
| 227 | } |
| 228 | |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 229 | # Function to print current statistics |
| 230 | __print_current_stats() { |
| 231 | if [ $PRINT_CURRENT_STATS -ne 0 ]; then |
BjornMagnussonXA | 3cc0b58 | 2021-08-30 10:46:41 +0200 | [diff] [blame] | 232 | echo " Current stats - exe-time, tests, passes, fails, conf fails, deviations: $(($SECONDS-$TCTEST_START)), $RES_TEST, $RES_PASS, $RES_FAIL, $RES_CONF_FAIL, $RES_DEVIATION" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 233 | fi |
| 234 | } |
| 235 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 236 | # General function to log a failed test case |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 237 | __log_test_fail_general() { |
| 238 | echo -e $RED" FAIL."$1 $ERED |
| 239 | ((RES_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 240 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 241 | __check_stop_at_error |
| 242 | } |
| 243 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 244 | # Function to log a test case failed due to incorrect response code |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 245 | __log_test_fail_status_code() { |
| 246 | echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED |
| 247 | ((RES_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 248 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 249 | __check_stop_at_error |
| 250 | } |
| 251 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 252 | # Function to log a test case failed due to incorrect response body |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 253 | __log_test_fail_body() { |
| 254 | echo -e $RED" FAIL, returned body not correct"$ERED |
| 255 | ((RES_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 256 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 257 | __check_stop_at_error |
| 258 | } |
| 259 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 260 | # Function to log a test case that is not supported |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 261 | __log_test_fail_not_supported() { |
| 262 | echo -e $RED" FAIL, function not supported"$ERED |
| 263 | ((RES_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 264 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 265 | __check_stop_at_error |
| 266 | } |
| 267 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 268 | # General function to log a passed test case |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 269 | __log_test_pass() { |
| 270 | if [ $# -gt 0 ]; then |
| 271 | echo $@ |
| 272 | fi |
| 273 | ((RES_PASS++)) |
| 274 | echo -e $GREEN" PASS"$EGREEN |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 275 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | #Counter for configurations |
| 279 | CONF_SEQUENCE_NR=1 |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 280 | |
| 281 | # Function to log the start of a configuration setup |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 282 | __log_conf_start() { |
| 283 | TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") |
| 284 | echo -e $BOLD"CONF $CONF_SEQUENCE_NR (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ $EBOLD |
| 285 | echo "CONF $CONF_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): "${FUNCNAME[1]} $@ >> $HTTPLOG |
| 286 | ((CONF_SEQUENCE_NR++)) |
| 287 | } |
| 288 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 289 | # Function to log a failed configuration setup |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 290 | __log_conf_fail_general() { |
| 291 | echo -e $RED" FAIL."$1 $ERED |
| 292 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 293 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 294 | __check_stop_at_error |
| 295 | } |
| 296 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 297 | # Function to log a failed configuration setup due to incorrect response code |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 298 | __log_conf_fail_status_code() { |
| 299 | echo -e $RED" FAIL. Exepected status "$1", got "$2 $3 $ERED |
| 300 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 301 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 302 | __check_stop_at_error |
| 303 | } |
| 304 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 305 | # Function to log a failed configuration setup due to incorrect response body |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 306 | __log_conf_fail_body() { |
| 307 | echo -e $RED" FAIL, returned body not correct"$ERED |
| 308 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 309 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 310 | __check_stop_at_error |
| 311 | } |
| 312 | |
BjornMagnussonXA | 366e36a | 2021-01-27 11:48:56 +0100 | [diff] [blame] | 313 | # Function to log a passed configuration setup |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 314 | __log_conf_ok() { |
| 315 | if [ $# -gt 0 ]; then |
| 316 | echo $@ |
| 317 | fi |
| 318 | echo -e $GREEN" OK"$EGREEN |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 319 | __print_current_stats |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 320 | } |
| 321 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 322 | #Var for measuring execution time |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 323 | TCTEST_START=$SECONDS |
| 324 | |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 325 | #File to save timer measurement results |
| 326 | TIMER_MEASUREMENTS=".timer_measurement.txt" |
| 327 | echo -e "Activity \t Duration" > $TIMER_MEASUREMENTS |
| 328 | |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 329 | # If this is set, some images (control by the parameter repo-polcy) will be re-tagged and pushed to this repo before any |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 330 | IMAGE_REPO_ADR="" |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 331 | IMAGE_REPO_POLICY="local" |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 332 | CLUSTER_TIME_OUT=0 |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 333 | |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 334 | echo "-------------------------------------------------------------------------------------------------" |
| 335 | echo "----------------------------------- Test case: "$ATC |
| 336 | echo "----------------------------------- Started: "$(date) |
| 337 | echo "-------------------------------------------------------------------------------------------------" |
| 338 | echo "-- Description: "$TC_ONELINE_DESCR |
| 339 | echo "-------------------------------------------------------------------------------------------------" |
| 340 | echo "----------------------------------- Test case setup -----------------------------------" |
| 341 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 342 | echo "Setting AUTOTEST_HOME="$AUTOTEST_HOME |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 343 | START_ARG=$1 |
| 344 | paramerror=0 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 345 | paramerror_str="" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 346 | if [ $# -lt 1 ]; then |
| 347 | paramerror=1 |
| 348 | fi |
| 349 | if [ $paramerror -eq 0 ]; then |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 350 | if [ "$1" != "remote" ] && [ "$1" != "remote-remove" ]; then |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 351 | paramerror=1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 352 | if [ -z "$paramerror_str" ]; then |
| 353 | paramerror_str="First arg shall be 'remote' or 'remote-remove'" |
| 354 | fi |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 355 | else |
| 356 | shift; |
| 357 | fi |
| 358 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 359 | if [ $paramerror -eq 0 ]; then |
| 360 | if [ "$1" != "docker" ] && [ "$1" != "kube" ]; then |
| 361 | paramerror=1 |
| 362 | if [ -z "$paramerror_str" ]; then |
| 363 | paramerror_str="Second arg shall be 'docker' or 'kube'" |
| 364 | fi |
| 365 | else |
| 366 | if [ $1 == "docker" ]; then |
| 367 | RUNMODE="DOCKER" |
| 368 | echo "Setting RUNMODE=DOCKER" |
| 369 | fi |
| 370 | if [ $1 == "kube" ]; then |
| 371 | RUNMODE="KUBE" |
| 372 | echo "Setting RUNMODE=KUBE" |
| 373 | fi |
| 374 | shift; |
| 375 | fi |
| 376 | fi |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 377 | foundparm=0 |
| 378 | while [ $paramerror -eq 0 ] && [ $foundparm -eq 0 ]; do |
| 379 | foundparm=1 |
| 380 | if [ $paramerror -eq 0 ]; then |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 381 | if [ "$1" == "release" ]; then |
| 382 | IMAGE_CATEGORY="RELEASE" |
| 383 | echo "Option set - Release image tags used for applicable images " |
| 384 | shift; |
| 385 | foundparm=0 |
| 386 | fi |
| 387 | fi |
| 388 | if [ $paramerror -eq 0 ]; then |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 389 | if [ "$1" == "auto-clean" ]; then |
| 390 | AUTO_CLEAN="auto" |
| 391 | echo "Option set - Auto clean at end of test script" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 392 | shift; |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 393 | foundparm=0 |
| 394 | fi |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 395 | fi |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 396 | if [ $paramerror -eq 0 ]; then |
| 397 | if [ "$1" == "--stop-at-error" ]; then |
| 398 | STOP_AT_ERROR=1 |
| 399 | echo "Option set - Stop at first error" |
| 400 | shift; |
| 401 | foundparm=0 |
| 402 | fi |
| 403 | fi |
| 404 | if [ $paramerror -eq 0 ]; then |
| 405 | if [ "$1" == "--ricsim-prefix" ]; then |
| 406 | shift; |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 407 | TMP_RIC_SIM_PREFIX=$1 #RIC_SIM_PREFIX need to be updated after sourcing of the env file |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 408 | if [ -z "$1" ]; then |
| 409 | paramerror=1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 410 | if [ -z "$paramerror_str" ]; then |
| 411 | paramerror_str="No prefix found for flag: '--ricsim-prefix'" |
| 412 | fi |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 413 | else |
| 414 | echo "Option set - Overriding RIC_SIM_PREFIX with: "$1 |
| 415 | shift; |
| 416 | foundparm=0 |
| 417 | fi |
| 418 | fi |
| 419 | fi |
| 420 | if [ $paramerror -eq 0 ]; then |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 421 | if [ "$1" == "--env-file" ]; then |
| 422 | shift; |
| 423 | TEST_ENV_VAR_FILE=$1 |
| 424 | if [ -z "$1" ]; then |
| 425 | paramerror=1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 426 | if [ -z "$paramerror_str" ]; then |
| 427 | paramerror_str="No env file found for flag: '--env-file'" |
| 428 | fi |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 429 | else |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 430 | echo "Option set - Reading test env from: "$1 |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 431 | shift; |
| 432 | foundparm=0 |
| 433 | fi |
| 434 | fi |
| 435 | fi |
| 436 | if [ $paramerror -eq 0 ]; then |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 437 | if [ "$1" == "--use-local-image" ]; then |
| 438 | USE_LOCAL_IMAGES="" |
| 439 | shift |
| 440 | while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do |
| 441 | USE_LOCAL_IMAGES=$USE_LOCAL_IMAGES" "$1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 442 | if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 443 | paramerror=1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 444 | if [ -z "$paramerror_str" ]; then |
| 445 | paramerror_str="App name $1 is not available for local override for flag: '--use-local-image'" |
| 446 | fi |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 447 | fi |
| 448 | shift; |
| 449 | done |
| 450 | foundparm=0 |
| 451 | if [ -z "$USE_LOCAL_IMAGES" ]; then |
| 452 | paramerror=1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 453 | if [ -z "$paramerror_str" ]; then |
| 454 | paramerror_str="No app name found for flag: '--use-local-image'" |
| 455 | fi |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 456 | else |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 457 | echo "Option set - Overriding with local images for app(s):"$USE_LOCAL_IMAGES |
| 458 | fi |
| 459 | fi |
| 460 | fi |
| 461 | if [ $paramerror -eq 0 ]; then |
| 462 | if [ "$1" == "--use-snapshot-image" ]; then |
| 463 | USE_SNAPSHOT_IMAGES="" |
| 464 | shift |
| 465 | while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do |
| 466 | USE_SNAPSHOT_IMAGES=$USE_SNAPSHOT_IMAGES" "$1 |
| 467 | if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then |
| 468 | paramerror=1 |
| 469 | if [ -z "$paramerror_str" ]; then |
| 470 | paramerror_str="App name $1 is not available for snapshot override for flag: '--use-snapshot-image'" |
| 471 | fi |
| 472 | fi |
| 473 | shift; |
| 474 | done |
| 475 | foundparm=0 |
| 476 | if [ -z "$USE_SNAPSHOT_IMAGES" ]; then |
| 477 | paramerror=1 |
| 478 | if [ -z "$paramerror_str" ]; then |
| 479 | paramerror_str="No app name found for flag: '--use-snapshot-image'" |
| 480 | fi |
| 481 | else |
| 482 | echo "Option set - Overriding with snapshot images for app(s):"$USE_SNAPSHOT_IMAGES |
| 483 | fi |
| 484 | fi |
| 485 | fi |
| 486 | if [ $paramerror -eq 0 ]; then |
| 487 | if [ "$1" == "--use-staging-image" ]; then |
| 488 | USE_STAGING_IMAGES="" |
| 489 | shift |
| 490 | while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do |
| 491 | USE_STAGING_IMAGES=$USE_STAGING_IMAGES" "$1 |
| 492 | if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then |
| 493 | paramerror=1 |
| 494 | if [ -z "$paramerror_str" ]; then |
| 495 | paramerror_str="App name $1 is not available for staging override for flag: '--use-staging-image'" |
| 496 | fi |
| 497 | fi |
| 498 | shift; |
| 499 | done |
| 500 | foundparm=0 |
| 501 | if [ -z "$USE_STAGING_IMAGES" ]; then |
| 502 | paramerror=1 |
| 503 | if [ -z "$paramerror_str" ]; then |
| 504 | paramerror_str="No app name found for flag: '--use-staging-image'" |
| 505 | fi |
| 506 | else |
| 507 | echo "Option set - Overriding with staging images for app(s):"$USE_STAGING_IMAGES |
| 508 | fi |
| 509 | fi |
| 510 | fi |
| 511 | if [ $paramerror -eq 0 ]; then |
| 512 | if [ "$1" == "--use-release-image" ]; then |
| 513 | USE_RELEASE_IMAGES="" |
| 514 | shift |
| 515 | while [ $# -gt 0 ] && [[ "$1" != "--"* ]]; do |
| 516 | USE_RELEASE_IMAGES=$USE_RELEASE_IMAGES" "$1 |
| 517 | if [[ "$AVAILABLE_IMAGES_OVERRIDE" != *"$1"* ]]; then |
| 518 | paramerror=1 |
| 519 | if [ -z "$paramerror_str" ]; then |
| 520 | paramerror_str="App name $1 is not available for release override for flag: '--use-release-image'" |
| 521 | fi |
| 522 | fi |
| 523 | shift; |
| 524 | done |
| 525 | foundparm=0 |
| 526 | if [ -z "$USE_RELEASE_IMAGES" ]; then |
| 527 | paramerror=1 |
| 528 | if [ -z "$paramerror_str" ]; then |
| 529 | paramerror_str="No app name found for flag: '--use-release-image'" |
| 530 | fi |
| 531 | else |
| 532 | echo "Option set - Overriding with release images for app(s):"$USE_RELEASE_IMAGES |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 533 | fi |
| 534 | fi |
| 535 | fi |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 536 | if [ $paramerror -eq 0 ]; then |
| 537 | if [ "$1" == "--image-repo" ]; then |
| 538 | shift; |
| 539 | IMAGE_REPO_ADR=$1 |
| 540 | if [ -z "$1" ]; then |
| 541 | paramerror=1 |
| 542 | if [ -z "$paramerror_str" ]; then |
| 543 | paramerror_str="No image repo url found for : '--image-repo'" |
| 544 | fi |
| 545 | else |
| 546 | echo "Option set - Image repo url: "$1 |
| 547 | shift; |
| 548 | foundparm=0 |
| 549 | fi |
| 550 | fi |
| 551 | fi |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 552 | if [ $paramerror -eq 0 ]; then |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 553 | if [ "$1" == "--repo-policy" ]; then |
| 554 | shift; |
| 555 | IMAGE_REPO_POLICY=$1 |
| 556 | if [ -z "$1" ]; then |
| 557 | paramerror=1 |
| 558 | if [ -z "$paramerror_str" ]; then |
| 559 | paramerror_str="No policy found for : '--repo-policy'" |
| 560 | fi |
| 561 | else |
| 562 | if [ "$1" == "local" ] || [ "$1" == "remote" ]; then |
| 563 | echo "Option set - Image repo policy: "$1 |
| 564 | shift; |
| 565 | foundparm=0 |
| 566 | else |
| 567 | paramerror=1 |
| 568 | if [ -z "$paramerror_str" ]; then |
| 569 | paramerror_str="Repo policy shall be 'local' or 'remote'" |
| 570 | fi |
| 571 | fi |
| 572 | fi |
| 573 | fi |
| 574 | fi |
| 575 | if [ $paramerror -eq 0 ]; then |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 576 | if [ "$1" == "--cluster-timeout" ]; then |
| 577 | shift; |
| 578 | CLUSTER_TIME_OUT=$1 |
| 579 | if [ -z "$1" ]; then |
| 580 | paramerror=1 |
| 581 | if [ -z "$paramerror_str" ]; then |
| 582 | paramerror_str="No timeout value found for : '--cluster-timeout'" |
| 583 | fi |
| 584 | else |
| 585 | #Check if positive int |
| 586 | case ${CLUSTER_TIME_OUT#[+]} in |
| 587 | *[!0-9]* | '') |
| 588 | paramerror=1 |
| 589 | if [ -z "$paramerror_str" ]; then |
| 590 | paramerror_str="Value for '--cluster-timeout' not an int : "$CLUSTER_TIME_OUT |
| 591 | fi |
| 592 | ;; |
| 593 | * ) ;; # Ok |
| 594 | esac |
| 595 | echo "Option set - Cluster timeout: "$1 |
| 596 | shift; |
| 597 | foundparm=0 |
| 598 | fi |
| 599 | fi |
| 600 | fi |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 601 | if [ $paramerror -eq 0 ]; then |
| 602 | if [ "$1" == "--print-stats" ]; then |
| 603 | PRINT_CURRENT_STATS=1 |
| 604 | echo "Option set - Print stats" |
| 605 | shift; |
| 606 | foundparm=0 |
| 607 | fi |
| 608 | fi |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 609 | done |
| 610 | echo "" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 611 | |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 612 | #Still params left? |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 613 | if [ $paramerror -eq 0 ] && [ $# -gt 0 ]; then |
| 614 | paramerror=1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 615 | if [ -z "$paramerror_str" ]; then |
| 616 | paramerror_str="Unknown parameter(s): "$@ |
| 617 | fi |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 618 | fi |
| 619 | |
| 620 | if [ $paramerror -eq 1 ]; then |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 621 | echo -e $RED"Incorrect arg list: "$paramerror_str$ERED |
| 622 | __print_args |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 623 | exit 1 |
| 624 | fi |
| 625 | |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 626 | # sourcing the selected env variables for the test case |
| 627 | if [ -f "$TEST_ENV_VAR_FILE" ]; then |
| 628 | echo -e $BOLD"Sourcing env vars from: "$TEST_ENV_VAR_FILE$EBOLD |
| 629 | . $TEST_ENV_VAR_FILE |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 630 | |
| 631 | if [ -z "$TEST_ENV_PROFILE" ] || [ -z "$SUPPORTED_PROFILES" ]; then |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 632 | 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 |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 633 | else |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 634 | found_profile=0 |
| 635 | for prof in $SUPPORTED_PROFILES; do |
| 636 | if [ "$TEST_ENV_PROFILE" == "$prof" ]; then |
| 637 | echo -e $GREEN"Test case supports the selected test env file"$EGREEN |
| 638 | found_profile=1 |
| 639 | fi |
| 640 | done |
| 641 | if [ $found_profile -ne 1 ]; then |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 642 | echo -e $RED"Test case does not support the selected test env file"$ERED |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 643 | echo "Profile: "$TEST_ENV_PROFILE" Supported profiles: "$SUPPORTED_PROFILES |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 644 | echo -e $RED"Exiting...."$ERED |
| 645 | exit 1 |
| 646 | fi |
| 647 | fi |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 648 | else |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 649 | echo -e $RED"Selected env var file does not exist: "$TEST_ENV_VAR_FILE$ERED |
BjornMagnussonXA | 39ad50e | 2020-10-22 09:55:25 +0200 | [diff] [blame] | 650 | echo " Select one of following env var file matching the intended target of the test" |
| 651 | echo " Restart the test using the flag '--env-file <path-to-env-file>" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 652 | ls $AUTOTEST_HOME/../common/test_env* | indent1 |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 653 | exit 1 |
| 654 | fi |
| 655 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 656 | #This var need be preserved from the command line option, if set, when env var is sourced. |
| 657 | if [ ! -z "$TMP_RIC_SIM_PREFIX" ]; then |
| 658 | RIC_SIM_PREFIX=$TMP_RIC_SIM_PREFIX |
| 659 | fi |
| 660 | |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 661 | if [ -z "$PROJECT_IMAGES_APP_NAMES" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 662 | echo -e $RED"Var PROJECT_IMAGES_APP_NAMES must be defined in: "$TEST_ENV_VAR_FILE $ERED |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 663 | exit 1 |
| 664 | fi |
| 665 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 666 | if [[ $SUPPORTED_RUNMODES != *"$RUNMODE"* ]]; then |
| 667 | echo -e $RED"This test script does not support RUNMODE $RUNMODE"$ERED |
| 668 | echo "Supported RUNMODEs: "$SUPPORTED_RUNMODES |
| 669 | exit 1 |
| 670 | fi |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 671 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 672 | # Choose list of included apps depending on run-mode |
| 673 | if [ $RUNMODE == "KUBE" ]; then |
| 674 | INCLUDED_IMAGES=$KUBE_INCLUDED_IMAGES |
| 675 | else |
| 676 | INCLUDED_IMAGES=$DOCKER_INCLUDED_IMAGES |
| 677 | fi |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 678 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 679 | # Check needed installed sw |
| 680 | tmp=$(which python3) |
| 681 | if [ $? -ne 0 ] || [ -z tmp ]; then |
| 682 | echo -e $RED"python3 is required to run the test environment, pls install"$ERED |
| 683 | exit 1 |
| 684 | fi |
| 685 | tmp=$(which docker) |
| 686 | if [ $? -ne 0 ] || [ -z tmp ]; then |
| 687 | echo -e $RED"docker is required to run the test environment, pls install"$ERED |
| 688 | exit 1 |
| 689 | fi |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 690 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 691 | tmp=$(which docker-compose) |
| 692 | if [ $? -ne 0 ] || [ -z tmp ]; then |
| 693 | if [ $RUNMODE == "DOCKER" ]; then |
| 694 | echo -e $RED"docker-compose is required to run the test environment, pls install"$ERED |
| 695 | exit 1 |
| 696 | fi |
| 697 | fi |
BjornMagnussonXA | 575869c | 2020-09-14 21:28:54 +0200 | [diff] [blame] | 698 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 699 | tmp=$(which kubectl) |
| 700 | if [ $? -ne 0 ] || [ -z tmp ]; then |
| 701 | if [ $RUNMODE == "KUBE" ]; then |
| 702 | echo -e $RED"kubectl is required to run the test environment in kubernetes mode, pls install"$ERED |
| 703 | exit 1 |
| 704 | fi |
BjornMagnussonXA | 9ef79da | 2021-06-15 00:08:11 +0200 | [diff] [blame] | 705 | else |
| 706 | if [ $RUNMODE == "KUBE" ]; then |
| 707 | res=$(kubectl cluster-info 2>&1) |
| 708 | if [ $? -ne 0 ]; then |
| 709 | echo -e "$BOLD$RED############################################# $ERED$EBOLD" |
| 710 | echo -e $BOLD$RED"Command 'kubectl cluster-info' returned error $ERED$EBOLD" |
| 711 | echo -e "$BOLD$RED############################################# $ERED$EBOLD" |
| 712 | echo " " |
| 713 | echo "kubectl response:" |
| 714 | echo $res |
| 715 | echo " " |
| 716 | echo "This script may have been started with user with no permission to run kubectl" |
| 717 | echo "Try running with 'sudo' or set 'KUBECONFIG'" |
| 718 | echo "Do either 1, 2 or 3 " |
| 719 | echo " " |
| 720 | echo "1" |
| 721 | echo "Run with sudo" |
| 722 | echo -e $BOLD"sudo <test-script-and-parameters>"$EBOLD |
| 723 | echo " " |
| 724 | echo "2" |
| 725 | echo "Export KUBECONFIG and pass env to sudo - (replace user)" |
| 726 | echo -e $BOLD"export KUBECONFIG='/home/<user>/.kube/config'"$EBOLD |
| 727 | echo -e $BOLD"sudo -E <test-script-and-parameters>"$EBOLD |
| 728 | echo " " |
| 729 | echo "3" |
| 730 | echo "Set KUBECONFIG inline (replace user)" |
| 731 | echo -e $BOLD"sudo KUBECONFIG='/home/<user>/.kube/config' <test-script-and-parameters>"$EBOLD |
| 732 | |
| 733 | exit 1 |
| 734 | fi |
| 735 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 736 | fi |
BjornMagnussonXA | de4d0f8 | 2020-11-29 16:04:06 +0100 | [diff] [blame] | 737 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 738 | echo -e $BOLD"Checking configured image setting for this test case"$EBOLD |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 739 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 740 | #Temp var to check for image variable name errors |
| 741 | IMAGE_ERR=0 |
| 742 | #Create a file with image info for later printing as a table |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 743 | image_list_file="./tmp/.image-list" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 744 | echo -e "Application\tApp short name\tImage\ttag\ttag-switch" > $image_list_file |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 745 | |
| 746 | # Check if image env var is set and if so export the env var with image to use (used by docker compose files) |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 747 | # arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name> |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 748 | __check_and_create_image_var() { |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 749 | |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 750 | if [ $# -ne 6 ]; then |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 751 | echo "Expected arg: <app-short-name> <target-variable-name> <image-variable-name> <image-tag-variable-name> <tag-suffix> <image name>" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 752 | ((IMAGE_ERR++)) |
| 753 | return |
| 754 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 755 | |
| 756 | __check_included_image $1 |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 757 | if [ $? -ne 0 ]; then |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 758 | echo -e "$6\t$1\t<image-excluded>\t<no-tag>" >> $image_list_file |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 759 | # Image is excluded since the corresponding app is not used in this test |
| 760 | return |
| 761 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 762 | tmp=${6}"\t"${1}"\t" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 763 | #Create var from the input var names |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 764 | image="${!3}" |
| 765 | tmptag=$4"_"$5 |
| 766 | tag="${!tmptag}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 767 | |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 768 | optional_image_repo_target="" |
| 769 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 770 | if [ -z $image ]; then |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 771 | __check_ignore_image $1 |
| 772 | if [ $? -eq 0 ]; then |
| 773 | app_ds=$6 |
| 774 | if [ -z "$6" ]; then |
| 775 | app_ds="<app ignored>" |
| 776 | fi |
| 777 | echo -e "$app_ds\t$1\t<image-ignored>\t<no-tag>" >> $image_list_file |
| 778 | # Image is ignored since the corresponding the images is not set in the env file |
| 779 | __remove_included_image $1 # Remove the image from the list of included images |
| 780 | return |
| 781 | fi |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 782 | echo -e $RED"\$"$3" not set in $TEST_ENV_VAR_FILE"$ERED |
| 783 | ((IMAGE_ERR++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 784 | echo "" |
| 785 | tmp=$tmp"<no-image>\t" |
| 786 | else |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 787 | |
| 788 | optional_image_repo_target=$image |
| 789 | |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 790 | #Add repo depending on image type |
| 791 | if [ "$5" == "REMOTE_RELEASE" ]; then |
| 792 | image=$NEXUS_RELEASE_REPO$image |
| 793 | fi |
| 794 | if [ "$5" == "REMOTE" ]; then |
| 795 | image=$NEXUS_STAGING_REPO$image |
| 796 | fi |
| 797 | if [ "$5" == "REMOTE_SNAPSHOT" ]; then |
| 798 | image=$NEXUS_SNAPSHOT_REPO$image |
| 799 | fi |
| 800 | if [ "$5" == "REMOTE_PROXY" ]; then |
| 801 | image=$NEXUS_PROXY_REPO$image |
| 802 | fi |
| 803 | if [ "$5" == "REMOTE_RELEASE_ONAP" ]; then |
| 804 | image=$NEXUS_RELEASE_REPO_ONAP$image |
| 805 | fi |
| 806 | if [ "$5" == "REMOTE_RELEASE_ORAN" ]; then |
| 807 | image=$NEXUS_RELEASE_REPO_ORAN$image |
| 808 | fi |
| 809 | #No nexus repo added for local images, tag: LOCAL |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 810 | tmp=$tmp$image"\t" |
| 811 | fi |
| 812 | if [ -z $tag ]; then |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 813 | echo -e $RED"\$"$tmptag" not set in $TEST_ENV_VAR_FILE"$ERED |
| 814 | ((IMAGE_ERR++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 815 | echo "" |
| 816 | tmp=$tmp"<no-tag>\t" |
| 817 | else |
| 818 | tmp=$tmp$tag |
| 819 | fi |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 820 | tmp=$tmp"\t"$5 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 821 | echo -e "$tmp" >> $image_list_file |
| 822 | #Export the env var |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 823 | export "${2}"=$image":"$tag #Note, this var may be set to the value of the target value below in __check_and_pull_image |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 824 | |
| 825 | remote_or_local_push=false |
| 826 | if [ ! -z "$IMAGE_REPO_ADR" ] && [[ $5 != *"PROXY"* ]]; then |
| 827 | if [ $5 == "LOCAL" ]; then |
| 828 | remote_or_local_push=true |
| 829 | fi |
| 830 | if [[ $5 == *"REMOTE"* ]]; then |
| 831 | if [ "$IMAGE_REPO_POLICY" == "remote" ]; then |
| 832 | remote_or_local_push=true |
| 833 | fi |
| 834 | fi |
| 835 | fi |
| 836 | if $remote_or_local_push; then # Only re-tag and push images according to policy, if repo is given |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 837 | export "${2}_SOURCE"=$image":"$tag #Var to keep the actual source image |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 838 | if [[ $optional_image_repo_target == *"/"* ]]; then # Replace all / with _ for images to push to external repo |
| 839 | optional_image_repo_target_tmp=${optional_image_repo_target//\//_} |
| 840 | optional_image_repo_target=$optional_image_repo_target_tmp |
| 841 | fi |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 842 | export "${2}_TARGET"=$IMAGE_REPO_ADR"/"$optional_image_repo_target":"$tag #Create image + tag for optional image repo - pushed later if needed |
| 843 | else |
| 844 | export "${2}_SOURCE"="" |
| 845 | export "${2}_TARGET"="" |
| 846 | fi |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 847 | } |
| 848 | |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 849 | # Check if app uses image included in this test run |
| 850 | # Returns 0 if image is included, 1 if not |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 851 | __check_included_image() { |
| 852 | for im in $INCLUDED_IMAGES; do |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 853 | if [ "$1" == "$im" ]; then |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 854 | return 0 |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 855 | fi |
| 856 | done |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 857 | return 1 |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 858 | } |
| 859 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 860 | # Check if app uses a project image |
| 861 | # Returns 0 if image is included, 1 if not |
| 862 | __check_project_image() { |
| 863 | for im in $PROJECT_IMAGES; do |
| 864 | if [ "$1" == "$im" ]; then |
| 865 | return 0 |
| 866 | fi |
| 867 | done |
| 868 | return 1 |
| 869 | } |
| 870 | |
| 871 | # Check if app uses image built by the test script |
| 872 | # Returns 0 if image is included, 1 if not |
| 873 | __check_image_local_build() { |
| 874 | for im in $LOCAL_IMAGE_BUILD; do |
| 875 | if [ "$1" == "$im" ]; then |
| 876 | return 0 |
| 877 | fi |
| 878 | done |
| 879 | return 1 |
| 880 | } |
| 881 | |
| 882 | # Check if app image is conditionally ignored in this test run |
| 883 | # Returns 0 if image is conditionally ignored, 1 if not |
| 884 | __check_ignore_image() { |
| 885 | for im in $CONDITIONALLY_IGNORED_IMAGES; do |
| 886 | if [ "$1" == "$im" ]; then |
| 887 | return 0 |
| 888 | fi |
| 889 | done |
| 890 | return 1 |
| 891 | } |
| 892 | |
| 893 | # Removed image from included list of included images |
| 894 | # Used when an image is marked as conditionally ignored |
| 895 | __remove_included_image() { |
| 896 | tmp_img_rem_list="" |
| 897 | for im in $INCLUDED_IMAGES; do |
| 898 | if [ "$1" != "$im" ]; then |
| 899 | tmp_img_rem_list=$tmp_img_rem_list" "$im |
| 900 | fi |
| 901 | done |
| 902 | INCLUDED_IMAGES=$tmp_img_rem_list |
| 903 | return 0 |
| 904 | } |
| 905 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 906 | # Check if app is included in the prestarted set of apps |
| 907 | # Returns 0 if image is included, 1 if not |
| 908 | __check_prestarted_image() { |
| 909 | for im in $KUBE_PRESTARTED_IMAGES; do |
| 910 | if [ "$1" == "$im" ]; then |
| 911 | return 0 |
| 912 | fi |
| 913 | done |
| 914 | return 1 |
| 915 | } |
| 916 | |
| 917 | # Check if an app shall use a local image, based on the cmd parameters |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 918 | __check_image_local_override() { |
| 919 | for im in $USE_LOCAL_IMAGES; do |
| 920 | if [ "$1" == "$im" ]; then |
| 921 | return 1 |
| 922 | fi |
| 923 | done |
| 924 | return 0 |
| 925 | } |
| 926 | |
| 927 | # Check if app uses image override |
| 928 | # Returns the image/tag suffix LOCAL for local image or REMOTE/REMOTE_RELEASE/REMOTE_SNAPSHOT for staging/release/snapshot image |
| 929 | __check_image_override() { |
| 930 | |
| 931 | for im in $ORAN_IMAGES_APP_NAMES; do |
| 932 | if [ "$1" == "$im" ]; then |
| 933 | echo "REMOTE_RELEASE_ORAN" |
| 934 | return 0 |
| 935 | fi |
| 936 | done |
| 937 | |
| 938 | for im in $ONAP_IMAGES_APP_NAMES; do |
| 939 | if [ "$1" == "$im" ]; then |
| 940 | echo "REMOTE_RELEASE_ONAP" |
| 941 | return 0 |
| 942 | fi |
| 943 | done |
| 944 | |
| 945 | found=0 |
| 946 | for im in $PROJECT_IMAGES_APP_NAMES; do |
| 947 | if [ "$1" == "$im" ]; then |
| 948 | found=1 |
| 949 | fi |
| 950 | done |
| 951 | |
| 952 | if [ $found -eq 0 ]; then |
| 953 | echo "REMOTE_PROXY" |
| 954 | return 0 |
| 955 | fi |
| 956 | |
| 957 | suffix="" |
| 958 | if [ $IMAGE_CATEGORY == "RELEASE" ]; then |
| 959 | suffix="REMOTE_RELEASE" |
| 960 | fi |
| 961 | if [ $IMAGE_CATEGORY == "DEV" ]; then |
| 962 | suffix="REMOTE" |
| 963 | fi |
| 964 | CTR=0 |
| 965 | for im in $USE_STAGING_IMAGES; do |
| 966 | if [ "$1" == "$im" ]; then |
| 967 | suffix="REMOTE" |
| 968 | ((CTR++)) |
| 969 | fi |
| 970 | done |
| 971 | for im in $USE_RELEASE_IMAGES; do |
| 972 | if [ "$1" == "$im" ]; then |
| 973 | suffix="REMOTE_RELEASE" |
| 974 | ((CTR++)) |
| 975 | fi |
| 976 | done |
| 977 | for im in $USE_SNAPSHOT_IMAGES; do |
| 978 | if [ "$1" == "$im" ]; then |
| 979 | suffix="REMOTE_SNAPSHOT" |
| 980 | ((CTR++)) |
| 981 | fi |
| 982 | done |
| 983 | for im in $USE_LOCAL_IMAGES; do |
| 984 | if [ "$1" == "$im" ]; then |
| 985 | suffix="LOCAL" |
| 986 | ((CTR++)) |
| 987 | fi |
| 988 | done |
| 989 | echo $suffix |
| 990 | if [ $CTR -gt 1 ]; then |
| 991 | exit 1 |
| 992 | fi |
| 993 | return 0 |
| 994 | } |
| 995 | |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 996 | # Function to re-tag and image and push to another image repo |
| 997 | __retag_and_push_image() { |
| 998 | if [ ! -z "$IMAGE_REPO_ADR" ]; then |
| 999 | source_image="${!1}" |
| 1000 | trg_var_name=$1_"TARGET" # This var is created in func __check_and_create_image_var |
| 1001 | target_image="${!trg_var_name}" |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 1002 | |
| 1003 | if [ -z $target_image ]; then |
| 1004 | return 0 # Image with no target shall not be pushed |
| 1005 | fi |
| 1006 | |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1007 | echo -ne " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD}${SAMELINE}" |
| 1008 | tmp=$(docker image tag $source_image ${target_image} ) |
| 1009 | if [ $? -ne 0 ]; then |
| 1010 | docker stop $tmp &> ./tmp/.dockererr |
| 1011 | ((IMAGE_ERR++)) |
| 1012 | echo "" |
| 1013 | echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}" |
| 1014 | cat ./tmp/.dockererr |
| 1015 | return 1 |
| 1016 | else |
| 1017 | echo -e " Attempt to re-tag image to: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}" |
| 1018 | fi |
| 1019 | echo -ne " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD}${SAMELINE}" |
| 1020 | tmp=$(docker push ${target_image} ) |
| 1021 | if [ $? -ne 0 ]; then |
| 1022 | docker stop $tmp &> ./tmp/.dockererr |
| 1023 | ((IMAGE_ERR++)) |
| 1024 | echo "" |
| 1025 | echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${RED}Failed${ERED}" |
| 1026 | cat ./tmp/.dockererr |
| 1027 | return 1 |
| 1028 | else |
| 1029 | echo -e " Attempt to push re-tagged image: ${BOLD}${target_image}${EBOLD} - ${GREEN}OK${EGREEN}" |
| 1030 | fi |
| 1031 | export "${1}"=$target_image |
| 1032 | fi |
| 1033 | return 0 |
| 1034 | } |
| 1035 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1036 | #Function to check if image exist and stop+remove the container+pull new images as needed |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1037 | #args <script-start-arg> <descriptive-image-name> <container-base-name> <image-with-tag-var-name> |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1038 | __check_and_pull_image() { |
| 1039 | |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1040 | source_image="${!4}" |
| 1041 | |
| 1042 | echo -e " Checking $BOLD$2$EBOLD container(s) with basename: $BOLD$3$EBOLD using image: $BOLD$source_image$EBOLD" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1043 | format_string="\"{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\"" |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1044 | tmp_im=$(docker images --format $format_string $source_image) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1045 | |
| 1046 | if [ $1 == "local" ]; then |
| 1047 | if [ -z "$tmp_im" ]; then |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1048 | 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 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1049 | ((IMAGE_ERR++)) |
| 1050 | return 1 |
| 1051 | else |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1052 | echo -e " "$2" (local image): \033[1m"$source_image"\033[0m "$GREEN"OK"$EGREEN |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1053 | fi |
| 1054 | elif [ $1 == "remote" ] || [ $1 == "remote-remove" ]; then |
| 1055 | if [ $1 == "remote-remove" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1056 | if [ $RUNMODE == "DOCKER" ]; then |
| 1057 | echo -ne " Attempt to stop and remove container(s), if running - ${SAMELINE}" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1058 | tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1059 | if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then |
| 1060 | docker stop $tmp &> ./tmp/.dockererr |
| 1061 | if [ $? -ne 0 ]; then |
| 1062 | ((IMAGE_ERR++)) |
| 1063 | echo "" |
| 1064 | echo -e $RED" Container(s) could not be stopped - try manual stopping the container(s)"$ERED |
| 1065 | cat ./tmp/.dockererr |
| 1066 | return 1 |
| 1067 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1068 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1069 | echo -ne " Attempt to stop and remove container(s), if running - "$GREEN"stopped"$EGREEN"${SAMELINE}" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1070 | tmp=$(docker ps -aq --filter name=${3} --filter network=${DOCKER_SIM_NWNAME}) &> /dev/null |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1071 | if [ $? -eq 0 ] && [ ! -z "$tmp" ]; then |
| 1072 | docker rm $tmp &> ./tmp/.dockererr |
| 1073 | if [ $? -ne 0 ]; then |
| 1074 | ((IMAGE_ERR++)) |
| 1075 | echo "" |
| 1076 | echo -e $RED" Container(s) could not be removed - try manual removal of the container(s)"$ERED |
| 1077 | cat ./tmp/.dockererr |
| 1078 | return 1 |
| 1079 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1080 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1081 | echo -e " Attempt to stop and remove container(s), if running - "$GREEN"stopped removed"$EGREEN |
| 1082 | tmp_im="" |
| 1083 | else |
| 1084 | tmp_im="" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1085 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1086 | fi |
| 1087 | if [ -z "$tmp_im" ]; then |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 1088 | echo -ne " Pulling image${SAMELINE}" |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1089 | out=$(docker pull $source_image) |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 1090 | if [ $? -ne 0 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1091 | echo "" |
| 1092 | echo -e " Pulling image -$RED could not be pulled"$ERED |
| 1093 | ((IMAGE_ERR++)) |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 1094 | echo $out > ./tmp/.dockererr |
| 1095 | echo $out |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1096 | return 1 |
| 1097 | fi |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 1098 | echo $out > ./tmp/.dockererr |
| 1099 | if [[ $out == *"up to date"* ]]; then |
| 1100 | echo -e " Pulling image -$GREEN Image is up to date $EGREEN" |
| 1101 | elif [[ $out == *"Downloaded newer image"* ]]; then |
| 1102 | echo -e " Pulling image -$GREEN Newer image pulled $EGREEN" |
| 1103 | else |
| 1104 | echo -e " Pulling image -$GREEN Pulled $EGREEN" |
| 1105 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1106 | else |
| 1107 | echo -e " Pulling image -$GREEN OK $EGREEN(exists in local repository)" |
| 1108 | fi |
| 1109 | fi |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1110 | |
| 1111 | __retag_and_push_image $4 |
| 1112 | |
| 1113 | return $? |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1116 | setup_testenvironment() { |
| 1117 | # Check that image env setting are available |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1118 | echo "" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1119 | |
| 1120 | # Image var setup for all project images included in the test |
| 1121 | for imagename in $APP_SHORT_NAMES; do |
| 1122 | __check_included_image $imagename |
| 1123 | incl=$? |
| 1124 | __check_project_image $imagename |
| 1125 | proj=$? |
| 1126 | if [ $incl -eq 0 ]; then |
| 1127 | if [ $proj -eq 0 ]; then |
| 1128 | IMAGE_SUFFIX=$(__check_image_override $imagename) |
| 1129 | if [ $? -ne 0 ]; then |
| 1130 | echo -e $RED"Image setting from cmd line not consistent for $imagename."$ERED |
| 1131 | ((IMAGE_ERR++)) |
| 1132 | fi |
| 1133 | else |
| 1134 | IMAGE_SUFFIX="none" |
| 1135 | fi |
| 1136 | # A function name is created from the app short name |
| 1137 | # for example app short name 'ECS' -> produce the function |
| 1138 | # name __ECS_imagesetup |
| 1139 | # This function is called and is expected to exist in the imported |
| 1140 | # file for the ecs test functions |
| 1141 | # The resulting function impl will call '__check_and_create_image_var' function |
| 1142 | # with appropriate parameters |
| 1143 | # If the image suffix is none, then the component decides the suffix |
| 1144 | function_pointer="__"$imagename"_imagesetup" |
| 1145 | $function_pointer $IMAGE_SUFFIX |
| 1146 | fi |
| 1147 | done |
| 1148 | |
| 1149 | #Errors in image setting - exit |
| 1150 | if [ $IMAGE_ERR -ne 0 ]; then |
| 1151 | exit 1 |
BjornMagnussonXA | 89b64ab | 2020-12-16 09:21:01 +0100 | [diff] [blame] | 1152 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1153 | |
| 1154 | #Print a tables of the image settings |
| 1155 | echo -e $BOLD"Images configured for start arg: "$START_ARG $EBOLD |
| 1156 | column -t -s $'\t' $image_list_file | indent1 |
| 1157 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1158 | echo "" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1159 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1160 | #Set the SIM_GROUP var |
| 1161 | echo -e $BOLD"Setting var to main dir of all container/simulator scripts"$EBOLD |
| 1162 | if [ -z "$SIM_GROUP" ]; then |
| 1163 | SIM_GROUP=$AUTOTEST_HOME/../simulator-group |
| 1164 | if [ ! -d $SIM_GROUP ]; then |
| 1165 | echo "Trying to set env var SIM_GROUP to dir 'simulator-group' in the nontrtric repo, but failed." |
| 1166 | echo -e $RED"Please set the SIM_GROUP manually in the applicable $TEST_ENV_VAR_FILE"$ERED |
| 1167 | exit 1 |
| 1168 | else |
| 1169 | echo " SIM_GROUP auto set to: " $SIM_GROUP |
| 1170 | fi |
| 1171 | elif [ $SIM_GROUP = *simulator_group ]; then |
| 1172 | 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 |
| 1173 | exit 1 |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1174 | else |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1175 | echo " SIM_GROUP env var already set to: " $SIM_GROUP |
| 1176 | fi |
| 1177 | |
| 1178 | echo "" |
| 1179 | |
| 1180 | #Temp var to check for image pull errors |
| 1181 | IMAGE_ERR=0 |
| 1182 | |
| 1183 | # The following sequence pull the configured images |
| 1184 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1185 | |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1186 | echo -e $BOLD"Pulling configured images, if needed"$EBOLD |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 1187 | if [ ! -z "$IMAGE_REPO_ADR" ] && [ $IMAGE_REPO_POLICY == "local" ]; then |
| 1188 | echo -e $YELLOW" Excluding all remote image check/pull when running with image repo: $IMAGE_REPO_ADR and image policy $IMAGE_REPO_POLICY"$EYELLOW |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1189 | else |
| 1190 | for imagename in $APP_SHORT_NAMES; do |
| 1191 | __check_included_image $imagename |
| 1192 | incl=$? |
| 1193 | __check_project_image $imagename |
| 1194 | proj=$? |
| 1195 | if [ $incl -eq 0 ]; then |
| 1196 | if [ $proj -eq 0 ]; then |
| 1197 | START_ARG_MOD=$START_ARG |
| 1198 | __check_image_local_override $imagename |
| 1199 | if [ $? -eq 1 ]; then |
| 1200 | START_ARG_MOD="local" |
| 1201 | fi |
| 1202 | else |
| 1203 | START_ARG_MOD=$START_ARG |
| 1204 | fi |
| 1205 | __check_image_local_build $imagename |
| 1206 | #No pull of images built locally |
| 1207 | if [ $? -ne 0 ]; then |
| 1208 | # A function name is created from the app short name |
| 1209 | # for example app short name 'HTTPPROXY' -> produce the function |
| 1210 | # name __HTTPPROXY_imagesetup |
| 1211 | # This function is called and is expected to exist in the imported |
| 1212 | # file for the httpproxy test functions |
| 1213 | # The resulting function impl will call '__check_and_pull_image' function |
| 1214 | # with appropriate parameters |
| 1215 | function_pointer="__"$imagename"_imagepull" |
| 1216 | $function_pointer $START_ARG_MOD $START_ARG |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1217 | fi |
| 1218 | else |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1219 | echo -e $YELLOW" Excluding $imagename image from image check/pull"$EYELLOW |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1220 | fi |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1221 | done |
| 1222 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1223 | |
| 1224 | #Errors in image setting - exit |
| 1225 | if [ $IMAGE_ERR -ne 0 ]; then |
| 1226 | echo "" |
| 1227 | echo "#################################################################################################" |
| 1228 | echo -e $RED"One or more images could not be pulled or containers using the images could not be stopped/removed"$ERED |
| 1229 | echo -e $RED"Or local image, overriding remote image, does not exist"$ERED |
| 1230 | if [ $IMAGE_CATEGORY == "DEV" ]; then |
BjornMagnussonXA | 720f5d7 | 2021-08-13 10:37:23 +0200 | [diff] [blame] | 1231 | echo "" |
| 1232 | echo -e $RED"Note that SNAPSHOT and staging images may be purged from nexus after a certain period."$ERED |
| 1233 | echo -e $RED"In addition, the image may not have been updated in the current release so no SNAPSHOT or staging image exists"$ERED |
| 1234 | echo -e $RED"In these cases, switch to use a released image instead, use the flag '--use-release-image <App-short-name>'"$ERED |
| 1235 | echo -e $RED"Use the 'App-short-name' for the applicable image from the above table: 'Images configured for start arg'."$ERED |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1236 | fi |
| 1237 | echo "#################################################################################################" |
| 1238 | echo "" |
BjornMagnussonXA | 2791e08 | 2020-11-12 00:52:08 +0100 | [diff] [blame] | 1239 | exit 1 |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1240 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1241 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1242 | echo "" |
YongchaoWu | f309b1b | 2020-01-22 13:24:48 +0100 | [diff] [blame] | 1243 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1244 | echo -e $BOLD"Building images needed for test"$EBOLD |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 1245 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1246 | for imagename in $APP_SHORT_NAMES; do |
| 1247 | cd $AUTOTEST_HOME #Always reset to orig dir |
| 1248 | __check_image_local_build $imagename |
| 1249 | if [ $? -eq 0 ]; then |
| 1250 | __check_included_image $imagename |
| 1251 | if [ $? -eq 0 ]; then |
| 1252 | # A function name is created from the app short name |
| 1253 | # for example app short name 'MR' -> produce the function |
| 1254 | # name __MR_imagebuild |
| 1255 | # This function is called and is expected to exist in the imported |
| 1256 | # file for the mr test functions |
| 1257 | # The resulting function impl shall build the imagee |
| 1258 | function_pointer="__"$imagename"_imagebuild" |
| 1259 | $function_pointer |
YongchaoWu | f309b1b | 2020-01-22 13:24:48 +0100 | [diff] [blame] | 1260 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1261 | else |
| 1262 | echo -e $YELLOW" Excluding image for app $imagename from image build"$EYELLOW |
| 1263 | fi |
| 1264 | fi |
| 1265 | done |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1266 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1267 | cd $AUTOTEST_HOME # Just to make sure... |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1268 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1269 | echo "" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1270 | |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1271 | # Create a table of the images used in the script - from local repo |
| 1272 | echo -e $BOLD"Local docker registry images used in this test script"$EBOLD |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1273 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1274 | docker_tmp_file=./tmp/.docker-images-table |
| 1275 | format_string="{{.Repository}}\\t{{.Tag}}\\t{{.CreatedSince}}\\t{{.Size}}\\t{{.CreatedAt}}" |
| 1276 | echo -e "Application\tRepository\tTag\tCreated since\tSize\tCreated at" > $docker_tmp_file |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1277 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1278 | for imagename in $APP_SHORT_NAMES; do |
| 1279 | __check_included_image $imagename |
| 1280 | if [ $? -eq 0 ]; then |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1281 | # Only print image data if image repo is null, or if image repo is set and image is local |
| 1282 | print_image_data=0 |
| 1283 | if [ -z "$IMAGE_REPO_ADR" ]; then |
| 1284 | print_image_data=1 |
| 1285 | else |
| 1286 | __check_image_local_build $imagename |
| 1287 | if [ $? -eq 0 ]; then |
| 1288 | print_image_data=1 |
| 1289 | fi |
| 1290 | fi |
| 1291 | if [ $print_image_data -eq 1 ]; then |
| 1292 | # A function name is created from the app short name |
| 1293 | # for example app short name 'MR' -> produce the function |
| 1294 | # name __MR_imagebuild |
| 1295 | # This function is called and is expected to exist in the imported |
| 1296 | # file for the mr test functions |
| 1297 | # The resulting function impl shall build the imagee |
| 1298 | function_pointer="__"$imagename"_image_data" |
| 1299 | $function_pointer "$format_string" $docker_tmp_file |
| 1300 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1301 | fi |
| 1302 | done |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 1303 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1304 | column -t -s $'\t' $docker_tmp_file | indent1 |
| 1305 | |
| 1306 | echo "" |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1307 | |
| 1308 | if [ ! -z "$IMAGE_REPO_ADR" ]; then |
| 1309 | |
| 1310 | # Create a table of the images used in the script - from remote repo |
| 1311 | echo -e $BOLD"Remote repo images used in this test script"$EBOLD |
| 1312 | echo -e $YELLOW"-- Note: These image will be pulled when the container starts. Images not managed by the test engine --"$EYELLOW |
| 1313 | |
| 1314 | docker_tmp_file=./tmp/.docker-images-table |
| 1315 | format_string="{{.Repository}}\\t{{.Tag}}" |
| 1316 | echo -e "Application\tRepository\tTag" > $docker_tmp_file |
| 1317 | |
| 1318 | for imagename in $APP_SHORT_NAMES; do |
| 1319 | __check_included_image $imagename |
| 1320 | if [ $? -eq 0 ]; then |
| 1321 | # Only print image data if image repo is null, or if image repo is set and image is local |
| 1322 | __check_image_local_build $imagename |
| 1323 | if [ $? -ne 0 ]; then |
| 1324 | # A function name is created from the app short name |
| 1325 | # for example app short name 'MR' -> produce the function |
| 1326 | # name __MR_imagebuild |
| 1327 | # This function is called and is expected to exist in the imported |
| 1328 | # file for the mr test functions |
| 1329 | # The resulting function impl shall build the imagee |
| 1330 | function_pointer="__"$imagename"_image_data" |
| 1331 | $function_pointer "$format_string" $docker_tmp_file |
| 1332 | fi |
| 1333 | fi |
| 1334 | done |
| 1335 | |
| 1336 | column -t -s $'\t' $docker_tmp_file | indent1 |
| 1337 | |
| 1338 | echo "" |
| 1339 | fi |
| 1340 | |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1341 | if [ $RUNMODE == "KUBE" ]; then |
| 1342 | |
| 1343 | echo "=================================================================================" |
| 1344 | echo "=================================================================================" |
| 1345 | |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1346 | if [ -z "$IMAGE_REPO_ADR" ]; then |
| 1347 | echo -e $YELLOW" The image pull policy is set to 'Never' - assuming a local image repo is available for all images"$EYELLOW |
| 1348 | echo -e " This setting only works on single node clusters on the local machine" |
| 1349 | echo -e " It does not work with multi-node clusters or remote clusters. " |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1350 | export KUBE_IMAGE_PULL_POLICY="Never" |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1351 | else |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1352 | echo -e $YELLOW" The image pull policy is set to 'Always'"$EYELLOW |
| 1353 | echo -e " This setting work on local clusters, multi-node clusters and remote cluster. " |
| 1354 | echo -e " Only locally built images are managed. Remote images are always pulled from remote repos" |
| 1355 | 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" |
| 1356 | export KUBE_IMAGE_PULL_POLICY="Always" |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1357 | fi |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1358 | CLUSTER_IP=$(kubectl config view -o jsonpath={.clusters[0].cluster.server} | awk -F[/:] '{print $4}') |
| 1359 | echo -e $YELLOW" The cluster hostname/ip is: $CLUSTER_IP"$EYELLOW |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 1360 | |
| 1361 | echo "=================================================================================" |
| 1362 | echo "=================================================================================" |
| 1363 | echo "" |
| 1364 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1365 | |
| 1366 | echo -e $BOLD"======================================================="$EBOLD |
| 1367 | echo -e $BOLD"== Common test setup completed - test script begins =="$EBOLD |
| 1368 | echo -e $BOLD"======================================================="$EBOLD |
| 1369 | echo "" |
| 1370 | |
| 1371 | } |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 1372 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1373 | # Function to print the test result, shall be the last cmd in a test script |
| 1374 | # args: - |
| 1375 | # (Function for test scripts) |
| 1376 | print_result() { |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 1377 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1378 | TCTEST_END=$SECONDS |
| 1379 | duration=$((TCTEST_END-TCTEST_START)) |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 1380 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1381 | echo "-------------------------------------------------------------------------------------------------" |
| 1382 | echo "------------------------------------- Test case: "$ATC |
| 1383 | echo "------------------------------------- Ended: "$(date) |
| 1384 | echo "-------------------------------------------------------------------------------------------------" |
| 1385 | echo "-- Description: "$TC_ONELINE_DESCR |
| 1386 | echo "-- Execution time: " $duration " seconds" |
BjornMagnussonXA | 2791e08 | 2020-11-12 00:52:08 +0100 | [diff] [blame] | 1387 | echo "-- Used env file: "$TEST_ENV_VAR_FILE |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1388 | echo "-------------------------------------------------------------------------------------------------" |
| 1389 | echo "------------------------------------- RESULTS" |
YongchaoWu | 4e489b0 | 2020-02-24 09:18:16 +0100 | [diff] [blame] | 1390 | echo "" |
YongchaoWu | 4e489b0 | 2020-02-24 09:18:16 +0100 | [diff] [blame] | 1391 | |
YongchaoWu | 21f17bb | 2020-03-05 12:44:08 +0100 | [diff] [blame] | 1392 | |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1393 | if [ $RES_DEVIATION -gt 0 ]; then |
| 1394 | echo "Test case deviations" |
| 1395 | echo "====================================" |
| 1396 | cat $DEVIATION_FILE |
| 1397 | fi |
| 1398 | echo "" |
| 1399 | echo "Timer measurement in the test script" |
| 1400 | echo "====================================" |
| 1401 | column -t -s $'\t' $TIMER_MEASUREMENTS |
| 1402 | echo "" |
| 1403 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1404 | total=$((RES_PASS+RES_FAIL)) |
| 1405 | if [ $RES_TEST -eq 0 ]; then |
| 1406 | echo -e "\033[1mNo tests seem to have been executed. Check the script....\033[0m" |
| 1407 | echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m" |
| 1408 | echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m" |
| 1409 | echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m" |
| 1410 | echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m" |
| 1411 | elif [ $total != $RES_TEST ]; then |
| 1412 | echo -e "\033[1mTotal number of tests does not match the sum of passed and failed tests. Check the script....\033[0m" |
| 1413 | echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m" |
| 1414 | echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m" |
| 1415 | echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m" |
| 1416 | echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m" |
| 1417 | elif [ $RES_CONF_FAIL -ne 0 ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1418 | echo -e "\033[1mOne or more configurations has failed. Check the script log....\033[0m" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1419 | echo -e "\033[31m\033[1m ___ ___ ___ ___ ___ _____ ___ _ ___ _ _ _ ___ ___ \033[0m" |
| 1420 | echo -e "\033[31m\033[1m/ __|/ __| _ \_ _| _ \_ _| | __/_\ |_ _| | | | | | _ \ __|\033[0m" |
| 1421 | echo -e "\033[31m\033[1m\__ \ (__| /| || _/ | | | _/ _ \ | || |_| |_| | / _| \033[0m" |
| 1422 | echo -e "\033[31m\033[1m|___/\___|_|_\___|_| |_| |_/_/ \_\___|____\___/|_|_\___|\033[0m" |
| 1423 | elif [ $RES_PASS = $RES_TEST ]; then |
| 1424 | echo -e "All tests \033[32m\033[1mPASS\033[0m" |
| 1425 | echo -e "\033[32m\033[1m ___ _ ___ ___ \033[0m" |
| 1426 | echo -e "\033[32m\033[1m | _ \/_\ / __/ __| \033[0m" |
| 1427 | echo -e "\033[32m\033[1m | _/ _ \\__ \__ \\ \033[0m" |
| 1428 | echo -e "\033[32m\033[1m |_|/_/ \_\___/___/ \033[0m" |
| 1429 | echo "" |
YongchaoWu | 21f17bb | 2020-03-05 12:44:08 +0100 | [diff] [blame] | 1430 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1431 | # Update test suite counter |
| 1432 | if [ -f .tmp_tcsuite_pass_ctr ]; then |
| 1433 | tmpval=$(< .tmp_tcsuite_pass_ctr) |
| 1434 | ((tmpval++)) |
| 1435 | echo $tmpval > .tmp_tcsuite_pass_ctr |
| 1436 | fi |
| 1437 | if [ -f .tmp_tcsuite_pass ]; then |
| 1438 | echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_pass |
| 1439 | fi |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 1440 | #Create file with OK exit code |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1441 | echo "0" > "$AUTOTEST_HOME/.result$ATC.txt" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1442 | else |
| 1443 | echo -e "One or more tests with status \033[31m\033[1mFAIL\033[0m " |
| 1444 | echo -e "\033[31m\033[1m ___ _ ___ _ \033[0m" |
| 1445 | echo -e "\033[31m\033[1m | __/_\ |_ _| | \033[0m" |
| 1446 | echo -e "\033[31m\033[1m | _/ _ \ | || |__ \033[0m" |
| 1447 | echo -e "\033[31m\033[1m |_/_/ \_\___|____|\033[0m" |
| 1448 | echo "" |
| 1449 | # Update test suite counter |
| 1450 | if [ -f .tmp_tcsuite_fail_ctr ]; then |
| 1451 | tmpval=$(< .tmp_tcsuite_fail_ctr) |
| 1452 | ((tmpval++)) |
| 1453 | echo $tmpval > .tmp_tcsuite_fail_ctr |
| 1454 | fi |
| 1455 | if [ -f .tmp_tcsuite_fail ]; then |
| 1456 | echo " - "$ATC " -- "$TC_ONELINE_DESCR" Execution time: "$duration" seconds" >> .tmp_tcsuite_fail |
| 1457 | fi |
YongchaoWu | 21f17bb | 2020-03-05 12:44:08 +0100 | [diff] [blame] | 1458 | fi |
YongchaoWu | 21f17bb | 2020-03-05 12:44:08 +0100 | [diff] [blame] | 1459 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1460 | echo "++++ Number of tests: "$RES_TEST |
| 1461 | echo "++++ Number of passed tests: "$RES_PASS |
| 1462 | echo "++++ Number of failed tests: "$RES_FAIL |
YongchaoWu | 4e489b0 | 2020-02-24 09:18:16 +0100 | [diff] [blame] | 1463 | echo "" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1464 | echo "++++ Number of failed configs: "$RES_CONF_FAIL |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 1465 | echo "" |
| 1466 | echo "++++ Number of test case deviations: "$RES_DEVIATION |
| 1467 | echo "" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1468 | echo "------------------------------------- Test case complete ---------------------------------" |
| 1469 | echo "-------------------------------------------------------------------------------------------------" |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 1470 | echo "" |
| 1471 | } |
| 1472 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1473 | ##################################################################### |
| 1474 | ###### Functions for start, configuring, stoping, cleaning etc ###### |
| 1475 | ##################################################################### |
YongchaoWu | 21f17bb | 2020-03-05 12:44:08 +0100 | [diff] [blame] | 1476 | |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 1477 | # Start timer for time measurement |
| 1478 | # args - (any args will be printed though) |
| 1479 | start_timer() { |
| 1480 | echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD |
| 1481 | TC_TIMER=$SECONDS |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 1482 | echo " Timer started: $(date)" |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | # Print the value of the time (in seconds) |
| 1486 | # args - <timer message to print> - timer value and message will be printed both on screen |
| 1487 | # and in the timer measurement report |
| 1488 | print_timer() { |
| 1489 | echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD |
| 1490 | if [ $# -lt 1 ]; then |
| 1491 | ((RES_CONF_FAIL++)) |
| 1492 | __print_err "need 1 or more args, <timer message to print>" $@ |
| 1493 | exit 1 |
| 1494 | fi |
| 1495 | duration=$(($SECONDS-$TC_TIMER)) |
| 1496 | if [ $duration -eq 0 ]; then |
| 1497 | duration="<1 second" |
| 1498 | else |
| 1499 | duration=$duration" seconds" |
| 1500 | fi |
| 1501 | echo " Timer duration :" $duration |
| 1502 | |
| 1503 | echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS |
| 1504 | } |
| 1505 | |
| 1506 | # Print the value of the time (in seconds) and reset the timer |
| 1507 | # args - <timer message to print> - timer value and message will be printed both on screen |
| 1508 | # and in the timer measurement report |
| 1509 | print_and_reset_timer() { |
| 1510 | echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD |
| 1511 | if [ $# -lt 1 ]; then |
| 1512 | ((RES_CONF_FAIL++)) |
| 1513 | __print_err "need 1 or more args, <timer message to print>" $@ |
| 1514 | exit 1 |
| 1515 | fi |
| 1516 | duration=$(($SECONDS-$TC_TIMER))" seconds" |
| 1517 | if [ $duration -eq 0 ]; then |
| 1518 | duration="<1 second" |
| 1519 | else |
| 1520 | duration=$duration" seconds" |
| 1521 | fi |
| 1522 | echo " Timer duration :" $duration |
| 1523 | TC_TIMER=$SECONDS |
| 1524 | echo " Timer reset" |
| 1525 | |
| 1526 | echo -e "${@:1} \t $duration" >> $TIMER_MEASUREMENTS |
| 1527 | |
| 1528 | } |
| 1529 | # Print info about a deviations from intended tests |
| 1530 | # Each deviation counted is also printed in the testreport |
| 1531 | # args <deviation message to print> |
| 1532 | deviation() { |
| 1533 | echo -e $BOLD"DEVIATION(${BASH_LINENO[0]}): "${FUNCNAME[0]} $EBOLD |
| 1534 | if [ $# -lt 1 ]; then |
| 1535 | ((RES_CONF_FAIL++)) |
| 1536 | __print_err "need 1 or more args, <deviation message to print>" $@ |
| 1537 | exit 1 |
| 1538 | fi |
| 1539 | ((RES_DEVIATION++)) |
| 1540 | echo -e $BOLD$YELLOW" Test case deviation: ${@:1}"$EYELLOW$EBOLD |
| 1541 | echo "Line: ${BASH_LINENO[0]} - ${@:1}" >> $DEVIATION_FILE |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 1542 | __print_current_stats |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 1543 | echo "" |
| 1544 | } |
YongchaoWu | 21f17bb | 2020-03-05 12:44:08 +0100 | [diff] [blame] | 1545 | |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 1546 | # Stop at first FAIL test case and take all logs - only for debugging/trouble shooting |
| 1547 | __check_stop_at_error() { |
| 1548 | if [ $STOP_AT_ERROR -eq 1 ]; then |
| 1549 | echo -e $RED"Test script configured to stop at first FAIL, taking all logs and stops"$ERED |
| 1550 | store_logs "STOP_AT_ERROR" |
| 1551 | exit 1 |
| 1552 | fi |
| 1553 | return 0 |
| 1554 | } |
| 1555 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1556 | # Stop and remove all containers |
| 1557 | # args: - |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1558 | # (Not for test scripts) |
| 1559 | __clean_containers() { |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1560 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1561 | echo -e $BOLD"Docker clean and stopping and removing all running containers, by container name"$EBOLD |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1562 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1563 | #Create empty file |
| 1564 | running_contr_file="./tmp/running_contr.txt" |
| 1565 | > $running_contr_file |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1566 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1567 | # Get list of all containers started by the test script |
| 1568 | for imagename in $APP_SHORT_NAMES; do |
| 1569 | 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 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1570 | done |
| 1571 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1572 | tab_heading1="App display name" |
| 1573 | tab_heading2="App short name" |
| 1574 | tab_heading3="Container name" |
| 1575 | |
| 1576 | tab_heading1_len=${#tab_heading1} |
| 1577 | tab_heading2_len=${#tab_heading2} |
| 1578 | tab_heading3_len=${#tab_heading3} |
| 1579 | cntr=0 |
| 1580 | #Calc field lengths of each item in the list of containers |
| 1581 | while read p; do |
| 1582 | if (( $cntr % 3 == 0 ));then |
| 1583 | if [ ${#p} -gt $tab_heading1_len ]; then |
| 1584 | tab_heading1_len=${#p} |
| 1585 | fi |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1586 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1587 | if (( $cntr % 3 == 1));then |
| 1588 | if [ ${#p} -gt $tab_heading2_len ]; then |
| 1589 | tab_heading2_len=${#p} |
| 1590 | fi |
| 1591 | fi |
| 1592 | if (( $cntr % 3 == 2));then |
| 1593 | if [ ${#p} -gt $tab_heading3_len ]; then |
| 1594 | tab_heading3_len=${#p} |
| 1595 | fi |
| 1596 | fi |
| 1597 | let cntr=cntr+1 |
| 1598 | done <$running_contr_file |
| 1599 | |
| 1600 | let tab_heading1_len=tab_heading1_len+2 |
| 1601 | while (( ${#tab_heading1} < $tab_heading1_len)); do |
| 1602 | tab_heading1="$tab_heading1"" " |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 1603 | done |
| 1604 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1605 | let tab_heading2_len=tab_heading2_len+2 |
| 1606 | while (( ${#tab_heading2} < $tab_heading2_len)); do |
| 1607 | tab_heading2="$tab_heading2"" " |
| 1608 | done |
| 1609 | |
| 1610 | let tab_heading3_len=tab_heading3_len+2 |
| 1611 | while (( ${#tab_heading3} < $tab_heading3_len)); do |
| 1612 | tab_heading3="$tab_heading3"" " |
| 1613 | done |
| 1614 | |
| 1615 | echo " $tab_heading1$tab_heading2$tab_heading3"" Actions" |
| 1616 | cntr=0 |
| 1617 | while read p; do |
| 1618 | if (( $cntr % 3 == 0 ));then |
| 1619 | row="" |
| 1620 | heading=$p |
| 1621 | heading_len=$tab_heading1_len |
| 1622 | fi |
| 1623 | if (( $cntr % 3 == 1));then |
| 1624 | heading=$p |
| 1625 | heading_len=$tab_heading2_len |
| 1626 | fi |
| 1627 | if (( $cntr % 3 == 2));then |
| 1628 | contr=$p |
| 1629 | heading=$p |
| 1630 | heading_len=$tab_heading3_len |
| 1631 | fi |
| 1632 | while (( ${#heading} < $heading_len)); do |
| 1633 | heading="$heading"" " |
| 1634 | done |
| 1635 | row=$row$heading |
| 1636 | if (( $cntr % 3 == 2));then |
| 1637 | echo -ne $row$SAMELINE |
| 1638 | echo -ne " $row ${GREEN}stopping...${EGREEN}${SAMELINE}" |
| 1639 | docker stop $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null |
| 1640 | echo -ne " $row ${GREEN}stopped removing...${EGREEN}${SAMELINE}" |
| 1641 | docker rm --force $(docker ps -qa --filter name=${contr} --filter network=$DOCKER_SIM_NWNAME) &> /dev/null |
| 1642 | echo -e " $row ${GREEN}stopped removed ${EGREEN}" |
| 1643 | fi |
| 1644 | let cntr=cntr+1 |
| 1645 | done <$running_contr_file |
| 1646 | |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 1647 | echo "" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1648 | |
| 1649 | echo -e $BOLD" Removing docker network"$EBOLD |
| 1650 | TMP=$(docker network ls -q --filter name=$DOCKER_SIM_NWNAME) |
| 1651 | if [ "$TMP" == $DOCKER_SIM_NWNAME ]; then |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1652 | docker network rm $DOCKER_SIM_NWNAME | indent2 |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1653 | if [ $? -ne 0 ]; then |
| 1654 | echo -e $RED" Cannot remove docker network. Manually remove or disconnect containers from $DOCKER_SIM_NWNAME"$ERED |
| 1655 | exit 1 |
| 1656 | fi |
| 1657 | fi |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1658 | echo -e "$GREEN Done$EGREEN" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1659 | |
| 1660 | echo -e $BOLD" Removing all unused docker neworks"$EBOLD |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1661 | docker network prune --force | indent2 |
| 1662 | echo -e "$GREEN Done$EGREEN" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1663 | |
| 1664 | echo -e $BOLD" Removing all unused docker volumes"$EBOLD |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1665 | docker volume prune --force | indent2 |
| 1666 | echo -e "$GREEN Done$EGREEN" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1667 | |
| 1668 | echo -e $BOLD" Removing all dangling/untagged docker images"$EBOLD |
| 1669 | docker rmi --force $(docker images -q -f dangling=true) &> /dev/null |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 1670 | echo -e "$GREEN Done$EGREEN" |
| 1671 | echo "" |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 1672 | |
| 1673 | CONTRS=$(docker ps | awk '$1 != "CONTAINER" { n++ }; END { print n+0 }') |
| 1674 | if [ $? -eq 0 ]; then |
| 1675 | if [ $CONTRS -ne 0 ]; then |
| 1676 | echo -e $RED"Containers running, may cause distubance to the test case"$ERED |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 1677 | docker ps -a | indent1 |
| 1678 | echo "" |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 1679 | fi |
| 1680 | fi |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 1681 | } |
| 1682 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1683 | ################################### |
| 1684 | ### Functions for kube management |
| 1685 | ################################### |
| 1686 | |
BjornMagnussonXA | 6f9c2b2 | 2021-06-11 16:31:40 +0200 | [diff] [blame] | 1687 | # Get resource type for scaling |
| 1688 | # args: <resource-name> <namespace> |
| 1689 | __kube_get_resource_type() { |
| 1690 | kubectl get deployment $1 -n $2 1> /dev/null 2> ./tmp/kubeerr |
| 1691 | if [ $? -eq 0 ]; then |
| 1692 | echo "deployment" |
| 1693 | return 0 |
| 1694 | fi |
| 1695 | kubectl get sts $1 -n $2 1> /dev/null 2> ./tmp/kubeerr |
| 1696 | if [ $? -eq 0 ]; then |
| 1697 | echo "sts" |
| 1698 | return 0 |
| 1699 | fi |
| 1700 | echo "unknown-resource-type" |
| 1701 | return 1 |
| 1702 | } |
| 1703 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1704 | # Scale a kube resource to a specific count |
| 1705 | # args: <resource-type> <resource-name> <namespace> <target-count> |
| 1706 | # (Not for test scripts) |
| 1707 | __kube_scale() { |
| 1708 | echo -ne " Setting $1 $2 replicas=$4 in namespace $3"$SAMELINE |
| 1709 | kubectl scale $1 $2 -n $3 --replicas=$4 1> /dev/null 2> ./tmp/kubeerr |
| 1710 | if [ $? -ne 0 ]; then |
| 1711 | echo -e " Setting $1 $2 replicas=$4 in namespace $3 $RED Failed $ERED" |
| 1712 | ((RES_CONF_FAIL++)) |
| 1713 | echo " Message: $(<./tmp/kubeerr)" |
| 1714 | return 1 |
| 1715 | else |
| 1716 | echo -e " Setting $1 $2 replicas=$4 in namespace $3 $GREEN OK $EGREEN" |
| 1717 | fi |
| 1718 | |
| 1719 | TSTART=$SECONDS |
| 1720 | |
| 1721 | for i in {1..500}; do |
| 1722 | count=$(kubectl get $1/$2 -n $3 -o jsonpath='{.status.replicas}' 2> /dev/null) |
| 1723 | retcode=$? |
| 1724 | if [ -z "$count" ]; then |
| 1725 | #No value is sometimes returned for some reason, in case the resource has replica 0 |
| 1726 | count=0 |
| 1727 | fi |
| 1728 | if [ $retcode -ne 0 ]; then |
| 1729 | echo -e "$RED Cannot fetch current replica count for $1 $2 in namespace $3 $ERED" |
| 1730 | ((RES_CONF_FAIL++)) |
| 1731 | return 1 |
| 1732 | fi |
| 1733 | #echo "" |
| 1734 | if [ $count -ne $4 ]; then |
| 1735 | echo -ne " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds $SAMELINE" |
| 1736 | sleep $i |
| 1737 | else |
| 1738 | echo -e " Waiting for $1 $2 replicas=$4 in namespace $3. Replicas=$count after $(($SECONDS-$TSTART)) seconds" |
| 1739 | echo -e " Replicas=$4 after $(($SECONDS-$TSTART)) seconds $GREEN OK $EGREEN" |
| 1740 | echo "" |
| 1741 | return 0 |
| 1742 | fi |
| 1743 | done |
| 1744 | echo "" |
| 1745 | echo -e "$RED Replica count did not reach target replicas=$4. Failed with replicas=$count $ERED" |
| 1746 | ((RES_CONF_FAIL++)) |
| 1747 | return 0 |
| 1748 | } |
| 1749 | |
| 1750 | # Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id |
| 1751 | # This function does not wait for the resource to reach 0 |
| 1752 | # args: <namespace> <label-name> <label-id> |
| 1753 | # (Not for test scripts) |
| 1754 | __kube_scale_all_resources() { |
| 1755 | namespace=$1 |
| 1756 | labelname=$2 |
| 1757 | labelid=$3 |
| 1758 | resources="deployment replicaset statefulset" |
| 1759 | for restype in $resources; do |
| 1760 | result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}') |
| 1761 | if [ $? -eq 0 ] && [ ! -z "$result" ]; then |
| 1762 | deleted_resourcetypes=$deleted_resourcetypes" "$restype |
| 1763 | for resid in $result; do |
| 1764 | echo -ne " Ordered caling $restype $resid from namespace $namespace with label $labelname=$labelid to 0"$SAMELINE |
| 1765 | kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr |
| 1766 | echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0 $GREEN OK $EGREEN" |
| 1767 | done |
| 1768 | fi |
| 1769 | done |
| 1770 | } |
| 1771 | |
| 1772 | # Scale all kube resource sets to 0 in a namespace for resources having a certain lable and label-id |
| 1773 | # This function do wait for the resource to reach 0 |
| 1774 | # args: <namespace> <label-name> <label-id> |
| 1775 | # (Not for test scripts) |
| 1776 | __kube_scale_and_wait_all_resources() { |
| 1777 | namespace=$1 |
| 1778 | labelname=$2 |
| 1779 | labelid=$3 |
| 1780 | resources="deployment replicaset statefulset" |
| 1781 | scaled_all=1 |
| 1782 | while [ $scaled_all -ne 0 ]; do |
| 1783 | scaled_all=0 |
| 1784 | for restype in $resources; do |
| 1785 | result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}') |
| 1786 | if [ $? -eq 0 ] && [ ! -z "$result" ]; then |
| 1787 | for resid in $result; do |
| 1788 | echo -e " Ordered scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0" |
| 1789 | kubectl scale $restype $resid -n $namespace --replicas=0 1> /dev/null 2> ./tmp/kubeerr |
| 1790 | count=1 |
| 1791 | T_START=$SECONDS |
| 1792 | while [ $count -ne 0 ]; do |
| 1793 | count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null) |
| 1794 | echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE |
| 1795 | if [ $? -eq 0 ] && [ ! -z "$count" ]; then |
| 1796 | sleep 0.5 |
| 1797 | else |
| 1798 | count=0 |
| 1799 | fi |
| 1800 | duration=$(($SECONDS-$T_START)) |
| 1801 | if [ $duration -gt 100 ]; then |
| 1802 | #Forcring count 0, to avoid hanging for failed scaling |
| 1803 | scaled_all=1 |
| 1804 | count=0 |
| 1805 | fi |
| 1806 | done |
| 1807 | echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN" |
| 1808 | done |
| 1809 | fi |
| 1810 | done |
| 1811 | done |
| 1812 | } |
| 1813 | |
| 1814 | # Remove all kube resources in a namespace for resources having a certain label and label-id |
| 1815 | # This function wait until the resources are gone. Scaling to 0 must have been ordered previously |
| 1816 | # args: <namespace> <label-name> <label-id> |
| 1817 | # (Not for test scripts) |
| 1818 | __kube_delete_all_resources() { |
| 1819 | namespace=$1 |
| 1820 | labelname=$2 |
| 1821 | labelid=$3 |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1822 | resources="deployments replicaset statefulset services pods configmaps persistentvolumeclaims persistentvolumes" |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1823 | deleted_resourcetypes="" |
| 1824 | for restype in $resources; do |
| 1825 | result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}') |
| 1826 | if [ $? -eq 0 ] && [ ! -z "$result" ]; then |
| 1827 | deleted_resourcetypes=$deleted_resourcetypes" "$restype |
| 1828 | for resid in $result; do |
| 1829 | if [ $restype == "replicaset" ] || [ $restype == "statefulset" ]; then |
| 1830 | count=1 |
| 1831 | while [ $count -ne 0 ]; do |
| 1832 | count=$(kubectl get $restype $resid -n $namespace -o jsonpath='{.status.replicas}' 2> /dev/null) |
| 1833 | echo -ne " Scaling $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count"$SAMELINE |
| 1834 | if [ $? -eq 0 ] && [ ! -z "$count" ]; then |
| 1835 | sleep 0.5 |
| 1836 | else |
| 1837 | count=0 |
| 1838 | fi |
| 1839 | done |
| 1840 | echo -e " Scaled $restype $resid from namespace $namespace with label $labelname=$labelid to 0,count=$count $GREEN OK $EGREEN" |
| 1841 | fi |
| 1842 | echo -ne " Deleting $restype $resid from namespace $namespace with label $labelname=$labelid "$SAMELINE |
| 1843 | kubectl delete $restype $resid -n $namespace 1> /dev/null 2> ./tmp/kubeerr |
| 1844 | if [ $? -eq 0 ]; then |
| 1845 | echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN OK $EGREEN" |
| 1846 | else |
| 1847 | echo -e " Deleted $restype $resid from namespace $namespace with label $labelname=$labelid $GREEN Does not exist - OK $EGREEN" |
| 1848 | fi |
| 1849 | #fi |
| 1850 | done |
| 1851 | fi |
| 1852 | done |
| 1853 | if [ ! -z "$deleted_resourcetypes" ]; then |
| 1854 | for restype in $deleted_resources; do |
| 1855 | echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted..."$SAMELINE |
| 1856 | T_START=$SECONDS |
| 1857 | result="dummy" |
| 1858 | while [ ! -z "$result" ]; do |
| 1859 | sleep 0.5 |
| 1860 | result=$(kubectl get $restype -n $namespace -o jsonpath='{.items[?(@.metadata.labels.'$labelname'=="'$labelid'")].metadata.name}') |
| 1861 | echo -ne " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds "$SAMELINE |
| 1862 | if [ -z "$result" ]; then |
| 1863 | echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $GREEN OK $EGREEN" |
| 1864 | elif [ $(($SECONDS-$T_START)) -gt 300 ]; then |
| 1865 | echo -e " Waiting for $restype in namespace $namespace with label $labelname=$labelid to be deleted...$(($SECONDS-$T_START)) seconds $RED Failed $ERED" |
| 1866 | result="" |
| 1867 | fi |
| 1868 | done |
| 1869 | done |
| 1870 | fi |
| 1871 | } |
| 1872 | |
| 1873 | # Creates a namespace if it does not exists |
| 1874 | # args: <namespace> |
| 1875 | # (Not for test scripts) |
| 1876 | __kube_create_namespace() { |
| 1877 | |
| 1878 | #Check if test namespace exists, if not create it |
| 1879 | kubectl get namespace $1 1> /dev/null 2> ./tmp/kubeerr |
| 1880 | if [ $? -ne 0 ]; then |
| 1881 | echo -ne " Creating namespace "$1 $SAMELINE |
| 1882 | kubectl create namespace $1 1> /dev/null 2> ./tmp/kubeerr |
| 1883 | if [ $? -ne 0 ]; then |
| 1884 | echo -e " Creating namespace $1 $RED$BOLD FAILED $EBOLD$ERED" |
| 1885 | ((RES_CONF_FAIL++)) |
| 1886 | echo " Message: $(<./tmp/kubeerr)" |
| 1887 | return 1 |
| 1888 | else |
| 1889 | echo -e " Creating namespace $1 $GREEN$BOLD OK $EBOLD$EGREEN" |
| 1890 | fi |
| 1891 | else |
| 1892 | echo -e " Creating namespace $1 $GREEN$BOLD Already exists, OK $EBOLD$EGREEN" |
| 1893 | fi |
| 1894 | return 0 |
| 1895 | } |
| 1896 | |
| 1897 | # Find the host ip of an app (using the service resource) |
| 1898 | # args: <app-name> <namespace> |
| 1899 | # (Not for test scripts) |
| 1900 | __kube_get_service_host() { |
| 1901 | if [ $# -ne 2 ]; then |
| 1902 | ((RES_CONF_FAIL++)) |
| 1903 | __print_err "need 2 args, <app-name> <namespace>" $@ |
| 1904 | exit 1 |
| 1905 | fi |
| 1906 | for timeout in {1..60}; do |
| 1907 | host=$(kubectl get svc $1 -n $2 -o jsonpath='{.spec.clusterIP}') |
| 1908 | if [ $? -eq 0 ]; then |
| 1909 | if [ ! -z "$host" ]; then |
| 1910 | echo $host |
| 1911 | return 0 |
| 1912 | fi |
| 1913 | fi |
| 1914 | sleep 0.5 |
| 1915 | done |
| 1916 | ((RES_CONF_FAIL++)) |
| 1917 | echo "host-not-found-fatal-error" |
| 1918 | return 1 |
| 1919 | } |
| 1920 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1921 | # Find the named port to an app (using the service resource) |
| 1922 | # args: <app-name> <namespace> <port-name> |
| 1923 | # (Not for test scripts) |
| 1924 | __kube_get_service_port() { |
| 1925 | if [ $# -ne 3 ]; then |
| 1926 | ((RES_CONF_FAIL++)) |
| 1927 | __print_err "need 3 args, <app-name> <namespace> <port-name>" $@ |
| 1928 | exit 1 |
| 1929 | fi |
| 1930 | |
| 1931 | for timeout in {1..60}; do |
| 1932 | port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].port}') |
| 1933 | if [ $? -eq 0 ]; then |
| 1934 | if [ ! -z "$port" ]; then |
| 1935 | echo $port |
| 1936 | return 0 |
| 1937 | fi |
| 1938 | fi |
| 1939 | sleep 0.5 |
| 1940 | done |
| 1941 | ((RES_CONF_FAIL++)) |
| 1942 | echo "0" |
| 1943 | return 1 |
| 1944 | } |
| 1945 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 1946 | # Find the named node port to an app (using the service resource) |
| 1947 | # args: <app-name> <namespace> <port-name> |
| 1948 | # (Not for test scripts) |
| 1949 | __kube_get_service_nodeport() { |
| 1950 | if [ $# -ne 3 ]; then |
| 1951 | ((RES_CONF_FAIL++)) |
| 1952 | __print_err "need 3 args, <app-name> <namespace> <port-name>" $@ |
| 1953 | exit 1 |
| 1954 | fi |
| 1955 | |
| 1956 | for timeout in {1..60}; do |
| 1957 | port=$(kubectl get svc $1 -n $2 -o jsonpath='{...ports[?(@.name=="'$3'")].nodePort}') |
| 1958 | if [ $? -eq 0 ]; then |
| 1959 | if [ ! -z "$port" ]; then |
| 1960 | echo $port |
| 1961 | return 0 |
| 1962 | fi |
| 1963 | fi |
| 1964 | sleep 0.5 |
| 1965 | done |
| 1966 | ((RES_CONF_FAIL++)) |
| 1967 | echo "0" |
| 1968 | return 1 |
| 1969 | } |
| 1970 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 1971 | # Create a kube resource from a yaml template |
| 1972 | # args: <resource-type> <resource-name> <template-yaml> <output-yaml> |
| 1973 | # (Not for test scripts) |
| 1974 | __kube_create_instance() { |
| 1975 | echo -ne " Creating $1 $2"$SAMELINE |
| 1976 | envsubst < $3 > $4 |
| 1977 | kubectl apply -f $4 1> /dev/null 2> ./tmp/kubeerr |
| 1978 | if [ $? -ne 0 ]; then |
| 1979 | ((RES_CONF_FAIL++)) |
| 1980 | echo -e " Creating $1 $2 $RED Failed $ERED" |
| 1981 | echo " Message: $(<./tmp/kubeerr)" |
| 1982 | return 1 |
| 1983 | else |
| 1984 | echo -e " Creating $1 $2 $GREEN OK $EGREEN" |
| 1985 | fi |
| 1986 | } |
| 1987 | |
| 1988 | # Function to create a configmap in kubernetes |
| 1989 | # args: <configmap-name> <namespace> <labelname> <labelid> <path-to-data-file> <path-to-output-yaml> |
| 1990 | # (Not for test scripts) |
| 1991 | __kube_create_configmap() { |
| 1992 | echo -ne " Creating configmap $1 "$SAMELINE |
| 1993 | envsubst < $5 > $5"_tmp" |
| 1994 | cp $5"_tmp" $5 #Need to copy back to orig file name since create configmap neeed the original file name |
| 1995 | kubectl create configmap $1 -n $2 --from-file=$5 --dry-run=client -o yaml > $6 |
| 1996 | if [ $? -ne 0 ]; then |
| 1997 | echo -e " Creating configmap $1 $RED Failed $ERED" |
| 1998 | ((RES_CONF_FAIL++)) |
| 1999 | return 1 |
| 2000 | fi |
| 2001 | |
| 2002 | kubectl apply -f $6 1> /dev/null 2> ./tmp/kubeerr |
| 2003 | if [ $? -ne 0 ]; then |
| 2004 | echo -e " Creating configmap $1 $RED Apply failed $ERED" |
| 2005 | echo " Message: $(<./tmp/kubeerr)" |
| 2006 | ((RES_CONF_FAIL++)) |
| 2007 | return 1 |
| 2008 | fi |
| 2009 | kubectl label configmap $1 -n $2 $3"="$4 --overwrite 1> /dev/null 2> ./tmp/kubeerr |
| 2010 | if [ $? -ne 0 ]; then |
| 2011 | echo -e " Creating configmap $1 $RED Labeling failed $ERED" |
| 2012 | echo " Message: $(<./tmp/kubeerr)" |
| 2013 | ((RES_CONF_FAIL++)) |
| 2014 | return 1 |
| 2015 | fi |
| 2016 | # Log the resulting map |
| 2017 | kubectl get configmap $1 -n $2 -o yaml > $6 |
| 2018 | |
| 2019 | echo -e " Creating configmap $1 $GREEN OK $EGREEN" |
| 2020 | return 0 |
| 2021 | } |
| 2022 | |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 2023 | # This function runs a kubectl cmd where a single output value is expected, for example get ip with jsonpath filter. |
| 2024 | # The function retries up to the timeout given in the cmd flag '--cluster-timeout' |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame] | 2025 | # args: <full kubectl cmd with parameters> |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 2026 | # (Not for test scripts) |
| 2027 | __kube_cmd_with_timeout() { |
| 2028 | TS_TMP=$(($SECONDS+$CLUSTER_TIME_OUT)) |
| 2029 | |
| 2030 | while true; do |
| 2031 | kube_cmd_result=$($@) |
| 2032 | if [ $? -ne 0 ]; then |
| 2033 | kube_cmd_result="" |
| 2034 | fi |
| 2035 | if [ $SECONDS -ge $TS_TMP ] || [ ! -z "$kube_cmd_result" ] ; then |
| 2036 | echo $kube_cmd_result |
| 2037 | return 0 |
| 2038 | fi |
| 2039 | sleep 1 |
| 2040 | done |
| 2041 | } |
| 2042 | |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame] | 2043 | # This function starts a pod that cleans a the contents of a path mounted as a pvc |
| 2044 | # After this action the pod should terminate |
| 2045 | # This should only be executed when the pod owning the pvc is not running |
| 2046 | # args: <appname> <namespace> <pvc-name> <path-to remove> |
| 2047 | # (Not for test scripts) |
| 2048 | __kube_clean_pvc() { |
| 2049 | |
| 2050 | export PVC_CLEANER_NAMESPACE=$2 |
| 2051 | export PVC_CLEANER_CLAIMNAME=$3 |
| 2052 | export PVC_CLEANER_RM_PATH=$4 |
| 2053 | input_yaml=$SIM_GROUP"/pvc-cleaner/"pvc-cleaner.yaml |
| 2054 | output_yaml=$PWD/tmp/$2-pvc-cleaner.yaml |
| 2055 | |
| 2056 | envsubst < $input_yaml > $output_yaml |
| 2057 | |
BjornMagnussonXA | 6f9c2b2 | 2021-06-11 16:31:40 +0200 | [diff] [blame] | 2058 | kubectl delete -f $output_yaml 1> /dev/null 2> /dev/null # Delete the previous terminated pod - if existing |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame] | 2059 | |
| 2060 | __kube_create_instance pod pvc-cleaner $input_yaml $output_yaml |
| 2061 | if [ $? -ne 0 ]; then |
| 2062 | echo $YELLOW" Could not clean pvc for app: $1 - persistent storage not clean - tests may not work" |
| 2063 | return 1 |
| 2064 | fi |
| 2065 | |
| 2066 | term_ts=$(($SECONDS+30)) |
| 2067 | while [ $term_ts -gt $SECONDS ]; do |
| 2068 | pod_status=$(kubectl get pod pvc-cleaner -n $PVC_CLEANER_NAMESPACE --no-headers -o custom-columns=":status.phase") |
| 2069 | if [ "$pod_status" == "Succeeded" ]; then |
| 2070 | return 0 |
| 2071 | fi |
| 2072 | done |
| 2073 | return 1 |
| 2074 | } |
| 2075 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2076 | # This function scales or deletes all resources for app selected by the testcase. |
| 2077 | # args: - |
| 2078 | # (Not for test scripts) |
| 2079 | __clean_kube() { |
| 2080 | echo -e $BOLD"Initialize kube services//pods/statefulsets/replicaset to initial state"$EBOLD |
| 2081 | |
| 2082 | # Scale prestarted or managed apps |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2083 | for imagename in $APP_SHORT_NAMES; do |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame] | 2084 | # A function name is created from the app short name |
| 2085 | # for example app short name 'RICMSIM' -> produce the function |
| 2086 | # name __RICSIM_kube_scale_zero or __RICSIM_kube_scale_zero_and_wait |
| 2087 | # This function is called and is expected to exist in the imported |
| 2088 | # file for the ricsim test functions |
| 2089 | # The resulting function impl shall scale the resources to 0 |
| 2090 | # For prestarted apps, the function waits until the resources are 0 |
| 2091 | # For included (not prestated) apps, the scaling is just ordered |
| 2092 | __check_prestarted_image $imagename |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2093 | if [ $? -eq 0 ]; then |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame] | 2094 | function_pointer="__"$imagename"_kube_scale_zero_and_wait" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2095 | echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0" |
| 2096 | $function_pointer |
BjornMagnussonXA | a549157 | 2021-05-04 09:21:24 +0200 | [diff] [blame] | 2097 | else |
| 2098 | __check_included_image $imagename |
| 2099 | if [ $? -eq 0 ]; then |
| 2100 | function_pointer="__"$imagename"_kube_scale_zero" |
| 2101 | echo -e " Scaling all kube resources for app $BOLD $imagename $EBOLD to 0" |
| 2102 | $function_pointer |
| 2103 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2104 | fi |
| 2105 | done |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2106 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2107 | # Delete managed apps |
| 2108 | for imagename in $APP_SHORT_NAMES; do |
| 2109 | __check_included_image $imagename |
| 2110 | if [ $? -eq 0 ]; then |
| 2111 | __check_prestarted_image $imagename |
| 2112 | if [ $? -ne 0 ]; then |
| 2113 | # A function name is created from the app short name |
| 2114 | # for example app short name 'RICMSIM' -> produce the function |
| 2115 | # name __RICSIM__kube_delete_all |
| 2116 | # This function is called and is expected to exist in the imported |
| 2117 | # file for the ricsim test functions |
| 2118 | # The resulting function impl shall delete all its resources |
| 2119 | function_pointer="__"$imagename"_kube_delete_all" |
| 2120 | echo -e " Deleting all kube resources for app $BOLD $imagename $EBOLD" |
| 2121 | $function_pointer |
| 2122 | fi |
| 2123 | fi |
| 2124 | done |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2125 | |
| 2126 | echo "" |
| 2127 | } |
| 2128 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2129 | # Function stop and remove all containers (docker) and services/deployments etc(kube) |
| 2130 | # args: - |
| 2131 | # Function for test script |
| 2132 | clean_environment() { |
| 2133 | if [ $RUNMODE == "KUBE" ]; then |
| 2134 | __clean_kube |
| 2135 | else |
| 2136 | __clean_containers |
| 2137 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | # 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 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2141 | # args: - |
| 2142 | # (Function for test scripts) |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2143 | auto_clean_environment() { |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2144 | echo |
| 2145 | if [ "$AUTO_CLEAN" == "auto" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2146 | echo -e $BOLD"Initiating automatic cleaning of environment"$EBOLD |
| 2147 | clean_environment |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2148 | fi |
| 2149 | } |
| 2150 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2151 | # Function to sleep a test case for a numner of seconds. Prints the optional text args as info |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 2152 | # args: <sleep-time-in-sec> [any-text-in-quotes-to-be-printed] |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2153 | # (Function for test scripts) |
| 2154 | sleep_wait() { |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2155 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2156 | echo -e $BOLD"INFO(${BASH_LINENO[0]}): "${FUNCNAME[0]}"," $@ $EBOLD |
| 2157 | if [ $# -lt 1 ]; then |
| 2158 | ((RES_CONF_FAIL++)) |
| 2159 | __print_err "need at least one arg, <sleep-time-in-sec> [any-text-to-printed]" $@ |
| 2160 | exit 1 |
| 2161 | fi |
| 2162 | #echo "---- Sleep for " $1 " seconds ---- "$2 |
| 2163 | start=$SECONDS |
| 2164 | duration=$((SECONDS-start)) |
| 2165 | while [ $duration -lt $1 ]; do |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2166 | echo -ne " Slept for ${duration} seconds${SAMELINE}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2167 | sleep 1 |
| 2168 | duration=$((SECONDS-start)) |
| 2169 | done |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2170 | echo -ne " Slept for ${duration} seconds${SAMELINE}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2171 | echo "" |
| 2172 | } |
| 2173 | |
| 2174 | # Print error info for the call in the parent script (test case). Arg: <error-message-to-print> |
| 2175 | # Not to be called from the test script itself. |
| 2176 | __print_err() { |
| 2177 | echo -e $RED ${FUNCNAME[1]} " "$1" " ${BASH_SOURCE[2]} " line" ${BASH_LINENO[1]} $ERED |
| 2178 | if [ $# -gt 1 ]; then |
| 2179 | echo -e $RED" Got: "${FUNCNAME[1]} ${@:2} $ERED |
| 2180 | fi |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 2181 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2184 | # Function to create the docker network for the test |
| 2185 | # Not to be called from the test script itself. |
| 2186 | __create_docker_network() { |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2187 | tmp=$(docker network ls --format={{.Name}} --filter name=$DOCKER_SIM_NWNAME) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2188 | if [ $? -ne 0 ]; then |
| 2189 | echo -e $RED" Could not check if docker network $DOCKER_SIM_NWNAME exists"$ERED |
| 2190 | return 1 |
| 2191 | fi |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2192 | if [ "$tmp" != $DOCKER_SIM_NWNAME ]; then |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 2193 | echo -e " Creating docker network:$BOLD $DOCKER_SIM_NWNAME $EBOLD" |
| 2194 | docker network create $DOCKER_SIM_NWNAME | indent2 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2195 | if [ $? -ne 0 ]; then |
| 2196 | echo -e $RED" Could not create docker network $DOCKER_SIM_NWNAME"$ERED |
| 2197 | return 1 |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 2198 | else |
| 2199 | echo -e "$GREEN Done$EGREEN" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2200 | fi |
| 2201 | else |
| 2202 | echo -e " Docker network $DOCKER_SIM_NWNAME already exists$GREEN OK $EGREEN" |
| 2203 | fi |
| 2204 | } |
| 2205 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2206 | # Function to start container with docker-compose and wait until all are in state running. |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2207 | # If the <docker-compose-file> is empty, the default 'docker-compose.yml' is assumed. |
| 2208 | #args: <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+ |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2209 | # (Not for test scripts) |
| 2210 | __start_container() { |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2211 | |
| 2212 | if [ $# -lt 5 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2213 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2214 | __print_err "need 5 or more args, <docker-compose-dir> <docker-compose-file> <docker-compose-arg>|NODOCKERARGS <count> <app-name>+" $@ |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2215 | exit 1 |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2216 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2217 | |
| 2218 | __create_docker_network |
| 2219 | |
| 2220 | curdir=$PWD |
| 2221 | cd $SIM_GROUP |
| 2222 | compose_dir=$1 |
| 2223 | cd $1 |
| 2224 | shift |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2225 | compose_file=$1 |
| 2226 | if [ -z "$compose_file" ]; then |
| 2227 | compose_file="docker-compose.yml" |
| 2228 | fi |
| 2229 | shift |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2230 | compose_args=$1 |
| 2231 | shift |
| 2232 | appcount=$1 |
| 2233 | shift |
| 2234 | |
| 2235 | if [ "$compose_args" == "NODOCKERARGS" ]; then |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2236 | docker-compose -f $compose_file up -d &> .dockererr |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2237 | if [ $? -ne 0 ]; then |
| 2238 | echo -e $RED"Problem to launch container(s) with docker-compose"$ERED |
| 2239 | cat .dockererr |
| 2240 | echo -e $RED"Stopping script...."$ERED |
| 2241 | exit 1 |
| 2242 | fi |
| 2243 | else |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2244 | docker-compose -f $compose_file up -d $compose_args &> .dockererr |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2245 | if [ $? -ne 0 ]; then |
| 2246 | echo -e $RED"Problem to launch container(s) with docker-compose"$ERED |
| 2247 | cat .dockererr |
| 2248 | echo -e $RED"Stopping script...."$ERED |
| 2249 | exit 1 |
| 2250 | fi |
| 2251 | fi |
| 2252 | |
| 2253 | cd $curdir |
| 2254 | |
| 2255 | appindex=0 |
| 2256 | while [ $appindex -lt $appcount ]; do |
| 2257 | appname=$1 |
| 2258 | shift |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 2259 | app_started=0 |
| 2260 | for i in {1..10}; do |
| 2261 | if [ "$(docker inspect --format '{{ .State.Running }}' $appname)" == "true" ]; then |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2262 | echo -e " Container $BOLD${appname}$EBOLD$GREEN running$EGREEN on$BOLD image $(docker inspect --format '{{ .Config.Image }}' ${appname}) $EBOLD" |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 2263 | app_started=1 |
| 2264 | break |
| 2265 | else |
| 2266 | sleep $i |
| 2267 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2268 | done |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 2269 | if [ $app_started -eq 0 ]; then |
| 2270 | ((RES_CONF_FAIL++)) |
| 2271 | echo "" |
| 2272 | echo -e $RED" Container $BOLD${appname}$EBOLD could not be started"$ERED |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 2273 | echo -e $RED" Stopping script..."$ERED |
| 2274 | exit 1 |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 2275 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2276 | let appindex=appindex+1 |
| 2277 | done |
| 2278 | return 0 |
| 2279 | } |
| 2280 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2281 | # Function to check if container/service is responding to http/https |
| 2282 | # args: <container-name>|<service-name> url |
| 2283 | # (Not for test scripts) |
| 2284 | __check_service_start() { |
| 2285 | |
| 2286 | if [ $# -ne 2 ]; then |
| 2287 | ((RES_CONF_FAIL++)) |
| 2288 | __print_err "need 2 args, <container-name>|<service-name> url" $@ |
| 2289 | return 1 |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2290 | fi |
| 2291 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2292 | if [ $RUNMODE == "KUBE" ]; then |
| 2293 | ENTITY="service/set/deployment" |
| 2294 | else |
| 2295 | ENTITY="container" |
| 2296 | fi |
| 2297 | appname=$1 |
| 2298 | url=$2 |
| 2299 | echo -ne " Container $BOLD${appname}$EBOLD starting${SAMELINE}" |
| 2300 | |
| 2301 | |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2302 | pa_st=false |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2303 | echo -ne " Waiting for ${ENTITY} ${appname} service status...${SAMELINE}" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 2304 | TSTART=$SECONDS |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2305 | loop_ctr=0 |
| 2306 | while (( $TSTART+600 > $SECONDS )); do |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 2307 | result="$(__do_curl -m 10 $url)" |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2308 | if [ $? -eq 0 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2309 | if [ ${#result} -gt 15 ]; then |
| 2310 | #If response is too long, truncate |
| 2311 | result="...response text too long, omitted" |
| 2312 | fi |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2313 | echo -ne " Waiting for {ENTITY} $BOLD${appname}$EBOLD service status on ${3}, result: $result${SAMELINE}" |
| 2314 | echo -ne " The ${ENTITY} $BOLD${appname}$EBOLD$GREEN is alive$EGREEN, responds to service status:$GREEN $result $EGREEN on ${url} after $(($SECONDS-$TSTART)) seconds" |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2315 | pa_st=true |
| 2316 | break |
| 2317 | else |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 2318 | TS_TMP=$SECONDS |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2319 | TS_OFFSET=$loop_ctr |
| 2320 | if (( $TS_OFFSET > 5 )); then |
| 2321 | TS_OFFSET=5 |
| 2322 | fi |
| 2323 | while [ $(($TS_TMP+$TS_OFFSET)) -gt $SECONDS ]; do |
| 2324 | echo -ne " Waiting for ${ENTITY} ${appname} service status on ${url}...$(($SECONDS-$TSTART)) seconds, retrying in $(($TS_TMP+$TS_OFFSET-$SECONDS)) seconds ${SAMELINE}" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 2325 | sleep 1 |
| 2326 | done |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2327 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2328 | let loop_ctr=loop_ctr+1 |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2329 | done |
| 2330 | |
| 2331 | if [ "$pa_st" = "false" ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2332 | ((RES_CONF_FAIL++)) |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2333 | echo -e $RED" The ${ENTITY} ${appname} did not respond to service status on ${url} in $(($SECONDS-$TSTART)) seconds"$ERED |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2334 | return 1 |
| 2335 | fi |
| 2336 | |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2337 | echo "" |
BjornMagnussonXA | 70e878f | 2020-05-11 14:11:30 +0200 | [diff] [blame] | 2338 | return 0 |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2339 | } |
| 2340 | |
BjornMagnussonXA | bf3700b | 2020-10-05 08:39:40 +0200 | [diff] [blame] | 2341 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2342 | ################# |
| 2343 | ### Log functions |
| 2344 | ################# |
| 2345 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2346 | __check_container_logs() { |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2347 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2348 | dispname=$1 |
| 2349 | appname=$2 |
| 2350 | logpath=$3 |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2351 | warning=$4 |
| 2352 | error=$5 |
| 2353 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2354 | echo -e $BOLD"Checking $dispname container $appname log ($logpath) for WARNINGs and ERRORs"$EBOLD |
| 2355 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2356 | if [ $RUNMODE == "KUBE" ]; then |
| 2357 | echo -e $YELLOW" Internal log for $dispname not checked in kube"$EYELLOW |
| 2358 | return |
| 2359 | fi |
| 2360 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2361 | #tmp=$(docker ps | grep $appname) |
| 2362 | tmp=$(docker ps -q --filter name=$appname) #get the container id |
| 2363 | if [ -z "$tmp" ]; then #Only check logs for running Policy Agent apps |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2364 | echo " "$dispname" is not running, no check made" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2365 | return |
| 2366 | fi |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2367 | foundentries="$(docker exec -t $tmp grep $warning $logpath | wc -l)" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2368 | if [ $? -ne 0 ];then |
| 2369 | echo " Problem to search $appname log $logpath" |
| 2370 | else |
| 2371 | if [ $foundentries -eq 0 ]; then |
| 2372 | echo " No WARN entries found in $appname log $logpath" |
| 2373 | else |
| 2374 | echo -e " Found \033[1m"$foundentries"\033[0m WARN entries in $appname log $logpath" |
| 2375 | fi |
| 2376 | fi |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2377 | foundentries="$(docker exec -t $tmp grep $error $logpath | wc -l)" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2378 | if [ $? -ne 0 ];then |
| 2379 | echo " Problem to search $appname log $logpath" |
| 2380 | else |
| 2381 | if [ $foundentries -eq 0 ]; then |
| 2382 | echo " No ERR entries found in $appname log $logpath" |
| 2383 | else |
| 2384 | echo -e $RED" Found \033[1m"$foundentries"\033[0m"$RED" ERR entries in $appname log $logpath"$ERED |
| 2385 | fi |
| 2386 | fi |
| 2387 | echo "" |
| 2388 | } |
| 2389 | |
| 2390 | # Store all container logs and other logs in the log dir for the script |
| 2391 | # Logs are stored with a prefix in case logs should be stored several times during a test |
| 2392 | # args: <logfile-prefix> |
| 2393 | # (Function for test scripts) |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2394 | store_logs() { |
| 2395 | if [ $# != 1 ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2396 | ((RES_CONF_FAIL++)) |
| 2397 | __print_err "need one arg, <file-prefix>" $@ |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2398 | exit 1 |
| 2399 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2400 | echo -e $BOLD"Storing all docker/kube container logs and other test logs in $TESTLOGS/$ATC using prefix: "$1$EBOLD |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2401 | |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 2402 | docker stats --no-stream > $TESTLOGS/$ATC/$1_docker_stats.log 2>&1 |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2403 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2404 | docker ps -a > $TESTLOGS/$ATC/$1_docker_ps.log 2>&1 |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2405 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2406 | cp .httplog_${ATC}.txt $TESTLOGS/$ATC/$1_httplog_${ATC}.txt 2>&1 |
| 2407 | |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2408 | if [ $RUNMODE == "DOCKER" ]; then |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2409 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2410 | # Store docker logs for all container |
| 2411 | for imagename in $APP_SHORT_NAMES; do |
| 2412 | __check_included_image $imagename |
| 2413 | if [ $? -eq 0 ]; then |
| 2414 | # A function name is created from the app short name |
| 2415 | # for example app short name 'RICMSIM' -> produce the function |
| 2416 | # name __RICSIM__store_docker_logs |
| 2417 | # This function is called and is expected to exist in the imported |
| 2418 | # file for the ricsim test functions |
| 2419 | # The resulting function impl shall store the docker logs for each container |
| 2420 | function_pointer="__"$imagename"_store_docker_logs" |
| 2421 | $function_pointer "$TESTLOGS/$ATC/" $1 |
| 2422 | fi |
| 2423 | done |
BjornMagnussonXA | e0b665e | 2021-01-08 22:19:18 +0100 | [diff] [blame] | 2424 | fi |
| 2425 | if [ $RUNMODE == "KUBE" ]; then |
| 2426 | namespaces=$(kubectl get namespaces -o jsonpath='{.items[?(@.metadata.name)].metadata.name}') |
| 2427 | for nsid in $namespaces; do |
| 2428 | pods=$(kubectl get pods -n $nsid -o jsonpath='{.items[?(@.metadata.labels.autotest)].metadata.name}') |
| 2429 | for podid in $pods; do |
| 2430 | kubectl logs -n $nsid $podid > $TESTLOGS/$ATC/$1_${podid}.log |
| 2431 | done |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2432 | done |
| 2433 | fi |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2434 | echo "" |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2435 | } |
| 2436 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2437 | ############### |
| 2438 | ## Generic curl |
| 2439 | ############### |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2440 | # Generic curl function, assumes all 200-codes are ok |
| 2441 | # args: <valid-curl-args-including full url> |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2442 | # returns: <returned response (without respose code)> or "<no-response-from-server>" or "<not found, <http-code>>"" |
| 2443 | # returns: The return code is 0 for ok and 1 for not ok |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2444 | __do_curl() { |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2445 | echo ${FUNCNAME[1]} "line: "${BASH_LINENO[1]} >> $HTTPLOG |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 2446 | proxyflag="" |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2447 | if [ $RUNMODE == "KUBE" ]; then |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 2448 | if [ ! -z "$KUBE_PROXY_PATH" ]; then |
BjornMagnussonXA | 674793d | 2021-05-06 19:49:17 +0200 | [diff] [blame] | 2449 | if [ $KUBE_PROXY_HTTPX == "http" ]; then |
| 2450 | proxyflag=" --proxy $KUBE_PROXY_PATH" |
| 2451 | else |
| 2452 | proxyflag=" --proxy-insecure --proxy $KUBE_PROXY_PATH" |
| 2453 | fi |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame] | 2454 | fi |
| 2455 | fi |
BjornMagnussonXA | 483ee33 | 2021-04-08 01:35:24 +0200 | [diff] [blame] | 2456 | curlString="curl -skw %{http_code} $proxyflag $@" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2457 | echo " CMD: $curlString" >> $HTTPLOG |
| 2458 | res=$($curlString) |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 2459 | retcode=$? |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2460 | echo " RESP: $res" >> $HTTPLOG |
BjornMagnussonXA | a69cd90 | 2021-04-22 23:46:10 +0200 | [diff] [blame] | 2461 | echo " RETCODE: $retcode" >> $HTTPLOG |
| 2462 | if [ $retcode -ne 0 ]; then |
| 2463 | echo "<no-response-from-server>" |
| 2464 | return 1 |
| 2465 | fi |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2466 | http_code="${res:${#res}-3}" |
| 2467 | if [ ${#res} -eq 3 ]; then |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2468 | if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then |
| 2469 | echo "<no-response-from-server>" |
| 2470 | return 1 |
| 2471 | else |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2472 | return 0 |
| 2473 | fi |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2474 | else |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2475 | if [ $http_code -lt 200 ] || [ $http_code -gt 299 ]; then |
YongchaoWu | 9a84f51 | 2019-12-16 22:54:11 +0100 | [diff] [blame] | 2476 | echo "<not found, resp:${http_code}>" |
| 2477 | return 1 |
| 2478 | fi |
| 2479 | if [ $# -eq 2 ]; then |
| 2480 | echo "${res:0:${#res}-3}" | xargs |
| 2481 | else |
| 2482 | echo "${res:0:${#res}-3}" |
| 2483 | fi |
| 2484 | |
| 2485 | return 0 |
| 2486 | fi |
| 2487 | } |
| 2488 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2489 | ####################################### |
| 2490 | ### Basic helper function for test cases |
| 2491 | ####################################### |
| 2492 | |
| 2493 | # Test a simulator container variable value towards target value using an condition operator with an optional timeout. |
| 2494 | # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> - This test is done |
| 2495 | # immediately and sets pass or fail depending on the result of comparing variable and target using the operator. |
| 2496 | # Arg: <simulator-name> <host> <variable-name> <condition-operator> <target-value> <timeout> - This test waits up to the timeout |
| 2497 | # before setting pass or fail depending on the result of comparing variable and target using the operator. |
| 2498 | # 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. |
| 2499 | # Not to be called from test script. |
| 2500 | |
| 2501 | __var_test() { |
| 2502 | checkjsonarraycount=0 |
BjornMagnussonXA | 3cc0b58 | 2021-08-30 10:46:41 +0200 | [diff] [blame] | 2503 | TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2504 | if [ $# -eq 6 ]; then |
| 2505 | if [[ $3 == "json:"* ]]; then |
| 2506 | checkjsonarraycount=1 |
| 2507 | fi |
| 2508 | |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 2509 | echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds"$EBOLD |
BjornMagnussonXA | 3cc0b58 | 2021-08-30 10:46:41 +0200 | [diff] [blame] | 2510 | echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5} within ${6} seconds" >> $HTTPLOG |
| 2511 | |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2512 | ((RES_TEST++)) |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 2513 | ((TEST_SEQUENCE_NR++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2514 | start=$SECONDS |
| 2515 | ctr=0 |
| 2516 | for (( ; ; )); do |
| 2517 | if [ $checkjsonarraycount -eq 0 ]; then |
| 2518 | result="$(__do_curl $2$3)" |
| 2519 | retcode=$? |
| 2520 | result=${result//[[:blank:]]/} #Strip blanks |
| 2521 | else |
| 2522 | path=${3:5} |
| 2523 | result="$(__do_curl $2$path)" |
| 2524 | retcode=$? |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2525 | echo "$result" > ./tmp/.tmp.curl.json |
| 2526 | result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json") |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2527 | fi |
| 2528 | duration=$((SECONDS-start)) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2529 | echo -ne " Result=${result} after ${duration} seconds${SAMELINE}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2530 | let ctr=ctr+1 |
| 2531 | if [ $retcode -ne 0 ]; then |
| 2532 | if [ $duration -gt $6 ]; then |
| 2533 | ((RES_FAIL++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2534 | echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2535 | __print_current_stats |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 2536 | __check_stop_at_error |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2537 | return |
| 2538 | fi |
| 2539 | elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then |
| 2540 | ((RES_PASS++)) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2541 | echo -e " Result=${result} after ${duration} seconds${SAMELINE}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2542 | echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2543 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2544 | return |
| 2545 | elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then |
| 2546 | ((RES_PASS++)) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2547 | echo -e " Result=${result} after ${duration} seconds${SAMELINE}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2548 | echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2549 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2550 | return |
| 2551 | elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then |
| 2552 | ((RES_PASS++)) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2553 | echo -e " Result=${result} after ${duration} seconds${SAMELINE}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2554 | echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2555 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2556 | return |
| 2557 | elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then |
| 2558 | ((RES_PASS++)) |
BjornMagnussonXA | 72667f1 | 2020-04-24 09:20:18 +0200 | [diff] [blame] | 2559 | echo -e " Result=${result} after ${duration} seconds${SAMELINE}" |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2560 | echo -e $GREEN" PASS${EGREEN} - Result=${result} after ${duration} seconds" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2561 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2562 | return |
| 2563 | else |
| 2564 | if [ $duration -gt $6 ]; then |
| 2565 | ((RES_FAIL++)) |
| 2566 | echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached in ${6} seconds, result = ${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2567 | __print_current_stats |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 2568 | __check_stop_at_error |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2569 | return |
| 2570 | fi |
| 2571 | fi |
| 2572 | sleep 1 |
| 2573 | done |
| 2574 | elif [ $# -eq 5 ]; then |
| 2575 | if [[ $3 == "json:"* ]]; then |
| 2576 | checkjsonarraycount=1 |
| 2577 | fi |
| 2578 | |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 2579 | echo -e $BOLD"TEST $TEST_SEQUENCE_NR (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}"$EBOLD |
BjornMagnussonXA | 3cc0b58 | 2021-08-30 10:46:41 +0200 | [diff] [blame] | 2580 | echo "TEST $TEST_SEQUENCE_NR - ${TIMESTAMP}: (${BASH_LINENO[1]}): ${1}, ${3} ${4} ${5}" >> $HTTPLOG |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2581 | ((RES_TEST++)) |
BjornMagnussonXA | 7b36db6 | 2020-11-23 10:57:57 +0100 | [diff] [blame] | 2582 | ((TEST_SEQUENCE_NR++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2583 | if [ $checkjsonarraycount -eq 0 ]; then |
| 2584 | result="$(__do_curl $2$3)" |
| 2585 | retcode=$? |
| 2586 | result=${result//[[:blank:]]/} #Strip blanks |
| 2587 | else |
| 2588 | path=${3:5} |
| 2589 | result="$(__do_curl $2$path)" |
| 2590 | retcode=$? |
BjornMagnussonXA | f38e1e8 | 2020-10-11 23:05:02 +0200 | [diff] [blame] | 2591 | echo "$result" > ./tmp/.tmp.curl.json |
| 2592 | result=$(python3 ../common/count_json_elements.py "./tmp/.tmp.curl.json") |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2593 | fi |
| 2594 | if [ $retcode -ne 0 ]; then |
| 2595 | ((RES_FAIL++)) |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2596 | echo -e $RED" FAIL ${ERED}- ${3} ${4} ${5} not reached, result = ${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2597 | __print_current_stats |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 2598 | __check_stop_at_error |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2599 | elif [ $4 = "=" ] && [ "$result" -eq $5 ]; then |
| 2600 | ((RES_PASS++)) |
| 2601 | echo -e $GREEN" PASS${EGREEN} - Result=${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2602 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2603 | elif [ $4 = ">" ] && [ "$result" -gt $5 ]; then |
| 2604 | ((RES_PASS++)) |
| 2605 | echo -e $GREEN" PASS${EGREEN} - Result=${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2606 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2607 | elif [ $4 = "<" ] && [ "$result" -lt $5 ]; then |
| 2608 | ((RES_PASS++)) |
| 2609 | echo -e $GREEN" PASS${EGREEN} - Result=${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2610 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2611 | elif [ $4 = "contain_str" ] && [[ $result =~ $5 ]]; then |
| 2612 | ((RES_PASS++)) |
| 2613 | echo -e $GREEN" PASS${EGREEN} - Result=${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2614 | __print_current_stats |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2615 | else |
| 2616 | ((RES_FAIL++)) |
| 2617 | echo -e $RED" FAIL${ERED} - ${3} ${4} ${5} not reached, result = ${result}" |
BjornMagnussonXA | fec823b | 2021-08-03 14:14:05 +0200 | [diff] [blame] | 2618 | __print_current_stats |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 2619 | __check_stop_at_error |
BjornMagnussonXA | 80a9200 | 2020-03-19 14:31:06 +0100 | [diff] [blame] | 2620 | fi |
| 2621 | else |
| 2622 | echo "Wrong args to __var_test, needs five or six args: <simulator-name> <host> <variable-name> <condition-operator> <target-value> [ <timeout> ]" |
| 2623 | echo "Got:" $@ |
| 2624 | exit 1 |
| 2625 | fi |
| 2626 | } |