X Tutup
Skip to content

Commit e22ce0f

Browse files
committed
platforms: add subarchless version of Only()
`OnlyStrict()` returns a match comparer for a single platform. Unlike `Only()`, `OnlyStrict()` does not match sub platforms. So, "arm/vN" will not match "arm/vM" where M < N, and "amd64" will not also match "386". `OnlyStrict()` matches non-canonical forms. So, "arm64" matches "arm/64/v8". Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 66fec3b commit e22ce0f

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

platforms/compare.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ func Only(platform specs.Platform) MatchComparer {
8585
return Ordered(platformVector(Normalize(platform))...)
8686
}
8787

88+
// OnlyStrict returns a match comparer for a single platform.
89+
//
90+
// Unlike Only, OnlyStrict does not match sub platforms.
91+
// So, "arm/vN" will not match "arm/vM" where M < N,
92+
// and "amd64" will not also match "386".
93+
//
94+
// OnlyStrict matches non-canonical forms.
95+
// So, "arm64" matches "arm/64/v8".
96+
func OnlyStrict(platform specs.Platform) MatchComparer {
97+
return Ordered(Normalize(platform))
98+
}
99+
88100
// Ordered returns a platform MatchComparer which matches any of the platforms
89101
// but orders them in order they are provided.
90102
func Ordered(platforms ...specs.Platform) MatchComparer {

platforms/compare_test.go

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,190 @@ func TestOnly(t *testing.T) {
206206
})
207207
}
208208
}
209+
210+
func TestOnlyStrict(t *testing.T) {
211+
for _, tc := range []struct {
212+
platform string
213+
matches map[bool][]string
214+
}{
215+
{
216+
platform: "linux/amd64",
217+
matches: map[bool][]string{
218+
true: {
219+
"linux/amd64",
220+
},
221+
false: {
222+
"linux/386",
223+
"linux/arm/v7",
224+
"linux/arm64",
225+
"windows/amd64",
226+
"windows/arm",
227+
},
228+
},
229+
},
230+
{
231+
platform: "linux/386",
232+
matches: map[bool][]string{
233+
true: {
234+
"linux/386",
235+
},
236+
false: {
237+
"linux/amd64",
238+
"linux/arm/v7",
239+
"linux/arm64",
240+
"windows/amd64",
241+
"windows/arm",
242+
},
243+
},
244+
},
245+
{
246+
platform: "windows/amd64",
247+
matches: map[bool][]string{
248+
true: {"windows/amd64"},
249+
false: {
250+
"linux/amd64",
251+
"linux/arm/v7",
252+
"linux/arm64",
253+
"windows/arm",
254+
},
255+
},
256+
},
257+
{
258+
platform: "linux/arm/v8",
259+
matches: map[bool][]string{
260+
true: {
261+
"linux/arm/v8",
262+
},
263+
false: {
264+
"linux/arm",
265+
"linux/arm/v5",
266+
"linux/arm/v6",
267+
"linux/arm/v7",
268+
"linux/amd64",
269+
"linux/arm/v4",
270+
"linux/arm64",
271+
"windows/amd64",
272+
"windows/arm",
273+
},
274+
},
275+
},
276+
{
277+
platform: "linux/arm/v7",
278+
matches: map[bool][]string{
279+
true: {
280+
"linux/arm",
281+
"linux/arm/v7",
282+
},
283+
false: {
284+
"linux/arm/v5",
285+
"linux/arm/v6",
286+
"linux/amd64",
287+
"linux/arm/v4",
288+
"linux/arm/v8",
289+
"linux/arm64",
290+
"windows/amd64",
291+
"windows/arm",
292+
},
293+
},
294+
},
295+
{
296+
platform: "linux/arm/v6",
297+
matches: map[bool][]string{
298+
true: {
299+
"linux/arm/v6",
300+
},
301+
false: {
302+
"linux/arm/v5",
303+
"linux/amd64",
304+
"linux/arm",
305+
"linux/arm/v4",
306+
"linux/arm/v7",
307+
"linux/arm/v8",
308+
"linux/arm64",
309+
"windows/amd64",
310+
"windows/arm",
311+
},
312+
},
313+
},
314+
{
315+
platform: "linux/arm/v5",
316+
matches: map[bool][]string{
317+
true: {
318+
"linux/arm/v5",
319+
},
320+
false: {
321+
"linux/amd64",
322+
"linux/arm",
323+
"linux/arm/v4",
324+
"linux/arm/v6",
325+
"linux/arm/v7",
326+
"linux/arm/v8",
327+
"linux/arm64",
328+
"windows/amd64",
329+
"windows/arm",
330+
},
331+
},
332+
},
333+
{
334+
platform: "linux/arm/v4",
335+
matches: map[bool][]string{
336+
true: {
337+
"linux/arm/v4",
338+
},
339+
false: {
340+
"linux/amd64",
341+
"linux/arm",
342+
"linux/arm/v5",
343+
"linux/arm/v6",
344+
"linux/arm/v7",
345+
"linux/arm/v8",
346+
"linux/arm64",
347+
"windows/amd64",
348+
"windows/arm",
349+
},
350+
},
351+
},
352+
{
353+
platform: "linux/arm64",
354+
matches: map[bool][]string{
355+
true: {
356+
"linux/arm64",
357+
"linux/arm64/v8",
358+
},
359+
false: {
360+
"linux/arm",
361+
"linux/arm/v5",
362+
"linux/arm/v6",
363+
"linux/arm/v7",
364+
"linux/arm/v8",
365+
"linux/amd64",
366+
"linux/arm/v4",
367+
"linux/arm/v9",
368+
"linux/arm64/v9",
369+
"windows/amd64",
370+
"windows/arm",
371+
},
372+
},
373+
},
374+
} {
375+
testcase := tc
376+
t.Run(testcase.platform, func(t *testing.T) {
377+
p, err := Parse(testcase.platform)
378+
if err != nil {
379+
t.Fatal(err)
380+
}
381+
m := OnlyStrict(p)
382+
for shouldMatch, platforms := range testcase.matches {
383+
for _, matchPlatform := range platforms {
384+
mp, err := Parse(matchPlatform)
385+
if err != nil {
386+
t.Fatal(err)
387+
}
388+
if match := m.Match(mp); shouldMatch != match {
389+
t.Errorf("OnlyStrict(%q).Match(%q) should return %v, but returns %v", testcase.platform, matchPlatform, shouldMatch, match)
390+
}
391+
}
392+
}
393+
})
394+
}
395+
}

platforms/defaults.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ func DefaultSpec() specs.Platform {
3636
Variant: cpuVariant,
3737
}
3838
}
39+
40+
// DefaultStrict returns strict form of Default.
41+
func DefaultStrict() MatchComparer {
42+
return OnlyStrict(DefaultSpec())
43+
}

0 commit comments

Comments
 (0)
X Tutup