...

Source file src/oras.land/oras-go/pkg/content/decompress_test.go

Documentation: oras.land/oras-go/pkg/content

     1  /*
     2  Copyright The ORAS Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package content_test
    17  
    18  import (
    19  	"bytes"
    20  	"compress/gzip"
    21  	"context"
    22  	"fmt"
    23  	"testing"
    24  
    25  	digest "github.com/opencontainers/go-digest"
    26  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    27  
    28  	"oras.land/oras-go/pkg/content"
    29  )
    30  
    31  func TestDecompressStore(t *testing.T) {
    32  	rawContent := []byte("Hello World!")
    33  	var buf bytes.Buffer
    34  	gw := gzip.NewWriter(&buf)
    35  	if _, err := gw.Write(rawContent); err != nil {
    36  		t.Fatalf("unable to create gzip content for testing: %v", err)
    37  	}
    38  	if err := gw.Close(); err != nil {
    39  		t.Fatalf("unable to close gzip writer creating content for testing: %v", err)
    40  	}
    41  	gzipContent := buf.Bytes()
    42  	gzipContentHash := digest.FromBytes(gzipContent)
    43  
    44  	extensions := []string{"+gzip", ".gzip"}
    45  	for _, ext := range extensions {
    46  		gzipDescriptor := ocispec.Descriptor{
    47  			MediaType: fmt.Sprintf("%s%s", ocispec.MediaTypeImageConfig, ext),
    48  			Digest:    gzipContentHash,
    49  			Size:      int64(len(gzipContent)),
    50  		}
    51  
    52  		memStore := content.NewMemory()
    53  		ctx := context.Background()
    54  		memPusher, _ := memStore.Pusher(ctx, "")
    55  		decompressStore := content.NewDecompress(memPusher, content.WithBlocksize(0))
    56  		decompressWriter, err := decompressStore.Push(ctx, gzipDescriptor)
    57  		if err != nil {
    58  			t.Fatalf("unable to get a decompress writer: %v", err)
    59  		}
    60  		n, err := decompressWriter.Write(gzipContent)
    61  		if err != nil {
    62  			t.Fatalf("failed to write to decompress writer: %v", err)
    63  		}
    64  		if n != len(gzipContent) {
    65  			t.Fatalf("wrote %d instead of expected %d bytes", n, len(gzipContent))
    66  		}
    67  		if err := decompressWriter.Commit(ctx, int64(len(gzipContent)), gzipContentHash); err != nil {
    68  			t.Fatalf("unexpected error committing decompress writer: %v", err)
    69  		}
    70  
    71  		// and now we should be able to get the decompressed data from the memory store
    72  		_, b, found := memStore.Get(gzipDescriptor)
    73  		if !found {
    74  			t.Fatalf("failed to get data from underlying memory store: %v", err)
    75  		}
    76  		if string(b) != string(rawContent) {
    77  			t.Errorf("mismatched data in underlying memory store, actual '%s', expected '%s'", b, rawContent)
    78  		}
    79  	}
    80  }
    81  

View as plain text