github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/meta/model/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 model
    15  
    16  import (
    17  	"regexp"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tiflow/engine/pkg/dbutil"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestGenerateDSNByParams(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	sf := DefaultStoreConfig()
    28  	sf.Endpoints = []string{"1.1.1.1"}
    29  	sf.User = "user"
    30  	sf.Password = "passwd"
    31  	dsn, err := GenerateDSNByParams(sf, map[string]string{
    32  		"sql_model": dbutil.GetSQLStrictMode(),
    33  	})
    34  	require.NoError(t, err)
    35  	require.Equal(t, "user:passwd@tcp(1.1.1.1)/?interpolateParams=true&"+
    36  		"loc=Local&parseTime=true&readTimeout=3s&sql_model=STRICT_TRANS_TABLES%2CSTRIC"+
    37  		"T_ALL_TABLES%2CERROR_FOR_DIVISION_BY_ZERO&timeout=3s&writeTimeout=3s", dsn)
    38  }
    39  
    40  func TestValidate(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	cases := []struct {
    44  		caseName string
    45  		conf     *StoreConfig
    46  		errMsg   string
    47  	}{
    48  		{
    49  			caseName: "Normal",
    50  			conf: &StoreConfig{
    51  				StoreType: defaultStoreType,
    52  				Schema:    "test",
    53  			},
    54  			errMsg: "",
    55  		},
    56  		{
    57  			caseName: "StoreTypeNotInEtcdOrSQL",
    58  			conf: &StoreConfig{
    59  				StoreType: "unknown",
    60  			},
    61  			errMsg: "store-type: must be a valid value",
    62  		},
    63  		{
    64  			caseName: "StoreTypeEtcdNotCheckSchema",
    65  			conf: &StoreConfig{
    66  				StoreType: StoreTypeEtcd,
    67  				Schema:    "",
    68  			},
    69  			errMsg: "",
    70  		},
    71  		{
    72  			caseName: "StoreTypeMySQLCheckSchema",
    73  			conf: &StoreConfig{
    74  				StoreType: StoreTypeMySQL,
    75  				Schema:    "",
    76  			},
    77  			errMsg: "schema: cannot be blank",
    78  		},
    79  	}
    80  
    81  	for _, ce := range cases {
    82  		err := ce.conf.Validate()
    83  		if ce.errMsg == "" {
    84  			require.NoError(t, err)
    85  		} else {
    86  			require.Regexp(t, regexp.QuoteMeta(ce.errMsg), err.Error())
    87  		}
    88  	}
    89  }