github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/integration-cli/docker_cli_proxy_test.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"strings"
     7  	"testing"
     8  
     9  	"gotest.tools/v3/assert"
    10  	"gotest.tools/v3/icmd"
    11  )
    12  
    13  type DockerCLIProxySuite struct {
    14  	ds *DockerSuite
    15  }
    16  
    17  func (s *DockerCLIProxySuite) TearDownTest(ctx context.Context, c *testing.T) {
    18  	s.ds.TearDownTest(ctx, c)
    19  }
    20  
    21  func (s *DockerCLIProxySuite) OnTimeout(c *testing.T) {
    22  	s.ds.OnTimeout(c)
    23  }
    24  
    25  func (s *DockerCLIProxySuite) TestCLIProxyDisableProxyUnixSock(c *testing.T) {
    26  	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
    27  
    28  	icmd.RunCmd(icmd.Cmd{
    29  		Command: []string{dockerBinary, "info"},
    30  		Env:     appendBaseEnv(false, "HTTP_PROXY=http://127.0.0.1:9999"),
    31  	}).Assert(c, icmd.Success)
    32  }
    33  
    34  // Can't use localhost here since go has a special case to not use proxy if connecting to localhost
    35  // See https://golang.org/pkg/net/http/#ProxyFromEnvironment
    36  func (s *DockerDaemonSuite) TestCLIProxyProxyTCPSock(c *testing.T) {
    37  	// get the IP to use to connect since we can't use localhost
    38  	addrs, err := net.InterfaceAddrs()
    39  	assert.NilError(c, err)
    40  	var ip string
    41  	for _, addr := range addrs {
    42  		sAddr := addr.String()
    43  		if !strings.Contains(sAddr, "127.0.0.1") {
    44  			addrArr := strings.Split(sAddr, "/")
    45  			ip = addrArr[0]
    46  			break
    47  		}
    48  	}
    49  
    50  	assert.Assert(c, ip != "")
    51  
    52  	s.d.Start(c, "-H", "tcp://"+ip+":2375")
    53  
    54  	icmd.RunCmd(icmd.Cmd{
    55  		Command: []string{dockerBinary, "info"},
    56  		Env:     []string{"DOCKER_HOST=tcp://" + ip + ":2375", "HTTP_PROXY=127.0.0.1:9999"},
    57  	}).Assert(c, icmd.Expected{Error: "exit status 1", ExitCode: 1})
    58  	// Test with no_proxy
    59  	icmd.RunCmd(icmd.Cmd{
    60  		Command: []string{dockerBinary, "info"},
    61  		Env:     []string{"DOCKER_HOST=tcp://" + ip + ":2375", "HTTP_PROXY=127.0.0.1:9999", "NO_PROXY=" + ip},
    62  	}).Assert(c, icmd.Success)
    63  }