From: Thomas Markwalder Date: Mon, 11 Apr 2016 19:16:58 +0000 (-0400) Subject: [4239] Added drop scripts for both MySQL and Postgresql X-Git-Tag: trac4106_update_base~38^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5b978dabe14432341999fcb0c48a9f2e3f88ed15;p=thirdparty%2Fkea.git [4239] Added drop scripts for both MySQL and Postgresql Rather than use hard-coded lists or query logic for dropping the database all MySQL and Posgresql tests use new drop scripts added to src/bind/admin/scripts. src/bin/admin/scripts/mysql/dhcpdb_drop.mysql - New SQL script to drop a MySQL database src/bin/admin/scripts/pgsql/dhcpdb_drop.pgsql - New SQL script to drop a Postgresql database src/bin/admin/tests/mysql_tests.sh.in - mysql_wipe() - modified to use new drop script src/bin/admin/tests/pgsql_tests.sh.in - pgsql_wipe() modified to use new drop script src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc - TEST(MySqlOpenTest, OpenDatabase) - added show_err=true to destroyMySQLSchema() calls src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc - TEST(PgSqlOpenTest, OpenDatabase) - added show_err=true to destroyMySQLSchema() calls src/lib/dhcpsrv/testutils/mysql_schema.cc - destroyMySQLSchema(bool show_err) - modified to use the new drop script and accept show_err parameter src/lib/dhcpsrv/testutils/pgsql_schema.cc - destroyPgSQLSchema(bool show_err) - modified to use the new drop script and accept show_err parameter --- diff --git a/src/bin/admin/scripts/mysql/dhcpdb_drop.mysql b/src/bin/admin/scripts/mysql/dhcpdb_drop.mysql new file mode 100755 index 0000000000..f05dd33a8b --- /dev/null +++ b/src/bin/admin/scripts/mysql/dhcpdb_drop.mysql @@ -0,0 +1,24 @@ +# Copyright (C) 2016 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/. + +# Turn off foreign key checks as CASCADE isn't really supported in MySQL +SET SESSION FOREIGN_KEY_CHECKS = 0; +DROP TABLE IF EXISTS lease4; +DROP TABLE IF EXISTS lease6; +DROP TABLE IF EXISTS lease6_types; +DROP TABLE IF EXISTS lease_hwaddr_source; +DROP TABLE IF EXISTS schema_version; +DROP TABLE IF EXISTS ipv6_reservations; +DROP TABLE IF EXISTS hosts; +DROP TABLE IF EXISTS dhcp4_options; +DROP TABLE IF EXISTS dhcp6_options; +DROP TABLE IF EXISTS host_identifier_type; +DROP TABLE IF EXISTS lease_state; +DROP TRIGGER IF EXISTS host_BDEL; +DROP PROCEDURE IF EXISTS lease4DumpHeader; +DROP PROCEDURE IF EXISTS lease4DumpData; +DROP PROCEDURE IF EXISTS lease6DumpHeader; +DROP PROCEDURE IF EXISTS lease6DumpData; diff --git a/src/bin/admin/scripts/pgsql/dhcpdb_drop.pgsql b/src/bin/admin/scripts/pgsql/dhcpdb_drop.pgsql new file mode 100644 index 0000000000..025a1db906 --- /dev/null +++ b/src/bin/admin/scripts/pgsql/dhcpdb_drop.pgsql @@ -0,0 +1,15 @@ +-- Copyright (C) 2016 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/. + +DROP TABLE IF EXISTS lease4 CASCADE; +DROP TABLE IF EXISTS lease6 CASCADE; +DROP TABLE IF EXISTS lease6_types CASCADE; +DROP TABLE IF EXISTS schema_version CASCADE; +DROP TABLE IF EXISTS lease_state CASCADE; +DROP FUNCTION IF EXISTS lease4DumpHeader(); +DROP FUNCTION IF EXISTS lease4DumpData(); +DROP FUNCTION IF EXISTS lease6DumpHeader(); +DROP FUNCTION IF EXISTS lease6DumpData(); diff --git a/src/bin/admin/tests/mysql_tests.sh.in b/src/bin/admin/tests/mysql_tests.sh.in index b9e08b3d8c..c6e6349b6a 100755 --- a/src/bin/admin/tests/mysql_tests.sh.in +++ b/src/bin/admin/tests/mysql_tests.sh.in @@ -30,24 +30,10 @@ keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin mysql_wipe() { printf "Wiping whole database %s\n" $db_name - # First we build the list of drop table commands - # We don't bother with "cascade" because as of MySQL - # 5.1 it is only there to ease porting, it doesn't - # actually do anything. - qry="\ -SELECT CONCAT('DROP TABLE ', table_schema, '.', table_name, ';') \ -FROM information_schema.tables \ -WHERE table_schema = 'keatest';" - - drop_sql=`mysql_execute "${qry}"` + drop_script="@abs_top_srcdir@/src/bin/admin/scripts/mysql/dhcpdb_drop.mysql" + mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 < $drop_script ERRCODE=$? - assert_eq 0 $ERRCODE "mysql-wipe: table query failed, exit code %d, expected %d" - # We need to turn off referrential integrity checks so tables - # are dropped regardless of whether they are used in foreign keys. - # (This is what cascade would normally do) - mysql_execute "SET SESSION FOREIGN_KEY_CHECKS = 0;$drop_sql" - ERRCODE=$? assert_eq 0 $ERRCODE "mysql-wipe: drop table sql failed, exit code %d, expected %d" } diff --git a/src/bin/admin/tests/pgsql_tests.sh.in b/src/bin/admin/tests/pgsql_tests.sh.in index 7b29d4ead2..32ec42a9ec 100644 --- a/src/bin/admin/tests/pgsql_tests.sh.in +++ b/src/bin/admin/tests/pgsql_tests.sh.in @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC") +# Copyright (C) 2015-2016 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 @@ -34,13 +34,9 @@ pgsql_wipe() { printf "Wiping whole database %s\n" $db_name export PGPASSWORD=$db_password - # Make a set of drop commands, one for each table owned by keatest - RESULT=`pgsql_execute "SELECT 'drop table if exists '||t.tablename || ' cascade;' as dcmd FROM pg_catalog.pg_tables t WHERE t.tableowner = 'keatest';"` - assert_eq 0 $? "pgsql_wipe select failed, expected exit code: %d, actual: %d" - - # Now execute the set of drop commands from the result set returned - RESULT=`pgsql_execute "$RESULT"` - assert_eq 0 $? "pgsql_wipe drop failed, expected exit code: %d, actual: %d" + drop_script="@abs_top_srcdir@/src/bin/admin/scripts/pgsql/dhcpdb_drop.pgsql" + cat $drop_script | psql --set ON_ERROR_STOP=1 -A -t -q -U keatest -d keatest >/dev/null 2>&1 + assert_eq 0 $? "pgsql_wipe drop failed, expected exit code: %d, actual: %d" } pgsql_lease_init_test() { diff --git a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc index 8f1f42b3d6..20225398b9 100755 --- a/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/mysql_lease_mgr_unittest.cc @@ -97,7 +97,7 @@ public: TEST(MySqlOpenTest, OpenDatabase) { // Schema needs to be created for the test to work. - destroyMySQLSchema(); + destroyMySQLSchema(true); createMySQLSchema(true); // Check that lease manager open the database opens correctly and tidy up. @@ -147,7 +147,7 @@ TEST(MySqlOpenTest, OpenDatabase) { NoDatabaseName); // Tidy up after the test - destroyMySQLSchema(); + destroyMySQLSchema(true); } /// @brief Check the getType() method diff --git a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc index 58370daa67..5dc97e4fef 100755 --- a/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc +++ b/src/lib/dhcpsrv/tests/pgsql_lease_mgr_unittest.cc @@ -97,8 +97,8 @@ public: TEST(PgSqlOpenTest, OpenDatabase) { // Schema needs to be created for the test to work. - destroyPgSQLSchema(); - createPgSQLSchema(); + destroyPgSQLSchema(true); + createPgSQLSchema(true); // Check that lease manager open the database opens correctly and tidy up. // If it fails, print the error message. @@ -153,7 +153,7 @@ TEST(PgSqlOpenTest, OpenDatabase) { NoDatabaseName); // Tidy up after the test - destroyPgSQLSchema(); + destroyPgSQLSchema(true); } /// @brief Check the getType() method diff --git a/src/lib/dhcpsrv/testutils/mysql_schema.cc b/src/lib/dhcpsrv/testutils/mysql_schema.cc index 7bf2928056..d2376c94b3 100644 --- a/src/lib/dhcpsrv/testutils/mysql_schema.cc +++ b/src/lib/dhcpsrv/testutils/mysql_schema.cc @@ -29,39 +29,8 @@ validMySQLConnectionString() { VALID_USER, VALID_PASSWORD)); } -void destroyMySQLSchema() { - MySqlHolder mysql; - // @todo - replace this with call to drop script once it exists - const char* destroy_statement[] = { - // Turning off referential integrity checks ensures tables get dropped - "SET SESSION FOREIGN_KEY_CHECKS = 0", - "DROP TABLE IF EXISTS lease4", - "DROP TABLE IF EXISTS lease6", - "DROP TABLE IF EXISTS lease6_types", - "DROP TABLE IF EXISTS lease_hwaddr_source", - "DROP TABLE IF EXISTS schema_version", - "DROP TABLE IF EXISTS ipv6_reservations", - "DROP TABLE IF EXISTS hosts", - "DROP TABLE IF EXISTS dhcp4_options", - "DROP TABLE IF EXISTS dhcp6_options", - "DROP TABLE IF EXISTS host_identifier_type", - "DROP TABLE IF EXISTS lease_state", - "DROP TRIGGER IF EXISTS host_BDEL", - "DROP PROCEDURE IF EXISTS lease4DumpHeader", - "DROP PROCEDURE IF EXISTS lease4DumpData", - "DROP PROCEDURE IF EXISTS lease6DumpHeader", - "DROP PROCEDURE IF EXISTS lease6DumpData", - NULL - }; - - // Open database - (void) mysql_real_connect(mysql, "localhost", "keatest", - "keatest", "keatest", 0, NULL, 0); - - // Get rid of everything in it. - for (int i = 0; destroy_statement[i] != NULL; ++i) { - (void) mysql_query(mysql, destroy_statement[i]); - } +void destroyMySQLSchema(bool show_err) { + runMySQLScript(TEST_ADMIN_SCRIPTS_DIR, "mysql/dhcpdb_drop.mysql", show_err); } void createMySQLSchema(bool show_err) { diff --git a/src/lib/dhcpsrv/testutils/mysql_schema.h b/src/lib/dhcpsrv/testutils/mysql_schema.h index e493fdd637..cb95fe2bc9 100644 --- a/src/lib/dhcpsrv/testutils/mysql_schema.h +++ b/src/lib/dhcpsrv/testutils/mysql_schema.h @@ -24,36 +24,43 @@ std::string validMySQLConnectionString(); /// @brief Clear everything from the database /// -/// There is no error checking in this code: if something fails, one of the -/// tests will (should) fall over. -void destroyMySQLSchema(); +/// Submits the current schema drop script: +/// +/// /mysql/dhcpdb_drop.mysql +/// +/// to the unit test MySQL database. If the script fails, the invoking test +/// will fail. The output of stderr is suppressed unless the parameter, +/// show_err is true. +/// +/// @param show_err flag which governs whether or not stderr is suppressed. +void destroyMySQLSchema(bool show_err = false); -// @brief Run a MySQL SQL script against the Postgresql unit test database -// -// Submits the given SQL script to MySQL via mysql CLI. The output of -// stderr is suppressed unless the parameter, show_err is true. The is done -// to suppress warnings that might otherwise make test output needlessly -// noisy. A gtest assertion occurs if the script fails to execute. -// -// @param path - path (if not blank) of the script to execute -// @param script_name - file name of the path to execute -// @param show_err flag which governs whether or not stderr is suppressed. +/// @brief Create the MySQL Schema +/// +/// Submits the current schema creation script: +/// +/// /mysql/dhcpdb_create.mysql +/// +/// to the unit test MySQL database. If the script fails, the invoking test +/// will fail. The output of stderr is suppressed unless the parameter, +/// show_err is true. +/// +/// @param show_err flag which governs whether or not stderr is suppressed. +void createMySQLSchema(bool show_err = false); + +/// @brief Run a MySQL SQL script against the Postgresql unit test database +/// +/// Submits the given SQL script to MySQL via mysql CLI. The output of +/// stderr is suppressed unless the parameter, show_err is true. The is done +/// to suppress warnings that might otherwise make test output needlessly +/// noisy. A gtest assertion occurs if the script fails to execute. +/// +/// @param path - path (if not blank) of the script to execute +/// @param script_name - file name of the path to execute +/// @param show_err flag which governs whether or not stderr is suppressed. void runMySQLScript(const std::string& path, const std::string& script_name, bool show_err); -// @brief Create the MySQL Schema -// -// Submits the current schema creation script: -// -// /mysql/dhcpdb_create.mysql -// -// to the unit test MySQL database. If the script fails, the invoking test -// will fail. The output of stderr is suppressed unless the parameter, -// show_err is true. -// -// @param show_err flag which governs whether or not stderr is suppressed. -void createMySQLSchema(bool show_err = false); - }; }; }; diff --git a/src/lib/dhcpsrv/testutils/pgsql_schema.cc b/src/lib/dhcpsrv/testutils/pgsql_schema.cc index b275d944ef..efa38541fe 100644 --- a/src/lib/dhcpsrv/testutils/pgsql_schema.cc +++ b/src/lib/dhcpsrv/testutils/pgsql_schema.cc @@ -30,33 +30,9 @@ validPgSQLConnectionString() { VALID_USER, VALID_PASSWORD)); } -void destroyPgSQLSchema() { - // @todo - replace this call to run drop script once the script exists - const char* destroy_statement[] = { - "DROP TABLE lease4 CASCADE", - "DROP TABLE LEASE6 CASCADE", - "DROP TABLE lease6_types CASCADE", - "DROP TABLE schema_version CASCADE", - "DROP TABLE lease_state CASCADE", - "DROP FUNCTION lease4DumpHeader()", - "DROP FUNCTION lease4DumpData()", - "DROP FUNCTION lease6DumpHeader()", - "DROP FUNCTION lease6DumpData()", - NULL - }; - - // Open database - PGconn* conn = 0; - conn = PQconnectdb("host = 'localhost' user = 'keatest'" - " password = 'keatest' dbname = 'keatest'"); - - // Get rid of everything in it. - for (int i = 0; destroy_statement[i] != NULL; ++i) { - PGresult* r = PQexec(conn, destroy_statement[i]); - PQclear(r); - } - - PQfinish(conn); +void destroyPgSQLSchema(bool show_err) { + runPgSQLScript(TEST_ADMIN_SCRIPTS_DIR, "pgsql/dhcpdb_drop.pgsql", + show_err); } void createPgSQLSchema(bool show_err) { diff --git a/src/lib/dhcpsrv/testutils/pgsql_schema.h b/src/lib/dhcpsrv/testutils/pgsql_schema.h index a679da163d..7b4cf54ecf 100644 --- a/src/lib/dhcpsrv/testutils/pgsql_schema.h +++ b/src/lib/dhcpsrv/testutils/pgsql_schema.h @@ -24,33 +24,40 @@ std::string validPgSQLConnectionString(); /// @brief Clear everything from the database /// -/// There is no error checking in this code: if something fails, one of the -/// tests will (should) fall over. -void destroyPgSQLSchema(); +/// Submits the current schema drop script: +/// +/// /pgsql/dhcpdb_drop.pgsql +/// +/// to the unit test Postgresql database. If the script fails, the invoking +/// test will fail. The output of stderr is suppressed unless the parameter, +/// show_err is true. +/// +/// @param show_err flag which governs whether or not stderr is suppressed. +void destroyPgSQLSchema(bool show_err = false); -// @brief Create the Postgresql Schema -// -// Submits the current schema creation script: -// -// /pgsql/dhcpdb_create.pgsql -// -// to the unit test Postgresql database. If the script fails, the invoking -// test will fail. The output of stderr is suppressed unless the parameter, -// show_err is true. -// -// @param show_err flag which governs whether or not stderr is suppressed. +/// @brief Create the Postgresql Schema +/// +/// Submits the current schema creation script: +/// +/// /pgsql/dhcpdb_create.pgsql +/// +/// to the unit test Postgresql database. If the script fails, the invoking +/// test will fail. The output of stderr is suppressed unless the parameter, +/// show_err is true. +/// +/// @param show_err flag which governs whether or not stderr is suppressed. void createPgSQLSchema(bool show_err = false); -// @brief Run a PgSQL SQL script against the Postgresql unit test database -// -// Submits the given SQL script to Postgresql via psql CLI. The output of -// stderr is suppressed unless the parameter, show_err is true. The is done -// to suppress warnings that might otherwise make test output needlessly -// noisy. A gtest assertion occurs if the script fails to execute. -// -// @param path - path (if not blank) of the script to execute -// @param script_name - file name of the path to execute -// @param show_err flag which governs whether or not stderr is suppressed. +/// @brief Run a PgSQL SQL script against the Postgresql unit test database +/// +/// Submits the given SQL script to Postgresql via psql CLI. The output of +/// stderr is suppressed unless the parameter, show_err is true. The is done +/// to suppress warnings that might otherwise make test output needlessly +/// noisy. A gtest assertion occurs if the script fails to execute. +/// +/// @param path - path (if not blank) of the script to execute +/// @param script_name - file name of the path to execute +/// @param show_err flag which governs whether or not stderr is suppressed. void runPgSQLScript(const std::string& path, const std::string& script_name, bool show_err);