Browse Source

[3884] Added unit tests for PostgreSQL lease-dump

src/bin/admin/tests/mysql_tests.sh.in
    added missing assert_eq placeholders

src/bin/admin/tests/pgsql_tests.sh.in
    pgsql_lease4_dump_test() - new function which tests dumping v4 leases
    pgsql_lease4_dump_test() - new function which tests dumping v6 leases

Added the following test reference data files:
src/bin/admin/tests/data/pgsql.lease4_dump_test.reference.csv
src/bin/admin/tests/data/pgsql.lease6_dump_test.reference.csv
Thomas Markwalder 10 years ago
parent
commit
0a9e9d2920

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

@@ -0,0 +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

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

@@ -0,0 +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

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

@@ -274,8 +274,8 @@ mysql_lease4_dump_test() {
 
 
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
-    output_file="$test_dir/data/lease4_dump_test.output.csv"
-    tmp_file="$test_dir/data/lease4_dump_test.output.csv.tmp"
+    output_file="$test_dir/data/mysql.lease4_dump_test.output.csv"
+    tmp_file="$output_file.tmp"
     ref_file="$test_dir/data/mysql.lease4_dump_test.reference.csv"
     ref_file="$test_dir/data/mysql.lease4_dump_test.reference.csv"
 
 
     # wipe out any residuals from prior failed runs
     # wipe out any residuals from prior failed runs
@@ -295,7 +295,7 @@ mysql_lease4_dump_test() {
     # Ok, now let's initalize the database
     # Ok, now let's initalize the database
     ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
     ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
     ERRCODE=$?
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "could not create database,  status code %d"
+    assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
 
 
     # Insert the reference record
     # Insert the reference record
     insert_sql="\
     insert_sql="\
@@ -305,17 +305,17 @@ insert into lease4 values(12,22,NULL,40,0,50,1,1,\"three.example.com\");"
 
 
     mysql_execute "$insert_sql"
     mysql_execute "$insert_sql"
     ERRCODE=$?
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "insert into lease4 failed, status code %d"
+    assert_eq 0 $ERRCODE "insert into lease4 failed, expected exit code %d, actual %d"
 
 
     # Dump lease4 to output_file
     # Dump lease4 to output_file
     ${keaadmin} lease-dump mysql -4 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
     ${keaadmin} lease-dump mysql -4 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
     ERRCODE=$?
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed,  status code %d"
+    assert_eq 0 $ERRCODE "kea-admin lease-dump -4 failed, expected exit code %d, actual %d"
 
 
     # Compare the dump output to reference file, they should be identical
     # Compare the dump output to reference file, they should be identical
     cmp -s $output_file  $ref_file
     cmp -s $output_file  $ref_file
     ERRCODE=$?
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "dump file does not match reference file"
+    assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %d, actual %d"
 
 
     # remove the output file
     # remove the output file
     rm $output_file
     rm $output_file
@@ -331,8 +331,8 @@ mysql_lease6_dump_test() {
 
 
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     test_dir="@abs_top_srcdir@/src/bin/admin/tests"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
     script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
-    output_file="$test_dir/data/lease6_dump_test.output.csv"
-    tmp_file="$test_dir/data/lease6_dump_test.output.csv.tmp"
+    output_file="$test_dir/data/mysql.lease6_dump_test.output.csv"
+    tmp_file="$output_file.tmp"
     ref_file="$test_dir/data/mysql.lease6_dump_test.reference.csv"
     ref_file="$test_dir/data/mysql.lease6_dump_test.reference.csv"
 
 
     # wipe out any residuals from prior failed runs
     # wipe out any residuals from prior failed runs
@@ -352,7 +352,7 @@ mysql_lease6_dump_test() {
     # Ok, now let's initalize the database
     # Ok, now let's initalize the database
     ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
     ${keaadmin} lease-init mysql -u $db_user -p $db_password -n $db_name -d $script_dir
     ERRCODE=$?
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "could not create database,  status code %d"
+    assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
 
 
     # Insert the reference record
     # Insert the reference record
     insert_sql="\
     insert_sql="\
@@ -362,7 +362,7 @@ insert into lease6 values(12,21,30,0,40,50,1,60,70,1,1,\"three.example.com\",80,
 
 
     mysql_execute "$insert_sql"
     mysql_execute "$insert_sql"
     ERRCODE=$?
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "insert into lease6 failed, status code %d"
+    assert_eq 0 $ERRCODE "insert into lease6 failed, expected exit code %, actual %d"
 
 
     # Dump lease4 to output_file
     # 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
     ${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
     # Compare the dump output to reference file, they should be identical
     cmp -s $output_file  $ref_file
     cmp -s $output_file  $ref_file
     ERRCODE=$?
     ERRCODE=$?
-    assert_eq 0 $ERRCODE "dump file does not match reference file"
+    assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %, actual %d"
 
 
     # remove the output file
     # remove the output file
     rm $output_file
     rm $output_file

+ 173 - 0
src/bin/admin/tests/pgsql_tests.sh.in

@@ -45,6 +45,7 @@ pgsql_wipe() {
     # Make a set of drop commands, one for each table owned by keatest
     # Make a set of drop commands, one for each table owned by keatest
     RESULT=`pgsql_execute "SELECT 'drop table if exists '||t.tablename || ' cascade;' as dcmd FROM pg_catalog.pg_tables t WHERE t.tableowner = 'keatest';"`
     RESULT=`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, expected exit code: %d, actual: %d"
     assert_eq 0 $? "pgsql_wipe select failed, expected exit code: %d, actual: %d"
+
     # Now execute the set of drop commands from the result set returned
     # Now execute the set of drop commands from the result set returned
     RESULT=`pgsql_execute "$RESULT"`
     RESULT=`pgsql_execute "$RESULT"`
     assert_eq 0 $? "pgsql_wipe drop failed, expected exit code: %d, actual: %d"
     assert_eq 0 $? "pgsql_wipe drop failed, expected exit code: %d, actual: %d"
@@ -130,6 +131,178 @@ pgsql_upgrade_test() {
     test_finish 0
     test_finish 0
 }
 }
 
 
+# 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
+# same.  Note that the expire field in the lease4 table
+# is of data type "timestamp with timezone". This means that
+# the dumped file content is dependent upon the timezone
+# setting the PostgreSQL server is using.  To account for
+# this the reference data contains a tag, "<timestamp>"
+# where the expire column's data would normally be. This
+# tag is replaced during text execution with a value
+# determined by querying the PostgreSQL server.  This
+# updated reference data is captured in a temporary file
+# which is used for the actual comparison.
+pgsql_lease4_dump_test() {
+    test_start "pgsql.lease4_dump_test"
+
+    test_dir="@abs_top_srcdir@/src/bin/admin/tests"
+    script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
+    output_file="$test_dir/data/pgsql.lease4_dump_test.output.csv"
+    ref_file="$test_dir/data/pgsql.lease4_dump_test.reference.csv"
+    ref_file_tmp=$ref_file.tmp
+
+    # wipe out any residuals from prior failed runs
+    if [ -e $output_file ]
+    then
+        rm $output_file
+    fi
+
+    if [ -e $ref_file_tmp ]
+    then
+        rm $ref_file_tmp
+    fi
+
+    # Let's wipe the whole database
+    pgsql_wipe
+
+    # Ok, now let's initalize the database
+    ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $script_dir
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "could not create database, expected exit code %d, actual %d"
+
+    # 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');"
+
+    pgsql_execute "$insert_sql"
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "insert into lease4 failed, expected exit code %d, actual %d"
+
+    # Dump lease4 to output_file
+    ${keaadmin} lease-dump pgsql -4 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
+    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"
+
+    # 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
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"
+
+    # Compare the dump output to reference file, they should be identical
+    cmp -s $output_file  $ref_file_tmp
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "dump file does not match reference file, expected exit code %d, actual %d"
+
+    # Remove the output file and temporary reference file
+    rm $output_file
+    rm $ref_file_tmp
+
+    # Let's wipe the whole database
+    pgsql_wipe
+
+    test_finish 0
+}
+
+# Test verifies the ability to dump lease6 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
+# same.  Note that the expire field in the lease6 table
+# is of data type "timestamp with timezone". This means that
+# the dumped file content is dependent upon the timezone
+# setting the PostgreSQL server is using.  To account for
+# this the reference data contains a tag, "<timestamp>"
+# where the expire column's data would normally be. This
+# tag is replaced during text execution with a value
+# determined by querying the PostgreSQL server.  This
+# updated reference data is captured in a temporary file
+# which is used for the actual comparison.
+pgsql_lease6_dump_test() {
+    test_start "pgsql.lease6_dump_test"
+
+    test_dir="@abs_top_srcdir@/src/bin/admin/tests"
+    script_dir="@abs_top_srcdir@/src/bin/admin/scripts"
+    output_file="$test_dir/data/pgsql.lease6_dump_test.output.csv"
+    ref_file="$test_dir/data/pgsql.lease6_dump_test.reference.csv"
+    ref_file_tmp=$ref_file.tmp
+
+    # wipe out any residuals from prior failed runs
+    if [ -e $output_file ]
+    then
+        rm $output_file
+    fi
+
+    if [ -e $ref_file_tmp ]
+    then
+        rm $ref_file_tmp
+    fi
+
+    # Let's wipe the whole database
+    pgsql_wipe
+
+    # Ok, now let's initalize the database
+    ${keaadmin} lease-init pgsql -u $db_user -p $db_password -n $db_name -d $script_dir
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "could not create database,  status code %d"
+
+    # 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');"
+
+    pgsql_execute "$insert_sql"
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "insert into lease6 failed, status code %d"
+
+    # Dump lease6 to output_file
+    ${keaadmin} lease-dump pgsql -6 -u $db_user -p $db_password -n $db_name -d $script_dir -o $output_file
+    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"
+
+    # 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
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "timestamp replacement failed, expected exit code %d, actual %d"
+
+    # Compare the dump output to reference file, they should be identical
+    cmp -s $output_file  $ref_file_tmp
+    ERRCODE=$?
+    assert_eq 0 $ERRCODE "dump file does not match reference file"
+
+    # Remove the output file and temporary reference file
+    rm $output_file
+    rm $ref_file_tmp
+
+    # Let's wipe the whole database
+    pgsql_wipe
+
+    test_finish 0
+}
+
 pgsql_lease_init_test
 pgsql_lease_init_test
 pgsql_lease_version_test
 pgsql_lease_version_test
 pgsql_upgrade_test
 pgsql_upgrade_test
+pgsql_lease4_dump_test
+pgsql_lease6_dump_test