beads/internal/testutil/dockercontext.go
Steve Yegge 7cd4246781
fix(test): pin DOCKER_HOST from the active docker context before TestMain swaps HOME (bd-84kos) (#5391)
* fix(test): pin DOCKER_HOST from the active docker context before TestMain swaps HOME (bd-84kos)

cmd/bd's testMainInner points HOME at a temp dir before any test runs,
which hides ~/.docker/config.json — the file holding the docker CLI's
active context. On machines where the daemon is reached via a context
(OrbStack, Docker Desktop's desktop-linux, remote daemons), 'docker
info' then falls back to unix:///var/run/docker.sock and fails, so
every container-gated test SKIPs 'Docker not available' even though
docker works fine in the invoking shell. Skips read as green, so the
proxied parity suites silently stopped running (bit bd-i8qx8).

Add testutil.PinDockerHostFromContext — resolve the active context's
endpoint ('docker context inspect -f {{.Endpoints.docker.Host}}') into
DOCKER_HOST, no-op when DOCKER_HOST is already set or docker is absent
— and call it in testMainInner just before the HOME swap.

Verified on macOS + OrbStack with no DOCKER_HOST in the environment:
TestProxiedServerExternalCreate skips 'Docker not available' on main,
and PASSes against a live container with this change.

* fix(test): harden PinDockerHostFromContext — 5s exec timeout + endpoint-shape check (bd-84kos)

Review hardenings: a pathological docker shim can no longer hang
TestMain (context inspect is local-metadata-only, so 5s is generous),
and a resolved value is only pinned when it is endpoint-shaped
(contains '://') — a shim printing something else degrades to no-op.
2026-08-07 00:32:40 -07:00

42 lines
1.5 KiB
Go

package testutil
import (
"context"
"os"
"os/exec"
"strings"
"time"
)
// PinDockerHostFromContext resolves the Docker CLI's active context endpoint
// and pins it into DOCKER_HOST, for TestMains that are about to swap HOME.
//
// The active context lives in ~/.docker/config.json; once a TestMain points
// HOME at a temp dir, `docker info` (and testcontainers) silently falls back
// to unix:///var/run/docker.sock. On machines where the daemon is reached via
// a context (OrbStack, Docker Desktop's desktop-linux, remote daemons) that
// socket does not exist, so every container-gated test SKIPs with "Docker not
// available" even though docker works fine in the invoking shell (bd-84kos).
//
// Call it BEFORE the HOME swap. No-op when DOCKER_HOST is already set (an
// explicit choice always wins) or when the endpoint cannot be resolved.
func PinDockerHostFromContext() {
if os.Getenv("DOCKER_HOST") != "" {
return
}
// context inspect reads local metadata only (never the daemon), so 5s is
// generous; the timeout guards against a pathological docker shim hanging
// TestMain.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "docker", "context", "inspect", "-f", "{{.Endpoints.docker.Host}}").Output()
if err != nil {
return
}
host := strings.TrimSpace(string(out))
if !strings.Contains(host, "://") {
// Not endpoint-shaped (empty, or a shim CLI printed something else).
return
}
_ = os.Setenv("DOCKER_HOST", host)
}