Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move windows matcher logic so all platforms can use #22

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package platforms

import (
"reflect"
"runtime"
"sort"
"testing"

imagespec "github.com/opencontainers/image-spec/specs-go/v1"
)

// TestMatchComparerMatch_ABICheckWCOW checks windows platform matcher
// behavior for stable ABI and non-stable ABI compliant versions
func TestMatchComparerMatch_ABICheckWCOW(t *testing.T) {
platformNoVersion := imagespec.Platform{
Architecture: "amd64",
OS: "windows",
}
platformWS2019 := imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763",
}
platformWS2022 := imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.20348",
}
platformWindows11 := imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.22621",
}

for _, test := range []struct {
hostPlatform imagespec.Platform
testPlatform imagespec.Platform
match bool
}{
{
hostPlatform: platformWS2019,
testPlatform: imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763",
},
match: true,
},
{
hostPlatform: platformWS2019,
testPlatform: imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.20348",
},
match: false,
},
{
hostPlatform: platformWS2022,
testPlatform: imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763",
},
match: false,
},
{
hostPlatform: platformWS2022,
testPlatform: imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.20348",
},
match: true,
},
{
hostPlatform: platformWindows11,
testPlatform: imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763",
},
match: false,
},
{
hostPlatform: platformWindows11,
testPlatform: imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.20348",
},
match: true,
},
{
hostPlatform: platformNoVersion,
testPlatform: platformWS2019,
match: true,
},
{
hostPlatform: platformNoVersion,
testPlatform: platformNoVersion,
match: true,
},
{
hostPlatform: platformNoVersion,
testPlatform: platformWindows11,
match: true,
},
} {
matcher := NewMatcher(test.hostPlatform)
if actual := matcher.Match(test.testPlatform); actual != test.match {
t.Errorf("should match: %t, test(%s) to host(%s)", test.match, test.hostPlatform, test.testPlatform)
}
}
}

func TestWindowsMatchComparerLess(t *testing.T) {
p := imagespec.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763.1",
}

m := NewMatcher(p)
if runtime.GOOS != "windows" {
// By default NewMatcher only returns the MatchComparer interface on Windows (which is only for backwards compatibility).
// On other platforms, we need to wrap the matcher in a windowsMatchComparer since the test is using it.
m = &windowsMatchComparer{m}
}
platforms := []imagespec.Platform{
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17764.1",
},
{
Architecture: "amd64",
OS: "windows",
},
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763.1",
},
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763.2",
},
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17762.1",
},
}
expected := []imagespec.Platform{
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763.2",
},
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763.1",
},
{
Architecture: "amd64",
OS: "windows",
},
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17764.1",
},
{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17762.1",
},
}
sort.SliceStable(platforms, func(i, j int) bool {
return m.(MatchComparer).Less(platforms[i], platforms[j])
})
if !reflect.DeepEqual(platforms, expected) {
t.Errorf("expected: %s\nactual : %s", expected, platforms)
}
}
76 changes: 0 additions & 76 deletions defaults_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package platforms
import (
"fmt"
"runtime"
"strconv"
"strings"

specs "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/sys/windows"
Expand All @@ -38,80 +36,6 @@ func DefaultSpec() specs.Platform {
}
}

type windowsmatcher struct {
specs.Platform
osVersionPrefix string
defaultMatcher Matcher
}

// Match matches platform with the same windows major, minor
// and build version.
func (m windowsmatcher) Match(p specs.Platform) bool {
match := m.defaultMatcher.Match(p)

if match && m.OS == "windows" {
// HPC containers do not have OS version filled
if m.OSVersion == "" || p.OSVersion == "" {
return true
}

hostOsVersion := getOSVersion(m.osVersionPrefix)
ctrOsVersion := getOSVersion(p.OSVersion)
return checkHostAndContainerCompat(hostOsVersion, ctrOsVersion)
}

return match
}

func getOSVersion(osVersionPrefix string) osVersion {
parts := strings.Split(osVersionPrefix, ".")
if len(parts) < 3 {
return osVersion{}
}

majorVersion, _ := strconv.ParseUint(parts[0], 10, 8)
minorVersion, _ := strconv.ParseUint(parts[1], 10, 8)
buildNumber, _ := strconv.ParseUint(parts[2], 10, 16)

return osVersion{
MajorVersion: uint8(majorVersion),
MinorVersion: uint8(minorVersion),
Build: uint16(buildNumber),
}
}

// Less sorts matched platforms in front of other platforms.
// For matched platforms, it puts platforms with larger revision
// number in front.
func (m windowsmatcher) Less(p1, p2 specs.Platform) bool {
m1, m2 := m.Match(p1), m.Match(p2)
if m1 && m2 {
r1, r2 := revision(p1.OSVersion), revision(p2.OSVersion)
return r1 > r2
}
return m1 && !m2
}

func revision(v string) int {
parts := strings.Split(v, ".")
if len(parts) < 4 {
return 0
}
r, err := strconv.Atoi(parts[3])
if err != nil {
return 0
}
return r
}

func prefix(v string) string {
parts := strings.Split(v, ".")
if len(parts) < 4 {
return v
}
return strings.Join(parts[0:3], ".")
}

// Default returns the current platform's default platform specification.
func Default() MatchComparer {
return Only(DefaultSpec())
Expand Down
Loading
Loading