docker_cli_proxy_test.go 1.6 KB

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