Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Refactored bounced spam test + Introduce common container setup template #2198

Merged
4 changes: 3 additions & 1 deletion target/scripts/startup/setup-stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1469,11 +1469,13 @@ function _setup_security_stack

if [[ ${SPAMASSASSIN_SPAM_TO_INBOX} -eq 1 ]]
then
_notify 'inf' 'Configure Spamassassin/Amavis to put SPAM inbox'
_notify 'inf' 'Configuring Spamassassin/Amavis to send SPAM to inbox'

sed -i "s|\$final_spam_destiny.*=.*$|\$final_spam_destiny = D_PASS;|g" /etc/amavis/conf.d/49-docker-mailserver
sed -i "s|\$final_bad_header_destiny.*=.*$|\$final_bad_header_destiny = D_PASS;|g" /etc/amavis/conf.d/49-docker-mailserver
else
_notify 'inf' 'Configuring Spamassassin/Amavis to bounce SPAM'

sed -i "s|\$final_spam_destiny.*=.*$|\$final_spam_destiny = D_BOUNCE;|g" /etc/amavis/conf.d/49-docker-mailserver
sed -i "s|\$final_bad_header_destiny.*=.*$|\$final_bad_header_destiny = D_BOUNCE;|g" /etc/amavis/conf.d/49-docker-mailserver

Expand Down
86 changes: 44 additions & 42 deletions test/mail_spam_bounced.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,75 @@ load 'test_helper/common'

# Test case
# ---------
# When SPAMASSASSIN_SPAM_TO_INBOX=0, spam messages must be bounced.
# 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

# TODO: ENV setup will move to actual ENV files in future.

function setup() {
run_setup_file_if_necessary
run_setup_file_if_necessary
}

function teardown() {
run_teardown_file_if_necessary
docker rm -f "${TEST_NAME}"
run_teardown_file_if_necessary
}

function setup_file() {
local PRIVATE_CONFIG
PRIVATE_CONFIG="$(duplicate_config_for_container . mail_spam_bounced_defined)"
docker run -d --name mail_spam_bounced_defined \
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \
-e ENABLE_SPAMASSASSIN=1 \
-e SPAMASSASSIN_SPAM_TO_INBOX=0 \
-h mail.my-domain.com -t "${NAME}"

wait_for_finished_setup_in_container mail_spam_bounced_defined

PRIVATE_CONFIG="$(duplicate_config_for_container . mail_spam_bounced_defined)"
docker run -d --name mail_spam_bounced_undefined \
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \
-e ENABLE_SPAMASSASSIN=1 \
-h mail.my-domain.com -t "${NAME}"

wait_for_finished_setup_in_container mail_spam_bounced_undefined
init_with_defaults
}

function teardown_file() {
docker rm -f mail_spam_bounced_defined
docker rm -f mail_spam_bounced_undefined
}
# Not used
# function teardown_file() {
# }

@test "first" {
skip 'this test must come first to reliably identify when to run setup_file'
}

