github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/config/server_config_test.go (about)

     1  // Copyright 2021 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 config
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestServerConfigMarshal(t *testing.T) {
    24  	t.Parallel()
    25  	rawConfig := testCfgTestServerConfigMarshal
    26  
    27  	conf := GetDefaultServerConfig()
    28  	conf.Addr = "192.155.22.33:8887"
    29  	b, err := conf.Marshal()
    30  	require.Nil(t, err)
    31  
    32  	require.Equal(t, rawConfig, mustIndentJSON(t, b))
    33  	conf2 := new(ServerConfig)
    34  	err = conf2.Unmarshal([]byte(rawConfig))
    35  	require.Nil(t, err)
    36  	require.Equal(t, conf, conf2)
    37  }
    38  
    39  func TestServerConfigClone(t *testing.T) {
    40  	t.Parallel()
    41  	conf := GetDefaultServerConfig()
    42  	conf.Addr = "192.155.22.33:8887"
    43  	conf.Sorter.SortDir = "/tmp"
    44  	conf2 := conf.Clone()
    45  	require.Equal(t, conf, conf2)
    46  	conf2.Sorter.SortDir = "/tmp/sorter"
    47  	require.Equal(t, "/tmp", conf.Sorter.SortDir)
    48  }
    49  
    50  func TestServerConfigValidateAndAdjust(t *testing.T) {
    51  	t.Parallel()
    52  	conf := new(ServerConfig)
    53  
    54  	require.Regexp(t, ".*bad cluster-id.*", conf.ValidateAndAdjust())
    55  	conf.ClusterID = "__backup__"
    56  	require.Regexp(t, ".*bad cluster-id.*", conf.ValidateAndAdjust())
    57  	conf.ClusterID = "default"
    58  	require.Regexp(t, ".*empty address", conf.ValidateAndAdjust())
    59  	conf.Addr = "cdc:1234"
    60  	require.Regexp(t, ".*empty GC TTL is not allowed", conf.ValidateAndAdjust())
    61  	conf.GcTTL = 60
    62  	require.Nil(t, conf.ValidateAndAdjust())
    63  	require.Equal(t, conf.Addr, conf.AdvertiseAddr)
    64  	conf.AdvertiseAddr = "advertise:1234"
    65  	require.Nil(t, conf.ValidateAndAdjust())
    66  	require.Equal(t, "cdc:1234", conf.Addr)
    67  	require.Equal(t, "advertise:1234", conf.AdvertiseAddr)
    68  	conf.AdvertiseAddr = "0.0.0.0:1234"
    69  	require.Regexp(t, ".*must be specified.*", conf.ValidateAndAdjust())
    70  	conf.Addr = "0.0.0.0:1234"
    71  	require.Regexp(t, ".*must be specified.*", conf.ValidateAndAdjust())
    72  	conf.AdvertiseAddr = "advertise"
    73  	require.Regexp(t, ".*does not contain a port", conf.ValidateAndAdjust())
    74  	conf.AdvertiseAddr = "advertise:1234"
    75  	conf.Debug.Messages.ServerWorkerPoolSize = 0
    76  	require.Nil(t, conf.ValidateAndAdjust())
    77  	require.EqualValues(t, GetDefaultServerConfig().Debug.Messages.ServerWorkerPoolSize, conf.Debug.Messages.ServerWorkerPoolSize)
    78  }
    79  
    80  func TestDBConfigValidateAndAdjust(t *testing.T) {
    81  	t.Parallel()
    82  	conf := GetDefaultServerConfig().Clone().Debug.DB
    83  
    84  	require.Nil(t, conf.ValidateAndAdjust())
    85  	conf.Compression = "none"
    86  	require.Nil(t, conf.ValidateAndAdjust())
    87  	conf.Compression = "snappy"
    88  	require.Nil(t, conf.ValidateAndAdjust())
    89  	conf.Compression = "invalid"
    90  	require.Error(t, conf.ValidateAndAdjust())
    91  }
    92  
    93  func TestKVClientConfigValidateAndAdjust(t *testing.T) {
    94  	t.Parallel()
    95  	conf := GetDefaultServerConfig().Clone().KVClient
    96  
    97  	require.Nil(t, conf.ValidateAndAdjust())
    98  	conf.RegionRetryDuration = TomlDuration(time.Second)
    99  	require.Nil(t, conf.ValidateAndAdjust())
   100  	conf.RegionRetryDuration = -TomlDuration(time.Second)
   101  	require.Error(t, conf.ValidateAndAdjust())
   102  }
   103  
   104  func TestSchedulerConfigValidateAndAdjust(t *testing.T) {
   105  	t.Parallel()
   106  	conf := GetDefaultServerConfig().Clone().Debug.Scheduler
   107  	require.Nil(t, conf.ValidateAndAdjust())
   108  
   109  	conf = GetDefaultServerConfig().Clone().Debug.Scheduler
   110  	conf.HeartbeatTick = -1
   111  	require.Error(t, conf.ValidateAndAdjust())
   112  	conf.HeartbeatTick = 0
   113  	require.Error(t, conf.ValidateAndAdjust())
   114  
   115  	conf = GetDefaultServerConfig().Clone().Debug.Scheduler
   116  	conf.CollectStatsTick = -1
   117  	require.Error(t, conf.ValidateAndAdjust())
   118  	conf.CollectStatsTick = 0
   119  	require.Error(t, conf.ValidateAndAdjust())
   120  
   121  	conf = GetDefaultServerConfig().Clone().Debug.Scheduler
   122  	conf.MaxTaskConcurrency = -1
   123  	require.Error(t, conf.ValidateAndAdjust())
   124  	conf.MaxTaskConcurrency = 0
   125  	require.Error(t, conf.ValidateAndAdjust())
   126  
   127  	conf = GetDefaultServerConfig().Clone().Debug.Scheduler
   128  	conf.CheckBalanceInterval = -1
   129  	require.Error(t, conf.ValidateAndAdjust())
   130  	conf.CheckBalanceInterval = TomlDuration(time.Second)
   131  	require.Error(t, conf.ValidateAndAdjust())
   132  
   133  	conf = GetDefaultServerConfig().Clone().Debug.Scheduler
   134  	conf.AddTableBatchSize = 0
   135  	require.Error(t, conf.ValidateAndAdjust())
   136  }
   137  
   138  func TestIsValidClusterID(t *testing.T) {
   139  	cases := []struct {
   140  		id    string
   141  		valid bool
   142  	}{
   143  		{"owner", false},
   144  		{"capture", false},
   145  		{"task", false},
   146  		{"changefeed", false},
   147  		{"job", false},
   148  		{"meta", false},
   149  		{"__backup__", false},
   150  		{"", false},
   151  		{"12345678901234567890123456789012345678901234567890123456789012345678901234567890" +
   152  			"1234567890123456789012345678901234567890123456789", false},
   153  		{"12345678901234567890123456789012345678901234567890123456789012345678901234567890" +
   154  			"123456789012345678901234567890123456789012345678", true},
   155  		{"default", true},
   156  	}
   157  	for _, c := range cases {
   158  		t.Log(c.id)
   159  		require.Equal(t, c.valid, isValidClusterID(c.id))
   160  	}
   161  }