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 25, 2020
1 parent 6df0303 commit 4007082
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
32 changes: 31 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 All @@ -71,3 +82,22 @@ func MediaTypes(mediaTypes []string) Matcher {
return false
}
}

// Digest returns a match.Matcher that matches a specific Digest
func Digest(digest v1.Hash) Matcher {
return func(desc v1.Descriptor) bool {
return desc.Digest == digest
}
}

// Digests returns a match.Matcher that matches at least one of the provided Digests
func Digests(digests []v1.Hash) Matcher {
digs := map[v1.Hash]bool{}
for _, digest := range digests {
digs[digest] = true
}
return func(desc v1.Descriptor) bool {
_, ok := digs[desc.Digest]
return ok
}
}
70 changes: 70 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 All @@ -104,3 +125,52 @@ func TestMediaTypes(t *testing.T) {
}
}
}

func TestDigests(t *testing.T) {
hashes := []string{
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"abcde1111111222f0123456789abcdef0123456789abcdef0123456789abcdef",
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
}
algo := "sha256"

tests := []struct {
desc v1.Descriptor
digests []v1.Hash
match bool
}{
{v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[0]}}, []v1.Hash{{Algorithm: algo, Hex: hashes[0]}, {Algorithm: algo, Hex: hashes[1]}}, true},
{v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[1]}}, []v1.Hash{{Algorithm: algo, Hex: hashes[0]}, {Algorithm: algo, Hex: hashes[1]}}, true},
{v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[2]}}, []v1.Hash{{Algorithm: algo, Hex: hashes[0]}, {Algorithm: algo, Hex: hashes[1]}}, false},
}
for i, tt := range tests {
f := match.Digests(tt.digests)
if match := f(tt.desc); match != tt.match {
t.Errorf("%d: mismatched, got %v expected %v for desc %#v digests %#v", i, match, tt.match, tt.desc, tt.digests)
}
}
}

func TestDigest(t *testing.T) {
hashes := []string{
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"abcde1111111222f0123456789abcdef0123456789abcdef0123456789abcdef",
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
}
algo := "sha256"

tests := []struct {
desc v1.Descriptor
digest v1.Hash
match bool
}{
{v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[0]}}, v1.Hash{Algorithm: algo, Hex: hashes[0]}, true},
{v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[1]}}, v1.Hash{Algorithm: algo, Hex: hashes[0]}, false},
}
for i, tt := range tests {
f := match.Digest(tt.digest)
if match := f(tt.desc); match != tt.match {
t.Errorf("%d: mismatched, got %v expected %v for desc %#v digests %#v", i, match, tt.match, tt.desc, tt.digest)
}
}
}

0 comments on commit 4007082

Please sign in to comment.