github.com/matrixorigin/matrixone@v1.2.0/pkg/proxy/config_test.go (about) 1 // Copyright 2021 - 2023 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 proxy 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestFillDefault(t *testing.T) { 25 c := Config{} 26 c.FillDefault() 27 require.NotEqual(t, "", c.ListenAddress) 28 require.NotEqual(t, 0, c.RebalanceInterval.Duration) 29 require.NotEqual(t, true, c.RebalanceDisabled) 30 require.NotEqual(t, 0, c.RebalanceTolerance) 31 require.Less(t, c.RebalanceTolerance, float64(1)) 32 require.NotEqual(t, 0, c.Cluster.RefreshInterval.Duration) 33 } 34 35 func TestValidate(t *testing.T) { 36 tests := []struct { 37 name string 38 cfg Config 39 wantErr bool 40 }{{ 41 name: "empty", 42 cfg: Config{}, 43 }, { 44 name: "plugin enabled but no backend", 45 cfg: Config{ 46 Plugin: &PluginConfig{}, 47 }, 48 wantErr: true, 49 }, { 50 name: "plugin enabled but no timeout", 51 cfg: Config{ 52 Plugin: &PluginConfig{ 53 Backend: "test", 54 }, 55 }, 56 wantErr: true, 57 }, { 58 name: "plugin valid", 59 cfg: Config{ 60 Plugin: &PluginConfig{ 61 Backend: "test", 62 Timeout: time.Second, 63 }, 64 }, 65 }} 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 err := tt.cfg.Validate() 69 if tt.wantErr { 70 require.Error(t, err) 71 } else { 72 require.NoError(t, err) 73 } 74 }) 75 } 76 }