test(image): add image test script

This commit is contained in:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 7ba68aba15
commit b5dc255e17
1 file changed
+280
+280
View File
@@ -0,0 +1,280 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2026 Bryan Joshua Pedini
set -eu
if [ "$#" -ne 3 ]; then
echo "usage: $0 <image> <version> <commit>" >&2
exit 2
fi
cd "$(dirname "$0")/.."
image=$1
version=$2
commit=$3
probe=${PROBE_IMAGE:-golang:1.27-alpine}
mysql_image=${MYSQL_IMAGE:-mysql:8.4}
name="cdwu-img-${IMG_ID:-$$}"
network="$name"
mysql="${name}-mysql"
server="${name}-app"
exported="${name}-export"
tmp=$(mktemp -d)
root_password=$(od -An -tx1 -N16 /dev/urandom | tr -d ' \n')
db_password=coredns_webui_dev
pw_admin="pw$(od -An -tx1 -N12 /dev/urandom | tr -d ' \n')"
pw_user="pw$(od -An -tx1 -N12 /dev/urandom | tr -d ' \n')"
pw_short="pw$(od -An -tx1 -N4 /dev/urandom | tr -d ' \n')"
rc=0
cleanup() {
status=$?
if [ "$status" -ne 0 ]; then
for container in "$server" "$mysql"; do
if docker inspect "$container" >/dev/null 2>&1; then
docker logs --tail 40 "$container" >&2 2>&1 || true
fi
done
fi
docker rm --force --volumes "$server" "$mysql" "$exported" >/dev/null 2>&1 || true
docker network rm "$network" >/dev/null 2>&1 || true
rm -rf "$tmp"
exit "$status"
}
trap cleanup EXIT
trap 'exit 130' INT TERM
fail() {
echo "FAIL: $*" >&2
exit 1
}
ok() {
echo "ok: $*"
}
label() {
docker image inspect --format "{{index .Config.Labels \"$1\"}}" "$image"
}
expect_rc() {
[ "$rc" -eq "$2" ] || fail "$1: exit code $rc, expected $2: $(head -c 300 "$tmp/err")"
}
capture() {
"$@" >"$tmp/out" 2>"$tmp/err" && rc=0 || rc=$?
}
scan() {
if grep -qF -e "$pw_admin" -e "$pw_user" -e "$pw_short" -e "$db_password" -e "$root_password" "$@"; then
fail "a secret appears in the output"
fi
}
cli() {
input=$1
shift
printf '%s\n' "$input" | docker exec -i "$server" /app "$@" >"$tmp/out" 2>"$tmp/err" && rc=0 || rc=$?
scan "$tmp/out" "$tmp/err"
}
sql() {
docker exec --env MYSQL_PWD="$root_password" "$mysql" mysql --user=root --batch --skip-column-names --execute "$1"
}
usage_text() {
cat <<'EOF'
usage:
coredns-web-ui start the HTTP server
coredns-web-ui help print this usage text
coredns-web-ui version print the version and commit
coredns-web-ui healthcheck query the local /health endpoint
coredns-web-ui user list [all] list the active users, or every user with all
coredns-web-ui user add <username> <role> create a user with role admin or user, reading the password from stdin
coredns-web-ui user passwd <username> set the password of a user, reading it from stdin
coredns-web-ui user role <username> <role> set the role of a user to admin or user
coredns-web-ui user delete <username> mark a user as deleted and end their sessions
coredns-web-ui check [zone] report the rows the plugin cannot serve correctly
coredns-web-ui audit [n] print the latest n audit rows (default 20)
EOF
}
usage_text >"$tmp/usage"
[ "$(label org.opencontainers.image.version)" = "$version" ] || fail "image version label is not $version"
[ "$(label org.opencontainers.image.revision)" = "$commit" ] || fail "image revision label is not $commit"
case $(label org.opencontainers.image.source) in
https://*/coredns-web-ui) ;;
*) fail "image source label is not a coredns-web-ui repository URL" ;;
esac
[ "$(label org.opencontainers.image.licenses)" = "GPL-2.0-or-later" ] || fail "image licenses label is not GPL-2.0-or-later"
ok "OCI labels"
[ "$(docker image inspect --format '{{.Config.User}}' "$image")" = "65532:65532" ] || fail "image user is not 65532:65532"
[ "$(docker image inspect --format '{{json .Config.Entrypoint}}' "$image")" = '["/app"]' ] || fail "image entrypoint is not /app"
[ "$(docker image inspect --format '{{json .Config.Healthcheck.Test}}' "$image")" = '["CMD","/app","healthcheck"]' ] || fail "image healthcheck is not /app healthcheck"
ok "image user, entrypoint and healthcheck"
capture docker run --rm --entrypoint /bin/sh "$image" -c true
[ "$rc" -ne 0 ] || fail "the image runs /bin/sh"
docker create --name "$exported" "$image" >/dev/null
docker export "$exported" | tar -t >"$tmp/files"
docker rm --force "$exported" >/dev/null
while IFS= read -r entry; do
case ${entry%/} in
app | etc | etc/ssl | etc/ssl/cert.pem | etc/hostname | etc/hosts | etc/resolv.conf | etc/mtab) ;;
dev | dev/console | dev/pts | dev/shm | proc | sys | .dockerenv) ;;
*) fail "unexpected path in the image filesystem: $entry" ;;
esac
done <"$tmp/files"
grep -qx 'app' "$tmp/files" || fail "/app missing from the image filesystem"
grep -qx 'etc/ssl/cert.pem' "$tmp/files" || fail "CA bundle missing from the image filesystem"
ok "image filesystem has no shell"
capture docker run --rm "$image"
expect_rc "start without configuration" 2
ok "exit 2 without configuration"
capture docker run --rm "$image" version
expect_rc "version" 0
[ "$(cat "$tmp/out")" = "coredns-web-ui $version $commit" ] || fail "version output is not 'coredns-web-ui $version $commit'"
ok "version output"
capture docker run --rm "$image" help
expect_rc "help" 0
cmp -s "$tmp/out" "$tmp/usage" || fail "help output differs from the usage text"
[ ! -s "$tmp/err" ] || fail "help wrote to stderr"
ok "help output"
for args in "bogus" "help extra"; do
capture docker run --rm "$image" $args
expect_rc "usage error ($args)" 2
[ ! -s "$tmp/out" ] || fail "usage error ($args) wrote to stdout"
head -n 1 "$tmp/err" | grep -q '^coredns-web-ui: usage error: ' || fail "usage error ($args) message has no usage error prefix"
tail -n +2 "$tmp/err" | cmp -s - "$tmp/usage" || fail "usage error ($args) does not end with the usage text"
done
ok "usage errors exit 2 with the usage text on stderr"
capture docker run --rm "$image" healthcheck
expect_rc "healthcheck without a server" 1
ok "healthcheck fails without a server"
docker network create --internal "$network" >/dev/null
docker create --name "$mysql" --network "$network" --network-alias mysql --tmpfs /var/lib/mysql \
--env MYSQL_ROOT_PASSWORD="$root_password" "$mysql_image" >/dev/null
docker cp dev/mysql-init.sql "$mysql:/docker-entrypoint-initdb.d/mysql-init.sql"
docker start "$mysql" >/dev/null
attempt=0
until docker exec --env MYSQL_PWD="$root_password" "$mysql" mysqladmin ping --host=127.0.0.1 --user=root --silent >/dev/null 2>&1; do
attempt=$((attempt + 1))
[ "$attempt" -lt 120 ] || fail "mysql did not become ready"
sleep 1
done
[ "$(sql 'SELECT COUNT(*) FROM coredns.coredns_records')" = 0 ] || fail "records table is not initialized"
ok "mysql ready"
docker run --detach --name "$server" --network "$network" --network-alias app \
--health-interval 2s --health-timeout 5s --health-start-period 2s --health-retries 5 \
--env DB_HOST=mysql --env DB_USER=coredns_webui --env DB_PASSWORD="$db_password" --env DB_NAME=coredns \
--env COOKIE_SECURE=false --env LOG_LEVEL=TRACE "$image" >/dev/null
attempt=0
while :; do
state=$(docker inspect --format '{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{end}}' "$server")
[ "$state" != "running healthy" ] || break
case $state in
running*) ;;
*) fail "the container is not running: $state" ;;
esac
attempt=$((attempt + 1))
[ "$attempt" -lt 90 ] || fail "the container did not become healthy: $state"
sleep 1
done
ok "container healthy through its HEALTHCHECK"
[ "$(docker inspect --format '{{.Config.User}}' "$server")" = "65532:65532" ] || fail "the container does not run as 65532:65532"
ok "container runs as 65532:65532"
capture docker exec "$server" /bin/sh -c true
[ "$rc" -ne 0 ] || fail "docker exec /bin/sh succeeded"
ok "docker exec /bin/sh fails"
capture docker exec "$server" /app healthcheck
expect_rc "docker exec healthcheck" 0
ok "docker exec healthcheck"
health=$(docker run --rm --network "$network" "$probe" wget -qO- http://app:8080/health) || fail "GET /health failed"
[ "$health" = '{"status":"ok"}' ] || fail "unexpected /health body: $health"
page=$(docker run --rm --network "$network" "$probe" wget -qO- http://app:8080/version) || fail "GET /version failed"
printf '%s' "$page" | grep -qF "$version" || fail "/version does not render the version"
printf '%s' "$page" | grep -qF "$commit" || fail "/version does not render the commit"
ok "/health and /version"
docker logs "$server" >"$tmp/startup.log" 2>&1
grep -qF "$commit" "$tmp/startup.log" || fail "commit $commit not in the startup log"
grep -qF "$version" "$tmp/startup.log" || fail "version $version not in the startup log"
ok "version and commit in the startup log"
cli "$pw_short" user add imgshort user
[ "$rc" -ne 0 ] || fail "user add accepted a short password"
[ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE username = 'imgshort'")" = 0 ] || fail "user add created a user with a short password"
cli "$pw_admin" user add imgadmin admin
expect_rc "user add admin" 0
cli "$pw_user" user add imguser user
expect_rc "user add user" 0
[ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imgadmin'")" = admin ] || fail "imgadmin does not have role admin"
[ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = user ] || fail "imguser does not have role user"
[ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE password_hash LIKE '\$argon2id\$%' AND deleted_at IS NULL")" = 2 ] || fail "passwords are not stored as argon2id hashes"
ok "user add with both roles"
cli "" user list
expect_rc "user list" 0
grep -q '^imgadmin ' "$tmp/out" || fail "user list does not show imgadmin"
grep -q '^imguser ' "$tmp/out" || fail "user list does not show imguser"
cli "" user role imguser admin
expect_rc "user role admin" 0
[ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = admin ] || fail "user role did not set admin"
cli "" user role imguser user
expect_rc "user role user" 0
[ "$(sql "SELECT role FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = user ] || fail "user role did not set user"
ok "user role"
cli "" user delete imguser
expect_rc "user delete" 0
[ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE username = 'imguser' AND deleted_at IS NOT NULL")" = 1 ] || fail "user delete did not leave the row with deleted_at set"
ok "user delete leaves the row with deleted_at"
cli "" user list
expect_rc "user list after delete" 0
if grep -q '^imguser ' "$tmp/out"; then
fail "user list shows a deleted user"
fi
cli "" user list all
expect_rc "user list all" 0
deleted_at=$(awk '$1 == "imguser" { print $NF }' "$tmp/out")
[ -n "$deleted_at" ] && [ "$deleted_at" != "-" ] || fail "user list all does not show the deleted user with its deletion time"
[ "$(awk '$1 == "imgadmin" { print $NF }' "$tmp/out")" = "-" ] || fail "user list all shows a deletion time for an active user"
ok "user list and user list all"
cli "$pw_user" user add imguser user
[ "$rc" -ne 0 ] || fail "user add accepted the username of a deleted user"
[ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_users WHERE username = 'imguser'")" = 1 ] || fail "user add changed the row of a deleted user"
ok "the username of a deleted user cannot be reused"
[ "$(sql "SELECT GROUP_CONCAT(operation ORDER BY id) FROM coredns.coredns_webui_audit")" = "user_create,user_create,user_role,user_role,user_delete" ] || fail "unexpected audit rows for the CLI changes"
[ "$(sql "SELECT COUNT(*) FROM coredns.coredns_webui_audit WHERE actor = 'cli' AND origin = 'local'")" = 5 ] || fail "CLI audit rows do not have actor cli and origin local"
ok "CLI changes are audited"
docker logs "$server" >"$tmp/server.log" 2>&1
scan "$tmp/server.log"
ok "no secret in the container logs"
started=$(date +%s)
docker stop --time 40 "$server" >/dev/null
elapsed=$(($(date +%s) - started))
[ "$(docker inspect --format '{{.State.ExitCode}}' "$server")" = 0 ] || fail "SIGTERM did not stop the container with exit code 0"
[ "$elapsed" -lt 25 ] || fail "SIGTERM took ${elapsed}s, longer than the 25s grace period"
docker logs "$server" >"$tmp/server.log" 2>&1
scan "$tmp/server.log"
ok "SIGTERM stops the container with exit 0 in ${elapsed}s"
echo "image test passed"