* 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.
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
//go:build !windows
|
|
|
|
package testutil
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestPinDockerHostFromContext_RespectsExistingValue(t *testing.T) {
|
|
t.Setenv("DOCKER_HOST", "unix:///explicit/choice.sock")
|
|
PinDockerHostFromContext()
|
|
if got := os.Getenv("DOCKER_HOST"); got != "unix:///explicit/choice.sock" {
|
|
t.Fatalf("DOCKER_HOST = %q, want the pre-set value untouched", got)
|
|
}
|
|
}
|
|
|
|
func TestPinDockerHostFromContext_ResolvesActiveContext(t *testing.T) {
|
|
dir := t.TempDir()
|
|
stub := filepath.Join(dir, "docker")
|
|
script := "#!/bin/sh\necho 'unix:///stub/context.sock'\n"
|
|
if err := os.WriteFile(stub, []byte(script), 0o755); err != nil {
|
|
t.Fatalf("writing docker stub: %v", err)
|
|
}
|
|
t.Setenv("PATH", dir)
|
|
t.Setenv("DOCKER_HOST", "")
|
|
_ = os.Unsetenv("DOCKER_HOST")
|
|
|
|
PinDockerHostFromContext()
|
|
if got := os.Getenv("DOCKER_HOST"); got != "unix:///stub/context.sock" {
|
|
t.Fatalf("DOCKER_HOST = %q, want the stub context endpoint", got)
|
|
}
|
|
}
|
|
|
|
func TestPinDockerHostFromContext_NoDockerIsANoOp(t *testing.T) {
|
|
t.Setenv("PATH", t.TempDir()) // no docker binary reachable
|
|
t.Setenv("DOCKER_HOST", "")
|
|
_ = os.Unsetenv("DOCKER_HOST")
|
|
|
|
PinDockerHostFromContext()
|
|
if got, set := os.LookupEnv("DOCKER_HOST"); set {
|
|
t.Fatalf("DOCKER_HOST = %q, want unset when docker is absent", got)
|
|
}
|
|
}
|