]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[github21] soft-wipe implemented.
authorTomek Mrugalski <tomasz@isc.org>
Mon, 6 Jun 2016 18:41:52 +0000 (20:41 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Mon, 6 Jun 2016 18:41:52 +0000 (20:41 +0200)
src/lib/dhcpsrv/tests/dscsql_lease_mgr_unittest.cc
src/lib/dhcpsrv/testutils/dscsql_schema.cc
src/lib/dhcpsrv/testutils/dscsql_schema.h
src/share/database/scripts/dscsql/Makefile.am

index 48071575d0952665453ba1c7570a5cf22da1b9dd..e28d51a1a34812190aec836cc0a4f033939154d3 100644 (file)
@@ -45,8 +45,8 @@ public:
     DSCSqlLeaseMgrTest() {
 
         // Ensure schema is the correct one.
-        destroyDSCSQLSchema();
-        createDSCSQLSchema();
+        destroyDSCSQLSchema(false, true);
+        createDSCSQLSchema(false, true);
 
         // Connect to the database
         try {
@@ -69,7 +69,7 @@ public:
     virtual ~DSCSqlLeaseMgrTest() {
         lmptr_->rollback();
         LeaseMgrFactory::destroy();
-        destroyDSCSQLSchema();
+        destroyDSCSQLSchema(false, true);
     }
 
     /// @brief Reopen the database
@@ -97,8 +97,8 @@ public:
 TEST(DSCSqlOpenTest, OpenDatabase) {
 
     // Schema needs to be created for the test to work.
-    destroyDSCSQLSchema();
-    createDSCSQLSchema();
+    destroyDSCSQLSchema(true, true);
+    createDSCSQLSchema(true, true);
 
     // Check that lease manager open the database opens correctly and tidy up.
     //  If it fails, print the error message.
@@ -147,7 +147,7 @@ TEST(DSCSqlOpenTest, OpenDatabase) {
         NoDatabaseName);
 
     // Tidy up after the test
-    destroyDSCSQLSchema();
+    destroyDSCSQLSchema(true, true);
 }
 
 /// @brief Check the getType() method
index 579dd28590defadd6b9295f13ff74ac4accf7f62..67ebbbd2f1426d2762cb3f435d2fdf74be2d7b22 100644 (file)
@@ -29,29 +29,54 @@ validDSCSQLConnectionString() {
                              VALID_USER, VALID_PASSWORD));
 }
 
-void destroyDSCSQLSchema(bool show_err) {
-    runDSCSQLScript(DATABASE_SCRIPTS_DIR, "dscsql/dhcpdb_drop.cql", show_err);
+bool softWipeEnabled() {
+    const char* const env = getenv("KEA_TEST_CASSANDRA_WIPE");
+    if (env && (string(env) == string("soft"))) {
+        return (true);
+    }
+
+    return (false);
+}
+
+void destroyDSCSQLSchema(bool force_wipe, bool show_err) {
+    if (force_wipe || !softWipeEnabled()) {
+        // Do full wipe
+        runDSCSQLScript(DATABASE_SCRIPTS_DIR, "dscsql/dhcpdb_drop.cql", show_err);
+    } else {
+
+        // do soft wipe (just remove the data, not the structures)
+        runDSCSQLScript(DATABASE_SCRIPTS_DIR, "dscsql/soft_wipe.cql", show_err);
+    }
 }
 
-void createDSCSQLSchema(bool show_err) {
-    runDSCSQLScript(DATABASE_SCRIPTS_DIR, "dscsql/dhcpdb_create.cql",
-                   show_err);
+void createDSCSQLSchema(bool force_wipe, bool show_err) {
+    if (force_wipe || !softWipeEnabled()) {
+        runDSCSQLScript(DATABASE_SCRIPTS_DIR, "dscsql/dhcpdb_create.cql",
+                        show_err);
+    }
 }
 
 void runDSCSQLScript(const std::string& path, const std::string& script_name,
                     bool show_err) {
     std::ostringstream cmd;
-    cmd << "cqlsh -u keatest -p keatest -k keatest -f";
+    cmd << "cqlsh -u keatest -p keatest -k keatest";
     if (!show_err) {
         cmd << " 2>/dev/null ";
     }
 
+    cmd << " -f ";
+
     if (!path.empty()) {
-        cmd << " < " << path << "/";
+        cmd << path << "/";
     }
-
     cmd << script_name;
 
+    /// @todo: Remove this once the unit-tests are in better shape.
+    ///
+    /// This print is here only temporary. We will remove it once all of the
+    /// Cassandra unit-tests will be passing.
+    std::cout << "cmdline:" << cmd.str() << endl;
+
     int retval = ::system(cmd.str().c_str());
     ASSERT_EQ(0, retval) << "runDSCSQLSchema failed:" << cmd.str();
 }
index 1f927c153a3a398cb1058f122c646858a1688572..4d41b36f586c802da8eeffaf315dd23b8ac72204 100644 (file)
@@ -32,8 +32,10 @@ std::string validDSCSQLConnectionString();
 /// will fail. The output of stderr is suppressed unless the parameter,
 /// show_err is true.
 ///
+/// @param force_wipe forces wipe of the database, even if KEA_TEST_CASSANDRA_WIPE
+///                   is set.
 /// @param show_err flag which governs whether or not stderr is suppressed.
-void destroyDSCSQLSchema(bool show_err = false);
+void destroyDSCSQLSchema(bool force_wipe, bool show_err = false);
 
 /// @brief Create the DSCSQL Schema
 ///
@@ -45,8 +47,10 @@ void destroyDSCSQLSchema(bool show_err = false);
 /// will fail. The output of stderr is suppressed unless the parameter,
 /// show_err is true.
 ///
+/// @param force_wipe forces wipe of the database, even if KEA_TEST_CASSANDRA_WIPE
+///                   is set.
 /// @param show_err flag which governs whether or not stderr is suppressed.
-void createDSCSQLSchema(bool show_err = false);
+void createDSCSQLSchema(bool force_wipe, bool show_err = false);
 
 /// @brief Run a DSCSQL SQL script against the DSCSQL unit test database
 ///
@@ -61,6 +65,29 @@ void createDSCSQLSchema(bool show_err = false);
 void runDSCSQLScript(const std::string& path, const std::string& script_name,
                     bool show_err);
 
+/// @brief Returns status if the soft-wipe is enabled or not.
+///
+/// In some deployments (In case of Tomek's dev system) Cassandra tests take
+/// a very long time to execute. This was traced back to slow table/indexes
+/// creation/deletion. With full wipe and recreation of all structures, it
+/// took over 60 seconds for each test to execute. To avoid this problem, a
+/// feature called soft-wipe has been implemented. If enabled, it does not
+/// remove the structures, just the data from essential tables. To enable
+/// it set KEA_TEST_CASSANDRA_WIPE environment variable to 'soft'. Make sure
+/// that the database schema is set up properly before running in soft-wipe
+/// mode.
+///
+/// For example to use soft-wipe mode, you can:
+///
+/// $ cqlsh -u keatest -p keatest -k keatest
+///         -f src/share/database/scripts/dscsql/dhcpdb_create.cql
+/// $ export KEA_TEST_CASSANDRA_WIPE=soft
+/// $ cd src/lib/dhcpsrv
+/// $ make -j9
+/// $ tests/libdhcpsrv_unittests --gtest_filter=DSCSqlLeaseMgrTest.*
+///
+/// @return true if soft-wipe is enabled, false otherwise
+bool softWipeEnabled();
 };
 };
 };
index fd2831bbaae121ee6f7db00f5f8da3d38c5ece26..920f6ff33a628e0914b864ab3253aabbe4e1b7ad 100644 (file)
@@ -1,6 +1,6 @@
 SUBDIRS = .
 
 sqlscriptsdir = ${datarootdir}/${PACKAGE_NAME}/scripts/dscsql
-sqlscripts_DATA = dhcpdb_create.cql
+sqlscripts_DATA = dhcpdb_create.cql dhcpdb_drop.cql soft_wipe.cql
 
 EXTRA_DIST = ${sqlscripts_DATA}