github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cmd/util_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 cmd
    15  
    16  import (
    17  	"os"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  )
    22  
    23  type utilsSuite struct{}
    24  
    25  var _ = check.Suite(&utilsSuite{})
    26  
    27  func (s *utilsSuite) TestProxyFields(c *check.C) {
    28  	defer testleak.AfterTest(c)()
    29  	revIndex := map[string]int{
    30  		"http_proxy":  0,
    31  		"https_proxy": 1,
    32  		"no_proxy":    2,
    33  	}
    34  	envs := [...]string{"http_proxy", "https_proxy", "no_proxy"}
    35  	envPreset := [...]string{"http://127.0.0.1:8080", "https://127.0.0.1:8443", "localhost,127.0.0.1"}
    36  
    37  	// Exhaust all combinations of those environment variables' selection.
    38  	// Each bit of the mask decided whether this index of `envs` would be set.
    39  	for mask := 0; mask <= 0b111; mask++ {
    40  		for _, env := range envs {
    41  			c.Assert(os.Unsetenv(env), check.IsNil)
    42  		}
    43  
    44  		for i := 0; i < 3; i++ {
    45  			if (1<<i)&mask != 0 {
    46  				c.Assert(os.Setenv(envs[i], envPreset[i]), check.IsNil)
    47  			}
    48  		}
    49  
    50  		for _, field := range proxyFields() {
    51  			idx, ok := revIndex[field.Key]
    52  			c.Assert(ok, check.IsTrue)
    53  			c.Assert((1<<idx)&mask, check.Not(check.Equals), 0)
    54  			c.Assert(field.String, check.Equals, envPreset[idx])
    55  		}
    56  	}
    57  }
    58  
    59  func (s *utilsSuite) TestVerifyPdEndpoint(c *check.C) {
    60  	defer testleak.AfterTest(c)()
    61  	// empty URL.
    62  	url := ""
    63  	c.Assert(verifyPdEndpoint(url, false), check.ErrorMatches, ".*PD endpoint should be a valid http or https URL.*")
    64  
    65  	// invalid URL.
    66  	url = "\r hi"
    67  	c.Assert(verifyPdEndpoint(url, false), check.ErrorMatches, ".*invalid control character in URL.*")
    68  
    69  	// http URL without host.
    70  	url = "http://"
    71  	c.Assert(verifyPdEndpoint(url, false), check.ErrorMatches, ".*PD endpoint should be a valid http or https URL.*")
    72  
    73  	// https URL without host.
    74  	url = "https://"
    75  	c.Assert(verifyPdEndpoint(url, false), check.ErrorMatches, ".*PD endpoint should be a valid http or https URL.*")
    76  
    77  	// postgres scheme.
    78  	url = "postgres://postgres@localhost/cargo_registry"
    79  	c.Assert(verifyPdEndpoint(url, false), check.ErrorMatches, ".*PD endpoint should be a valid http or https URL.*")
    80  
    81  	// https scheme without TLS.
    82  	url = "https://aa"
    83  	c.Assert(verifyPdEndpoint(url, false), check.ErrorMatches, ".*PD endpoint scheme is https, please provide certificate.*")
    84  
    85  	// http scheme with TLS.
    86  	url = "http://aa"
    87  	c.Assert(verifyPdEndpoint(url, true), check.ErrorMatches, ".*PD endpoint scheme should be https.*")
    88  
    89  	// valid http URL.
    90  	c.Assert(verifyPdEndpoint("http://aa", false), check.IsNil)
    91  
    92  	// valid https URL with TLS.
    93  	c.Assert(verifyPdEndpoint("https://aa", true), check.IsNil)
    94  }