src/share/database/scripts/cql/Makefile
src/share/database/scripts/cql/upgrade_1.0_to_2.0.sh
src/share/database/scripts/cql/upgrade_2.0_to_3.0.sh
+ src/share/database/scripts/cql/wipe_data.sh
src/share/database/scripts/mysql/Makefile
src/share/database/scripts/mysql/upgrade_1.0_to_2.0.sh
src/share/database/scripts/mysql/upgrade_2.0_to_3.0.sh
}
void
-destroyCqlSchema(bool force_wipe, bool show_err) {
- if (force_wipe || !softWipeEnabled()) {
- // Do full wipe
+destroyCqlSchema(bool force, bool show_err) {
+ // If force is true or wipeCqlData() fails, destory the schema.
+ if (force || (!softWipeEnabled()) || wipeCqlData(show_err)) {
runCqlScript(DATABASE_SCRIPTS_DIR, "cql/dhcpdb_drop.cql", show_err);
- } else {
- // do soft wipe (just remove the data, not the structures)
- runCqlScript(DATABASE_SCRIPTS_DIR, "cql/soft_wipe.cql", show_err);
}
}
void
-createCqlSchema(bool force_wipe, bool show_err) {
- if (force_wipe || !softWipeEnabled()) {
+createCqlSchema(bool force, bool show_err) {
+ // If force is true or wipeCqlData() fails, recreate the schema.
+ if (force || (!softWipeEnabled()) || wipeCqlData(show_err)) {
+ destroyCqlSchema(show_err, true);
runCqlScript(DATABASE_SCRIPTS_DIR, "cql/dhcpdb_create.cql", show_err);
}
}
const std::string& script_name,
bool show_err) {
std::ostringstream cmd;
- cmd << "cqlsh -u keatest -p keatest -k keatest";
+ cmd << "cqlsh -u keatest -p keatest -k keatest --request-timeout=6000";
if (!show_err) {
cmd << " 2>/dev/null ";
}
}
}
+bool wipeCqlData(bool show_err) {
+ std::ostringstream cmd;
+ cmd << "sh " << DATABASE_SCRIPTS_DIR << "/";
+
+ std::ostringstream version;
+ version << CQL_SCHEMA_VERSION_MAJOR << "." << CQL_SCHEMA_VERSION_MINOR;
+
+ cmd << "cql/wipe_data.sh" << " " << version.str()
+ << " -u keatest -p keatest -k keatest --request-timeout=6000";
+
+ if (!show_err) {
+ cmd << " 2>/dev/null ";
+ }
+
+ int retval = ::system(cmd.str().c_str());
+ if (retval) {
+ std::cerr << "wipeCqlData failed:[" << cmd.str() << "]" << std::endl;
+ }
+
+ return(retval);
+}
+
} // namespace test
} // namespace dhcp
} // namespace isc
/// @return valid CQL connection string.
std::string validCqlConnectionString();
-/// @brief Clear everything from the database
+/// @brief Clear the unit test database
///
-/// Submits the current schema drop script:
+/// In order to reduce test execution time, this function
+/// defaults to first attempting to delete transient data
+/// from the database by calling @c wipeCqlData. If that
+/// function fails it will then attempt to destroy the database
+/// schema by running the SQL script:
///
/// <TEST_ADMIN_SCRIPTS_DIR>/cql/dhcpdb_drop.cql
///
-/// to the unit test CQL database. If the script fails, the invoking test
-/// will fail. The output of stderr is suppressed unless the parameter,
-/// show_err is true.
+/// The default behavior of wiping the data only may be overridden
+/// in one of two ways:
///
-/// @param force_wipe forces wipe of the database, even if
-/// KEA_TEST_CASSANDRA_WIPE is set.
+/// -# Setting the force parameter to true
+/// -# Defining the environment variable:
+/// KEA_TEST_DB_WIPE_DATA_ONLY="false"
+///
+/// @param force if true, the function will skip deleting the data and
/// @param show_err flag which governs whether or not stderr is suppressed.
-void destroyCqlSchema(bool force_wipe, bool show_err = false);
+/// destroy the schema.
+void destroyCqlSchema(bool force, bool show_err = false);
-/// @brief Create the CQL Schema
+/// @brief Create the unit test Cql Schema
///
-/// Submits the current schema creation script:
+/// Ensures the unit test database is a empty and version-correct.
+/// Unless, the force parameter is true, it will first attempt
+/// to wipe the data from the database by calling @c wipeCqlData.
+/// If this call succeeds the function returns, otherwise it will
+/// will call @c destroyCqlSchema to forcibly remove the
+/// existing schema and then submits the SQL script:
///
/// <TEST_ADMIN_SCRIPTS_DIR>/cql/dhcpdb_create.cql
///
-/// to the unit test CQL database. If the script fails, the invoking test
-/// will fail. The output of stderr is suppressed unless the parameter,
-/// show_err is true.
+/// to the unit test Cql database.
+///
+/// The default behavior of wiping the data only may be overridden
+/// in one of two ways:
+///
+/// -# Setting the force parameter to true
+/// -# Defining the environment variable:
+/// KEA_TEST_DB_WIPE_DATA_ONLY="false"
///
-/// @param force_wipe forces wipe of the database, even if
-/// KEA_TEST_CASSANDRA_WIPE is set.
+/// @param force flag when true, the function will recreate the database
+/// schema.
/// @param show_err flag which governs whether or not stderr is suppressed.
-void createCqlSchema(bool force_wipe, bool show_err = false);
+void createCqlSchema(bool force, bool show_err = false);
/// @brief Run a CQL script against the CQL unit test database
///
/// @throw Unexpected when the script returns an error.
void runCqlScript(const std::string& path, const std::string& script_name,
bool show_err);
+
+/// @brief Attempts to wipe data from the Cql unit test database
+///
+/// Runs the shell script
+///
+/// <TEST_ADMIN_SCRIPTS_DIR>/cql/wipe_data.sh
+///
+/// This will fail if there is no schema, if the existing schema
+/// version is incorrect (i.e. does not match CQL_SCHEMA_VERSION_MAJOR
+/// and CQL_SCHEMA_VERSION_MINOR), or a SQL error occurs. Otherwise,
+/// the script is should delete all transient data, leaving intact
+/// reference tables.
+///
+/// @param show_err flag which governs whether or not stderr is suppressed.
+bool wipeCqlData(bool show_err);
+
};
};
};
/// @brief Clears the database and opens connection to it.
void initializeTest() {
// Ensure schema is the correct one.
- destroyCqlSchema(false, true);
createCqlSchema(false, true);
// Connect to the database
TEST(CqlOpenTest, OpenDatabase) {
// Schema needs to be created for the test to work.
- destroyCqlSchema(false, true);
createCqlSchema(false, true);
// Check that lease manager open the database opens correctly and tidy up.
}
void destroyMySQLSchema(bool show_err, bool force) {
- // If force is true or wipeData() fails, destory the schema.
- if (force || (!softWipeEnabled()) || wipeData(show_err)) {
+ // If force is true or wipeMySQLData() fails, destory the schema.
+ if (force || (!softWipeEnabled()) || wipeMySQLData(show_err)) {
runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_drop.mysql", show_err);
}
}
void createMySQLSchema(bool show_err, bool force) {
- // If force is true or wipeData() fails, recreate the schema.
- if (force || (!softWipeEnabled()) || wipeData(show_err)) {
+ // If force is true or wipeMySQLData() fails, recreate the schema.
+ if (force || (!softWipeEnabled()) || wipeMySQLData(show_err)) {
destroyMySQLSchema(show_err, true);
runMySQLScript(DATABASE_SCRIPTS_DIR, "mysql/dhcpdb_create.mysql", show_err);
}
}
-bool wipeData(bool show_err) {
+bool wipeMySQLData(bool show_err) {
std::ostringstream cmd;
cmd << "sh " << DATABASE_SCRIPTS_DIR << "/";
int retval = ::system(cmd.str().c_str());
if (retval) {
- std::cerr << "wipeData failed:[" << cmd.str() << "]" << std::endl;
+ std::cerr << "wipeMySQLData failed:[" << cmd.str() << "]" << std::endl;
}
return(retval);
/// @brief Clear the unit test database
///
-/// Either calls @c wipeData to wipe the data from the
-/// database /or destroys the database itself by submitting the
-/// SQL script:
+/// In order to reduce test execution time, this function
+/// defaults to first attempting to delete transient data
+/// from the database by calling @c wipeMySQLData. If that
+/// function fails it will then attempt to destroy the database
+/// schema by running the SQL script:
///
/// <TEST_ADMIN_SCRIPTS_DIR>/mysql/dhcpdb_drop.mysql
///
-/// If wipeData() is called and fails, it will destroy
-/// the schema. If the schema destruction fails, the
-/// invoking test should fail.
+/// The default behavior of wiping the data only may be overridden
+/// in one of two ways:
///
-/// The output stderr is suppressed unless the parameter,
-/// show_err is true.
+/// -# Setting the force parameter to true
+/// -# Defining the environment variable:
+/// KEA_TEST_DB_WIPE_DATA_ONLY="false"
///
/// @param show_err flag which governs whether or not stderr is suppressed.
-/// @param force if true, the function will simply destroy the schema.
+/// @param force if true, the function will skip deleting the data and
+/// destroy the schema.
void destroyMySQLSchema(bool show_err = false, bool force = false);
/// @brief Create the unit test MySQL Schema
///
/// Ensures the unit test database is a empty and version-correct.
/// Unless, the force parameter is true, it will first attempt
-/// to wipe the data from the database by calling @c wipeData.
+/// to wipe the data from the database by calling @c wipeMySQLData.
/// If this call succeeds the function returns, otherwise it will
/// will call @c destroyMySQLSchema to forcibly remove the
/// existing schema and then submits the SQL script:
///
/// to the unit test MySQL database.
///
+/// The default behavior of wiping the data only may be overridden
+/// in one of two ways:
+///
+/// -# Setting the force parameter to true
+/// -# Defining the environment variable:
+/// KEA_TEST_DB_WIPE_DATA_ONLY="false"
+///
/// @param show_err flag which governs whether or not stderr is suppressed.
/// @param force flag when true, the function will recreate the database
/// schema.
/// reference tables.
///
/// @param show_err flag which governs whether or not stderr is suppressed.
-bool wipeData(bool show_err = false);
+bool wipeMySQLData(bool show_err = false);
/// @brief Run a MySQL SQL script against the MySQL unit test database
///
/upgrade_1.0_to_2.0.sh
/upgrade_2.0_to_3.0.sh
-
+/wipe_data.sh
sqlscripts_DATA += dhcpdb_drop.cql
sqlscripts_DATA += upgrade_1.0_to_2.0.sh
sqlscripts_DATA += upgrade_2.0_to_3.0.sh
-sqlscripts_DATA += soft_wipe.cql
+sqlscripts_DATA += wipe_data.sh
EXTRA_DIST = ${sqlscripts_DATA}
+++ /dev/null
--- Copyright (C) 2016-2018 Internet Systems Consortium.
---
--- This Source Code Form is subject to the terms of the Mozilla Public
--- License, v. 2.0. If a copy of the MPL was not distributed with this
--- file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
--- This is the soft wipe script for CQL (Cassandra). This capability may be
--- used when running Cassandra unit-tests.
---
--- In some deployments (e.g. 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.
-
-TRUNCATE TABLE lease4;
-TRUNCATE TABLE lease6;
-TRUNCATE TABLE lease6_types;
-TRUNCATE TABLE lease_hwaddr_source;
-TRUNCATE TABLE lease_state;
-TRUNCATE TABLE schema_version;
-TRUNCATE TABLE host_reservations;
-TRUNCATE TABLE logs;
--- /dev/null
+# Copyright (C) 2019 Internet Systems Consortium, Inc. ("ISC")
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#!/bin/sh
+
+# This script is primarily used for CQL unit tests, which need to
+# ensure an empty, but schema correct database for each test. It
+# deletes ALL transient data from an existing Kea CQL schema,
+# including leases, reservations, etc... Use at your own peril.
+# Reference tables will be left in-tact.
+
+# Include utilities. Use installed version if available and
+# use build version if it isn't.
+if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
+ . @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh
+else
+ . @abs_top_builddir@/src/bin/admin/admin-utils.sh
+fi
+
+# First argument is must be the expected schema version <major>.<minor>
+exp_version="$1"
+shift;
+
+# Remaining arguments are used as cql command line arguments
+
+# If the existing schema doesn't match, the fail
+VERSION=`cql_version "$@"`
+if [ "$VERSION" = "" ]; then
+ printf "Cannot wipe data, schema version could not be detected.\n"
+ exit 1
+fi
+
+if [ "$VERSION" != "$exp_version" ]; then
+ printf "Cannot wipe data, wrong schema version. Expected $exp_version, found version $VERSION.\n"
+ exit 1
+fi
+
+# Delete transient data from tables.
+cqlsh "$@" <<EOF
+TRUNCATE TABLE lease4;
+TRUNCATE TABLE lease6;
+TRUNCATE TABLE lease6_types;
+TRUNCATE TABLE lease_hwaddr_source;
+TRUNCATE TABLE lease_state;
+TRUNCATE TABLE host_reservations;
+TRUNCATE TABLE logs;
+EOF
+
+RESULT=$?
+
+exit $RESULT