github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/service_bootstrap_test.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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 logservice
    16  
    17  import (
    18  	"context"
    19  	"path"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/defines"
    23  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    24  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestGetBackupData(t *testing.T) {
    29  	var nextID uint64 = 900
    30  	nextIDByKey := make(map[string]uint64)
    31  	nextIDByKey["a"] = 1
    32  	nextIDByKey["b"] = 2
    33  
    34  	backup := pb.BackupData{
    35  		NextID:      nextID,
    36  		NextIDByKey: nextIDByKey,
    37  	}
    38  	data, err := backup.Marshal()
    39  	assert.NoError(t, err)
    40  	assert.NotNil(t, data)
    41  
    42  	ctx := context.Background()
    43  	dir := t.TempDir()
    44  	name := defines.LocalFileServiceName
    45  	fs, err := fileservice.NewLocalFS(ctx, name, dir, fileservice.DisabledCacheConfig, nil)
    46  	assert.Nil(t, err)
    47  	assert.NotNil(t, fs)
    48  
    49  	s := &Service{
    50  		fileService: fs,
    51  	}
    52  	// If the file do not exist, do not return error.
    53  	restore, err := s.getBackupData(ctx)
    54  	assert.NoError(t, err)
    55  	assert.Nil(t, restore)
    56  
    57  	ioVec := fileservice.IOVector{
    58  		FilePath: path.Join(dir, name),
    59  		Entries:  make([]fileservice.IOEntry, 1),
    60  	}
    61  	ioVec.Entries[0] = fileservice.IOEntry{
    62  		Offset: 0,
    63  		Size:   int64(len(data)),
    64  		Data:   data,
    65  	}
    66  	err = fs.Write(ctx, ioVec)
    67  	assert.NoError(t, err)
    68  
    69  	restore, err = s.getBackupData(ctx)
    70  	assert.NoError(t, err)
    71  	assert.Nil(t, restore)
    72  
    73  	s.cfg.BootstrapConfig.Restore.FilePath = path.Join(dir, name)
    74  	restore, err = s.getBackupData(ctx)
    75  	assert.NoError(t, err)
    76  	assert.NotNil(t, restore)
    77  	assert.Equal(t, nextID, restore.NextID)
    78  	assert.Equal(t, nextIDByKey, restore.NextIDByKey)
    79  }