github.com/matrixorigin/matrixone@v1.2.0/cmd/mo-service/config_test.go (about) 1 // Copyright 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 main 16 17 import ( 18 "context" 19 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 20 "reflect" 21 "testing" 22 23 "github.com/matrixorigin/matrixone/pkg/defines" 24 "github.com/matrixorigin/matrixone/pkg/fileservice" 25 "github.com/matrixorigin/matrixone/pkg/logservice" 26 "github.com/matrixorigin/matrixone/pkg/tnservice" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestParseTNConfig(t *testing.T) { 31 data := ` 32 # service node type, [TN|CN|LOG] 33 service-type = "TN" 34 35 [log] 36 level = "debug" 37 format = "json" 38 max-size = 512 39 40 [hakeeper-client] 41 service-addresses = [ 42 "1", 43 "2" 44 ] 45 46 [[fileservice]] 47 # local fileservice instance, used to store TAE Data and TNStore metadata. 48 name = "local" 49 # use disk as fileservice backend 50 backend = "DISK" 51 # set the directory used by DISK backend. There must has a file named "thisisalocalfileservicedir" 52 # in the data dir 53 data-dir = "data dir" 54 55 [[fileservice]] 56 # s3 fileservice instance, used to store data. 57 name = "SHARED" 58 # use disk as fileservice backend. 59 backend = "DISK" 60 # set the directory used by DISK backend. There must has a file named "thisisalocalfileservicedir" 61 # in the data dir 62 data-dir = "data dir" 63 64 [tn.Txn.Storage] 65 # txn storage backend implementation. [TAE|MEM] 66 backend = "MEM" 67 ` 68 cfg := &Config{} 69 err := parseFromString(data, cfg) 70 assert.NoError(t, err) 71 assert.Equal(t, tnservice.StorageMEM, cfg.getTNServiceConfig().Txn.Storage.Backend) 72 assert.Equal(t, 2, len(cfg.FileServices)) 73 assert.Equal(t, "local", cfg.FileServices[0].Name) 74 assert.Equal(t, defines.SharedFileServiceName, cfg.FileServices[1].Name) 75 assert.Equal(t, 2, len(cfg.getTNServiceConfig().HAKeeper.ClientConfig.ServiceAddresses)) 76 } 77 78 func TestFileServiceFactory(t *testing.T) { 79 ctx := context.Background() 80 81 c := &Config{} 82 c.FileServices = append(c.FileServices, fileservice.Config{ 83 Name: "a", 84 Backend: "MEM", 85 }) 86 c.FileServices = append(c.FileServices, fileservice.Config{ 87 Name: defines.LocalFileServiceName, 88 Backend: "MEM", 89 }) 90 c.FileServices = append(c.FileServices, fileservice.Config{ 91 Name: defines.SharedFileServiceName, 92 Backend: "MEM", 93 }) 94 c.FileServices = append(c.FileServices, fileservice.Config{ 95 Name: defines.ETLFileServiceName, 96 Backend: "DISK-ETL", 97 }) 98 99 fs, err := c.createFileService(ctx, metadata.ServiceType_CN, "") 100 assert.NoError(t, err) 101 assert.NotNil(t, fs) 102 } 103 104 func TestResolveGossipSeedAddresses(t *testing.T) { 105 tests := []struct { 106 addrs []string 107 results []string 108 err error 109 }{ 110 { 111 []string{"localhost:32001", "localhost:32011"}, 112 []string{"127.0.0.1:32001", "127.0.0.1:32011"}, 113 nil, 114 }, 115 { 116 []string{"localhost:32001", "localhost:32011", "127.0.0.1:32021"}, 117 []string{"127.0.0.1:32001", "127.0.0.1:32011", "127.0.0.1:32021"}, 118 nil, 119 }, 120 { 121 []string{"127.0.0.1:32001"}, 122 []string{"127.0.0.1:32001"}, 123 nil, 124 }, 125 { 126 []string{"localhost:32001", "of-course-no-such-address42033.io:32001"}, 127 []string{"127.0.0.1:32001", "of-course-no-such-address42033.io:32001"}, 128 nil, 129 }, 130 } 131 132 for _, tt := range tests { 133 cfg := Config{ 134 LogService: logservice.Config{ 135 GossipSeedAddresses: tt.addrs, 136 }, 137 } 138 err := cfg.resolveGossipSeedAddresses() 139 if err != tt.err { 140 t.Errorf("expected %v, got %v", tt.err, err) 141 } 142 if got := cfg.LogService.GossipSeedAddresses; !reflect.DeepEqual(got, tt.results) { 143 t.Errorf("expected %v, got %v", tt.results, got) 144 } 145 } 146 } 147 148 func TestGossipSeedAddressesAreResolved(t *testing.T) { 149 data := ` 150 service-type = "LOG" 151 152 [log] 153 level = "debug" 154 format = "json" 155 max-size = 512 156 157 [logservice] 158 deployment-id = 1 159 uuid = "9c4dccb4-4d3c-41f8-b482-5251dc7a41bf" 160 gossip-seed-addresses = [ 161 "localhost:32002", 162 ] 163 164 [logservice.BootstrapConfig] 165 bootstrap-cluster = true 166 num-of-log-shards = 1 167 num-of-tn-shards = 1 168 num-of-log-shard-replicas = 1 169 init-hakeeper-members = [ 170 "131072:9c4dccb4-4d3c-41f8-b482-5251dc7a41bf", 171 ] 172 173 [hakeeper-client] 174 service-addresses = [ 175 "127.0.0.1:32000", 176 ] 177 ` 178 cfg := &Config{} 179 err := parseFromString(data, cfg) 180 assert.NoError(t, err) 181 assert.NoError(t, cfg.validate()) 182 assert.NoError(t, cfg.resolveGossipSeedAddresses()) 183 assert.Equal(t, 1, len(cfg.LogService.GossipSeedAddresses)) 184 assert.Equal(t, "127.0.0.1:32002", cfg.LogService.GossipSeedAddresses[0]) 185 } 186 187 func TestDumpCommonConfig(t *testing.T) { 188 cfg1 := *NewConfig() 189 _, err := dumpCommonConfig(cfg1) 190 assert.NoError(t, err) 191 }