You've already forked docker-mailserver
mirror of
https://github.com/docker-mailserver/docker-mailserver.git
synced 2025-08-07 23:03:10 +02:00
tests: Use swaks
instead of nc
for sending mail (#3732)
See associated `CHANGELOG.md` entry for details. --------- Co-authored-by: Brennan Kinney <5098581+polarathene@users.noreply.github.com>
This commit is contained in:
@ -8,11 +8,12 @@
|
||||
# ! ATTENTION: This file requires helper functions from `common.sh`!
|
||||
|
||||
# Sends a mail from localhost (127.0.0.1) to a container. To send
|
||||
# a custom email, create a file at `test/test-files/<TEST FILE>`,
|
||||
# a custom email, create a file at `test/files/<TEST FILE>`,
|
||||
# and provide `<TEST FILE>` as an argument to this function.
|
||||
#
|
||||
# @param ${1} = template file (path) name
|
||||
# @param ${2} = parameters for `nc` [OPTIONAL] (default: `0.0.0.0 25`)
|
||||
# Parameters include all options that one can supply to `swaks`
|
||||
# itself. The `--data` parameter expects a relative path from `emails/`
|
||||
# where the contents will be implicitly provided to `swaks` via STDIN.
|
||||
#
|
||||
# ## Attention
|
||||
#
|
||||
@ -23,17 +24,42 @@
|
||||
# send the email but it will not make sure the mail queue is empty after the mail
|
||||
# has been sent.
|
||||
function _send_email() {
|
||||
local TEMPLATE_FILE=${1:?Must provide name of template file}
|
||||
local NC_PARAMETERS=${2:-0.0.0.0 25}
|
||||
[[ -v CONTAINER_NAME ]] || return 1
|
||||
|
||||
assert_not_equal "${NC_PARAMETERS}" ''
|
||||
assert_not_equal "${CONTAINER_NAME:-}" ''
|
||||
# Parameter defaults common to our testing needs:
|
||||
local EHLO='mail.external.tld'
|
||||
local FROM='user@external.tld'
|
||||
local TO='user1@localhost.localdomain'
|
||||
local SERVER='0.0.0.0'
|
||||
local PORT=25
|
||||
# Extra options for `swaks` that aren't covered by the default options above:
|
||||
local ADDITIONAL_SWAKS_OPTIONS=()
|
||||
# Specifically for handling `--data` option below:
|
||||
local FINAL_SWAKS_OPTIONS=()
|
||||
|
||||
_run_in_container_bash "nc ${NC_PARAMETERS} < /tmp/docker-mailserver-test/${TEMPLATE_FILE}.txt"
|
||||
assert_success
|
||||
while [[ ${#} -gt 0 ]]; do
|
||||
case "${1}" in
|
||||
( '--ehlo' ) EHLO=${2:?--ehlo given but no argument} ; shift 2 ;;
|
||||
( '--from' ) FROM=${2:?--from given but no argument} ; shift 2 ;;
|
||||
( '--to' ) TO=${2:?--to given but no argument} ; shift 2 ;;
|
||||
( '--server' ) SERVER=${2:?--server given but no argument} ; shift 2 ;;
|
||||
( '--port' ) PORT=${2:?--port given but no argument} ; shift 2 ;;
|
||||
( '--data' )
|
||||
local TEMPLATE_FILE="/tmp/docker-mailserver-test/emails/${2:?--data given but no argument provided}.txt"
|
||||
FINAL_SWAKS_OPTIONS+=('--data')
|
||||
FINAL_SWAKS_OPTIONS+=('-')
|
||||
FINAL_SWAKS_OPTIONS+=('<')
|
||||
FINAL_SWAKS_OPTIONS+=("${TEMPLATE_FILE}")
|
||||
shift 2
|
||||
;;
|
||||
( * ) ADDITIONAL_SWAKS_OPTIONS+=("${1}") ; shift 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
_run_in_container_bash "swaks --server ${SERVER} --port ${PORT} --ehlo ${EHLO} --from ${FROM} --to ${TO} ${ADDITIONAL_SWAKS_OPTIONS[*]} ${FINAL_SWAKS_OPTIONS[*]}"
|
||||
}
|
||||
|
||||
# Like `_send_mail` with two major differences:
|
||||
# Like `_send_email` with two major differences:
|
||||
#
|
||||
# 1. this function waits for the mail to be processed; there is no asynchronicity
|
||||
# because filtering the logs in a synchronous way is easier and safer!
|
||||
@ -42,8 +68,7 @@ function _send_email() {
|
||||
# No. 2 is especially useful in case you send more than one email in a single
|
||||
# test file and need to assert certain log entries for each mail individually.
|
||||
#
|
||||
# @param ${1} = template file (path) name
|
||||
# @param ${2} = parameters for `nc` [OPTIONAL] (default: `0.0.0.0 25`)
|
||||
# This function takes the same arguments as `_send_mail`.
|
||||
#
|
||||
# ## Attention
|
||||
#
|
||||
@ -57,17 +82,13 @@ function _send_email() {
|
||||
# chosen. Sending more than one mail at any given point in time with this function
|
||||
# is UNDEFINED BEHAVIOR!
|
||||
function _send_email_and_get_id() {
|
||||
local TEMPLATE_FILE=${1:?Must provide name of template file}
|
||||
local NC_PARAMETERS=${2:-0.0.0.0 25}
|
||||
[[ -v CONTAINER_NAME ]] || return 1
|
||||
|
||||
_wait_for_empty_mail_queue_in_container
|
||||
_send_email "${@}"
|
||||
_wait_for_empty_mail_queue_in_container
|
||||
|
||||
local MAIL_ID
|
||||
|
||||
assert_not_equal "${NC_PARAMETERS}" ''
|
||||
assert_not_equal "${CONTAINER_NAME:-}" ''
|
||||
|
||||
_wait_for_empty_mail_queue_in_container
|
||||
_send_email "${TEMPLATE_FILE}"
|
||||
_wait_for_empty_mail_queue_in_container
|
||||
|
||||
# The unique ID Postfix (and other services) use may be different in length
|
||||
# on different systems (e.g. amd64 (11) vs aarch64 (10)). Hence, we use a
|
||||
# range to safely capture it.
|
||||
|
Reference in New Issue
Block a user