docker_cli_proxy_test.go 1.7 KB

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