1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Add tests of tcp port allocator

This commit is contained in:
Solomon Hykes 2013-04-05 13:03:24 -07:00
parent d32f184696
commit febaeebfb8
2 changed files with 38 additions and 0 deletions

View file

@ -176,6 +176,7 @@ func (alloc *PortAllocator) runFountain() {
// FIXME: Release can no longer fail, change its prototype to reflect that.
func (alloc *PortAllocator) Release(port int) error {
Debugf("Releasing %d", port)
alloc.lock.Lock()
delete(alloc.inUse, port)
alloc.lock.Unlock()
@ -183,6 +184,7 @@ func (alloc *PortAllocator) Release(port int) error {
}
func (alloc *PortAllocator) Acquire(port int) (int, error) {
Debugf("Acquiring %d", port)
if port == 0 {
// Allocate a port from the fountain
for port := range alloc.fountain {

View file

@ -18,6 +18,42 @@ func TestIptables(t *testing.T) {
}
}
func TestPortAllocation(t *testing.T) {
allocator, err := newPortAllocator()
if err != nil {
t.Fatal(err)
}
if port, err := allocator.Acquire(80); err != nil {
t.Fatal(err)
} else if port != 80 {
t.Fatalf("Acquire(80) should return 80, not %d", port)
}
port, err := allocator.Acquire(0)
if err != nil {
t.Fatal(err)
}
if port <= 0 {
t.Fatalf("Acquire(0) should return a non-zero port")
}
if _, err := allocator.Acquire(port); err == nil {
t.Fatalf("Acquiring a port already in use should return an error")
}
if newPort, err := allocator.Acquire(0); err != nil {
t.Fatal(err)
} else if newPort == port {
t.Fatalf("Acquire(0) allocated the same port twice: %d", port)
}
if _, err := allocator.Acquire(80); err == nil {
t.Fatalf("Acquiring a port already in use should return an error")
}
if err := allocator.Release(80); err != nil {
t.Fatal(err)
}
if _, err := allocator.Acquire(80); err != nil {
t.Fatal(err)
}
}
func TestNetworkRange(t *testing.T) {
// Simple class C test
_, network, _ := net.ParseCIDR("192.168.0.1/24")