diff --git a/Makefile b/Makefile index b1c9e055..00122325 100644 --- a/Makefile +++ b/Makefile @@ -244,15 +244,6 @@ run: -e DMS_DEBUG=0 \ -h mail.my-domain.com -t $(NAME) sleep 15 - docker run -d --name mail_with_default_relay \ - -v "`pwd`/test/config/relay-hosts":/tmp/docker-mailserver \ - -v "`pwd`/test/test-files":/tmp/docker-mailserver-test:ro \ - -e DEFAULT_RELAY_HOST=default.relay.host.invalid:25 \ - --cap-add=SYS_PTRACE \ - -e PERMIT_DOCKER=host \ - -e DMS_DEBUG=0 \ - -h mail.my-domain.com -t $(NAME) - sleep 15 generate-accounts-after-run: docker run --rm -e MAIL_USER=added@localhost.localdomain -e MAIL_PASS=mypassword -t $(NAME) /bin/sh -c 'echo "$$MAIL_USER|$$(doveadm pw -s SHA512-CRYPT -u $$MAIL_USER -p $$MAIL_PASS)"' >> test/config/postfix-accounts.cf @@ -328,8 +319,7 @@ clean: mail_override_hostname \ mail_domainname \ mail_srs_domainname \ - mail_with_relays \ - mail_with_default_relay + mail_with_relays @if [ -d config.bak ]; then\ rm -rf config ;\ diff --git a/test/default_relay_host.bats b/test/default_relay_host.bats new file mode 100644 index 00000000..3798f639 --- /dev/null +++ b/test/default_relay_host.bats @@ -0,0 +1,26 @@ +load 'test_helper/common' + +function setup() { + docker run -d --name mail_with_default_relay \ + -v "`pwd`/test/config/relay-hosts":/tmp/docker-mailserver \ + -v "`pwd`/test/test-files":/tmp/docker-mailserver-test:ro \ + -e DEFAULT_RELAY_HOST=default.relay.host.invalid:25 \ + --cap-add=SYS_PTRACE \ + -e PERMIT_DOCKER=host \ + -e DMS_DEBUG=0 \ + -h mail.my-domain.com -t ${NAME} + wait_for_finished_setup_in_container mail_with_default_relay +} + +function teardown() { + docker rm -f mail_with_default_relay +} + +# +# default relay host +# + +@test "checking default relay host: default relay host is added to main.cf" { + run docker exec mail_with_default_relay /bin/sh -c 'grep -e "^relayhost = default.relay.host.invalid:25" /etc/postfix/main.cf | wc -l | grep 1' + assert_success +} \ No newline at end of file diff --git a/test/permit_docker.bats b/test/permit_docker.bats index 1f005750..4429ab11 100644 --- a/test/permit_docker.bats +++ b/test/permit_docker.bats @@ -1,8 +1,6 @@ -load 'test_helper/bats-support/load' -load 'test_helper/bats-assert/load' +load 'test_helper/common' NON_DEFAULT_DOCKER_MAIL_NETWORK_NAME=non-default-docker-mail-network -NAME=tvial/docker-mailserver:testing setup() { docker network create --driver bridge ${NON_DEFAULT_DOCKER_MAIL_NETWORK_NAME} docker network create --driver bridge ${NON_DEFAULT_DOCKER_MAIL_NETWORK_NAME}2 @@ -31,7 +29,7 @@ setup() { -t ${NAME} # wait until postfix is up - repeat_until_success_or_timeout 60 docker exec mail_smtponly_second_network /bin/sh -c "nc -z 0.0.0.0 25" + wait_for_smtp_port_in_container mail_smtponly_second_network } teardown() { @@ -41,20 +39,6 @@ teardown() { docker network rm ${NON_DEFAULT_DOCKER_MAIL_NETWORK_NAME} ${NON_DEFAULT_DOCKER_MAIL_NETWORK_NAME}2 } -function repeat_until_success_or_timeout { - TIMEOUT=$1 - STARTTIME=$SECONDS - shift 1 - until "$@" - do - sleep 5 - if [[ $(($SECONDS - $STARTTIME )) -gt $TIMEOUT ]]; then - echo "Timed out on command: $@" - exit 1 - fi - done -} - @test "checking PERMIT_DOCKER: connected-networks" { ipnet1=$(docker network inspect --format '{{(index .IPAM.Config 0).Subnet}}' non-default-docker-mail-network) ipnet2=$(docker network inspect --format '{{(index .IPAM.Config 0).Subnet}}' non-default-docker-mail-network2) diff --git a/test/test_helper/common.bash b/test/test_helper/common.bash new file mode 100644 index 00000000..3ee7c7d4 --- /dev/null +++ b/test/test_helper/common.bash @@ -0,0 +1,35 @@ +load 'test_helper/bats-support/load' +load 'test_helper/bats-assert/load' + +NAME=tvial/docker-mailserver:testing + +# default timeout is 60 seconds +TEST_TIMEOUT_IN_SECONDS=${TIMEOUT-60} + +function repeat_until_success_or_timeout { + if ![[ "$1" ~= '^[0-9]+$' ]]; then + echo "First parameter for timeout must be an integer, recieved \"$1\"" + exit 1 + fi + TIMEOUT=$1 + STARTTIME=$SECONDS + shift 1 + until "$@" + do + sleep 5 + if [[ $(($SECONDS - $STARTTIME )) -gt $TIMEOUT ]]; then + echo "Timed out on command: $@" + exit 1 + fi + done +} + +# @param $1 name of the postfix container +function wait_for_smtp_port_in_container() { + repeat_until_success_or_timeout $TEST_TIMEOUT_IN_SECONDS docker exec $1 /bin/sh -c "nc -z 0.0.0.0 25" +} + +# @param $1 name of the postfix container +function wait_for_finished_setup_in_container() { + repeat_until_success_or_timeout $TEST_TIMEOUT_IN_SECONDS sh -c "docker logs $1 | grep 'Starting mail server'" +} \ No newline at end of file diff --git a/test/tests.bats b/test/tests.bats index d3c90408..3d6629ed 100644 --- a/test/tests.bats +++ b/test/tests.bats @@ -1,5 +1,4 @@ -load 'test_helper/bats-support/load' -load 'test_helper/bats-assert/load' + # @@ -1816,14 +1815,7 @@ function count_processed_changes() { assert_success } -# -# default relay host -# -@test "checking default relay host: default relay host is added to main.cf" { - run docker exec mail_with_default_relay /bin/sh -c 'grep -e "^relayhost = default.relay.host.invalid:25" /etc/postfix/main.cf | wc -l | grep 1' - assert_success -} # # relay hosts