driver_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. )
  9. func init() {
  10. // reset the new proxy command for mocking out the userland proxy in tests
  11. portmapper.NewProxy = portmapper.NewMockProxyCommand
  12. }
  13. func findFreePort(t *testing.T) int {
  14. l, err := net.Listen("tcp", ":0")
  15. if err != nil {
  16. t.Fatal("Failed to find a free port")
  17. }
  18. defer l.Close()
  19. result, err := net.ResolveTCPAddr("tcp", l.Addr().String())
  20. if err != nil {
  21. t.Fatal("Failed to resolve address to identify free port")
  22. }
  23. return result.Port
  24. }
  25. func newPortAllocationJob(eng *engine.Engine, port int) (job *engine.Job) {
  26. strPort := strconv.Itoa(port)
  27. job = eng.Job("allocate_port", "container_id")
  28. job.Setenv("HostIP", "127.0.0.1")
  29. job.Setenv("HostPort", strPort)
  30. job.Setenv("Proto", "tcp")
  31. job.Setenv("ContainerPort", strPort)
  32. return
  33. }
  34. func newPortAllocationJobWithInvalidHostIP(eng *engine.Engine, port int) (job *engine.Job) {
  35. strPort := strconv.Itoa(port)
  36. job = eng.Job("allocate_port", "container_id")
  37. job.Setenv("HostIP", "localhost")
  38. job.Setenv("HostPort", strPort)
  39. job.Setenv("Proto", "tcp")
  40. job.Setenv("ContainerPort", strPort)
  41. return
  42. }
  43. func TestAllocatePortDetection(t *testing.T) {
  44. eng := engine.New()
  45. eng.Logging = false
  46. freePort := findFreePort(t)
  47. // Init driver
  48. job := eng.Job("initdriver")
  49. if res := InitDriver(job); res != engine.StatusOK {
  50. t.Fatal("Failed to initialize network driver")
  51. }
  52. // Allocate interface
  53. job = eng.Job("allocate_interface", "container_id")
  54. if res := Allocate(job); res != engine.StatusOK {
  55. t.Fatal("Failed to allocate network interface")
  56. }
  57. // Allocate same port twice, expect failure on second call
  58. job = newPortAllocationJob(eng, freePort)
  59. if res := AllocatePort(job); res != engine.StatusOK {
  60. t.Fatal("Failed to find a free port to allocate")
  61. }
  62. if res := AllocatePort(job); res == engine.StatusOK {
  63. t.Fatal("Duplicate port allocation granted by AllocatePort")
  64. }
  65. }
  66. func TestHostnameFormatChecking(t *testing.T) {
  67. eng := engine.New()
  68. eng.Logging = false
  69. freePort := findFreePort(t)
  70. // Init driver
  71. job := eng.Job("initdriver")
  72. if res := InitDriver(job); res != engine.StatusOK {
  73. t.Fatal("Failed to initialize network driver")
  74. }
  75. // Allocate interface
  76. job = eng.Job("allocate_interface", "container_id")
  77. if res := Allocate(job); res != engine.StatusOK {
  78. t.Fatal("Failed to allocate network interface")
  79. }
  80. // Allocate port with invalid HostIP, expect failure with Bad Request http status
  81. job = newPortAllocationJobWithInvalidHostIP(eng, freePort)
  82. if res := AllocatePort(job); res == engine.StatusOK {
  83. t.Fatal("Failed to check invalid HostIP")
  84. }
  85. }
  86. func TestMacAddrGeneration(t *testing.T) {
  87. ip := net.ParseIP("192.168.0.1")
  88. mac := generateMacAddr(ip).String()
  89. // Should be consistent.
  90. if generateMacAddr(ip).String() != mac {
  91. t.Fatal("Inconsistent MAC address")
  92. }
  93. // Should be unique.
  94. ip2 := net.ParseIP("192.168.0.2")
  95. if generateMacAddr(ip2).String() == mac {
  96. t.Fatal("Non-unique MAC address")
  97. }
  98. }