1 package mutate_test
2
3 import (
4 "testing"
5
6 "github.com/google/go-containerregistry/pkg/name"
7 v1 "github.com/google/go-containerregistry/pkg/v1"
8 "github.com/stretchr/testify/assert"
9
10 "edge-infra.dev/pkg/f8n/warehouse"
11 "edge-infra.dev/pkg/f8n/warehouse/oci"
12 "edge-infra.dev/pkg/f8n/warehouse/oci/match"
13 "edge-infra.dev/pkg/f8n/warehouse/oci/mutate"
14 "edge-infra.dev/pkg/f8n/warehouse/pallet"
15 )
16
17 func TestName(t *testing.T) {
18 var (
19 certMgr oci.Artifact
20 shoot oci.Artifact
21
22 err error
23 )
24
25 shoot, err = path.Get(name.MustParseReference("shoot:latest"))
26 if err != nil {
27 t.Fatal(err)
28 }
29 certMgr, err = path.Get(name.MustParseReference("cert-manager:latest"))
30 if err != nil {
31 t.Fatal(err)
32 }
33
34 t.Run("Image", func(t *testing.T) {
35 name := "certy-boi"
36 updated, err := mutate.Name(certMgr, name)
37 assert.NoError(t, err)
38 checkImgName(t, updated, name)
39
40 p, err := pallet.New(certMgr)
41 assert.NoError(t, err)
42 updated, err = mutate.Name(p, name)
43 assert.NoError(t, err)
44 checkImgName(t, updated, name)
45 })
46
47 t.Run("Image_Index", func(t *testing.T) {
48 name := "bamboo-boi"
49 descsToReplace, err := match.FindManifests(shoot.(v1.ImageIndex), match.Annotation(
50 warehouse.AnnotationRefName,
51 "shoot",
52 ))
53 assert.NoError(t, err)
54
55 updated, err := mutate.Name(shoot, name)
56 assert.NoError(t, err)
57 checkIdxName(t, updated.(v1.ImageIndex), name, descsToReplace)
58
59 p, err := pallet.New(shoot)
60 assert.NoError(t, err)
61 updated, err = mutate.Name(p, name)
62 assert.NoError(t, err)
63 checkIdxName(t, updated.(v1.ImageIndex), name, descsToReplace)
64 })
65 }
66
67 func checkImgName(t *testing.T, a oci.Artifact, name string) {
68 t.Helper()
69
70 annos, err := oci.Annotations(a)
71 assert.NoError(t, err)
72 assert.Equal(t, name, annos[warehouse.AnnotationName])
73 }
74
75 func checkIdxName(t *testing.T, a v1.ImageIndex, name string, replaced []v1.Descriptor) {
76 t.Helper()
77
78 m, err := a.IndexManifest()
79 assert.NoError(t, err)
80 assert.Equal(t, name, m.Annotations[warehouse.AnnotationName])
81
82 for _, r := range replaced {
83 found := false
84
85 for _, d := range m.Manifests {
86 if d.Annotations[warehouse.AnnotationRefName] == r.Annotations[warehouse.AnnotationRefName] {
87 t.Error("found descriptor that should have been replaced", d)
88 }
89 if d.Annotations[warehouse.AnnotationRefName] == name {
90 found = true
91 if found {
92
93 img, err := oci.ArtifactFromIdx(a, d)
94 assert.NoError(t, err)
95 checkImgName(t, img, name)
96 }
97 }
98 }
99
100 if !found {
101 t.Error("didnt find new descriptor")
102 }
103 }
104 }
105
View as plain text