Browse Source

[3359] One memfile, one mysql test moved to common backend test class.

Tomek Mrugalski 11 years ago
parent
commit
c62714b4a8

+ 8 - 0
src/lib/dhcpsrv/tests/lease_mgr_unittest.cc

@@ -247,6 +247,14 @@ class LeaseMgrTest : public GenericLeaseMgrTest {
 public:
     LeaseMgrTest() {
     }
+
+    /// @brief Reopen the database
+    ///
+    /// No-op implementation. We need to provide concrete implementation,
+    /// as this is a pure virtual method in GenericLeaseMgrTest.
+    void reopen() {
+    }
+
 };
 
 namespace {

+ 22 - 80
src/lib/dhcpsrv/tests/memfile_lease_mgr_unittest.cc

@@ -34,13 +34,25 @@ namespace {
 // empty class for now, but may be extended once Addr6 becomes bigger
 class MemfileLeaseMgrTest : public GenericLeaseMgrTest {
 public:
+
+    /// @brief memfile lease mgr test constructor
+    ///
+    /// Creates memfile and stores it in lmptr_ pointer
     MemfileLeaseMgrTest() {
         const LeaseMgr::ParameterMap pmap;
         lmptr_ = new Memfile_LeaseMgr(pmap);
     }
 
+    virtual void reopen() {
+        /// @todo: write lease to disk, flush, read file from disk
+    }
+
+    /// @brief destructor
+    ///
+    /// destroys lease manager backend.
     virtual ~MemfileLeaseMgrTest() {
         delete lmptr_;
+        lmptr_ = 0;
     }
 
 };
@@ -57,91 +69,21 @@ TEST_F(MemfileLeaseMgrTest, constructor) {
 
 // Checks if the getType() and getName() methods both return "memfile".
 TEST_F(MemfileLeaseMgrTest, getTypeAndName) {
-    const LeaseMgr::ParameterMap pmap;  // Empty parameter map
-    boost::scoped_ptr<Memfile_LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
-
-    EXPECT_EQ(std::string("memfile"), lease_mgr->getType());
-    EXPECT_EQ(std::string("memory"), lease_mgr->getName());
+    EXPECT_EQ(std::string("memfile"), lmptr_->getType());
+    EXPECT_EQ(std::string("memory"),  lmptr_->getName());
 }
 
 // Checks that adding/getting/deleting a Lease6 object works.
 TEST_F(MemfileLeaseMgrTest, addGetDelete6) {
-    const LeaseMgr::ParameterMap pmap;  // Empty parameter map
-    boost::scoped_ptr<LeaseMgr> lease_mgr(new Memfile_LeaseMgr(pmap));
-
-    IOAddress addr("2001:db8:1::456");
-
-    uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
-    DuidPtr duid(new DUID(llt, sizeof(llt)));
-
-    uint32_t iaid = 7; // just a number
-
-    SubnetID subnet_id = 8; // just another number
-
-    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr,
-                               duid, iaid, 100, 200, 50, 80,
-                               subnet_id));
-
-    EXPECT_TRUE(lease_mgr->addLease(lease));
-
-    // should not be allowed to add a second lease with the same address
-    EXPECT_FALSE(lease_mgr->addLease(lease));
-
-    Lease6Ptr x = lease_mgr->getLease6(Lease::TYPE_NA,
-                                       IOAddress("2001:db8:1::234"));
-    EXPECT_EQ(Lease6Ptr(), x);
-
-    x = lease_mgr->getLease6(Lease::TYPE_NA,
-                             IOAddress("2001:db8:1::456"));
-    ASSERT_TRUE(x);
-
-    EXPECT_EQ(x->addr_, addr);
-    EXPECT_TRUE(*x->duid_ == *duid);
-    EXPECT_EQ(x->iaid_, iaid);
-    EXPECT_EQ(x->subnet_id_, subnet_id);
-
-    // These are not important from lease management perspective, but
-    // let's check them anyway.
-    EXPECT_EQ(x->type_, Lease::TYPE_NA);
-    EXPECT_EQ(x->preferred_lft_, 100);
-    EXPECT_EQ(x->valid_lft_, 200);
-    EXPECT_EQ(x->t1_, 50);
-    EXPECT_EQ(x->t2_, 80);
-
-    // Test getLease6(duid, iaid, subnet_id) - positive case
-    Lease6Ptr y = lease_mgr->getLease6(Lease::TYPE_NA, *duid, iaid,
-                                       subnet_id);
-    ASSERT_TRUE(y);
-    EXPECT_TRUE(*y->duid_ == *duid);
-    EXPECT_EQ(y->iaid_, iaid);
-    EXPECT_EQ(y->addr_, addr);
-
-    // Test getLease6(duid, iaid, subnet_id) - wrong iaid
-    uint32_t invalid_iaid = 9; // no such iaid
-    y = lease_mgr->getLease6(Lease::TYPE_NA, *duid, invalid_iaid,
-                             subnet_id);
-    EXPECT_FALSE(y);
-
-    uint32_t invalid_subnet_id = 999;
-    y = lease_mgr->getLease6(Lease::TYPE_NA, *duid, iaid,
-                             invalid_subnet_id);
-    EXPECT_FALSE(y);
-
-    // truncated duid
-    DuidPtr invalid_duid(new DUID(llt, sizeof(llt) - 1));
-    y = lease_mgr->getLease6(Lease::TYPE_NA, *invalid_duid, iaid,
-                             subnet_id);
-    EXPECT_FALSE(y);
-
-    // should return false - there's no such address
-    EXPECT_FALSE(lease_mgr->deleteLease(IOAddress("2001:db8:1::789")));
-
-    // this one should succeed
-    EXPECT_TRUE(lease_mgr->deleteLease(IOAddress("2001:db8:1::456")));
+    testAddGetDelete6(true);
+}
 
-    // after the lease is deleted, it should really be gone
-    x = lease_mgr->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::456"));
-    EXPECT_EQ(Lease6Ptr(), x);
+/// @brief Basic Lease4 Checks
+///
+/// Checks that the addLease, getLease4 (by address) and deleteLease (with an
+/// IPv4 address) works.
+TEST_F(MemfileLeaseMgrTest, basicLease4) {
+    testBasicLease4();
 }
 
 /// @todo Write more memfile tests

+ 4 - 38
src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc

@@ -323,45 +323,11 @@ TEST_F(MySqlLeaseMgrTest, checkVersion) {
 /// Checks that the addLease, getLease4 (by address) and deleteLease (with an
 /// IPv4 address) works.
 TEST_F(MySqlLeaseMgrTest, basicLease4) {
-    // Get the leases to be used for the test.
-    vector<Lease4Ptr> leases = createLeases4();
-
-    // Start the tests.  Add three leases to the database, read them back and
-    // check they are what we think they are.
-    EXPECT_TRUE(lmptr_->addLease(leases[1]));
-    EXPECT_TRUE(lmptr_->addLease(leases[2]));
-    EXPECT_TRUE(lmptr_->addLease(leases[3]));
-    lmptr_->commit();
-
-    // Reopen the database to ensure that they actually got stored.
-    reopen();
-
-    Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
-    ASSERT_TRUE(l_returned);
-    detailCompareLease(leases[1], l_returned);
-
-    l_returned = lmptr_->getLease4(ioaddress4_[2]);
-    ASSERT_TRUE(l_returned);
-    detailCompareLease(leases[2], l_returned);
-
-    l_returned = lmptr_->getLease4(ioaddress4_[3]);
-    ASSERT_TRUE(l_returned);
-    detailCompareLease(leases[3], l_returned);
-
-    // Check that we can't add a second lease with the same address
-    EXPECT_FALSE(lmptr_->addLease(leases[1]));
-
-    // Delete a lease, check that it's gone, and that we can't delete it
-    // a second time.
-    EXPECT_TRUE(lmptr_->deleteLease(ioaddress4_[1]));
-    l_returned = lmptr_->getLease4(ioaddress4_[1]);
-    EXPECT_FALSE(l_returned);
-    EXPECT_FALSE(lmptr_->deleteLease(ioaddress4_[1]));
+    testBasicLease4();
+}
 
-    // Check that the second address is still there.
-    l_returned = lmptr_->getLease4(ioaddress4_[2]);
-    ASSERT_TRUE(l_returned);
-    detailCompareLease(leases[2], l_returned);
+TEST_F(MySqlLeaseMgrTest, testAddGetDelete6) {
+    testAddGetDelete6(false);
 }
 
 /// @brief Basic Lease4 Checks

+ 124 - 0
src/lib/dhcpsrv/tests/test_utils.cc

@@ -577,6 +577,130 @@ GenericLeaseMgrTest::testGetLease4ClientIdHWAddrSubnetId() {
     EXPECT_FALSE(lease);
 }
 
+// Test that IPv6 lease can be added, retrieved and deleted
+void
+GenericLeaseMgrTest::testAddGetDelete6(bool check_t1_t2) {
+    IOAddress addr("2001:db8:1::456");
+
+    uint8_t llt[] = {0, 1, 2, 3, 4, 5, 6, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
+    DuidPtr duid(new DUID(llt, sizeof(llt)));
+
+    uint32_t iaid = 7; // just a number
+
+    SubnetID subnet_id = 8; // just another number
+
+    Lease6Ptr lease(new Lease6(Lease::TYPE_NA, addr, duid, iaid, 100, 200, 50,
+                               80, subnet_id));
+
+    EXPECT_TRUE(lmptr_->addLease(lease));
+
+    // should not be allowed to add a second lease with the same address
+    EXPECT_FALSE(lmptr_->addLease(lease));
+
+    Lease6Ptr x = lmptr_->getLease6(Lease::TYPE_NA,
+                                    IOAddress("2001:db8:1::234"));
+    EXPECT_EQ(Lease6Ptr(), x);
+
+    x = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::456"));
+    ASSERT_TRUE(x);
+
+    EXPECT_EQ(x->addr_, addr);
+    EXPECT_TRUE(*x->duid_ == *duid);
+    EXPECT_EQ(x->iaid_, iaid);
+    EXPECT_EQ(x->subnet_id_, subnet_id);
+
+    // These are not important from lease management perspective, but
+    // let's check them anyway.
+    EXPECT_EQ(Lease::TYPE_NA, x->type_);
+    EXPECT_EQ(100, x->preferred_lft_);
+    EXPECT_EQ(200, x->valid_lft_);
+    if (check_t1_t2) {
+        // Backend supports T1,T2 storage: check the values
+        EXPECT_EQ(50, x->t1_);
+        EXPECT_EQ(80, x->t2_);
+    } else {
+        // Backend does not support storing, check that it returns 0s.
+        EXPECT_EQ(0, x->t1_);
+        EXPECT_EQ(0, x->t2_);
+    }
+
+    // Test getLease6(duid, iaid, subnet_id) - positive case
+    Lease6Ptr y = lmptr_->getLease6(Lease::TYPE_NA, *duid, iaid, subnet_id);
+    ASSERT_TRUE(y);
+    EXPECT_TRUE(*y->duid_ == *duid);
+    EXPECT_EQ(y->iaid_, iaid);
+    EXPECT_EQ(y->addr_, addr);
+
+    // Test getLease6(duid, iaid, subnet_id) - wrong iaid
+    uint32_t invalid_iaid = 9; // no such iaid
+    y = lmptr_->getLease6(Lease::TYPE_NA, *duid, invalid_iaid, subnet_id);
+    EXPECT_FALSE(y);
+
+    uint32_t invalid_subnet_id = 999;
+    y = lmptr_->getLease6(Lease::TYPE_NA, *duid, iaid, invalid_subnet_id);
+    EXPECT_FALSE(y);
+
+    // truncated duid
+    DuidPtr invalid_duid(new DUID(llt, sizeof(llt) - 1));
+    y = lmptr_->getLease6(Lease::TYPE_NA, *invalid_duid, iaid, subnet_id);
+    EXPECT_FALSE(y);
+
+    // should return false - there's no such address
+    EXPECT_FALSE(lmptr_->deleteLease(IOAddress("2001:db8:1::789")));
+
+    // this one should succeed
+    EXPECT_TRUE(lmptr_->deleteLease(IOAddress("2001:db8:1::456")));
+
+    // after the lease is deleted, it should really be gone
+    x = lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::456"));
+    EXPECT_EQ(Lease6Ptr(), x);
+}
+
+/// Checks that the addLease, getLease4 (by address) and deleteLease (with an
+/// IPv4 address) works.
+void
+GenericLeaseMgrTest::testBasicLease4() {
+    // Get the leases to be used for the test.
+    vector<Lease4Ptr> leases = createLeases4();
+
+    // Start the tests.  Add three leases to the database, read them back and
+    // check they are what we think they are.
+    EXPECT_TRUE(lmptr_->addLease(leases[1]));
+    EXPECT_TRUE(lmptr_->addLease(leases[2]));
+    EXPECT_TRUE(lmptr_->addLease(leases[3]));
+    lmptr_->commit();
+
+    // Reopen the database to ensure that they actually got stored.
+    reopen();
+
+    Lease4Ptr l_returned = lmptr_->getLease4(ioaddress4_[1]);
+    ASSERT_TRUE(l_returned);
+    detailCompareLease(leases[1], l_returned);
+
+    l_returned = lmptr_->getLease4(ioaddress4_[2]);
+    ASSERT_TRUE(l_returned);
+    detailCompareLease(leases[2], l_returned);
+
+    l_returned = lmptr_->getLease4(ioaddress4_[3]);
+    ASSERT_TRUE(l_returned);
+    detailCompareLease(leases[3], l_returned);
+
+    // Check that we can't add a second lease with the same address
+    EXPECT_FALSE(lmptr_->addLease(leases[1]));
+
+    // Delete a lease, check that it's gone, and that we can't delete it
+    // a second time.
+    EXPECT_TRUE(lmptr_->deleteLease(ioaddress4_[1]));
+    l_returned = lmptr_->getLease4(ioaddress4_[1]);
+    EXPECT_FALSE(l_returned);
+    EXPECT_FALSE(lmptr_->deleteLease(ioaddress4_[1]));
+
+    // Check that the second address is still there.
+    l_returned = lmptr_->getLease4(ioaddress4_[2]);
+    ASSERT_TRUE(l_returned);
+    detailCompareLease(leases[2], l_returned);
+}
+
 
 };
 };

+ 19 - 0
src/lib/dhcpsrv/tests/test_utils.h

@@ -56,6 +56,12 @@ public:
     /// @brief Virtual destructor.
     virtual ~GenericLeaseMgrTest();
 
+    /// @brief Reopen the database
+    ///
+    /// Closes the database and re-open it. This must be implemented
+    /// in derived classes.
+    virtual void reopen() = 0;
+
     /// @brief Initialize Lease4 Fields
     ///
     /// Returns a pointer to a Lease4 structure.  Different values are put into
@@ -106,6 +112,9 @@ public:
     /// @return vector<Lease6Ptr> Vector of pointers to leases
     std::vector<Lease6Ptr> createLeases6();
 
+    /// @brief checks that addLease, getLease4(addr) and deleteLease() works
+    void testBasicLease4();
+
     /// @brief Test lease retrieval using client id.
     void testGetLease4ClientId();
 
@@ -118,6 +127,16 @@ public:
     /// @brief Test lease retrieval using client id, HW address and subnet id.
     void testGetLease4ClientIdHWAddrSubnetId();
 
+    /// @brief Test that IPv6 lease can be added, retrieved and deleted.
+    ///
+    /// This method checks basic IPv6 lease operations. There's check_t1_t2
+    /// parameter that controls whether the backend supports storing T1, T2
+    /// parameters. memfile supports it, while MySQL doesn't. If T1,T2
+    /// storage is not supported, the expected values are 0.
+    ///
+    /// @param check_t1_t2 controls whether T1,T2 timers should be checked
+    void testAddGetDelete6(bool check_t1_t2);
+
     // Member variables
     std::vector<std::string>  straddress4_;   ///< String forms of IPv4 addresses
     std::vector<isc::asiolink::IOAddress> ioaddress4_;  ///< IOAddress forms of IPv4 addresses