1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package match_test
16
17 import (
18 "testing"
19
20 v1 "github.com/google/go-containerregistry/pkg/v1"
21 "github.com/google/go-containerregistry/pkg/v1/match"
22 "github.com/google/go-containerregistry/pkg/v1/types"
23 imagespec "github.com/opencontainers/image-spec/specs-go/v1"
24 )
25
26 func TestName(t *testing.T) {
27 tests := []struct {
28 desc v1.Descriptor
29 name string
30 match bool
31 }{
32 {v1.Descriptor{Annotations: map[string]string{imagespec.AnnotationRefName: "foo"}}, "foo", true},
33 {v1.Descriptor{Annotations: map[string]string{imagespec.AnnotationRefName: "foo"}}, "bar", false},
34 {v1.Descriptor{Annotations: map[string]string{}}, "bar", false},
35 {v1.Descriptor{Annotations: nil}, "bar", false},
36 {v1.Descriptor{}, "bar", false},
37 }
38 for i, tt := range tests {
39 f := match.Name(tt.name)
40 if match := f(tt.desc); match != tt.match {
41 t.Errorf("%d: mismatched, got %v expected %v for desc %#v name %s", i, match, tt.match, tt.desc, tt.name)
42 }
43 }
44 }
45
46 func TestAnnotation(t *testing.T) {
47 tests := []struct {
48 desc v1.Descriptor
49 key string
50 value string
51 match bool
52 }{
53 {v1.Descriptor{Annotations: map[string]string{"foo": "bar"}}, "foo", "bar", true},
54 {v1.Descriptor{Annotations: map[string]string{"foo": "bar"}}, "bar", "foo", false},
55 {v1.Descriptor{Annotations: map[string]string{}}, "foo", "bar", false},
56 {v1.Descriptor{Annotations: nil}, "foo", "bar", false},
57 {v1.Descriptor{}, "foo", "bar", false},
58 }
59 for i, tt := range tests {
60 f := match.Annotation(tt.key, tt.value)
61 if match := f(tt.desc); match != tt.match {
62 t.Errorf("%d: mismatched, got %v expected %v for desc %#v annotation %s:%s", i, match, tt.match, tt.desc, tt.key, tt.value)
63 }
64 }
65 }
66
67 func TestPlatforms(t *testing.T) {
68 tests := []struct {
69 desc v1.Descriptor
70 platforms []v1.Platform
71 match bool
72 }{
73 {v1.Descriptor{Platform: &v1.Platform{Architecture: "amd64", OS: "linux"}}, []v1.Platform{{Architecture: "amd64", OS: "darwin"}, {Architecture: "amd64", OS: "linux"}}, true},
74 {v1.Descriptor{Platform: &v1.Platform{Architecture: "amd64", OS: "linux"}}, []v1.Platform{{Architecture: "arm64", OS: "linux"}, {Architecture: "s390x", OS: "linux"}}, false},
75 {v1.Descriptor{Platform: &v1.Platform{OS: "linux"}}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
76 {v1.Descriptor{Platform: &v1.Platform{}}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
77 {v1.Descriptor{Platform: nil}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
78 {v1.Descriptor{}, []v1.Platform{{Architecture: "arm64", OS: "linux"}}, false},
79 }
80 for i, tt := range tests {
81 f := match.Platforms(tt.platforms...)
82 if match := f(tt.desc); match != tt.match {
83 t.Errorf("%d: mismatched, got %v expected %v for desc %#v platform %#v", i, match, tt.match, tt.desc, tt.platforms)
84 }
85 }
86 }
87
88 func TestMediaTypes(t *testing.T) {
89 tests := []struct {
90 desc v1.Descriptor
91 mediaTypes []string
92 match bool
93 }{
94 {v1.Descriptor{MediaType: types.OCIImageIndex}, []string{string(types.OCIImageIndex)}, true},
95 {v1.Descriptor{MediaType: types.OCIImageIndex}, []string{string(types.OCIManifestSchema1)}, false},
96 {v1.Descriptor{MediaType: types.OCIImageIndex}, []string{string(types.OCIManifestSchema1), string(types.OCIImageIndex)}, true},
97 {v1.Descriptor{MediaType: types.OCIImageIndex}, []string{"a", "b"}, false},
98 {v1.Descriptor{}, []string{string(types.OCIManifestSchema1), string(types.OCIImageIndex)}, false},
99 }
100 for i, tt := range tests {
101 f := match.MediaTypes(tt.mediaTypes...)
102 if match := f(tt.desc); match != tt.match {
103 t.Errorf("%d: mismatched, got %v expected %v for desc %#v mediaTypes %#v", i, match, tt.match, tt.desc, tt.mediaTypes)
104 }
105 }
106 }
107
108 func TestDigests(t *testing.T) {
109 hashes := []string{
110 "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
111 "abcde1111111222f0123456789abcdef0123456789abcdef0123456789abcdef",
112 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
113 }
114 algo := "sha256"
115
116 tests := []struct {
117 desc v1.Descriptor
118 digests []v1.Hash
119 match bool
120 }{
121 {v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[0]}}, []v1.Hash{{Algorithm: algo, Hex: hashes[0]}, {Algorithm: algo, Hex: hashes[1]}}, true},
122 {v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[1]}}, []v1.Hash{{Algorithm: algo, Hex: hashes[0]}, {Algorithm: algo, Hex: hashes[1]}}, true},
123 {v1.Descriptor{Digest: v1.Hash{Algorithm: algo, Hex: hashes[2]}}, []v1.Hash{{Algorithm: algo, Hex: hashes[0]}, {Algorithm: algo, Hex: hashes[1]}}, false},
124 }
125 for i, tt := range tests {
126 f := match.Digests(tt.digests...)
127 if match := f(tt.desc); match != tt.match {
128 t.Errorf("%d: mismatched, got %v expected %v for desc %#v digests %#v", i, match, tt.match, tt.desc, tt.digests)
129 }
130 }
131 }
132
View as plain text