Merge pull request #5914 from claudiubelu/windows/platform-default-match

This commit is contained in:
Fu Wei 2021-08-30 17:40:47 +08:00 committed by GitHub
commit ebcf876bf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 16 deletions

View File

@ -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())

View File

@ -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())

View File

@ -1,3 +1,6 @@
//go:build !windows
// +build !windows
/*
Copyright The containerd Authors.

View File

@ -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

View File

@ -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",