From e31e906e4e66335951905c1ddcb75634e4b8a737 Mon Sep 17 00:00:00 2001 From: Chris Telfer Date: Fri, 1 Jun 2018 14:03:20 -0400 Subject: [PATCH] Add pool reuse test to unit tests Add a test to confirm that the pool allocator will iterate through all the pools even if some earlier ones were freed before coming back to previously allocated pools. Signed-off-by: Chris Telfer --- libnetwork/ipam/allocator_test.go | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/libnetwork/ipam/allocator_test.go b/libnetwork/ipam/allocator_test.go index 567c1d0b4e..58e25d296b 100644 --- a/libnetwork/ipam/allocator_test.go +++ b/libnetwork/ipam/allocator_test.go @@ -590,6 +590,45 @@ func TestGetSameAddress(t *testing.T) { } } +func TestPoolAllocationReuse(t *testing.T) { + for _, store := range []bool{false, true} { + a, err := getAllocator(store) + assert.NoError(t, err) + + // First get all pools until they are exhausted to + pList := []string{} + pool, _, _, err := a.RequestPool(localAddressSpace, "", "", nil, false) + for err == nil { + pList = append(pList, pool) + pool, _, _, err = a.RequestPool(localAddressSpace, "", "", nil, false) + } + nPools := len(pList) + for _, pool := range pList { + if err := a.ReleasePool(pool); err != nil { + t.Fatal(err) + } + } + + // Now try to allocate then free nPool pools sequentially. + // Verify that we don't see any repeat networks even though + // we have freed them. + seen := map[string]bool{} + for i := 0; i < nPools; i++ { + pool, nw, _, err := a.RequestPool(localAddressSpace, "", "", nil, false) + if err != nil { + t.Fatal(err) + } + if _, ok := seen[nw.String()]; ok { + t.Fatalf("Network %s was reused before exhausing the pool list", nw.String()) + } + seen[nw.String()] = true + if err := a.ReleasePool(pool); err != nil { + t.Fatal(err) + } + } + } +} + func TestGetAddressSubPoolEqualPool(t *testing.T) { for _, store := range []bool{false, true} { a, err := getAllocator(store)