test helpers: add functionality for sending emails (#3026)

* add functionality for filtering mail log by ID

This was not planned, but as @polarthene mentioned in
https://github.com/docker-mailserver/docker-mailserver/pull/3033#issuecomment-1407169569
, filtering the mail log by email ID would be (the only) correct
approach for the Rspamd test (to eliminate race conditions).

I asserted the currect state, and came to the conclusion that this might
(or actually is) something we want in more than one place. So I went
ahead and implemented a solution.

The solution for acquiring the ID is a bit slower because it ensures the
mail queue is empty _before_ and _after_ the mail is sent. This is the
tradeoff one has to make if they want to send multiple emails in one
test file and get their IDs.

I hope you like this approach. I will provide another PR that adjusts
our current tests to use these new functions.

* added note about our helper functions in the docs

I think our work for our custom test framework should be noted in the
docs for newcomers to better understand what they should do.

* adjust Rspamd test to use new helpers for sending

* improve filter helpers further

* add sanity check when acquiring mail ID

* re-add `refute_output` to test which should now work well
This commit is contained in:
Georg Lauterbach 2023-01-29 14:52:38 +01:00 committed by GitHub
parent 5f94d7b36b
commit f496897b09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 36 deletions

View file

@ -1,7 +1,7 @@
load "${REPOSITORY_ROOT}/test/helper/setup"
load "${REPOSITORY_ROOT}/test/helper/common"
BATS_TEST_NAME_PREFIX='[rspamd] '
BATS_TEST_NAME_PREFIX='[Rspamd] '
CONTAINER_NAME='dms-test_rspamd'
function setup_file() {
@ -30,14 +30,9 @@ function setup_file() {
# We will send 3 emails: the first one should pass just fine; the second one should
# be rejected due to spam; the third one should be rejected due to a virus.
_run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/existing-user1.txt"
assert_success
_run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/rspamd-spam.txt"
assert_success
_run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/rspamd-virus.txt"
assert_success
_wait_for_empty_mail_queue_in_container "${CONTAINER_NAME}"
export MAIL_ID1=$(_send_mail_and_get_id 'existing-user1')
export MAIL_ID2=$(_send_mail_and_get_id 'rspamd-spam')
export MAIL_ID3=$(_send_mail_and_get_id 'rspamd-virus')
}
function teardown_file() { _default_teardown ; }
@ -48,49 +43,39 @@ function teardown_file() { _default_teardown ; }
}
@test "logs exist and contains proper content" {
_should_contain_string_rspamd 'rspamd .* is loading configuration'
_should_contain_string_rspamd 'lua module clickhouse is disabled in the configuration'
_should_contain_string_rspamd 'lua module dkim_signing is disabled in the configuration'
_should_contain_string_rspamd 'lua module elastic is disabled in the configuration'
_should_contain_string_rspamd 'lua module rbl is disabled in the configuration'
_should_contain_string_rspamd 'lua module reputation is disabled in the configuration'
_should_contain_string_rspamd 'lua module spamassassin is disabled in the configuration'
_should_contain_string_rspamd 'lua module url_redirector is disabled in the configuration'
_should_contain_string_rspamd 'lua module metric_exporter is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'rspamd .* is loading configuration'
_service_log_should_contain_string 'rspamd' 'lua module clickhouse is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'lua module dkim_signing is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'lua module elastic is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'lua module rbl is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'lua module reputation is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'lua module spamassassin is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'lua module url_redirector is disabled in the configuration'
_service_log_should_contain_string 'rspamd' 'lua module metric_exporter is disabled in the configuration'
}
@test "normal mail passes fine" {
_should_contain_string_rspamd 'F (no action)'
_service_log_should_contain_string 'rspamd' 'F \(no action\)'
run docker logs -n 100 "${CONTAINER_NAME}"
assert_success
_print_mail_log_for_id "${MAIL_ID1}"
assert_output --partial "stored mail into mailbox 'INBOX'"
}
@test "detects and rejects spam" {
_should_contain_string_rspamd 'S (reject)'
_should_contain_string_rspamd 'reject "Gtube pattern"'
_service_log_should_contain_string 'rspamd' 'S \(reject\)'
_service_log_should_contain_string 'rspamd' 'reject "Gtube pattern"'
run docker logs -n 100 "${CONTAINER_NAME}"
assert_success
_print_mail_log_for_id "${MAIL_ID2}"
assert_output --partial 'milter-reject'
assert_output --partial '5.7.1 Gtube pattern'
}
@test "detects and rejects virus" {
_should_contain_string_rspamd 'T (reject)'
_should_contain_string_rspamd 'reject "ClamAV FOUND VIRUS "Eicar-Signature"'
_service_log_should_contain_string 'rspamd' 'T \(reject\)'
_service_log_should_contain_string 'rspamd' 'reject "ClamAV FOUND VIRUS "Eicar-Signature"'
run docker logs -n 8 "${CONTAINER_NAME}"
assert_success
_print_mail_log_for_id "${MAIL_ID3}"
assert_output --partial 'milter-reject'
assert_output --partial '5.7.1 ClamAV FOUND VIRUS "Eicar-Signature"'
refute_output --partial "stored mail into mailbox 'INBOX'"
}
function _should_contain_string_rspamd() {
local STRING=${1:?No string provided to _should_contain_string_rspamd}
_run_in_container grep -q "${STRING}" /var/log/supervisor/rspamd.log
assert_success
}