Skip to content

Commit

Permalink
Merge pull request #22 from cpuguy83/windows_everywhere
Browse files Browse the repository at this point in the history
Move windows matcher logic so all platforms can use
  • Loading branch information
kzys authored Jan 11, 2025
2 parents 9ada2e3 + 7c58292 commit e3566b8
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 367 deletions.
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

0 comments on commit e3566b8

Please sign in to comment.