ci: refactored sedfile & used _send_mail where possible (#3103)

Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
This commit is contained in:
Georg Lauterbach 2023-02-22 10:26:04 +01:00 committed by GitHub
parent 853301338c
commit 35692a9111
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 76 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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'

View file

@ -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
}

View file

@ -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
}

View file

@ -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() {

View file

@ -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'

View file

@ -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=<user1@localhost.localdomain>, 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 <user1@localhost.localdomain> ixPpB+Zvv2P7BAAAUi6ngw Saved)

View file

@ -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'
}

View file

@ -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

View file

@ -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}"
}