From e70684d949072b8921b9c3d2f2dae1985760860e Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Tue, 23 Feb 2021 22:45:46 +0200 Subject: [PATCH] handle different gzip extensions Signed-off-by: Avi Deitcher --- pkg/content/decompressstore.go | 8 +++- pkg/content/decompressstore_test.go | 62 +++++++++++++++-------------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/pkg/content/decompressstore.go b/pkg/content/decompressstore.go index 42d832a4b..11804355c 100644 --- a/pkg/content/decompressstore.go +++ b/pkg/content/decompressstore.go @@ -124,11 +124,17 @@ func (d DecompressStore) Writer(ctx context.Context, opts ...ctrcontent.WriterOp func checkCompression(mediaType string) (gzip, tar bool, mt string) { mt = mediaType gzipSuffix := "+gzip" + gzipAltSuffix := ".gzip" tarSuffix := ".tar" - if strings.HasSuffix(mt, gzipSuffix) { + switch { + case strings.HasSuffix(mt, gzipSuffix): mt = mt[:len(mt)-len(gzipSuffix)] gzip = true + case strings.HasSuffix(mt, gzipAltSuffix): + mt = mt[:len(mt)-len(gzipAltSuffix)] + gzip = true } + if strings.HasSuffix(mt, tarSuffix) { mt = mt[:len(mt)-len(tarSuffix)] tar = true diff --git a/pkg/content/decompressstore_test.go b/pkg/content/decompressstore_test.go index 0e12a85fb..b1bac0dc0 100644 --- a/pkg/content/decompressstore_test.go +++ b/pkg/content/decompressstore_test.go @@ -25,36 +25,40 @@ func TestDecompressStore(t *testing.T) { } gzipContent := buf.Bytes() gzipContentHash := digest.FromBytes(gzipContent) - gzipDescriptor := ocispec.Descriptor{ - MediaType: fmt.Sprintf("%s+gzip", ocispec.MediaTypeImageConfig), - Digest: gzipContentHash, - Size: int64(len(gzipContent)), - } - memStore := content.NewMemoryStore() - decompressStore := content.NewDecompressStore(memStore, content.WithBlocksize(0)) - ctx := context.Background() - decompressWriter, err := decompressStore.Writer(ctx, ctrcontent.WithDescriptor(gzipDescriptor)) - if err != nil { - t.Fatalf("unable to get a decompress writer: %v", err) - } - n, err := decompressWriter.Write(gzipContent) - if err != nil { - t.Fatalf("failed to write to decompress writer: %v", err) - } - if n != len(gzipContent) { - t.Fatalf("wrote %d instead of expected %d bytes", n, len(gzipContent)) - } - if err := decompressWriter.Commit(ctx, int64(len(gzipContent)), gzipContentHash); err != nil { - t.Fatalf("unexpected error committing decompress writer: %v", err) - } + extensions := []string{"+gzip", ".gzip"} + for _, ext := range extensions { + gzipDescriptor := ocispec.Descriptor{ + MediaType: fmt.Sprintf("%s%s", ocispec.MediaTypeImageConfig, ext), + Digest: gzipContentHash, + Size: int64(len(gzipContent)), + } - // and now we should be able to get the decompressed data from the memory store - _, b, found := memStore.Get(gzipDescriptor) - if !found { - t.Fatalf("failed to get data from underlying memory store: %v", err) - } - if string(b) != string(rawContent) { - t.Errorf("mismatched data in underlying memory store, actual '%s', expected '%s'", b, rawContent) + memStore := content.NewMemoryStore() + decompressStore := content.NewDecompressStore(memStore, content.WithBlocksize(0)) + ctx := context.Background() + decompressWriter, err := decompressStore.Writer(ctx, ctrcontent.WithDescriptor(gzipDescriptor)) + if err != nil { + t.Fatalf("unable to get a decompress writer: %v", err) + } + n, err := decompressWriter.Write(gzipContent) + if err != nil { + t.Fatalf("failed to write to decompress writer: %v", err) + } + if n != len(gzipContent) { + t.Fatalf("wrote %d instead of expected %d bytes", n, len(gzipContent)) + } + if err := decompressWriter.Commit(ctx, int64(len(gzipContent)), gzipContentHash); err != nil { + t.Fatalf("unexpected error committing decompress writer: %v", err) + } + + // and now we should be able to get the decompressed data from the memory store + _, b, found := memStore.Get(gzipDescriptor) + if !found { + t.Fatalf("failed to get data from underlying memory store: %v", err) + } + if string(b) != string(rawContent) { + t.Errorf("mismatched data in underlying memory store, actual '%s', expected '%s'", b, rawContent) + } } }