From 0868f52a41a3ec9abbed46f0c65297d021e635f2 Mon Sep 17 00:00:00 2001 From: Marcin Siodelski Date: Tue, 3 Feb 2015 12:23:08 +0100 Subject: [PATCH] [3692] Added unit test for the allocation engine short pool. --- .../dhcpsrv/tests/alloc_engine_unittest.cc | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/src/lib/dhcpsrv/tests/alloc_engine_unittest.cc b/src/lib/dhcpsrv/tests/alloc_engine_unittest.cc index 55ffe109cc..fd8bc18863 100644 --- a/src/lib/dhcpsrv/tests/alloc_engine_unittest.cc +++ b/src/lib/dhcpsrv/tests/alloc_engine_unittest.cc @@ -433,11 +433,7 @@ public: // instantiate cfg_mgr CfgMgr& cfg_mgr = CfgMgr::instance(); - subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3)); - pool_ = Pool4Ptr(new Pool4(IOAddress("192.0.2.100"), - IOAddress("192.0.2.109"))); - subnet_->addPool(pool_); - cfg_mgr.getStagingCfg()->getCfgSubnets4()->add(subnet_); + initSubnet(IOAddress("192.0.2.100"), IOAddress("192.0.2.109")); cfg_mgr.commit(); factory_.create("type=memfile universe=4 persist=false"); @@ -476,6 +472,20 @@ public: /// @todo: check cltt } + /// @brief Create a subnet with a specified pool of addresses. + /// + /// @param pool_start First address in the pool. + /// @param pool_end Last address in the pool. + void initSubnet(const IOAddress& pool_start, const IOAddress& pool_end) { + CfgMgr& cfg_mgr = CfgMgr::instance(); + + subnet_ = Subnet4Ptr(new Subnet4(IOAddress("192.0.2.0"), 24, 1, 2, 3)); + pool_ = Pool4Ptr(new Pool4(pool_start, pool_end)); + subnet_->addPool(pool_); + + cfg_mgr.getStagingCfg()->getCfgSubnets4()->add(subnet_); + } + virtual ~AllocEngine4Test() { factory_.destroy(); } @@ -2263,6 +2273,44 @@ TEST_F(AllocEngine4Test, reservedAddressVsDynamicPool) { EXPECT_NE(allocated_lease->addr_.toText(), "192.0.2.100"); } +// This test checks that the allocation engine refuses to allocate an +// address when the pool is exhausted, and the only one available +// address is reserved for a different client. +TEST_F(AllocEngine4Test, reservedAddressShortPool) { + AllocEngine engine(AllocEngine::ALLOC_ITERATIVE, 100, false); + + // Create short pool with only one address. + initSubnet(IOAddress("192.0.2.100"), IOAddress("192.0.2.100")); + // Reserve the address for a different client. + HostPtr host(new Host(&hwaddr2_->hwaddr_[0], hwaddr_->hwaddr_.size(), + Host::IDENT_HWADDR, subnet_->getID(), + SubnetID(0), IOAddress("192.0.2.100"))); + CfgMgr::instance().getStagingCfg()->getCfgHosts()->add(host); + CfgMgr::instance().commit(); + + // Allocation engine should determine that the available address is + // reserved for someone else and not allocate it. + Lease4Ptr allocated_lease = engine.allocateLease4(subnet_, ClientIdPtr(), + hwaddr_, + IOAddress("0.0.0.0"), + false, false, "", false, + CalloutHandlePtr(), + old_lease_); + EXPECT_FALSE(allocated_lease); + + // Now, let's remove the reservation. + initSubnet(IOAddress("192.0.2.100"), IOAddress("192.0.2.100")); + CfgMgr::instance().commit(); + + // Address should be successfully allocated. + allocated_lease = engine.allocateLease4(subnet_, ClientIdPtr(), hwaddr_, + IOAddress("0.0.0.0"), false, false, + "", false, CalloutHandlePtr(), + old_lease_); + ASSERT_TRUE(allocated_lease); + EXPECT_EQ("192.0.2.100", allocated_lease->addr_.toText()); +} + /// @brief helper class used in Hooks testing in AllocEngine6 /// /// It features a couple of callout functions and buffers to store -- 2.47.3