Skip the docker-backed service tests when there is no docker (#4799)

Let the docker-backed service tests be skipped with a flag.

TestMain called log.Fatalf when it could not reach a docker daemon, so
the whole package refused to run without one, including every test in it
that needs no container at all.

Record why docker is unavailable instead, and gate the tests that want a
container on it. A run asks to go without them with -docker=false;
otherwise a missing daemon still fails the package, so an unreachable
daemon stays a broken build rather than a run that quietly covers less
than the last one did.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alex Feldgendler
2026-08-26 10:32:15 -07:00
committed by GitHub
co-authored by Claude Opus 5
parent 3cfbd12426
commit 64a1211517
+33 -11
View File
@@ -16,6 +16,7 @@ package service_test
import (
"context"
"flag"
"fmt"
"log"
"net"
@@ -31,24 +32,43 @@ import (
var Docker dockertest.ClosablePool
func TestMain(m *testing.M) {
ctx := context.Background()
pool, err := dockertest.NewPool(ctx, "")
if err != nil {
log.Fatalf("Could not construct pool: %s", err)
}
// go test -docker=false ./pkg/service skips the tests that need a docker
// daemon, for a checkout without one. Running them is the default: a run that
// quietly covers less than the last one is worse than a run that stops, so a
// daemon that should be there and is not still fails the whole package.
var useDocker = flag.Bool("docker", true, "run the tests that need a docker daemon")
// uses pool to try to connect to Docker
_, err = pool.Client().Ping(ctx, mobyclient.PingOptions{})
if err != nil {
log.Fatalf("Could not connect to Docker: %s", err)
func TestMain(m *testing.M) {
// m.Run would parse them, but the flag is read before that
flag.Parse()
if *useDocker {
ctx := context.Background()
pool, err := dockertest.NewPool(ctx, "")
if err != nil {
log.Fatalf("Could not construct pool: %s", err)
}
// uses pool to try to connect to Docker
_, err = pool.Client().Ping(ctx, mobyclient.PingOptions{})
if err != nil {
log.Fatalf("Could not connect to Docker: %s", err)
}
Docker = pool
}
Docker = pool
code := m.Run()
os.Exit(code)
}
func requireDocker(t testing.TB) {
t.Helper()
if !*useDocker {
t.Skip("this test needs a docker daemon, and -docker=false says there is none")
}
}
func waitTCPPort(t testing.TB, addr string) {
if err := Docker.Retry(t.Context(), 30*time.Second, func() error {
conn, err := net.Dial("tcp", addr)
@@ -66,6 +86,8 @@ func waitTCPPort(t testing.TB, addr string) {
var redisLast atomic.Uint32
func runRedis(t testing.TB) string {
requireDocker(t)
c, err := Docker.Run(t.Context(),
"redis",
dockertest.WithName(fmt.Sprintf("lktest-redis-%d", redisLast.Inc())),