Implement and use run_until_success_or_timeout

This commit is contained in:
Martin Schulze 2020-09-20 01:10:05 +02:00
parent a477040abb
commit 40dd3ae985
2 changed files with 23 additions and 10 deletions

View file

@ -51,11 +51,7 @@ function teardown_file() {
run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map
assert_output '' assert_output ''
run ./setup.sh -c mail_with_relays email add user0@domainzero.tld password123 run ./setup.sh -c mail_with_relays email add user0@domainzero.tld password123
for i in {1..10}; do run_until_success_or_timeout 10 docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map
sleep 1
run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map
[[ $status == 0 ]] && break
done
assert_output -e '^@domainzero.tld\s+\[default.relay.com\]:2525$' 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 run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map
assert_output '' assert_output ''
run ./setup.sh -c mail_with_relays alias add user2@domain2.tld user2@domaintwo.tld run ./setup.sh -c mail_with_relays alias add user2@domain2.tld user2@domaintwo.tld
for i in {1..10}; do run_until_success_or_timeout 10 docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map
sleep 1
run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map
[[ $status == 0 ]] && break
done
assert_output -e '^@domain2.tld\s+\[default.relay.com\]:2525$' assert_output -e '^@domain2.tld\s+\[default.relay.com\]:2525$'
} }

View file

@ -37,6 +37,27 @@ function repeat_until_success_or_timeout {
done 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 $1 timeout
# @param $2 container name # @param $2 container name
# @param ... test command for container # @param ... test command for container