github.com/titanous/docker@v1.4.1/daemon/networkdriver/bridge/driver_test.go (about) 1 package bridge 2 3 import ( 4 "net" 5 "strconv" 6 "testing" 7 8 "github.com/docker/docker/daemon/networkdriver/portmapper" 9 "github.com/docker/docker/engine" 10 ) 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 17 func findFreePort(t *testing.T) int { 18 l, err := net.Listen("tcp", ":0") 19 if err != nil { 20 t.Fatal("Failed to find a free port") 21 } 22 defer l.Close() 23 24 result, err := net.ResolveTCPAddr("tcp", l.Addr().String()) 25 if err != nil { 26 t.Fatal("Failed to resolve address to identify free port") 27 } 28 return result.Port 29 } 30 31 func newPortAllocationJob(eng *engine.Engine, port int) (job *engine.Job) { 32 strPort := strconv.Itoa(port) 33 34 job = eng.Job("allocate_port", "container_id") 35 job.Setenv("HostIP", "127.0.0.1") 36 job.Setenv("HostPort", strPort) 37 job.Setenv("Proto", "tcp") 38 job.Setenv("ContainerPort", strPort) 39 return 40 } 41 42 func newPortAllocationJobWithInvalidHostIP(eng *engine.Engine, port int) (job *engine.Job) { 43 strPort := strconv.Itoa(port) 44 45 job = eng.Job("allocate_port", "container_id") 46 job.Setenv("HostIP", "localhost") 47 job.Setenv("HostPort", strPort) 48 job.Setenv("Proto", "tcp") 49 job.Setenv("ContainerPort", strPort) 50 return 51 } 52 53 func TestAllocatePortDetection(t *testing.T) { 54 eng := engine.New() 55 eng.Logging = false 56 57 freePort := findFreePort(t) 58 59 // Init driver 60 job := eng.Job("initdriver") 61 if res := InitDriver(job); res != engine.StatusOK { 62 t.Fatal("Failed to initialize network driver") 63 } 64 65 // Allocate interface 66 job = eng.Job("allocate_interface", "container_id") 67 if res := Allocate(job); res != engine.StatusOK { 68 t.Fatal("Failed to allocate network interface") 69 } 70 71 // Allocate same port twice, expect failure on second call 72 job = newPortAllocationJob(eng, freePort) 73 if res := AllocatePort(job); res != engine.StatusOK { 74 t.Fatal("Failed to find a free port to allocate") 75 } 76 if res := AllocatePort(job); res == engine.StatusOK { 77 t.Fatal("Duplicate port allocation granted by AllocatePort") 78 } 79 } 80 81 func TestHostnameFormatChecking(t *testing.T) { 82 eng := engine.New() 83 eng.Logging = false 84 85 freePort := findFreePort(t) 86 87 // Init driver 88 job := eng.Job("initdriver") 89 if res := InitDriver(job); res != engine.StatusOK { 90 t.Fatal("Failed to initialize network driver") 91 } 92 93 // Allocate interface 94 job = eng.Job("allocate_interface", "container_id") 95 if res := Allocate(job); res != engine.StatusOK { 96 t.Fatal("Failed to allocate network interface") 97 } 98 99 // Allocate port with invalid HostIP, expect failure with Bad Request http status 100 job = newPortAllocationJobWithInvalidHostIP(eng, freePort) 101 if res := AllocatePort(job); res == engine.StatusOK { 102 t.Fatal("Failed to check invalid HostIP") 103 } 104 } 105 106 func TestMacAddrGeneration(t *testing.T) { 107 ip := net.ParseIP("192.168.0.1") 108 mac := generateMacAddr(ip).String() 109 110 // Should be consistent. 111 if generateMacAddr(ip).String() != mac { 112 t.Fatal("Inconsistent MAC address") 113 } 114 115 // Should be unique. 116 ip2 := net.ParseIP("192.168.0.2") 117 if generateMacAddr(ip2).String() == mac { 118 t.Fatal("Non-unique MAC address") 119 } 120 }