Skip to content

Commit

Permalink
Merge pull request moby#138 from fmzhen/test-dev
Browse files Browse the repository at this point in the history
Add some tests
  • Loading branch information
mavenugo committed May 10, 2015
2 parents 9ea1fd0 + de690d7 commit 37d554b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
39 changes: 39 additions & 0 deletions drivers/bridge/link_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
80 changes: 80 additions & 0 deletions netutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 37d554b

Please sign in to comment.