@@ -18,7 +18,10 @@ package platforms
1818
1919import (
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