Browse Source

[3884] Addressed review comments

Minor cleanup. Lease dump tests for both MySQL and PostgreSQL
now use multiple timestamps values.
Thomas Markwalder 9 years ago
parent
commit
d6ec7d8068

+ 28 - 10
doc/guide/admin.xml

@@ -366,12 +366,14 @@ $
 
             <listitem>
               <para>
-                Create the database tables using the new user's
-                credentials and the dhcpdb_create.pgsql script supplied
-                with Kea.  After entering the following command, you
-                will be prompted for the new user's password. When the
-                command completes you will be returned to the shell
-                prompt. You should see output similar to following:
+                At this point you are ready to create the database tables.
+                This can be done using the <command>kea-admin</command> tool
+                as explained in the next section (recommended), or manually.
+                To create the tables manually enter the following command.
+                Note that PostgreSQL will prompt you to enter the new user's
+                password you specified in Step 3. When the command completes
+                you will be returned to the shell prompt. You should see output
+                similar to following:
 <screen>
 $ <userinput>psql -d <replaceable>database-name</replaceable> -U <replaceable>user-name</replaceable> -f <replaceable>path-to-kea</replaceable>/share/kea/scripts/pgsql/dhcpdb_create.pgsql</userinput>
 Password for user <replaceable>user-name</replaceable>:
@@ -434,12 +436,28 @@ host    <replaceable>database-name</replaceable>    <replaceable>user-name</repl
 
       <section>
         <title>Initialize the PostgreSQL Database Using kea-admin</title>
-
         <para>
-          Support for PostgreSQL in <command>kea-admin</command> is
-          currently not implemented.
+          If you elected not to create the tables manually, you can do
+          so now by running the <command>kea-admin</command> tool:
+<screen>
+$ <userinput>kea-admin lease-init pgsql -u <replaceable>database-user</replaceable> -p <replaceable>database-password</replaceable> -n <replaceable>database-name</replaceable></userinput>
+</screen>
+          Do not do this if you already created the tables in manually.
+          <command>kea-admin</command> implements rudimentary checks:
+          it will refuse to initialize a database that contains any
+          existing tables. If you want to start from scratch, you
+          must remove all data manually. (This process is a manual
+          operation on purpose to avoid possibly irretrievable mistakes
+          by <command>kea-admin</command>.)
+        </para>
+      </section>
+      <section id="pgsql-upgrade">
+        <title>Upgrading a PostgreSQL Database from an Earlier Version of Kea</title>
+        <para>
+          Currently, PostgreSQL only supports Kea schema version 1.0 so no upgrades
+          are available.  As upgrades become available, <command>kea-admin</command>
+          will support them.
         </para>
-        <!-- @todo: document PgSQL upgrade once they are implemented in kea-admin -->
       </section>
     </section> <!-- end of PostgreSQL sections -->
     <section>

+ 3 - 4
src/bin/admin/admin-utils.sh