@test "checking amavis: spam message is bounced" {
# this warning should only be raised when no explicit value for SPAMASSASSIN_SPAM_TO_INBOX is defined
run sh -c "docker logs mail_spam_bounced_defined | grep 'Spam messages WILL NOT BE DELIVERED'"
assert_failure
@test "checking amavis: spam message is bounced (rejected)" {
local TEST_ENV_FILE="${PRIVATE_CONFIG}/defined.env"
echo 'ENABLE_SPAMASSASSIN=1' > "${TEST_ENV_FILE}"
echo 'SPAMASSASSIN_SPAM_TO_INBOX=0' >> "${TEST_ENV_FILE}"

# send a spam message
run docker exec mail_spam_bounced_defined /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt"
assert_success
common_container_setup "${TEST_ENV_FILE}"

run repeat_until_success_or_timeout 20 sh -c "docker logs mail_spam_bounced_defined | grep 'Blocked SPAM {NoBounceInbound,Quarantined}'"
assert_success
run _should_emit_warning
assert_failure

_should_bounce_spam
}

@test "checking amavis: spam message is bounced, undefined SPAMASSASSIN_SPAM_TO_INBOX raise a warning" {
run sh -c "docker logs mail_spam_bounced_undefined | grep 'Spam messages WILL NOT BE DELIVERED'"
assert_success
@test "checking amavis: spam message is bounced (rejected), undefined SPAMASSASSIN_SPAM_TO_INBOX should raise a warning" {
# SPAMASSASSIN_SPAM_TO_INBOX=0 is the default. If no explicit ENV value is set, it should log a warning at startup.
local TEST_ENV_FILE="${PRIVATE_CONFIG}/undefined.env"
echo 'ENABLE_SPAMASSASSIN=1' > "${TEST_ENV_FILE}"

# send a spam message
run docker exec mail_spam_bounced_defined /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt"
assert_success
common_container_setup "${TEST_ENV_FILE}"

run repeat_until_success_or_timeout 20 sh -c "docker logs mail_spam_bounced_defined | grep 'Blocked SPAM {NoBounceInbound,Quarantined}'"
run _should_emit_warning
assert_success

_should_bounce_spam
}

@test "last" {
skip 'this test is only there to reliably mark the end for the teardown_file'
}

# This warning should only be raised when the env SPAMASSASSIN_SPAM_TO_INBOX has no explicit value set
function _should_emit_warning() {
sh -c "docker logs ${TEST_NAME} | grep 'Spam messages WILL NOT BE DELIVERED'"
}

function _should_bounce_spam() {
wait_for_smtp_port_in_container_to_respond "${TEST_NAME}"

# send a spam message
run docker exec "${TEST_NAME}" /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-spam.txt"
assert_success

run repeat_until_success_or_timeout 20 sh -c "docker logs ${TEST_NAME} | grep 'Blocked SPAM {NoBounceInbound,Quarantined}'"
assert_success
}
32 changes: 32 additions & 0 deletions test/test_helper/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function wait_for_amavis_port_in_container() {
wait_for_tcp_port_in_container 10024 "${1}"
}

# TODO: Should also fail early on "docker logs ${1} | egrep '^[ FATAL ]'"?
# @param ${1} name of the postfix container
function wait_for_finished_setup_in_container() {
local STATUS=0
Expand Down Expand Up @@ -205,3 +206,34 @@ function wait_for_empty_mail_queue_in_container() {
# shellcheck disable=SC2016
repeat_in_container_until_success_or_timeout "${TIMEOUT}" "${CONTAINER_NAME}" bash -c '[[ $(mailq) == *"Mail queue is empty"* ]]'
}

# Common defaults appropriate for most tests, override vars in each test when necessary.
# TODO: Check how many tests need write access. Consider using `docker create` + `docker cp` for easier cleanup.
function init_with_defaults() {
# Ignore absolute dir path and file extension, only extract filename:
export TEST_NAME
TEST_NAME="$(basename "${BATS_TEST_FILENAME}" '.bats')"

export PRIVATE_CONFIG
PRIVATE_CONFIG="$(duplicate_config_for_container . "${TEST_NAME}")"
export TEST_FILES_VOLUME="${PWD}/test/test-files:/tmp/docker-mailserver-test:ro"
export TEST_CONFIG_VOLUME="${PRIVATE_CONFIG}:/tmp/docker-mailserver:ro"

export TEST_FQDN='mail.my-domain.com'
polarathene marked this conversation as resolved.
Show resolved Hide resolved
}

# Common docker run command that should satisfy most tests which only modify ENV.
function common_container_setup() {
local TEST_ENV_FILE=$1

run docker run -d --name "${TEST_NAME}" \
--volume "${TEST_FILES_VOLUME}" \
--volume "${TEST_CONFIG_VOLUME}" \
--hostname "${TEST_FQDN}" \
--env-file "${TEST_ENV_FILE}" \
--tty \
"${NAME}"
assert_success

wait_for_finished_setup_in_container "${TEST_NAME}"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub says here is no newline character at the end of this file. Intended?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does that to me too randomly. Very odd when it happens.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly an auto-formatting issue?

Copy link
Member Author

@polarathene polarathene Sep 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope not intended, usually my editor adds them on save. I noticed similar was happening with a PR from @NorseGaud , although I'm not sure if I was overly fussed about it, we may have some existing files that are missing new line at end of file, is there an easy way to check all files for it?

EDIT: Oh, I should have refreshed, I see there was more than one comment since, whoops 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that I copy/pasted this portion to the file from the bats test that I originally had it in. It's not a Github thing as my local commit has it missing too.

Looks like I need to tell VSCode to make sure it adds new lines on save, or get it to load the .editorconfig file, which I recall being a hassle with VSCode to get working properly in the past :\

Do either of you have .editorconfig working with VSCode? Otherwise another option is to have a local git commit hook or github workflow on merge that ensures compliance with .editorconfig, actually I thought we had a lint for this...?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I can test it locally again.

Sure but lets not enforce the linting on that just yet (assuming many lint issues are raised as a result).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do either of you have .editorconfig working with VSCode?

I'm using the same extension @georglauterbach mentioned and never had an issue before 💁🏻

Name: EditorConfig for VS Code
Id: editorconfig.editorconfig
Description: EditorConfig Support for Visual Studio Code
Version: 0.16.4
Publisher: EditorConfig
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig

or github workflow on merge that ensures compliance with .editorconfig, actually I thought we had a lint for this...

Should be covered by


We need to check if this lint ever worked and which config it is using ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and never had an issue before 💁🏻

I haven't tried in a while, but I recall it not working properly with VSCode when I last tried, while my previou editor Atom had no issue. I'm on Linux, if one of you are also, then it's probably an issue with my system. I'll try that extension again at some point :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS 💻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux swift 5.11.0-34-generic #36-Ubuntu SMP Thu Aug 26 19:22:09 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux :D