From 601056f3a719a0fa4d437d07a7214bda9ba25e95 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Fri, 16 Jun 2023 11:34:55 +0100 Subject: [PATCH] tests: share single docker between docker-container backends This means that we can run our docker-container tests in parallel again, which can help speed up our test runs by a *significant* amount. Signed-off-by: Justin Chadwell --- tests/workers/docker-container.go | 37 ++++++++++++++++++++----------- tests/workers/docker.go | 2 +- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/tests/workers/docker-container.go b/tests/workers/docker-container.go index 86eeadac..b57c7e87 100644 --- a/tests/workers/docker-container.go +++ b/tests/workers/docker-container.go @@ -4,6 +4,7 @@ import ( "context" "os" "os/exec" + "sync" "github.com/moby/buildkit/identity" "github.com/moby/buildkit/util/testutil/integration" @@ -18,6 +19,11 @@ func InitDockerContainerWorker() { type containerWorker struct { id string + + docker integration.Backend + dockerClose func() error + dockerErr error + dockerOnce sync.Once } func (w *containerWorker) Name() string { @@ -29,9 +35,11 @@ func (w *containerWorker) Rootless() bool { } func (w *containerWorker) New(ctx context.Context, cfg *integration.BackendConfig) (integration.Backend, func() error, error) { - bk, bkclose, err := dockerWorker{id: w.id}.New(ctx, cfg) - if err != nil { - return bk, bkclose, err + w.dockerOnce.Do(func() { + w.docker, w.dockerClose, w.dockerErr = dockerWorker{id: w.id}.New(ctx, cfg) + }) + if w.dockerErr != nil { + return w.docker, w.dockerClose, w.dockerErr } name := "integration-container-" + identity.NewID() @@ -42,29 +50,32 @@ func (w *containerWorker) New(ctx context.Context, cfg *integration.BackendConfi "--driver=docker-container", "--driver-opt=network=host", ) - cmd.Env = append(os.Environ(), "DOCKER_CONTEXT="+bk.DockerAddress()) + cmd.Env = append(os.Environ(), "DOCKER_CONTEXT="+w.docker.DockerAddress()) if err := cmd.Run(); err != nil { return nil, nil, errors.Wrapf(err, "failed to create buildx instance %s", name) } cl := func() error { - var err error - if err1 := bkclose(); err == nil { - err = err1 - } cmd := exec.Command("buildx", "rm", "-f", name) - if err1 := cmd.Run(); err == nil { - err = err1 - } - return err + return cmd.Run() } return &backend{ - context: bk.DockerAddress(), + context: w.docker.DockerAddress(), builder: name, }, cl, nil } func (w *containerWorker) Close() error { + if close := w.dockerClose; close != nil { + return close() + } + + // reset the worker to be ready to go again + w.docker = nil + w.dockerClose = nil + w.dockerErr = nil + w.dockerOnce = sync.Once{} + return nil } diff --git a/tests/workers/docker.go b/tests/workers/docker.go index 486b92af..380190ac 100644 --- a/tests/workers/docker.go +++ b/tests/workers/docker.go @@ -42,7 +42,7 @@ func (c dockerWorker) New(ctx context.Context, cfg *integration.BackendConfig) ( "--docker", "host="+bk.DockerAddress(), ) if err := cmd.Run(); err != nil { - return nil, cl, errors.Wrapf(err, "failed to create buildx instance %s", name) + return bk, cl, errors.Wrapf(err, "failed to create buildx instance %s", name) } cl = func() error {