2022-11-25 22:37:58 +00:00
|
|
|
load "${REPOSITORY_ROOT}/test/test_helper/common"
|
2020-10-18 01:11:10 +00:00
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
BATS_TEST_NAME_PREFIX='test helper functions:'
|
2022-11-25 21:59:14 +00:00
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "repeat_until_success_or_timeout returns instantly on success" {
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
repeat_until_success_or_timeout 1 true
|
|
|
|
[[ ${SECONDS} -le 1 ]]
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "repeat_until_success_or_timeout waits for timeout on persistent failure" {
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
run repeat_until_success_or_timeout 2 false
|
|
|
|
[[ ${SECONDS} -ge 2 ]]
|
|
|
|
assert_failure
|
|
|
|
assert_output --partial "Timed out on command"
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "repeat_until_success_or_timeout aborts immediately on fatal failure" {
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
run repeat_until_success_or_timeout --fatal-test false 2 false
|
|
|
|
[[ ${SECONDS} -le 1 ]]
|
|
|
|
assert_failure
|
|
|
|
assert_output --partial "early aborting"
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "repeat_until_success_or_timeout expects integer timeout" {
|
2022-05-30 00:53:30 +00:00
|
|
|
run repeat_until_success_or_timeout 1 true
|
|
|
|
assert_success
|
2020-10-18 01:11:10 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
run repeat_until_success_or_timeout timeout true
|
|
|
|
assert_failure
|
2020-10-18 13:44:01 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
run repeat_until_success_or_timeout --fatal-test true timeout true
|
|
|
|
assert_failure
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "run_until_success_or_timeout returns instantly on success" {
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
run_until_success_or_timeout 2 true
|
|
|
|
[[ ${SECONDS} -le 1 ]]
|
|
|
|
assert_success
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "run_until_success_or_timeout waits for timeout on persistent failure" {
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
! run_until_success_or_timeout 2 false
|
|
|
|
[[ ${SECONDS} -ge 2 ]]
|
|
|
|
assert_failure
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "repeat_in_container_until_success_or_timeout fails immediately for non-running container" {
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
! repeat_in_container_until_success_or_timeout 10 name-of-non-existing-container true
|
|
|
|
[[ ${SECONDS} -le 1 ]]
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "repeat_in_container_until_success_or_timeout run command in container" {
|
2022-05-30 00:53:30 +00:00
|
|
|
local CONTAINER_NAME
|
|
|
|
CONTAINER_NAME=$(docker run --rm -d alpine sleep 100)
|
|
|
|
SECONDS=0
|
|
|
|
! repeat_in_container_until_success_or_timeout 10 "${CONTAINER_NAME}" sh -c "echo '${CONTAINER_NAME}' > /tmp/marker"
|
|
|
|
[[ ${SECONDS} -le 1 ]]
|
|
|
|
run docker exec "${CONTAINER_NAME}" cat /tmp/marker
|
|
|
|
assert_output "${CONTAINER_NAME}"
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "container_is_running" {
|
2022-05-30 00:53:30 +00:00
|
|
|
local CONTAINER_NAME
|
|
|
|
CONTAINER_NAME=$(docker run --rm -d alpine sleep 100)
|
|
|
|
container_is_running "${CONTAINER_NAME}"
|
|
|
|
docker rm -f "${CONTAINER_NAME}"
|
|
|
|
! container_is_running "${CONTAINER_NAME}"
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "wait_for_smtp_port_in_container aborts wait after timeout" {
|
2022-05-30 00:53:30 +00:00
|
|
|
local CONTAINER_NAME
|
|
|
|
CONTAINER_NAME=$(docker run --rm -d alpine sleep 100)
|
|
|
|
SECONDS=0
|
|
|
|
TEST_TIMEOUT_IN_SECONDS=2 run wait_for_smtp_port_in_container "${CONTAINER_NAME}"
|
|
|
|
[[ ${SECONDS} -ge 2 ]]
|
|
|
|
assert_failure
|
|
|
|
assert_output --partial "Timed out on command"
|
2020-10-18 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
# NOTE: Test requires external network access available
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "wait_for_smtp_port_in_container returns immediately when port found" {
|
2022-05-30 00:53:30 +00:00
|
|
|
local CONTAINER_NAME
|
2023-01-04 15:24:08 +00:00
|
|
|
CONTAINER_NAME=$(docker run --rm -d alpine sh -c "sleep 100")
|
2020-10-18 01:11:10 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
docker exec "${CONTAINER_NAME}" apk add netcat-openbsd
|
|
|
|
docker exec "${CONTAINER_NAME}" nc -l 25 &
|
2020-10-18 01:11:10 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
TEST_TIMEOUT_IN_SECONDS=5 run wait_for_smtp_port_in_container "${CONTAINER_NAME}"
|
|
|
|
[[ ${SECONDS} -lt 5 ]]
|
|
|
|
assert_success
|
2020-10-18 13:44:01 +00:00
|
|
|
}
|
2020-10-19 21:33:54 +00:00
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "wait_for_finished_setup_in_container" {
|
2022-05-30 00:53:30 +00:00
|
|
|
# variable not local to make visible to teardown
|
|
|
|
local PRIVATE_CONFIG
|
|
|
|
PRIVATE_CONFIG=$(duplicate_config_for_container .)
|
|
|
|
|
|
|
|
CONTAINER_NAME=$(docker run -d --rm \
|
|
|
|
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
|
|
|
|
-h mail.my-domain.com \
|
|
|
|
-t "${NAME}")
|
|
|
|
|
|
|
|
teardown() { docker rm -f "${CONTAINER_NAME}"; }
|
|
|
|
|
|
|
|
# the setup should not be finished immediately after starting
|
|
|
|
! TEST_TIMEOUT_IN_SECONDS=0 wait_for_finished_setup_in_container "${CONTAINER_NAME}"
|
|
|
|
|
|
|
|
# but it will finish eventually
|
|
|
|
SECONDS=1
|
|
|
|
|
|
|
|
wait_for_finished_setup_in_container "${CONTAINER_NAME}"
|
|
|
|
[[ ${SECONDS} -gt 0 ]]
|
2020-10-19 22:50:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "duplicate_config_for_container" {
|
2022-05-30 00:53:30 +00:00
|
|
|
local path
|
|
|
|
path=$(duplicate_config_for_container duplicate_config_test)
|
2020-10-19 22:50:19 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
run cat "${path}/marker"
|
|
|
|
assert_line "This marker file is there to identify the correct config being copied"
|
2020-10-19 22:50:19 +00:00
|
|
|
|
2023-01-11 00:31:21 +00:00
|
|
|
run duplicate_config_for_container non-existent-source-folder "${BATS_TEST_NAME}2"
|
2022-05-30 00:53:30 +00:00
|
|
|
assert_failure
|
2020-10-19 22:50:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "container_has_service_running/wait_for_service" {
|
2022-05-30 00:53:30 +00:00
|
|
|
local PRIVATE_CONFIG
|
|
|
|
PRIVATE_CONFIG=$(duplicate_config_for_container .)
|
|
|
|
|
|
|
|
# variable not local to make visible to teardown
|
|
|
|
CONTAINER_NAME=$(docker run -d --rm \
|
|
|
|
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
|
|
|
|
-h mail.my-domain.com \
|
|
|
|
-t "${NAME}")
|
|
|
|
|
|
|
|
teardown() { docker rm -f "${CONTAINER_NAME}"; }
|
|
|
|
|
|
|
|
# pick a service that was not started
|
|
|
|
! container_has_service_running "${CONTAINER_NAME}" clamav
|
|
|
|
|
|
|
|
# wait for a service that should be started
|
|
|
|
wait_for_service "${CONTAINER_NAME}" postfix
|
|
|
|
|
|
|
|
# shut down the service
|
|
|
|
docker exec "${CONTAINER_NAME}" supervisorctl stop postfix
|
|
|
|
|
|
|
|
# now it should be off
|
|
|
|
SECONDS=0
|
|
|
|
TEST_TIMEOUT_IN_SECONDS=5 run wait_for_service "${CONTAINER_NAME}" postfix
|
|
|
|
[[ ${SECONDS} -ge 5 ]]
|
|
|
|
assert_failure
|
2020-10-19 22:50:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 12:43:42 +00:00
|
|
|
# TODO investigate why this test fails
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "wait_for_empty_mail_queue_in_container fails when timeout reached" {
|
2022-05-30 00:53:30 +00:00
|
|
|
skip 'disabled as it fails randomly: https://github.com/docker-mailserver/docker-mailserver/pull/2177'
|
2021-09-11 09:30:01 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
local PRIVATE_CONFIG
|
|
|
|
PRIVATE_CONFIG=$(duplicate_config_for_container .)
|
2020-11-05 12:32:42 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
# variable not local to make visible to teardown
|
|
|
|
# enable ClamAV to make message delivery slower, so we can detect it
|
|
|
|
CONTAINER_NAME=$(docker run -d --rm \
|
|
|
|
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
|
|
|
|
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \
|
|
|
|
-e ENABLE_CLAMAV=1 \
|
|
|
|
-h mail.my-domain.com \
|
|
|
|
-t "${NAME}")
|
2020-10-19 21:33:54 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
teardown() { docker rm -f "${CONTAINER_NAME}"; }
|
2020-10-19 21:33:54 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
wait_for_smtp_port_in_container "${CONTAINER_NAME}" || docker logs "${CONTAINER_NAME}"
|
2020-10-19 21:33:54 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
SECONDS=0
|
|
|
|
# no mails -> should return immediately
|
|
|
|
TEST_TIMEOUT_IN_SECONDS=5 wait_for_empty_mail_queue_in_container "${CONTAINER_NAME}"
|
|
|
|
[[ ${SECONDS} -lt 5 ]]
|
2020-11-05 12:32:42 +00:00
|
|
|
|
2022-05-30 00:53:30 +00:00
|
|
|
# fill the queue with a message
|
|
|
|
docker exec "${CONTAINER_NAME}" /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-virus.txt"
|
|
|
|
|
|
|
|
# that should still be stuck in the queue
|
|
|
|
! TEST_TIMEOUT_IN_SECONDS=0 wait_for_empty_mail_queue_in_container "${CONTAINER_NAME}"
|
2020-10-19 22:50:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 12:43:42 +00:00
|
|
|
# TODO investigate why this test fails
|
2023-01-09 07:54:04 +00:00
|
|
|
@test "wait_for_empty_mail_queue_in_container succeeds within timeout" {
|
2022-05-30 00:53:30 +00:00
|
|
|
skip 'disabled as it fails randomly: https://github.com/docker-mailserver/docker-mailserver/pull/2177'
|
|
|
|
|
|
|
|
local PRIVATE_CONFIG
|
|
|
|
PRIVATE_CONFIG=$(duplicate_config_for_container .)
|
|
|
|
|
|
|
|
# variable not local to make visible to teardown
|
|
|
|
# enable ClamAV to make message delivery slower, so we can detect it
|
|
|
|
CONTAINER_NAME=$(docker run -d --rm \
|
|
|
|
-v "${PRIVATE_CONFIG}":/tmp/docker-mailserver \
|
|
|
|
-v "$(pwd)/test/test-files":/tmp/docker-mailserver-test:ro \
|
|
|
|
-e ENABLE_CLAMAV=1 \
|
|
|
|
-h mail.my-domain.com \
|
|
|
|
-t "${NAME}")
|
|
|
|
|
|
|
|
teardown() { docker rm -f "${CONTAINER_NAME}"; }
|
|
|
|
|
|
|
|
wait_for_smtp_port_in_container "${CONTAINER_NAME}" || docker logs "${CONTAINER_NAME}"
|
|
|
|
|
|
|
|
# fill the queue with a message
|
|
|
|
docker exec "${CONTAINER_NAME}" /bin/sh -c "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-virus.txt"
|
|
|
|
|
|
|
|
# give it some time to clear the queue
|
|
|
|
SECONDS=0
|
|
|
|
wait_for_empty_mail_queue_in_container "${CONTAINER_NAME}"
|
|
|
|
[[ ${SECONDS} -gt 0 ]]
|
2020-10-19 21:33:54 +00:00
|
|
|
}
|