github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/utils/env_test.go (about)

     1  // Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.
     2  
     3  package utils
     4  
     5  import (
     6  	"os"
     7  
     8  	"github.com/pingcap/check"
     9  )
    10  
    11  type envSuit struct{}
    12  
    13  var _ = check.Suite(&envSuit{})
    14  
    15  func (s *envSuit) TestProxyFields(c *check.C) {
    16  	revIndex := map[string]int{
    17  		"http_proxy":  0,
    18  		"https_proxy": 1,
    19  		"no_proxy":    2,
    20  	}
    21  	envs := [...]string{"http_proxy", "https_proxy", "no_proxy"}
    22  	envPreset := [...]string{"http://127.0.0.1:8080", "https://127.0.0.1:8443", "localhost,127.0.0.1"}
    23  
    24  	// Exhaust all combinations of those environment variables' selection.
    25  	// Each bit of the mask decided whether this index of `envs` would be set.
    26  	for mask := 0; mask <= 0b111; mask++ {
    27  		for _, env := range envs {
    28  			c.Assert(os.Unsetenv(env), check.IsNil)
    29  		}
    30  
    31  		for i := 0; i < 3; i++ {
    32  			if (1<<i)&mask != 0 {
    33  				c.Assert(os.Setenv(envs[i], envPreset[i]), check.IsNil)
    34  			}
    35  		}
    36  
    37  		for _, field := range proxyFields() {
    38  			idx, ok := revIndex[field.Key]
    39  			c.Assert(ok, check.IsTrue)
    40  			c.Assert((1<<idx)&mask, check.Not(check.Equals), 0)
    41  			c.Assert(field.String, check.Equals, envPreset[idx])
    42  		}
    43  	}
    44  }