X Tutup
Skip to content

Commit e2c769d

Browse files
committed
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 <cbelu@cloudbasesolutions.com>
1 parent af1a090 commit e2c769d

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

platforms/defaults.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,11 @@
1616

1717
package platforms
1818

19-
import (
20-
"runtime"
21-
22-
specs "github.com/opencontainers/image-spec/specs-go/v1"
23-
)
24-
2519
// DefaultString returns the default string specifier for the platform.
2620
func DefaultString() string {
2721
return Format(DefaultSpec())
2822
}
2923

30-
// DefaultSpec returns the current platform's default platform specification.
31-
func DefaultSpec() specs.Platform {
32-
return specs.Platform{
33-
OS: runtime.GOOS,
34-
Architecture: runtime.GOARCH,
35-
// The Variant field will be empty if arch != ARM.
36-
Variant: cpuVariant(),
37-
}
38-
}
39-
4024
// DefaultStrict returns strict form of Default.
4125
func DefaultStrict() MatchComparer {
4226
return OnlyStrict(DefaultSpec())

platforms/defaults_unix.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@
1919

2020
package platforms
2121

22+
import (
23+
"runtime"
24+
25+
specs "github.com/opencontainers/image-spec/specs-go/v1"
26+
)
27+
28+
// DefaultSpec returns the current platform's default platform specification.
29+
func DefaultSpec() specs.Platform {
30+
return specs.Platform{
31+
OS: runtime.GOOS,
32+
Architecture: runtime.GOARCH,
33+
// The Variant field will be empty if arch != ARM.
34+
Variant: cpuVariant(),
35+
}
36+
}
37+
2238
// Default returns the default matcher for the platform.
2339
func Default() MatchComparer {
2440
return Only(DefaultSpec())
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows
2+
// +build !windows
3+
14
/*
25
Copyright The containerd Authors.
36

platforms/defaults_windows.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ import (
2727
"golang.org/x/sys/windows"
2828
)
2929

30+
// DefaultSpec returns the current platform's default platform specification.
31+
func DefaultSpec() specs.Platform {
32+
major, minor, build := windows.RtlGetNtVersionNumbers()
33+
return specs.Platform{
34+
OS: runtime.GOOS,
35+
Architecture: runtime.GOARCH,
36+
OSVersion: fmt.Sprintf("%d.%d.%d", major, minor, build),
37+
// The Variant field will be empty if arch != ARM.
38+
Variant: cpuVariant(),
39+
}
40+
}
41+
3042
type matchComparer struct {
3143
defaults Matcher
3244
osVersionPrefix string

platforms/defaults_windows_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,36 @@
1717
package platforms
1818

1919
import (
20+
"fmt"
21+
"reflect"
22+
"runtime"
2023
"sort"
2124
"testing"
2225

2326
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2427
"github.com/stretchr/testify/assert"
28+
"golang.org/x/sys/windows"
2529
)
2630

31+
func TestDefault(t *testing.T) {
32+
major, minor, build := windows.RtlGetNtVersionNumbers()
33+
expected := imagespec.Platform{
34+
OS: runtime.GOOS,
35+
Architecture: runtime.GOARCH,
36+
OSVersion: fmt.Sprintf("%d.%d.%d", major, minor, build),
37+
Variant: cpuVariant(),
38+
}
39+
p := DefaultSpec()
40+
if !reflect.DeepEqual(p, expected) {
41+
t.Fatalf("default platform not as expected: %#v != %#v", p, expected)
42+
}
43+
44+
s := DefaultString()
45+
if s != Format(p) {
46+
t.Fatalf("default specifier should match formatted default spec: %v != %v", s, p)
47+
}
48+
}
49+
2750
func TestMatchComparerMatch(t *testing.T) {
2851
m := matchComparer{
2952
defaults: Only(imagespec.Platform{
@@ -36,6 +59,10 @@ func TestMatchComparerMatch(t *testing.T) {
3659
platform imagespec.Platform
3760
match bool
3861
}{
62+
{
63+
platform: DefaultSpec(),
64+
match: true,
65+
},
3966
{
4067
platform: imagespec.Platform{
4168
Architecture: "amd64",

0 commit comments

Comments
 (0)
X Tutup