Files
Heimdall/scripts/acceptance/agent-heartbeat-timeout.sh
arnaud.dartois 5009c96120
Heimdall Gitea / Detect CI scope (push) Successful in 6s
Heimdall Gitea / Build and tests (push) Skipped
Heimdall Gitea / Build and scan server image (push) Skipped
Heimdall Gitea / Build and scan agent image (push) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (push) Skipped
Heimdall Gitea / Publish versioned installer release (push) Skipped
Heimdall Gitea / CI policy (push) Successful in 2s
docs: centralize Heimdall agent guidance (#418) (#420)
Co-authored-by: Arnaud Dartois <admin@tordais.cc>
2026-11-19 21:07:51 +01:00

189 lines
6.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
suffix="$(date -u +%Y%m%d%H%M%S)-$(od -An -N5 -tx1 /dev/urandom | tr -d ' \n')"
project="heimdall-heartbeat-${suffix}"
central_image="heimdall-heartbeat-mock:${suffix}"
agent_image="heimdall-agent:heartbeat-${suffix}"
temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/${project}.XXXXXX")
compose_file="$temp_dir/compose.yml"
env_file="$temp_dir/acceptance.env"
local_image_tags=("$agent_image" "$central_image")
cleanup() {
status=$?
trap - EXIT
cleanup_failed=0
docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \
down --volumes --remove-orphans || cleanup_failed=1
containers=$(docker ps -aq --filter "label=com.docker.compose.project=$project") || cleanup_failed=1
volumes=$(docker volume ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1
networks=$(docker network ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1
if [[ -n "$containers" || -n "$volumes" || -n "$networks" ]]; then
printf 'Project resource cleanup failed: containers=%s volumes=%s networks=%s\n' \
"${containers:-<none>}" "${volumes:-<none>}" "${networks:-<none>}" >&2
cleanup_failed=1
else
printf 'Verified no containers, volumes, or networks remain for %s\n' "$project"
fi
for tag in "${local_image_tags[@]}"; do
docker image rm "$tag" >/dev/null 2>&1 || true
if docker image inspect "$tag" >/dev/null 2>&1; then
printf 'Recipe image still exists: %s\n' "$tag" >&2
cleanup_failed=1
else
printf 'Verified recipe image absent: %s\n' "$tag"
fi
done
rm -rf -- "$temp_dir" || cleanup_failed=1
if (( cleanup_failed )); then
printf 'Disposable recipe cleanup failed for project %s\n' "$project" >&2
status=1
fi
exit "$status"
}
trap cleanup EXIT
site_token=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n')
cat >"$env_file" <<EOF
ACCEPTANCE_PROJECT=$project
ACCEPTANCE_AGENT_IMAGE=$agent_image
ACCEPTANCE_CENTRAL_IMAGE=$central_image
ACCEPTANCE_SITE_TOKEN=$site_token
EOF
mkdir -p "$temp_dir/mock-central"
cat >"$temp_dir/mock-central/Dockerfile" <<'EOF'
FROM python:3.13-alpine
WORKDIR /app
COPY mock_central.py .
CMD ["python", "-u", "/app/mock_central.py"]
EOF
cat >"$temp_dir/mock-central/mock_central.py" <<'PY'
import json
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
heartbeat_count = 0
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/healthz":
self.send_response(200)
self.end_headers()
elif self.path == "/counts":
payload = json.dumps({"heartbeats": heartbeat_count}).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
else:
self.send_error(404)
def do_POST(self):
global heartbeat_count
length = int(self.headers.get("Content-Length", "0"))
self.rfile.read(length)
if self.path == "/api/agents/register":
payload = json.dumps({
"agentId": "a583ec73-665f-4628-93e3-fcfe746a0e02",
"apiKey": "synthetic-acceptance-key",
"apiKeyPrefix": "synthetic",
}).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
elif self.path.endswith("/heartbeat"):
heartbeat_count += 1
if heartbeat_count == 1:
time.sleep(12)
self.send_response(200)
self.end_headers()
else:
self.send_error(404)
def log_message(self, *_args):
pass
ThreadingHTTPServer(("0.0.0.0", 8080), Handler).serve_forever()
PY
cat >"$compose_file" <<'YAML'
services:
fake-central:
image: ${ACCEPTANCE_CENTRAL_IMAGE}
build:
context: ./mock-central
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/healthz')"]
interval: 1s
timeout: 2s
retries: 20
agent:
image: ${ACCEPTANCE_AGENT_IMAGE}
build:
context: ${ACCEPTANCE_REPO_ROOT}
dockerfile: docker/Dockerfile.agent
environment:
ASPNETCORE_URLS: http://127.0.0.1:8081
Agent__CentralUrl: http://fake-central:8080
Agent__AllowInsecureHttp: "true"
Agent__SiteToken: ${ACCEPTANCE_SITE_TOKEN}
Agent__AgentName: heartbeat-acceptance
Agent__HeartbeatIntervalSeconds: "5"
Agent__PublicIpResolverUrl: ""
Agent__StateDirectory: /var/lib/heimdall
volumes:
- agent_state:/var/lib/heimdall
ports:
- "127.0.0.1::8081"
depends_on:
fake-central:
condition: service_healthy
volumes:
agent_state:
YAML
printf 'ACCEPTANCE_REPO_ROOT=%s\n' "$repo_root" >>"$env_file"
printf 'Disposable project: %s\n' "$project"
printf 'Locally built image tags: %s %s\n' "$agent_image" "$central_image"
docker compose -p "$project" --env-file "$env_file" -f "$compose_file" up -d --build
deadline=$((SECONDS + 45))
while (( SECONDS < deadline )); do
if docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \
exec -T agent wget -q --spider http://127.0.0.1:8081/healthz >/dev/null 2>&1; then
break
fi
sleep 1
done
if (( SECONDS >= deadline )); then
printf 'Agent health endpoint did not become available\n' >&2
exit 1
fi
deadline=$((SECONDS + 35))
while (( SECONDS < deadline )); do
counts=$(docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \
exec -T fake-central python -c \
'import json,urllib.request; print(json.load(urllib.request.urlopen("http://127.0.0.1:8080/counts"))["heartbeats"])')
if [[ "$counts" =~ ^[0-9]+$ ]] && (( counts >= 2 )); then
printf 'Acceptance passed: Agent remained healthy and fake Central observed %s heartbeats, including a retry after its first delayed response.\n' "$counts"
exit 0
fi
sleep 1
done
printf 'Acceptance failed: fake Central did not observe a heartbeat retry (last count: %s)\n' "${counts:-unknown}" >&2
docker compose -p "$project" --env-file "$env_file" -f "$compose_file" logs --no-color agent | tail -80 >&2 || true
exit 1