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 <me@jedevc.com>
(cherry picked from commit 601056f3a7)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
pull/1958/head
Justin Chadwell 2 years ago committed by Sebastiaan van Stijn
parent 0424ae14c0
commit 5657006c1f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

@ -4,6 +4,7 @@ import (
"context" "context"
"os" "os"
"os/exec" "os/exec"
"sync"
"github.com/moby/buildkit/identity" "github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/testutil/integration" "github.com/moby/buildkit/util/testutil/integration"
@ -18,6 +19,11 @@ func InitDockerContainerWorker() {
type containerWorker struct { type containerWorker struct {
id string id string
docker integration.Backend
dockerClose func() error
dockerErr error
dockerOnce sync.Once
} }
func (w *containerWorker) Name() string { 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) { 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) w.dockerOnce.Do(func() {
if err != nil { w.docker, w.dockerClose, w.dockerErr = dockerWorker{id: w.id}.New(ctx, cfg)
return bk, bkclose, err })
if w.dockerErr != nil {
return w.docker, w.dockerClose, w.dockerErr
} }
name := "integration-container-" + identity.NewID() name := "integration-container-" + identity.NewID()
@ -42,29 +50,32 @@ func (w *containerWorker) New(ctx context.Context, cfg *integration.BackendConfi
"--driver=docker-container", "--driver=docker-container",
"--driver-opt=network=host", "--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 { if err := cmd.Run(); err != nil {
return nil, nil, errors.Wrapf(err, "failed to create buildx instance %s", name) return nil, nil, errors.Wrapf(err, "failed to create buildx instance %s", name)
} }
cl := func() error { cl := func() error {
var err error
if err1 := bkclose(); err == nil {
err = err1
}
cmd := exec.Command("buildx", "rm", "-f", name) cmd := exec.Command("buildx", "rm", "-f", name)
if err1 := cmd.Run(); err == nil { return cmd.Run()
err = err1
}
return err
} }
return &backend{ return &backend{
context: bk.DockerAddress(), context: w.docker.DockerAddress(),
builder: name, builder: name,
}, cl, nil }, cl, nil
} }
func (w *containerWorker) Close() error { 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 return nil
} }

@ -42,7 +42,7 @@ func (c dockerWorker) New(ctx context.Context, cfg *integration.BackendConfig) (
"--docker", "host="+bk.DockerAddress(), "--docker", "host="+bk.DockerAddress(),
) )
if err := cmd.Run(); err != nil { 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 { cl = func() error {

Loading…
Cancel
Save