From: Francis Dupont Date: Fri, 6 Sep 2024 21:52:27 +0000 (+0200) Subject: [#3419] Added a DB test mode X-Git-Tag: Kea-2.7.3~38 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fe2bb913047560c59cdbe21050c708c953292a43;p=thirdparty%2Fkea.git [#3419] Added a DB test mode --- diff --git a/src/lib/database/database_connection.cc b/src/lib/database/database_connection.cc index 607c706376..29f99f6ec1 100644 --- a/src/lib/database/database_connection.cc +++ b/src/lib/database/database_connection.cc @@ -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 diff --git a/src/lib/database/database_connection.h b/src/lib/database/database_connection.h index 4729ade919..00dbc03424 100644 --- a/src/lib/database/database_connection.h +++ b/src/lib/database/database_connection.h @@ -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. diff --git a/src/lib/database/tests/database_connection_unittest.cc b/src/lib/database/tests/database_connection_unittest.cc index 53a71c1a7a..a0ee38ae02 100644 --- a/src/lib/database/tests/database_connection_unittest.cc +++ b/src/lib/database/tests/database_connection_unittest.cc @@ -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_); +} 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 01a3266ee1..50d519668a 100644 --- a/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc @@ -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 { diff --git a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc index 7b910f08ec..ac2162e0ca 100644 --- a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc @@ -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 { 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 df2fb093d5..f5cea85be7 100644 --- a/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc @@ -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 { diff --git a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc index a68ace9638..cdb5c27df8 100644 --- a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc @@ -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 { diff --git a/src/lib/mysql/mysql_connection.cc b/src/lib/mysql/mysql_connection.cc index 84523d8b79..97a303029b 100644 --- a/src/lib/mysql/mysql_connection.cc +++ b/src/lib/mysql/mysql_connection.cc @@ -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 diff --git a/src/lib/pgsql/pgsql_connection.cc b/src/lib/pgsql/pgsql_connection.cc index e63a699a1a..f64d4dcb68 100644 --- a/src/lib/pgsql/pgsql_connection.cc +++ b/src/lib/pgsql/pgsql_connection.cc @@ -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