@@ -24,13 +24,13 @@
 #
 # It returns the mysql command exit status to the caller as $?
 mysql_execute() {
+    QUERY=$1
+    shift
     if [ $# -gt 1 ]; then
-        QUERY="$1"
-        shift
         mysql -N -B  $* -e "${QUERY}"
         retcode=$?
     else
-        mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name
+        mysql -N -B --user=$db_user --password=$db_password -e "${QUERY}" $db_name
         retcode="$?"
     fi
 
@@ -90,7 +90,6 @@ pgsql_execute_script() {
     return $retcode
 }
 
-
 pgsql_version() {
     pgsql_execute "SELECT version || '.' || minor FROM schema_version" "$@"
     return $?

+ 3 - 3
src/bin/admin/tests/data/mysql.lease4_dump_test.reference.csv

@@ -1,4 +1,4 @@
 address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname
-0.0.0.10,3230,3330,40,0000-00-00 00:00:00,50,1,1,one.example.com
-0.0.0.11,,313233,40,0000-00-00 00:00:00,50,1,1,
-0.0.0.12,3232,,40,0000-00-00 00:00:00,50,1,1,three.example.com
+0.0.0.10,3230,3330,40,2015-01-01 01:15:30,50,1,1,one.example.com
+0.0.0.11,,313233,40,2015-02-02 02:30:45,50,1,1,
+0.0.0.12,3232,,40,2015-03-03 11:01:07,50,1,1,three.example.com

+ 3 - 3
src/bin/admin/tests/data/mysql.lease6_dump_test.reference.csv

@@ -1,4 +1,4 @@
 address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,hwtype,hwaddr_source
-10,3230,30,0000-00-00 00:00:00,40,50,IA_TA,60,70,1,1,one.example.com,3830,90,100
-11,,30,0000-00-00 00:00:00,40,50,IA_TA,60,70,1,1,,3830,90,100
-12,3231,30,0000-00-00 00:00:00,40,50,IA_TA,60,70,1,1,three.example.com,3830,90,100
+10,3230,30,2015-04-04 01:15:30,40,50,IA_TA,60,70,1,1,one.example.com,3830,90,100
+11,,30,2015-05-05 02:30:45,40,50,IA_TA,60,70,1,1,,3830,90,100
+12,3231,30,2015-06-06 11:01:07,40,50,IA_TA,60,70,1,1,three.example.com,3830,90,100

+ 3 - 3
src/bin/admin/tests/data/pgsql.lease4_dump_test.reference.csv

@@ -1,4 +1,4 @@
 address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname
-0.0.0.10,20,30,40,<timestamp>,50,1,1,one.example.com
-0.0.0.11,,013233,40,<timestamp>,50,1,1,
-0.0.0.12,22,,40,<timestamp>,50,1,1,three.example.com
+0.0.0.10,20,30,40,<timestamp1>,50,1,1,one.example.com
+0.0.0.11,,013233,40,<timestamp2>,50,1,1,
+0.0.0.12,22,,40,<timestamp3>,50,1,1,three.example.com

+ 3 - 3
src/bin/admin/tests/data/pgsql.lease6_dump_test.reference.csv

@@ -1,4 +1,4 @@
 address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname
-12,21,30,<timestamp>,40,50,IA_TA,60,70,t,t,three.example.com
-11,,30,<timestamp>,40,50,IA_TA,60,70,t,t,
-10,20,30,<timestamp>,40,50,IA_TA,60,70,t,t,one.example.com
+12,21,30,<timestamp3>,40,50,IA_TA,60,70,t,t,three.example.com
+11,,30,<timestamp2>,40,50,IA_TA,60,70,t,t,
+10,20,30,<timestamp1>,40,50,IA_TA,60,70,t,t,one.example.com

+ 8 - 8
src/bin/admin/tests/mysql_tests.sh.in

@@ -299,9 +299,9 @@ mysql_lease4_dump_test() {
 
     # Insert the reference record
     insert_sql="\
-insert into lease4 values(10,20,30,40,0,50,1,1,\"one.example.com\");\
-insert into lease4 values(11,NULL,123,40,0,50,1,1,\"\");\
-insert into lease4 values(12,22,NULL,40,0,50,1,1,\"three.example.com\");"
+insert into lease4 values(10,20,30,40,\"2015-01-01 01:15:30\",50,1,1,\"one.example.com\");\
+insert into lease4 values(11,NULL,123,40,\"2015-02-02 02:30:45\",50,1,1,\"\");\
+insert into lease4 values(12,22,NULL,40,\"2015-03-03 11:01:07\",50,1,1,\"three.example.com\");"
 
     mysql_execute "$insert_sql"
     ERRCODE=$?
@@ -356,13 +356,13 @@ mysql_lease6_dump_test() {
 
     # Insert the reference record
     insert_sql="\
-insert into lease6 values(10,20,30,0,40,50,1,60,70,1,1,\"one.example.com\",80,90,100);\
-insert into lease6 values(11,NULL,30,0,40,50,1,60,70,1,1,\"\",80,90,100);\
-insert into lease6 values(12,21,30,0,40,50,1,60,70,1,1,\"three.example.com\",80,90,100);"
+insert into lease6 values(10,20,30,\"2015-04-04 01:15:30\",40,50,1,60,70,1,1,\"one.example.com\",80,90,100);\
+insert into lease6 values(11,NULL,30,\"2015-05-05 02:30:45\",40,50,1,60,70,1,1,\"\",80,90,100);\
+insert into lease6 values(12,21,30,\"2015-06-06 11:01:07\",40,50,1,60,70,1,1,\"three.example.com\",80,90,100);"
 
     mysql_execute "$insert_sql"
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "insert into lease6 failed, expected exit code %, actual %d"
+    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
@@ -372,7 +372,7 @@ insert into lease6 values(12,21,30,0,40,50,1,60,70,1,1,\"three.example.com\",80,
     # Compare the dump output to reference file, they should be identical
     cmp -s $output_file  $ref_file
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %, actual %d"
+    assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %d, actual %d"
 
     # remove the output file
     rm $output_file

+ 50 - 22
src/bin/admin/tests/pgsql_tests.sh.in

@@ -131,6 +131,20 @@ pgsql_upgrade_test() {
     test_finish 0
 }
 
+# Given a valid timestamp string, returns a timestamp with timezone string
+# for the give time localized by the PostgreSQL server.
+get_local_time() {
+    timestamp="$1"
+
+    # Expiration field is a "timestamp with timezone" so we need a reference
+    # time for the machine/DB this test is running upon.
+    ref_timestamp=`pgsql_execute "SELECT timestamptz '$1';"`
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "reference time query failed for [$timestamp], expected exit code %d, actual %d"
+    echo $ref_timestamp
+}
+
+
 # Test verifies the ability to dump lease4 data to CSV file
 # The dump output file is compared against a reference file.
 # If the dump is successful, the file contents will be the
@@ -172,11 +186,15 @@ pgsql_lease4_dump_test() {
     ERRCODE=$?
     assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
 
+    timestamp1="2015-01-01 01:15:30"
+    timestamp2="2015-02-02 02:30:45"
+    timestamp3="2015-03-03 11:01:07"
+
     # Insert the reference records
     insert_sql="\
-insert into lease4 values(10,E'\\x20',E'\\x30',40,'2001-01-01 00:00:00',50,'t','t','one.example.com');\
-insert into lease4 values(11,'',E'\\x0123',40,'2001-01-01 00:00:00',50,'t','t','');\
-insert into lease4 values(12,E'\\x22','',40,'2001-01-01 00:00:00',50,'t','t','three.example.com');"
+insert into lease4 values(10,E'\\x20',E'\\x30',40,'$timestamp1',50,'t','t','one.example.com');\
+insert into lease4 values(11,'',E'\\x0123',40,'$timestamp2',50,'t','t','');\
+insert into lease4 values(12,E'\\x22','',40,'$timestamp3',50,'t','t','three.example.com');"
 
     pgsql_execute "$insert_sql"
     ERRCODE=$?
@@ -187,17 +205,20 @@ insert into lease4 values(12,E'\\x22','',40,'2001-01-01 00:00:00',50,'t','t','th
     ERRCODE=$?
     assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed,  status code %d"
 
-    # Expiration field is a "timestamp with timezone" so we need a reference
-    # time for the machine/DB this test is running upon.
-    ref_timestamp=`pgsql_execute "SELECT timestamptz '2001-01-01 00:00:00';"`
-    ERRCODE=$?
-    assert_eq 0 $ERRCODE "reference time query failed, expected exit code %d, actual %d"
-    printf "reference timestamp is [$ref_timestamp]\n"
+    # Expiration field is a "timestamp with timezone" so we need a localized reference
+    # times for the machine/DB this test is running upon.
+    local_timestamp1=`get_local_time "$timestamp1"`
+    local_timestamp2=`get_local_time "$timestamp2"`
+    local_timestamp3=`get_local_time "$timestamp3"`
 
     # Create the comparison file by replacing the <timestamp> tags
     # with the local reference timestamp
-    sedstr="'s/<timestamp>/$ref_timestamp/g'"
-    eval sed $sedstr $ref_file >$ref_file_tmp
+    sedstr="\
+sed 's/<timestamp1>/$local_timestamp1/g' $ref_file | \
+sed 's/<timestamp2>/$local_timestamp2/g' | \
+sed 's/<timestamp3>/$local_timestamp3/g' "
+
+    eval $sedstr >$ref_file_tmp
     ERRCODE=$?
     assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"
 
@@ -257,11 +278,15 @@ pgsql_lease6_dump_test() {
     ERRCODE=$?
     assert_eq 0 $ERRCODE "could not create database,  status code %d"
 
+    timestamp1="2015-04-04 01:15:30"
+    timestamp2="2015-02-02 02:30:45"
+    timestamp3="2015-06-06 11:01:07"
+
     # Insert the reference records
     insert_sql="\
-insert into lease6 values(10,E'\\x20',30,'2001-01-01 00:00:00',40,50,1,60,70,'t','t','one.example.com');\
-insert into lease6 values(11,'',30,'2001-01-01 00:00:00',40,50,1,60,70,'t','t','');\
-insert into lease6 values(12,E'\\x21',30,'2001-01-01 00:00:00',40,50,1,60,70,'t','t','three.example.com');"
+insert into lease6 values(10,E'\\x20',30,'$timestamp1',40,50,1,60,70,'t','t','one.example.com');\
+insert into lease6 values(11,'',30,'$timestamp2',40,50,1,60,70,'t','t','');\
+insert into lease6 values(12,E'\\x21',30,'$timestamp3',40,50,1,60,70,'t','t','three.example.com');"
 
     pgsql_execute "$insert_sql"
     ERRCODE=$?
@@ -272,17 +297,20 @@ insert into lease6 values(12,E'\\x21',30,'2001-01-01 00:00:00',40,50,1,60,70,'t'
     ERRCODE=$?
     assert_eq 0 $ERRCODE "kea-admin lease-dump -6 failed, status code %d"
 
-    # Expiration field is a "timestamp with timezone" so we need a reference
-    # time for the machine/DB this test is running upon.
-    ref_timestamp=`pgsql_execute "SELECT timestamptz '2001-01-01 00:00:00';"`
-    ERRCODE=$?
-    assert_eq 0 $ERRCODE "reference time query failed, expected exit code %d, actual %d"
-    printf "reference timestamp is [$ref_timestamp]\n"
+    # Expiration field is a "timestamp with timezone" so we need a localized reference
+    # times for the machine/DB this test is running upon.
+    local_timestamp1=`get_local_time "$timestamp1"`
+    local_timestamp2=`get_local_time "$timestamp2"`
+    local_timestamp3=`get_local_time "$timestamp3"`
 
     # Create the comparison file by replacing the <timestamp> tags
     # with the local reference timestamp
-    sedstr="'s/<timestamp>/$ref_timestamp/g'"
-    eval sed $sedstr $ref_file >$ref_file_tmp
+    sedstr="\
+sed 's/<timestamp1>/$local_timestamp1/g' $ref_file | \
+sed 's/<timestamp2>/$local_timestamp2/g' | \
+sed 's/<timestamp3>/$local_timestamp3/g' "
+
+    eval $sedstr >$ref_file_tmp
     ERRCODE=$?
     assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"