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

other: test normalizations #3739

Closed
3 of 4 tasks
georglauterbach opened this issue Jan 1, 2024 · 2 comments · Fixed by #3747
Closed
3 of 4 tasks

other: test normalizations #3739

georglauterbach opened this issue Jan 1, 2024 · 2 comments · Fixed by #3747
Assignees
Labels
area/tests kind/improvement Improve an existing feature, configuration file or the documentation priority/medium
Milestone

Comments

@georglauterbach
Copy link
Member

georglauterbach commented Jan 1, 2024

Subject

I would like to contribute to the project

Description

We need a follow-up of #3732. Tasks:

  •  use .txt suffix for _send_email and _nc_wrapper
  •  in smtp_delivery.bats, remove the need for existing/userX and remove these files (ref: https://github.com/docker-mailserver/docker-mailserver/pull/3732/files#r1438975373)
  •  add a new helper _send_email_unchecked that works like _send_email but has no assert_success directly afterward to check that all went well and add such an assert_success in _send_email
  • use --auth PLAIN instead --auth LOGIN ?
@georglauterbach georglauterbach added the meta/help wanted The OP requests help from others - chime in! :D label Jan 1, 2024
@georglauterbach georglauterbach self-assigned this Jan 2, 2024
@georglauterbach georglauterbach added this to the v14.0.0 milestone Jan 2, 2024
@georglauterbach georglauterbach added priority/medium area/tests kind/improvement Improve an existing feature, configuration file or the documentation and removed meta/help wanted The OP requests help from others - chime in! :D labels Jan 2, 2024
@georglauterbach
Copy link
Member Author

georglauterbach commented Jan 2, 2024

Re-write _send_email_and_get_id

function _send_email_and_get_id() {
  export "${1:?Mail ID must be set for _send_email_and_get_id}"
  local -n MAIL_ID=${1}
  shift 1

  _wait_for_empty_mail_queue_in_container
  _send_email "${@}"
  _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.
  MAIL_ID=$(_exec_in_container tac /var/log/mail.log              \
    | grep -E -m 1 'postfix/smtpd.*: [A-Z0-9]+: client=localhost' \
    | grep -E -o '[A-Z0-9]{9,12}' || true)

  assert_not_equal "${MAIL_ID}" ''
}

This avoids subshells that mess with checks (e.g. this changes makes the final check assert_not_equal work properly).


Reminder to myself: this change is already stashed; just pop the stash.

@polarathene
Copy link
Member

polarathene commented Jan 21, 2024

Just going to dump some notes that have since gone a bit stale but were related to the changes PRs for this issue addressed.

No expectation for anyone to read through all this, but there may be something helpful when troubleshooting the changes associated to this issue.


Initial swaks PR change overview (Outdated)
  • Renames:
    • test/test-files/ renamed to test/files/
    • test/files/email-templates/ renamed to test/files/emails/
    • test/files/auth/ and test/files/emails/ restructured to use common file prefixes as folder names instead for grouping related files.
  • Added swaks package to replace nc where possible for sending email:
    • _send_email() adapted to swaks from nc, _nc_wrapper() helper provides equivalent support where swaks is not compatible.
    • test/files/emails/ removed SMTP commands, file contents only contain the DATA portion for providing to swaks via --data.
    • test/files/emails/ adds an nc_raw/ directory for files that are not swaks compatible, and thus must go through _nc_wrapper() instead of _send_email().
      • dsn/ is for dsn.bats, where swaks needs to support the equivalent of NOTIFY=success,failure for the RCPT TO SMTP command (--to in swaks).
      • postscreen.bats needs to test a misbehaving client that does not wait their turn, thus we use _nc_wrapper() here, since it sends all input to the destination at once.
      • permit_docker.bats relies on smtp-only.txt only as a generic mail to test toggling permission to send via network trust (PERMIT_DOCKER). Blocked by test file as not yet migrated to new testing format (required to access the _send_email helper).
    • test/files/auth/ no longer has SMTP Auth (adapted to swaks with --quit-after AUTH). Remaining files rely on _nc_wrapper() for IMAP / POP3 auth related testing.

TODO:

  • nc_raw/smtp-ehlo.txt: Not necessary, swaks --quit-after FIRST-HELO

Follow-up PRs

  • Mail intended to be submitted from a client to DMS (most over port 465 with auth): --ehlo mail.example.test + --from [email protected] + --protocol SSMTPA (port 465 with auth)
  • Mail intended to be delivered to DMS from an external MTA (most tests): --ehlo mail.external.tld + --from [email protected] + --protocol ESMTP (uses port 25 implicitly, this is the default --protocol value)
    • non-existing-user.txt: Removable, only needs to send a generic mail with --to [email protected].
    • amavis/spam.txt + rspamd/spam.txt can be replaced by a generic mail that sends the standard GTUBE with --from [email protected].
    • amavis/virus.txt + rspamd/virus.txt can likewise do the same with EICAR and --from [email protected].
    • emails/auth/ are all for testing the SPOOF_PROTECTION=1 feature.
      • These are specifically testing the authenticated DMS account against the envelope sender (--from).
      • Sending a generic mail is sufficient, while --auth-user / --from can be provided separately as overrides.
    • emails/existing/ are all generic (with the exception of the envelope-recipient via --to) for testing Postfix features related to routing mail delivery.
      • smtp_delivery.bats tests against each mail by asserting against their Subject header.
      • Additionally user-and-cc-local-alias.txt needs to append the CC header Cc: Existing Local Alias <[email protected]>
        • The alias (alias2) resolves to [email protected].
        • Alias resolution is asserted in test-case: redirects mail to external aliases, however that alias is used in another mail sent which makes the test-case not reliable for coverage on the CC header?
        • NOTE: The file name incorrectly has a local-alias suffix while the alias is to an external domain? (resolved since all files from this group were removed by a follow-up PR)
  • Docs permalink update: tests: Use swaks instead of nc for sending mail #3732 (comment)
  • Changing external.tld => external-third-party.test + localhost.localdomain => example.test, etc changes: tests: Use swaks instead of nc for sending mail #3732 (comment)
  • postgrey.bats whitelist test-case, investigate removing workaround with a separate client container and not sending mail direct to Postgrey port: tests: Use swaks instead of nc for sending mail #3732 (comment)
  • postscreen.bats split container (needs resolvable sender address domain, and likely TXT DNS record for SPF after that): tests: Use swaks instead of nc for sending mail #3732 (comment)
  • Instead of -tlsc (or the long option name --tls-on-connect), prefer setting --protocol with SSMTP or SSMTPA which is equivalent to specifying options --auth PLAIN --port 465 -tlsc. Although better alternative going forward would be to have a separate base config for swaks that includes the auth options. Usage in tests would then change to providing a different config with --config path/to/swaks/config/with/auth.

Maintenance notes

Unrelated to swaks migration:

References

May still have some relevance/value when refactoring existing tests and with recent changes this context would potentially be even less obvious and not fun to track down 😅

Original text files history:

local-alias.txt vs external-alias.txt (and similar variants where local/external is reversed in usage):

user-and-cc-local-alias.txt:

catchall-local.txt:

emails/root-email.txt:

existing/user1 was a common file used for generic mail testing. With the switch to swaks this is roughly the defaults, but there was still some inconsistencies (sometimes intentional by specifying defaults explicitly as options for visibility) vs how it used to be.


swaks related references

Most mail can use the following as base config and override via CLI options as needed (this was initial work towards advising a swaks config file for base/defaults):

# Envelope sender and recipient:
--from: [email protected]
--to: [email protected]
# Sender and recipient mail headers:
--h-From: "External mail client" <[email protected]>
--h-To: "Existing Local User" <[email protected]>

Prior to swaks we relied on netcat / nc to stream a series of SMTP commands with the common format / convention:

HELO mail.external.tld
MAIL FROM: [email protected]
RCPT TO: <recipient here>
DATA
<data content here>

.
QUIT

For context, the SMTP commands are removed in favor of retaining only the DATA content.

  • A PR introduced swaks with fallback config (helper method explicit CLI options with defaults) that represents the now dropped files most common SMTP commands.
  • The remaining content is fed into swaks command via --data and STDIN (a filename path should work too when prefixed with @).

We also switched to ESMTP with EHLO for all mail when adopting swaks, previously that was only used when necessary (eg: SMTP Auth).

When I was originally putting together a comment about suggesting to provide default swaks config via file (before the helper method with CLI options was proposed), I cited the swaks docs with (paraphrased):

Options can be given to swaks in three ways (config, env, CLI).

  • When swaks evaluates its options:
    • It first looks for a configuration file (either in a default location or specified with --config).
    • Then it evaluates any options in environment variables.
    • Finally, it evaluates command line options.
  • At each round of processing, any options set earlier will be overridden.
  • Additionally, any option can be prefixed with no- to cause swaks to forget that the variable had previously been set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/tests kind/improvement Improve an existing feature, configuration file or the documentation priority/medium
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants