github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/config_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 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func getTestConfig() Config { 26 c := DefaultConfig() 27 c.UUID = "uuid" 28 c.DeploymentID = 100 29 c.DataDir = "/mydata/dir" 30 c.LogServicePort = 9000 31 c.RaftPort = 9001 32 c.GossipPort = 9002 33 c.GossipSeedAddresses = []string{"localhost:9002"} 34 c.Fill() 35 return c 36 } 37 38 func TestSplitAddress(t *testing.T) { 39 tests := []struct { 40 input string 41 output []string 42 }{ 43 {"", []string{}}, 44 {" ; ", []string{}}, 45 {" ;; ", []string{}}, 46 {";", []string{}}, 47 {"localhost:1000;localhost:1001", []string{"localhost:1000", "localhost:1001"}}, 48 {" localhost:1000 ; localhost:1001\t\n", []string{"localhost:1000", "localhost:1001"}}, 49 {"localhost:1000 \n", []string{"localhost:1000"}}, 50 {"localhost:1000;", []string{"localhost:1000"}}, 51 {";localhost:1000", []string{"localhost:1000"}}, 52 } 53 54 for _, tt := range tests { 55 v := splitAddresses(tt.input) 56 assert.Equal(t, tt.output, v) 57 } 58 } 59 60 func TestGetInitHAKeeperMembers(t *testing.T) { 61 cfg1 := Config{} 62 cfg1.BootstrapConfig.InitHAKeeperMembers = []string{" 131072 :9c4dccb4-4d3c-41f8-b482-5251dc7a41bf "} 63 result, err := cfg1.GetInitHAKeeperMembers() 64 require.NoError(t, err) 65 assert.Equal(t, 1, len(result)) 66 v, ok := result[131072] 67 assert.True(t, ok) 68 assert.Equal(t, "9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", v) 69 70 cfg2 := Config{} 71 cfg2.BootstrapConfig.InitHAKeeperMembers = []string{"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", "131073:9c4dccb4-4d3c-41f8-b482-5251dc7a41be"} 72 result, err = cfg2.GetInitHAKeeperMembers() 73 require.NoError(t, err) 74 assert.Equal(t, 2, len(result)) 75 v1, ok1 := result[131072] 76 v2, ok2 := result[131073] 77 assert.True(t, ok1) 78 assert.True(t, ok2) 79 assert.Equal(t, "9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", v1) 80 assert.Equal(t, "9c4dccb4-4d3c-41f8-b482-5251dc7a41be", v2) 81 82 tests := [][]string{ 83 {"131071:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"}, 84 {"262144:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"}, 85 {"262145:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"}, 86 {"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41b"}, 87 {"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", ""}, 88 {"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", "1:1"}, 89 } 90 91 for _, v := range tests { 92 cfg := Config{} 93 cfg.BootstrapConfig.InitHAKeeperMembers = v 94 _, err := cfg.GetInitHAKeeperMembers() 95 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 96 } 97 } 98 99 func TestConfigCanBeValidated(t *testing.T) { 100 c := getTestConfig() 101 assert.NoError(t, c.Validate()) 102 103 c1 := c 104 c1.DeploymentID = 0 105 err := c1.Validate() 106 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 107 108 c2 := c 109 c2.ServiceAddress = "" 110 c2.LogServicePort = 0 111 err = c2.Validate() 112 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 113 114 c3 := c 115 c3.RaftAddress = "" 116 c3.RaftPort = 0 117 err = c2.Validate() 118 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 119 120 c4 := c 121 c4.GossipAddress = "" 122 c4.GossipPort = 0 123 err = c4.Validate() 124 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 125 126 c5 := c 127 c5.GossipSeedAddresses = []string{} 128 err = c5.Validate() 129 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 130 131 c6 := c 132 c6.GossipProbeInterval.Duration = 0 133 err = c6.Validate() 134 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 135 } 136 137 func TestBootstrapConfigCanBeValidated(t *testing.T) { 138 c := getTestConfig() 139 assert.NoError(t, c.Validate()) 140 141 c.BootstrapConfig.BootstrapCluster = true 142 c.BootstrapConfig.NumOfLogShards = 3 143 c.BootstrapConfig.NumOfTNShards = 3 144 c.BootstrapConfig.NumOfLogShardReplicas = 1 145 c.BootstrapConfig.InitHAKeeperMembers = []string{"131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf"} 146 assert.NoError(t, c.Validate()) 147 148 c1 := c 149 c1.BootstrapConfig.NumOfLogShards = 0 150 err := c1.Validate() 151 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 152 153 c2 := c 154 c2.BootstrapConfig.NumOfTNShards = 0 155 err = c2.Validate() 156 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 157 158 c3 := c 159 c3.BootstrapConfig.NumOfTNShards = 2 160 err = c3.Validate() 161 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 162 163 c4 := c 164 c4.BootstrapConfig.NumOfLogShardReplicas = 2 165 err = c4.Validate() 166 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 167 } 168 169 func TestClientConfigValidate(t *testing.T) { 170 tests := []struct { 171 cfg ClientConfig 172 ok bool 173 }{ 174 { 175 ClientConfig{}, false, 176 }, 177 { 178 ClientConfig{LogShardID: 1, DiscoveryAddress: "localhost:9090"}, false, 179 }, 180 { 181 ClientConfig{LogShardID: 1, DiscoveryAddress: "localhost:9090"}, false, 182 }, 183 { 184 ClientConfig{LogShardID: 1, TNReplicaID: 100, DiscoveryAddress: "localhost:9090"}, true, 185 }, 186 { 187 ClientConfig{ 188 LogShardID: 1, 189 TNReplicaID: 100, 190 DiscoveryAddress: "localhost:9090", 191 ServiceAddresses: []string{"localhost:9091"}, 192 }, 193 true, 194 }, 195 } 196 197 for _, tt := range tests { 198 err := tt.cfg.Validate() 199 if tt.ok { 200 assert.NoError(t, err) 201 } else { 202 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 203 } 204 } 205 } 206 207 func TestHAKeeperClientConfigValidate(t *testing.T) { 208 tests := []struct { 209 cfg HAKeeperClientConfig 210 ok bool 211 }{ 212 { 213 HAKeeperClientConfig{}, true, 214 }, 215 { 216 HAKeeperClientConfig{DiscoveryAddress: "localhost:9090"}, true, 217 }, 218 { 219 HAKeeperClientConfig{ServiceAddresses: []string{"localhost:9090"}}, true, 220 }, 221 { 222 HAKeeperClientConfig{ 223 DiscoveryAddress: "localhost:9091", 224 ServiceAddresses: []string{"localhost:9090"}, 225 }, true, 226 }, 227 } 228 229 for _, tt := range tests { 230 err := tt.cfg.Validate() 231 if tt.ok { 232 assert.NoError(t, err) 233 } else { 234 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrBadConfig)) 235 } 236 } 237 }