From 64a1211517776c37c47751d9acba1c774ba2892e Mon Sep 17 00:00:00 2001 From: Alex Feldgendler Date: Wed, 26 Aug 2026 20:32:15 +0300 Subject: [PATCH] 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) --- pkg/service/docker_test.go | 44 ++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/pkg/service/docker_test.go b/pkg/service/docker_test.go index 9fc5b90b..df25e432 100644 --- a/pkg/service/docker_test.go +++ b/pkg/service/docker_test.go @@ -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())),