From 35692a9111ba8b340354d8667e172ca5eb961253 Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Date: Wed, 22 Feb 2023 10:26:04 +0100 Subject: [PATCH] ci: refactored `sedfile` & used `_send_mail` where possible (#3103) Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com> --- test/helper/sending.bash | 4 +- .../parallel/set1/dovecot/dovecot_sieve.bats | 4 +- .../set1/dovecot/special_use_folders.bats | 8 +-- .../parallel/set1/spam_virus/clamav.bats | 5 +- .../disabled_clamav_spamassassin.bats | 3 +- .../set1/spam_virus/spam_junk_folder.bats | 7 +-- .../container_configuration/hostname.bats | 4 +- test/tests/parallel/set3/mta/lmtp_ip.bats | 5 +- .../parallel/set3/mta/smtp_delivery.bats | 30 +++++----- test/tests/parallel/set3/mta/smtponly.bats | 4 +- test/tests/serial/sedfile.bats | 57 ++++++++----------- 11 files changed, 55 insertions(+), 76 deletions(-) diff --git a/test/helper/sending.bash b/test/helper/sending.bash index 81809b5c..29a45622 100644 --- a/test/helper/sending.bash +++ b/test/helper/sending.bash @@ -13,6 +13,7 @@ # # @param ${1} = template file (path) name # @param ${2} = container name [OPTIONAL] +# @param ${3} = port `nc` will use [OPTIONAL] # # ## Attention # @@ -22,8 +23,9 @@ function _send_email() { local TEMPLATE_FILE=${1:?Must provide name of template file} local CONTAINER_NAME=$(__handle_container_name "${2:-}") + local PORT=${3:-25} - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/${TEMPLATE_FILE}.txt" + _run_in_container_bash "nc 0.0.0.0 ${PORT} < /tmp/docker-mailserver-test/email-templates/${TEMPLATE_FILE}.txt" assert_success } diff --git a/test/tests/parallel/set1/dovecot/dovecot_sieve.bats b/test/tests/parallel/set1/dovecot/dovecot_sieve.bats index 3bddd84f..9e80932b 100644 --- a/test/tests/parallel/set1/dovecot/dovecot_sieve.bats +++ b/test/tests/parallel/set1/dovecot/dovecot_sieve.bats @@ -26,9 +26,9 @@ function setup_file() { _wait_for_smtp_port_in_container # Single mail sent from 'spam@spam.com' that is handled by User (relocate) and Global (copy) sieves for user1: - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/sieve-spam-folder.txt" + _send_email 'sieve-spam-folder' # Mail for user2 triggers the sieve-pipe: - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/sieve-pipe.txt" + _send_email 'sieve-pipe' _wait_for_empty_mail_queue_in_container } diff --git a/test/tests/parallel/set1/dovecot/special_use_folders.bats b/test/tests/parallel/set1/dovecot/special_use_folders.bats index 9c1bead3..69db014d 100644 --- a/test/tests/parallel/set1/dovecot/special_use_folders.bats +++ b/test/tests/parallel/set1/dovecot/special_use_folders.bats @@ -13,14 +13,12 @@ function setup_file() { function teardown_file() { _default_teardown ; } -@test "normal delivery works" { - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt" - assert_success - +@test 'normal delivery works' { + _send_email 'existing-user1' _count_files_in_directory_in_container /var/mail/localhost.localdomain/user1/new 1 } -@test "(IMAP) special-use folders should not exist yet" { +@test '(IMAP) special-use folders should not exist yet' { _should_have_content_in_directory '/var/mail/localhost.localdomain/user1' refute_line '.Drafts' refute_line '.Sent' diff --git a/test/tests/parallel/set1/spam_virus/clamav.bats b/test/tests/parallel/set1/spam_virus/clamav.bats index 74356da4..e80c3068 100644 --- a/test/tests/parallel/set1/spam_virus/clamav.bats +++ b/test/tests/parallel/set1/spam_virus/clamav.bats @@ -25,10 +25,7 @@ function setup_file() { _wait_for_service postfix _wait_for_smtp_port_in_container - - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-virus.txt" - assert_success - + _send_email 'amavis-virus' _wait_for_empty_mail_queue_in_container } diff --git a/test/tests/parallel/set1/spam_virus/disabled_clamav_spamassassin.bats b/test/tests/parallel/set1/spam_virus/disabled_clamav_spamassassin.bats index aba521fd..e639950b 100644 --- a/test/tests/parallel/set1/spam_virus/disabled_clamav_spamassassin.bats +++ b/test/tests/parallel/set1/spam_virus/disabled_clamav_spamassassin.bats @@ -17,8 +17,7 @@ function setup_file() { _common_container_setup 'CUSTOM_SETUP_ARGUMENTS' _wait_for_smtp_port_in_container - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt" - assert_success + _send_email 'existing-user1' _wait_for_empty_mail_queue_in_container } 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 96f549b4..7a1bf609 100644 --- a/test/tests/parallel/set1/spam_virus/spam_junk_folder.bats +++ b/test/tests/parallel/set1/spam_virus/spam_junk_folder.bats @@ -71,11 +71,8 @@ function teardown() { _default_teardown ; } function _should_send_spam_message() { _wait_for_smtp_port_in_container - # Port 10024 (Amavis) - _wait_for_tcp_port_in_container 10024 - - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt" - assert_success + _wait_for_tcp_port_in_container 10024 # port 10024 is for Amavis + _send_email 'amavis-spam' } function _should_be_received_by_amavis() { diff --git a/test/tests/parallel/set3/container_configuration/hostname.bats b/test/tests/parallel/set3/container_configuration/hostname.bats index ceebe0f6..cede4bca 100644 --- a/test/tests/parallel/set3/container_configuration/hostname.bats +++ b/test/tests/parallel/set3/container_configuration/hostname.bats @@ -206,9 +206,7 @@ function _should_have_correct_mail_headers() { # (eg: OVERRIDE_HOSTNAME or `--hostname mail --domainname example.test`) local EXPECTED_HOSTNAME=${3:-${EXPECTED_FQDN}} - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt" - assert_success - + _send_email 'existing-user1' _wait_for_empty_mail_queue_in_container _count_files_in_directory_in_container '/var/mail/localhost.localdomain/user1/new/' '1' diff --git a/test/tests/parallel/set3/mta/lmtp_ip.bats b/test/tests/parallel/set3/mta/lmtp_ip.bats index a9a01b4b..ae74001b 100644 --- a/test/tests/parallel/set3/mta/lmtp_ip.bats +++ b/test/tests/parallel/set3/mta/lmtp_ip.bats @@ -38,10 +38,7 @@ function teardown_file() { _default_teardown ; } @test "delivers mail to existing account" { _wait_for_smtp_port_in_container - - # Send a test mail: - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt" - assert_success + _send_email 'existing-user1' # send a test email # Verify delivery was successful, log line should look similar to: # postfix/lmtp[1274]: 0EA424ABE7D9: to=, relay=127.0.0.1[127.0.0.1]:24, delay=0.13, delays=0.07/0.01/0.01/0.05, dsn=2.0.0, status=sent (250 2.0.0 ixPpB+Zvv2P7BAAAUi6ngw Saved) diff --git a/test/tests/parallel/set3/mta/smtp_delivery.bats b/test/tests/parallel/set3/mta/smtp_delivery.bats index 4251fdb9..5b6ec54b 100644 --- a/test/tests/parallel/set3/mta/smtp_delivery.bats +++ b/test/tests/parallel/set3/mta/smtp_delivery.bats @@ -65,30 +65,30 @@ function setup_file() { # _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-virus.txt" # Required for 'delivers mail to existing alias': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-alias-external.txt' + _send_email 'existing-alias-external' # Required for 'delivers mail to existing alias with recipient delimiter': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-alias-recipient-delimiter.txt' + _send_email 'existing-alias-recipient-delimiter' # Required for 'delivers mail to existing catchall': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-catchall-local.txt' + _send_email 'existing-catchall-local' # Required for 'delivers mail to regexp alias': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-regexp-alias-local.txt' + _send_email 'existing-regexp-alias-local' # Required for 'rejects mail to unknown user': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/non-existing-user.txt' + _send_email 'non-existing-user' # Required for 'redirects mail to external aliases': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-regexp-alias-external.txt' - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-alias-local.txt' + _send_email 'existing-regexp-alias-external' + _send_email 'existing-alias-local' # Required for 'rejects spam': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt' + _send_email 'amavis-spam' # Required for 'delivers mail to existing account': - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt' - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user2.txt' - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user3.txt' - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-added.txt' - _run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user-and-cc-local-alias.txt' - _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' + _send_email 'existing-user1' + _send_email 'existing-user2' + _send_email 'existing-user3' + _send_email 'existing-added' + _send_email 'existing-user-and-cc-local-alias' + _send_email 'sieve-spam-folder' + _send_email 'sieve-pipe' _run_in_container_bash 'sendmail root < /tmp/docker-mailserver-test/email-templates/root-email.txt' } diff --git a/test/tests/parallel/set3/mta/smtponly.bats b/test/tests/parallel/set3/mta/smtponly.bats index 425d7008..a53107fc 100644 --- a/test/tests/parallel/set3/mta/smtponly.bats +++ b/test/tests/parallel/set3/mta/smtponly.bats @@ -31,10 +31,8 @@ function teardown_file() { _default_teardown ; } assert_success - # it looks as if someone tries to send mail to another domain outside of DMS - _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/smtp-only.txt" - assert_success + _send_email 'smtp-only' _wait_for_empty_mail_queue_in_container # this seemingly succeeds, but looking at the logs, it doesn't diff --git a/test/tests/serial/sedfile.bats b/test/tests/serial/sedfile.bats index 5b8c593f..bef2e6a8 100644 --- a/test/tests/serial/sedfile.bats +++ b/test/tests/serial/sedfile.bats @@ -1,88 +1,81 @@ -load "${REPOSITORY_ROOT}/test/test_helper/common" +load "${REPOSITORY_ROOT}/test/helper/setup" +load "${REPOSITORY_ROOT}/test/helper/common" -CONTAINER='sedfile' TEST_FILE='/tmp/sedfile-test.txt' +BATS_TEST_NAME_PREFIX='[sedfile] ' +CONTAINER_NAME='dms-test_sedfile' # prepare tests function setup_file() { - local PRIVATE_CONFIG - PRIVATE_CONFIG="$(duplicate_config_for_container . )" - - docker run -d --name "${CONTAINER}" \ - -v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \ - -h mail.my-domain.com "${NAME}" - - wait_for_finished_setup_in_container "${CONTAINER}" + _init_with_defaults + _common_container_setup } +function teardown_file() { _default_teardown ; } + function setup() { # create test file - docker exec "${CONTAINER}" bash -c 'echo "foo bar" > "'"${TEST_FILE}"'"' + _run_in_container_bash "echo 'foo bar' >'${TEST_FILE}'" } -@test "checking sedfile parameter count" { - run docker exec "${CONTAINER}" sedfile +@test 'checking parameter count' { + _run_in_container sedfile assert_failure assert_output --partial 'At least three parameters must be given' } -@test "checking sedfile substitute success" { +@test 'checking substitute success' { # change 'bar' to 'baz' - run docker exec "${CONTAINER}" sedfile -i 's|bar|baz|' "${TEST_FILE}" + _run_in_container sedfile -i 's|bar|baz|' "${TEST_FILE}" assert_success assert_output '' # file modified? - run docker exec "${CONTAINER}" cat "${TEST_FILE}" + _run_in_container cat "${TEST_FILE}" assert_success assert_output 'foo baz' } -@test "checking sedfile substitute failure (on first container start)" { +@test 'checking sedfile substitute failure (on first container start)' { # delete marker - run docker exec "${CONTAINER}" rm '/CONTAINER_START' + _run_in_container rm '/CONTAINER_START' assert_success # try to change 'baz' to 'something' and fail - run docker exec "${CONTAINER}" sedfile -i 's|baz|something|' "${TEST_FILE}" + _run_in_container sedfile -i 's|baz|something|' "${TEST_FILE}" assert_failure assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|baz|something| /tmp/sedfile-test.txt)" # file unchanged? - run docker exec "${CONTAINER}" cat "${TEST_FILE}" + _run_in_container cat "${TEST_FILE}" assert_success assert_output 'foo bar' # recreate marker - run docker exec "${CONTAINER}" touch '/CONTAINER_START' + _run_in_container touch '/CONTAINER_START' assert_success } -@test "checking sedfile silent failure on substitute (when DMS was restarted)" { +@test 'checking sedfile silent failure on substitute (when DMS was restarted)' { # try to change 'baz' to 'something' and fail silently - run docker exec "${CONTAINER}" sedfile -i 's|baz|something|' "${TEST_FILE}" + _run_in_container sedfile -i 's|baz|something|' "${TEST_FILE}" assert_success assert_output '' # file unchanged? - run docker exec "${CONTAINER}" cat "${TEST_FILE}" + _run_in_container cat "${TEST_FILE}" assert_success assert_output 'foo bar' } -@test "checking sedfile substitude failure (strict)" { +@test 'checking sedfile substitude failure (strict)' { # try to change 'baz' to 'something' and fail - run docker exec "${CONTAINER}" sedfile --strict -i 's|baz|something|' "${TEST_FILE}" + _run_in_container sedfile --strict -i 's|baz|something|' "${TEST_FILE}" assert_failure assert_output --partial "No difference after call to 'sed' in 'sedfile' (sed -i s|baz|something| /tmp/sedfile-test.txt)" # file unchanged? - run docker exec "${CONTAINER}" cat "${TEST_FILE}" + _run_in_container cat "${TEST_FILE}" assert_success assert_output 'foo bar' } - -# clean up -function teardown_file() { - docker rm -f "${CONTAINER}" -}