diff --git a/test/helper/common.bash b/test/helper/common.bash index a59d7769..1d395351 100644 --- a/test/helper/common.bash +++ b/test/helper/common.bash @@ -221,6 +221,7 @@ function _run_until_success_or_timeout() { local STARTTIME=${SECONDS} + # shellcheck disable=SC2154 until run "${@}" && [[ ${status} -eq 0 ]] do sleep 1 @@ -367,6 +368,7 @@ function _add_mail_account_then_wait_until_ready() { # # @param ${1} = number of lines that the output should have function _should_output_number_of_lines() { + # shellcheck disable=SC2154 assert_equal "${#lines[@]}" "${1:?Number of lines not provided}" } diff --git a/test/tests/parallel/set1/spam_virus/amavis.bats b/test/tests/parallel/set1/spam_virus/amavis.bats index 4211b4c9..18097e69 100644 --- a/test/tests/parallel/set1/spam_virus/amavis.bats +++ b/test/tests/parallel/set1/spam_virus/amavis.bats @@ -1,7 +1,7 @@ load "${REPOSITORY_ROOT}/test/helper/common" load "${REPOSITORY_ROOT}/test/helper/setup" -BATS_TEST_NAME_PREFIX='[Amavis] ' +BATS_TEST_NAME_PREFIX='[Amavis + SA] ' CONTAINER_NAME='dms-test_amavis' function setup_file() { @@ -18,10 +18,29 @@ function setup_file() { 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 run _repeat_in_container_until_success_or_timeout 5 "${CONTAINER_NAME}" grep 'ANTI-SPAM-SA' /var/log/mail/mail.log assert_success assert_output --partial '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*** ';" +} diff --git a/test/tests/serial/mail_pop3.bats b/test/tests/serial/mail_pop3.bats index ab10b19d..c181de79 100644 --- a/test/tests/serial/mail_pop3.bats +++ b/test/tests/serial/mail_pop3.bats @@ -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() { - local PRIVATE_CONFIG - PRIVATE_CONFIG=$(duplicate_config_for_container .) + _init_with_defaults - docker run -d --name mail_pop3 \ - -v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \ - -v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \ - -e ENABLE_POP3=1 \ - -e PERMIT_DOCKER=container \ - -h mail.my-domain.com -t "${NAME}" + local CUSTOM_SETUP_ARGUMENTS=( + --env ENABLE_POP3=1 + --env PERMIT_DOCKER=container + ) - wait_for_finished_setup_in_container mail_pop3 + _common_container_setup 'CUSTOM_SETUP_ARGUMENTS' } -function teardown_file() { - docker rm -f mail_pop3 +function teardown_file() { _default_teardown ; } + +@test 'server is ready' { + _run_in_container nc -w 1 0.0.0.0 110 + assert_success + assert_output --partial '+OK' } -# -# pop -# - -@test "checking pop: server is ready" { - run docker exec mail_pop3 /bin/bash -c "nc -w 1 0.0.0.0 110 | grep '+OK'" +@test 'authentication works' { + _run_in_container_bash 'nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/pop3-auth.txt' assert_success } -@test "checking pop: 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" +@test 'added user authentication works' { + _run_in_container_bash 'nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/added-pop3-auth.txt' assert_success } -@test "checking pop: 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/added-pop3-auth.txt" - 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 +@test '/var/log/mail/mail.log is error-free' { + _run_in_container grep 'non-null host address bits in' /var/log/mail/mail.log assert_failure - - run docker exec mail_pop3 grep ': error:' /var/log/mail/mail.log + _run_in_container grep ': error:' /var/log/mail/mail.log assert_failure } -# -# sieve -# - -@test "checking manage sieve: disabled per default" { - run docker exec mail_pop3 /bin/bash -c "nc -z 0.0.0.0 4190" +@test '(Manage Sieve) disabled per default' { + _run_in_container nc -z 0.0.0.0 4190 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 -} diff --git a/test/tests/serial/mail_with_imap.bats b/test/tests/serial/mail_with_imap.bats index 1b3d54d5..6225ff36 100644 --- a/test/tests/serial/mail_with_imap.bats +++ b/test/tests/serial/mail_with_imap.bats @@ -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() { - local PRIVATE_CONFIG - PRIVATE_CONFIG=$(duplicate_config_for_container .) +BATS_TEST_NAME_PREFIX='[SASLauthd + RIMAP] ' +CONTAINER_NAME='dms-test_saslauthd_and_rimap' - docker run -d --name mail_with_imap \ - -v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \ - -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}" +function setup_file() { + _init_with_defaults - 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() { - docker rm -f mail_with_imap -} +function teardown_file() { _default_teardown ; } -# -# RIMAP -# - -# 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" +@test '(Dovecot) LDAP RIMAP connection and authentication works' { + _run_in_container_bash "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-auth.txt" assert_success } -# saslauthd -@test "checking saslauthd: sasl rimap authentication works" { - run docker exec mail_with_imap bash -c "testsaslauthd -u user1@localhost.localdomain -p mypassword" +@test '(SASLauthd) SASL RIMAP authentication works' { + _run_in_container testsaslauthd -u 'user1@localhost.localdomain' -p 'mypassword' assert_success } -@test "checking saslauthd: rimap smtp authentication" { - 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'" +@test '(SASLauthd) RIMAP SMTP authentication works' { + _run_in_container_bash 'nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt' assert_success + assert_output --partial 'Authentication successful' } -# master account -@test "checking dovecot: master account can login" { - run docker exec mail_with_imap bash -c "testsaslauthd -u user1@localhost.localdomain*masterusername -p masterpassword" +@test '(Dovecot) master account can login' { + _run_in_container testsaslauthd -u 'user1@localhost.localdomain*masterusername' -p 'masterpassword' assert_success } diff --git a/test/tests/serial/mail_with_relays.bats b/test/tests/serial/mail_with_relays.bats index ae42d812..066e9fcf 100644 --- a/test/tests/serial/mail_with_relays.bats +++ b/test/tests/serial/mail_with_relays.bats @@ -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() { - # We use a temporary config directory since we'll be dynamically editing - # it with setup.sh. - tmp_confdir=$(mktemp -d /tmp/docker-mailserver-config-relay-hosts-XXXXX) - cp -a test/config/relay-hosts/* "${tmp_confdir}/" + _init_with_defaults - docker run -d --name mail_with_relays \ - -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}" + mv "${TEST_TMP_CONFIG}/relay-hosts/"* "${TEST_TMP_CONFIG}/" - 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() { - docker rm -f mail_with_relays - rm -rf "${tmp_confdir}" +function teardown_file() { _default_teardown ; } + +@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" { - run docker exec mail_with_relays grep -e domainone.tld /etc/postfix/relayhost_map - assert_output -e '^@domainone.tld[[:space:]]+\[default.relay.com\]:2525$' +@test 'default mapping is added from ENV variables for virtual user entry' { + _run_in_container grep 'domain1.tld' /etc/postfix/relayhost_map + 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" { - run docker exec mail_with_relays grep -e domain1.tld /etc/postfix/relayhost_map - assert_output -e '^@domain1.tld[[:space:]]+\[default.relay.com\]:2525$' +@test 'default mapping is added from ENV variables for new user entry' { + _run_in_container grep 'domainzero.tld' /etc/postfix/relayhost_map + 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" { - run docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map - assert_output '' +@test 'default mapping is added from ENV variables for new virtual user (alias) entry' { + _run_in_container grep 'domain2.tld' /etc/postfix/relayhost_map + assert_failure - run ./setup.sh -c mail_with_relays email add user0@domainzero.tld password123 - run_until_success_or_timeout 10 docker exec mail_with_relays grep -e domainzero.tld /etc/postfix/relayhost_map - assert_output -e '^@domainzero.tld[[:space:]]+\[default.relay.com\]:2525$' + run ./setup.sh -c "${CONTAINER_NAME}" alias add 'user2@domain2.tld' 'user2@domaintwo.tld' + assert_success + _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" { - run docker exec mail_with_relays grep -e domain2.tld /etc/postfix/relayhost_map - assert_output '' - - 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 'custom mapping is added from file' { + _run_in_container grep 'domaintwo.tld' /etc/postfix/relayhost_map + assert_success + assert_output --regexp '^@domaintwo.tld[[:space:]]+\[other.relay.com\]:587$' } -@test "checking relay hosts: custom mapping is added from file" { - run docker exec mail_with_relays grep -e domaintwo.tld /etc/postfix/relayhost_map - assert_output -e '^@domaintwo.tld[[:space:]]+\[other.relay.com\]:587$' +@test 'ignored domain is not added' { + _run_in_container grep domainthree.tld /etc/postfix/relayhost_map + assert_failure } -@test "checking relay hosts: ignored domain is not added" { - run docker exec mail_with_relays grep -e domainthree.tld /etc/postfix/relayhost_map - assert_failure 1 - assert_output '' -} - -@test "checking relay hosts: sasl_passwd exists" { - run docker exec mail_with_relays [ -f /etc/postfix/sasl_passwd ] +@test '/etc/postfix/sasl_passwd exists' { + _run_in_container_bash '[[ -f /etc/postfix/sasl_passwd ]]' assert_success } -@test "checking relay hosts: 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' +@test 'auth entry is added' { + _run_in_container grep '^@domaintwo.tld\s\+smtp_user_2:smtp_password_2' /etc/postfix/sasl_passwd assert_success - assert_output 1 + _should_output_number_of_lines 1 } -@test "checking relay hosts: 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' +@test 'default auth entry is added' { + _run_in_container grep '^\[default.relay.com\]:2525\s\+smtp_user:smtp_password' /etc/postfix/sasl_passwd assert_success - assert_output 1 + _should_output_number_of_lines 1 } diff --git a/test/tests/serial/no_container.bats b/test/tests/serial/no_container.bats index 16512527..c443bd62 100644 --- a/test/tests/serial/no_container.bats +++ b/test/tests/serial/no_container.bats @@ -8,10 +8,10 @@ load "${REPOSITORY_ROOT}/test/helper/common" BATS_TEST_NAME_PREFIX='[No Existing Container] ' function setup_file() { - # Fail early if the testing image is already running: - assert_not_equal "$(docker ps | grep -o "${IMAGE_NAME}")" "${IMAGE_NAME}" + run docker ps # fail early if the testing image is already running: + 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 TEST_TMP_CONFIG=$(_duplicate_config_for_container . 'no_container') }