Skip to content

Commit 49dc309

Browse files
committed
Add tests for OCI layer selector
Signed-off-by: Stefan Prodan <[email protected]>
1 parent 11dc0a3 commit 49dc309

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

controllers/ocirepository_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, obj *sour
457457
}
458458
if !found {
459459
e := serror.NewGeneric(
460-
fmt.Errorf("failed to find layer with media type '%s' in artifact: %w", obj.GetLayerMediaType(), err),
460+
fmt.Errorf("failed to find layer with media type '%s' in artifact", obj.GetLayerMediaType()),
461461
sourcev1.OCILayerOperationFailedReason,
462462
)
463463
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())

controllers/ocirepository_controller_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,109 @@ func TestOCIRepository_Reconcile(t *testing.T) {
248248
}
249249
}
250250

251+
func TestOCIRepository_Reconcile_MediaType(t *testing.T) {
252+
g := NewWithT(t)
253+
254+
// Registry server with public images
255+
tmpDir := t.TempDir()
256+
regServer, err := setupRegistryServer(ctx, tmpDir, registryOptions{})
257+
if err != nil {
258+
g.Expect(err).ToNot(HaveOccurred())
259+
}
260+
261+
podinfoVersions, err := pushMultiplePodinfoImages(regServer.registryHost, "6.1.4", "6.1.5", "6.1.6")
262+
263+
tests := []struct {
264+
name string
265+
url string
266+
tag string
267+
mediaType string
268+
wantErr bool
269+
}{
270+
{
271+
name: "Works with no media type",
272+
url: podinfoVersions["6.1.4"].url,
273+
tag: podinfoVersions["6.1.4"].tag,
274+
},
275+
{
276+
name: "Works with Flux CLI media type",
277+
url: podinfoVersions["6.1.5"].url,
278+
tag: podinfoVersions["6.1.5"].tag,
279+
mediaType: "application/vnd.docker.image.rootfs.diff.tar.gzip",
280+
},
281+
{
282+
name: "Fails with unknown media type",
283+
url: podinfoVersions["6.1.6"].url,
284+
tag: podinfoVersions["6.1.6"].tag,
285+
mediaType: "application/invalid.tar.gzip",
286+
wantErr: true,
287+
},
288+
}
289+
290+
for _, tt := range tests {
291+
t.Run(tt.name, func(t *testing.T) {
292+
293+
g := NewWithT(t)
294+
295+
ns, err := testEnv.CreateNamespace(ctx, "ocirepository-mediatype-test")
296+
g.Expect(err).ToNot(HaveOccurred())
297+
defer func() { g.Expect(testEnv.Delete(ctx, ns)).To(Succeed()) }()
298+
299+
obj := &sourcev1.OCIRepository{
300+
ObjectMeta: metav1.ObjectMeta{
301+
GenerateName: "ocirepository-reconcile",
302+
Namespace: ns.Name,
303+
},
304+
Spec: sourcev1.OCIRepositorySpec{
305+
URL: tt.url,
306+
Interval: metav1.Duration{Duration: 60 * time.Minute},
307+
Reference: &sourcev1.OCIRepositoryRef{
308+
Tag: tt.tag,
309+
},
310+
LayerSelector: &sourcev1.OCILayerSelector{
311+
MediaType: tt.mediaType,
312+
},
313+
},
314+
}
315+
316+
g.Expect(testEnv.Create(ctx, obj)).To(Succeed())
317+
318+
key := client.ObjectKey{Name: obj.Name, Namespace: obj.Namespace}
319+
320+
// Wait for the finalizer to be set
321+
g.Eventually(func() bool {
322+
if err := testEnv.Get(ctx, key, obj); err != nil {
323+
return false
324+
}
325+
return len(obj.Finalizers) > 0
326+
}, timeout).Should(BeTrue())
327+
328+
// Wait for the object to be reconciled
329+
g.Eventually(func() bool {
330+
if err := testEnv.Get(ctx, key, obj); err != nil {
331+
return false
332+
}
333+
readyCondition := conditions.Get(obj, meta.ReadyCondition)
334+
return readyCondition != nil
335+
}, timeout).Should(BeTrue())
336+
337+
g.Expect(conditions.IsReady(obj)).To(BeIdenticalTo(!tt.wantErr))
338+
if tt.wantErr {
339+
g.Expect(conditions.Get(obj, meta.ReadyCondition).Message).Should(ContainSubstring("failed to find layer with media type"))
340+
}
341+
342+
// Wait for the object to be deleted
343+
g.Expect(testEnv.Delete(ctx, obj)).To(Succeed())
344+
g.Eventually(func() bool {
345+
if err := testEnv.Get(ctx, key, obj); err != nil {
346+
return apierrors.IsNotFound(err)
347+
}
348+
return false
349+
}, timeout).Should(BeTrue())
350+
})
351+
}
352+
}
353+
251354
func TestOCIRepository_reconcileSource_authStrategy(t *testing.T) {
252355
type secretOptions struct {
253356
username string

0 commit comments

Comments
 (0)