From e2c769d6fb17391df70bd658e1573360d4ccbb7b Mon Sep 17 00:00:00 2001 From: Claudiu Belu Date: Thu, 26 Aug 2021 10:39:59 +0000 Subject: [PATCH] windows: The DefaultSpec platform should match the Default matcher The Windows Default matcher also checks the the OS Version prefix, however, the platforms.DefaultSpec does not include it, which means that it won't match the matcher. This solves the issue by adding the OS Version to the DefaultSpec. Signed-off-by: Claudiu Belu --- platforms/defaults.go | 16 ----------- platforms/defaults_unix.go | 16 +++++++++++ ...defaults_test.go => defaults_unix_test.go} | 3 +++ platforms/defaults_windows.go | 12 +++++++++ platforms/defaults_windows_test.go | 27 +++++++++++++++++++ 5 files changed, 58 insertions(+), 16 deletions(-) rename platforms/{defaults_test.go => defaults_unix_test.go} (96%) diff --git a/platforms/defaults.go b/platforms/defaults.go index cb77fbc9f..cfa3ff34a 100644 --- a/platforms/defaults.go +++ b/platforms/defaults.go @@ -16,27 +16,11 @@ package platforms -import ( - "runtime" - - specs "github.com/opencontainers/image-spec/specs-go/v1" -) - // DefaultString returns the default string specifier for the platform. func DefaultString() string { return Format(DefaultSpec()) } -// DefaultSpec returns the current platform's default platform specification. -func DefaultSpec() specs.Platform { - return specs.Platform{ - OS: runtime.GOOS, - Architecture: runtime.GOARCH, - // The Variant field will be empty if arch != ARM. - Variant: cpuVariant(), - } -} - // DefaultStrict returns strict form of Default. func DefaultStrict() MatchComparer { return OnlyStrict(DefaultSpec()) diff --git a/platforms/defaults_unix.go b/platforms/defaults_unix.go index 359063efd..e88b5b0d0 100644 --- a/platforms/defaults_unix.go +++ b/platforms/defaults_unix.go @@ -19,6 +19,22 @@ package platforms +import ( + "runtime" + + specs "github.com/opencontainers/image-spec/specs-go/v1" +) + +// DefaultSpec returns the current platform's default platform specification. +func DefaultSpec() specs.Platform { + return specs.Platform{ + OS: runtime.GOOS, + Architecture: runtime.GOARCH, + // The Variant field will be empty if arch != ARM. + Variant: cpuVariant(), + } +} + // Default returns the default matcher for the platform. func Default() MatchComparer { return Only(DefaultSpec()) diff --git a/platforms/defaults_test.go b/platforms/defaults_unix_test.go similarity index 96% rename from platforms/defaults_test.go rename to platforms/defaults_unix_test.go index 62df5350d..a718c5c73 100644 --- a/platforms/defaults_test.go +++ b/platforms/defaults_unix_test.go @@ -1,3 +1,6 @@ +//go:build !windows +// +build !windows + /* Copyright The containerd Authors. diff --git a/platforms/defaults_windows.go b/platforms/defaults_windows.go index ba2c5555d..c1aaf72ca 100644 --- a/platforms/defaults_windows.go +++ b/platforms/defaults_windows.go @@ -27,6 +27,18 @@ import ( "golang.org/x/sys/windows" ) +// DefaultSpec returns the current platform's default platform specification. +func DefaultSpec() specs.Platform { + major, minor, build := windows.RtlGetNtVersionNumbers() + return specs.Platform{ + OS: runtime.GOOS, + Architecture: runtime.GOARCH, + OSVersion: fmt.Sprintf("%d.%d.%d", major, minor, build), + // The Variant field will be empty if arch != ARM. + Variant: cpuVariant(), + } +} + type matchComparer struct { defaults Matcher osVersionPrefix string diff --git a/platforms/defaults_windows_test.go b/platforms/defaults_windows_test.go index 9b046700a..65256a7d9 100644 --- a/platforms/defaults_windows_test.go +++ b/platforms/defaults_windows_test.go @@ -17,13 +17,36 @@ package platforms import ( + "fmt" + "reflect" + "runtime" "sort" "testing" imagespec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/stretchr/testify/assert" + "golang.org/x/sys/windows" ) +func TestDefault(t *testing.T) { + major, minor, build := windows.RtlGetNtVersionNumbers() + expected := imagespec.Platform{ + OS: runtime.GOOS, + Architecture: runtime.GOARCH, + OSVersion: fmt.Sprintf("%d.%d.%d", major, minor, build), + Variant: cpuVariant(), + } + p := DefaultSpec() + if !reflect.DeepEqual(p, expected) { + t.Fatalf("default platform not as expected: %#v != %#v", p, expected) + } + + s := DefaultString() + if s != Format(p) { + t.Fatalf("default specifier should match formatted default spec: %v != %v", s, p) + } +} + func TestMatchComparerMatch(t *testing.T) { m := matchComparer{ defaults: Only(imagespec.Platform{ @@ -36,6 +59,10 @@ func TestMatchComparerMatch(t *testing.T) { platform imagespec.Platform match bool }{ + { + platform: DefaultSpec(), + match: true, + }, { platform: imagespec.Platform{ Architecture: "amd64",