docker_cli_proxy_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "net"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. func TestCliProxyDisableProxyUnixSock(t *testing.T) {
  9. cmd := exec.Command(dockerBinary, "info")
  10. cmd.Env = []string{"HTTP_PROXY=http://127.0.0.1:9999"}
  11. if out, _, err := runCommandWithOutput(cmd); err != nil {
  12. t.Fatal(err, out)
  13. }
  14. logDone("cli proxy - HTTP_PROXY is not used when connecting to unix sock")
  15. }
  16. // Can't use localhost here since go has a special case to not use proxy if connecting to localhost
  17. // See http://golang.org/pkg/net/http/#ProxyFromEnvironment
  18. func TestCliProxyProxyTCPSock(t *testing.T) {
  19. // get the IP to use to connect since we can't use localhost
  20. addrs, err := net.InterfaceAddrs()
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. var ip string
  25. for _, addr := range addrs {
  26. sAddr := addr.String()
  27. if !strings.Contains(sAddr, "127.0.0.1") {
  28. addrArr := strings.Split(sAddr, "/")
  29. ip = addrArr[0]
  30. break
  31. }
  32. }
  33. if ip == "" {
  34. t.Fatal("could not find ip to connect to")
  35. }
  36. d := NewDaemon(t)
  37. if err := d.Start("-H", "tcp://"+ip+":2375"); err != nil {
  38. t.Fatal(err)
  39. }
  40. cmd := exec.Command(dockerBinary, "info")
  41. cmd.Env = []string{"DOCKER_HOST=tcp://" + ip + ":2375", "HTTP_PROXY=127.0.0.1:9999"}
  42. if out, _, err := runCommandWithOutput(cmd); err == nil {
  43. t.Fatal(err, out)
  44. }
  45. // Test with no_proxy
  46. cmd.Env = append(cmd.Env, "NO_PROXY="+ip)
  47. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "info")); err != nil {
  48. t.Fatal(err, out)
  49. }
  50. logDone("cli proxy - HTTP_PROXY is used for TCP sock")
  51. }