]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2764] Moved test functions to generic lease test
authorMarcin Siodelski <marcin@isc.org>
Mon, 20 Feb 2023 19:53:47 +0000 (20:53 +0100)
committerMarcin Siodelski <msiodelski@gmail.com>
Tue, 14 Mar 2023 18:23:31 +0000 (19:23 +0100)
src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc
src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h
src/lib/dhcpsrv/tests/tracking_lease_mgr_unittest.cc

index e278eb735d6204f2e610f55c2306bfea9d9eb8e7..3ea4ad71697f8f1e8cce9222ad96249094264003 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -2944,6 +2944,23 @@ GenericLeaseMgrTest::makeLease6(const Lease::Type& type,
     return lease;
 }
 
+void
+GenericLeaseMgrTest::logCallback(const TrackingLeaseMgr::CallbackType type, SubnetID subnet_id,
+                                 const LeasePtr& lease) {
+    logs_.push_back(Log{type, subnet_id, lease});
+}
+
+int
+GenericLeaseMgrTest::countLogs(TrackingLeaseMgr::CallbackType type, SubnetID subnet_id) const {
+    int count = 0;
+    for (auto log : logs_) {
+        if ((log.type == type) && (log.subnet_id == subnet_id)) {
+            ++count;
+        }
+    }
+    return (count);
+}
+
 void
 GenericLeaseMgrTest::testRecountLeaseStats4() {
     using namespace stats;
index fc45b862dcd5a48c7453ca613fa91562185128bc..b7c707df8514da1fa58b855420522514c81ce20f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2022 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2023 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -8,7 +8,7 @@
 #define GENERIC_LEASE_MGR_UNITTEST_H
 
 #include <asiolink/io_service.h>
-#include <dhcpsrv/lease_mgr.h>
+#include <dhcpsrv/tracking_lease_mgr.h>
 #include <dhcpsrv/timer_mgr.h>
 
 #include <gtest/gtest.h>
@@ -36,6 +36,17 @@ public:
     /// @brief Universe (V4 or V6).
     enum Universe { V4, V6 };
 
+    /// @brief A structure holding a single callback log entry.
+    ///
+    /// The @c logCallback function inserts logs of this type into the
+    /// @c logs vector. The vector can be later examined to see what
+    /// callbacks have been invoked.
+    typedef struct {
+        TrackingLeaseMgr::CallbackType type;
+        SubnetID subnet_id;
+        LeasePtr lease;
+    } Log;
+
     /// @brief Default constructor.
     GenericLeaseMgrTest();
 
@@ -154,6 +165,27 @@ public:
                          const uint32_t state = Lease::STATE_DEFAULT,
                          const data::ConstElementPtr user_context = data::ConstElementPtr());
 
+    /// @brief Callback function recording its parameters.
+    ///
+    /// It is used in the unit tests that verify that appropriate callbacks
+    /// have been invoked.
+    ///
+    /// @param type callback type.
+    /// @param subnet_id subnet identifier.
+    /// @param lease lease instance.
+    void logCallback(const TrackingLeaseMgr::CallbackType type, SubnetID subnet_id,
+                     const LeasePtr& lease);
+
+    /// @brief Counts log entries.
+    ///
+    /// It counts the logs associated with the specific callback type and subnet id.
+    ///
+    /// @param type callback type.
+    /// @param subnet_id subnet identifier.
+    /// @return The number of callback logs associated with the specific type and
+    /// the subnet id.
+    int countLogs(TrackingLeaseMgr::CallbackType type, SubnetID subnet_id) const;
+
     /// @brief checks that addLease, getLease4(addr) and deleteLease() works
     void testBasicLease4();
 
@@ -551,6 +583,9 @@ public:
     /// @brief IOAddress forms of IPv6 addresses
     std::vector<isc::asiolink::IOAddress> ioaddress6_;
 
+    /// Recorded callback logs.
+    std::vector<Log> logs_;
+
     /// @brief Pointer to the lease manager
     LeaseMgr* lmptr_;
 };
