tests: skip according to buildkit version constraint

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-03-10 00:29:40 +01:00
parent 7432b483ce
commit dd4d52407f
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
6 changed files with 99 additions and 81 deletions

View File

@ -26,7 +26,7 @@ gotestArgs="-mod=vendor -coverprofile=/testreports/coverage-report$TEST_REPORT_S
cacheVolume="buildx-test-cache" cacheVolume="buildx-test-cache"
if ! docker container inspect "$cacheVolume" >/dev/null 2>/dev/null; then if ! docker container inspect "$cacheVolume" >/dev/null 2>/dev/null; then
docker create -v /root/.cache -v /root/.cache/registry -v /go/pkg/mod --name "$cacheVolume" alpine docker create -v /root/.cache -v /root/.cache/registry -v /root/.cache/undock -v /go/pkg/mod --name "$cacheVolume" alpine
fi fi
if [ "$TEST_KEEP_CACHE" != "1" ]; then if [ "$TEST_KEEP_CACHE" != "1" ]; then
trap 'docker rm -v $cacheVolume' EXIT trap 'docker rm -v $cacheVolume' EXIT

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"github.com/containerd/continuity/fs/fstest" "github.com/containerd/continuity/fs/fstest"
@ -680,6 +679,7 @@ func testBakeMultiExporters(t *testing.T, sb integration.Sandbox) {
if !isDockerContainerWorker(sb) { if !isDockerContainerWorker(sb) {
t.Skip("only testing with docker-container worker") t.Skip("only testing with docker-container worker")
} }
skipNoCompatBuildKit(t, sb, ">= 0.13.0-0", "multi exporters")
registry, err := sb.NewRegistry() registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) { if errors.Is(err, integration.ErrRequirements) {
@ -690,30 +690,12 @@ func testBakeMultiExporters(t *testing.T, sb integration.Sandbox) {
targetReg := registry + "/buildx/registry:latest" targetReg := registry + "/buildx/registry:latest"
targetStore := "buildx:local-" + identity.NewID() targetStore := "buildx:local-" + identity.NewID()
var builderName string
t.Cleanup(func() { t.Cleanup(func() {
if builderName == "" {
return
}
cmd := dockerCmd(sb, withArgs("image", "rm", targetStore)) cmd := dockerCmd(sb, withArgs("image", "rm", targetStore))
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run()) require.NoError(t, cmd.Run())
out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
}) })
// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)
dockerfile := []byte(` dockerfile := []byte(`
FROM scratch FROM scratch
COPY foo /foo COPY foo /foo
@ -735,7 +717,6 @@ target "default" {
"--set", fmt.Sprintf("*.output=type=oci,dest=%s/result", dir), "--set", fmt.Sprintf("*.output=type=oci,dest=%s/result", dir),
} }
cmd := buildxCmd(sb, withDir(dir), withArgs("bake"), withArgs(outputs...)) cmd := buildxCmd(sb, withDir(dir), withArgs("bake"), withArgs(outputs...))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput() outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb)) require.NoError(t, err, string(outb))
@ -761,6 +742,7 @@ func testBakeLoadPush(t *testing.T, sb integration.Sandbox) {
if !isDockerContainerWorker(sb) { if !isDockerContainerWorker(sb) {
t.Skip("only testing with docker-container worker") t.Skip("only testing with docker-container worker")
} }
skipNoCompatBuildKit(t, sb, ">= 0.13.0-0", "multi exporters")
registry, err := sb.NewRegistry() registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) { if errors.Is(err, integration.ErrRequirements) {
@ -770,30 +752,12 @@ func testBakeLoadPush(t *testing.T, sb integration.Sandbox) {
target := registry + "/buildx/registry:" + identity.NewID() target := registry + "/buildx/registry:" + identity.NewID()
var builderName string
t.Cleanup(func() { t.Cleanup(func() {
if builderName == "" {
return
}
cmd := dockerCmd(sb, withArgs("image", "rm", target)) cmd := dockerCmd(sb, withArgs("image", "rm", target))
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run()) require.NoError(t, cmd.Run())
out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
}) })
// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)
dockerfile := []byte(` dockerfile := []byte(`
FROM scratch FROM scratch
COPY foo /foo COPY foo /foo
@ -810,7 +774,6 @@ target "default" {
) )
cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--push", "--load", fmt.Sprintf("--set=*.tags=%s", target))) cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--push", "--load", fmt.Sprintf("--set=*.tags=%s", target)))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput() outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb)) require.NoError(t, err, string(outb))

