github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/storage/compress_test.go (about)

     1  // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
     2  
     3  package storage
     4  
     5  import (
     6  	"context"
     7  	"io"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	. "github.com/pingcap/check"
    13  )
    14  
    15  func (r *testStorageSuite) TestWithCompressReadWriteFile(c *C) {
    16  	dir := c.MkDir()
    17  	backend, err := ParseBackend("local://"+filepath.ToSlash(dir), nil)
    18  	c.Assert(err, IsNil)
    19  	ctx := context.Background()
    20  	storage, err := Create(ctx, backend, true)
    21  	c.Assert(err, IsNil)
    22  	storage = WithCompression(storage, Gzip)
    23  	name := "with compress test"
    24  	content := "hello,world!"
    25  	fileName := strings.ReplaceAll(name, " ", "-") + ".txt.gz"
    26  	err = storage.WriteFile(ctx, fileName, []byte(content))
    27  	c.Assert(err, IsNil)
    28  
    29  	// make sure compressed file is written correctly
    30  	file, err := os.Open(filepath.Join(dir, fileName))
    31  	c.Assert(err, IsNil)
    32  	uncompressedFile, err := newCompressReader(Gzip, file)
    33  	c.Assert(err, IsNil)
    34  	newContent, err := io.ReadAll(uncompressedFile)
    35  	c.Assert(err, IsNil)
    36  	c.Assert(string(newContent), Equals, content)
    37  
    38  	// test withCompression ReadFile
    39  	newContent, err = storage.ReadFile(ctx, fileName)
    40  	c.Assert(err, IsNil)
    41  	c.Assert(string(newContent), Equals, content)
    42  }