Skip to content

Commit 1480aaf

Browse files
committed
verify every layer exists - optimized
Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com>
1 parent dfeb7a6 commit 1480aaf

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

integration/convert_test.go

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,50 @@ func TestConvertWithForceRecreateZtocs(t *testing.T) {
101101
func validateConversion(t *testing.T, sh *shell.Shell, originalDigest, convertedDigest string) {
102102
t.Helper()
103103

104+
// Helper function to get digest label mapping
105+
getDigestLabelMapping := func(manifestDigest string) (map[string][]string, error) {
106+
digestMap := make(map[string][]string)
107+
108+
manifestLabelOutput := sh.O("ctr", "content", "label", manifestDigest)
109+
manifestLabels := strings.Split(strings.TrimSpace(string(manifestLabelOutput)), ",")
110+
111+
if len(manifestLabels) <= 0 {
112+
return nil, fmt.Errorf("manifest does not contain any labels")
113+
}
114+
115+
for _, label := range manifestLabels {
116+
keyVal := strings.Split(label, "=")
117+
if len(keyVal) != 2 {
118+
continue
119+
}
120+
digestMap[keyVal[1]] = append(digestMap[keyVal[1]], keyVal[0])
121+
}
122+
return digestMap, nil
123+
}
124+
125+
// Helper function to verify image layers contain required labels
126+
verifyImageLayersContainsLabel := func(digestMap map[string][]string, manifest ocispec.Manifest) (bool, error) {
127+
for i := 0; i < len(manifest.Layers); i++ {
128+
layerDigest := manifest.Layers[i].Digest.String()
129+
if labels, ok := digestMap[layerDigest]; ok {
130+
found := false
131+
for _, label := range labels {
132+
if strings.Contains(label, "containerd.io/gc.ref.content") {
133+
found = true
134+
break
135+
}
136+
}
137+
if !found {
138+
return false, fmt.Errorf("digest %s does not have associated gc label", layerDigest)
139+
}
140+
} else {
141+
// Layer digest not found in digestMap
142+
return false, fmt.Errorf("digest %s not found in digestMap", layerDigest)
143+
}
144+
}
145+
return true, nil
146+
}
147+
104148
if originalDigest == convertedDigest {
105149
t.Fatalf("conversion did not change the digest: %s", originalDigest)
106150
}
@@ -199,7 +243,7 @@ func validateConversion(t *testing.T, sh *shell.Shell, originalDigest, converted
199243
}
200244

201245
// get labels associated with the manifest and extract the digest
202-
configDigest, err := getDigestLabelMapping(sh, manifestDesc.Digest.String())
246+
configDigest, err := getDigestLabelMapping(manifestDesc.Digest.String())
203247
if err != nil {
204248
t.Errorf("failed to get config digest from manifest: %v", err)
205249
continue
@@ -214,49 +258,6 @@ func validateConversion(t *testing.T, sh *shell.Shell, originalDigest, converted
214258

215259
}
216260

217-
func getDigestLabelMapping(sh *shell.Shell, manifestDigest string) (map[string][]string, error) {
218-
digestMap := make(map[string][]string)
219-
220-
manifestLabelOutput := sh.O("ctr", "content", "label", manifestDigest)
221-
manifestLabels := strings.Split(strings.TrimSpace(string(manifestLabelOutput)), ",")
222-
223-
if len(manifestLabels) <= 0 {
224-
return nil, fmt.Errorf("manifest does not contain any labels")
225-
}
226-
227-
for _, label := range manifestLabels {
228-
keyVal := strings.Split(label, "=")
229-
if len(keyVal) != 2 {
230-
continue
231-
} else {
232-
digestMap[keyVal[1]] = append(digestMap[keyVal[1]], keyVal[0])
233-
}
234-
}
235-
return digestMap, nil
236-
}
237-
238-
func verifyImageLayersContainsLabel(digestMap map[string][]string, manifest ocispec.Manifest) (bool, error) {
239-
for i := 0; i < len(manifest.Layers); i = i + 1 {
240-
layerDigest := manifest.Layers[i].Digest.String()
241-
if labels, ok := digestMap[layerDigest]; ok {
242-
found := false
243-
for _, label := range labels {
244-
if strings.Contains(label, "containerd.io/gc.ref.content") {
245-
found = true
246-
break
247-
}
248-
}
249-
if !found {
250-
return false, fmt.Errorf("digest %s does not have associated gc label", layerDigest)
251-
}
252-
} else {
253-
// Layer digest not found in digestMap
254-
return false, fmt.Errorf("digest %s not found in digestMap", layerDigest)
255-
}
256-
}
257-
return true, nil
258-
}
259-
260261
func TestConvert(t *testing.T) {
261262
sh, done := newSnapshotterBaseShell(t)
262263
defer done()

0 commit comments

Comments
 (0)