Skip to content

Commit

Permalink
match.Platforms
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <avi@deitcher.net>
  • Loading branch information
deitch committed Nov 24, 2020
1 parent 6df0303 commit e138239
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/v1/match/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ func Annotation(key, value string) Matcher {
// Platform returns a match.Matcher that matches on the provided platform.
// Ignores any descriptors that do not have a platform.
func Platform(platform v1.Platform) Matcher {
return Platforms([]v1.Platform{platform})
}

// Platforms returns a match.Matcher that matches on any one of the provided platforms.
// Ignores any descriptors that do not have a platform.
func Platforms(platforms []v1.Platform) Matcher {
return func(desc v1.Descriptor) bool {
if desc.Platform == nil {
return false
}
return desc.Platform.Equals(platform)
for _, platform := range platforms {
if desc.Platform.Equals(platform) {
return true
}
}
return false
}
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/v1/match/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ func TestPlatform(t *testing.T) {
}
}

func TestPlatforms(t *testing.T) {
tests := []struct {
desc v1.Descriptor
platforms []v1.Platform
match bool
}{
{v1.Descriptor{Platform: &v1.Platform{Architecture: "amd64", OS: "linux"}}, []v1.Platform{{Architecture: "amd64", OS: "darwin"}, {Architecture: "amd64", OS: "linux"}}, true},
{v1.Descriptor{Platform: &v1.Platform{Architecture: "amd64", OS: "linux"}}, []v1.Platform{{Architecture: "arm64", OS: "linux"}, {Architecture: "s390x", OS: "linux"}}, false},
{v1.Descriptor{Platform: &v1.Platform{OS: "linux"}}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
{v1.Descriptor{Platform: &v1.Platform{}}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
{v1.Descriptor{Platform: nil}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
{v1.Descriptor{}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
}
for i, tt := range tests {
f := match.Platforms(tt.platforms)
if match := f(tt.desc); match != tt.match {
t.Errorf("%d: mismatched, got %v expected %v for desc %#v platform %#v", i, match, tt.match, tt.desc, tt.platforms)
}
}
}

func TestMediaTypes(t *testing.T) {
tests := []struct {
desc v1.Descriptor
Expand Down

0 comments on commit e138239

Please sign in to comment.