diff --git a/test/mail_with_relays.bats b/test/mail_with_relays.bats index 5ae3e82d..07afab7e 100644 --- a/test/mail_with_relays.bats +++ b/test/mail_with_relays.bats @@ -51,11 +51,7 @@ function teardown_file() { run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map assert_output '' run ./setup.sh -c mail_with_relays email add user0@domainzero.tld password123 - for i in {1..10}; do - sleep 1 - run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map - [[ $status == 0 ]] && break - done + run_until_success_or_timeout 10 docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map assert_output -e '^@domainzero.tld\s+\[default.relay.com\]:2525$' } @@ -63,11 +59,7 @@ function teardown_file() { run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map assert_output '' run ./setup.sh -c mail_with_relays alias add user2@domain2.tld user2@domaintwo.tld - for i in {1..10}; do - sleep 1 - run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map - [[ $status == 0 ]] && break - done + run_until_success_or_timeout 10 docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map assert_output -e '^@domain2.tld\s+\[default.relay.com\]:2525$' } diff --git a/test/test_helper/common.bash b/test/test_helper/common.bash index 428ddecd..5982cb94 100644 --- a/test/test_helper/common.bash +++ b/test/test_helper/common.bash @@ -37,6 +37,27 @@ function repeat_until_success_or_timeout { done } +# like repeat_until_success_or_timeout but with wrapping the command to run into `run` for later bats consumption +# @param $1 timeout +# @param ... test command to run +function run_until_success_or_timeout { + if ! [[ "$1" =~ ^[0-9]+$ ]]; then + echo "First parameter for timeout must be an integer, recieved \"$1\"" + return 1 + fi + TIMEOUT=$1 + STARTTIME=$SECONDS + shift 1 + until run "$@" && [[ $status -eq 0 ]] + do + sleep 1 + if [[ $(($SECONDS - $STARTTIME )) -gt $TIMEOUT ]]; then + echo "Timed out on command: $@" >&2 + return 1 + fi + done +} + # @param $1 timeout # @param $2 container name # @param ... test command for container