From 2c89d488ba0b7ed9030ec3e52614b51d63fe94db Mon Sep 17 00:00:00 2001 From: Thomas Markwalder Date: Fri, 4 Mar 2022 11:03:22 -0500 Subject: [PATCH] [#2330] Addressed review comments src/lib/dhcpsrv/testutils/generic_backend_unittest.* GenericBackendTest - Refactored to provide audit entry testing and logging src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.* src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.* Removed refactored stuff --- .../testutils/generic_backend_unittest.cc | 158 +++++++++++++++++- .../testutils/generic_backend_unittest.h | 78 +++++++++ .../testutils/generic_cb_dhcp4_unittest.cc | 154 +---------------- .../testutils/generic_cb_dhcp4_unittest.h | 74 ++------ .../testutils/generic_cb_dhcp6_unittest.cc | 101 +---------- .../testutils/generic_cb_dhcp6_unittest.h | 52 ++---- 6 files changed, 279 insertions(+), 338 deletions(-) diff --git a/src/lib/dhcpsrv/testutils/generic_backend_unittest.cc b/src/lib/dhcpsrv/testutils/generic_backend_unittest.cc index 092f57473f..c8ad19cf6e 100644 --- a/src/lib/dhcpsrv/testutils/generic_backend_unittest.cc +++ b/src/lib/dhcpsrv/testutils/generic_backend_unittest.cc @@ -11,15 +11,19 @@ #include #include #include +#include using namespace isc::data; +using namespace isc::db; namespace isc { namespace dhcp { namespace test { -GenericBackendTest::GenericBackendTest() { +GenericBackendTest::GenericBackendTest() + : timestamps_(), audit_entries_() { LibDHCP::clearRuntimeOptionDefs(); + initTimestamps(); } GenericBackendTest::~GenericBackendTest() { @@ -123,6 +127,158 @@ GenericBackendTest::checkConfiguredGlobal(const SrvConfigPtr& srv_cfg, checkConfiguredGlobal(srv_cfg, exp_global->getName(), exp_global->getElementValue()); } + +void +GenericBackendTest::testNewAuditEntry(const std::string& exp_object_type, + const AuditEntry::ModificationType& exp_modification_type, + const std::string& exp_log_message, + const ServerSelector& server_selector, + const size_t new_entries_num, + const size_t max_tested_entries) { + // Get the server tag for which the entries are fetched. + std::string tag; + if (server_selector.getType() == ServerSelector::Type::ALL) { + // Server tag is 'all'. + tag = "all"; + } else { + const auto& tags = server_selector.getTags(); + // This test is not meant to handle multiple server tags all at once. + if (tags.size() > 1) { + ADD_FAILURE() << "Test error: do not use multiple server tags"; + } else if (tags.size() == 1) { + // Get the server tag for which we run the current test. + tag = tags.begin()->get(); + } + } + + auto audit_entries_size_save = audit_entries_[tag].size(); + + // Audit entries for different server tags are stored in separate + // containers. + ASSERT_NO_THROW_LOG(audit_entries_[tag] + = getRecentAuditEntries(server_selector, timestamps_["two days ago"], 0)); + + ASSERT_EQ(audit_entries_size_save + new_entries_num, audit_entries_[tag].size()) + << logExistingAuditEntries(tag); + + auto& mod_time_idx = audit_entries_[tag].get(); + + // Iterate over specified number of entries starting from the most recent + // one and check they have correct values. + for (auto audit_entry_it = mod_time_idx.rbegin(); + ((std::distance(mod_time_idx.rbegin(), audit_entry_it) < new_entries_num) && + (std::distance(mod_time_idx.rbegin(), audit_entry_it) < max_tested_entries)); + ++audit_entry_it) { + auto audit_entry = *audit_entry_it; + EXPECT_EQ(exp_object_type, audit_entry->getObjectType()) + << logExistingAuditEntries(tag); + EXPECT_EQ(exp_modification_type, audit_entry->getModificationType()) + << logExistingAuditEntries(tag); + EXPECT_EQ(exp_log_message, audit_entry->getLogMessage()) + << logExistingAuditEntries(tag); + } +} + +void +GenericBackendTest::testNewAuditEntry(const std::vector& exp_entries, + const ServerSelector& server_selector) { + // Get the server tag for which the entries are fetched. + std::string tag; + if (server_selector.getType() == ServerSelector::Type::ALL) { + // Server tag is 'all'. + tag = "all"; + } else { + const auto& tags = server_selector.getTags(); + // This test is not meant to handle multiple server tags all at once. + if (tags.size() != 1) { + ADD_FAILURE() << "Test error: tags.size(): " << tags.size() + << ", you must specify one and only one server tag"; + } + + // Get the server tag for which we run the current test. + tag = tags.begin()->get(); + } + + size_t new_entries_num = exp_entries.size(); + + auto audit_entries_size_save = audit_entries_[tag].size(); + + // Audit entries for different server tags are stored in separate + // containers. + ASSERT_NO_THROW_LOG(audit_entries_[tag] + = getRecentAuditEntries(server_selector, timestamps_["two days ago"], 0)); + + ASSERT_EQ(audit_entries_size_save + new_entries_num, audit_entries_[tag].size()) + << logExistingAuditEntries(tag); + + auto& mod_time_idx = audit_entries_[tag].get(); + + // Iterate over specified number of entries starting from the most recent + // one and check they have correct values. + auto exp_entry = exp_entries.rbegin(); + for (auto audit_entry_it = mod_time_idx.rbegin(); + ((std::distance(mod_time_idx.rbegin(), audit_entry_it) < new_entries_num)); + ++audit_entry_it) { + + auto audit_entry = *audit_entry_it; + EXPECT_EQ((*exp_entry).object_type, audit_entry->getObjectType()) + << logExistingAuditEntries(tag); + EXPECT_EQ((*exp_entry).modification_type, audit_entry->getModificationType()) + << logExistingAuditEntries(tag); + EXPECT_EQ((*exp_entry).log_message, audit_entry->getLogMessage()) + << logExistingAuditEntries(tag); + + ++exp_entry; + } +} + +void +GenericBackendTest::initTimestamps() { + // Current time minus 1 hour to make sure it is in the past. + timestamps_["today"] = boost::posix_time::second_clock::local_time() + - boost::posix_time::hours(1); + // One second after today. + timestamps_["after today"] = timestamps_["today"] + boost::posix_time::seconds(1); + // Yesterday. + timestamps_["yesterday"] = timestamps_["today"] - boost::posix_time::hours(24); + // One second after yesterday. + timestamps_["after yesterday"] = timestamps_["yesterday"] + boost::posix_time::seconds(1); + // Two days ago. + timestamps_["two days ago"] = timestamps_["today"] - boost::posix_time::hours(48); + // Tomorrow. + timestamps_["tomorrow"] = timestamps_["today"] + boost::posix_time::hours(24); + // One second after tomorrow. + timestamps_["after tomorrow"] = timestamps_["tomorrow"] + boost::posix_time::seconds(1); +} + +std::string +GenericBackendTest::logExistingAuditEntries(const std::string& server_tag) { + std::ostringstream s; + + auto& mod_time_idx = audit_entries_[server_tag].get(); + + for (auto audit_entry_it = mod_time_idx.begin(); + audit_entry_it != mod_time_idx.end(); + ++audit_entry_it) { + auto audit_entry = *audit_entry_it; + s << audit_entry->getObjectType() << ", " + << audit_entry->getObjectId() << ", " + << static_cast(audit_entry->getModificationType()) << ", " + << audit_entry->getModificationTime() << ", " + << audit_entry->getRevisionId() << ", " + << audit_entry->getLogMessage() + << std::endl; + } + + return (s.str()); +} + +AuditEntryCollection +GenericBackendTest::getRecentAuditEntries(const ServerSelector&, const boost::posix_time::ptime&, + const uint64_t&) const { + return (AuditEntryCollection()); +} + } // end of namespace isc::dhcp::test } // end of namespace isc::dhcp } // end of namespace isc diff --git a/src/lib/dhcpsrv/testutils/generic_backend_unittest.h b/src/lib/dhcpsrv/testutils/generic_backend_unittest.h index b431ea5425..2db29308dc 100644 --- a/src/lib/dhcpsrv/testutils/generic_backend_unittest.h +++ b/src/lib/dhcpsrv/testutils/generic_backend_unittest.h @@ -9,6 +9,8 @@ #include #include +#include +#include #include #include #include @@ -22,6 +24,18 @@ namespace isc { namespace dhcp { namespace test { +/// @brief Describes an expected audit table entry. +struct ExpAuditEntry { + /// @brief Type of object changed. + std::string object_type; + + /// @brief Timestamp the change occurred. + db::AuditEntry::ModificationType modification_type; + + /// @brief Log message describing the change. + std::string log_message; +}; + /// @brief Generic test fixture class with utility functions for /// testing database backends. class GenericBackendTest : public ::testing::Test { @@ -264,6 +278,70 @@ public: void checkConfiguredGlobal(const SrvConfigPtr& srv_cfg, data::StampedValuePtr& exp_global); + /// @brief Tests that the new audit entry is added. + /// + /// This method retrieves a collection of the existing audit entries and + /// checks that the new one has been added at the end of this collection. + /// It then verifies the values of the audit entry against the values + /// specified by the caller. + /// + /// @param exp_object_type Expected object type. + /// @param exp_modification_type Expected modification type. + /// @param exp_log_message Expected log message. + /// @param server_selector Server selector to be used for next query. + /// @param new_entries_num Number of the new entries expected to be inserted. + /// @param max_tested_entries Maximum number of entries tested. + void testNewAuditEntry(const std::string& exp_object_type, + const db::AuditEntry::ModificationType& exp_modification_type, + const std::string& exp_log_message, + const db::ServerSelector& server_selector = db::ServerSelector::ALL(), + const size_t new_entries_num = 1, + const size_t max_tested_entries = 65535); + + /// @brief Checks the new audit entries against a list of + /// expected entries. + /// + /// This method retrieves a collection of the existing audit entries and + /// checks that number and content of the expected new entries have been + /// added to the end of this collection. + /// + /// @param exp_entries a list of the expected new audit entries. + /// @param server_selector Server selector to be used for next query. + void testNewAuditEntry(const std::vector& exp_entries, + const db::ServerSelector& server_selector); + + /// @brief Logs audit entries in the @c audit_entries_ member. + /// + /// This function is called in case of an error. + /// + /// @param server_tag Server tag for which the audit entries should be logged. + std::string logExistingAuditEntries(const std::string& server_tag); + + /// @brief Retrieves the most recent audit entries. + /// + /// Allowed server selectors: ALL, ONE. + /// Not allowed server selectors: ANY, UNASSIGNED, MULTIPLE. + /// + /// @param server_selector Server selector. + /// @param modification_time Timestamp being a lower limit for the returned + /// result set, i.e. entries later than specified time are returned. + /// @param modification_id Identifier being a lower limit for the returned + /// result set, used when two (or more) entries have the same + /// modification_time. + /// @return Collection of audit entries. + virtual db::AuditEntryCollection + getRecentAuditEntries(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time, + const uint64_t& modification_id) const ; + + /// @brief Initialize posix time values used in tests. + void initTimestamps(); + + /// @brief Holds timestamp values used in tests. + std::map timestamps_; + + /// @brief Holds the most recent audit entries. + std::map audit_entries_; }; } // end of namespace isc::dhcp::test diff --git a/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.cc b/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.cc index 568f61ec6c..587e969d2e 100644 --- a/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.cc +++ b/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.cc @@ -66,7 +66,6 @@ GenericConfigBackendDHCPv4Test::SetUp() { initTestSharedNetworks(); initTestOptionDefs(); initTestClientClasses(); - initTimestamps(); } void @@ -76,6 +75,13 @@ GenericConfigBackendDHCPv4Test::TearDown() { destroySchema(); } +db::AuditEntryCollection +GenericConfigBackendDHCPv4Test::getRecentAuditEntries(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time, + const uint64_t& modification_id) const { + return (cbptr_->getRecentAuditEntries(server_selector, modification_time, modification_id)); +} + void GenericConfigBackendDHCPv4Test::initTestServers() { test_servers_.push_back(Server::create(ServerTag("server1"), "this is server 1")); @@ -390,47 +396,6 @@ GenericConfigBackendDHCPv4Test::initTestClientClasses() { test_client_classes_.push_back(class3); } -void -GenericConfigBackendDHCPv4Test::initTimestamps() { - // Current time minus 1 hour to make sure it is in the past. - timestamps_["today"] = boost::posix_time::second_clock::local_time() - - boost::posix_time::hours(1); - // One second after today. - timestamps_["after today"] = timestamps_["today"] + boost::posix_time::seconds(1); - // Yesterday. - timestamps_["yesterday"] = timestamps_["today"] - boost::posix_time::hours(24); - // One second after yesterday. - timestamps_["after yesterday"] = timestamps_["yesterday"] + boost::posix_time::seconds(1); - // Two days ago. - timestamps_["two days ago"] = timestamps_["today"] - boost::posix_time::hours(48); - // Tomorrow. - timestamps_["tomorrow"] = timestamps_["today"] + boost::posix_time::hours(24); - // One second after tomorrow. - timestamps_["after tomorrow"] = timestamps_["tomorrow"] + boost::posix_time::seconds(1); -} - -std::string -GenericConfigBackendDHCPv4Test::logExistingAuditEntries(const std::string& server_tag) { - std::ostringstream s; - - auto& mod_time_idx = audit_entries_[server_tag].get(); - - for (auto audit_entry_it = mod_time_idx.begin(); - audit_entry_it != mod_time_idx.end(); - ++audit_entry_it) { - auto audit_entry = *audit_entry_it; - s << audit_entry->getObjectType() << ", " - << audit_entry->getObjectId() << ", " - << static_cast(audit_entry->getModificationType()) << ", " - << audit_entry->getModificationTime() << ", " - << audit_entry->getRevisionId() << ", " - << audit_entry->getLogMessage() - << std::endl; - } - - return (s.str()); -} - void GenericConfigBackendDHCPv4Test::getTypeTest(const std::string& expected_type) { DatabaseConnection::ParameterMap params; @@ -464,111 +429,6 @@ GenericConfigBackendDHCPv4Test::getPortTest() { EXPECT_EQ(0, cbptr_->getPort()); } -void -GenericConfigBackendDHCPv4Test::testNewAuditEntry(const std::string& exp_object_type, - const AuditEntry::ModificationType& - exp_modification_type, - const std::string& exp_log_message, - const ServerSelector& server_selector, - const size_t new_entries_num, - const size_t max_tested_entries) { - // Get the server tag for which the entries are fetched. - std::string tag; - if (server_selector.getType() == ServerSelector::Type::ALL) { - // Server tag is 'all'. - tag = "all"; - } else { - const auto& tags = server_selector.getTags(); - // This test is not meant to handle multiple server tags all at once. - if (tags.size() > 1) { - ADD_FAILURE() << "Test error: do not use multiple server tags"; - } else if (tags.size() == 1) { - // Get the server tag for which we run the current test. - tag = tags.begin()->get(); - } - } - - auto audit_entries_size_save = audit_entries_[tag].size(); - - // Audit entries for different server tags are stored in separate - // containers. - ASSERT_NO_THROW_LOG(audit_entries_[tag] - = cbptr_->getRecentAuditEntries(server_selector, - timestamps_["two days ago"], 0)); - ASSERT_EQ(audit_entries_size_save + new_entries_num, audit_entries_[tag].size()) - << logExistingAuditEntries(tag); - - auto& mod_time_idx = audit_entries_[tag].get(); - - // Iterate over specified number of entries starting from the most recent - // one and check they have correct values. - for (auto audit_entry_it = mod_time_idx.rbegin(); - ((std::distance(mod_time_idx.rbegin(), audit_entry_it) < new_entries_num) && - (std::distance(mod_time_idx.rbegin(), audit_entry_it) < max_tested_entries)); - ++audit_entry_it) { - auto audit_entry = *audit_entry_it; - EXPECT_EQ(exp_object_type, audit_entry->getObjectType()) - << logExistingAuditEntries(tag); - EXPECT_EQ(exp_modification_type, audit_entry->getModificationType()) - << logExistingAuditEntries(tag); - EXPECT_EQ(exp_log_message, audit_entry->getLogMessage()) - << logExistingAuditEntries(tag); - } -} - -void -GenericConfigBackendDHCPv4Test::testNewAuditEntry(const std::vector& exp_entries, - const ServerSelector& server_selector) { - // Get the server tag for which the entries are fetched. - std::string tag; - if (server_selector.getType() == ServerSelector::Type::ALL) { - // Server tag is 'all'. - tag = "all"; - } else { - const auto& tags = server_selector.getTags(); - // This test is not meant to handle multiple server tags all at once. - if (tags.size() != 1) { - ADD_FAILURE() << "Test error: tags.size(): " << tags.size() - << ", you must specify one and only one server tag"; - } - - // Get the server tag for which we run the current test. - tag = tags.begin()->get(); - } - - size_t new_entries_num = exp_entries.size(); - - auto audit_entries_size_save = audit_entries_[tag].size(); - - // Audit entries for different server tags are stored in separate - // containers. - ASSERT_NO_THROW_LOG(audit_entries_[tag] - = cbptr_->getRecentAuditEntries(server_selector, - timestamps_["two days ago"], 0)); - ASSERT_EQ(audit_entries_size_save + new_entries_num, audit_entries_[tag].size()) - << logExistingAuditEntries(tag); - - auto& mod_time_idx = audit_entries_[tag].get(); - - // Iterate over specified number of entries starting from the most recent - // one and check they have correct values. - auto exp_entry = exp_entries.rbegin(); - for (auto audit_entry_it = mod_time_idx.rbegin(); - ((std::distance(mod_time_idx.rbegin(), audit_entry_it) < new_entries_num)); - ++audit_entry_it) { - - auto audit_entry = *audit_entry_it; - EXPECT_EQ((*exp_entry).object_type, audit_entry->getObjectType()) - << logExistingAuditEntries(tag); - EXPECT_EQ((*exp_entry).modification_type, audit_entry->getModificationType()) - << logExistingAuditEntries(tag); - EXPECT_EQ((*exp_entry).log_message, audit_entry->getLogMessage()) - << logExistingAuditEntries(tag); - - ++exp_entry; - } -} - void GenericConfigBackendDHCPv4Test::createUpdateDeleteServerTest() { // Explicitly set modification time to make sure that the time diff --git a/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.h b/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.h index b954eb8181..a2fa5dcea3 100644 --- a/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.h +++ b/src/lib/dhcpsrv/testutils/generic_cb_dhcp4_unittest.h @@ -15,18 +15,6 @@ namespace isc { namespace dhcp { namespace test { -/// @brief Describes an expected audit table entry. -struct ExpAuditEntry { - /// @brief Type of object changed. - std::string object_type; - - /// @brief Timestamp the change occurred. - db::AuditEntry::ModificationType modification_type; - - /// @brief Log message describing the change. - std::string log_message; -}; - /// @brief Generic test fixture class for testing DHCPv4 /// config backend operations. class GenericConfigBackendDHCPv4Test : public GenericBackendTest { @@ -34,8 +22,7 @@ public: /// @brief Constructor. GenericConfigBackendDHCPv4Test() : test_subnets_(), test_networks_(), test_option_defs_(), - test_options_(), test_client_classes_(), test_servers_(), timestamps_(), - cbptr_(), audit_entries_() { + test_options_(), test_client_classes_(), test_servers_(), cbptr_() { } /// @brief Destructor. @@ -102,16 +89,6 @@ public: /// @brief Creates several client classes used in tests. void initTestClientClasses(); - /// @brief Initialize posix time values used in tests. - void initTimestamps(); - - /// @brief Logs audit entries in the @c audit_entries_ member. - /// - /// This function is called in case of an error. - /// - /// @param server_tag Server tag for which the audit entries should be logged. - std::string logExistingAuditEntries(const std::string& server_tag); - /// @brief Tests that a backend of the given type can be instantiated. /// /// @param expected_type type of the back end created (i.e. "mysql", @@ -124,37 +101,22 @@ public: /// @brief Verifies that a backend on the localhost port 0 can be instantiated. void getPortTest(); - /// @brief Tests that the new audit entry is added. + /// @brief Retrieves the most recent audit entries. /// - /// This method retrieves a collection of the existing audit entries and - /// checks that the new one has been added at the end of this collection. - /// It then verifies the values of the audit entry against the values - /// specified by the caller. + /// Allowed server selectors: ALL, ONE. + /// Not allowed server selectors: ANY, UNASSIGNED, MULTIPLE. /// - /// @param exp_object_type Expected object type. - /// @param exp_modification_type Expected modification type. - /// @param exp_log_message Expected log message. - /// @param server_selector Server selector to be used for next query. - /// @param new_entries_num Number of the new entries expected to be inserted. - /// @param max_tested_entries Maximum number of entries tested. - void testNewAuditEntry(const std::string& exp_object_type, - const db::AuditEntry::ModificationType& exp_modification_type, - const std::string& exp_log_message, - const db::ServerSelector& server_selector = db::ServerSelector::ALL(), - const size_t new_entries_num = 1, - const size_t max_tested_entries = 65535); - - /// @brief Checks the new audit entries against a list of - /// expected entries. - /// - /// This method retrieves a collection of the existing audit entries and - /// checks that number and content of the expected new entries have been - /// added to the end of this collection. - /// - /// @param exp_entries a list of the expected new audit entries. - /// @param server_selector Server selector to be used for next query. - void testNewAuditEntry(const std::vector& exp_entries, - const db::ServerSelector& server_selector); + /// @param server_selector Server selector. + /// @param modification_time Timestamp being a lower limit for the returned + /// result set, i.e. entries later than specified time are returned. + /// @param modification_id Identifier being a lower limit for the returned + /// result set, used when two (or more) entries have the same + /// modification_time. + /// @return Collection of audit entries. + virtual db::AuditEntryCollection + getRecentAuditEntries(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time, + const uint64_t& modification_id) const; /// @brief This test verifies that the server can be added, updated and deleted. void createUpdateDeleteServerTest(); @@ -417,14 +379,8 @@ public: /// @brief Holds pointers to the servers used in tests. std::vector test_servers_; - /// @brief Holds timestamp values used in tests. - std::map timestamps_; - /// @brief Holds pointer to the backend. boost::shared_ptr cbptr_; - - /// @brief Holds the most recent audit entries. - std::map audit_entries_; }; } // namespace test diff --git a/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.cc b/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.cc index a2cf6afd33..b302e2b504 100644 --- a/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.cc +++ b/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.cc @@ -66,7 +66,6 @@ GenericConfigBackendDHCPv6Test::SetUp() { initTestSharedNetworks(); initTestOptionDefs(); initTestClientClasses(); - initTimestamps(); } void @@ -76,6 +75,13 @@ GenericConfigBackendDHCPv6Test::TearDown() { destroySchema(); } +db::AuditEntryCollection +GenericConfigBackendDHCPv6Test::getRecentAuditEntries(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time, + const uint64_t& modification_id) const { + return (cbptr_->getRecentAuditEntries(server_selector, modification_time, modification_id)); +} + void GenericConfigBackendDHCPv6Test::initTestServers() { test_servers_.push_back(Server::create(ServerTag("server1"), "this is server 1")); @@ -422,47 +428,6 @@ GenericConfigBackendDHCPv6Test::initTestClientClasses() { test_client_classes_.push_back(class3); } -void -GenericConfigBackendDHCPv6Test::initTimestamps() { - // Current time minus 1 hour to make sure it is in the past. - timestamps_["today"] = boost::posix_time::second_clock::local_time() - - boost::posix_time::hours(1); - // One second after today. - timestamps_["after today"] = timestamps_["today"] + boost::posix_time::seconds(1); - // Yesterday. - timestamps_["yesterday"] = timestamps_["today"] - boost::posix_time::hours(24); - // One second after yesterday. - timestamps_["after yesterday"] = timestamps_["yesterday"] + boost::posix_time::seconds(1); - // Two days ago. - timestamps_["two days ago"] = timestamps_["today"] - boost::posix_time::hours(48); - // Tomorrow. - timestamps_["tomorrow"] = timestamps_["today"] + boost::posix_time::hours(24); - // One second after tomorrow. - timestamps_["after tomorrow"] = timestamps_["tomorrow"] + boost::posix_time::seconds(1); -} - -std::string -GenericConfigBackendDHCPv6Test::logExistingAuditEntries(const std::string& server_tag) { - std::ostringstream s; - - auto& mod_time_idx = audit_entries_[server_tag].get(); - - for (auto audit_entry_it = mod_time_idx.begin(); - audit_entry_it != mod_time_idx.end(); - ++audit_entry_it) { - auto audit_entry = *audit_entry_it; - s << audit_entry->getObjectType() << ", " - << audit_entry->getObjectId() << ", " - << static_cast(audit_entry->getModificationType()) << ", " - << audit_entry->getModificationTime() << ", " - << audit_entry->getRevisionId() << ", " - << audit_entry->getLogMessage() - << std::endl; - } - - return (s.str()); -} - void GenericConfigBackendDHCPv6Test::getTypeTest(const std::string& expected_type) { DatabaseConnection::ParameterMap params; @@ -496,58 +461,6 @@ GenericConfigBackendDHCPv6Test::getPortTest() { EXPECT_EQ(0, cbptr_->getPort()); } -void -GenericConfigBackendDHCPv6Test::testNewAuditEntry(const std::string& exp_object_type, - const AuditEntry::ModificationType& - exp_modification_type, - const std::string& exp_log_message, - const ServerSelector& server_selector, - const size_t new_entries_num, - const size_t max_tested_entries) { - // Get the server tag for which the entries are fetched. - std::string tag; - if (server_selector.getType() == ServerSelector::Type::ALL) { - // Server tag is 'all'. - tag = "all"; - } else { - const auto& tags = server_selector.getTags(); - // This test is not meant to handle multiple server tags all at once. - if (tags.size() > 1) { - ADD_FAILURE() << "Test error: do not use multiple server tags"; - } else if (tags.size() == 1) { - // Get the server tag for which we run the current test. - tag = tags.begin()->get(); - } - } - - auto audit_entries_size_save = audit_entries_[tag].size(); - - // Audit entries for different server tags are stored in separate - // containers. - ASSERT_NO_THROW_LOG(audit_entries_[tag] - = cbptr_->getRecentAuditEntries(server_selector, - timestamps_["two days ago"], 0)); - ASSERT_EQ(audit_entries_size_save + new_entries_num, audit_entries_[tag].size()) - << logExistingAuditEntries(tag); - - auto& mod_time_idx = audit_entries_[tag].get(); - - // Iterate over specified number of entries starting from the most recent - // one and check they have correct values. - for (auto audit_entry_it = mod_time_idx.rbegin(); - ((std::distance(mod_time_idx.rbegin(), audit_entry_it) < new_entries_num) && - (std::distance(mod_time_idx.rbegin(), audit_entry_it) < max_tested_entries)); - ++audit_entry_it) { - auto audit_entry = *audit_entry_it; - EXPECT_EQ(exp_object_type, audit_entry->getObjectType()) - << logExistingAuditEntries(tag); - EXPECT_EQ(exp_modification_type, audit_entry->getModificationType()) - << logExistingAuditEntries(tag); - EXPECT_EQ(exp_log_message, audit_entry->getLogMessage()) - << logExistingAuditEntries(tag); - } -} - void GenericConfigBackendDHCPv6Test::createUpdateDeleteServerTest() { // Explicitly set modification time to make sure that the time diff --git a/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.h b/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.h index 4e90d19ed9..61e85647c1 100644 --- a/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.h +++ b/src/lib/dhcpsrv/testutils/generic_cb_dhcp6_unittest.h @@ -22,8 +22,7 @@ public: /// @brief Constructor. GenericConfigBackendDHCPv6Test() : test_subnets_(), test_networks_(), test_option_defs_(), - test_options_(), test_client_classes_(), test_servers_(), timestamps_(), - cbptr_(), audit_entries_() { + test_options_(), test_client_classes_(), test_servers_(), cbptr_() { } /// @brief Destructor. @@ -90,16 +89,6 @@ public: /// @brief Creates several client classes used in tests. void initTestClientClasses(); - /// @brief Initialize posix time values used in tests. - void initTimestamps(); - - /// @brief Logs audit entries in the @c audit_entries_ member. - /// - /// This function is called in case of an error. - /// - /// @param server_tag Server tag for which the audit entries should be logged. - std::string logExistingAuditEntries(const std::string& server_tag); - /// @brief Tests that a backend of the given type can be instantiated. /// /// @param expected_type type of the back end created (i.e. "mysql", @@ -112,27 +101,22 @@ public: /// @brief Verifies that a backend on the localhost port 0 can be instantiated. void getPortTest(); - /// @brief Tests that the new audit entry is added. + /// @brief Retrieves the most recent audit entries. /// - /// This method retrieves a collection of the existing audit entries and - /// checks that the new one has been added at the end of this collection. - /// It then verifies the values of the audit entry against the values - /// specified by the caller. + /// Allowed server selectors: ALL, ONE. + /// Not allowed server selectors: ANY, UNASSIGNED, MULTIPLE. /// - /// @param exp_object_type Expected object type. - /// @param exp_modification_type Expected modification type. - /// @param exp_log_message Expected log message. - /// @param server_selector Server selector to be used for next query. - /// @param new_entries_num Number of the new entries expected to be inserted. - /// @param max_tested_entries Maximum number of entries tested. - void testNewAuditEntry(const std::string& exp_object_type, - const db::AuditEntry::ModificationType& exp_modification_type, - const std::string& exp_log_message, - const db::ServerSelector& server_selector = db::ServerSelector::ALL(), - const size_t new_entries_num = 1, - const size_t max_tested_entries = 65535); - - + /// @param server_selector Server selector. + /// @param modification_time Timestamp being a lower limit for the returned + /// result set, i.e. entries later than specified time are returned. + /// @param modification_id Identifier being a lower limit for the returned + /// result set, used when two (or more) entries have the same + /// modification_time. + /// @return Collection of audit entries. + virtual db::AuditEntryCollection + getRecentAuditEntries(const db::ServerSelector& server_selector, + const boost::posix_time::ptime& modification_time, + const uint64_t& modification_id) const; /// @brief This test verifies that the server can be added, updated and deleted. void createUpdateDeleteServerTest(); @@ -399,14 +383,8 @@ public: /// @brief Holds pointers to the servers used in tests. std::vector test_servers_; - /// @brief Holds timestamp values used in tests. - std::map timestamps_; - /// @brief Holds pointer to the backend. boost::shared_ptr cbptr_; - - /// @brief Holds the most recent audit entries. - std::map audit_entries_; }; } // namespace test