Add tests for test/common.bash

This commit is contained in:
Martin Schulze 2020-10-18 03:11:10 +02:00
parent 7fb13c18cd
commit 5030e8278c
2 changed files with 98 additions and 11 deletions

93
test/test_helper.bats Normal file
View file

@ -0,0 +1,93 @@
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
load 'test_helper/common'
@test "repeat_until_success_or_timeout returns instantly on success" {
SECONDS=0
repeat_until_success_or_timeout 1 true
[[ $SECONDS -le 1 ]]
}
@test "repeat_until_success_or_timeout waits for timeout on persistent failure" {
SECONDS=0
run repeat_until_success_or_timeout 2 false
[[ $SECONDS -ge 2 ]]
assert_failure
assert_output --partial "Timed out on command"
}
@test "repeat_until_success_or_timeout aborts immediately on fatal failure" {
SECONDS=0
run repeat_until_success_or_timeout --fatal-test false 2 false
[[ $SECONDS -le 1 ]]
assert_failure
assert_output --partial "early aborting"
}
@test "repeat_until_success_or_timeout expects integer timeout" {
run repeat_until_success_or_timeout 1 true
assert_success
run repeat_until_success_or_timeout timeout true
assert_failure
run repeat_until_success_or_timeout --fatal-test true timeout true
assert_failure
}
@test "run_until_success_or_timeout returns instantly on success" {
SECONDS=0
run_until_success_or_timeout 2 true
[[ $SECONDS -le 1 ]]
assert_success
}
@test "run_until_success_or_timeout waits for timeout on persistent failure" {
SECONDS=0
! run_until_success_or_timeout 2 false
[[ $SECONDS -ge 2 ]]
assert_failure
}
@test "repeat_in_container_until_success_or_timeout fails immediately for non-running container" {
SECONDS=0
! repeat_in_container_until_success_or_timeout 10 name-of-non-existing-container true
[[ $SECONDS -le 1 ]]
}
@test "repeat_in_container_until_success_or_timeout run command in container" {
local 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"
}
@test "container_is_running" {
local CONTAINER_NAME=$(docker run --rm -d alpine sleep 100)
container_is_running "$CONTAINER_NAME"
docker rm -f "$CONTAINER_NAME"
! container_is_running "$CONTAINER_NAME"
}
@test "wait_for_smtp_port_in_container aborts wait after timeout" {
local 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"
}
@test "wait_for_smtp_port_in_container returns immediately when port found" {
local CONTAINER_NAME=$(docker run --rm -d alpine sh -c "sleep 10")
docker exec "$CONTAINER_NAME" apk add netcat-openbsd
docker exec "$CONTAINER_NAME" nc -l 25 &
SECONDS=0
TEST_TIMEOUT_IN_SECONDS=5 run wait_for_smtp_port_in_container "$CONTAINER_NAME"
[[ $SECONDS -lt 5 ]]
assert_success
}

View file

@ -27,9 +27,9 @@ function repeat_until_success_or_timeout {
do
if [[ -n "${fatal_failure_test_command}" ]] && ! eval "${fatal_failure_test_command}"; then
echo "\`${fatal_failure_test_command}\` failed, early aborting repeat_until_success of \`${*}\`" >&2
exit 1
return 1
fi
sleep 5
sleep 1
if [[ $(( SECONDS - STARTTIME )) -gt ${TIMEOUT} ]]; then
echo "Timed out on command: ${*}" >&2
return 1
@ -51,7 +51,7 @@ function run_until_success_or_timeout {
until run "${@}" && [[ ${status} -eq 0 ]]
do
sleep 1
if [[ $(( SECONDS - STARTTIME )) -gt ${TIMEOUT} ]]; then
if (( SECONDS - STARTTIME > TIMEOUT )); then
echo "Timed out on command: ${*}" >&2
return 1
fi
@ -153,7 +153,8 @@ function private_config_path() {
# @param ${2} (optional) container name, defaults to ${BATS_TEST_FILENAME}
# @return path to the folder where the config is duplicated
function duplicate_config_for_container() {
local OUTPUT_FOLDER="$(private_config_path "${2}")"
local OUTPUT_FOLDER
OUTPUT_FOLDER="$(private_config_path "${2}")"
rm -rf "${OUTPUT_FOLDER:?}/" # cleanup
mkdir -p "${OUTPUT_FOLDER}"
cp -r "${PWD}/test/config/${1:?}/." "${OUTPUT_FOLDER}"
@ -173,13 +174,6 @@ function wait_for_service() {
container_has_service_running "${CONTAINER_NAME}" "${SERVICE_NAME}"
}
function count_processed_changes() {
local CONTAINER_NAME=${1}
docker exec "${CONTAINER_NAME}" cat /var/log/supervisor/changedetector.log | grep "Change detected" -c \
|| [[ ${?} == 1 ]] # don't error when no matches were found
}
function wait_for_changes_to_be_detected_in_container() {
local CONTAINER_NAME="${1}"
local TIMEOUT=${TEST_TIMEOUT_IN_SECONDS}