index d5bde6b268a4aa89ab1082b6d747b8ceb90f96a6..64d284d2eae373fb6cafab503ba1319c3120087c 100644 (file)
@@ -27,17 +27,6 @@ namespace {
 class TrackingLeaseMgrTest : public GenericLeaseMgrTest {
 public:
 
-    /// @brief A structure holding a single callback log entry.
-    ///
-    /// The @c logCallback function inserts logs of this type into the
-    /// @c logs vector. The vector can be later examined to see what
-    /// callbacks have been invoked.
-    typedef struct {
-        TrackingLeaseMgr::CallbackType type;
-        SubnetID subnet_id;
-        LeasePtr lease;
-    } Log;
-
     /// @brief Reopen the database
     ///
     /// No-op implementation. We need to provide concrete implementation,
@@ -46,49 +35,21 @@ public:
 
     /// @brief Convenience function creating a lease instance.
     ///
+    /// We don't use the @c initializeLease4 and the initializeLease6 functions
+    /// here because they use tons of parameters we don't really need here.
+    /// We simply need the subnet id and the address. It improves the tests
+    /// readability.
+    ///
     /// @param subnet_id subnet identifier for the lease.
     /// @param address leased address.
     /// @tparam LeaseType type of the lease: @c Lease4 or Lease6.
     template<typename LeaseType>
-    boost::shared_ptr<LeaseType> createLease(int subnet_id, std::string address) const {
+    boost::shared_ptr<LeaseType> initializeLease(int subnet_id, std::string address) const {
         auto lease = boost::make_shared<LeaseType>();
         lease->subnet_id_ = SubnetID(subnet_id);
         lease->addr_ = IOAddress(address);
         return (lease);
     }
-
-    /// @brief Callback function recording its parameters.
-    ///
-    /// It is used in the unit tests that verify that appropriate callbacks
-    /// have been invoked.
-    ///
-    /// @param type callback type.
-    /// @param subnet_id subnet identifier.
-    /// @param lease lease instance.
-    void logCallback(const TrackingLeaseMgr::CallbackType type, SubnetID subnet_id, const LeasePtr& lease) {
-        logs_.push_back(Log{type, subnet_id, lease});
-    }
-
-    /// @brief Counts log entries.
-    ///
-    /// It counts the logs associated with the specific callback type and subnet id.
-    ///
-    /// @param type callback type.
-    /// @param subnet_id subnet identifier.
-    /// @return The number of callback logs associated with the specific type and
-    /// the subnet id.
-    int countLogs(TrackingLeaseMgr::CallbackType type, SubnetID subnet_id) const {
-        int count = 0;
-        for (auto log : logs_) {
-            if ((log.type == type) && (log.subnet_id == subnet_id)) {
-                ++count;
-            }
-        }
-        return (count);
-    }
-
-    /// Recorded callback logs.
-    std::vector<Log> logs_;
 };
 
 /// Tests that leases can be locked and unlocked. When a lease is locked
@@ -98,19 +59,19 @@ TEST_F(TrackingLeaseMgrTest, tryLock) {
     ConcreteLeaseMgr mgr(pmap);
 
     // An attempt to lock an already locked lease should fail.
-    EXPECT_TRUE(mgr.tryLock(createLease<Lease4>(1, "192.0.2.1")));
-    EXPECT_FALSE(mgr.tryLock(createLease<Lease4>(1, "192.0.2.1")));
+    EXPECT_TRUE(mgr.tryLock(initializeLease<Lease4>(1, "192.0.2.1")));
+    EXPECT_FALSE(mgr.tryLock(initializeLease<Lease4>(1, "192.0.2.1")));
 
     // We can lock another lease but we cannot lock an already locked one.
-    EXPECT_TRUE(mgr.tryLock(createLease<Lease4>(1, "192.0.2.2")));
-    EXPECT_FALSE(mgr.tryLock(createLease<Lease4>(1, "192.0.2.1")));
-    EXPECT_FALSE(mgr.tryLock(createLease<Lease4>(2, "192.0.2.2")));
+    EXPECT_TRUE(mgr.tryLock(initializeLease<Lease4>(1, "192.0.2.2")));
+    EXPECT_FALSE(mgr.tryLock(initializeLease<Lease4>(1, "192.0.2.1")));
+    EXPECT_FALSE(mgr.tryLock(initializeLease<Lease4>(2, "192.0.2.2")));
 
     // If we unlock the lease, it can be locked again. However, unlocking
     // the lease should not affect other locks.
-    mgr.unlock(createLease<Lease4>(1, "192.0.2.1"));
-    EXPECT_FALSE(mgr.tryLock(createLease<Lease4>(2, "192.0.2.2")));
-    EXPECT_TRUE(mgr.tryLock(createLease<Lease4>(1, "192.0.2.1")));
+    mgr.unlock(initializeLease<Lease4>(1, "192.0.2.1"));
+    EXPECT_FALSE(mgr.tryLock(initializeLease<Lease4>(2, "192.0.2.2")));
+    EXPECT_TRUE(mgr.tryLock(initializeLease<Lease4>(1, "192.0.2.1")));
 }
 
 /// Tests registering the callbacks.
@@ -155,14 +116,14 @@ TEST_F(TrackingLeaseMgrTest, registerCallbacks) {
                                                    _1)));
 
     // This call should trigger the lease add callbacks for subnet id 0 and 1.
