github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/mydump/reader_test.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package mydump_test
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"io/ioutil"
    20  	"os"
    21  
    22  	"github.com/golang/mock/gomock"
    23  	"github.com/pingcap/br/pkg/storage"
    24  	. "github.com/pingcap/check"
    25  
    26  	. "github.com/pingcap/tidb-lightning/lightning/mydump"
    27  	"github.com/pingcap/tidb-lightning/mock"
    28  )
    29  
    30  //////////////////////////////////////////////////////////
    31  
    32  var _ = Suite(&testMydumpReaderSuite{})
    33  
    34  type testMydumpReaderSuite struct{}
    35  
    36  func (s *testMydumpReaderSuite) SetUpSuite(c *C)    {}
    37  func (s *testMydumpReaderSuite) TearDownSuite(c *C) {}
    38  
    39  func (s *testMydumpReaderSuite) TestExportStatementNoTrailingNewLine(c *C) {
    40  	dir := c.MkDir()
    41  	file, err := ioutil.TempFile(dir, "tidb_lightning_test_reader")
    42  	c.Assert(err, IsNil)
    43  	defer os.Remove(file.Name())
    44  
    45  	store, err := storage.NewLocalStorage(dir)
    46  	c.Assert(err, IsNil)
    47  
    48  	_, err = file.Write([]byte("CREATE DATABASE whatever;"))
    49  	c.Assert(err, IsNil)
    50  	stat, err := file.Stat()
    51  	c.Assert(err, IsNil)
    52  	err = file.Close()
    53  	c.Assert(err, IsNil)
    54  
    55  	f := FileInfo{FileMeta: SourceFileMeta{Path: stat.Name(), FileSize: stat.Size()}}
    56  	data, err := ExportStatement(context.TODO(), store, f, "auto")
    57  	c.Assert(err, IsNil)
    58  	c.Assert(data, DeepEquals, []byte("CREATE DATABASE whatever;"))
    59  }
    60  
    61  func (s *testMydumpReaderSuite) TestExportStatementWithComment(c *C) {
    62  	dir := c.MkDir()
    63  	file, err := ioutil.TempFile(dir, "tidb_lightning_test_reader")
    64  	c.Assert(err, IsNil)
    65  	defer os.Remove(file.Name())
    66  
    67  	_, err = file.Write([]byte(`
    68  		/* whatever blabla 
    69  			multiple lines comment
    70  			multiple lines comment
    71  			multiple lines comment
    72  			multiple lines comment
    73  			multiple lines comment
    74  		 */;
    75  		CREATE DATABASE whatever;  
    76  `))
    77  	c.Assert(err, IsNil)
    78  	stat, err := file.Stat()
    79  	c.Assert(err, IsNil)
    80  	err = file.Close()
    81  	c.Assert(err, IsNil)
    82  
    83  	store, err := storage.NewLocalStorage(dir)
    84  	c.Assert(err, IsNil)
    85  
    86  	f := FileInfo{FileMeta: SourceFileMeta{Path: stat.Name(), FileSize: stat.Size()}}
    87  	data, err := ExportStatement(context.TODO(), store, f, "auto")
    88  	c.Assert(err, IsNil)
    89  	c.Assert(data, DeepEquals, []byte("CREATE DATABASE whatever;"))
    90  }
    91  
    92  func (s *testMydumpReaderSuite) TestExportStatementWithCommentNoTrailingNewLine(c *C) {
    93  	dir := c.MkDir()
    94  	file, err := ioutil.TempFile(dir, "tidb_lightning_test_reader")
    95  	c.Assert(err, IsNil)
    96  	defer os.Remove(file.Name())
    97  
    98  	_, err = file.Write([]byte(`
    99  		/* whatever blabla 
   100  			multiple lines comment
   101  			multiple lines comment
   102  			multiple lines comment
   103  			multiple lines comment
   104  			multiple lines comment
   105  		 */;
   106  		CREATE DATABASE whatever;`))
   107  	c.Assert(err, IsNil)
   108  	stat, err := file.Stat()
   109  	c.Assert(err, IsNil)
   110  	err = file.Close()
   111  	c.Assert(err, IsNil)
   112  
   113  	store, err := storage.NewLocalStorage(dir)
   114  	c.Assert(err, IsNil)
   115  	f := FileInfo{FileMeta: SourceFileMeta{Path: stat.Name(), FileSize: stat.Size()}}
   116  	data, err := ExportStatement(context.TODO(), store, f, "auto")
   117  	c.Assert(err, IsNil)
   118  	c.Assert(data, DeepEquals, []byte("CREATE DATABASE whatever;"))
   119  }
   120  
   121  func (s *testMydumpReaderSuite) TestExportStatementGBK(c *C) {
   122  	dir := c.MkDir()
   123  	file, err := ioutil.TempFile(dir, "tidb_lightning_test_reader")
   124  	c.Assert(err, IsNil)
   125  	defer os.Remove(file.Name())
   126  
   127  	_, err = file.Write([]byte("CREATE TABLE a (b int(11) COMMENT '"))
   128  	c.Assert(err, IsNil)
   129  	// "D7 DC B0 B8 C0 FD" is the GBK encoding of "总案例".
   130  	_, err = file.Write([]byte{0xD7, 0xDC, 0xB0, 0xB8, 0xC0, 0xFD})
   131  	c.Assert(err, IsNil)
   132  	_, err = file.Write([]byte("');\n"))
   133  	c.Assert(err, IsNil)
   134  	stat, err := file.Stat()
   135  	c.Assert(err, IsNil)
   136  	err = file.Close()
   137  	c.Assert(err, IsNil)
   138  
   139  	store, err := storage.NewLocalStorage(dir)
   140  	c.Assert(err, IsNil)
   141  	f := FileInfo{FileMeta: SourceFileMeta{Path: stat.Name(), FileSize: stat.Size()}}
   142  	data, err := ExportStatement(context.TODO(), store, f, "auto")
   143  	c.Assert(err, IsNil)
   144  	c.Assert(data, DeepEquals, []byte("CREATE TABLE a (b int(11) COMMENT '总案例');"))
   145  }
   146  
   147  func (s *testMydumpReaderSuite) TestExportStatementGibberishError(c *C) {
   148  	dir := c.MkDir()
   149  	file, err := ioutil.TempFile(dir, "tidb_lightning_test_reader")
   150  	c.Assert(err, IsNil)
   151  	defer os.Remove(file.Name())
   152  
   153  	_, err = file.Write([]byte("\x9e\x02\xdc\xfbZ/=n\xf3\xf2N8\xc1\xf2\xe9\xaa\xd0\x85\xc5}\x97\x07\xae6\x97\x99\x9c\x08\xcb\xe8;"))
   154  	c.Assert(err, IsNil)
   155  	stat, err := file.Stat()
   156  	c.Assert(err, IsNil)
   157  	err = file.Close()
   158  	c.Assert(err, IsNil)
   159  
   160  	store, err := storage.NewLocalStorage(dir)
   161  	c.Assert(err, IsNil)
   162  
   163  	f := FileInfo{FileMeta: SourceFileMeta{Path: stat.Name(), FileSize: stat.Size()}}
   164  	data, err := ExportStatement(context.TODO(), store, f, "auto")
   165  	c.Assert(data, HasLen, 0)
   166  	c.Assert(err, ErrorMatches, `failed to decode \w* as auto: invalid schema encoding`)
   167  }
   168  
   169  type AlwaysErrorReadSeekCloser struct{}
   170  
   171  func (AlwaysErrorReadSeekCloser) Read([]byte) (int, error) {
   172  	return 0, errors.New("read error!")
   173  }
   174  func (AlwaysErrorReadSeekCloser) Seek(int64, int) (int64, error) {
   175  	return 0, errors.New("seek error!")
   176  }
   177  func (AlwaysErrorReadSeekCloser) Close() error {
   178  	return nil
   179  }
   180  
   181  func (s *testMydumpReaderSuite) TestExportStatementHandleNonEOFError(c *C) {
   182  	controller := gomock.NewController(c)
   183  	defer controller.Finish()
   184  
   185  	ctx := context.TODO()
   186  
   187  	mockStorage := mock.NewMockExternalStorage(controller)
   188  	mockStorage.EXPECT().
   189  		Open(ctx, "no-perm-file").
   190  		Return(AlwaysErrorReadSeekCloser{}, nil)
   191  
   192  	f := FileInfo{FileMeta: SourceFileMeta{Path: "no-perm-file", FileSize: 1}}
   193  	_, err := ExportStatement(ctx, mockStorage, f, "auto")
   194  	c.Assert(err, ErrorMatches, "read error!")
   195  }