Files
docker-pi-hole/test/helpers.sh
Adam Warner acd1cf7014 refactor: move container lifecycle management into BATS test files
Container creation, waiting, and teardown previously lived in run.sh
and were passed to test_suite.bats via exported environment variables.
This scattered container lifecycle between two files and required
custom inline logic for tests that needed their own container.

Replace test_suite.bats with two focused files, each owning their
container via setup_file/teardown_file:

- test_default.bats: plain container; covers FTL binary, cron,
  password generation, and graceful shutdown (run last, sequentially)
- test_env_vars.bats: custom UID/GID, FTLCONF_ vars, ADDITIONAL_PACKAGES,
  and TAIL_FTL_LOG=0 all exercised in a single container

Extract start_container and wait_for_log into test/helpers.sh, which
each .bats file loads directly. run.sh now only builds the image,
installs BATS, and invokes the test files.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
2026-03-30 22:08:23 +01:00

28 lines
805 B
Bash

#!/usr/bin/env bash
# Shared container helper functions for BATS test files
start_container() {
local platform_args=()
[ -n "${CIPLATFORM:-}" ] && platform_args=(--platform "${CIPLATFORM}")
docker run -d -t "${platform_args[@]}" -e TZ="Europe/London" "$@" pihole:test
}
wait_for_log() {
local container="$1"
local pattern="$2"
local timeout=60
local elapsed=0
printf "Waiting for '%s' in %.30s... " "${pattern}" "${container}"
until docker logs "${container}" 2>&1 | grep -q "${pattern}"; do
sleep 1
elapsed=$(( elapsed + 1 ))
if (( elapsed >= timeout )); then
echo "TIMEOUT"
echo "--- Container logs ---"
docker logs "${container}"
return 1
fi
done
echo "ready (${elapsed}s)"
}