-    EXPECT_NO_THROW(mgr.trackAddLease(createLease<Lease4>(1, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackAddLease(initializeLease<Lease4>(1, "192.0.2.1"), false));
     EXPECT_EQ(2, logs_.size());
     EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, 0));
     EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, 1));
 
     // This call should trigger the lease add callback for subnet id 0 only. That's
     // because we have no callback for the subnet id 3.
-    EXPECT_NO_THROW(mgr.trackAddLease(createLease<Lease4>(3, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackAddLease(initializeLease<Lease4>(3, "192.0.2.1"), false));
     EXPECT_EQ(3, logs_.size());
     EXPECT_EQ(2, countLogs(TrackingLeaseMgr::TRACK_ADD_LEASE, 0));
 }
@@ -239,7 +200,7 @@ TEST_F(TrackingLeaseMgrTest, trackUpdateLease) {
                                                    TrackingLeaseMgr::TRACK_DELETE_LEASE,
                                                    0,
                                                    _1)));
-    EXPECT_NO_THROW(mgr.trackUpdateLease(createLease<Lease4>(1, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackUpdateLease(initializeLease<Lease4>(1, "192.0.2.1"), false));
     EXPECT_EQ(1, logs_.size());
     EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_UPDATE_LEASE, 0));
 }
@@ -267,7 +228,7 @@ TEST_F(TrackingLeaseMgrTest, trackDeleteLease) {
                                                    TrackingLeaseMgr::TRACK_DELETE_LEASE,
                                                    0,
                                                    _1)));
-    EXPECT_NO_THROW(mgr.trackDeleteLease(createLease<Lease4>(1, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackDeleteLease(initializeLease<Lease4>(1, "192.0.2.1"), false));
     EXPECT_EQ(1, logs_.size());
     EXPECT_EQ(1, countLogs(TrackingLeaseMgr::TRACK_DELETE_LEASE, 0));
 }
@@ -325,9 +286,9 @@ TEST_F(TrackingLeaseMgrTest, unregisterCallbacksBySubnetID) {
     EXPECT_NO_THROW(mgr.unregisterCallbacks(SubnetID(1)));
 
     // Invoke the remaining callbacksm for the subnet id 1.
-    EXPECT_NO_THROW(mgr.trackAddLease(createLease<Lease4>(1, "192.0.2.1"), false));
-    EXPECT_NO_THROW(mgr.trackUpdateLease(createLease<Lease4>(1, "192.0.2.1"), false));
-    EXPECT_NO_THROW(mgr.trackDeleteLease(createLease<Lease4>(1, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackAddLease(initializeLease<Lease4>(1, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackUpdateLease(initializeLease<Lease4>(1, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackDeleteLease(initializeLease<Lease4>(1, "192.0.2.1"), false));
 
     // It should only run the callback for the subnet id 0 that is still
     // registered.
@@ -338,7 +299,7 @@ TEST_F(TrackingLeaseMgrTest, unregisterCallbacksBySubnetID) {
     EXPECT_NO_THROW(mgr.unregisterCallbacks(SubnetID(0)));
 
     // Make sure it is no longer invoked.
-    EXPECT_NO_THROW(mgr.trackAddLease(createLease<Lease4>(1, "192.0.2.1"), false));
+    EXPECT_NO_THROW(mgr.trackAddLease(initializeLease<Lease4>(1, "192.0.2.1"), false));
     EXPECT_EQ(1, logs_.size());
 
     // Unregistering it again should be no-op.