tests: handle multiple docker versions

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2024-11-20 00:59:09 +01:00
parent a6ef9db84d
commit a69d857b8a
7 changed files with 81 additions and 12 deletions

View File

@ -2,10 +2,14 @@ package workers
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/testutil/dockerd"
"github.com/moby/buildkit/util/testutil/integration"
bkworkers "github.com/moby/buildkit/util/testutil/workers"
"github.com/pkg/errors"
@ -13,18 +17,61 @@ import (
func InitDockerWorker() {
integration.Register(&dockerWorker{
id: "docker",
id: "docker",
binary: dockerd.DefaultDockerdBinary,
})
integration.Register(&dockerWorker{
id: "docker+containerd",
binary: dockerd.DefaultDockerdBinary,
containerdSnapshotter: true,
})
// e.g. `docker@26.0=/opt/docker-26.0,docker@25.0=/opt/docker-25.0`
if s := os.Getenv("TEST_DOCKER_EXTRA"); s != "" {
entries := strings.Split(s, ",")
for _, entry := range entries {
ver, bin, err := func(entry string) (string, string, error) {
p1 := strings.Split(strings.TrimSpace(entry), "=")
if len(p1) != 2 {
return "", "", errors.Errorf("invalid entry: %q", entry)
}
name, bin := p1[0], p1[1]
if _, err := os.Stat(bin); err != nil {
return "", "", errors.Wrapf(err, "bin not found: %q", bin)
}
p2 := strings.Split(strings.TrimSpace(name), "@")
if len(p2) != 2 {
return "", "", errors.Errorf("invalid name: %q", name)
}
_, ver := p2[0], p2[1]
if ver == "" {
return "", "", errors.New("empty version")
}
return ver, bin, nil
}(entry)
if err != nil {
panic(errors.Wrapf(err, "unexpected TEST_DOCKER_EXTRA: %q", s))
}
integration.Register(&dockerWorker{
id: fmt.Sprintf("docker@%s", ver),
binary: filepath.Join(bin, "dockerd"),
extraEnv: []string{fmt.Sprintf("PATH=%s:%s", bin, os.Getenv("PATH"))},
})
integration.Register(&dockerWorker{
id: fmt.Sprintf("docker+containerd@%s", ver),
binary: filepath.Join(bin, "dockerd"),
containerdSnapshotter: true,
extraEnv: []string{fmt.Sprintf("PATH=%s:%s", bin, os.Getenv("PATH"))},
})
}
}
}
type dockerWorker struct {
id string
binary string
containerdSnapshotter bool
unsupported []string
extraEnv []string
}
func (c dockerWorker) Name() string {
@ -42,7 +89,9 @@ func (c *dockerWorker) NetNSDetached() bool {
func (c dockerWorker) New(ctx context.Context, cfg *integration.BackendConfig) (b integration.Backend, cl func() error, err error) {
moby := bkworkers.Moby{
ID: c.id,
Binary: c.binary,
ContainerdSnapshotter: c.containerdSnapshotter,
ExtraEnv: c.extraEnv,
}
bk, bkclose, err := moby.New(ctx, cfg)
if err != nil {