]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1375] fixed unittests
authorRazvan Becheriu <razvan@isc.org>
Fri, 6 Nov 2020 18:41:22 +0000 (20:41 +0200)
committerRazvan Becheriu <razvan@isc.org>
Wed, 9 Dec 2020 17:12:46 +0000 (19:12 +0200)
src/lib/dhcpsrv/mysql_host_data_source.cc
src/lib/dhcpsrv/pgsql_host_data_source.cc
src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.cc
src/lib/dhcpsrv/tests/generic_lease_mgr_unittest.h
src/lib/dhcpsrv/tests/host_mgr_unittest.cc
src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc
src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc

index a1dd6a3053ba5b131a3e10a9ae0bb1b101794c2a..3ef19cebb7e54c7d6313d7d66f93422d005f35ed 100644 (file)
@@ -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<std::string>(reinterpret_cast<uint64_t>(this));
     std::string timer_name = manager + "]DbReconnectTimer";
 
index 2d941acbb808d2cff1435022c4fa0844487f5a50..3277548df7355b16d7f4d110efc0b6e1c4d112a0 100644 (file)
@@ -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<std::string>(reinterpret_cast<uint64_t>(this));
     std::string timer_name = manager + "]DbReconnectTimer";
 
index 6fca3a4f1f449985881f09ebac860fdc2639ca90..7968330f6ad896614fe033ea7555da54c41882a0 100644 (file)
@@ -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_);
 }
index 75fe229a4528e32c3061e1c3f3906b0689463f86..5f2987278820e974af0a0eb160ae85fceef7b514 100644 (file)
@@ -7,8 +7,12 @@
 #ifndef GENERIC_LEASE_MGR_UNITTEST_H
 #define GENERIC_LEASE_MGR_UNITTEST_H
 
+#include <asiolink/io_service.h>
 #include <dhcpsrv/lease_mgr.h>
+
 #include <gtest/gtest.h>
+
+#include <boost/make_shared.hpp>
 #include <vector>
 #include <set>
 
@@ -521,12 +525,16 @@ public:
 
 class LeaseMgrDbLostCallbackTest : public ::testing::Test {
 public:
-    LeaseMgrDbLostCallbackTest() {
+    LeaseMgrDbLostCallbackTest()
+        : callback_called_(false),
+          io_service_(boost::make_shared<isc::asiolink::IOService>()) {
         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
index 973c08358e1fc1545fdc62611c92ff35f3a8435a..227814cfb79d2ae1ed24c8b23d96c26ec75fcf80 100644 (file)
@@ -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<isc::asiolink::IOService>()) {
+    }
 
     /// @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_);
 }
index a040b30533c4f8b01db26187aee692750ef33f9e..c28417e708b944aa6821adb83f05659401db0153 100644 (file)
@@ -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
 ///
index 32a51f6e59d769b4ae4b4c1479cded44532e895c..c7e5546831bc756f672a4bcee409133b4a073482 100644 (file)
@@ -17,8 +17,8 @@
 #include <dhcpsrv/host_data_source_factory.h>
 #include <pgsql/pgsql_connection.h>
 #include <pgsql/testutils/pgsql_schema.h>
-#include <util/multi_threading_mgr.h>
 #include <testutils/multi_threading_utils.h>
+#include <util/multi_threading_mgr.h>
 
 #include <gtest/gtest.h>
 
@@ -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(