Commit 131fcf52 authored by Thomas Markwalder's avatar Thomas Markwalder

[master] Unit tests now use production database scripts

    Merges in trac4239.
parents 6ad5c07e f1162218
......@@ -1404,19 +1404,11 @@ AC_CONFIG_FILES([compatcheck/Makefile
src/bin/perfdhcp/tests/testdata/Makefile
src/bin/admin/Makefile
src/bin/admin/kea-admin
src/bin/admin/scripts/Makefile
src/bin/admin/tests/Makefile
src/bin/admin/tests/data/Makefile
src/bin/admin/tests/memfile_tests.sh
src/bin/admin/tests/mysql_tests.sh
src/bin/admin/tests/pgsql_tests.sh
src/bin/admin/scripts/mysql/Makefile
src/bin/admin/scripts/mysql/upgrade_1.0_to_2.0.sh
src/bin/admin/scripts/mysql/upgrade_2.0_to_3.0.sh
src/bin/admin/scripts/mysql/upgrade_3.0_to_4.0.sh
src/bin/admin/scripts/mysql/upgrade_4.0_to_4.1.sh
src/bin/admin/scripts/pgsql/Makefile
src/bin/admin/scripts/pgsql/upgrade_1.0_to_2.0.sh
src/hooks/Makefile
src/hooks/dhcp/Makefile
src/hooks/dhcp/user_chk/Makefile
......@@ -1483,6 +1475,16 @@ AC_CONFIG_FILES([compatcheck/Makefile
src/lib/util/unittests/Makefile
src/lib/eval/Makefile
src/lib/eval/tests/Makefile
src/share/Makefile
src/share/database/Makefile
src/share/database/scripts/Makefile
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
src/share/database/scripts/mysql/upgrade_3.0_to_4.0.sh
src/share/database/scripts/mysql/upgrade_4.0_to_4.1.sh
src/share/database/scripts/pgsql/Makefile
src/share/database/scripts/pgsql/upgrade_1.0_to_2.0.sh
tools/Makefile
tools/path_replacer.sh
])
......
SUBDIRS = lib bin hooks
SUBDIRS = share lib bin hooks
EXTRA_DIST = \
cppcheck-suppress.lst \
......
SUBDIRS = . scripts tests
SUBDIRS = . tests
# Install kea-admin in sbin.
sbin_SCRIPTS = kea-admin
......
......@@ -9,15 +9,11 @@
# Include common test library.
. @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh
# If the code is installed, include admin-utils.sh from the destination
# directory. If not, include it from the sources.
prefix=@prefix@
# Include admin utilities
. @abs_top_srcdir@/src/bin/admin/admin-utils.sh
if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
. @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh
else
. @abs_top_srcdir@/src/bin/admin/admin-utils.sh
fi
# Set path to the production schema scripts
db_scripts_dir=@abs_top_srcdir@/src/share/database/scripts
db_user="keatest"
db_password="keatest"
......@@ -30,24 +26,9 @@ 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}"`
mysql -u$db_user -p$db_password $db_name >/dev/null 2>&1 < $db_scripts_dir/mysql/dhcpdb_drop.mysql
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"
}
......@@ -58,7 +39,7 @@ mysql_lease_init_test() {
mysql_wipe
# Ok, now let's initalize the database
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-init mysql returned non-zero status code %d, expected %d"
......@@ -137,7 +118,7 @@ mysql_host_reservation_init_test() {
mysql_wipe
# Ok, now let's initalize the database
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-init mysql returned non-zero status code %d, expected %d"
......@@ -202,12 +183,12 @@ mysql_upgrade_test() {
mysql -u$db_user -p$db_password $db_name < @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.mysql
# Sanity check - verify that it reports version 1.0.
version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts)
version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir)
assert_str_eq "1.0" ${version} "Expected kea-admin to return %s, returned value was %s"
# Ok, we have a 1.0 database. Let's upgrade it to 4.1
${keaadmin} lease-upgrade mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts
${keaadmin} lease-upgrade mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-upgrade mysql returned non-zero status code %d, expected %d"
......@@ -309,7 +290,7 @@ EOF
assert_eq 1 $count "lease6DumpData doesn't have order by clause. (returned count %d, expected %d)"
# Verify upgraded schemd reports version 4.1.
version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name -d @abs_top_srcdir@/src/bin/admin/scripts)
version=$(${keaadmin} lease-version mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir)
assert_str_eq "4.1" ${version} "Expected kea-admin to return %s, returned value was %s"
......@@ -346,7 +327,7 @@ mysql_lease4_dump_test() {
mysql_wipe
# Ok, now let's initalize the database
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
......@@ -361,7 +342,7 @@ insert into lease4 values(12,22,NULL,40,\"2015-03-03 11:01:07\",50,1,1,\"three.e
assert_eq 0 $ERRCODE "insert into lease4 failed, expected exit code %d, actual %d"
# Dump lease4 to output_file
${keaadmin} lease-dump mysql -4 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
${keaadmin} lease-dump mysql -4 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed, expected exit code %d, actual %d"
......@@ -406,7 +387,7 @@ mysql_lease6_dump_test() {
mysql_wipe
# Ok, now let's initalize the database
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
......@@ -421,7 +402,7 @@ insert into lease6 values(12,21,30,\"2015-06-06 11:01:07\",40,50,1,60,70,1,1,\"t
assert_eq 0 $ERRCODE "insert into lease6 failed, expected exit code %d, actual %d"
# Dump lease4 to output_file
${keaadmin} lease-dump mysql -6 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
${keaadmin} lease-dump mysql -6 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-dump -6 failed, status code %d"
......
#!/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
......@@ -9,15 +9,11 @@
# Include common test library.
. @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh
# If the code is installed, include admin-utils.sh from the destination
# directory. If not, include it from the sources.
prefix=@prefix@
# Include admin utilities
. @abs_top_srcdir@/src/bin/admin/admin-utils.sh
if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then
. @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh
else
. @abs_top_srcdir@/src/bin/admin/admin-utils.sh
fi
# Set path to the production schema scripts
db_scripts_dir=@abs_top_srcdir@/src/share/database/scripts
db_user="keatest"
db_password="keatest"
......@@ -26,21 +22,13 @@ db_name="keatest"
# Set location of the kea-admin.
keaadmin=@abs_top_builddir@/src/bin/admin/kea-admin
# Set location of scripts directory
scripts_dir=@abs_top_srcdir@/src/bin/admin/scripts
# Wipe all tables from the DB:
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"
cat $db_scripts_dir/pgsql/dhcpdb_drop.pgsql | 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() {
......@@ -50,7 +38,7 @@ pgsql_lease_init_test() {
pgsql_wipe
# Create the database
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
assert_eq 0 $? "kea-admin lease-init pgsql failed, expected exit code: %d, actual: %d"
# Verify that all the expected tables exist
......@@ -79,7 +67,7 @@ pgsql_lease_init_test() {
# check
echo ""
echo "DB created successfully, make sure we aren't allowed to try it again:"
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
assert_eq 2 $? "kea-admin failed to deny lease-init, expected exit code: %d, actual: %d"
# Let's wipe the whole database
......@@ -95,7 +83,7 @@ pgsql_lease_version_test() {
pgsql_wipe
# Create the database
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d"
# Verfiy that kea-admin lease-version returns the correct version
......@@ -118,7 +106,7 @@ pgsql_upgrade_test() {
pgsql_execute_script @abs_top_srcdir@/src/bin/admin/tests/dhcpdb_create_1.0.pgsql
assert_eq 0 $? "cannot initialize the database, expected exit code: %d, actual: %d"
${keaadmin} lease-upgrade pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
${keaadmin} lease-upgrade pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
assert_eq 0 $? "lease-upgrade failed, expected exit code: %d, actual: %d"
#table: state column added to lease4 (upgrade 1.0 -> 2.0)
......@@ -204,7 +192,7 @@ pgsql_lease4_dump_test() {
pgsql_wipe
# Ok, now let's initalize the database
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $script_dir
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
......@@ -223,7 +211,7 @@ insert into lease4 values(12,E'\\x22','',40,'$timestamp3',50,'t','t','three.exam
assert_eq 0 $ERRCODE "insert into lease4 failed, expected exit code %d, actual %d"
# Dump lease4 to output_file
${keaadmin} lease-dump pgsql -4 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
${keaadmin} lease-dump pgsql -4 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed, status code %d"
......@@ -299,7 +287,7 @@ pgsql_lease6_dump_test() {
pgsql_wipe
# Ok, now let's initalize the database
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $script_dir
${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $db_scripts_dir
ERRCODE=$?
assert_eq 0 $ERRCODE "could not create database, status code %d"
......@@ -318,7 +306,7 @@ insert into lease6 values(12,E'\\x21',30,'$timestamp3',40,50,1,60,70,'t','t','th
assert_eq 0 $ERRCODE "insert into lease6 failed, status code %d"
# Dump lease6 to output_file
${keaadmin} lease-dump pgsql -6 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
${keaadmin} lease-dump pgsql -6 -u $db_user -p $db_password -n $db_name -d $db_scripts_dir -o $output_file
ERRCODE=$?
assert_eq 0 $ERRCODE "kea-admin lease-dump -6 failed, status code %d"
......
// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
// Copyright (C) 2014-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
......@@ -417,7 +417,7 @@ public:
return (value);
}
/// @brief Converts a column in a row in a result set to a boolean.
/// @brief Fetches boolean text ('t' or 'f') as a bool.
///
/// @param r the result set containing the query results
/// @param row the row number within the result set
......@@ -440,7 +440,7 @@ public:
}
}
/// @brief Converts a column in a row in a result set to a uint32_t.
/// @brief Fetches an integer text column as a uint32_t.
///
/// @param r the result set containing the query results
/// @param row the row number within the result set
......@@ -461,7 +461,28 @@ public:
}
}
/// @brief Converts a column in a row in a result set to a uint8_t.
/// @brief Fetches an integer text column as a int32_t.
///
/// @param r the result set containing the query results
/// @param row the row number within the result set
/// @param col the column number within the row
/// @param[out] value parameter to receive the converted value
///
/// @throw DbOperationError if the value cannot be fetched or is
/// invalid.
void getColumnValue(PGresult*& r, const int row, const size_t col,
int32_t &value) const {
const char* data = getRawColumnValue(r, row, col);
try {
value = boost::lexical_cast<int32_t>(data);
} catch (const std::exception& ex) {
isc_throw(DbOperationError, "Invalid int32_t data: " << data
<< " for: " << getColumnLabel(col) << " row:" << row
<< " : " << ex.what());
}
}
/// @brief Fetches an integer text column as a uint8_t.
///
/// @param r the result set containing the query results
/// @param row the row number within the result set
......@@ -484,7 +505,7 @@ public:
}
}
/// @brief Converts a column in a row in a result set to a Lease6::Type
/// @brief Fetches an integer text column as a Lease6::Type
///
/// @param r the result set containing the query results
/// @param row the row number within the result set
......@@ -807,7 +828,7 @@ private:
public:
PgSqlLease6Exchange()
: lease_(), duid_length_(0), duid_(), iaid_(0), iaid_str_(""),
: lease_(), duid_length_(0), duid_(), iaid_u_(0), iaid_str_(""),
lease_type_(Lease6::TYPE_NA), lease_type_str_(""), prefix_len_(0),
prefix_len_str_(""), pref_lifetime_(0), preferred_lft_str_("") {
......@@ -878,7 +899,11 @@ public:
lease_type_str_ = boost::lexical_cast<std::string>(lease_->type_);
bind_array.add(lease_type_str_);
iaid_str_ = boost::lexical_cast<std::string>(lease_->iaid_);
// The iaid is stored as an INT in lease6 table, so we must
// lexically cast from an integer version to avoid out of range
// exception failure upon insert.
iaid_u_.uval_ = lease_->iaid_;
iaid_str_ = boost::lexical_cast<std::string>(iaid_u_.ival_);
bind_array.add(iaid_str_);
prefix_len_str_ = boost::lexical_cast<std::string>
......@@ -930,7 +955,7 @@ public:
getColumnValue(r, row, LEASE_TYPE_COL, lease_type_);
getColumnValue(r, row , IAID_COL, iaid_);
getColumnValue(r, row , IAID_COL, iaid_u_.ival_);
getColumnValue(r, row , PREFIX_LEN_COL, prefix_len_);
......@@ -942,8 +967,9 @@ public:
/// @todo: implement this in #3557.
HWAddrPtr hwaddr;
Lease6Ptr result(new Lease6(lease_type_, addr, duid_ptr, iaid_,
pref_lifetime_, valid_lifetime_, 0, 0,
Lease6Ptr result(new Lease6(lease_type_, addr, duid_ptr,
iaid_u_.uval_, pref_lifetime_,
valid_lifetime_, 0, 0,
subnet_id_, fqdn_fwd_, fqdn_rev_,
hostname_, hwaddr, prefix_len_));
result->cltt_ = cltt_;
......@@ -987,7 +1013,20 @@ private:
size_t duid_length_;
vector<uint8_t> duid_;
uint8_t duid_buffer_[DUID::MAX_DUID_LEN];
uint32_t iaid_;
/// @brief Union for marshalling IAID into and out of the database
/// IAID is defined in the RFC as 4 octets, which Kea code handles as
/// a uint32_t. Postgresql however, offers only signed integer types
/// of sizes 2, 4, and 8 bytes (SMALLINT, INT, and BIGINT respectively).
/// IAID is used in several indexes so rather than use the BIGINT, we
/// use this union to safely move the value into and out of an INT column.
union Uiaid {
Uiaid(uint32_t val) : uval_(val){};
Uiaid(int32_t val) : ival_(val){};
uint32_t uval_;
int32_t ival_;
} iaid_u_;
std::string iaid_str_;
Lease6::Type lease_type_;
std::string lease_type_str_;
......
......@@ -115,7 +115,6 @@ if HAVE_PGSQL
libdhcpsrv_unittests_SOURCES += pgsql_lease_mgr_unittest.cc
endif
libdhcpsrv_unittests_SOURCES += pool_unittest.cc
libdhcpsrv_unittests_SOURCES += schema_pgsql_copy.h
libdhcpsrv_unittests_SOURCES += srv_config_unittest.cc
libdhcpsrv_unittests_SOURCES += subnet_unittest.cc
libdhcpsrv_unittests_SOURCES += test_get_callout_handle.cc test_get_callout_handle.h
......
// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
// Copyright (C) 2014-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
......@@ -916,8 +916,8 @@ GenericLeaseMgrTest::testLease6HWTypeAndSource() {
// Those should use defines from Pkt::HWADDR_SOURCE_*, but let's
// test an uncommon value (and 0 which means unknown).
hwaddr1->source_ = 123456u;
hwaddr2->source_ = 0;
hwaddr1->source_ = HWAddr::HWADDR_SOURCE_RAW;
hwaddr2->source_ = HWAddr::HWADDR_SOURCE_DUID;
leases[1]->hwaddr_ = hwaddr1; // Add hardware address to leases 1 and 2
leases[2]->hwaddr_ = hwaddr2;
......@@ -938,14 +938,14 @@ GenericLeaseMgrTest::testLease6HWTypeAndSource() {
ASSERT_TRUE(stored1);
ASSERT_TRUE(stored1->hwaddr_);
EXPECT_EQ(123, stored1->hwaddr_->htype_);
EXPECT_EQ(123456, stored1->hwaddr_->source_);
EXPECT_EQ(HWAddr::HWADDR_SOURCE_RAW, stored1->hwaddr_->source_);
// Second lease should have a hardware address in it
Lease6Ptr stored2 = lmptr_->getLease6(leasetype6_[2], ioaddress6_[2]);
ASSERT_TRUE(stored2);
ASSERT_TRUE(stored2->hwaddr_);
EXPECT_EQ(456, stored2->hwaddr_->htype_);
EXPECT_EQ(0, stored2->hwaddr_->source_);
EXPECT_EQ(HWAddr::HWADDR_SOURCE_DUID, stored2->hwaddr_->source_);
// Third lease should NOT have any hardware address.
Lease6Ptr stored3 = lmptr_->getLease6(leasetype6_[3], ioaddress6_[3]);
......
......@@ -125,21 +125,21 @@ TEST(MySqlHostDataSource, OpenDatabase) {
// Check that invalid login data causes an exception.
EXPECT_THROW(HostDataSourceFactory::create(connectionString(
VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(HostDataSourceFactory::create(connectionString(
VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(HostDataSourceFactory::create(connectionString(
VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(HostDataSourceFactory::create(connectionString(
VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
DbOpenError);
// Check for missing parameters
EXPECT_THROW(HostDataSourceFactory::create(connectionString(
VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
NoDatabaseName);
// Tidy up after the test
......
......@@ -97,8 +97,8 @@ public:
TEST(MySqlOpenTest, OpenDatabase) {
// Schema needs to be created for the test to work.
destroyMySQLSchema();
createMySQLSchema();
destroyMySQLSchema(true);
createMySQLSchema(true);
// Check that lease manager open the database opens correctly and tidy up.
// If it fails, print the error message.
......@@ -129,25 +129,25 @@ TEST(MySqlOpenTest, OpenDatabase) {
// Check that invalid login data causes an exception.
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, INVALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, VALID_NAME, INVALID_HOST, VALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
DbOpenError);
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
MYSQL_VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, INVALID_PASSWORD)),
DbOpenError);
// Check for missing parameters
EXPECT_THROW(LeaseMgrFactory::create(connectionString(
VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
MYSQL_VALID_TYPE, NULL, VALID_HOST, INVALID_USER, VALID_PASSWORD)),
NoDatabaseName);
// Tidy up after the test
destroyMySQLSchema();
destroyMySQLSchema(true);
}
/// @brief Check the getType() method
......
// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
// Copyright (C) 2014-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
......@@ -11,9 +11,9 @@
#include <dhcpsrv/pgsql_lease_mgr.h>
#include <dhcpsrv/tests/test_utils.h>
#include <dhcpsrv/tests/generic_lease_mgr_unittest.h>
#include <dhcpsrv/testutils/pgsql_schema.h>
#include <exceptions/exceptions.h>
#include <gtest/gtest.h>
#include <algorithm>
......@@ -22,6 +22,8 @@
#include <string>
#include <utility>
#include <stdlib.h>
using namespace isc;
using namespace isc::asiolink;
using namespace isc::dhcp;
......@@ -30,112 +32,6 @@ using namespace std;
namespace {
// This holds statements to create and destroy the schema.
#include "schema_pgsql_copy.h"
// Connection strings.
// Database: keatest
// Host: localhost
// Username: keatest
// Password: keatest
const char* VALID_TYPE = "type=postgresql";
const char* INVALID_TYPE = "type=unknown";
const char* VALID_NAME = "name=keatest";
const char* INVALID_NAME = "name=invalidname";
const char* VALID_HOST = "host=localhost";
const char* INVALID_HOST = "host=invalidhost";
const char* VALID_USER = "user=keatest";
const char* INVALID_USER = "user=invaliduser";
const char* VALID_PASSWORD = "password=keatest";
const char* INVALID_PASSWORD = "password=invalid";
// Given a combination of strings above, produce a connection string.
string connectionString(const char* type, const char* name, const char* host,
const char* user, const char* password) {
const string space = " ";
string result = "";
if (type != NULL) {
result += string(type);
}
if (name != NULL) {
if (! result.empty()) {
result += space;
}
result += string(name);
}
if (host != NULL) {
if (! result.empty()) {
result += space;
}
result += string(host);
}
if (user != NULL) {
if (! result.empty()) {
result += space;
}
result += string(user);
}
if (password != NULL) {
if (! result.empty()) {
result += space;
}
result += string(password);
}
return (result);
}
// Return valid connection string
string
validConnectionString() {
return (connectionString(VALID_TYPE, VALID_NAME, VALID_HOST,
VALID_USER, VALID_PASSWORD));
}
// @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 destroySchema() {
// 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);