Browse Source

[963] Miscellaneous changes

1. Fix minor gremlins in unit test and main program
2. Update to rename b10-dbutil process (and so avoid an error
from the process_rename_test check).
3. With (2), add run_dbutil to run in test mode
Stephen Morris 13 years ago
parent
commit
7e7b44723b

+ 2 - 0
configure.ac

@@ -1125,6 +1125,7 @@ AC_OUTPUT([doc/version.ent
            src/bin/cmdctl/tests/cmdctl_test
            src/bin/cmdctl/tests/cmdctl_test
            src/bin/cmdctl/cmdctl.spec.pre
            src/bin/cmdctl/cmdctl.spec.pre
            src/bin/dbutil/dbutil.py
            src/bin/dbutil/dbutil.py
+           src/bin/dbutil/run_dbutil.sh
            src/bin/dbutil/tests/dbutil_test.sh
            src/bin/dbutil/tests/dbutil_test.sh
            src/bin/ddns/ddns.py
            src/bin/ddns/ddns.py
            src/bin/xfrin/tests/xfrin_test
            src/bin/xfrin/tests/xfrin_test
@@ -1209,6 +1210,7 @@ AC_OUTPUT([doc/version.ent
            chmod +x src/bin/zonemgr/run_b10-zonemgr.sh
            chmod +x src/bin/zonemgr/run_b10-zonemgr.sh
            chmod +x src/bin/bind10/run_bind10.sh
            chmod +x src/bin/bind10/run_bind10.sh
            chmod +x src/bin/cmdctl/tests/cmdctl_test
            chmod +x src/bin/cmdctl/tests/cmdctl_test
+           chmod +x src/bin/dbutil/run_dbutil.sh
            chmod +x src/bin/dbutil/tests/dbutil_test.sh
            chmod +x src/bin/dbutil/tests/dbutil_test.sh
            chmod +x src/bin/xfrin/tests/xfrin_test
            chmod +x src/bin/xfrin/tests/xfrin_test
            chmod +x src/bin/xfrout/tests/xfrout_test
            chmod +x src/bin/xfrout/tests/xfrout_test

+ 1 - 1
src/bin/Makefile.am

@@ -1,4 +1,4 @@
 SUBDIRS = bind10 bindctl cfgmgr ddns loadzone msgq host cmdctl auth xfrin \
 SUBDIRS = bind10 bindctl cfgmgr ddns loadzone msgq host cmdctl auth xfrin \
-	xfrout usermgr zonemgr stats tests resolver sockcreator dhcp4 dhcp6
+	xfrout usermgr zonemgr stats tests resolver sockcreator dhcp4 dhcp6 dbutil
 
 
 check-recursive: all-recursive
 check-recursive: all-recursive

+ 5 - 1
src/bin/dbutil/Makefile.am

@@ -2,10 +2,14 @@ SUBDIRS = . tests
 
 
 bin_SCRIPTS = b10-dbutil
 bin_SCRIPTS = b10-dbutil
 
 
+noinst_SCRIPTS = run_dbutil.sh
+
 CLEANFILES = b10-dbutil b10-dbutil.pyc
 CLEANFILES = b10-dbutil b10-dbutil.pyc
 
 
 b10-dbutil: dbutil.py
 b10-dbutil: dbutil.py
-	$(SED) -e "s|@@LOCALSTATEDIR@@|$(localstatedir)|" dbutil.py > $@
+	$(SED) -e "s|@@PYTHONPATH@@|@pyexecdir@|" \
+	       -e "s|@@SYSCONFDIR@@|@sysconfdir@|" \
+	       -e "s|@@LIBEXECDIR@@|$(pkglibexecdir)|" dbutil.py >$@
 	chmod a+x $@
 	chmod a+x $@
 
 
 CLEANDIRS = __pycache__
 CLEANDIRS = __pycache__

+ 71 - 25
src/bin/dbutil/dbutil.py.in

@@ -33,12 +33,21 @@
 # ".backup" already exists).  This is used to restore the database if the
 # ".backup" already exists).  This is used to restore the database if the
 # upgrade fails.
 # upgrade fails.
 
 
-import os, sqlite3, shutil, sys
+import sys; sys.path.append("@@PYTHONPATH@@")
+import os, sqlite3, shutil
 from optparse import OptionParser
 from optparse import OptionParser
+import isc.util.process
+
+isc.util.process.rename()
 
 
 # Default database to use if the database is not given on the command line.
 # Default database to use if the database is not given on the command line.
 # (This is the same string as in "auth.spec.pre.in".)
 # (This is the same string as in "auth.spec.pre.in".)
-default_database_file = "@@LOCALSTATEDIR@@/@PACKAGE@/zone.sqlite3"
+DEFAULT_DATABASE_FILE = "@@LOCALSTATEDIR@@/@PACKAGE@/zone.sqlite3"
+
+# This is the version displayed to the user.  It comprises the module name,
+# the module version number, and the overall BIND 10 version number (set in
+# configure.ac)
+VERSION = "b10-dbutil 20120319 (BIND 10 @PACKAGE_VERSION@)"
 
 
 # Statements to update the database.
 # Statements to update the database.
 #
 #
@@ -174,12 +183,53 @@ upgrades = [
 class DbutilException(Exception):
 class DbutilException(Exception):
     pass
     pass
 
 
+# Functions for outputting messages in a consistent format.  As this is intended
+# to be an interactive utility, it was not considered necessary to use the full
+# logging framework for messages.
+
+def output(writer, prefix, text, ex = None):
+    """
+    @brief Write error message to output stream
+
+    @param writer Function to do the writing
+    @param prefix Prefix to the message
+    @param text Text to output
+    @param ex Possible exception holding additiona information
+    """
+    writer(prefix + ": " + text)
+    if ex is not None:
+        writer(" - " + str(ex))
+    writer("\n")
+
+
+def error(text, ex = None):
+    """
+    @brief Write error message to stderr.
+
+    @param text Text to output
+    @param ex Possible exception holding additiona information
+    """
+    output(sys.stderr.write, "ERROR", text, ex)
+
+
+def warn(text, ex = None):
+    """
+    @brief Write warning message to stderr.
 
 
-def info(text):
+    @param text Text to output
+    @param ex Possible exception holding additiona information
+    """
+    output(sys.stderr.write, "WARN", text, ex)
+
+
+def info(text, ex = None):
     """
     """
     @brief Write informational message to stdout.
     @brief Write informational message to stdout.
+
+    @param text Text to output
+    @param ex Possible exception holding additiona information
     """
     """
-    sys.stdout.write("INFO: " + text + "\n")
+    output(sys.stdout.write, "INFO", text, ex)
 
 
 
 
 # @brief Database Encapsulation
 # @brief Database Encapsulation
@@ -294,12 +344,12 @@ def prompt_user():
     """
     """
     sys.stdout.write(
     sys.stdout.write(
 """You have selected the upgrade option.  This will upgrade the schema of the
 """You have selected the upgrade option.  This will upgrade the schema of the
-selected BIND 10 database to the latest version.
+selected BIND 10 zone database to the latest version.
 
 
-The utility will take a copy of the database file before running so, in the
-unlikely event of a problem, you will be able to restore the database from
+The utility will take a copy of the zone database file before executing so, in
+the event of a problem, you will be able to restore the zone database from
 the backup.  To ensure that the integrity of this backup, please ensure that
 the backup.  To ensure that the integrity of this backup, please ensure that
-BIND 10 is not running before proceeding.
+BIND 10 is not running before continuing.
 """)
 """)
     yes_entered = False
     yes_entered = False
     no_entered = False
     no_entered = False
@@ -474,7 +524,7 @@ def parse_command():
     """
     """
     usage = ("usage: %prog --check [options] [db_file]\n" +
     usage = ("usage: %prog --check [options] [db_file]\n" +
              "       %prog --upgrade [--noconfirm] [options] [db_file]")
              "       %prog --upgrade [--noconfirm] [options] [db_file]")
-    parser = OptionParser(usage=usage)
+    parser = OptionParser(usage = usage, version = VERSION)
     parser.add_option("-c", "--check", action="store_true",
     parser.add_option("-c", "--check", action="store_true",
                       dest="check", default=False,
                       dest="check", default=False,
                       help="Print database version and check if it " +
                       help="Print database version and check if it " +
@@ -492,20 +542,20 @@ def parse_command():
 
 
     # Set the database file on which to operate
     # Set the database file on which to operate
     if (len(args) > 1):
     if (len(args) > 1):
-        sys.stderr.write(usage + "\n")
+        error("too many arguments to the command, maximum of one expected")
+        parser.print_usage()
         sys.exit(1)
         sys.exit(1)
     elif len(args) == 0:
     elif len(args) == 0:
-        args.append(default_database_file)
+        args.append(DEFAULT_DATABASE_FILE)
 
 
     # Check for conflicting options.  If some are found, output a suitable
     # Check for conflicting options.  If some are found, output a suitable
     # error message and print the usage.
     # error message and print the usage.
     if options.check and options.upgrade:
     if options.check and options.upgrade:
-        sys.stderr.write("cannot select both --check and --upgrade, " +
-                         "please choose one")
+        error("--upgrade is not compatible with --check")
     elif (not options.check) and (not options.upgrade):
     elif (not options.check) and (not options.upgrade):
-        sys.stderr.write("must select one of --check or --upgrade")
+        error("must select one of --check or --upgrade")
     elif (options.check and options.noconfirm):
     elif (options.check and options.noconfirm):
-        sys.stderr.write("--noconfirm is not compatible with --check")
+        error("--noconfirm is not compatible with --check")
     else:
     else:
         return (options, args)
         return (options, args)
 
 
@@ -525,8 +575,7 @@ if __name__ == "__main__":
             check_version(db)
             check_version(db)
             db.close()
             db.close()
         except Exception as ex:
         except Exception as ex:
-            sys.stderr.write("ERROR: unable to check database version - " +
-                             str(ex) + "\n")
+            error("unable to check database version - " + str(ex))
             sys.exit(1)
             sys.exit(1)
 
 
     elif options.upgrade:
     elif options.upgrade:
@@ -547,15 +596,12 @@ if __name__ == "__main__":
             db.close()
             db.close()
         except Exception as ex:
         except Exception as ex:
             if in_progress:
             if in_progress:
-                sys.stderr.write("ERROR: upgrade failed - " + str(ex) + "\n")
-                sys.stderr.write("WARN: database may be corrupt, " +
-                                 "restore database from backup\n")
+                error("upgrade failed - " + str(ex))
+                warn("database may be corrupt, restore it from backup")
             else:
             else:
-                sys.stderr.write("ERROR: upgrade preparation failed - " +
-                                 str(ex) + "\n")
-                sys.stderr.write("INFO: database upgrade was not attempted\n")
+                error("upgrade preparation failed - " + str(ex))
+                info("database upgrade was not attempted")
             sys.exit(1)
             sys.exit(1)
     else:
     else:
-        sys.stderr.write("ERROR: internal error, neither --check nor " +
-                         " --upgrade selected")
+        error("internal error, neither --check nor --upgrade selected")
         sys.exit(1)
         sys.exit(1)

+ 41 - 0
src/bin/dbutil/run_dbutil.sh.in

@@ -0,0 +1,41 @@
+#! /bin/sh
+
+# Copyright (C) 2010  Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and 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 INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM 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.
+
+PYTHON_EXEC=${PYTHON_EXEC:-@PYTHON@}
+export PYTHON_EXEC
+
+DBUTIL_PATH=@abs_top_builddir@/src/bin/dbutil
+
+PYTHONPATH=@abs_top_srcdir@/src/bin:@abs_top_builddir@/src/lib/python/isc/log_messages:@abs_top_builddir@/src/lib/python:@abs_top_builddir@/src/bin:@abs_top_srcdir@/src/lib/python
+export PYTHONPATH
+
+# If necessary (rare cases), explicitly specify paths to dynamic libraries
+# required by loadable python modules.
+SET_ENV_LIBRARY_PATH=@SET_ENV_LIBRARY_PATH@
+if test $SET_ENV_LIBRARY_PATH = yes; then
+	@ENV_LIBRARY_PATH@=@abs_top_builddir@/src/lib/dns/.libs:@abs_top_builddir@/src/lib/dns/python/.libs:@abs_top_builddir@/src/lib/cryptolink/.libs:@abs_top_builddir@/src/lib/cc/.libs:@abs_top_builddir@/src/lib/config/.libs:@abs_top_builddir@/src/lib/log/.libs:@abs_top_builddir@/src/lib/util/.libs:@abs_top_builddir@/src/lib/util/io/.libs:@abs_top_builddir@/src/lib/exceptions/.libs:@abs_top_builddir@/src/lib/datasrc/.libs:$@ENV_LIBRARY_PATH@
+	export @ENV_LIBRARY_PATH@
+fi
+
+B10_FROM_SOURCE=@abs_top_srcdir@
+export B10_FROM_SOURCE
+
+BIND10_MSGQ_SOCKET_FILE=@abs_top_builddir@/msgq_socket
+export BIND10_MSGQ_SOCKET_FILE
+
+cd ${DBUTIL_PATH}
+exec ${PYTHON_EXEC} -O b10-dbutil "$@"

+ 21 - 19
src/bin/dbutil/tests/dbutil_test.sh.in

@@ -147,7 +147,7 @@ get_schema() {
 # @param $1 Database to upgrade
 # @param $1 Database to upgrade
 upgrade_ok_test() {
 upgrade_ok_test() {
     cp $1 $tempfile
     cp $1 $tempfile
-    ../b10-dbutil --upgrade --noconfirm $tempfile
+    ../run_dbutil.sh --upgrade --noconfirm $tempfile
     if [ $? -eq 0 ]
     if [ $? -eq 0 ]
     then
     then
         # Compare schema with the reference
         # Compare schema with the reference
@@ -188,7 +188,7 @@ record_count_test() {
     records_count=`sqlite3 $tempfile 'select count(*) from records'`
     records_count=`sqlite3 $tempfile 'select count(*) from records'`
     zones_count=`sqlite3 $tempfile 'select count(*) from zones'`
     zones_count=`sqlite3 $tempfile 'select count(*) from zones'`
 
 
-    ../b10-dbutil --upgrade --noconfirm $tempfile
+    ../run_dbutil.sh --upgrade --noconfirm $tempfile
     if [ $? -ne 0 ]
     if [ $? -ne 0 ]
     then
     then
         # Reason for failure should already have been output
         # Reason for failure should already have been output
@@ -234,12 +234,12 @@ record_count_test() {
 # @param $2 Expected version string
 # @param $2 Expected version string
 check_version() {
 check_version() {
     cp $1 $verfile
     cp $1 $verfile
-    ../b10-dbutil --check $verfile
+    ../run_dbutil.sh --check $verfile
     if [ $? -ne 0 ]
     if [ $? -ne 0 ]
     then
     then
         fail "version check failed on database $1"
         fail "version check failed on database $1"
     else
     else
-        ../b10-dbutil --check $verfile | grep "$2"
+        ../run_dbutil.sh --check $verfile | grep "$2" > /dev/null
         if [ $? -ne 0 ]
         if [ $? -ne 0 ]
         then
         then
             fail "database $1 not at expected version $2"
             fail "database $1 not at expected version $2"
@@ -257,12 +257,12 @@ rm -f $tempfile $backupfile
 
 
 # Test 1 - check that the utility fails if the database does not exist
 # Test 1 - check that the utility fails if the database does not exist
 echo "1.1. Non-existent database - check"
 echo "1.1. Non-existent database - check"
-../b10-dbutil --check $tempfile
+../run_dbutil.sh --check $tempfile
 failzero $?
 failzero $?
 check_no_backup $tempfile $backupfile
 check_no_backup $tempfile $backupfile
 
 
 echo "1.2. Non-existent database - upgrade"
 echo "1.2. Non-existent database - upgrade"
-../b10-dbutil --upgrade --noconfirm $tempfile
+../run_dbutil.sh --upgrade --noconfirm $tempfile
 failzero $?
 failzero $?
 check_no_backup $tempfile $backupfile
 check_no_backup $tempfile $backupfile
 rm -f $tempfile $backupfile
 rm -f $tempfile $backupfile
@@ -271,14 +271,14 @@ rm -f $tempfile $backupfile
 # Test 2 - should fail to check an empty file and fail to upgrade it
 # Test 2 - should fail to check an empty file and fail to upgrade it
 echo "2.1. Database is an empty file - check"
 echo "2.1. Database is an empty file - check"
 touch $tempfile
 touch $tempfile
-../b10-dbutil --check $tempfile
+../run_dbutil.sh --check $tempfile
 failzero $?
 failzero $?
 check_no_backup $tempfile $backupfile
 check_no_backup $tempfile $backupfile
 rm -f $tempfile $backupfile
 rm -f $tempfile $backupfile
 
 
 echo "2.2. Database is an empty file - upgrade"
 echo "2.2. Database is an empty file - upgrade"
 touch $tempfile
 touch $tempfile
-../b10-dbutil --upgrade --noconfirm $tempfile
+../run_dbutil.sh --upgrade --noconfirm $tempfile
 failzero $?
 failzero $?
 # A backup is performed before anything else, so the backup should exist.
 # A backup is performed before anything else, so the backup should exist.
 check_backup $tempfile $backupfile
 check_backup $tempfile $backupfile
@@ -287,12 +287,12 @@ rm -f $tempfile $backupfile
 
 
 echo "3.1. Database is not an SQLite file - check"
 echo "3.1. Database is not an SQLite file - check"
 echo "This is not an sqlite3 database" > $tempfile
 echo "This is not an sqlite3 database" > $tempfile
-../b10-dbutil --check $tempfile 
+../run_dbutil.sh --check $tempfile 
 failzero $?
 failzero $?
 check_no_backup $tempfile $backupfile
 check_no_backup $tempfile $backupfile
 
 
 echo "3.2. Database is not an SQLite file - upgrade"
 echo "3.2. Database is not an SQLite file - upgrade"
-../b10-dbutil --upgrade --noconfirm $tempfile 
+../run_dbutil.sh --upgrade --noconfirm $tempfile 
 failzero $?
 failzero $?
 # ...and as before, a backup should have been created
 # ...and as before, a backup should have been created
 check_backup $tempfile $backupfile
 check_backup $tempfile $backupfile
@@ -301,14 +301,14 @@ rm -f $tempfile $backupfile
 
 
 echo "4.1. Database is an SQLite3 file without the schema table - check"
 echo "4.1. Database is an SQLite3 file without the schema table - check"
 cp $testdata/no_schema.sqlite3 $tempfile
 cp $testdata/no_schema.sqlite3 $tempfile
-../b10-dbutil --check $tempfile
+../run_dbutil.sh --check $tempfile
 failzero $?
 failzero $?
 check_no_backup $tempfile $backupfile
 check_no_backup $tempfile $backupfile
 rm -f $tempfile $backupfile
 rm -f $tempfile $backupfile
 
 
 echo "4.1. Database is an SQLite3 file without the schema table - upgrade"
 echo "4.1. Database is an SQLite3 file without the schema table - upgrade"
 cp $testdata/no_schema.sqlite3 $tempfile
 cp $testdata/no_schema.sqlite3 $tempfile
-../b10-dbutil --upgrade --noconfirm $tempfile
+../run_dbutil.sh --upgrade --noconfirm $tempfile
 failzero $?
 failzero $?
 check_backup $testdata/no_schema.sqlite3 $backupfile
 check_backup $testdata/no_schema.sqlite3 $backupfile
 rm -f $tempfile $backupfile
 rm -f $tempfile $backupfile
@@ -360,25 +360,27 @@ check_backup $testdata/v2_0.sqlite3 ${backupfile}-2
 rm -f $tempfile $backupfile ${backupfile}-1 ${backupfile}-2
 rm -f $tempfile $backupfile ${backupfile}-1 ${backupfile}-2
 
 
 
 
-echo "10.1 Incompatible flags"
+echo "10.1 Command-line errors"
 cp $testdata/old_v1.sqlite3 $tempfile
 cp $testdata/old_v1.sqlite3 $tempfile
-../b10-util --upgrade --check $tempfile
+../run_dbutil.sh $tempfile
 failzero $?
 failzero $?
-../b10-util --upgrade --check $tempfile
+../run_dbutil.sh --upgrade --check $tempfile
 failzero $?
 failzero $?
-../b10-util --noconfirm --check $tempfile
+../run_dbutil.sh --noconfirm --check $tempfile
+failzero $?
+../run_dbutil.sh --check $tempfile $backupfile
 failzero $?
 failzero $?
 rm -f $tempfile $backupfile
 rm -f $tempfile $backupfile
 
 
 echo "10.2 verbose flag"
 echo "10.2 verbose flag"
 cp $testdata/old_v1.sqlite3 $tempfile
 cp $testdata/old_v1.sqlite3 $tempfile
-../b10-dbutil --upgrade --noconfirm --verbose $tempfile
+../run_dbutil.sh --upgrade --noconfirm --verbose $tempfile
 passzero $?
 passzero $?
 rm -f $tempfile $backupfile
 rm -f $tempfile $backupfile
 
 
 echo "10.3 Interactive prompt - yes"
 echo "10.3 Interactive prompt - yes"
 cp $testdata/old_v1.sqlite3 $tempfile
 cp $testdata/old_v1.sqlite3 $tempfile
-../b10-dbutil --upgrade $tempfile << .
+../run_dbutil.sh --upgrade $tempfile << .
 Yes
 Yes
 .
 .
 passzero $?
 passzero $?
@@ -387,7 +389,7 @@ rm -f $tempfile $backupfile
 
 
 echo "10.4 Interactive prompt - no"
 echo "10.4 Interactive prompt - no"
 cp $testdata/old_v1.sqlite3 $tempfile
 cp $testdata/old_v1.sqlite3 $tempfile
-../b10-dbutil --upgrade $tempfile << .
+../run_dbutil.sh --upgrade $tempfile << .
 no
 no
 .
 .
 passzero $?
 passzero $?