From ed63a6f90acb8afa1f682bdb218534e94713f96b Mon Sep 17 00:00:00 2001 From: Brennan Kinney <5098581+polarathene@users.noreply.github.com> Date: Mon, 30 Jan 2023 01:29:25 +1300 Subject: [PATCH] tests(refactor): Amavis `spam_junk_folder.bats` + `spam_bounced.bats` (#3036) * tests(fix): `spam_junk_folder.bats` Wait on Amavis port to be ready Postfix can potentially be ready before Amavis is. This caused test failures as mail was sent before Amavis was ready to process it. Both test cases shared the same test logic, except for the expected location to deliver the spam to. Extracted into a shared test method, and moved the port conditions into there. * tests(chore): `spam_junk_folder.bats` minor revisions Test case descriptions, container names and test prefix are now more descriptive of what is under test here (an ENV for Amavis). * tests(chore): Move Amavis bounce test into `spam_junk_folder.bats` These two tests seem to be related to the same feature. Grouping them into a single test file instead. * tests(refactor): Split shared method into smaller methods Now it can be better shared with the bounce test case. * tests(chore): Shift test cases to match their CONTAINER_NAME order No changes to code, just cut + paste of the `CONTAINER3_NAME` test case to shift it to the last test case position. --- .../set1/spam_virus/spam_bounced.bats | 36 ------- .../set1/spam_virus/spam_junk_folder.bats | 102 +++++++++++------- 2 files changed, 65 insertions(+), 73 deletions(-) delete mode 100644 test/tests/parallel/set1/spam_virus/spam_bounced.bats diff --git a/test/tests/parallel/set1/spam_virus/spam_bounced.bats b/test/tests/parallel/set1/spam_virus/spam_bounced.bats deleted file mode 100644 index a46e6a23..00000000 --- a/test/tests/parallel/set1/spam_virus/spam_bounced.bats +++ /dev/null @@ -1,36 +0,0 @@ -load "${REPOSITORY_ROOT}/test/helper/setup" -load "${REPOSITORY_ROOT}/test/helper/common" - -BATS_TEST_NAME_PREFIX='[Spam] (bounced) ' -CONTAINER_NAME='dms-test_spam-bounced' - -function setup_file() { - _init_with_defaults - - local CUSTOM_SETUP_ARGUMENTS=( - --env ENABLE_AMAVIS=1 - --env ENABLE_SPAMASSASSIN=1 - --env PERMIT_DOCKER=container - --env SPAMASSASSIN_SPAM_TO_INBOX=0 - ) - - _common_container_setup 'CUSTOM_SETUP_ARGUMENTS' - _wait_for_smtp_port_in_container_to_respond -} - -function teardown_file() { _default_teardown ; } - -# Test case -# --------- -# When SPAMASSASSIN_SPAM_TO_INBOX=0, spam messages must be bounced (rejected). -# SPAMASSASSIN_SPAM_TO_INBOX=1 is covered in `mail_spam_junk_folder.bats`. -# Original test PR: https://github.com/docker-mailserver/docker-mailserver/pull/1485 -@test "spam message is bounced (rejected)" { - # send a spam message - _run_in_container /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt" - assert_success - - # message will be added to a queue with varying delay until amavis receives it - run _repeat_until_success_or_timeout 60 sh -c "docker logs ${CONTAINER_NAME} | grep 'Blocked SPAM {NoBounceInbound,Quarantined}'" - assert_success -} diff --git a/test/tests/parallel/set1/spam_virus/spam_junk_folder.bats b/test/tests/parallel/set1/spam_virus/spam_junk_folder.bats index f586d7b2..96f549b4 100644 --- a/test/tests/parallel/set1/spam_virus/spam_junk_folder.bats +++ b/test/tests/parallel/set1/spam_virus/spam_junk_folder.bats @@ -1,67 +1,95 @@ load "${REPOSITORY_ROOT}/test/helper/setup" load "${REPOSITORY_ROOT}/test/helper/common" -BATS_TEST_NAME_PREFIX='[Spam] (junk folder) ' -CONTAINER1_NAME='dms-test_spam-junk-folder_1' -CONTAINER2_NAME='dms-test_spam-junk-folder_2' +# Tests originally contributed in PR: https://github.com/docker-mailserver/docker-mailserver/pull/1485 +# That introduced both ENV: SPAMASSASSIN_SPAM_TO_INBOX and MOVE_SPAM_TO_JUNK + +BATS_TEST_NAME_PREFIX='[Spam - Amavis] ENV SPAMASSASSIN_SPAM_TO_INBOX ' +CONTAINER1_NAME='dms-test_spam-amavis_bounced' +CONTAINER2_NAME='dms-test_spam-amavis_env-move-spam-to-junk-0' +CONTAINER3_NAME='dms-test_spam-amavis_env-move-spam-to-junk-1' function teardown() { _default_teardown ; } -# Test case -# --------- -# When SPAMASSASSIN_SPAM_TO_INBOX=1, spam messages must be delivered -# and eventually (MOVE_SPAM_TO_JUNK=1) moved to the Junk folder. - -@test "(Amavis) spam message delivered & moved to Junk folder" { +@test "(disabled) spam message should be bounced (rejected)" { export CONTAINER_NAME=${CONTAINER1_NAME} + local CUSTOM_SETUP_ARGUMENTS=( --env ENABLE_AMAVIS=1 --env ENABLE_SPAMASSASSIN=1 - --env MOVE_SPAM_TO_JUNK=1 + --env SPAMASSASSIN_SPAM_TO_INBOX=0 --env PERMIT_DOCKER=container - --env SA_SPAM_SUBJECT="SPAM: " - --env SPAMASSASSIN_SPAM_TO_INBOX=1 ) _init_with_defaults _common_container_setup 'CUSTOM_SETUP_ARGUMENTS' - _wait_for_smtp_port_in_container - # send a spam message - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt" - assert_success - - # message will be added to a queue with varying delay until amavis receives it - run _repeat_until_success_or_timeout 60 bash -c "docker logs ${CONTAINER_NAME} | grep 'Passed SPAM {RelayedTaggedInbound,Quarantined}'" - assert_success - - # spam moved to Junk folder - run _repeat_until_success_or_timeout 20 bash -c "docker exec ${CONTAINER_NAME} sh -c 'grep \"Subject: SPAM: \" /var/mail/localhost.localdomain/user1/.Junk/new/ -R'" - assert_success + _should_send_spam_message + _should_be_received_by_amavis 'Blocked SPAM {NoBounceInbound,Quarantined}' } -@test "(Amavis) spam message delivered to INBOX" { +@test "(enabled + MOVE_SPAM_TO_JUNK=0) should deliver spam message into INBOX" { export CONTAINER_NAME=${CONTAINER2_NAME} + local CUSTOM_SETUP_ARGUMENTS=( --env ENABLE_AMAVIS=1 --env ENABLE_SPAMASSASSIN=1 + --env SA_SPAM_SUBJECT="SPAM: " + --env SPAMASSASSIN_SPAM_TO_INBOX=1 --env MOVE_SPAM_TO_JUNK=0 --env PERMIT_DOCKER=container - --env SA_SPAM_SUBJECT="SPAM: " - --env SPAMASSASSIN_SPAM_TO_INBOX=1 ) _init_with_defaults _common_container_setup 'CUSTOM_SETUP_ARGUMENTS' + + _should_send_spam_message + _should_be_received_by_amavis 'Passed SPAM {RelayedTaggedInbound,Quarantined}' + + # Should move delivered spam to INBOX + _should_receive_spam_at '/var/mail/localhost.localdomain/user1/new/' +} + +@test "(enabled + MOVE_SPAM_TO_JUNK=1) should deliver spam message into Junk folder" { + export CONTAINER_NAME=${CONTAINER3_NAME} + + local CUSTOM_SETUP_ARGUMENTS=( + --env ENABLE_AMAVIS=1 + --env ENABLE_SPAMASSASSIN=1 + --env SA_SPAM_SUBJECT="SPAM: " + --env SPAMASSASSIN_SPAM_TO_INBOX=1 + --env MOVE_SPAM_TO_JUNK=1 + --env PERMIT_DOCKER=container + ) + _init_with_defaults + _common_container_setup 'CUSTOM_SETUP_ARGUMENTS' + + _should_send_spam_message + _should_be_received_by_amavis 'Passed SPAM {RelayedTaggedInbound,Quarantined}' + + # Should move delivered spam to Junk folder + _should_receive_spam_at '/var/mail/localhost.localdomain/user1/.Junk/new/' +} + +function _should_send_spam_message() { _wait_for_smtp_port_in_container + # Port 10024 (Amavis) + _wait_for_tcp_port_in_container 10024 - # send a spam message - _run_in_container /bin/bash -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt" - assert_success - - # message will be added to a queue with varying delay until amavis receives it - run _repeat_until_success_or_timeout 60 bash -c "docker logs ${CONTAINER_NAME} | grep 'Passed SPAM {RelayedTaggedInbound,Quarantined}'" - assert_success - - # spam moved to INBOX - run _repeat_until_success_or_timeout 20 bash -c "docker exec ${CONTAINER_NAME} sh -c 'grep \"Subject: SPAM: \" /var/mail/localhost.localdomain/user1/new/ -R'" + _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt" + assert_success +} + +function _should_be_received_by_amavis() { + local MATCH_CONTENT=${1} + + # message will be added to a queue with varying delay until amavis receives it + _run_in_container_bash "timeout 60 tail -F /var/log/mail/mail.log | grep --max-count 1 '${MATCH_CONTENT}'" + assert_success +} + +function _should_receive_spam_at() { + local MAIL_DIR=${1} + + # spam moved into MAIL_DIR + _repeat_in_container_until_success_or_timeout 20 "${CONTAINER_NAME}" grep -R 'Subject: SPAM: ' "${MAIL_DIR}" assert_success }