github.com/matrixorigin/matrixone@v1.2.0/pkg/tnservice/store_metadata_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 tnservice
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/fagongzi/util/protoc"
    22  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    23  	"github.com/matrixorigin/matrixone/pkg/defines"
    24  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    25  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestInitMetadata(t *testing.T) {
    30  	fs, err := fileservice.NewMemoryFS(defines.LocalFileServiceName, fileservice.DisabledCacheConfig, nil)
    31  	assert.NoError(t, err)
    32  
    33  	s := &store{rt: runtime.DefaultRuntime(), metadataFileService: fs}
    34  	s.cfg = &Config{UUID: "1"}
    35  	s.mu.metadata.UUID = "1"
    36  	s.mu.metadata.Shards = append(s.mu.metadata.Shards, metadata.TNShard{ReplicaID: 1})
    37  	assert.NoError(t, s.initMetadata())
    38  
    39  	v := s.mu.metadata
    40  	s.mu.metadata.Shards = nil
    41  	assert.NoError(t, s.initMetadata())
    42  	assert.Equal(t, v, s.mu.metadata)
    43  }
    44  
    45  func TestInitMetadataWithExistData(t *testing.T) {
    46  	fs, err := fileservice.NewMemoryFS(defines.LocalFileServiceName, fileservice.DisabledCacheConfig, nil)
    47  	assert.NoError(t, err)
    48  	value := metadata.TNStore{
    49  		UUID: "dn1",
    50  		Shards: []metadata.TNShard{
    51  			{
    52  				TNShardRecord: metadata.TNShardRecord{ShardID: 1},
    53  			},
    54  			{
    55  				TNShardRecord: metadata.TNShardRecord{ShardID: 2},
    56  			},
    57  		},
    58  	}
    59  	assert.NoError(t, fs.Write(context.Background(), fileservice.IOVector{
    60  		FilePath: getMetadataFile(value.UUID),
    61  		Entries: []fileservice.IOEntry{
    62  			{
    63  				Offset: 0,
    64  				Size:   int64(value.Size()),
    65  				Data:   protoc.MustMarshal(&value),
    66  			},
    67  		},
    68  	}))
    69  
    70  	s := &store{rt: runtime.DefaultRuntime(), metadataFileService: fs}
    71  	s.cfg = &Config{UUID: "dn1"}
    72  	s.mu.metadata.UUID = "dn1"
    73  	assert.NoError(t, s.initMetadata())
    74  	assert.Equal(t, value, s.mu.metadata)
    75  }
    76  
    77  func TestInitMetadataWithInvalidUUIDWillPanic(t *testing.T) {
    78  	defer func() {
    79  		if err := recover(); err != nil {
    80  			return
    81  		}
    82  		assert.Fail(t, "must panic")
    83  	}()
    84  
    85  	fs, err := fileservice.NewMemoryFS(defines.LocalFileServiceName, fileservice.DisabledCacheConfig, nil)
    86  	assert.NoError(t, err)
    87  	value := metadata.TNStore{
    88  		UUID: "dn1",
    89  	}
    90  	assert.NoError(t, fs.Write(context.Background(), fileservice.IOVector{
    91  		FilePath: getMetadataFile(value.UUID),
    92  		Entries: []fileservice.IOEntry{
    93  			{
    94  				Offset: 0,
    95  				Size:   int64(value.Size()),
    96  				Data:   protoc.MustMarshal(&value),
    97  			},
    98  		},
    99  	}))
   100  
   101  	s := &store{rt: runtime.DefaultRuntime(), metadataFileService: fs}
   102  	assert.NoError(t, s.initMetadata())
   103  }