Skip to content

Commit 8c0d9f9

Browse files
mweibelMichael Weibel
authored andcommitted
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 2fec6c4 commit 8c0d9f9

3 files changed

Lines changed: 77 additions & 15 deletions

File tree

defaults_windows.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,6 @@ func getWindowsOSVersion(osVersionPrefix string) windowsOSVersion {
135135
}
136136
}
137137

138-
func winRevision(v string) int {
139-
parts := strings.Split(v, ".")
140-
if len(parts) < 4 {
141-
return 0
142-
}
143-
r, err := strconv.Atoi(parts[3])
144-
if err != nil {
145-
return 0
146-
}
147-
return r
148-
}
149-
150138
type windowsVersionMatcher struct {
151139
windowsOSVersion
152140
}
@@ -170,8 +158,7 @@ type windowsMatchComparer struct {
170158
func (c *windowsMatchComparer) Less(p1, p2 specs.Platform) bool {
171159
m1, m2 := c.Match(p1), c.Match(p2)
172160
if m1 && m2 {
173-
r1, r2 := winRevision(p1.OSVersion), winRevision(p2.OSVersion)
174-
return r1 > r2
161+
return p1.OSVersion > p2.OSVersion
175162
}
176163
return m1 && !m2
177164
}

platform_windows_compat_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ package platforms
1818

1919
import (
2020
"fmt"
21+
"sort"
2122
"testing"
23+
24+
specs "github.com/opencontainers/image-spec/specs-go/v1"
2225
)
2326

2427
// Test the platform compatibility of the different OS Versions
@@ -110,3 +113,75 @@ func Test_PlatformCompat(t *testing.T) {
110113
})
111114
}
112115
}
116+
117+
func Test_PlatformOrder(t *testing.T) {
118+
linuxPlatform := specs.Platform{
119+
Architecture: "amd64",
120+
OS: "linux",
121+
OSVersion: "",
122+
OSFeatures: nil,
123+
Variant: "",
124+
}
125+
ws2022Platform := specs.Platform{
126+
Architecture: "amd64",
127+
OS: "windows",
128+
OSVersion: "10.0.20348.3091",
129+
OSFeatures: nil,
130+
Variant: "",
131+
}
132+
ws2025Platform := specs.Platform{
133+
Architecture: "amd64",
134+
OS: "windows",
135+
OSVersion: "10.0.26100.2894",
136+
OSFeatures: nil,
137+
Variant: "",
138+
}
139+
ws2025Rev3000Platform := specs.Platform{
140+
Architecture: "amd64",
141+
OS: "windows",
142+
OSVersion: "10.0.26100.3000",
143+
OSFeatures: nil,
144+
Variant: "",
145+
}
146+
147+
tt := []struct {
148+
name string
149+
hostPlatform specs.Platform
150+
platforms []specs.Platform
151+
wantPlatform specs.Platform
152+
}{
153+
{
154+
name: "Windows Server 2022 should select 2022",
155+
hostPlatform: ws2022Platform,
156+
platforms: []specs.Platform{linuxPlatform, ws2022Platform, ws2025Platform},
157+
wantPlatform: ws2022Platform,
158+
},
159+
{
160+
name: "Windows Server 2025 should select 2025",
161+
hostPlatform: ws2025Platform,
162+
platforms: []specs.Platform{linuxPlatform, ws2022Platform, ws2025Platform},
163+
wantPlatform: ws2025Platform,
164+
},
165+
{
166+
name: "Windows Server 2025 should select 2025 latest rev",
167+
hostPlatform: ws2025Platform,
168+
platforms: []specs.Platform{linuxPlatform, ws2022Platform, ws2025Rev3000Platform},
169+
wantPlatform: ws2025Rev3000Platform,
170+
},
171+
}
172+
173+
for _, tc := range tt {
174+
t.Run(tc.name, func(t *testing.T) {
175+
comparer := &windowsMatchComparer{Matcher: NewMatcher(tc.hostPlatform)}
176+
177+
sort.SliceStable(tc.platforms, func(i, j int) bool {
178+
return comparer.Less(tc.platforms[i], tc.platforms[j])
179+
})
180+
181+
if tc.platforms[0].OS != tc.wantPlatform.OS || tc.platforms[0].OSVersion != tc.wantPlatform.OSVersion {
182+
t.Errorf("Platform mismatch, want %q/%q, got %q/%q", tc.wantPlatform.OS, tc.wantPlatform.OSVersion, tc.platforms[0].OS, tc.platforms[0].OSVersion)
183+
}
184+
})
185+
}
186+
187+
}

0 commit comments

Comments
 (0)