test(agent): validate startup dependency composition #343

Merged
arnaud.dartois merged 2 commits from test/260-agent-startup-di-container into main 2026-11-12 16:24:09 +01:00
2 changed files with 165 additions and 0 deletions

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
---
name: agent-startup-composition
description: Validate Heimdall Agent dependency-injection composition by starting its source-built container without a Central dependency.
---
# Agent startup composition
`src/Heimdall.Agent/Program.cs` constructs `SnapshotPipelineServiceOptions` in
the service-registration factory before registering the hosted service. Do not
register `SnapshotPipelineService` with the generic `AddHostedService<T>()`
unless every constructor dependency, including that options record, is
registered in DI. A missing dependency can prevent `Host.StartAsync` and leave
the Agent container unable to start.
## Acceptance
Run `bash skills/agent-startup-composition/acceptance.sh` from the repository
root. The recipe builds only the CPU Agent image from the current checkout with
a unique local tag and starts it in a disposable Compose project with generated
site-token data and an internal-only network. Its Central URL points to the
Agent container's own unused loopback port; it cannot reach a deployment.
The recipe proves that:
1. The Agent host starts and remains running with its production service
registrations, including `SnapshotPipelineService`.
2. `/health/live` responds successfully before registration with Central; the
readiness endpoint `/healthz` is intentionally not used because it reports
Unhealthy until Central registration succeeds.
3. Startup logs do not contain the missing
`SnapshotPipelineServiceOptions` dependency or a host-start failure.
4. Cleanup removes the exact Compose project, generated state volume/network,
and this recipe's one exact Agent image tag, then verifies their absence.
This checks DI and host startup only. It does not validate Central registration,
camera discovery, model inference, a published Agent, or a deployed installation.
The EXIT trap preserves the test result, attempts each cleanup independently,
and reports cleanup failures. The Dockerfile's shared .NET base images remain.
+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env bash
set -Eeuo pipefail
repo_root=$(git rev-parse --show-toplevel)
project="heimdall-agent-startup-$(date -u +%Y%m%d%H%M%S)-$(openssl rand -hex 3)"
image_tag="heimdall-agent:${project}-agent"
temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/${project}.XXXXXX")
env_file="$temp_dir/acceptance.env"
compose_file="$temp_dir/compose.yaml"
local_image_tags=("$image_tag")
cleanup() {
status=$?
trap - EXIT
cleanup_failed=0
docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \
down --volumes --remove-orphans >/dev/null 2>&1 || cleanup_failed=1
containers=
volumes=
networks=
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
[[ -z "${containers:-}" && -z "${volumes:-}" && -z "${networks:-}" ]] || cleanup_failed=1
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
cleanup_failed=1
fi
done
rm -f -- "$env_file" "$compose_file" || cleanup_failed=1
rmdir -- "$temp_dir" || cleanup_failed=1
if (( cleanup_failed )); then
printf 'Disposable Agent startup cleanup failed for project %s\n' "$project" >&2
status=1
else
printf 'Cleanup verified: project=%s; containers/volumes/networks absent; image tag %s absent.\n' \
"$project" "$image_tag"
fi
exit "$status"
}
trap cleanup EXIT
umask 077
cat >"$env_file" <<EOF
PROJECT=$project
SOURCE_ROOT=$repo_root
IMAGE_TAG=$image_tag
SYNTHETIC_SITE_TOKEN=$(openssl rand -hex 32)
EOF
cat >"$compose_file" <<'EOF'
services:
agent:
image: ${IMAGE_TAG}
build:
context: ${SOURCE_ROOT}
dockerfile: docker/Dockerfile.agent
network: host
restart: "no"
environment:
Agent__CentralUrl: http://127.0.0.1:1
Agent__AllowInsecureHttp: "true"
Agent__SiteToken: ${SYNTHETIC_SITE_TOKEN}
Agent__AgentName: ${PROJECT}
Agent__RegistrationMaxRetries: "20"
Agent__HeartbeatIntervalSeconds: "5"
Agent__DiscoveryEnabled: "false"
Agent__UpdateCheckIntervalHours: "0"
Agent__StateDirectory: /var/lib/heimdall
Sentry__Dsn: ""
volumes:
- agent_state:/var/lib/heimdall
networks:
- isolated
volumes:
agent_state:
networks:
isolated:
internal: true
EOF
docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \
up --build --detach agent
container_id=$(docker compose -p "$project" --env-file "$env_file" -f "$compose_file" ps -q agent)
[[ -n "$container_id" ]] || { printf 'Agent container was not created.\n' >&2; exit 1; }
deadline=$((SECONDS + 90))
live_ready=0
while (( SECONDS < deadline )); do
running=$(docker inspect --format '{{.State.Running}}' "$container_id" 2>/dev/null || true)
if [[ "$running" != true ]]; then
printf 'Agent container exited before its live endpoint became ready.\n' >&2
docker compose -p "$project" --env-file "$env_file" -f "$compose_file" ps >&2 || true
exit 1
fi
if docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \
exec -T agent wget -q --tries=1 --spider http://127.0.0.1:8080/health/live >/dev/null 2>&1; then
live_ready=1
break
fi
sleep 1
done
(( live_ready == 1 )) || { printf 'Agent /health/live did not become ready.\n' >&2; exit 1; }
restart_count=$(docker inspect --format '{{.RestartCount}}' "$container_id")
[[ "$restart_count" == 0 ]] || { printf 'Agent container restarted unexpectedly.\n' >&2; exit 1; }
if docker compose -p "$project" --env-file "$env_file" -f "$compose_file" logs agent 2>&1 |
rg -q "Unable to resolve service for type 'Heimdall\.Agent\.Services\.SnapshotPipelineServiceOptions'|Hosting failed to start"; then
printf 'Agent startup log contains a dependency-injection or host-start failure.\n' >&2
exit 1
fi
printf 'Agent host started; /health/live returned success; container remained running without restart.\n'
printf 'Project: %s\nLocal image tag: %s\n' "$project" "$image_tag"