tests: add tests for helper function _add_to_or_update_postfix_main()#3505
tests: add tests for helper function _add_to_or_update_postfix_main()#3505casperklein merged 6 commits intodocker-mailserver:masterfrom
_add_to_or_update_postfix_main()#3505Conversation
|
Actually, my bad. I forgot that our 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. |
|
All suggestions applied 👍 |
| 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" | ||
| } |
There was a problem hiding this comment.
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:
| 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 🤯
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😎
_add_to_or_update_postfix_main()
Description
Some tests for the
_add_to_or_update_postfix_main()helper function.Fixes #
Type of change
Checklist:
docs/)