...

Source file src/github.com/google/go-containerregistry/internal/compression/compression_test.go

Documentation: github.com/google/go-containerregistry/internal/compression

     1  // Copyright 2020 Google LLC All Rights Reserved.
     2  //
     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  package compression
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"testing"
    21  
    22  	"github.com/google/go-containerregistry/internal/and"
    23  	"github.com/google/go-containerregistry/internal/gzip"
    24  	"github.com/google/go-containerregistry/internal/zstd"
    25  )
    26  
    27  type Compressor = func(rc io.ReadCloser) io.ReadCloser
    28  type Decompressor = func(rc io.ReadCloser) (io.ReadCloser, error)
    29  
    30  func testPeekCompression(t *testing.T,
    31  	compressionExpected string,
    32  	compress Compressor,
    33  	decompress Decompressor,
    34  ) {
    35  	content := "This is the input string."
    36  	contentBuf := bytes.NewBufferString(content)
    37  
    38  	compressed := compress(io.NopCloser(contentBuf))
    39  	compressionDetected, pr, err := PeekCompression(compressed)
    40  	if err != nil {
    41  		t.Error("PeekCompression() =", err)
    42  	}
    43  
    44  	if got := string(compressionDetected); got != compressionExpected {
    45  		t.Errorf("PeekCompression(); got %q, content %q", got, compressionExpected)
    46  	}
    47  
    48  	decompressed, err := decompress(withCloser(pr, compressed))
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	b, err := io.ReadAll(decompressed)
    54  	if err != nil {
    55  		t.Error("ReadAll() =", err)
    56  	}
    57  
    58  	if got := string(b); got != content {
    59  		t.Errorf("ReadAll(); got %q, content %q", got, content)
    60  	}
    61  }
    62  
    63  func TestPeekCompression(t *testing.T) {
    64  	testPeekCompression(t, "gzip", gzip.ReadCloser, gzip.UnzipReadCloser)
    65  	testPeekCompression(t, "zstd", zstd.ReadCloser, zstd.UnzipReadCloser)
    66  
    67  	nopCompress := func(rc io.ReadCloser) io.ReadCloser { return rc }
    68  	nopDecompress := func(rc io.ReadCloser) (io.ReadCloser, error) { return rc, nil }
    69  
    70  	testPeekCompression(t, "none", nopCompress, nopDecompress)
    71  }
    72  
    73  func withCloser(pr PeekReader, rc io.ReadCloser) io.ReadCloser {
    74  	return &and.ReadCloser{
    75  		Reader:    pr,
    76  		CloseFunc: rc.Close,
    77  	}
    78  }
    79  

View as plain text