]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3419] Added a DB test mode
authorFrancis Dupont <fdupont@isc.org>
Fri, 6 Sep 2024 21:52:27 +0000 (23:52 +0200)
committerFrancis Dupont <fdupont@isc.org>
Mon, 16 Sep 2024 10:05:12 +0000 (12:05 +0200)
src/lib/database/database_connection.cc
src/lib/database/database_connection.h
src/lib/database/tests/database_connection_unittest.cc
src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc
src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc
src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc
src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc
src/lib/mysql/mysql_connection.cc
src/lib/pgsql/pgsql_connection.cc

index 607c706376236b4547e2b626634370f26fd707c3..29f99f6ec1028bd7c46208f8947efc353de6630d 100644 (file)
@@ -283,5 +283,7 @@ DbCallback DatabaseConnection::db_failed_callback_ = 0;
 bool DatabaseConnection::retry_ = false;
 IOServicePtr DatabaseConnection::io_service_ = IOServicePtr();
 
+bool DatabaseConnection::test_mode_ = false;
+
 }  // namespace db
 }  // namespace isc
index 4729ade919ab37c2216ead41e57aad65b8f45db2..00dbc0342410a43205d1d127d4bcb061a6a066c5 100644 (file)
@@ -277,6 +277,26 @@ public:
         return (unusable_);
     }
 
+    /// @brief Test mode flag (default false).
+    static bool test_mode_;
+
+    /// @brief RAII device to set the test mode.
+    class EnterTest {
+    public:
+
+        /// @brief Constructor.
+        /// Set the test mode to true.
+        EnterTest() {
+            DatabaseConnection::test_mode_ = true;
+        }
+
+        /// @brief Destructor.
+        /// Reset the test mode to false.
+        ~EnterTest() {
+            DatabaseConnection::test_mode_ = false;
+        }
+    };
+
 protected:
 
     /// @brief Sets the unusable flag to true.
index 53a71c1a7a0443ed11de257a9eded21f3b4af276..a0ee38ae0254396b7e9e3382ab618c2927c6495b 100644 (file)
@@ -641,3 +641,13 @@ TEST(DatabaseConnection, toElementDbAccessStringEmpty) {
     ASSERT_TRUE(elements);
     ASSERT_EQ(0, elements->size());
 }
+
+// Check that the test mode works as expected.
+TEST(DatabaseConnection, testMode) {
+    ASSERT_FALSE(DatabaseConnection::test_mode_);
+    {
+        DatabaseConnection::EnterTest et;
+        EXPECT_TRUE(DatabaseConnection::test_mode_);
+    }
+    EXPECT_FALSE(DatabaseConnection::test_mode_);
+}
index 01a3266ee165a0d5bcb1880cb01510673e6c32f0..50d519668a39dc51cd04f53e3943ee6849aa1815 100644 (file)
@@ -166,6 +166,9 @@ TEST(MySqlHostDataSource, OpenDatabase) {
     destroyMySQLSchema();
     createMySQLSchema();
 
+    // Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
+    DatabaseConnection::EnterTest et;
+
     // Check that host manager opens the database correctly and tidy up.  If it
     // fails, print the error message.
     try {
@@ -254,6 +257,9 @@ TEST(MySqlHostDataSource, OpenDatabaseMultiThreading) {
     destroyMySQLSchema();
     createMySQLSchema();
 
+    // Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
+    DatabaseConnection::EnterTest et;
+
     // Check that host manager opens the database correctly and tidy up.  If it
     // fails, print the error message.
     try {
index 7b910f08ec5c3666f255f39c83c21be4944a7b9e..ac2162e0cad638a84cb13233e65d4747a74ec5b7 100644 (file)
@@ -121,6 +121,9 @@ TEST(MySqlOpenTest, OpenDatabase) {
     // Schema needs to be created for the test to work.
     createMySQLSchema(true);
 
+    // Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
+    DatabaseConnection::EnterTest et;
+
     // Check that lease manager opens the database correctly and tidy up.  If it
     // fails, print the error message.
     try {
index df2fb093d5456075505c3f17acdba5b6599c4edb..f5cea85be788e8074af44cc2bdb5aeb190cbf7a9 100644 (file)
@@ -164,6 +164,9 @@ TEST(PgSqlHostDataSource, OpenDatabase) {
     destroyPgSQLSchema();
     createPgSQLSchema();
 
+    // Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
+    DatabaseConnection::EnterTest et;
+
     // Check that host manager opens the database correctly and tidy up.  If it
     // fails, print the error message.
     try {
@@ -263,6 +266,9 @@ TEST(PgSqlHostDataSource, OpenDatabaseMultiThreading) {
     destroyPgSQLSchema();
     createPgSQLSchema();
 
+    // Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
+    DatabaseConnection::EnterTest et;
+
     // Check that host manager opens the database correctly and tidy up.  If it
     // fails, print the error message.
     try {
index a68ace963831c3ba3aba30d4663b4373d0bb953a..cdb5c27df87d567cfcb1d9b326831bfbe66b0ef9 100644 (file)
@@ -121,6 +121,9 @@ TEST(PgSqlOpenTest, OpenDatabase) {
     // Schema needs to be created for the test to work.
     createPgSQLSchema();
 
+    // Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
+    DatabaseConnection::EnterTest et;
+
     // Check that lease manager opens the database correctly and tidy up.  If it
     // fails, print the error message.
     try {
index 84523d8b796e58b6319a9535ea4b47a36417d978..97a303029b59d5863a93e48c0efd9e426fcf90e8 100644 (file)
@@ -415,6 +415,10 @@ MySqlConnection::ensureSchemaVersion(const ParameterMap& parameters,
     } catch (DbOpenErrorWithRetry const& exception) {
         throw;
     } catch (exception const& exception) {
+        // Disable the recovery mechanism in test mode.
+        if (DatabaseConnection::test_mode_) {
+            throw;
+        }
         // This failure may occur for a variety of reasons. We are looking at
         // initializing schema as the only potential mitigation. We could narrow
         // down on the error that would suggest an uninitialized schema
index e63a699a1a42caf0bbc641a67cf2329f490c0550..f64d4dcb68d956c74bc7f96408d80c4583bef06c 100644 (file)
@@ -191,6 +191,10 @@ PgSqlConnection::ensureSchemaVersion(const ParameterMap& parameters,
     } catch (DbOpenErrorWithRetry const& exception) {
         throw;
     } catch (exception const& exception) {
+        // Disable the recovery mechanism in test mode.
+        if (DatabaseConnection::test_mode_) {
+            throw;
+        }
         // This failure may occur for a variety of reasons. We are looking at
         // initializing schema as the only potential mitigation. We could narrow
         // down on the error that would suggest an uninitialized schema