github.com/matrixorigin/matrixone@v1.2.0/pkg/cnservice/server_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 cnservice
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/fagongzi/util/protoc"
    21  	"github.com/matrixorigin/matrixone/pkg/defines"
    22  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    23  	"github.com/matrixorigin/matrixone/pkg/logutil"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    25  	"github.com/matrixorigin/matrixone/pkg/util/file"
    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 := &service{logger: logutil.GetPanicLogger(), metadataFS: fs}
    34  	s.cfg = &Config{UUID: "1"}
    35  	s.metadata.UUID = s.cfg.UUID
    36  	assert.NoError(t, s.initMetadata())
    37  
    38  	v, err := file.ReadFile(s.metadataFS, getMetadataFile("1"))
    39  	assert.NoError(t, err)
    40  	assert.NotEmpty(t, v)
    41  }
    42  
    43  func TestInitMetadataWithExistData(t *testing.T) {
    44  	fs, err := fileservice.NewMemoryFS(defines.LocalFileServiceName, fileservice.DisabledCacheConfig, nil)
    45  	assert.NoError(t, err)
    46  	value := metadata.CNStore{
    47  		UUID: "cn1",
    48  	}
    49  	assert.NoError(t, file.WriteFile(fs, getMetadataFile(value.UUID), protoc.MustMarshal(&value)))
    50  
    51  	s := &service{logger: logutil.GetPanicLogger(), metadataFS: fs}
    52  	s.cfg = &Config{UUID: "cn1"}
    53  	s.metadata.UUID = s.cfg.UUID
    54  	assert.NoError(t, s.initMetadata())
    55  	assert.Equal(t, value, s.metadata)
    56  }
    57  
    58  func TestInitMetadataWithInvalidUUIDWillPanic(t *testing.T) {
    59  	defer func() {
    60  		if err := recover(); err != nil {
    61  			return
    62  		}
    63  		assert.Fail(t, "must panic")
    64  	}()
    65  
    66  	fs, err := fileservice.NewMemoryFS(defines.LocalFileServiceName, fileservice.DisabledCacheConfig, nil)
    67  	assert.NoError(t, err)
    68  	value := metadata.CNStore{
    69  		UUID: "cn1",
    70  	}
    71  	assert.NoError(t, file.WriteFile(fs, getMetadataFile(value.UUID), protoc.MustMarshal(&value)))
    72  
    73  	s := &service{logger: logutil.GetPanicLogger(), metadataFS: fs}
    74  	assert.NoError(t, s.initMetadata())
    75  }