driver_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package bridge
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "testing"
  7. "github.com/docker/docker/daemon/network"
  8. "github.com/docker/docker/daemon/networkdriver/portmapper"
  9. "github.com/docker/docker/nat"
  10. "github.com/docker/docker/pkg/iptables"
  11. )
  12. func init() {
  13. // reset the new proxy command for mocking out the userland proxy in tests
  14. portmapper.NewProxy = portmapper.NewMockProxyCommand
  15. }
  16. func findFreePort(t *testing.T) string {
  17. l, err := net.Listen("tcp", ":0")
  18. if err != nil {
  19. t.Fatal("Failed to find a free port")
  20. }
  21. defer l.Close()
  22. result, err := net.ResolveTCPAddr("tcp", l.Addr().String())
  23. if err != nil {
  24. t.Fatal("Failed to resolve address to identify free port")
  25. }
  26. return strconv.Itoa(result.Port)
  27. }
  28. func TestAllocatePortDetection(t *testing.T) {
  29. freePort := findFreePort(t)
  30. if err := InitDriver(new(Config)); err != nil {
  31. t.Fatal("Failed to initialize network driver")
  32. }
  33. // Allocate interface
  34. if _, err := Allocate("container_id", "", "", ""); err != nil {
  35. t.Fatal("Failed to allocate network interface")
  36. }
  37. port := nat.Port(freePort + "/tcp")
  38. binding := nat.PortBinding{HostIp: "127.0.0.1", HostPort: freePort}
  39. // Allocate same port twice, expect failure on second call
  40. if _, err := AllocatePort("container_id", port, binding); err != nil {
  41. t.Fatal("Failed to find a free port to allocate")
  42. }
  43. if _, err := AllocatePort("container_id", port, binding); err == nil {
  44. t.Fatal("Duplicate port allocation granted by AllocatePort")
  45. }
  46. }
  47. func TestHostnameFormatChecking(t *testing.T) {
  48. freePort := findFreePort(t)
  49. if err := InitDriver(new(Config)); err != nil {
  50. t.Fatal("Failed to initialize network driver")
  51. }
  52. // Allocate interface
  53. if _, err := Allocate("container_id", "", "", ""); err != nil {
  54. t.Fatal("Failed to allocate network interface")
  55. }
  56. port := nat.Port(freePort + "/tcp")
  57. binding := nat.PortBinding{HostIp: "localhost", HostPort: freePort}
  58. if _, err := AllocatePort("container_id", port, binding); err == nil {
  59. t.Fatal("Failed to check invalid HostIP")
  60. }
  61. }
  62. func newInterfaceAllocation(t *testing.T, globalIPv6 *net.IPNet, requestedMac, requestedIP, requestedIPv6 string, expectFail bool) *network.Settings {
  63. // set IPv6 global if given
  64. if globalIPv6 != nil {
  65. globalIPv6Network = globalIPv6
  66. }
  67. networkSettings, err := Allocate("container_id", requestedMac, requestedIP, requestedIPv6)
  68. if err == nil && expectFail {
  69. t.Fatal("Doesn't fail to allocate network interface")
  70. } else if err != nil && !expectFail {
  71. t.Fatal("Failed to allocate network interface")
  72. }
  73. if globalIPv6 != nil {
  74. // check for bug #11427
  75. if globalIPv6Network.IP.String() != globalIPv6.IP.String() {
  76. t.Fatal("globalIPv6Network was modified during allocation")
  77. }
  78. // clean up IPv6 global
  79. globalIPv6Network = nil
  80. }
  81. return networkSettings
  82. }
  83. func TestIPv6InterfaceAllocationAutoNetmaskGt80(t *testing.T) {
  84. _, subnet, _ := net.ParseCIDR("2001:db8:1234:1234:1234::/81")
  85. networkSettings := newInterfaceAllocation(t, subnet, "", "", "", false)
  86. // ensure low manually assigend global ip
  87. ip := net.ParseIP(networkSettings.GlobalIPv6Address)
  88. _, subnet, _ = net.ParseCIDR(fmt.Sprintf("%s/%d", subnet.IP.String(), 120))
  89. if !subnet.Contains(ip) {
  90. t.Fatalf("Error ip %s not in subnet %s", ip.String(), subnet.String())
  91. }
  92. }
  93. func TestIPv6InterfaceAllocationAutoNetmaskLe80(t *testing.T) {
  94. _, subnet, _ := net.ParseCIDR("2001:db8:1234:1234:1234::/80")
  95. networkSettings := newInterfaceAllocation(t, subnet, "ab:cd:ab:cd:ab:cd", "", "", false)
  96. // ensure global ip with mac
  97. ip := net.ParseIP(networkSettings.GlobalIPv6Address)
  98. expectedIP := net.ParseIP("2001:db8:1234:1234:1234:abcd:abcd:abcd")
  99. if ip.String() != expectedIP.String() {
  100. t.Fatalf("Error ip %s should be %s", ip.String(), expectedIP.String())
  101. }
  102. // ensure link local format
  103. ip = net.ParseIP(networkSettings.LinkLocalIPv6Address)
  104. expectedIP = net.ParseIP("fe80::a9cd:abff:fecd:abcd")
  105. if ip.String() != expectedIP.String() {
  106. t.Fatalf("Error ip %s should be %s", ip.String(), expectedIP.String())
  107. }
  108. }
  109. func TestIPv6InterfaceAllocationRequest(t *testing.T) {
  110. _, subnet, _ := net.ParseCIDR("2001:db8:1234:1234:1234::/80")
  111. expectedIP := "2001:db8:1234:1234:1234::1328"
  112. networkSettings := newInterfaceAllocation(t, subnet, "", "", expectedIP, false)
  113. // ensure global ip with mac
  114. ip := net.ParseIP(networkSettings.GlobalIPv6Address)
  115. if ip.String() != expectedIP {
  116. t.Fatalf("Error ip %s should be %s", ip.String(), expectedIP)
  117. }
  118. // retry -> fails for duplicated address
  119. _ = newInterfaceAllocation(t, subnet, "", "", expectedIP, true)
  120. }
  121. func TestMacAddrGeneration(t *testing.T) {
  122. ip := net.ParseIP("192.168.0.1")
  123. mac := generateMacAddr(ip).String()
  124. // Should be consistent.
  125. if generateMacAddr(ip).String() != mac {
  126. t.Fatal("Inconsistent MAC address")
  127. }
  128. // Should be unique.
  129. ip2 := net.ParseIP("192.168.0.2")
  130. if generateMacAddr(ip2).String() == mac {
  131. t.Fatal("Non-unique MAC address")
  132. }
  133. }
  134. func TestLinkContainers(t *testing.T) {
  135. // Init driver
  136. if err := InitDriver(new(Config)); err != nil {
  137. t.Fatal("Failed to initialize network driver")
  138. }
  139. // Allocate interface
  140. if _, err := Allocate("container_id", "", "", ""); err != nil {
  141. t.Fatal("Failed to allocate network interface")
  142. }
  143. bridgeIface = "lo"
  144. if _, err := iptables.NewChain("DOCKER", bridgeIface, iptables.Filter, false); err != nil {
  145. t.Fatal(err)
  146. }
  147. if err := LinkContainers("-I", "172.17.0.1", "172.17.0.2", []nat.Port{nat.Port("1234")}, false); err != nil {
  148. t.Fatal("LinkContainers failed")
  149. }
  150. // flush rules
  151. if _, err := iptables.Raw([]string{"-F", "DOCKER"}...); err != nil {
  152. t.Fatal(err)
  153. }
  154. }