2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-02 06:55:16 +00:00

[#1654] Checkpoint: UT and hook to do

This commit is contained in:
Francis Dupont
2022-09-05 17:43:04 +02:00
parent 7bcfebf80c
commit e891976e33
10 changed files with 184 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2012-2020 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2012-2022 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this

View File

@@ -829,11 +829,12 @@ public:
/// or equal to this value will be included /// or equal to this value will be included
/// ///
/// @return collection of IPv4 leases /// @return collection of IPv4 leases
virtual Lease4Collection getLeases4ByRelayId(const OptionBuffer& relay_id, virtual Lease4Collection
const asiolink::IOAddress& lower_bound_address, getLeases4ByRelayId(const OptionBuffer& relay_id,
const LeasePageSize& page_size, const asiolink::IOAddress& lower_bound_address,
const time_t& qry_start_time = 0, const LeasePageSize& page_size,
const time_t& qry_end_time = 0) = 0; const time_t& qry_start_time = 0,
const time_t& qry_end_time = 0) = 0;
/// @brief Returns existing IPv4 leases with a given remote-id. /// @brief Returns existing IPv4 leases with a given remote-id.
/// ///
@@ -847,11 +848,12 @@ public:
/// or equal to this value will be included. Defaults to zero. /// or equal to this value will be included. Defaults to zero.
/// ///
/// @return collection of IPv4 leases /// @return collection of IPv4 leases
virtual Lease4Collection getLeases4ByRemoteId(const OptionBuffer& remote_id, virtual Lease4Collection
const asiolink::IOAddress& lower_bound_address, getLeases4ByRemoteId(const OptionBuffer& remote_id,
const LeasePageSize& page_size, const asiolink::IOAddress& lower_bound_address,
const time_t& qry_start_time = 0, const LeasePageSize& page_size,
const time_t& qry_end_time = 0) = 0; const time_t& qry_start_time = 0,
const time_t& qry_end_time = 0) = 0;
/// @brief Returns existing IPv6 leases with a given relay-id. /// @brief Returns existing IPv6 leases with a given relay-id.
/// ///
@@ -862,10 +864,11 @@ public:
/// @param page_size maximum size of the page returned. /// @param page_size maximum size of the page returned.
/// ///
/// @return collection of IPv6 leases /// @return collection of IPv6 leases
virtual Lease6Collection getLeases6ByRelayId(const DUID& relay_id, virtual Lease6Collection
const asiolink::IOAddress& link_addr, getLeases6ByRelayId(const DUID& relay_id,
const asiolink::IOAddress& lower_bound_address, const asiolink::IOAddress& link_addr,
const LeasePageSize& page_size) = 0; const asiolink::IOAddress& lower_bound_address,
const LeasePageSize& page_size) = 0;
/// @brief Returns existing IPv6 leases with a given remote-id. /// @brief Returns existing IPv6 leases with a given remote-id.
/// ///
@@ -876,10 +879,11 @@ public:
/// @param page_size maximum size of the page returned. /// @param page_size maximum size of the page returned.
/// ///
/// @return collection of IPv6 leases /// @return collection of IPv6 leases
virtual Lease6Collection getLeases6ByRemoteId(const OptionBuffer& remote_id, virtual Lease6Collection
const asiolink::IOAddress& link_addr, getLeases6ByRemoteId(const OptionBuffer& remote_id,
const asiolink::IOAddress& lower_bound_address, const asiolink::IOAddress& link_addr,
const LeasePageSize& page_size) = 0; const asiolink::IOAddress& lower_bound_address,
const LeasePageSize& page_size) = 0;
/// @brief Returns existing IPv6 leases with on a given link. /// @brief Returns existing IPv6 leases with on a given link.
/// ///
@@ -889,9 +893,20 @@ public:
/// @param page_size maximum size of the page returned. /// @param page_size maximum size of the page returned.
/// ///
/// @return collection of IPv6 leases /// @return collection of IPv6 leases
virtual Lease6Collection getLeases6ByLink(const asiolink::IOAddress& link_addr, virtual Lease6Collection
const asiolink::IOAddress& lower_bound_address, getLeases6ByLink(const asiolink::IOAddress& link_addr,
const LeasePageSize& page_size) = 0; const asiolink::IOAddress& lower_bound_address,
const LeasePageSize& page_size) = 0;
/// @brief Write V4 leases to a file.
///
/// @param filename File name to write leases.
virtual void writeLeases4(const std::string& filename) = 0;
/// @brief Write V6 leases to a file.
///
/// @param filename File name to write leases.
virtual void writeLeases6(const std::string& filename) = 0;
private: private:
/// The IOService object, used for all ASIO operations. /// The IOService object, used for all ASIO operations.

View File

@@ -2428,5 +2428,71 @@ Memfile_LeaseMgr::getLeases6ByLink(const IOAddress& /* link_addr */,
isc_throw(NotImplemented, "Memfile_LeaseMgr::getLeases6ByLink not implemented"); isc_throw(NotImplemented, "Memfile_LeaseMgr::getLeases6ByLink not implemented");
} }
void
Memfile_LeaseMgr::writeLeases4(const std::string& filename) {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
writeLeases4Internal(filename);
} else {
writeLeases4Internal(filename);
}
}
void
Memfile_LeaseMgr::writeLeases4Internal(const std::string& filename) {
if (lease_file4_->getFilename() == filename) {
lease_file4_->close();
}
try {
std::ostringstream old;
old << filename << ".bak" << getpid();
::rename(filename.c_str(), old.str().c_str());
CSVLeaseFile4 backup(filename);
backup.open();
for (const auto& lease : storage4_) {
backup.append(*lease);
}
backup.close();
} catch (const std::exception&) {
if (lease_file4_->getFilename() == filename) {
lease_file4_->open(true);
}
throw;
}
}
void
Memfile_LeaseMgr::writeLeases6(const std::string& filename) {
if (MultiThreadingMgr::instance().getMode()) {
std::lock_guard<std::mutex> lock(*mutex_);
writeLeases6Internal(filename);
} else {
writeLeases6Internal(filename);
}
}
void
Memfile_LeaseMgr::writeLeases6Internal(const std::string& filename) {
if (lease_file6_->getFilename() == filename) {
lease_file6_->close();
}
try {
std::ostringstream old;
old << filename << ".bak" << getpid();
::rename(filename.c_str(), old.str().c_str());
CSVLeaseFile6 backup(filename);
backup.open();
for (const auto& lease : storage6_) {
backup.append(*lease);
}
backup.close();
} catch (const std::exception&) {
if (lease_file6_->getFilename() == filename) {
lease_file6_->open(true);
}
throw;
}
}
} // namespace dhcp } // namespace dhcp
} // namespace isc } // namespace isc

