github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/flags/urls_test.go (about)

     1  // Copyright 2020 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 flags
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  )
    22  
    23  func Test(t *testing.T) {
    24  	check.TestingT(t)
    25  }
    26  
    27  var _ = check.Suite(&testUrlsSuite{})
    28  
    29  type testUrlsSuite struct{}
    30  
    31  func (t *testUrlsSuite) TestNewURLsValue(c *check.C) {
    32  	defer testleak.AfterTest(c)()
    33  	cases := []struct {
    34  		url        string
    35  		hostString string
    36  	}{
    37  		{"http://127.0.0.1:2379", "127.0.0.1:2379"},
    38  		{"http://127.0.0.1:2379,http://127.0.0.1:2380", "127.0.0.1:2379,127.0.0.1:2380"},
    39  		{"http://pd-1:2379,http://pd-2:2380", "pd-1:2379,pd-2:2380"},
    40  		{"https://127.0.0.1:2379,https://127.0.0.1:2380", "127.0.0.1:2379,127.0.0.1:2380"},
    41  		// TODO: unix socket not supported now
    42  		// {"unix:///home/tidb/tidb.sock", "/home/tidb/tidb.sock"},
    43  	}
    44  
    45  	for _, testCase := range cases {
    46  		urlsValue, err := NewURLsValue(testCase.url)
    47  		c.Assert(err, check.IsNil)
    48  		hs := urlsValue.HostString()
    49  		c.Assert(hs, check.Equals, testCase.hostString)
    50  	}
    51  }
    52  
    53  func (t *testUrlsSuite) TestNewURLsValueError(c *check.C) {
    54  	defer testleak.AfterTest(c)()
    55  	urls := []string{
    56  		"http:///192.168.199.111:2379",
    57  		"http://192.168.199.111",
    58  		"127.0.0.1:1080",
    59  		"http://192.168.199.112:8080/api/v1",
    60  	}
    61  	for _, url := range urls {
    62  		_, err := NewURLsValue(url)
    63  		c.Assert(err, check.NotNil)
    64  	}
    65  }