View File

@ -152,6 +152,10 @@ func testBuildRegistryExportAttestations(t *testing.T, sb integration.Sandbox) {
require.Error(t, err) require.Error(t, err)
require.Contains(t, out, "Attestation is not supported") require.Contains(t, out, "Attestation is not supported")
return return
} else if !isMobyContainerdSnapWorker(sb) && !matchesBuildKitVersion(t, sb, ">= 0.11.0-0") {
require.Error(t, err)
require.Contains(t, out, "Attestations are not supported by the current BuildKit daemon")
return
} }
require.NoError(t, err, string(out)) require.NoError(t, err, string(out))
@ -270,6 +274,7 @@ RUN busybox | head -1 | grep v1.36.1
} }
func testBuildDetailsLink(t *testing.T, sb integration.Sandbox) { func testBuildDetailsLink(t *testing.T, sb integration.Sandbox) {
skipNoCompatBuildKit(t, sb, ">= 0.11.0-0", "build details link")
buildDetailsPattern := regexp.MustCompile(`(?m)^View build details: docker-desktop://dashboard/build/[^/]+/[^/]+/[^/]+\n$`) buildDetailsPattern := regexp.MustCompile(`(?m)^View build details: docker-desktop://dashboard/build/[^/]+/[^/]+/[^/]+\n$`)
// build simple dockerfile // build simple dockerfile
@ -346,6 +351,7 @@ func testBuildAnnotations(t *testing.T, sb integration.Sandbox) {
if isMobyWorker(sb) { if isMobyWorker(sb) {
t.Skip("annotations not supported on docker worker") t.Skip("annotations not supported on docker worker")
} }
skipNoCompatBuildKit(t, sb, ">= 0.11.0-0", "annotations")
dir := createTestProject(t) dir := createTestProject(t)
@ -457,6 +463,7 @@ func testBuildNetworkModeBridge(t *testing.T, sb integration.Sandbox) {
if !isDockerContainerWorker(sb) { if !isDockerContainerWorker(sb) {
t.Skip("only testing with docker-container worker") t.Skip("only testing with docker-container worker")
} }
skipNoCompatBuildKit(t, sb, ">= 0.13.0-0", "network bridge")
var builderName string var builderName string
t.Cleanup(func() { t.Cleanup(func() {
@ -467,8 +474,10 @@ func testBuildNetworkModeBridge(t *testing.T, sb integration.Sandbox) {
require.NoError(t, err, out) require.NoError(t, err, out)
}) })
// TODO: use stable buildkit image when v0.13.0 released out, err := createCmd(sb, withArgs(
out, err := createCmd(sb, withArgs("--driver", "docker-container", "--buildkitd-flags=--oci-worker-net=bridge --allow-insecure-entitlement=network.host", "--driver-opt", "image=moby/buildkit:v0.13.0-rc3")) "--driver", "docker-container",
"--buildkitd-flags=--oci-worker-net=bridge --allow-insecure-entitlement=network.host",
))
require.NoError(t, err, out) require.NoError(t, err, out)
builderName = strings.TrimSpace(out) builderName = strings.TrimSpace(out)
@ -577,6 +586,7 @@ func testBuildMultiExporters(t *testing.T, sb integration.Sandbox) {
if !isDockerContainerWorker(sb) { if !isDockerContainerWorker(sb) {
t.Skip("only testing with docker-container worker") t.Skip("only testing with docker-container worker")
} }
skipNoCompatBuildKit(t, sb, ">= 0.13.0-0", "multi exporters")
registry, err := sb.NewRegistry() registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) { if errors.Is(err, integration.ErrRequirements) {
@ -587,30 +597,12 @@ func testBuildMultiExporters(t *testing.T, sb integration.Sandbox) {
targetReg := registry + "/buildx/registry:latest" targetReg := registry + "/buildx/registry:latest"
targetStore := "buildx:local-" + identity.NewID() targetStore := "buildx:local-" + identity.NewID()
var builderName string
t.Cleanup(func() { t.Cleanup(func() {
if builderName == "" {
return
}
cmd := dockerCmd(sb, withArgs("image", "rm", targetStore)) cmd := dockerCmd(sb, withArgs("image", "rm", targetStore))
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run()) require.NoError(t, cmd.Run())
out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
}) })
// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)
dir := createTestProject(t) dir := createTestProject(t)
outputs := []string{ outputs := []string{
@ -619,7 +611,6 @@ func testBuildMultiExporters(t *testing.T, sb integration.Sandbox) {
"--output", fmt.Sprintf("type=oci,dest=%s/result", dir), "--output", fmt.Sprintf("type=oci,dest=%s/result", dir),
} }
cmd := buildxCmd(sb, withArgs("build"), withArgs(outputs...), withArgs(dir)) cmd := buildxCmd(sb, withArgs("build"), withArgs(outputs...), withArgs(dir))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput() outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb)) require.NoError(t, err, string(outb))
@ -645,6 +636,7 @@ func testBuildLoadPush(t *testing.T, sb integration.Sandbox) {
if !isDockerContainerWorker(sb) { if !isDockerContainerWorker(sb) {
t.Skip("only testing with docker-container worker") t.Skip("only testing with docker-container worker")
} }
skipNoCompatBuildKit(t, sb, ">= 0.13.0-0", "multi exporters")
registry, err := sb.NewRegistry() registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) { if errors.Is(err, integration.ErrRequirements) {
@ -654,30 +646,12 @@ func testBuildLoadPush(t *testing.T, sb integration.Sandbox) {
target := registry + "/buildx/registry:" + identity.NewID() target := registry + "/buildx/registry:" + identity.NewID()
var builderName string
t.Cleanup(func() { t.Cleanup(func() {
if builderName == "" {
return
}
cmd := dockerCmd(sb, withArgs("image", "rm", target)) cmd := dockerCmd(sb, withArgs("image", "rm", target))
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run()) require.NoError(t, cmd.Run())
out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
}) })
// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)
dir := createTestProject(t) dir := createTestProject(t)
cmd := buildxCmd(sb, withArgs( cmd := buildxCmd(sb, withArgs(
@ -685,7 +659,6 @@ func testBuildLoadPush(t *testing.T, sb integration.Sandbox) {
fmt.Sprintf("-t=%s", target), fmt.Sprintf("-t=%s", target),
dir, dir,
)) ))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput() outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb)) require.NoError(t, err, string(outb))

View File

@ -61,6 +61,7 @@ func testDialStdio(t *testing.T, sb integration.Sandbox) {
} }
}() }()
skipNoCompatBuildKit(t, sb, ">= 0.11.0-0", "unknown method Info for service moby.buildkit.v1.Control")
_, err = c.Info(sb.Context()) _, err = c.Info(sb.Context())
require.NoError(t, err) require.NoError(t, err)

View File

@ -3,10 +3,13 @@ package tests
import ( import (
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"testing" "testing"
"github.com/Masterminds/semver/v3"
"github.com/containerd/continuity/fs/fstest" "github.com/containerd/continuity/fs/fstest"
"github.com/moby/buildkit/util/testutil/integration" "github.com/moby/buildkit/util/testutil/integration"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -14,6 +17,8 @@ import (
const defaultBuildKitTag = "buildx-stable-1" const defaultBuildKitTag = "buildx-stable-1"
var buildkitImage string
func tmpdir(t *testing.T, appliers ...fstest.Applier) string { func tmpdir(t *testing.T, appliers ...fstest.Applier) string {
t.Helper() t.Helper()
tmpdir := t.TempDir() tmpdir := t.TempDir()
@ -82,6 +87,11 @@ func isMobyWorker(sb integration.Sandbox) bool {
return name == "docker" && !hasFeature return name == "docker" && !hasFeature
} }
func isMobyContainerdSnapWorker(sb integration.Sandbox) bool {
name, hasFeature := driverName(sb.Name())
return name == "docker" && hasFeature
}
func isDockerWorker(sb integration.Sandbox) bool { func isDockerWorker(sb integration.Sandbox) bool {
name, _ := driverName(sb.Name()) name, _ := driverName(sb.Name())
return name == "docker" return name == "docker"
@ -116,3 +126,74 @@ func buildkitTag() string {
} }
return defaultBuildKitTag return defaultBuildKitTag
} }
var (
bkvers map[string]string
bkversMu sync.Mutex
)
func buildkitVersion(t *testing.T, sb integration.Sandbox) string {
bkversMu.Lock()
defer bkversMu.Unlock()
if bkvers == nil {
bkvers = make(map[string]string)
}
ver, ok := bkvers[sb.Name()]
if !ok {
out, err := inspectCmd(sb, withArgs(sb.Address()))
require.NoError(t, err, out)
for _, line := range strings.Split(out, "\n") {
if v, ok := strings.CutPrefix(line, "BuildKit version:"); ok {
ver = strings.TrimSpace(v)
bkvers[sb.Name()] = ver
}
}
if ver == "" {
t.Logf("BuildKit version not found in inspect output, extract it from the image.\n%s", out)
undockBin, err := exec.LookPath("undock")
require.NoError(t, err, "undock not found")
destDir := t.TempDir()
t.Cleanup(func() {
os.RemoveAll(destDir)
})
cmd := exec.Command(undockBin, "--cachedir", "/root/.cache/undock", "--include", "/usr/bin/buildkitd", "--rm-dist", buildkitImage, destDir)
require.NoErrorf(t, cmd.Run(), "failed to extract buildkitd binary from %q", buildkitImage)
cmd = exec.Command(filepath.Join(destDir, "usr", "bin", "buildkitd"), "--version")
out, err := cmd.CombinedOutput()
require.NoErrorf(t, err, "failed to get BuildKit version from %q: %s", buildkitImage, string(out))
v := strings.Fields(strings.TrimSpace(string(out)))
if len(v) != 4 {
require.Fail(t, "unexpected version format: "+strings.TrimSpace(string(out)))
}
ver = v[2]
bkvers[sb.Name()] = ver
}
}
return ver
}
func matchesBuildKitVersion(t *testing.T, sb integration.Sandbox, constraint string) bool {
c, err := semver.NewConstraint(constraint)
if err != nil {
return false
}
v, err := semver.NewVersion(buildkitVersion(t, sb))
if err != nil {
// if the version is not a valid semver, we assume it matches (master)
return true
}
return c.Check(v)
}
func skipNoCompatBuildKit(t *testing.T, sb integration.Sandbox, constraint string, msg string) {
if !matchesBuildKitVersion(t, sb, constraint) {
t.Skipf("buildkit version %s does not match %s constraint (%s)", buildkitVersion(t, sb), constraint, msg)
}
}

View File

@ -35,7 +35,7 @@ func TestIntegration(t *testing.T) {
func testIntegration(t *testing.T, funcs ...func(t *testing.T, sb integration.Sandbox)) { func testIntegration(t *testing.T, funcs ...func(t *testing.T, sb integration.Sandbox)) {
mirroredImages := integration.OfficialImages("busybox:latest", "alpine:latest") mirroredImages := integration.OfficialImages("busybox:latest", "alpine:latest")
buildkitImage := "docker.io/moby/buildkit:" + buildkitTag() buildkitImage = "docker.io/moby/buildkit:" + buildkitTag()
if bkworkers.IsTestDockerd() { if bkworkers.IsTestDockerd() {
if img, ok := os.LookupEnv("TEST_BUILDKIT_IMAGE"); ok { if img, ok := os.LookupEnv("TEST_BUILDKIT_IMAGE"); ok {
ref, err := reference.ParseNormalizedNamed(img) ref, err := reference.ParseNormalizedNamed(img)