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