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

     1  // Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.
     2  
     3  package metautil
     4  
     5  import (
     6  	"context"
     7  	"crypto/sha256"
     8  	"testing"
     9  
    10  	"github.com/golang/mock/gomock"
    11  	. "github.com/pingcap/check"
    12  	backuppb "github.com/pingcap/kvproto/pkg/backup"
    13  
    14  	mockstorage "github.com/pingcap/br/pkg/mock/storage"
    15  )
    16  
    17  type metaSuit struct{}
    18  
    19  var _ = Suite(&metaSuit{})
    20  
    21  func Test(t *testing.T) { TestingT(t) }
    22  
    23  func checksum(m *backuppb.MetaFile) []byte {
    24  	b, err := m.Marshal()
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  	sum := sha256.Sum256(b)
    29  	return sum[:]
    30  }
    31  
    32  func (m *metaSuit) TestWalkMetaFileEmpty(c *C) {
    33  	files := []*backuppb.MetaFile{}
    34  	collect := func(m *backuppb.MetaFile) { files = append(files, m) }
    35  	err := walkLeafMetaFile(context.Background(), nil, nil, collect)
    36  	c.Assert(err, IsNil)
    37  	c.Assert(files, HasLen, 0)
    38  
    39  	empty := &backuppb.MetaFile{}
    40  	err = walkLeafMetaFile(context.Background(), nil, empty, collect)
    41  	c.Assert(err, IsNil)
    42  	c.Assert(files, HasLen, 1)
    43  	c.Assert(files[0], Equals, empty)
    44  }
    45  
    46  func (m *metaSuit) TestWalkMetaFileLeaf(c *C) {
    47  	leaf := &backuppb.MetaFile{Schemas: []*backuppb.Schema{
    48  		{Db: []byte("db"), Table: []byte("table")},
    49  	}}
    50  	files := []*backuppb.MetaFile{}
    51  	collect := func(m *backuppb.MetaFile) { files = append(files, m) }
    52  	err := walkLeafMetaFile(context.Background(), nil, leaf, collect)
    53  	c.Assert(err, IsNil)
    54  	c.Assert(files, HasLen, 1)
    55  	c.Assert(files[0], Equals, leaf)
    56  }
    57  
    58  func (m *metaSuit) TestWalkMetaFileInvalid(c *C) {
    59  	controller := gomock.NewController(c)
    60  	defer controller.Finish()
    61  	mockStorage := mockstorage.NewMockExternalStorage(controller)
    62  
    63  	ctx := context.Background()
    64  	leaf := &backuppb.MetaFile{Schemas: []*backuppb.Schema{
    65  		{Db: []byte("db"), Table: []byte("table")},
    66  	}}
    67  	mockStorage.EXPECT().ReadFile(ctx, "leaf").Return(leaf.Marshal())
    68  
    69  	root := &backuppb.MetaFile{MetaFiles: []*backuppb.File{
    70  		{Name: "leaf", Sha256: []byte{}},
    71  	}}
    72  
    73  	collect := func(m *backuppb.MetaFile) { panic("unreachable") }
    74  	err := walkLeafMetaFile(ctx, mockStorage, root, collect)
    75  	c.Assert(err, ErrorMatches, ".*ErrInvalidMetaFile.*")
    76  }
    77  
    78  func (m *metaSuit) TestWalkMetaFile(c *C) {
    79  	controller := gomock.NewController(c)
    80  	defer controller.Finish()
    81  	mockStorage := mockstorage.NewMockExternalStorage(controller)
    82  
    83  	ctx := context.Background()
    84  	expect := make([]*backuppb.MetaFile, 0, 6)
    85  	leaf31S1 := &backuppb.MetaFile{Schemas: []*backuppb.Schema{
    86  		{Db: []byte("db31S1"), Table: []byte("table31S1")},
    87  	}}
    88  	mockStorage.EXPECT().ReadFile(ctx, "leaf31S1").Return(leaf31S1.Marshal())
    89  	expect = append(expect, leaf31S1)
    90  
    91  	leaf31S2 := &backuppb.MetaFile{Schemas: []*backuppb.Schema{
    92  		{Db: []byte("db31S2"), Table: []byte("table31S2")},
    93  	}}
    94  	mockStorage.EXPECT().ReadFile(ctx, "leaf31S2").Return(leaf31S2.Marshal())
    95  	expect = append(expect, leaf31S2)
    96  
    97  	leaf32S1 := &backuppb.MetaFile{Schemas: []*backuppb.Schema{
    98  		{Db: []byte("db32S1"), Table: []byte("table32S1")},
    99  	}}
   100  	mockStorage.EXPECT().ReadFile(ctx, "leaf32S1").Return(leaf32S1.Marshal())
   101  	expect = append(expect, leaf32S1)
   102  
   103  	node21 := &backuppb.MetaFile{MetaFiles: []*backuppb.File{
   104  		{Name: "leaf31S1", Sha256: checksum(leaf31S1)},
   105  		{Name: "leaf31S2", Sha256: checksum(leaf31S2)},
   106  	}}
   107  	mockStorage.EXPECT().ReadFile(ctx, "node21").Return(node21.Marshal())
   108  
   109  	node22 := &backuppb.MetaFile{MetaFiles: []*backuppb.File{
   110  		{Name: "leaf32S1", Sha256: checksum(leaf32S1)},
   111  	}}
   112  	mockStorage.EXPECT().ReadFile(ctx, "node22").Return(node22.Marshal())
   113  
   114  	leaf23S1 := &backuppb.MetaFile{Schemas: []*backuppb.Schema{
   115  		{Db: []byte("db23S1"), Table: []byte("table23S1")},
   116  	}}
   117  	mockStorage.EXPECT().ReadFile(ctx, "leaf23S1").Return(leaf23S1.Marshal())
   118  	expect = append(expect, leaf23S1)
   119  
   120  	root := &backuppb.MetaFile{MetaFiles: []*backuppb.File{
   121  		{Name: "node21", Sha256: checksum(node21)},
   122  		{Name: "node22", Sha256: checksum(node22)},
   123  		{Name: "leaf23S1", Sha256: checksum(leaf23S1)},
   124  	}}
   125  
   126  	files := []*backuppb.MetaFile{}
   127  	collect := func(m *backuppb.MetaFile) { files = append(files, m) }
   128  	err := walkLeafMetaFile(ctx, mockStorage, root, collect)
   129  	c.Assert(err, IsNil)
   130  
   131  	c.Assert(files, HasLen, len(expect))
   132  	for i := range expect {
   133  		c.Assert(files[i], DeepEquals, expect[i])
   134  	}
   135  }