1
16
17 package registry
18
19 import (
20 "reflect"
21 "testing"
22 "time"
23
24 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
25
26 "helm.sh/helm/v3/pkg/chart"
27 helmtime "helm.sh/helm/v3/pkg/time"
28 )
29
30 func TestGenerateOCIChartAnnotations(t *testing.T) {
31
32 nowString := helmtime.Now().Format(time.RFC3339)
33
34 tests := []struct {
35 name string
36 chart *chart.Metadata
37 expect map[string]string
38 }{
39 {
40 "Baseline chart",
41 &chart.Metadata{
42 Name: "oci",
43 Version: "0.0.1",
44 },
45 map[string]string{
46 "org.opencontainers.image.title": "oci",
47 "org.opencontainers.image.version": "0.0.1",
48 "org.opencontainers.image.created": nowString,
49 },
50 },
51 {
52 "Simple chart values",
53 &chart.Metadata{
54 Name: "oci",
55 Version: "0.0.1",
56 Description: "OCI Helm Chart",
57 Home: "https://helm.sh",
58 },
59 map[string]string{
60 "org.opencontainers.image.title": "oci",
61 "org.opencontainers.image.version": "0.0.1",
62 "org.opencontainers.image.created": nowString,
63 "org.opencontainers.image.description": "OCI Helm Chart",
64 "org.opencontainers.image.url": "https://helm.sh",
65 },
66 },
67 {
68 "Maintainer without email",
69 &chart.Metadata{
70 Name: "oci",
71 Version: "0.0.1",
72 Description: "OCI Helm Chart",
73 Home: "https://helm.sh",
74 Maintainers: []*chart.Maintainer{
75 {
76 Name: "John Snow",
77 },
78 },
79 },
80 map[string]string{
81 "org.opencontainers.image.title": "oci",
82 "org.opencontainers.image.version": "0.0.1",
83 "org.opencontainers.image.created": nowString,
84 "org.opencontainers.image.description": "OCI Helm Chart",
85 "org.opencontainers.image.url": "https://helm.sh",
86 "org.opencontainers.image.authors": "John Snow",
87 },
88 },
89 {
90 "Maintainer with email",
91 &chart.Metadata{
92 Name: "oci",
93 Version: "0.0.1",
94 Description: "OCI Helm Chart",
95 Home: "https://helm.sh",
96 Maintainers: []*chart.Maintainer{
97 {Name: "John Snow", Email: "john@winterfell.com"},
98 },
99 },
100 map[string]string{
101 "org.opencontainers.image.title": "oci",
102 "org.opencontainers.image.version": "0.0.1",
103 "org.opencontainers.image.created": nowString,
104 "org.opencontainers.image.description": "OCI Helm Chart",
105 "org.opencontainers.image.url": "https://helm.sh",
106 "org.opencontainers.image.authors": "John Snow (john@winterfell.com)",
107 },
108 },
109 {
110 "Multiple Maintainers",
111 &chart.Metadata{
112 Name: "oci",
113 Version: "0.0.1",
114 Description: "OCI Helm Chart",
115 Home: "https://helm.sh",
116 Maintainers: []*chart.Maintainer{
117 {Name: "John Snow", Email: "john@winterfell.com"},
118 {Name: "Jane Snow"},
119 },
120 },
121 map[string]string{
122 "org.opencontainers.image.title": "oci",
123 "org.opencontainers.image.version": "0.0.1",
124 "org.opencontainers.image.created": nowString,
125 "org.opencontainers.image.description": "OCI Helm Chart",
126 "org.opencontainers.image.url": "https://helm.sh",
127 "org.opencontainers.image.authors": "John Snow (john@winterfell.com), Jane Snow",
128 },
129 },
130 {
131 "Chart with Sources",
132 &chart.Metadata{
133 Name: "oci",
134 Version: "0.0.1",
135 Description: "OCI Helm Chart",
136 Sources: []string{
137 "https://github.com/helm/helm",
138 },
139 },
140 map[string]string{
141 "org.opencontainers.image.title": "oci",
142 "org.opencontainers.image.version": "0.0.1",
143 "org.opencontainers.image.created": nowString,
144 "org.opencontainers.image.description": "OCI Helm Chart",
145 "org.opencontainers.image.source": "https://github.com/helm/helm",
146 },
147 },
148 }
149
150 for _, tt := range tests {
151
152 result := generateChartOCIAnnotations(tt.chart, nowString)
153
154 if !reflect.DeepEqual(tt.expect, result) {
155 t.Errorf("%s: expected map %v, got %v", tt.name, tt.expect, result)
156 }
157
158 }
159 }
160
161 func TestGenerateOCIAnnotations(t *testing.T) {
162
163 nowString := helmtime.Now().Format(time.RFC3339)
164
165 tests := []struct {
166 name string
167 chart *chart.Metadata
168 expect map[string]string
169 }{
170 {
171 "Baseline chart",
172 &chart.Metadata{
173 Name: "oci",
174 Version: "0.0.1",
175 },
176 map[string]string{
177 "org.opencontainers.image.title": "oci",
178 "org.opencontainers.image.version": "0.0.1",
179 "org.opencontainers.image.created": nowString,
180 },
181 },
182 {
183 "Simple chart values with custom Annotations",
184 &chart.Metadata{
185 Name: "oci",
186 Version: "0.0.1",
187 Description: "OCI Helm Chart",
188 Annotations: map[string]string{
189 "extrakey": "extravlue",
190 "anotherkey": "anothervalue",
191 },
192 },
193 map[string]string{
194 "org.opencontainers.image.title": "oci",
195 "org.opencontainers.image.version": "0.0.1",
196 "org.opencontainers.image.description": "OCI Helm Chart",
197 "org.opencontainers.image.created": nowString,
198 "extrakey": "extravlue",
199 "anotherkey": "anothervalue",
200 },
201 },
202 {
203 "Verify Chart Name and Version cannot be overridden from annotations",
204 &chart.Metadata{
205 Name: "oci",
206 Version: "0.0.1",
207 Description: "OCI Helm Chart",
208 Annotations: map[string]string{
209 "org.opencontainers.image.title": "badchartname",
210 "org.opencontainers.image.version": "1.0.0",
211 "extrakey": "extravlue",
212 },
213 },
214 map[string]string{
215 "org.opencontainers.image.title": "oci",
216 "org.opencontainers.image.version": "0.0.1",
217 "org.opencontainers.image.description": "OCI Helm Chart",
218 "org.opencontainers.image.created": nowString,
219 "extrakey": "extravlue",
220 },
221 },
222 }
223
224 for _, tt := range tests {
225
226 result := generateOCIAnnotations(tt.chart, nowString)
227
228 if !reflect.DeepEqual(tt.expect, result) {
229 t.Errorf("%s: expected map %v, got %v", tt.name, tt.expect, result)
230 }
231
232 }
233 }
234
235 func TestGenerateOCICreatedAnnotations(t *testing.T) {
236
237 nowTime := helmtime.Now()
238 nowTimeString := nowTime.Format(time.RFC3339)
239
240 chart := &chart.Metadata{
241 Name: "oci",
242 Version: "0.0.1",
243 }
244
245 result := generateOCIAnnotations(chart, nowTimeString)
246
247
248 if _, ok := result[ocispec.AnnotationCreated]; !ok {
249 t.Errorf("%s annotation not created", ocispec.AnnotationCreated)
250 }
251
252
253 if _, err := helmtime.Parse(time.RFC3339, result[ocispec.AnnotationCreated]); err != nil {
254 t.Errorf("%s annotation with value '%s' not in RFC3339 format", ocispec.AnnotationCreated, result[ocispec.AnnotationCreated])
255 }
256
257
258 result = generateOCIAnnotations(chart, "")
259
260
261 if _, ok := result[ocispec.AnnotationCreated]; !ok {
262 t.Errorf("%s annotation not created", ocispec.AnnotationCreated)
263 }
264
265 if createdTimeAnnotation, err := helmtime.Parse(time.RFC3339, result[ocispec.AnnotationCreated]); err != nil {
266 t.Errorf("%s annotation with value '%s' not in RFC3339 format", ocispec.AnnotationCreated, result[ocispec.AnnotationCreated])
267
268
269 if !nowTime.Before(createdTimeAnnotation) {
270 t.Errorf("%s annotation with value '%s' not configured properly. Annotation value is not after %s", ocispec.AnnotationCreated, result[ocispec.AnnotationCreated], nowTimeString)
271 }
272
273 }
274
275 }
276
View as plain text