github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/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/stretchr/testify/require"
    20  )
    21  
    22  func TestNewURLsValue(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	cases := []struct {
    26  		url        string
    27  		hostString string
    28  	}{
    29  		{"http://127.0.0.1:2379", "127.0.0.1:2379"},
    30  		{"http://127.0.0.1:2379,http://127.0.0.1:2380", "127.0.0.1:2379,127.0.0.1:2380"},
    31  		{"http://pd-1:2379,http://pd-2:2380", "pd-1:2379,pd-2:2380"},
    32  		{"https://127.0.0.1:2379,https://127.0.0.1:2380", "127.0.0.1:2379,127.0.0.1:2380"},
    33  		// TODO: unix socket not supported now
    34  		// {"unix:///home/tidb/tidb.sock", "/home/tidb/tidb.sock"},
    35  	}
    36  
    37  	for _, testCase := range cases {
    38  		urlsValue, err := NewURLsValue(testCase.url)
    39  		require.Nil(t, err)
    40  		hs := urlsValue.HostString()
    41  		require.Equal(t, testCase.hostString, hs)
    42  	}
    43  }
    44  
    45  func TestNewURLsValueError(t *testing.T) {
    46  	t.Parallel()
    47  
    48  	urls := []string{
    49  		"http:///192.168.199.111:2379",
    50  		"http://192.168.199.111",
    51  		"127.0.0.1:1080",
    52  		"http://192.168.199.112:8080/api/v1",
    53  	}
    54  	for _, url := range urls {
    55  		_, err := NewURLsValue(url)
    56  		require.NotNil(t, err)
    57  	}
    58  }