#!/bin/sh
-# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+# Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
}
pgsql_init() {
- log_error "NOT IMPLEMENTED"
- exit 1
+ printf "Checking if there is a database initialized already. Please ignore errors.\n"
+
+ # Let's try to count the number of tables. Anything above 0 means that there
+ # is some database in place. If there is anything, we abort.
+ pgsql_execute "\d"
+ COUNT=`echo "$_RESULT" | wc -w`
+ if [ $COUNT -gt 0 ]; then
+ printf "\n"
+ log_error "Expected empty database $db_name, but the following tables are present \n$_RESULT. Aborting."
+ exit 2
+ fi
+
+ init_script="$scripts_dir/pgsql/dhcpdb_create.pgsql"
+ printf "Initializing database using script %s\n" $init_script
+ pgsql_execute_script $init_script
+ ERRCODE=$?
+ if [ "$ERRCODE" -ne 0 ]; then
+ log_error "Database initializatio failed, status code: $ERRCODE?"
+ exit 1
+ fi
+
+ printf "Lease DB version reported after initialization: "
+ pgsql_version_print
+ printf "\n"
+ exit 0
}
### Functions that implement database version checking commands
exit 1
}
-pgsql_version() {
- log_error "NOT IMPLEMENTED"
- exit 1
-}
-
### Functions used for upgrade
memfile_upgrade() {
# @todo Implement this as part of #3601
}
pgsql_upgrade() {
- log_error "NOT IMPLEMENTED"
+ # @todo - When PostgreSQL has a schema greater than 1.0, this will need
+ # to be implemented
+# log_error "Cannot upgrade: PostgreSQL backend only supports schema version 1.0."
+# exit 1
+
+ pgysql_version_print
+ printf "\n"
+
+ # Check if the scripts directory exists at all.
+ if [ ! -d ${scripts_dir}/pgsql ]; then
+ log_error "Invalid scripts directory: ${scripts_dir}/pgsql"
+ exit 1
+ fi
+
+ # Check if there are any files in it
+ num_files=$(find ${scripts_dir}/pgsql/upgrade*.sh -type f | wc -l)
+ if [ $num_files -eq 0 ]; then
+ log_error "No scripts in ${scripts_dir}/pgsql or the directory is not readable or does not have any upgrade* scripts."
+ exit 1
+ fi
+
+ for script in ${scripts_dir}/pgsql/upgrade*.sh
+ do
+ echo "Processing $script file..."
+ sh ${script} --user=${db_user} --password=${db_password} ${db_name}
+ done
+
+ printf "Lease DB version reported after upgrade: "
+ pgsql_version_print
+ printf "\n"
+ exit 0
}
printf "\n"
;;
pgsql)
- pgsql_version
+ pgsql_version_print
;;
esac
;;
--- /dev/null
+#!/bin/sh
+
+# Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# 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@
+
+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
+
+db_user="keatest"
+db_password="keatest"
+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
+ 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, exit code: $?"
+
+ # Now execute the set of drop commands from the result set returned
+ pgsql_execute "$_RESULT"
+ assert_eq 0 $? "pgsql_wipe drop failed, exit code: $?"
+}
+
+pgsql_lease_init_test() {
+ test_start "pgsql.lease-init"
+
+ # Let's wipe the whole database
+ pgsql_wipe
+
+ # Create the database
+ ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
+ assert_eq 0 $? "kea-admin lease-init pgsql failed, exit code: $?"
+
+ # Verify that all the expected tables exist
+
+ # Check schema_version table
+ pgsql_execute "SELECT version, minor FROM schema_version;"
+ assert_eq 0 $? "schema_vesion table check failed, exit code: $?"
+
+ # Check lease4 table
+ pgsql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname FROM lease4;"
+ assert_eq 0 $? "lease4 table check failed, exit code: $?"
+
+ # Check lease6 table
+ pgsql_execute "SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname FROM lease6;"
+ assert_eq 0 $? "lease6 table check failed, exit code: $?"
+
+ # Check lease6_types table
+ pgsql_execute "SELECT lease_type, name FROM lease6_types;"
+ assert_eq 0 $? "lease6_types table check failed, exit code: $?"
+
+ # Trying to create it again should fail. This verifies the db present
+ # 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
+ assert_eq 2 $? "kea-admin failed to deny lease-init, exit code: $?"
+
+ # Let's wipe the whole database
+ pgsql_wipe
+
+ test_finish 0
+}
+
+pgsql_lease_version_test() {
+ test_start "pgsql.lease-version"
+
+ # Wipe the whole database
+ pgsql_wipe
+
+ # Create the database
+ ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
+ assert_eq 0 $? "cannot initialize the database, exit code: $?"
+
+ # Verfiy that kea-admin lease-version returns the correct version
+ version=$(${keaadmin} lease-version pgsql -u $db_user -p $db_password -n $db_name)
+ assert_str_eq "1.0" ${version} "Expected kea-admin to return %s, returned value was %s"
+
+ # Let's wipe the whole database
+ pgsql_wipe
+
+ test_finish 0
+}
+
+pgsql_upgrade_test() {
+ test_start "pgsql.upgrade-test"
+
+ ${keaadmin} lease-upgrade pgsql -u $db_user -p $db_password -n $db_name -d $scripts_dir
+ assert_eq 1 $? "lease-upgrade should have failed, exit code: $?"
+
+ test_finish 0
+}
+
+pgsql_lease_init_test
+pgsql_lease_version_test
+pgsql_upgrade_test