From 1c8a160621b09ff88faea5385e9f11cd31b9c1da Mon Sep 17 00:00:00 2001 From: Brennan Kinney <5098581+polarathene@users.noreply.github.com> Date: Sun, 19 Feb 2023 03:51:28 +1300 Subject: [PATCH] chore: Remove delay starting the change detection service (#3064) * chore: Only replace `CHKSUM_FILE` when a change has been processed * chore: Change Detection service should be the last daemon started * chore: Remove 10 second startup delay for change detector There should be no concern with conflicts as any writes should have already been done by the time this daemon service is started. * tests(fix): `smtp_delivery.bats` must wait for Amavis The change event for adding a user can be processed much sooner now, which means Amavis may not yet be ready. Added extra condition to wait on at least the Amavis port being reachable, and some failure asserts with the mail queue to better catch / debug when this problem occurs. * chore: Add some minor delay to avoid Amavis failing to connect --- target/scripts/check-for-changes.sh | 9 +++----- target/scripts/start-mailserver.sh | 2 +- test/helper/common.bash | 2 +- .../parallel/set3/mta/smtp_delivery.bats | 22 +++++++++++++++---- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/target/scripts/check-for-changes.sh b/target/scripts/check-for-changes.sh index 62abd3aa..f23a2cb0 100755 --- a/target/scripts/check-for-changes.sh +++ b/target/scripts/check-for-changes.sh @@ -29,9 +29,6 @@ fi _log_with_date 'trace' "Using postmaster address '${POSTMASTER_ADDRESS}'" -# Change detection delayed during startup to avoid conflicting writes -sleep 10 - _log_with_date 'debug' "Changedetector is ready" function _check_for_changes @@ -64,10 +61,10 @@ function _check_for_changes _remove_lock _log_with_date 'debug' 'Completed handling of detected change' - fi - # mark changes as applied - mv "${CHKSUM_FILE}.new" "${CHKSUM_FILE}" + # mark changes as applied + mv "${CHKSUM_FILE}.new" "${CHKSUM_FILE}" + fi } function _get_changed_files diff --git a/target/scripts/start-mailserver.sh b/target/scripts/start-mailserver.sh index 170b2904..4c709f05 100755 --- a/target/scripts/start-mailserver.sh +++ b/target/scripts/start-mailserver.sh @@ -185,8 +185,8 @@ function _register_functions [[ ${ENABLE_FAIL2BAN} -eq 1 ]] && _register_start_daemon '_start_daemon_fail2ban' [[ ${ENABLE_FETCHMAIL} -eq 1 ]] && _register_start_daemon '_start_daemon_fetchmail' [[ ${ENABLE_CLAMAV} -eq 1 ]] && _register_start_daemon '_start_daemon_clamav' - [[ ${ACCOUNT_PROVISIONER} == 'FILE' ]] && _register_start_daemon '_start_daemon_changedetector' [[ ${ENABLE_AMAVIS} -eq 1 ]] && _register_start_daemon '_start_daemon_amavis' + [[ ${ACCOUNT_PROVISIONER} == 'FILE' ]] && _register_start_daemon '_start_daemon_changedetector' } function _register_start_daemon diff --git a/test/helper/common.bash b/test/helper/common.bash index dc6546db..09e0b9f1 100644 --- a/test/helper/common.bash +++ b/test/helper/common.bash @@ -261,7 +261,7 @@ function _wait_for_smtp_port_in_container() { _wait_for_tcp_port_in_container 25 } -# Wait until the SMPT port (25) can respond. +# Wait until the SMTP port (25) can respond. # # @param ${1} = name of the container [OPTIONAL] function _wait_for_smtp_port_in_container_to_respond() { diff --git a/test/tests/parallel/set3/mta/smtp_delivery.bats b/test/tests/parallel/set3/mta/smtp_delivery.bats index 50384a2c..4251fdb9 100644 --- a/test/tests/parallel/set3/mta/smtp_delivery.bats +++ b/test/tests/parallel/set3/mta/smtp_delivery.bats @@ -5,6 +5,8 @@ load "${REPOSITORY_ROOT}/test/helper/setup" BATS_TEST_NAME_PREFIX='[SMTP] (delivery) ' CONTAINER_NAME='dms-test_smtp-delivery' +function teardown_file() { _default_teardown ; } + function setup_file() { _init_with_defaults @@ -49,7 +51,14 @@ function setup_file() { assert_success _wait_until_change_detection_event_completes - _wait_for_smtp_port_in_container + # Even if the Amavis port is reachable at this point, it may still refuse connections? + _wait_for_tcp_port_in_container 10024 + _wait_for_smtp_port_in_container_to_respond + + # Amavis may still not be ready to receive mail, sleep a little to avoid connection failures: + sleep 1 + + ### Send mail to queue for delivery ### # TODO: Move to clamav tests (For use when ClamAV is enabled): # _repeat_in_container_until_success_or_timeout 60 "${CONTAINER_NAME}" test -e /var/run/clamav/clamd.ctl @@ -81,11 +90,16 @@ function setup_file() { _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/sieve-spam-folder.txt' _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/sieve-pipe.txt' _run_in_container_bash 'sendmail root < /tmp/docker-mailserver-test/email-templates/root-email.txt' - - _wait_for_empty_mail_queue_in_container } -function teardown_file() { _default_teardown ; } +@test "should succeed at emptying mail queue" { + # Try catch errors preventing emptying the queue ahead of waiting: + _run_in_container mailq + # Amavis (Port 10024) may not have been ready when first mail was sent: + refute_output --partial 'Connection refused' + refute_output --partial '(unknown mail transport error)' + _wait_for_empty_mail_queue_in_container +} @test "should successfully authenticate with good password (plain)" { _run_in_container_bash 'nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-plain.txt'