Files
cv/tests/test.sh
T
Sagent 7b8caa0463
PR Checks / build-test (pull_request) Successful in 21s
PR Checks / hadolint (pull_request) Successful in 9s
fix: migrate from jcabillot/phpapache to dunglas/frankenphp
2026-06-29 12:50:41 +00:00

88 lines
2.5 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
IMAGE="${1:?Usage: test.sh <image>}"
CONTAINER_NAME="test-$(echo "$IMAGE" | tr ':/' '-')-$$"
PASSED=0
FAILED=0
TOTAL=0
cleanup() {
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
}
trap cleanup EXIT
assert() {
local name="$1" expected="$2" actual="$3"
TOTAL=$((TOTAL + 1))
if [ "$expected" = "$actual" ]; then
echo " PASS: $name"
PASSED=$((PASSED + 1))
else
echo " FAIL: $name (expected: '$expected', got: '$actual')"
FAILED=$((FAILED + 1))
fi
}
assert_match() {
local name="$1" pattern="$2" actual="$3"
TOTAL=$((TOTAL + 1))
if echo "$actual" | grep -qE "$pattern"; then
echo " PASS: $name"
PASSED=$((PASSED + 1))
else
echo " FAIL: $name (pattern: '$pattern', got: '$actual')"
FAILED=$((FAILED + 1))
fi
}
echo "Running container: $IMAGE"
docker run -d --name "$CONTAINER_NAME" -p 8080:8080 "$IMAGE" >/dev/null
DOCKER_GW=$(docker network inspect bridge --format '{{range .IPAM.Config}}{{.Gateway}}{{end}}')
BASE_URL="http://${DOCKER_GW}:8080"
echo "Waiting for container on ${DOCKER_GW}:8080..."
for i in $(seq 1 30); do
if curl -sf "$BASE_URL/" >/dev/null 2>&1; then
echo "Container ready after ${i}s"
break
fi
if [ "$i" -eq 30 ]; then
echo "FAIL: Container did not become ready within 30s"
docker logs "$CONTAINER_NAME"
exit 1
fi
sleep 1
done
echo ""
echo "Test: GET / (HTML redirect)"
# Use temp file to avoid pipefail/SIGPIPE issues
TMPDIR=$(mktemp -d)
cleanup() {
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
rm -rf "$TMPDIR"
}
curl -sf -o "$TMPDIR/body" -D "$TMPDIR/headers" "$BASE_URL/"
STATUS=$(head -1 "$TMPDIR/headers" | grep -oP '\d{3}')
CONTENT_TYPE=$(grep -i 'content-type' "$TMPDIR/headers" | tr -d '\r' | cut -d: -f2- | xargs)
BODY=$(cat "$TMPDIR/body")
trap cleanup EXIT
assert "HTTP status is 200" "200" "$STATUS"
assert_match "Content-Type is text/html" "text/html" "$CONTENT_TYPE"
assert_match "Body contains redirect to CV" "cabillot_julien_cv.pdf" "$BODY"
echo ""
echo "Test: GET /cabillot_julien_cv.pdf"
STATUS=$(curl -sf -o /dev/null -w '%{http_code}' "$BASE_URL/cabillot_julien_cv.pdf")
CONTENT_TYPE=$(curl -sf -o /dev/null -D - "$BASE_URL/cabillot_julien_cv.pdf" | grep -i 'content-type' | tr -d '\r' | cut -d: -f2- | xargs)
assert "HTTP status is 200" "200" "$STATUS"
assert_match "Content-Type is application/pdf" "application/pdf" "$CONTENT_TYPE"
echo ""
echo "Results: $PASSED/$TOTAL passed, $FAILED failed"
[ "$FAILED" -eq 0 ]