Skip to content

Commit fe2bb91

Browse files
committed
[#3419] Added a DB test mode
1 parent 0fbb3ac commit fe2bb91

File tree

9 files changed

+58
-0
lines changed

9 files changed

+58
-0
lines changed

src/lib/database/database_connection.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,7 @@ DbCallback DatabaseConnection::db_failed_callback_ = 0;
283283
bool DatabaseConnection::retry_ = false;
284284
IOServicePtr DatabaseConnection::io_service_ = IOServicePtr();
285285

286+
bool DatabaseConnection::test_mode_ = false;
287+
286288
} // namespace db
287289
} // namespace isc

src/lib/database/database_connection.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,26 @@ class DatabaseConnection : public boost::noncopyable {
277277
return (unusable_);
278278
}
279279

280+
/// @brief Test mode flag (default false).
281+
static bool test_mode_;
282+
283+
/// @brief RAII device to set the test mode.
284+
class EnterTest {
285+
public:
286+
287+
/// @brief Constructor.
288+
/// Set the test mode to true.
289+
EnterTest() {
290+
DatabaseConnection::test_mode_ = true;
291+
}
292+
293+
/// @brief Destructor.
294+
/// Reset the test mode to false.
295+
~EnterTest() {
296+
DatabaseConnection::test_mode_ = false;
297+
}
298+
};
299+
280300
protected:
281301

282302
/// @brief Sets the unusable flag to true.

src/lib/database/tests/database_connection_unittest.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,13 @@ TEST(DatabaseConnection, toElementDbAccessStringEmpty) {
641641
ASSERT_TRUE(elements);
642642
ASSERT_EQ(0, elements->size());
643643
}
644+
645+
// Check that the test mode works as expected.
646+
TEST(DatabaseConnection, testMode) {
647+
ASSERT_FALSE(DatabaseConnection::test_mode_);
648+
{
649+
DatabaseConnection::EnterTest et;
650+
EXPECT_TRUE(DatabaseConnection::test_mode_);
651+
}
652+
EXPECT_FALSE(DatabaseConnection::test_mode_);
653+
}

src/lib/dhcpsrv/tests/mysql_host_data_source_unittest.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ TEST(MySqlHostDataSource, OpenDatabase) {
166166
destroyMySQLSchema();
167167
createMySQLSchema();
168168

169+
// Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
170+
DatabaseConnection::EnterTest et;
171+
169172
// Check that host manager opens the database correctly and tidy up. If it
170173
// fails, print the error message.
171174
try {
@@ -254,6 +257,9 @@ TEST(MySqlHostDataSource, OpenDatabaseMultiThreading) {
254257
destroyMySQLSchema();
255258
createMySQLSchema();
256259

260+
// Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
261+
DatabaseConnection::EnterTest et;
262+
257263
// Check that host manager opens the database correctly and tidy up. If it
258264
// fails, print the error message.
259265
try {

src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ TEST(MySqlOpenTest, OpenDatabase) {
121121
// Schema needs to be created for the test to work.
122122
createMySQLSchema(true);
123123

124+
// Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
125+
DatabaseConnection::EnterTest et;
126+
124127
// Check that lease manager opens the database correctly and tidy up. If it
125128
// fails, print the error message.
126129
try {

src/lib/dhcpsrv/tests/pgsql_host_data_source_unittest.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ TEST(PgSqlHostDataSource, OpenDatabase) {
164164
destroyPgSQLSchema();
165165
createPgSQLSchema();
166166

167+
// Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
168+
DatabaseConnection::EnterTest et;
169+
167170
// Check that host manager opens the database correctly and tidy up. If it
168171
// fails, print the error message.
169172
try {
@@ -263,6 +266,9 @@ TEST(PgSqlHostDataSource, OpenDatabaseMultiThreading) {
263266
destroyPgSQLSchema();
264267
createPgSQLSchema();
265268

269+
// Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
270+
DatabaseConnection::EnterTest et;
271+
266272
// Check that host manager opens the database correctly and tidy up. If it
267273
// fails, print the error message.
268274
try {

src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ TEST(PgSqlOpenTest, OpenDatabase) {
121121
// Schema needs to be created for the test to work.
122122
createPgSQLSchema();
123123

124+
// Enter test mode to avoid ensureSchemaVersion to invole kea-admin.
125+
DatabaseConnection::EnterTest et;
126+
124127
// Check that lease manager opens the database correctly and tidy up. If it
125128
// fails, print the error message.
126129
try {

src/lib/mysql/mysql_connection.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@ MySqlConnection::ensureSchemaVersion(const ParameterMap& parameters,
415415
} catch (DbOpenErrorWithRetry const& exception) {
416416
throw;
417417
} catch (exception const& exception) {
418+
// Disable the recovery mechanism in test mode.
419+
if (DatabaseConnection::test_mode_) {
420+
throw;
421+
}
418422
// This failure may occur for a variety of reasons. We are looking at
419423
// initializing schema as the only potential mitigation. We could narrow
420424
// down on the error that would suggest an uninitialized schema

src/lib/pgsql/pgsql_connection.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ PgSqlConnection::ensureSchemaVersion(const ParameterMap& parameters,
191191
} catch (DbOpenErrorWithRetry const& exception) {
192192
throw;
193193
} catch (exception const& exception) {
194+
// Disable the recovery mechanism in test mode.
195+
if (DatabaseConnection::test_mode_) {
196+
throw;
197+
}
194198
// This failure may occur for a variety of reasons. We are looking at
195199
// initializing schema as the only potential mitigation. We could narrow
196200
// down on the error that would suggest an uninitialized schema

0 commit comments

Comments
 (0)