Files
ip/tests/test.sh
T
opencodecabilloteu 268d8d2b62
Docker Build and Push / lint (pull_request) Successful in 13s
Docker Build and Push / build (pull_request) Successful in 1m4s
Docker Build and Push / test (pull_request) Failing after 1h7m58s
Docker Build and Push / push (pull_request) Failing after 14m37s
fix: use container IP instead of port mapping for health check
When running inside Gitea Actions runner (Docker-in-Docker),
127.0.0.1:HOST_PORT doesn't work because the runner container
doesn't have access to the host's port mappings.
Use docker inspect to get the container's direct IP address.
2026-06-08 16:46:13 +00:00

73 lines
1.9 KiB
Bash
Executable File

#!/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 0:8080 "$IMAGE" >/dev/null
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$CONTAINER_NAME")
BASE_URL="http://${CONTAINER_IP}:8080"
echo "Waiting for container (ip $CONTAINER_IP)..."
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 /"
RESPONSE=$(curl -sf -D - "$BASE_URL/")
STATUS=$(echo "$RESPONSE" | head -1 | grep -oP '\d{3}')
CONTENT_TYPE=$(echo "$RESPONSE" | grep -i 'content-type' | tr -d '\r' | cut -d: -f2- | xargs)
BODY=$(echo "$RESPONSE" | tail -1)
assert "HTTP status is 200" "200" "$STATUS"
assert_match "Content-Type is application/json" "application/json" "$CONTENT_TYPE"
assert_match "Body contains valid IP" '^(\"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\"|\"[0-9a-fA-F:]+\")$' "$BODY"
echo ""
echo "Results: $PASSED/$TOTAL passed, $FAILED failed"
[ "$FAILED" -eq 0 ]