diff --git a/test/mail_with_postgrey.bats b/test/mail_with_postgrey.bats index 71149a07..03248295 100644 --- a/test/mail_with_postgrey.bats +++ b/test/mail_with_postgrey.bats @@ -1,15 +1,11 @@ load 'test_helper/common' function setup() { - if [ "$BATS_TEST_NUMBER" -eq 1 ]; then - setup_file - fi + run_setup_file_if_necessary } function teardown() { - if [ "$BATS_TEST_NUMBER" -eq ${#BATS_TEST_NAMES[@]} ]; then - teardown_file - fi + run_teardown_file_if_necessary } function setup_file() { @@ -31,6 +27,10 @@ function teardown_file() { docker rm -f mail_with_postgrey } +@test "first" { + # this test must come first to reliably identify when to run setup_file +} + @test "checking postgrey: /etc/postfix/main.cf correctly edited" { run docker exec mail_with_postgrey /bin/bash -c "grep 'bl.spamcop.net, check_policy_service inet:127.0.0.1:10023' /etc/postfix/main.cf | wc -l" assert_success @@ -94,4 +94,8 @@ function teardown_file() { run docker exec mail_with_postgrey /bin/sh -c "grep -i 'action=pass, reason=recipient whitelist' /var/log/mail/mail.log | wc -l" assert_success assert_output 1 +} + +@test "last" { + # this test is only there to reliably mark the end for the teardown_file } \ No newline at end of file diff --git a/test/mail_with_relays.bats b/test/mail_with_relays.bats index 58447859..fa2ea0ef 100644 --- a/test/mail_with_relays.bats +++ b/test/mail_with_relays.bats @@ -1,15 +1,11 @@ load 'test_helper/common' function setup() { - if [ "$BATS_TEST_NUMBER" -eq 1 ]; then - setup_file - fi + run_setup_file_if_necessary } function teardown() { - if [ "$BATS_TEST_NUMBER" -eq ${#BATS_TEST_NAMES[@]} ]; then - teardown_file - fi + run_teardown_file_if_necessary } function setup_file() { @@ -31,6 +27,10 @@ function teardown_file() { docker rm -f mail_with_relays } +@test "first" { + # this test must come first to reliably identify when to run setup_file +} + @test "checking relay hosts: default mapping is added from env vars" { run docker exec mail_with_relays /bin/sh -c 'cat /etc/postfix/relayhost_map | grep -e "^@domainone.tld\s\+\[default.relay.com\]:2525" | wc -l | grep 1' assert_success @@ -54,4 +54,8 @@ function teardown_file() { @test "checking relay hosts: default auth entry is added" { run docker exec mail_with_relays /bin/sh -c 'cat /etc/postfix/sasl_passwd | grep -e "^\[default.relay.com\]:2525\s\+smtp_user:smtp_password" | wc -l | grep 1' assert_success +} + +@test "last" { + # this test is only there to reliably mark the end for the teardown_file } \ No newline at end of file diff --git a/test/test_helper/common.bash b/test/test_helper/common.bash index 3ee7c7d4..5e119f99 100644 --- a/test/test_helper/common.bash +++ b/test/test_helper/common.bash @@ -4,7 +4,7 @@ load 'test_helper/bats-assert/load' NAME=tvial/docker-mailserver:testing # default timeout is 60 seconds -TEST_TIMEOUT_IN_SECONDS=${TIMEOUT-60} +TEST_TIMEOUT_IN_SECONDS=${TEST_TIMEOUT_IN_SECONDS-60} function repeat_until_success_or_timeout { if ![[ "$1" ~= '^[0-9]+$' ]]; then @@ -32,4 +32,18 @@ function wait_for_smtp_port_in_container() { # @param $1 name of the postfix container function wait_for_finished_setup_in_container() { repeat_until_success_or_timeout $TEST_TIMEOUT_IN_SECONDS sh -c "docker logs $1 | grep 'Starting mail server'" +} + +# use in setup() in conjunction with a `@test "first" {}` to trigger setup_file reliably +function run_setup_file_if_necessary() { + if [ "$BATS_TEST_NAME" == 'test_first' ]; then + setup_file + fi +} + +# use in teardown() in conjunction with a `@test "last" {}` to trigger teardown_file reliably +function run_teardown_file_if_necessary() { + if [ "$BATS_TEST_NAME" == 'test_last' ]; then + teardown_file + fi } \ No newline at end of file