diff --git a/libnetwork/drivers/bridge/link_test.go b/libnetwork/drivers/bridge/link_test.go new file mode 100644 index 0000000000..c021fd63c7 --- /dev/null +++ b/libnetwork/drivers/bridge/link_test.go @@ -0,0 +1,39 @@ +package bridge + +import ( + "testing" + + "github.com/docker/libnetwork/netutils" +) + +func getPorts() []netutils.TransportPort { + return []netutils.TransportPort{ + netutils.TransportPort{Proto: netutils.TCP, Port: uint16(5000)}, + netutils.TransportPort{Proto: netutils.UDP, Port: uint16(400)}, + netutils.TransportPort{Proto: netutils.TCP, Port: uint16(600)}, + } +} + +func TestLinkNew(t *testing.T) { + ports := getPorts() + + link := newLink("172.0.17.3", "172.0.17.2", ports, "docker0") + + if link == nil { + t.FailNow() + } + if link.parentIP != "172.0.17.3" { + t.Fail() + } + if link.childIP != "172.0.17.2" { + t.Fail() + } + for i, p := range link.ports { + if p != ports[i] { + t.Fail() + } + } + if link.bridge != "docker0" { + t.Fail() + } +} diff --git a/libnetwork/netutils/utils_test.go b/libnetwork/netutils/utils_test.go index 12ff40a639..4ef8d48040 100644 --- a/libnetwork/netutils/utils_test.go +++ b/libnetwork/netutils/utils_test.go @@ -261,3 +261,83 @@ func TestNetIPCopyFunctions(t *testing.T) { t.Fatalf("Failed to return a true copy of net.IPNet") } } + +func TestPortBindingEqual(t *testing.T) { + pb1 := &PortBinding{ + Proto: TCP, + IP: net.ParseIP("172.17.0.1"), + Port: 80, + HostIP: net.ParseIP("192.168.100.1"), + HostPort: 8080, + } + + pb2 := &PortBinding{ + Proto: UDP, + IP: net.ParseIP("172.17.0.1"), + Port: 22, + HostIP: net.ParseIP("192.168.100.1"), + HostPort: 2222, + } + if !pb1.Equal(pb1) { + t.Fatalf("PortBinding.Equal() returned false negative") + } + + if pb1.Equal(nil) { + t.Fatalf("PortBinding.Equal() returned false negative") + } + + if pb1.Equal(pb2) { + t.Fatalf("PortBinding.Equal() returned false positive") + } + + if pb1.Equal(pb2) != pb2.Equal(pb1) { + t.Fatalf("PortBinding.Equal() failed commutative check") + } +} + +func TestPortBindingGetCopy(t *testing.T) { + pb := &PortBinding{ + Proto: TCP, + IP: net.ParseIP("172.17.0.1"), + Port: 80, + HostIP: net.ParseIP("192.168.100.1"), + HostPort: 8080, + } + cp := pb.GetCopy() + + if !pb.Equal(&cp) { + t.Fatalf("Failed to return a copy of PortBinding") + } + + if pb == &cp { + t.Fatalf("Failed to return a true copy of PortBinding") + } +} + +func TestPortBindingContainerAddr(t *testing.T) { + pb := PortBinding{ + Proto: TCP, + IP: net.ParseIP("172.17.0.1"), + Port: 80, + HostIP: net.ParseIP("192.168.100.1"), + HostPort: 8080, + } + + container, err := pb.ContainerAddr() + + if err != nil { + t.Fatal(err) + } + + switch netAddr := container.(type) { + case *net.TCPAddr: + if !pb.IP.Equal(netAddr.IP) { + t.Fatalf("PortBinding.ContainerAddr() Failed to return a ContainerAddr") + } + if int(pb.Port) != netAddr.Port { + t.Fatalf("PortBinding.ContainerAddr() Failed to return a ContainerAddr") + } + case *net.UDPAddr: + t.Fatalf("PortBinding.ContainerAddr() Failed to check correct proto") + } +}