View File

@@ -1066,6 +1066,7 @@ private:
/// @brief stores IPv6 leases /// @brief stores IPv6 leases
Lease6Storage storage6_; Lease6Storage storage6_;
protected:
/// @brief Holds the pointer to the DHCPv4 lease file IO. /// @brief Holds the pointer to the DHCPv4 lease file IO.
boost::shared_ptr<CSVLeaseFile4> lease_file4_; boost::shared_ptr<CSVLeaseFile4> lease_file4_;
@@ -1356,6 +1357,29 @@ public:
getLeases6ByLink(const asiolink::IOAddress& link_addr, getLeases6ByLink(const asiolink::IOAddress& link_addr,
const asiolink::IOAddress& lower_bound_address, const asiolink::IOAddress& lower_bound_address,
const LeasePageSize& page_size) override; const LeasePageSize& page_size) override;
/// @brief Write V4 leases to a file.
///
/// @param filename File name to write leases.
virtual void writeLeases4(const std::string& filename) override;
/// @brief Write V6 leases to a file.
///
/// @param filename File name to write leases.
virtual void writeLeases6(const std::string& filename) override;
private:
/// @brief Write V4 leases to a file.
///
/// @param filename File name to write leases.
/// Must be called from a thread-safe context.
virtual void writeLeases4Internal(const std::string& filename);
/// @brief Write V6 leases to a file.
///
/// @param filename File name to write leases.
/// Must be called from a thread-safe context.
virtual void writeLeases6Internal(const std::string& filename);
}; };
} // namespace dhcp } // namespace dhcp

View File

