From: Razvan Becheriu Date: Fri, 6 Nov 2020 18:41:22 +0000 (+0200) Subject: [#1375] fixed unittests X-Git-Tag: Kea-1.9.3~92 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee896f0f56bf233e38873c063a915c5d7e18ee57;p=thirdparty%2Fkea.git [#1375] fixed unittests --- diff --git a/src/lib/dhcpsrv/mysql_host_data_source.cc b/src/lib/dhcpsrv/mysql_host_data_source.cc index a1dd6a3053..3ef19cebb7 100644 --- a/src/lib/dhcpsrv/mysql_host_data_source.cc +++ b/src/lib/dhcpsrv/mysql_host_data_source.cc @@ -2789,7 +2789,7 @@ MySqlHostDataSourceImpl::MySqlHostDataSourceImpl(const DatabaseConnection::Param auto db_reconnect_ctl = pool_->pool_[0]->conn_.reconnectCtl(); - std::string manager = "MySqlLeaseMgr["; + std::string manager = "MySqlHostMgr["; manager += boost::lexical_cast(reinterpret_cast(this)); std::string timer_name = manager + "]DbReconnectTimer"; diff --git a/src/lib/dhcpsrv/pgsql_host_data_source.cc b/src/lib/dhcpsrv/pgsql_host_data_source.cc index 2d941acbb8..3277548df7 100644 --- a/src/lib/dhcpsrv/pgsql_host_data_source.cc +++ b/src/lib/dhcpsrv/pgsql_host_data_source.cc @@ -2230,7 +2230,7 @@ PgSqlHostDataSourceImpl::PgSqlHostDataSourceImpl(const DatabaseConnection::Param auto db_reconnect_ctl = pool_->pool_[0]->conn_.reconnectCtl(); - std::string manager = "PgSqlLeaseMgr["; + std::string manager = "PgSqlHostMgr["; manager += boost::lexical_cast(reinterpret_cast(this)); std::string timer_name = manager + "]DbReconnectTimer"; diff --git a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc index 6fca3a4f1f..7968330f6a 100644 --- a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc @@ -3278,6 +3278,8 @@ LeaseMgrDbLostCallbackTest::testNoCallbackOnOpenFailure() { ASSERT_THROW(LeaseMgrFactory::create(invalidConnectString()), DbOpenError); + io_service_->run_one(); + EXPECT_FALSE(callback_called_); } @@ -3318,6 +3320,8 @@ LeaseMgrDbLostCallbackTest::testDbLostCallback() { ASSERT_THROW(lease = lm.getLease4(IOAddress("192.0.1.0")), DbConnectionUnusable); + io_service_->run_one(); + // Our lost connectivity callback should have been invoked. EXPECT_TRUE(callback_called_); } diff --git a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h index 75fe229a45..5f29872788 100644 --- a/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h +++ b/src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h @@ -7,8 +7,12 @@ #ifndef GENERIC_LEASE_MGR_UNITTEST_H #define GENERIC_LEASE_MGR_UNITTEST_H +#include #include + #include + +#include #include #include @@ -521,12 +525,16 @@ public: class LeaseMgrDbLostCallbackTest : public ::testing::Test { public: - LeaseMgrDbLostCallbackTest() { + LeaseMgrDbLostCallbackTest() + : callback_called_(false), + io_service_(boost::make_shared()) { db::DatabaseConnection::db_lost_callback_ = 0; + LeaseMgr::setIOService(io_service_); } virtual ~LeaseMgrDbLostCallbackTest() { db::DatabaseConnection::db_lost_callback_ = 0; + LeaseMgr::setIOService(isc::asiolink::IOServicePtr()); } /// @brief Prepares the class for a test. @@ -582,6 +590,8 @@ public: /// @brief Flag used to detect calls to db_lost_callback function bool callback_called_; + /// The IOService object, used for all ASIO operations. + isc::asiolink::IOServicePtr io_service_; }; } // namespace test diff --git a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc index 973c08358e..227814cfb7 100644 --- a/src/lib/dhcpsrv/tests/host_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_mgr_unittest.cc @@ -1375,7 +1375,10 @@ TEST_F(HostMgrTest, addNoDataSource) { class HostMgrDbLostCallbackTest : public ::testing::Test { public: - HostMgrDbLostCallbackTest() : callback_called_(false) {}; + HostMgrDbLostCallbackTest() + : callback_called_(false), + io_service_(boost::make_shared()) { + } /// @brief Prepares the class for a test. /// @@ -1383,6 +1386,7 @@ public: /// appropriate schema and create a basic host manager to /// wipe out any prior instance virtual void SetUp() { + HostMgr::setIOService(io_service_); DatabaseConnection::db_lost_callback_ = 0; // Ensure we have the proper schema with no transient data. createSchema(); @@ -1395,6 +1399,7 @@ public: /// Invoked by gtest upon test exit, we destroy the schema /// we created. virtual void TearDown() { + HostMgr::setIOService(isc::asiolink::IOServicePtr()); DatabaseConnection::db_lost_callback_ = 0; // If data wipe enabled, delete transient data otherwise destroy the schema destroySchema(); @@ -1431,6 +1436,9 @@ public: /// @brief Flag used to detect calls to db_lost_callback function bool callback_called_; + + /// The IOService object, used for all ASIO operations. + isc::asiolink::IOServicePtr io_service_; }; #if defined(HAVE_MYSQL) || defined(HAVE_PGSQL) @@ -1471,6 +1479,8 @@ HostMgrDbLostCallbackTest::testDbLostCallback() { ASSERT_THROW(hosts = HostMgr::instance().getAll4(IOAddress("192.0.2.5")), DbConnectionUnusable); + io_service_->run_one(); + // Our lost connectivity callback should have been invoked. EXPECT_TRUE(callback_called_); } diff --git a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc index a040b30533..c28417e708 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -328,6 +328,64 @@ TEST(MySqlHostDataSource, OpenDatabaseMultiThreading) { destroyMySQLSchema(); } +/// @brief Flag used to detect calls to db_lost_callback function +bool callback_called = false; + +/// @brief Callback function used in open database testing +bool db_lost_callback(ReconnectCtlPtr /* db_conn_retry */) { + return (callback_called = true); +} + +/// @brief Make sure open failures do NOT invoke db lost callback +/// The db lost callback should only be invoked after successfully +/// opening the DB and then subsequently losing it. Failing to +/// open should be handled directly by the application layer. +/// There is simply no good way to break the connection in a +/// unit test environment. So testing the callback invocation +/// in a unit test is next to impossible. That has to be done +/// as a system test. +TEST(MySqlHostDataSource, NoCallbackOnOpenFail) { + // Schema needs to be created for the test to work. + destroyMySQLSchema(); + createMySQLSchema(); + + callback_called = false; + DatabaseConnection::db_lost_callback_ = db_lost_callback; + HostMgr::create(); + EXPECT_THROW(HostMgr::addBackend(connectionString( + MYSQL_VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)), + DbOpenError); + + EXPECT_FALSE(callback_called); + destroyMySQLSchema(); +} + +/// @brief Make sure open failures do NOT invoke db lost callback +/// The db lost callback should only be invoked after successfully +/// opening the DB and then subsequently losing it. Failing to +/// open should be handled directly by the application layer. +/// There is simply no good way to break the connection in a +/// unit test environment. So testing the callback invocation +/// in a unit test is next to impossible. That has to be done +/// as a system test. +TEST(MySqlHostDataSource, NoCallbackOnOpenFailMultiThreading) { + // Enable Multi-Threading. + MultiThreadingTest mt(true); + + // Schema needs to be created for the test to work. + destroyMySQLSchema(); + createMySQLSchema(); + + callback_called = false; + DatabaseConnection::db_lost_callback_ = db_lost_callback; + HostMgr::create(); + EXPECT_THROW(HostMgr::addBackend(connectionString( + MYSQL_VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)), + DbOpenError); + + EXPECT_FALSE(callback_called); + destroyMySQLSchema(); +} /// @brief Check conversion functions /// diff --git a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc index 32a51f6e59..c7e5546831 100644 --- a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc @@ -17,8 +17,8 @@ #include #include #include -#include #include +#include #include @@ -225,6 +225,9 @@ TEST(PgSqlHostDataSource, OpenDatabase) { EXPECT_THROW(HostMgr::addBackend(connectionString( PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_2)), DbInvalidTimeout); + EXPECT_THROW(HostMgr::addBackend(connectionString( + PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, + VALID_TIMEOUT, INVALID_READONLY_DB)), DbInvalidReadOnly); // Check for missing parameters EXPECT_THROW(HostMgr::addBackend(connectionString( @@ -310,6 +313,9 @@ TEST(PgSqlHostDataSource, OpenDatabaseMultiThreading) { EXPECT_THROW(HostMgr::addBackend(connectionString( PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, INVALID_TIMEOUT_2)), DbInvalidTimeout); + EXPECT_THROW(HostMgr::addBackend(connectionString( + PGSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD, + VALID_TIMEOUT, INVALID_READONLY_DB)), DbInvalidReadOnly); // Check for missing parameters EXPECT_THROW(HostMgr::addBackend(connectionString(