From: Thomas Markwalder Date: Thu, 28 May 2015 18:18:04 +0000 (-0400) Subject: [3883] Added support for PostgreSQL to kea-admin X-Git-Tag: trac3732a_base~22^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=094d49f6937b3fb4bb28ff7eb796617cc7955ade;p=thirdparty%2Fkea.git [3883] Added support for PostgreSQL to kea-admin configure.ac - added src/bin/admin/tests/pgsql_tests.sh to list of config generated files (i.e AC_CONFIG_FILES) src/bin/admin/tests/pgsql_tests.sh.in - new file containing unit tests of for kea-admin PostgreSQL support src/bin/admin/tests/Makefile.am - added pgsql_tests.sh src/bin/admin/admin-utils.sh - added PosgreSQL support functions: pgsql_execute() pgsql_execute_script() pgsql_version() pgsql_version_print() src/bin/admin/kea-admin.in - implemented pgsql_init() - removed pgsql_version(), kea-admin uses function in admin-utils.sh directly - implemented pgsql_upgrade() - note that it fails with exit 1 as there are not yet any upgrades available --- diff --git a/configure.ac b/configure.ac index 1fcb031f77..0e3d3316a3 100644 --- a/configure.ac +++ b/configure.ac @@ -1453,9 +1453,10 @@ AC_CONFIG_FILES([compatcheck/Makefile src/bin/admin/tests/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_2.0_to_3.0.sh src/bin/admin/scripts/pgsql/Makefile src/hooks/Makefile src/hooks/dhcp/Makefile diff --git a/src/bin/admin/admin-utils.sh b/src/bin/admin/admin-utils.sh index 9f23c3dee0..58e6c74f4e 100644 --- a/src/bin/admin/admin-utils.sh +++ b/src/bin/admin/admin-utils.sh @@ -1,4 +1,4 @@ -# 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 @@ -42,3 +42,44 @@ mysql_version_print() { mysql_version "$@" printf "%s" $_RESULT } + +pgsql_execute() { + QUERY=$1 + shift + if [ $# -gt 0 ]; then + _RESULT=$(echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q $*) + retcode=$? + else + export PGPASSWORD=$db_password + _RESULT=$(echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name) + retcode=$? + fi + return $retcode +} + +pgsql_execute_script() { + file=$1 + shift + if [ $# -gt 0 ]; then + _RESULT=$(psql --set ON_ERROR_STOP=1 -A -t -q -f $file $*) + retcode=$? + else + export PGPASSWORD=$db_password + _RESULT=$(psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name -f $file) + retcode=$? + fi + return $retcode +} + + +pgsql_version() { + pgsql_execute "SELECT version || '.' || minor FROM schema_version" "$@" + return $? +} + +pgsql_version_print() { + pgsql_version "$@" + retcode=$? + printf "%s" $_RESULT + return $? +} diff --git a/src/bin/admin/kea-admin.in b/src/bin/admin/kea-admin.in index c910a28fe1..b07d371882 100644 --- a/src/bin/admin/kea-admin.in +++ b/src/bin/admin/kea-admin.in @@ -1,6 +1,6 @@ #!/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 @@ -158,8 +158,31 @@ mysql_init() { } 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 @@ -169,11 +192,6 @@ memfile_version() { exit 1 } -pgsql_version() { - log_error "NOT IMPLEMENTED" - exit 1 -} - ### Functions used for upgrade memfile_upgrade() { # @todo Implement this as part of #3601 @@ -218,7 +236,37 @@ mysql_upgrade() { } 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 } @@ -329,7 +377,7 @@ case ${command} in printf "\n" ;; pgsql) - pgsql_version + pgsql_version_print ;; esac ;; diff --git a/src/bin/admin/tests/Makefile.am b/src/bin/admin/tests/Makefile.am index 03fc1ee37e..4b88a50e58 100644 --- a/src/bin/admin/tests/Makefile.am +++ b/src/bin/admin/tests/Makefile.am @@ -6,12 +6,16 @@ if HAVE_MYSQL SHTESTS += mysql_tests.sh endif +if HAVE_PGSQL +SHTESTS += pgsql_tests.sh +endif + noinst_SCRIPTS = $(SHTESTS) EXTRA_DIST = dhcpdb_create_1.0.mysql CLEANFILES = *.log -DISTCLEANFILES = memfile_tests.sh mysql_tests.sh +DISTCLEANFILES = memfile_tests.sh mysql_tests.sh pgsql_tests.sh # Execute all test scripts. check-local: diff --git a/src/bin/admin/tests/pgsql_tests.sh.in b/src/bin/admin/tests/pgsql_tests.sh.in new file mode 100644 index 0000000000..99ab3ec027 --- /dev/null +++ b/src/bin/admin/tests/pgsql_tests.sh.in @@ -0,0 +1,126 @@ +#!/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