driver_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package bridge
  2. import (
  3. "net"
  4. "strconv"
  5. "testing"
  6. "github.com/docker/docker/daemon/networkdriver/portmapper"
  7. "github.com/docker/docker/engine"
  8. "github.com/docker/docker/pkg/iptables"
  9. )
  10. func init() {
  11. // reset the new proxy command for mocking out the userland proxy in tests
  12. portmapper.NewProxy = portmapper.NewMockProxyCommand
  13. }
  14. func findFreePort(t *testing.T) int {
  15. l, err := net.Listen("tcp", ":0")
  16. if err != nil {
  17. t.Fatal("Failed to find a free port")
  18. }
  19. defer l.Close()
  20. result, err := net.ResolveTCPAddr("tcp", l.Addr().String())
  21. if err != nil {
  22. t.Fatal("Failed to resolve address to identify free port")
  23. }
  24. return result.Port
  25. }
  26. func newPortAllocationJob(eng *engine.Engine, port int) (job *engine.Job) {
  27. strPort := strconv.Itoa(port)
  28. job = eng.Job("allocate_port", "container_id")
  29. job.Setenv("HostIP", "127.0.0.1")
  30. job.Setenv("HostPort", strPort)
  31. job.Setenv("Proto", "tcp")
  32. job.Setenv("ContainerPort", strPort)
  33. return
  34. }
  35. func newPortAllocationJobWithInvalidHostIP(eng *engine.Engine, port int) (job *engine.Job) {
  36. strPort := strconv.Itoa(port)
  37. job = eng.Job("allocate_port", "container_id")
  38. job.Setenv("HostIP", "localhost")
  39. job.Setenv("HostPort", strPort)
  40. job.Setenv("Proto", "tcp")
  41. job.Setenv("ContainerPort", strPort)
  42. return
  43. }
  44. func TestAllocatePortDetection(t *testing.T) {
  45. eng := engine.New()
  46. eng.Logging = false
  47. freePort := findFreePort(t)
  48. // Init driver
  49. job := eng.Job("initdriver")
  50. if res := InitDriver(job); res != engine.StatusOK {
  51. t.Fatal("Failed to initialize network driver")
  52. }
  53. // Allocate interface
  54. job = eng.Job("allocate_interface", "container_id")
  55. if res := Allocate(job); res != engine.StatusOK {
  56. t.Fatal("Failed to allocate network interface")
  57. }
  58. // Allocate same port twice, expect failure on second call
  59. job = newPortAllocationJob(eng, freePort)
  60. if res := AllocatePort(job); res != engine.StatusOK {
  61. t.Fatal("Failed to find a free port to allocate")
  62. }
  63. if res := AllocatePort(job); res == engine.StatusOK {
  64. t.Fatal("Duplicate port allocation granted by AllocatePort")
  65. }
  66. }
  67. func TestHostnameFormatChecking(t *testing.T) {
  68. eng := engine.New()
  69. eng.Logging = false
  70. freePort := findFreePort(t)
  71. // Init driver
  72. job := eng.Job("initdriver")
  73. if res := InitDriver(job); res != engine.StatusOK {
  74. t.Fatal("Failed to initialize network driver")
  75. }
  76. // Allocate interface
  77. job = eng.Job("allocate_interface", "container_id")
  78. if res := Allocate(job); res != engine.StatusOK {
  79. t.Fatal("Failed to allocate network interface")
  80. }
  81. // Allocate port with invalid HostIP, expect failure with Bad Request http status
  82. job = newPortAllocationJobWithInvalidHostIP(eng, freePort)
  83. if res := AllocatePort(job); res == engine.StatusOK {
  84. t.Fatal("Failed to check invalid HostIP")
  85. }
  86. }
  87. func TestMacAddrGeneration(t *testing.T) {
  88. ip := net.ParseIP("192.168.0.1")
  89. mac := generateMacAddr(ip).String()
  90. // Should be consistent.
  91. if generateMacAddr(ip).String() != mac {
  92. t.Fatal("Inconsistent MAC address")
  93. }
  94. // Should be unique.
  95. ip2 := net.ParseIP("192.168.0.2")
  96. if generateMacAddr(ip2).String() == mac {
  97. t.Fatal("Non-unique MAC address")
  98. }
  99. }
  100. func TestLinkContainers(t *testing.T) {
  101. eng := engine.New()
  102. eng.Logging = false
  103. // Init driver
  104. job := eng.Job("initdriver")
  105. if res := InitDriver(job); res != engine.StatusOK {
  106. t.Fatal("Failed to initialize network driver")
  107. }
  108. // Allocate interface
  109. job = eng.Job("allocate_interface", "container_id")
  110. if res := Allocate(job); res != engine.StatusOK {
  111. t.Fatal("Failed to allocate network interface")
  112. }
  113. job.Args[0] = "-I"
  114. job.Setenv("ChildIP", "172.17.0.2")
  115. job.Setenv("ParentIP", "172.17.0.1")
  116. job.SetenvBool("IgnoreErrors", false)
  117. job.SetenvList("Ports", []string{"1234"})
  118. bridgeIface = "lo"
  119. _, err := iptables.NewChain("DOCKER", bridgeIface, iptables.Filter)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. if res := LinkContainers(job); res != engine.StatusOK {
  124. t.Fatalf("LinkContainers failed")
  125. }
  126. // flush rules
  127. if _, err = iptables.Raw([]string{"-F", "DOCKER"}...); err != nil {
  128. t.Fatal(err)
  129. }
  130. }