github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/dm/metadata/cluster_info_test.go (about) 1 // Copyright 2022 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 metadata 15 16 import ( 17 "context" 18 "testing" 19 20 "github.com/coreos/go-semver/semver" 21 "github.com/pingcap/tiflow/engine/pkg/adapter" 22 "github.com/pingcap/tiflow/engine/pkg/meta/mock" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestInfoStore(t *testing.T) { 27 ver1 := *semver.New("1.0.0") 28 clusterInfo := NewClusterInfo(ver1) 29 require.True(t, clusterInfo.Version.Equal(ver1)) 30 31 clusterInfoStore := NewClusterInfoStore(mock.NewMetaMock()) 32 key := clusterInfoStore.key() 33 keys, err := adapter.DMInfoKeyAdapter.Decode(key) 34 require.NoError(t, err) 35 require.Len(t, keys, 1) 36 require.Equal(t, keys[0], "") 37 38 state := clusterInfoStore.createState() 39 require.IsType(t, &ClusterInfo{}, state) 40 41 state, err = clusterInfoStore.Get(context.Background()) 42 require.EqualError(t, err, "state not found") 43 require.Nil(t, state) 44 require.EqualError(t, clusterInfoStore.UpdateVersion(context.Background(), ver1), "state not found") 45 require.NoError(t, clusterInfoStore.Put(context.Background(), clusterInfo)) 46 state, err = clusterInfoStore.Get(context.Background()) 47 require.NoError(t, err) 48 clusterInfo2 := state.(*ClusterInfo) 49 require.True(t, clusterInfo2.Version.Equal(clusterInfo.Version)) 50 51 ver2 := *semver.New("2.0.0") 52 clusterInfoStore.UpdateVersion(context.Background(), ver2) 53 state, err = clusterInfoStore.Get(context.Background()) 54 require.NoError(t, err) 55 clusterInfo3 := state.(*ClusterInfo) 56 require.True(t, clusterInfo3.Version.Equal(ver2)) 57 }