Skip to content

tests: Refactor LDAP tests to current conventions#3483

Merged
polarathene merged 12 commits intodocker-mailserver:masterfrom
polarathene:tests/ldap-refactor
Aug 17, 2023
Merged

tests: Refactor LDAP tests to current conventions#3483
polarathene merged 12 commits intodocker-mailserver:masterfrom
polarathene:tests/ldap-refactor

Conversation

@polarathene
Copy link
Copy Markdown
Member

@polarathene polarathene commented Aug 15, 2023

Description

Part of the big effort to migrate all tests to use the better helpers and conventions that support much better test maintenance (and a step towards better coverage).

  • Migration has been staged out via smaller commits for easier review of diff. Each commit contains a detailed message of changes it covers.
  • Various TODO have been added for future improvements.
  • Due to size of refactor (and relocation of files, which aren't as friendly with git blame history when changes are also applied), I've held off adjusting some email addresses (.tld / .localdomain) until after this PR is merged.
All commit messages for easier reference

tests: Switch to setup helper and conventions

tests: Adapt run command to new conventions

  • We have two helper methods with implicit CONTAINER_NAME reference, which is a bit more DRY and improves readability.
  • wc -l + assert_output 1 converted to use helper _should_output_number_of_lines 1
  • DOMAIN var changed from my-domain.com to local testing domain example.test.

tests: Refactor setup_file()

  • Test wide ENV defined at the top
  • OpenLDAP build and run logic grouped together. Added notes, network alias and tty not required.
  • Extracted out special LDAP Postfix/Dovecot ENV into separate array. LDAP specific provisioning / auth ENV also included, with comments + linebreak to better group related ENV.
  • Likewise additional ENV to support test cases has been extracted to a separate array with additional comments for context.
  • Those two arrays are expanded back into the main CUSTOM_SETUP_ARGUMENTS that configure hostname and network for the DMS container.

tests: Refactor the LDAP account table query testcase

  • Covers 3 accounts to test from LDAP.
    • 2 are the same query against users/aliases/groups tables in Postfix, only differing by account queried (and expected as returned result).
    • 1 separate test to ensure a difference in config is supported correctly.
  • Extracted repeated test logic into a helper method.
  • Added additional context in comments about the creation source of these LDAP accounts and their related Postfix config / interaction. Direct reference to special case PR (since git blame will be less useful).

tests: Use iteration for grep setting checks

  • More DRY approach. With a bit more helpful failure context via assert_output (and only grepping the key). Simpler to grok what's being covered.

tests: DRY test email delivery

  • A bit more verbose with the new helper method. test-email.txt template is only used by the LDAP test, as is the sendmail command.
  • Helper will take two args to support the testcases, but at a later date should be refactored to be consistent with the _send_email() helper (which presently uses nc that is required for plain-text mail/auth, otherwise we'd have used openssl, bigger refactor required).

tests: Slight revisions and relocating testcases

  • Dovecot quota plugin testcase revised to check files exist instead of rely on ls failure.
  • Moved Postfix quota plugin testcase into prior dovecot testcase for quota plugin check. Better error output by only querying the smtpd_recipient_restrictions setting (which should be the only one configured for the service).
  • Moved the saslauthd and pflogsumm testcases (no changes beyond revised comments) above the ATTENTION comment, and one testcase below the comment that belonged to that group.

tests: Simplify openldap docker build command

  • --no-cache was creating a new image on the Docker host each time the test is run. Shouldn't be any need to build without cache.
  • No need to use pushd + popd, can just provide the path context directly, and the ./Dockerfile is an implicit default thus -f not required either.
  • Additionally removed the old checking prefix from testcase names.

tests: Move LDAP specific config into test/config/ldap/

  • No changes to any of these config files, just better isolation as not relevant to any other tests.
  • Section heading in setup_file() added to distinguish the remainder of the function is dedicated to the DMS container setup.
  • Comment providing some context about the mv to maintainers, this should be done after defaults are initialized but before starting up the container.

Type of change

  • Improvement (non-breaking change that does improve existing functionality)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • If necessary I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

- We have two helper methods with implicit `CONTAINER_NAME` reference, which is a bit more DRY and improves readability.
- `wc -l` + `assert_output 1` converted to use helper `_should_output_number_of_lines 1`
- `DOMAIN` var changed from `my-domain.com` to local testing domain `example.test`.
- Test wide ENV defined at the top
- OpenLDAP build and run logic grouped together. Added notes, network alias and tty not required.
- Extracted out special LDAP Postfix/Dovecot ENV into separate array. LDAP specific provisioning / auth ENV also included, with comments + linebreak to better group related ENV.
- Likewise additional ENV to support test cases has been extracted to a separate array with additional comments for context.
- Those two arrays are expanded back into the main `CUSTOM_SETUP_ARGUMENTS` that configure hostname and network for the DMS container.
- Covers 3 accounts to test from LDAP.
  - 2 are the same query against users/aliases/groups tables in Postfix, only differing by account queried (and expected as returned result).
  - 1 separate test to ensure a difference in config is supported correctly.
- Extracted repeated test logic into a helper method.
- Added additional context in comments about the creation source of these LDAP accounts and their related Postfix config / interaction. Direct reference to special case PR (since `git blame` will be less useful).
More DRY approach. With a bit more helpful failure context via `assert_output` (_and only grepping the key_). Simpler to grok what's being covered.
A bit more verbose with the new helper method. `test-email.txt` template is only used by the LDAP test, as is the `sendmail` command.

Helper will take two args to support the testcases, but at a later date should be refactored to be consistent with the `_send_email()` helper (_which presently uses `nc` that is required for plain-text mail/auth, otherwise we'd have used `openssl`, bigger refactor required_).
- Dovecot quota plugin testcase revised to check files exist instead of rely on `ls` failure.
- Moved Postfix quota plugin testcase into prior dovecot testcase for quota plugin check. Better error output by only querying the `smtpd_recipient_restrictions` setting (_which should be the only one configured for the service_).
- Moved the saslauthd and pflogsumm testcases (_no changes beyond revised comments_) above the `ATTENTION` comment, and one testcase below the comment that belonged to that group.
- `--no-cache` was creating a new image on the Docker host each time the test is run. Shouldn't be any need to build without cache.
- No need to use `pushd` + `popd`, can just provide the path context directly, and the `./Dockerfile` is an implicit default thus `-f` not required either.

Additionally removed the old `checking` prefix from testcase names.
- No changes to any of these config files, just better isolation as not relevant to any other tests.
- Section heading in `setup_file()` added to distinguish the remainder of the function is dedicated to the DMS container setup.
- Comment providing some context about the `mv` to maintainers, this should be done after defaults are initialized but before starting up the container.
@polarathene polarathene added area/tests service/ldap kind/improvement Improve an existing feature, configuration file or the documentation labels Aug 15, 2023
@polarathene polarathene added this to the v13.0.0 milestone Aug 15, 2023
@polarathene polarathene self-assigned this Aug 15, 2023
Copy link
Copy Markdown
Member

@georglauterbach georglauterbach left a comment

Choose a reason for hiding this comment

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

Looks good at first glance, but I will give it a more thorough review as soon as I have a few minutes to spare again. I will not be blocking on this though, so earlier merging without my review is fine for me.

Copy link
Copy Markdown
Member

@georglauterbach georglauterbach left a comment

Choose a reason for hiding this comment

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

Looks good; just some minor changes.

# Setup local openldap service:
# NOTE: Building via Dockerfile is required? Image won't accept read-only if it needs to adjust permissions for bootstrap files.
# TODO: Upstream image is no longer maintained, may want to migrate?
docker build -t dms-openldap test/config/ldap/docker-openldap/
Copy link
Copy Markdown
Member

@georglauterbach georglauterbach Aug 15, 2023

Choose a reason for hiding this comment

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

Just FYI: I have been wondering for a long time why we build this ourselves.. I do like that we have a TODO item here now saying that we should do something about the issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I got a bit familiar with it while working through the refactoring of the test.

I tried a few other openldap images, but ran into problems carrying over the config .ldif + .schema we have. I'll give it a bit more time today but call it quits if I can't figure out a suitable replacement.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍🏼

Comment thread test/tests/serial/mail_with_ldap.bats Outdated
Comment thread test/tests/serial/mail_with_ldap.bats Outdated
Comment thread test/tests/serial/mail_with_ldap.bats Outdated
run docker exec mail_with_ldap ls /etc/dovecot/conf.d/90-quota.conf.disab
assert_success
}
_run_in_container_bash 'openssl s_client -quiet -connect 0.0.0.0:465 < /tmp/docker-mailserver-test/auth/sasl-ldap-smtp-auth.txt'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just as a reminder for the future: _send_email should probably get a "ports" option and choose between openssl s_client and nc dynamically. Or, simpler, we provide a new helper _send_mail_openssl().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We could, but instead of switching between openssl / nc it might also be good to just use a CLI tool that sends mail and can support all ports?

Preferably it'd ingest a different mail template, I'd prefer we handle the auth ones with the account password in plaintext, instead of the encoded (hashed? base64?) format exchanged presently for AUTH LOGIN. We have an issue with that for the skipped test, and the .ldif files have SSHA hashed passwords themselves, so I've no clue what they were originally 😅

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, that might also be a good idea. But no rush, it works fine for now

Co-authored-by: Georg Lauterbach <[email protected]>
@polarathene polarathene merged commit 5bada0a into docker-mailserver:master Aug 17, 2023
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 service/ldap

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants