mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-12 22:47:09 +08:00
tests: skip according to buildkit version constraint
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@ -3,10 +3,13 @@ package tests
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/containerd/continuity/fs/fstest"
|
||||
"github.com/moby/buildkit/util/testutil/integration"
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -14,6 +17,8 @@ import (
|
||||
|
||||
const defaultBuildKitTag = "buildx-stable-1"
|
||||
|
||||
var buildkitImage string
|
||||
|
||||
func tmpdir(t *testing.T, appliers ...fstest.Applier) string {
|
||||
t.Helper()
|
||||
tmpdir := t.TempDir()
|
||||
@ -82,6 +87,11 @@ func isMobyWorker(sb integration.Sandbox) bool {
|
||||
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 {
|
||||
name, _ := driverName(sb.Name())
|
||||
return name == "docker"
|
||||
@ -116,3 +126,74 @@ func buildkitTag() string {
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user