From d6ec7d8068dd88e381efd66a33d85e74645ec66b Mon Sep 17 00:00:00 2001 From: Thomas Markwalder Date: Mon, 15 Jun 2015 11:23:37 -0400 Subject: [PATCH] [3884] Addressed review comments Minor cleanup. Lease dump tests for both MySQL and PostgreSQL now use multiple timestamps values. --- doc/guide/admin.xml | 38 +++++++--- src/bin/admin/admin-utils.sh | 7 +- .../data/mysql.lease4_dump_test.reference.csv | 6 +- .../data/mysql.lease6_dump_test.reference.csv | 6 +- .../data/pgsql.lease4_dump_test.reference.csv | 6 +- .../data/pgsql.lease6_dump_test.reference.csv | 6 +- src/bin/admin/tests/mysql_tests.sh.in | 16 ++--- src/bin/admin/tests/pgsql_tests.sh.in | 72 +++++++++++++------ 8 files changed, 101 insertions(+), 56 deletions(-) diff --git a/doc/guide/admin.xml b/doc/guide/admin.xml index 5b0cefbce7..92f014e233 100644 --- a/doc/guide/admin.xml +++ b/doc/guide/admin.xml @@ -366,12 +366,14 @@ $ - 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 kea-admin 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: $ psql -d database-name -U user-name -f path-to-kea/share/kea/scripts/pgsql/dhcpdb_create.pgsql Password for user user-name: @@ -434,12 +436,28 @@ host database-name user-name Initialize the PostgreSQL Database Using kea-admin - - Support for PostgreSQL in kea-admin is - currently not implemented. + If you elected not to create the tables manually, you can do + so now by running the kea-admin tool: + +$ kea-admin lease-init pgsql -u database-user -p database-password -n database-name + + Do not do this if you already created the tables in manually. + kea-admin 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 kea-admin.) + + +
+ Upgrading a PostgreSQL Database from an Earlier Version of Kea + + Currently, PostgreSQL only supports Kea schema version 1.0 so no upgrades + are available. As upgrades become available, kea-admin + will support them. -
diff --git a/src/bin/admin/admin-utils.sh b/src/bin/admin/admin-utils.sh index c48f50ae71..a7acc8b404 100644 --- a/src/bin/admin/admin-utils.sh +++ b/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 $? diff --git a/src/bin/admin/tests/data/mysql.lease4_dump_test.reference.csv b/src/bin/admin/tests/data/mysql.lease4_dump_test.reference.csv index e09be378cf..a2917b7d9a 100644 --- a/src/bin/admin/tests/data/mysql.lease4_dump_test.reference.csv +++ b/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 diff --git a/src/bin/admin/tests/data/mysql.lease6_dump_test.reference.csv b/src/bin/admin/tests/data/mysql.lease6_dump_test.reference.csv index 7d9f6f4a58..31a6c3f42a 100644 --- a/src/bin/admin/tests/data/mysql.lease6_dump_test.reference.csv +++ b/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 diff --git a/src/bin/admin/tests/data/pgsql.lease4_dump_test.reference.csv b/src/bin/admin/tests/data/pgsql.lease4_dump_test.reference.csv index 87bc6ccad9..310a4662d6 100644 --- a/src/bin/admin/tests/data/pgsql.lease4_dump_test.reference.csv +++ b/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,,50,1,1,one.example.com -0.0.0.11,,013233,40,,50,1,1, -0.0.0.12,22,,40,,50,1,1,three.example.com +0.0.0.10,20,30,40,,50,1,1,one.example.com +0.0.0.11,,013233,40,,50,1,1, +0.0.0.12,22,,40,,50,1,1,three.example.com diff --git a/src/bin/admin/tests/data/pgsql.lease6_dump_test.reference.csv b/src/bin/admin/tests/data/pgsql.lease6_dump_test.reference.csv index d5d7d72282..346c58c86c 100644 --- a/src/bin/admin/tests/data/pgsql.lease6_dump_test.reference.csv +++ b/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,,40,50,IA_TA,60,70,t,t,three.example.com -11,,30,,40,50,IA_TA,60,70,t,t, -10,20,30,,40,50,IA_TA,60,70,t,t,one.example.com +12,21,30,,40,50,IA_TA,60,70,t,t,three.example.com +11,,30,,40,50,IA_TA,60,70,t,t, +10,20,30,,40,50,IA_TA,60,70,t,t,one.example.com diff --git a/src/bin/admin/tests/mysql_tests.sh.in b/src/bin/admin/tests/mysql_tests.sh.in index 16099d0e3f..0ce5bf22ff 100644 --- a/src/bin/admin/tests/mysql_tests.sh.in +++ b/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 diff --git a/src/bin/admin/tests/pgsql_tests.sh.in b/src/bin/admin/tests/pgsql_tests.sh.in index a4bf9b0c4a..2335b84b9e 100644 --- a/src/bin/admin/tests/pgsql_tests.sh.in +++ b/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 tags # with the local reference timestamp - sedstr="'s//$ref_timestamp/g'" - eval sed $sedstr $ref_file >$ref_file_tmp + sedstr="\ +sed 's//$local_timestamp1/g' $ref_file | \ +sed 's//$local_timestamp2/g' | \ +sed 's//$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 tags # with the local reference timestamp - sedstr="'s//$ref_timestamp/g'" - eval sed $sedstr $ref_file >$ref_file_tmp + sedstr="\ +sed 's//$local_timestamp1/g' $ref_file | \ +sed 's//$local_timestamp2/g' | \ +sed 's//$local_timestamp3/g' " + + eval $sedstr >$ref_file_tmp ERRCODE=$? assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"