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

@ -505,7 +505,7 @@ RUN exit 1`)
func testBuildProgress(t *testing.T, sb integration.Sandbox) {
dir := createTestProject(t)
sbDriver, _ := driverName(sb.Name())
sbDriver, _, _ := driverName(sb.Name())
name := sb.Address()
// progress=tty

View File

@ -45,7 +45,7 @@ func testInspect(t *testing.T, sb integration.Sandbox) {
}
require.Equal(t, sb.Address(), name)
sbDriver, _ := driverName(sb.Name())
sbDriver, _, _ := driverName(sb.Name())
require.Equal(t, sbDriver, driver)
if isDockerWorker(sb) {
require.NotEmpty(t, hostGatewayIP, "host-gateway-ip worker label should be set with docker driver")

View File

@ -95,33 +95,37 @@ func buildxConfig(sb integration.Sandbox) string {
}
func isMobyWorker(sb integration.Sandbox) bool {
name, hasFeature := driverName(sb.Name())
name, _, hasFeature := driverName(sb.Name())
return name == "docker" && !hasFeature
}
func isMobyContainerdSnapWorker(sb integration.Sandbox) bool {
name, hasFeature := driverName(sb.Name())
name, _, hasFeature := driverName(sb.Name())
return name == "docker" && hasFeature
}
func isDockerWorker(sb integration.Sandbox) bool {
name, _ := driverName(sb.Name())
name, _, _ := driverName(sb.Name())
return name == "docker"
}
func isDockerContainerWorker(sb integration.Sandbox) bool {
name, _ := driverName(sb.Name())
name, _, _ := driverName(sb.Name())
return name == "docker-container"
}
func driverName(sbName string) (string, bool) {
func driverName(sbName string) (string, bool, bool) {
name := sbName
var hasFeature bool
var hasVersion, hasFeature bool
if b, _, ok := strings.Cut(sbName, "@"); ok {
name = b
hasVersion = true
}
if b, _, ok := strings.Cut(name, "+"); ok {
name = b
hasFeature = true
}
return name, hasFeature
return name, hasVersion, hasFeature
}
func isExperimental() bool {

View File

@ -34,7 +34,7 @@ func testLs(t *testing.T, sb integration.Sandbox) {
},
}
sbDriver, _ := driverName(sb.Name())
sbDriver, _, _ := driverName(sb.Name())
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {

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 {