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

     1  // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
     2  
     3  package storage
     4  
     5  import (
     6  	"net/url"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	. "github.com/pingcap/check"
    12  	backuppb "github.com/pingcap/kvproto/pkg/backup"
    13  )
    14  
    15  func Test(t *testing.T) {
    16  	TestingT(t)
    17  }
    18  
    19  type testStorageSuite struct{}
    20  
    21  var _ = Suite(&testStorageSuite{})
    22  
    23  func (r *testStorageSuite) TestCreateStorage(c *C) {
    24  	_, err := ParseBackend("1invalid:", nil)
    25  	c.Assert(err, ErrorMatches, "parse (.*)1invalid:(.*): first path segment in URL cannot contain colon")
    26  
    27  	_, err = ParseBackend("net:storage", nil)
    28  	c.Assert(err, ErrorMatches, "storage net not support yet.*")
    29  
    30  	s, err := ParseBackend("local:///tmp/storage", nil)
    31  	c.Assert(err, IsNil)
    32  	c.Assert(s.GetLocal().GetPath(), Equals, "/tmp/storage")
    33  
    34  	s, err = ParseBackend("file:///tmp/storage", nil)
    35  	c.Assert(err, IsNil)
    36  	c.Assert(s.GetLocal().GetPath(), Equals, "/tmp/storage")
    37  
    38  	s, err = ParseBackend("noop://", nil)
    39  	c.Assert(err, IsNil)
    40  	c.Assert(s.GetNoop(), NotNil)
    41  
    42  	_, err = ParseBackend("s3:///bucket/more/prefix/", &BackendOptions{})
    43  	c.Assert(err, ErrorMatches, `please specify the bucket for s3 in s3:///bucket/more/prefix/.*`)
    44  
    45  	s3opt := &BackendOptions{
    46  		S3: S3BackendOptions{
    47  			Endpoint: "https://s3.example.com/",
    48  		},
    49  	}
    50  	s, err = ParseBackend("s3://bucket2/prefix/", s3opt)
    51  	c.Assert(err, IsNil)
    52  	s3 := s.GetS3()
    53  	c.Assert(s3, NotNil)
    54  	c.Assert(s3.Bucket, Equals, "bucket2")
    55  	c.Assert(s3.Prefix, Equals, "prefix")
    56  	c.Assert(s3.Endpoint, Equals, "https://s3.example.com/")
    57  	c.Assert(s3.ForcePathStyle, IsFalse)
    58  
    59  	// nolint:lll
    60  	s, err = ParseBackend(`s3://bucket3/prefix/path?endpoint=https://127.0.0.1:9000&force_path_style=0&SSE=aws:kms&sse-kms-key-id=TestKey&xyz=abc`, nil)
    61  	c.Assert(err, IsNil)
    62  	s3 = s.GetS3()
    63  	c.Assert(s3, NotNil)
    64  	c.Assert(s3.Bucket, Equals, "bucket3")
    65  	c.Assert(s3.Prefix, Equals, "prefix/path")
    66  	c.Assert(s3.Endpoint, Equals, "https://127.0.0.1:9000")
    67  	c.Assert(s3.ForcePathStyle, IsFalse)
    68  	c.Assert(s3.Sse, Equals, "aws:kms")
    69  	c.Assert(s3.SseKmsKeyId, Equals, "TestKey")
    70  
    71  	// special character in access keys
    72  	s, err = ParseBackend(`s3://bucket4/prefix/path?access-key=NXN7IPIOSAAKDEEOLMAF&secret-access-key=nREY/7Dt+PaIbYKrKlEEMMF/ExCiJEX=XMLPUANw`, nil)
    73  	c.Assert(err, IsNil)
    74  	s3 = s.GetS3()
    75  	c.Assert(s3, NotNil)
    76  	c.Assert(s3.Bucket, Equals, "bucket4")
    77  	c.Assert(s3.Prefix, Equals, "prefix/path")
    78  	c.Assert(s3.AccessKey, Equals, "NXN7IPIOSAAKDEEOLMAF")
    79  	c.Assert(s3.SecretAccessKey, Equals, "nREY/7Dt+PaIbYKrKlEEMMF/ExCiJEX=XMLPUANw")
    80  	c.Assert(s3.ForcePathStyle, IsTrue)
    81  
    82  	gcsOpt := &BackendOptions{
    83  		GCS: GCSBackendOptions{
    84  			Endpoint: "https://gcs.example.com/",
    85  		},
    86  	}
    87  	s, err = ParseBackend("gcs://bucket2/prefix/", gcsOpt)
    88  	c.Assert(err, IsNil)
    89  	gcs := s.GetGcs()
    90  	c.Assert(gcs, NotNil)
    91  	c.Assert(gcs.Bucket, Equals, "bucket2")
    92  	c.Assert(gcs.Prefix, Equals, "prefix")
    93  	c.Assert(gcs.Endpoint, Equals, "https://gcs.example.com/")
    94  	c.Assert(gcs.CredentialsBlob, Equals, "")
    95  
    96  	s, err = ParseBackend("gcs://bucket2", gcsOpt)
    97  	c.Assert(err, IsNil)
    98  	gcs = s.GetGcs()
    99  	c.Assert(gcs, NotNil)
   100  	c.Assert(gcs.Bucket, Equals, "bucket2")
   101  	c.Assert(gcs.Prefix, Equals, "")
   102  	c.Assert(gcs.Endpoint, Equals, "https://gcs.example.com/")
   103  	c.Assert(gcs.CredentialsBlob, Equals, "")
   104  
   105  	var credFeilPerm os.FileMode = 0o600
   106  	fakeCredentialsFile := filepath.Join(c.MkDir(), "fakeCredentialsFile")
   107  	err = os.WriteFile(fakeCredentialsFile, []byte("fakeCredentials"), credFeilPerm)
   108  	c.Assert(err, IsNil)
   109  
   110  	gcsOpt.GCS.CredentialsFile = fakeCredentialsFile
   111  
   112  	s, err = ParseBackend("gcs://bucket/more/prefix/", gcsOpt)
   113  	c.Assert(err, IsNil)
   114  	gcs = s.GetGcs()
   115  	c.Assert(gcs, NotNil)
   116  	c.Assert(gcs.Bucket, Equals, "bucket")
   117  	c.Assert(gcs.Prefix, Equals, "more/prefix")
   118  	c.Assert(gcs.Endpoint, Equals, "https://gcs.example.com/")
   119  	c.Assert(gcs.CredentialsBlob, Equals, "fakeCredentials")
   120  
   121  	err = os.WriteFile(fakeCredentialsFile, []byte("fakeCreds2"), credFeilPerm)
   122  	c.Assert(err, IsNil)
   123  	s, err = ParseBackend("gs://bucket4/backup/?credentials-file="+url.QueryEscape(fakeCredentialsFile), nil)
   124  	c.Assert(err, IsNil)
   125  	gcs = s.GetGcs()
   126  	c.Assert(gcs, NotNil)
   127  	c.Assert(gcs.Bucket, Equals, "bucket4")
   128  	c.Assert(gcs.Prefix, Equals, "backup")
   129  	c.Assert(gcs.CredentialsBlob, Equals, "fakeCreds2")
   130  
   131  	s, err = ParseBackend("/test", nil)
   132  	c.Assert(err, IsNil)
   133  	local := s.GetLocal()
   134  	c.Assert(local, NotNil)
   135  	expectedLocalPath, err := filepath.Abs("/test")
   136  	c.Assert(err, IsNil)
   137  	c.Assert(local.GetPath(), Equals, expectedLocalPath)
   138  }
   139  
   140  func (r *testStorageSuite) TestFormatBackendURL(c *C) {
   141  	url := FormatBackendURL(&backuppb.StorageBackend{
   142  		Backend: &backuppb.StorageBackend_Local{
   143  			Local: &backuppb.Local{Path: "/tmp/file"},
   144  		},
   145  	})
   146  	c.Assert(url.String(), Equals, "local:///tmp/file")
   147  
   148  	url = FormatBackendURL(&backuppb.StorageBackend{
   149  		Backend: &backuppb.StorageBackend_Noop{
   150  			Noop: &backuppb.Noop{},
   151  		},
   152  	})
   153  	c.Assert(url.String(), Equals, "noop:///")
   154  
   155  	url = FormatBackendURL(&backuppb.StorageBackend{
   156  		Backend: &backuppb.StorageBackend_S3{
   157  			S3: &backuppb.S3{
   158  				Bucket:   "bucket",
   159  				Prefix:   "/some prefix/",
   160  				Endpoint: "https://s3.example.com/",
   161  			},
   162  		},
   163  	})
   164  	c.Assert(url.String(), Equals, "s3://bucket/some%20prefix/")
   165  
   166  	url = FormatBackendURL(&backuppb.StorageBackend{
   167  		Backend: &backuppb.StorageBackend_Gcs{
   168  			Gcs: &backuppb.GCS{
   169  				Bucket:   "bucket",
   170  				Prefix:   "/some prefix/",
   171  				Endpoint: "https://gcs.example.com/",
   172  			},
   173  		},
   174  	})
   175  	c.Assert(url.String(), Equals, "gcs://bucket/some%20prefix/")
   176  }