Skip to content

Commit 16d71c2

Browse files
Kern WalsterKern--
authored andcommitted
Test invalid conversions
If a SOCI index or image manifest is removed from a converted image, the image should be invalid. This change adds a test to confirm this. This effectively tests that we are actually putting the SOCI index into the converted Image Index. Signed-off-by: Kern Walster <walster@amazon.com>
1 parent f2d8312 commit 16d71c2

2 files changed

Lines changed: 87 additions & 3 deletions

File tree

integration/convert_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ package integration
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"slices"
23+
"strings"
2224
"testing"
2325

2426
"github.com/awslabs/soci-snapshotter/soci"
2527
shell "github.com/awslabs/soci-snapshotter/util/dockershell"
28+
"github.com/containerd/platforms"
2629
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2730
)
2831

@@ -229,3 +232,72 @@ func TestConvertAndPush(t *testing.T) {
229232
})
230233
}
231234
}
235+
236+
func TestInvalidConversion(t *testing.T) {
237+
registryConfig := newRegistryConfig()
238+
sh, done := newShellWithRegistry(t, registryConfig)
239+
defer done()
240+
241+
tests := []struct {
242+
name string
243+
repo string
244+
modifier func(t *testing.T, sh *shell.Shell, img imageInfo)
245+
}{
246+
{
247+
name: "deleting manifest invalidates image",
248+
repo: "manifest",
249+
modifier: func(t *testing.T, sh *shell.Shell, img imageInfo) {
250+
digest, err := getManifestDigest(sh, img.ref, platforms.DefaultSpec())
251+
if err != nil {
252+
t.Fatalf("failed to get manifest digest: %v", err)
253+
}
254+
sh.X("ctr", "content", "delete", digest)
255+
},
256+
},
257+
{
258+
name: "deleting soci index invalidates image",
259+
repo: "sociindex",
260+
modifier: func(t *testing.T, sh *shell.Shell, img imageInfo) {
261+
index, err := getImageIndex(sh, img.ref)
262+
if err != nil {
263+
t.Fatalf("failed to get image index: %v", err)
264+
}
265+
idx := slices.IndexFunc(index.Manifests, func(desc ocispec.Descriptor) bool {
266+
return desc.ArtifactType == soci.SociIndexArtifactTypeV2
267+
})
268+
if idx == -1 {
269+
t.Fatalf("no soci index found")
270+
}
271+
sh.X("ctr", "content", "delete", index.Manifests[idx].Digest.String())
272+
},
273+
},
274+
}
275+
276+
for _, imageName := range convertImages {
277+
t.Run(imageName, func(t *testing.T) {
278+
rebootContainerd(t, sh, "", "")
279+
img := dockerhub(imageName)
280+
281+
sh.X("nerdctl", "pull", "--all-platforms", img.ref)
282+
283+
for _, test := range tests {
284+
t.Run(test.name, func(t *testing.T) {
285+
imageName = fmt.Sprintf("%s/%s", test.repo, imageName)
286+
convertedImg := registryConfig.mirror(imageName)
287+
sh.X("soci", "convert", "--min-layer-size", "0", img.ref, convertedImg.ref)
288+
289+
test.modifier(t, sh, convertedImg)
290+
291+
sh.X("nerdctl", "login", "--username", registryConfig.user, "--password", registryConfig.pass, convertedImg.ref)
292+
out, err := sh.CombinedOLog("nerdctl", "push", "--all-platforms", convertedImg.ref)
293+
if err == nil {
294+
t.Fatalf("expected push to fail")
295+
}
296+
if !strings.Contains(string(out), "not found") {
297+
t.Fatalf("expected push to fail with 'not found' error, got %s", string(out))
298+
}
299+
})
300+
}
301+
})
302+
}
303+
}

integration/util_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,16 +740,28 @@ func generateBasicHtpasswd(user, pass string) ([]byte, error) {
740740
return []byte(user + ":" + string(bpass) + "\n"), nil
741741
}
742742

743+
func getImageIndex(sh *shell.Shell, ref string) (*spec.Index, error) {
744+
content := sh.O("ctr", "content", "get", getImageDigest(sh, ref))
745+
var index spec.Index
746+
err := json.Unmarshal(content, &index)
747+
if err != nil {
748+
return nil, err
749+
}
750+
if !images.IsIndexType(index.MediaType) {
751+
return nil, fmt.Errorf("%s is not an index mediatype", index.MediaType)
752+
}
753+
754+
return &index, nil
755+
}
756+
743757
func getImageDigest(sh *shell.Shell, ref string) string {
744758
buffer := new(bytes.Buffer)
745759
sh.Pipe(buffer, []string{"ctr", "image", "list", "name==" + ref}, []string{"awk", `NR==2{printf "%s", $3}`})
746760
return buffer.String()
747761
}
748762

749763
func getManifestDigest(sh *shell.Shell, ref string, platform spec.Platform) (string, error) {
750-
content := sh.O("ctr", "content", "get", getImageDigest(sh, ref))
751-
var index spec.Index
752-
err := json.Unmarshal(content, &index)
764+
index, err := getImageIndex(sh, ref)
753765
if err != nil {
754766
return "", err
755767
}

0 commit comments

Comments
 (0)