@@ -3210,6 +3210,16 @@ MySqlLeaseMgr::clearClassLeaseCounts() {
isc_throw(NotImplemented, "MySqlLeaseMgr::clearClassLeaseCounts() not implemented"); isc_throw(NotImplemented, "MySqlLeaseMgr::clearClassLeaseCounts() not implemented");
} }
void
MySqlLeaseMgr::writeLeases4(const std::string&) {
isc_throw(NotImplemented, "MySqlLeaseMgr::writeLeases4() not implemented");
}
void
MySqlLeaseMgr::writeLeases6(const std::string&) {
isc_throw(NotImplemented, "MySqlLeaseMgr::writeLeases6() not implemented");
}
LeaseStatsQueryPtr LeaseStatsQueryPtr
MySqlLeaseMgr::startLeaseStatsQuery4() { MySqlLeaseMgr::startLeaseStatsQuery4() {
// Get a context // Get a context

View File

@@ -997,6 +997,12 @@ private:
/// @brief Clears the class-lease count map. /// @brief Clears the class-lease count map.
virtual void clearClassLeaseCounts() override; virtual void clearClassLeaseCounts() override;
/// @brief Write V4 leases to a file.
virtual void writeLeases4(const std::string& /*filename*/) override;
/// @brief Write V6 leases to a file.
virtual void writeLeases6(const std::string& /*filename*/) override;
/// @brief Check Error and Throw Exception /// @brief Check Error and Throw Exception
/// ///
/// This method invokes @ref MySqlConnection::checkError. /// This method invokes @ref MySqlConnection::checkError.

View File

@@ -2413,6 +2413,16 @@ PgSqlLeaseMgr::clearClassLeaseCounts() {
isc_throw(NotImplemented, "PgSqlLeaseMgr::clearClassLeaseCounts() not implemented"); isc_throw(NotImplemented, "PgSqlLeaseMgr::clearClassLeaseCounts() not implemented");
} }
void
PgSqlLeaseMgr::writeLeases4(const std::string&) {
isc_throw(NotImplemented, "PgSqlLeaseMgr::writeLeases4() not implemented");
}
void
PgSqlLeaseMgr::writeLeases6(const std::string&) {
isc_throw(NotImplemented, "PgSqlLeaseMgr::writeLeases6() not implemented");
}
LeaseStatsQueryPtr LeaseStatsQueryPtr
PgSqlLeaseMgr::startLeaseStatsQuery4() { PgSqlLeaseMgr::startLeaseStatsQuery4() {
// Get a context // Get a context

View File

@@ -1056,6 +1056,12 @@ private:
const asiolink::IOAddress& lower_bound_address, const asiolink::IOAddress& lower_bound_address,
const LeasePageSize& page_size) override; const LeasePageSize& page_size) override;
/// @brief Write V4 leases to a file.
virtual void writeLeases4(const std::string& /*filename*/) override;
/// @brief Write V6 leases to a file.
virtual void writeLeases6(const std::string& /*filename*/) override;
/// @brief Context RAII Allocator. /// @brief Context RAII Allocator.
class PgSqlLeaseContextAlloc { class PgSqlLeaseContextAlloc {
public: public:

View File

@@ -396,7 +396,6 @@ public:
isc_throw(NotImplemented, "ConcreteLeaseMgr::clearClassLeaseCounts() not implemented"); isc_throw(NotImplemented, "ConcreteLeaseMgr::clearClassLeaseCounts() not implemented");
} }
/// @brief Stub implementation. /// @brief Stub implementation.
Lease4Collection Lease4Collection
getLeases4ByRelayId(const OptionBuffer& /* relay_id */, getLeases4ByRelayId(const OptionBuffer& /* relay_id */,
@@ -441,6 +440,15 @@ public:
const IOAddress& /* lower_bound_address */, const IOAddress& /* lower_bound_address */,
const LeasePageSize& /* page_size */) override { const LeasePageSize& /* page_size */) override {
isc_throw(NotImplemented, "ConcreteLeaseMgr::getLeases6ByLink not implemented"); isc_throw(NotImplemented, "ConcreteLeaseMgr::getLeases6ByLink not implemented");
/// @brief Pretends to write V4 leases to a file.
virtual void writeLeases4(const std::string&) override {
isc_throw(NotImplemented, "ConcreteLeaseMgr::writeLeases4() not implemented");
}
/// @brief Pretends to write V6 leases to a file.
virtual void writeLeases6(const std::string&) override {
isc_throw(NotImplemented, "ConcreteLeaseMgr::writeLeases6() not implemented");
} }
/// @brief Returns backend type. /// @brief Returns backend type.

View File

@@ -2804,4 +2804,20 @@ TEST_F(MemfileLeaseMgrTest, classLeaseRecount6) {
EXPECT_EQ(2, memfile_mgr->getClassLeaseCount("slice", Lease::TYPE_PD)); EXPECT_EQ(2, memfile_mgr->getClassLeaseCount("slice", Lease::TYPE_PD));
} }
/// @brief Class derived from @c Memfile_LeaseMgr to test write to file.
class WFMemfileLeaseMgr : public Memfile_LeaseMgr {
public:
/// @brief Constructor.
WFMemfileLeaseMgr(const DatabaseConnection::ParameterMap& parameters)
: Memfile_LeaseMgr(parameters) {
}
using Memfile_LeaseMgr::lease_file4_;
using Memfile_LeaseMgr::lease_file6_;
};
//////// test plan:
// bad file, basic, overwrite file, not-persistent x v4/v6
} // namespace } // namespace