tests: refactor POP3, IMAP (actually SASLauthd + RIMAP) & relay (#3062)

* refactor `mail_pop3.bats`
* refactor `mail_with_imap.bats`
* refactor `mail_with_relays.bats`
* moved test that that did not belong into POP3 test
* slightly clean up `no_container.bats`

Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
This commit is contained in:
Georg Lauterbach 2023-02-06 23:59:01 +01:00 committed by GitHub
parent 4efbbbe571
commit 2caec2cdf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 156 deletions

View file

@ -221,6 +221,7 @@ function _run_until_success_or_timeout() {
local STARTTIME=${SECONDS} local STARTTIME=${SECONDS}
# shellcheck disable=SC2154
until run "${@}" && [[ ${status} -eq 0 ]] until run "${@}" && [[ ${status} -eq 0 ]]
do do
sleep 1 sleep 1
@ -367,6 +368,7 @@ function _add_mail_account_then_wait_until_ready() {
# #
# @param ${1} = number of lines that the output should have # @param ${1} = number of lines that the output should have
function _should_output_number_of_lines() { function _should_output_number_of_lines() {
# shellcheck disable=SC2154
assert_equal "${#lines[@]}" "${1:?Number of lines not provided}" assert_equal "${#lines[@]}" "${1:?Number of lines not provided}"
} }

View file

@ -1,7 +1,7 @@
load "${REPOSITORY_ROOT}/test/helper/common" load "${REPOSITORY_ROOT}/test/helper/common"
load "${REPOSITORY_ROOT}/test/helper/setup" load "${REPOSITORY_ROOT}/test/helper/setup"
BATS_TEST_NAME_PREFIX='[Amavis] ' BATS_TEST_NAME_PREFIX='[Amavis + SA] '
CONTAINER_NAME='dms-test_amavis' CONTAINER_NAME='dms-test_amavis'
function setup_file() { function setup_file() {
@ -18,10 +18,29 @@ function setup_file() {
function teardown_file() { _default_teardown ; } function teardown_file() { _default_teardown ; }
@test "SpamAssassin integration should be active" { @test 'SpamAssassin integration should be active' {
# give Amavis just a bit of time to print out its full debug log # give Amavis just a bit of time to print out its full debug log
run _repeat_in_container_until_success_or_timeout 5 "${CONTAINER_NAME}" grep 'ANTI-SPAM-SA' /var/log/mail/mail.log run _repeat_in_container_until_success_or_timeout 5 "${CONTAINER_NAME}" grep 'ANTI-SPAM-SA' /var/log/mail/mail.log
assert_success assert_success
assert_output --partial 'loaded' assert_output --partial 'loaded'
refute_output --partial 'NOT loaded' refute_output --partial 'NOT loaded'
} }
@test 'SA ENV should update Amavis config' {
local AMAVIS_DEFAULTS_FILE='/etc/amavis/conf.d/20-debian_defaults'
_run_in_container grep '\$sa_tag_level_deflt' "${AMAVIS_DEFAULTS_FILE}"
assert_success
assert_output --partial '= 2.0'
_run_in_container grep '\$sa_tag2_level_deflt' "${AMAVIS_DEFAULTS_FILE}"
assert_success
assert_output --partial '= 6.31'
_run_in_container grep '\$sa_kill_level_deflt' "${AMAVIS_DEFAULTS_FILE}"
assert_success
assert_output --partial '= 6.31'
_run_in_container grep '\$sa_spam_subject_tag' "${AMAVIS_DEFAULTS_FILE}"
assert_success
assert_output --partial "= '***SPAM*** ';"
}

View file

@ -1,85 +1,46 @@
load "${REPOSITORY_ROOT}/test/test_helper/common" load "${REPOSITORY_ROOT}/test/helper/common"
load "${REPOSITORY_ROOT}/test/helper/setup"
BATS_TEST_NAME_PREFIX='[POP3] '
CONTAINER_NAME='dms-test_pop3'
function setup_file() { function setup_file() {
local PRIVATE_CONFIG _init_with_defaults
PRIVATE_CONFIG=$(duplicate_config_for_container .)
docker run -d --name mail_pop3 \ local CUSTOM_SETUP_ARGUMENTS=(
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \ --env ENABLE_POP3=1
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \ --env PERMIT_DOCKER=container
-e ENABLE_POP3=1 \ )
-e PERMIT_DOCKER=container \
-h mail.my-domain.com -t "${NAME}"
wait_for_finished_setup_in_container mail_pop3 _common_container_setup 'CUSTOM_SETUP_ARGUMENTS'
} }
function teardown_file() { function teardown_file() { _default_teardown ; }
docker rm -f mail_pop3
@test 'server is ready' {
_run_in_container nc -w 1 0.0.0.0 110
assert_success
assert_output --partial '+OK'
} }
# @test 'authentication works' {
# pop _run_in_container_bash 'nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/pop3-auth.txt'
#
@test "checking pop: server is ready" {
run docker exec mail_pop3 /bin/bash -c "nc -w 1 0.0.0.0 110 | grep '+OK'"
assert_success assert_success
} }
@test "checking pop: authentication works" { @test 'added user authentication works' {
run docker exec mail_pop3 /bin/sh -c "nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/pop3-auth.txt" _run_in_container_bash 'nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/added-pop3-auth.txt'
assert_success assert_success
} }
@test "checking pop: added user authentication works" { @test '/var/log/mail/mail.log is error-free' {
run docker exec mail_pop3 /bin/sh -c "nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/added-pop3-auth.txt" _run_in_container grep 'non-null host address bits in' /var/log/mail/mail.log
assert_success
}
#
# spamassassin
#
@test "checking spamassassin: docker env variables are set correctly (default)" {
run docker exec mail_pop3 /bin/sh -c "grep '\$sa_tag_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 2.0'"
assert_success
run docker exec mail_pop3 /bin/sh -c "grep '\$sa_tag2_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 6.31'"
assert_success
run docker exec mail_pop3 /bin/sh -c "grep '\$sa_kill_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 6.31'"
assert_success
run docker exec mail_pop3 /bin/sh -c "grep '\$sa_spam_subject_tag' /etc/amavis/conf.d/20-debian_defaults | grep '= .\*\*\*SPAM\*\*\* .'"
assert_success
}
#
# system
#
@test "checking system: /var/log/mail/mail.log is error free" {
run docker exec mail_pop3 grep 'non-null host address bits in' /var/log/mail/mail.log
assert_failure assert_failure
_run_in_container grep ': error:' /var/log/mail/mail.log
run docker exec mail_pop3 grep ': error:' /var/log/mail/mail.log
assert_failure assert_failure
} }
# @test '(Manage Sieve) disabled per default' {
# sieve _run_in_container nc -z 0.0.0.0 4190
#
@test "checking manage sieve: disabled per default" {
run docker exec mail_pop3 /bin/bash -c "nc -z 0.0.0.0 4190"
assert_failure assert_failure
} }
#
# PERMIT_DOCKER mynetworks
#
@test "checking PERMIT_DOCKER: my network value" {
run docker exec mail_pop3 /bin/sh -c "postconf | grep '^mynetworks =' | egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}/32'"
assert_success
}

View file

@ -1,49 +1,42 @@
load "${REPOSITORY_ROOT}/test/test_helper/common" load "${REPOSITORY_ROOT}/test/helper/common"
load "${REPOSITORY_ROOT}/test/helper/setup"
setup_file() { BATS_TEST_NAME_PREFIX='[SASLauthd + RIMAP] '
local PRIVATE_CONFIG CONTAINER_NAME='dms-test_saslauthd_and_rimap'
PRIVATE_CONFIG=$(duplicate_config_for_container .)
docker run -d --name mail_with_imap \ function setup_file() {
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \ _init_with_defaults
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \
-e ENABLE_SASLAUTHD=1 \
-e POSTMASTER_ADDRESS=postmaster@localhost.localdomain \
-e SASLAUTHD_MECH_OPTIONS=127.0.0.1 \
-e SASLAUTHD_MECHANISMS=rimap \
-e PERMIT_DOCKER=container \
-h mail.my-domain.com -t "${NAME}"
wait_for_smtp_port_in_container mail_with_imap local CUSTOM_SETUP_ARGUMENTS=(
--env ENABLE_SASLAUTHD=1
--env SASLAUTHD_MECH_OPTIONS=127.0.0.1
--env SASLAUTHD_MECHANISMS=rimap
--env PERMIT_DOCKER=container
)
_common_container_setup 'CUSTOM_SETUP_ARGUMENTS'
_wait_for_smtp_port_in_container mail_with_imap
} }
teardown_file() { function teardown_file() { _default_teardown ; }
docker rm -f mail_with_imap
}
# @test '(Dovecot) LDAP RIMAP connection and authentication works' {
# RIMAP _run_in_container_bash "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-auth.txt"
#
# dovecot
@test "checking dovecot: ldap rimap connection and authentication works" {
run docker exec mail_with_imap /bin/sh -c "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-auth.txt"
assert_success assert_success
} }
# saslauthd @test '(SASLauthd) SASL RIMAP authentication works' {
@test "checking saslauthd: sasl rimap authentication works" { _run_in_container testsaslauthd -u 'user1@localhost.localdomain' -p 'mypassword'
run docker exec mail_with_imap bash -c "testsaslauthd -u user1@localhost.localdomain -p mypassword"
assert_success assert_success
} }
@test "checking saslauthd: rimap smtp authentication" { @test '(SASLauthd) RIMAP SMTP authentication works' {
run docker exec mail_with_imap /bin/sh -c "nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt | grep 'Authentication successful'" _run_in_container_bash 'nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt'
assert_success assert_success
assert_output --partial 'Authentication successful'
} }
# master account @test '(Dovecot) master account can login' {
@test "checking dovecot: master account can login" { _run_in_container testsaslauthd -u 'user1@localhost.localdomain*masterusername' -p 'masterpassword'
run docker exec mail_with_imap bash -c "testsaslauthd -u user1@localhost.localdomain*masterusername -p masterpassword"
assert_success assert_success
} }

View file

@ -1,81 +1,84 @@
load "${REPOSITORY_ROOT}/test/test_helper/common" load "${REPOSITORY_ROOT}/test/helper/common"
load "${REPOSITORY_ROOT}/test/helper/setup"
BATS_TEST_NAME_PREFIX='[Relay Host] '
CONTAINER_NAME='dms-test_relay'
function setup_file() { function setup_file() {
# We use a temporary config directory since we'll be dynamically editing _init_with_defaults
# it with setup.sh.
tmp_confdir=$(mktemp -d /tmp/docker-mailserver-config-relay-hosts-XXXXX)
cp -a test/config/relay-hosts/* "${tmp_confdir}/"
docker run -d --name mail_with_relays \ mv "${TEST_TMP_CONFIG}/relay-hosts/"* "${TEST_TMP_CONFIG}/"
-v "${tmp_confdir}":/tmp/docker-mailserver \
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \
-e RELAY_HOST=default.relay.com \
-e RELAY_PORT=2525 \
-e RELAY_USER=smtp_user \
-e RELAY_PASSWORD=smtp_password \
-e PERMIT_DOCKER=host \
-h mail.my-domain.com -t "${NAME}"
wait_for_finished_setup_in_container mail_with_relays local CUSTOM_SETUP_ARGUMENTS=(
--env RELAY_HOST=default.relay.com
--env RELAY_PORT=2525
--env RELAY_USER=smtp_user
--env RELAY_PASSWORD=smtp_password
--env PERMIT_DOCKER=host
)
_common_container_setup 'CUSTOM_SETUP_ARGUMENTS'
} }
function teardown_file() { function teardown_file() { _default_teardown ; }
docker rm -f mail_with_relays
rm -rf "${tmp_confdir}" @test 'default mapping is added from ENV variables' {
_run_in_container grep 'domainone.tld' /etc/postfix/relayhost_map
assert_success
assert_output --regexp '^@domainone.tld[[:space:]]+\[default.relay.com\]:2525$'
} }
@test "checking relay hosts: default mapping is added from env vars" { @test 'default mapping is added from ENV variables for virtual user entry' {
run docker exec mail_with_relays grep -e domainone.tld /etc/postfix/relayhost_map _run_in_container grep 'domain1.tld' /etc/postfix/relayhost_map
assert_output -e '^@domainone.tld[[:space:]]+\[default.relay.com\]:2525$' assert_success
assert_output --regexp '^@domain1.tld[[:space:]]+\[default.relay.com\]:2525$'
} }
@test "checking relay hosts: default mapping is added from env vars for virtual user entry" { @test 'default mapping is added from ENV variables for new user entry' {
run docker exec mail_with_relays grep -e domain1.tld /etc/postfix/relayhost_map _run_in_container grep 'domainzero.tld' /etc/postfix/relayhost_map
assert_output -e '^@domain1.tld[[:space:]]+\[default.relay.com\]:2525$' assert_failure
_add_mail_account_then_wait_until_ready 'user0@domainzero.tld' 'password123'
_run_until_success_or_timeout 20 _exec_in_container grep 'domainzero.tld' /etc/postfix/relayhost_map
assert_success
assert_output --regexp '^@domainzero.tld[[:space:]]+\[default.relay.com\]:2525$'
} }
@test "checking relay hosts: default mapping is added from env vars for new user entry" { @test 'default mapping is added from ENV variables for new virtual user (alias) entry' {
run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map _run_in_container grep 'domain2.tld' /etc/postfix/relayhost_map
assert_output '' assert_failure
run ./setup.sh -c mail_with_relays email add user0@domainzero.tld password123 run ./setup.sh -c "${CONTAINER_NAME}" alias add 'user2@domain2.tld' 'user2@domaintwo.tld'
run_until_success_or_timeout 10 docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map assert_success
assert_output -e '^@domainzero.tld[[:space:]]+\[default.relay.com\]:2525$' _run_until_success_or_timeout 10 _exec_in_container grep 'domain2.tld' /etc/postfix/relayhost_map
assert_success
assert_output --regexp '^@domain2.tld[[:space:]]+\[default.relay.com\]:2525$'
} }
@test "checking relay hosts: default mapping is added from env vars for new virtual user entry" { @test 'custom mapping is added from file' {
run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map _run_in_container grep 'domaintwo.tld' /etc/postfix/relayhost_map
assert_output '' assert_success
assert_output --regexp '^@domaintwo.tld[[:space:]]+\[other.relay.com\]:587$'
run ./setup.sh -c mail_with_relays alias add user2@domain2.tld user2@domaintwo.tld
run_until_success_or_timeout 10 docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map
assert_output -e '^@domain2.tld[[:space:]]+\[default.relay.com\]:2525$'
} }
@test "checking relay hosts: custom mapping is added from file" { @test 'ignored domain is not added' {
run docker exec mail_with_relays grep -e domaintwo.tld /etc/postfix/relayhost_map _run_in_container grep domainthree.tld /etc/postfix/relayhost_map
assert_output -e '^@domaintwo.tld[[:space:]]+\[other.relay.com\]:587$' assert_failure
} }
@test "checking relay hosts: ignored domain is not added" { @test '/etc/postfix/sasl_passwd exists' {
run docker exec mail_with_relays grep -e domainthree.tld /etc/postfix/relayhost_map _run_in_container_bash '[[ -f /etc/postfix/sasl_passwd ]]'
assert_failure 1
assert_output ''
}
@test "checking relay hosts: sasl_passwd exists" {
run docker exec mail_with_relays [ -f /etc/postfix/sasl_passwd ]
assert_success assert_success
} }
@test "checking relay hosts: auth entry is added" { @test 'auth entry is added' {
run docker exec mail_with_relays /bin/sh -c 'cat /etc/postfix/sasl_passwd | grep -e "^@domaintwo.tld\s\+smtp_user_2:smtp_password_2" | wc -l' _run_in_container grep '^@domaintwo.tld\s\+smtp_user_2:smtp_password_2' /etc/postfix/sasl_passwd
assert_success assert_success
assert_output 1 _should_output_number_of_lines 1
} }
@test "checking relay hosts: default auth entry is added" { @test 'default auth entry is added' {
run docker exec mail_with_relays /bin/sh -c 'cat /etc/postfix/sasl_passwd | grep -e "^\[default.relay.com\]:2525\s\+smtp_user:smtp_password" | wc -l' _run_in_container grep '^\[default.relay.com\]:2525\s\+smtp_user:smtp_password' /etc/postfix/sasl_passwd
assert_success assert_success
assert_output 1 _should_output_number_of_lines 1
} }

View file

@ -8,10 +8,10 @@ load "${REPOSITORY_ROOT}/test/helper/common"
BATS_TEST_NAME_PREFIX='[No Existing Container] ' BATS_TEST_NAME_PREFIX='[No Existing Container] '
function setup_file() { function setup_file() {
# Fail early if the testing image is already running: run docker ps # fail early if the testing image is already running:
assert_not_equal "$(docker ps | grep -o "${IMAGE_NAME}")" "${IMAGE_NAME}" assert_success
refute_output --partial "${IMAGE_NAME}"
# Copy the base config that `setup.sh` will volume mount to a container it runs:
export TEST_TMP_CONFIG export TEST_TMP_CONFIG
TEST_TMP_CONFIG=$(_duplicate_config_for_container . 'no_container') TEST_TMP_CONFIG=$(_duplicate_config_for_container . 'no_container')
} }