Skip to content

Commit 064fd86

Browse files
committed
use windowsMatchComparer for OSVersion match order
Windows OS version should match based on the full OSVersion. When sorting a manifest, the entries should be sorted using the `Less` function. Signed-off-by: Michael Weibel <[email protected]>
1 parent e3566b8 commit 064fd86

3 files changed

+77
-3
lines changed

defaults_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ func DefaultSpec() specs.Platform {
3838

3939
// Default returns the current platform's default platform specification.
4040
func Default() MatchComparer {
41-
return Only(DefaultSpec())
41+
return &windowsMatchComparer{Matcher: NewMatcher(DefaultSpec())}
4242
}

platform_windows_compat.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ type windowsMatchComparer struct {
149149
func (c *windowsMatchComparer) Less(p1, p2 specs.Platform) bool {
150150
m1, m2 := c.Match(p1), c.Match(p2)
151151
if m1 && m2 {
152-
r1, r2 := winRevision(p1.OSVersion), winRevision(p2.OSVersion)
153-
return r1 > r2
152+
return p1.OSVersion > p2.OSVersion
154153
}
155154
return m1 && !m2
156155
}

platform_windows_compat_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package platforms
1818

1919
import (
20+
"sort"
2021
"testing"
22+
23+
specs "github.com/opencontainers/image-spec/specs-go/v1"
2124
)
2225

2326
// Test the platform compatibility of the different
@@ -82,3 +85,75 @@ func Test_PlatformCompat(t *testing.T) {
8285
}
8386
}
8487
}
88+
89+
func Test_PlatformOrder(t *testing.T) {
90+
linuxPlatform := specs.Platform{
91+
Architecture: "amd64",
92+
OS: "linux",
93+
OSVersion: "",
94+
OSFeatures: nil,
95+
Variant: "",
96+
}
97+
ws2022Platform := specs.Platform{
98+
Architecture: "amd64",
99+
OS: "windows",
100+
OSVersion: "10.0.20348.3091",
101+
OSFeatures: nil,
102+
Variant: "",
103+
}
104+
ws2025Platform := specs.Platform{
105+
Architecture: "amd64",
106+
OS: "windows",
107+
OSVersion: "10.0.26100.2894",
108+
OSFeatures: nil,
109+
Variant: "",
110+
}
111+
ws2025Rev3000Platform := specs.Platform{
112+
Architecture: "amd64",
113+
OS: "windows",
114+
OSVersion: "10.0.26100.3000",
115+
OSFeatures: nil,
116+
Variant: "",
117+
}
118+
119+
tt := []struct {
120+
name string
121+
hostPlatform specs.Platform
122+
platforms []specs.Platform
123+
wantPlatform specs.Platform
124+
}{
125+
{
126+
name: "Windows Server 2022 should select 2022",
127+
hostPlatform: ws2022Platform,
128+
platforms: []specs.Platform{linuxPlatform, ws2022Platform, ws2025Platform},
129+
wantPlatform: ws2022Platform,
130+
},
131+
{
132+
name: "Windows Server 2025 should select 2025",
133+
hostPlatform: ws2025Platform,
134+
platforms: []specs.Platform{linuxPlatform, ws2022Platform, ws2025Platform},
135+
wantPlatform: ws2025Platform,
136+
},
137+
{
138+
name: "Windows Server 2025 should select 2025 latest rev",
139+
hostPlatform: ws2025Platform,
140+
platforms: []specs.Platform{linuxPlatform, ws2022Platform, ws2025Rev3000Platform},
141+
wantPlatform: ws2025Rev3000Platform,
142+
},
143+
}
144+
145+
for _, tc := range tt {
146+
t.Run(tc.name, func(t *testing.T) {
147+
comparer := &windowsMatchComparer{Matcher: NewMatcher(tc.hostPlatform)}
148+
149+
sort.SliceStable(tc.platforms, func(i, j int) bool {
150+
return comparer.Less(tc.platforms[i], tc.platforms[j])
151+
})
152+
153+
if tc.platforms[0].OS != tc.wantPlatform.OS || tc.platforms[0].OSVersion != tc.wantPlatform.OSVersion {
154+
t.Errorf("Platform mismatch, want %q/%q, got %q/%q", tc.wantPlatform.OS, tc.wantPlatform.OSVersion, tc.platforms[0].OS, tc.platforms[0].OSVersion)
155+
}
156+
})
157+
}
158+
159+
}

0 commit comments

Comments
 (0)