Skip to content

tests: add tests for helper function _add_to_or_update_postfix_main()#3505

Merged
casperklein merged 6 commits intodocker-mailserver:masterfrom
casperklein:new-tests
Sep 1, 2023
Merged

tests: add tests for helper function _add_to_or_update_postfix_main()#3505
casperklein merged 6 commits intodocker-mailserver:masterfrom
casperklein:new-tests

Conversation

@casperklein
Copy link
Copy Markdown
Member

@casperklein casperklein commented Aug 27, 2023

Description

Some tests for the _add_to_or_update_postfix_main() helper function.

Fixes #

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (non-breaking change that does improve existing functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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
  • I have made corresponding changes to the documentation (README.md or the documentation under docs/)
  • 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

@casperklein casperklein self-assigned this Aug 27, 2023
@casperklein casperklein added this to the v13.0.0 milestone Aug 27, 2023
Copy link
Copy Markdown
Member

@polarathene polarathene left a comment

Choose a reason for hiding this comment

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

Nice test, thanks for putting this together! 😁

I think we can make it a bit more DRY?

Comment thread test/tests/parallel/set3/scripts/postconf-helper.bats
Comment thread test/tests/parallel/set3/scripts/postconf-helper.bats Outdated
Comment thread test/tests/parallel/set3/scripts/postconf-helper.bats Outdated
Comment thread test/tests/parallel/set3/scripts/postconf-helper.bats Outdated
@polarathene
Copy link
Copy Markdown
Member

Actually, my bad. I forgot that our parallel tests only run entire test files concurrently, not their test cases, that will be sequential.

So it's only an issue when other tests have containers running that would affect each other, which barely applies to most of our tests.

@casperklein
Copy link
Copy Markdown
Member Author

All suggestions applied 👍

@casperklein casperklein marked this pull request as ready for review August 28, 2023 18:39
Comment on lines +4 to +59
BATS_TEST_NAME_PREFIX='[Scripts] (helper functions) (postfix.sh) (_add_to_or_update_postfix_main) '
CONTAINER_NAME='dms-test_postconf-helper'

# Various tests for the helper function _add_to_or_update_postfix_main()

function setup_file() {
_init_with_defaults
_common_container_setup

# remove 'relayhost' from main.cf
_run_in_container postconf -X relayhost
assert_success
}

function teardown_file() { _default_teardown ; }

# Add key 'relayhost' with a value to Postfix's main configuration file
# or update an existing key. An already existing key can be updated
# by either appending to the existing value (default) or by prepending.
#
# @param ${1} = new value (appended or prepended)
# @param ${2} = action "append" (default) or "prepend" [OPTIONAL]
function _modify_postfix_main.cf() {
_run_in_container_bash "source /usr/local/bin/helpers/{postfix,utils}.sh && _add_to_or_update_postfix_main relayhost '$1' '$2'"
_run_in_container grep "^relayhost" "/etc/postfix/main.cf"
}

@test "check if initial value is empty" {
_run_in_container postconf -h "relayhost"
assert_output ""
}

@test "add single value" {
_modify_postfix_main.cf "single-value-test"
assert_output "relayhost = single-value-test"
}

@test "prepend value" {
_modify_postfix_main.cf "prepend-test" "prepend"
assert_output "relayhost = prepend-test single-value-test"
}

@test "append value (explicit)" {
_modify_postfix_main.cf "append-test-explicit" "append"
assert_output "relayhost = prepend-test single-value-test append-test-explicit"
}

@test "append value (implicit)" {
_modify_postfix_main.cf "append-test-implicit"
assert_output "relayhost = prepend-test single-value-test append-test-explicit append-test-implicit"
}

@test "try to append already existing value" {
_modify_postfix_main.cf "append-test-implicit"
assert_output "relayhost = prepend-test single-value-test append-test-explicit append-test-implicit"
}
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.

TIL that you can use . in function names, but just because we can we probably shouldn't 😅

Not able to edit directly, so sending a full edit via suggestion:

Suggested change
BATS_TEST_NAME_PREFIX='[Scripts] (helper functions) (postfix.sh) (_add_to_or_update_postfix_main) '
CONTAINER_NAME='dms-test_postconf-helper'
# Various tests for the helper function _add_to_or_update_postfix_main()
function setup_file() {
_init_with_defaults
_common_container_setup
# remove 'relayhost' from main.cf
_run_in_container postconf -X relayhost
assert_success
}
function teardown_file() { _default_teardown ; }
# Add key 'relayhost' with a value to Postfix's main configuration file
# or update an existing key. An already existing key can be updated
# by either appending to the existing value (default) or by prepending.
#
# @param ${1} = new value (appended or prepended)
# @param ${2} = action "append" (default) or "prepend" [OPTIONAL]
function _modify_postfix_main.cf() {
_run_in_container_bash "source /usr/local/bin/helpers/{postfix,utils}.sh && _add_to_or_update_postfix_main relayhost '$1' '$2'"
_run_in_container grep "^relayhost" "/etc/postfix/main.cf"
}
@test "check if initial value is empty" {
_run_in_container postconf -h "relayhost"
assert_output ""
}
@test "add single value" {
_modify_postfix_main.cf "single-value-test"
assert_output "relayhost = single-value-test"
}
@test "prepend value" {
_modify_postfix_main.cf "prepend-test" "prepend"
assert_output "relayhost = prepend-test single-value-test"
}
@test "append value (explicit)" {
_modify_postfix_main.cf "append-test-explicit" "append"
assert_output "relayhost = prepend-test single-value-test append-test-explicit"
}
@test "append value (implicit)" {
_modify_postfix_main.cf "append-test-implicit"
assert_output "relayhost = prepend-test single-value-test append-test-explicit append-test-implicit"
}
@test "try to append already existing value" {
_modify_postfix_main.cf "append-test-implicit"
assert_output "relayhost = prepend-test single-value-test append-test-explicit append-test-implicit"
}
BATS_TEST_NAME_PREFIX='[Scripts] (helper functions) (postfix - _add_to_or_update_postfix_main) '
CONTAINER_NAME='dms-test_postconf-helper'
# Various tests for the helper function `_add_to_or_update_postfix_main()`
function setup_file() {
_init_with_defaults
_common_container_setup
# Begin tests without 'relayhost' defined in 'main.cf'
_run_in_container postconf -X relayhost
assert_success
}
function teardown_file() { _default_teardown ; }
# Add or modify in Postfix config `main.cf` a parameter key with the provided value.
# When the key already exists, the new value is appended (default), or prepended (explicitly requested).
# NOTE: This test-case helper is hard-coded for testing with the 'relayhost' parameter.
#
# @param ${1} = new value (appended or prepended)
# @param ${2} = action "append" (default) or "prepend" [OPTIONAL]
function _modify_postfix_main_config() {
_run_in_container_bash "source /usr/local/bin/helpers/{postfix,utils}.sh && _add_to_or_update_postfix_main relayhost '${1}' '${2}'"
_run_in_container grep '^relayhost' '/etc/postfix/main.cf'
}
@test "check if initial value is empty" {
_run_in_container postconf -h 'relayhost'
assert_output ''
}
@test "add single value" {
_modify_postfix_main_config 'single-value-test'
assert_output 'relayhost = single-value-test'
}
@test "prepend value" {
_modify_postfix_main_config 'prepend-test' 'prepend'
assert_output 'relayhost = prepend-test single-value-test'
}
@test "append value (explicit)" {
_modify_postfix_main_config 'append-test-explicit' 'append'
assert_output 'relayhost = prepend-test single-value-test append-test-explicit'
}
@test "append value (implicit)" {
_modify_postfix_main_config 'append-test-implicit'
assert_output 'relayhost = prepend-test single-value-test append-test-explicit append-test-implicit'
}
@test "try to append already existing value" {
_modify_postfix_main_config 'append-test-implicit'
assert_output 'relayhost = prepend-test single-value-test append-test-explicit append-test-implicit'
}

Revised some comments, changed the helper name to not use a ., and swapped double-quotes for single-quotes where appropriate. A slight change to the test prefix (which I still feel is quite lengthy).

Oh that diff is actually decent for once and self-explanatory 🤯

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.

Oh, seems I can't apply the suggestion directly. I was going to do that and approve, leaving you to revert anything or merge 🤷‍♂️

Apply what you're comfortable with and I'll approve 👍

Copy link
Copy Markdown
Member Author

@casperklein casperklein Sep 1, 2023

Choose a reason for hiding this comment

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

TIL that you can use . in function names, but just because we can we probably shouldn't 😅

I would have been surprised, if neither you or Georg would be triggered by that 😆

but just because we can we probably shouldn't

What's the problem? In that special case, I found it pretty suitable and precise. By calling the function, you exactly know, which file is modified.

PS: You can use a lot of characters for functions, even emoticons 😆 . The limitations from variable naming does not apply for functions. For example, @test is a perfectly valid function name.

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.

What's the problem?

My distrust / paranoia of it eventually contributing to some unexpected gotcha 😅

Probably some bias with it seeming off since a . usually has meaning elsewhere, especially other languages.

I know filenames can be problematic compatibility wise for example, and Apple allowed emoji when setting your password in the past but not inputting emoji for actual login, locking you out.

Emoji is actually a pretty bad one, have experienced my own gotchas when writing a hash function since some emoji can represent more than 20 bytes (rendered as one emoji but like a ligature is actually many separate emoji components, can trip up some text editors too).

So it's mostly down to playing it safe and predictable 😎

@georglauterbach georglauterbach changed the title tests: add tests for helper function _add_to_or_update_postfix_main() tests: add tests for helper function _add_to_or_update_postfix_main() Aug 29, 2023
@casperklein casperklein enabled auto-merge (squash) September 1, 2023 10:07
@casperklein casperklein merged commit 9578aa8 into docker-mailserver:master Sep 1, 2023
@casperklein casperklein deleted the new-tests branch September 1, 2023 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants