diff --git a/test/test_helper.bats b/test/test_helper.bats index 82ff8a0f..07577bba 100644 --- a/test/test_helper.bats +++ b/test/test_helper.bats @@ -95,3 +95,28 @@ load 'test_helper/common' [[ ${SECONDS} -lt 5 ]] assert_success } + +@test "wait_for_empty_mail_queue_in_container" { + # variable not local to make visible to teardown + CONTAINER_NAME="$(docker run -d --rm \ + -v "$(duplicate_config_for_container .)":/tmp/docker-mailserver \ + -v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \ + -h mail.my-domain.com -t "${NAME}")" + + teardown() { docker rm -f "${CONTAINER_NAME}"; } + + wait_for_smtp_port_in_container "${CONTAINER_NAME}" || docker logs "${CONTAINER_NAME}" + + SECONDS=0 + # no mails -> should return immediately + TEST_TIMEOUT_IN_SECONDS=5 wait_for_empty_mail_queue_in_container "${CONTAINER_NAME}" + [[ $SECONDS -lt 5 ]] + + # fill the queue with a message + docker exec "${CONTAINER_NAME}" /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-virus.txt" + # the first shot should fail + ! TEST_TIMEOUT_IN_SECONDS=0 wait_for_empty_mail_queue_in_container "${CONTAINER_NAME}" + + # now give it some time to clear the queue + wait_for_empty_mail_queue_in_container "${CONTAINER_NAME}" +} diff --git a/test/test_helper/common.bash b/test/test_helper/common.bash index b9e82542..a6c64cfe 100644 --- a/test/test_helper/common.bash +++ b/test/test_helper/common.bash @@ -183,3 +183,9 @@ function wait_for_changes_to_be_detected_in_container() { # shellcheck disable=SC2016 repeat_in_container_until_success_or_timeout "${TIMEOUT}" "${CONTAINER_NAME}" bash -c 'source /usr/local/bin/helper_functions.sh; cmp --silent -- <(_monitored_files_checksums) "${CHKSUM_FILE}" >/dev/null' } + +function wait_for_empty_mail_queue_in_container() { + local CONTAINER_NAME="${1}" + local TIMEOUT=${TEST_TIMEOUT_IN_SECONDS} + repeat_in_container_until_success_or_timeout "${TIMEOUT}" "${CONTAINER_NAME}" bash -c '[[ $(mailq) == *"Mail queue is empty"* ]]' +} diff --git a/test/tests.bats b/test/tests.bats index 528fcfc0..e1181873 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -66,8 +66,8 @@ setup_file() { docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/sieve-pipe.txt" docker exec mail /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/non-existing-user.txt" docker exec mail /bin/sh -c "sendmail root < /tmp/docker-mailserver-test/email-templates/root-email.txt" - # wait for mails to be analyzed - #sleep 80 + + wait_for_empty_mail_queue_in_container mail } teardown() {