Skip to content

Commit 1cdcd82

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

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

integration/convert_test.go

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,57 @@ 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+
// Helper function to check if a digest has the required label
128+
checkDigestLabels := func(digest string) error {
129+
if labels, ok := digestMap[digest]; ok {
130+
for _, label := range labels {
131+
if strings.Contains(label, "containerd.io/gc.ref.content") {
132+
return nil // Found the required label
133+
}
134+
}
135+
return fmt.Errorf("digest %s does not have associated gc label", digest)
136+
}
137+
return fmt.Errorf("digest %s not found in digestMap", digest)
138+
}
139+
140+
// Check all layer digests
141+
for _, layer := range manifest.Layers {
142+
if err := checkDigestLabels(layer.Digest.String()); err != nil {
143+
return false, err
144+
}
145+
}
146+
147+
// Check config digest
148+
if err := checkDigestLabels(manifest.Config.Digest.String()); err != nil {
149+
return false, err
150+
}
151+
152+
return true, nil
153+
}
154+
104155
if originalDigest == convertedDigest {
105156
t.Fatalf("conversion did not change the digest: %s", originalDigest)
106157
}
@@ -199,13 +250,13 @@ func validateConversion(t *testing.T, sh *shell.Shell, originalDigest, converted
199250
}
200251

201252
// get labels associated with the manifest and extract the digest
202-
configDigest, err := getDigestLabelMapping(sh, manifestDesc.Digest.String())
253+
digestLabelMap, err := getDigestLabelMapping(manifestDesc.Digest.String())
203254
if err != nil {
204255
t.Errorf("failed to get config digest from manifest: %v", err)
205256
continue
206257
}
207258

208-
_, err = verifyImageLayersContainsLabel(configDigest, manifest)
259+
_, err = verifyImageLayersContainsLabel(digestLabelMap, manifest)
209260
if err != nil {
210261
t.Errorf("error verifying layers: %v", err)
211262
continue
@@ -214,49 +265,6 @@ func validateConversion(t *testing.T, sh *shell.Shell, originalDigest, converted
214265

215266
}
216267

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-
260268
func TestConvert(t *testing.T) {
261269
sh, done := newSnapshotterBaseShell(t)
262270
defer done()

0 commit comments

Comments
 (0)