github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/servermaster/config_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 servermaster 15 16 import ( 17 "os" 18 "path/filepath" 19 "testing" 20 21 "github.com/pingcap/tiflow/pkg/cmd/util" 22 "github.com/pingcap/tiflow/pkg/security" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestMetaStoreConfig(t *testing.T) { 27 t.Parallel() 28 29 testToml := ` 30 name = "server-master-test" 31 [framework-meta] 32 endpoints = ["mysql-0:3306"] 33 user = "root" 34 password = "passwd" 35 schema = "test0" 36 37 [framework-meta.security] 38 ca-path = "ca.pem" 39 cert-path = "cert.pem" 40 key-path = "key.pem" 41 cert-allowed-cn = ["framework"] 42 43 [business-meta] 44 endpoints = ["metastore:12479"] 45 store-type = "ETCD" 46 ` 47 fileName := mustWriteToTempFile(t, testToml) 48 49 config := GetDefaultMasterConfig() 50 err := util.StrictDecodeFile(fileName, "tiflow master", config) 51 require.Nil(t, err) 52 err = config.AdjustAndValidate() 53 require.Nil(t, err) 54 55 require.Equal(t, "server-master-test", config.Name) 56 require.Equal(t, "mysql-0:3306", config.FrameworkMeta.Endpoints[0]) 57 require.Equal(t, "root", config.FrameworkMeta.User) 58 require.Equal(t, "passwd", config.FrameworkMeta.Password) 59 require.Equal(t, "test0", config.FrameworkMeta.Schema) 60 require.Equal(t, "mysql", config.FrameworkMeta.StoreType) 61 62 require.Equal(t, "etcd", config.BusinessMeta.StoreType) 63 require.Equal(t, "metastore:12479", config.BusinessMeta.Endpoints[0]) 64 require.Equal(t, defaultBusinessMetaSchema, config.BusinessMeta.Schema) 65 66 frameworkSecurityConfig := &security.Credential{ 67 CAPath: "ca.pem", 68 CertPath: "cert.pem", 69 KeyPath: "key.pem", 70 CertAllowedCN: []string{"framework"}, 71 } 72 require.Equal(t, frameworkSecurityConfig, config.FrameworkMeta.Security) 73 } 74 75 func mustWriteToTempFile(t *testing.T, content string) (filePath string) { 76 dir := t.TempDir() 77 fd, err := os.CreateTemp(dir, "*") 78 require.NoError(t, err) 79 _, err = fd.WriteString(content) 80 require.NoError(t, err) 81 82 return fd.Name() 83 } 84 85 func TestDefaultMetaStoreManager(t *testing.T) { 86 t.Parallel() 87 88 store := newFrameMetaConfig() 89 require.Equal(t, FrameMetaID, store.StoreID) 90 require.Equal(t, defaultFrameMetaEndpoints, store.Endpoints[0]) 91 92 store = NewDefaultBusinessMetaConfig() 93 require.Equal(t, DefaultBusinessMetaID, store.StoreID) 94 require.Equal(t, defaultBusinessMetaEndpoints, store.Endpoints[0]) 95 } 96 97 func TestLoadConfigFromFile(t *testing.T) { 98 t.Parallel() 99 cfg := GetDefaultMasterConfig() 100 data, err := cfg.Toml() 101 require.NoError(t, err) 102 103 dir := t.TempDir() 104 filename := filepath.Join(dir, "master-ut.toml") 105 err = os.WriteFile(filename, []byte(data), 0o600) 106 require.NoError(t, err) 107 108 cfg2 := &Config{} 109 err = util.StrictDecodeFile(filename, "tiflow master", cfg2) 110 require.NoError(t, err) 111 require.Equal(t, cfg, cfg2) 112 } 113 114 func TestExternalStorageConfig(t *testing.T) { 115 t.Parallel() 116 cfg := GetDefaultMasterConfig() 117 err := cfg.AdjustAndValidate() 118 require.NoError(t, err) 119 120 cfg.Storage.S3.Bucket = "s3-bucket" 121 err = cfg.AdjustAndValidate() 122 require.NoError(t, err) 123 124 cfg.Storage.GCS.Bucket = "s3-bucket" 125 err = cfg.AdjustAndValidate() 126 require.Error(t, err) 127 128 cfg.Storage.S3.Bucket = "" 129 err = cfg.AdjustAndValidate() 130 require.NoError(t, err) 131 }