]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2038] kea-admin lease-dump pgsql tests
authorAndrei Pavel <andrei@isc.org>
Tue, 11 Jan 2022 15:51:17 +0000 (17:51 +0200)
committerAndrei Pavel <andrei@isc.org>
Tue, 18 Jan 2022 20:45:25 +0000 (22:45 +0200)
src/bin/admin/tests/data/Makefile.am
src/bin/admin/tests/data/lease4_dump_test.reference.csv [moved from src/bin/admin/tests/data/mysql.lease4_dump_test.reference.csv with 100% similarity]
src/bin/admin/tests/data/lease6_dump_test.reference.csv [moved from src/bin/admin/tests/data/mysql.lease6_dump_test.reference.csv with 100% similarity]
src/bin/admin/tests/data/pgsql.lease4_dump_test.reference.csv [deleted file]
src/bin/admin/tests/data/pgsql.lease6_dump_test.reference.csv [deleted file]
src/bin/admin/tests/mysql_tests.sh.in
src/bin/admin/tests/pgsql_tests.sh.in

index 1e3d7b32075717a91554490f68cef731f27e4bd9..bf5af824bdf9cdab6b4b94974e1a561886d69fdf 100644 (file)
@@ -1,7 +1,6 @@
-EXTRA_DIST = mysql.lease4_dump_test.reference.csv \
-    mysql.lease6_dump_test.reference.csv \
-    pgsql.lease4_dump_test.reference.csv \
-    pgsql.lease6_dump_test.reference.csv \
+EXTRA_DIST = \
+    lease4_dump_test.reference.csv \
+    lease6_dump_test.reference.csv \
     cql.lease4_dump_test.reference.csv \
     cql.lease6_dump_test.reference.csv \
     cql.subnet_id_test.reference.csv \
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
deleted file mode 100644 (file)
index b194fe6..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname,state,user_context
-0.0.0.10,20,30,40,<timestamp1>,50,1,1,one.example.com,default,
-0.0.0.11,,013233,40,<timestamp2>,50,1,1,,declined,
-0.0.0.12,22,,40,<timestamp3>,50,1,1,three.example.com,expired-reclaimed,
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
deleted file mode 100644 (file)
index 851c174..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,state,hwaddr,hwtype,hwaddr_source,user_context
-10,20,30,<timestamp1>,40,50,IA_TA,60,70,1,1,one.example.com,default,80,90,HWADDR_SOURCE_REMOTE_ID,
-11,,30,<timestamp2>,40,50,IA_TA,60,70,1,1,,declined,80,90,HWADDR_SOURCE_RAW,
-12,21,30,<timestamp3>,40,50,IA_TA,60,70,1,1,three.example.com,expired-reclaimed,80,90,HWADDR_SOURCE_DUID,
index 8a5dd45054e76766aeeedf11a68e6b1f4956cf6f..8f418a0bcdae39dc1bcd5aeccfed7343aaeec6f1 100644 (file)
@@ -977,7 +977,7 @@ mysql_lease4_dump_test() {
     output_file="$output_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/lease4_dump_test.reference.csv"
 
     # wipe out any residuals from prior failed runs
     if [ -e $output_file ]
@@ -1036,7 +1036,7 @@ mysql_lease6_dump_test() {
     output_file="$output_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/lease6_dump_test.reference.csv"
 
     # wipe out any residuals from prior failed runs
     if [ -e $output_file ]
index 640219de810b54c87e8a1d55d56debfa3f6cb8ab..cc9d3233df5e546eb68eac1ed6c40832850e87f3 100644 (file)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-# Copyright (C) 2015-2021 Internet Systems Consortium, Inc. ("ISC")
+# Copyright (C) 2015-2022 Internet Systems Consortium, Inc. ("ISC")
 #
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -233,15 +233,19 @@ pgsql_upgrade_2_0_to_3_0() {
     run_command \
         pgsql_execute "\sf lease4DumpData"
     assert_eq 0 "${EXIT_CODE}" "\sf of lease4DumpData failed. (expected status code %d, returned %d)"
-    count=$(echo "${OUTPUT}" | grep -Fci 'order by l.address') || true
-    assert_eq 1 "${count}" "lease4DumpData is missing order by clause"
+    count=$(echo "${OUTPUT}" | grep -Eci 'order by [a-z]*[\.]?address') || true
+    assert_eq 1 "${count}" "lease4DumpData is missing order by clause. (expected count %d, returned %d)"
 
     # Added hwaddr columns to lease6DumpHeader
     run_command \
         pgsql_execute "select lease6DumpHeader from lease6DumpHeader();"
     assert_eq 0 "${EXIT_CODE}" "function lease6DumpHeader() broken or missing. (expected status code %d, returned %d)"
-    count=$(echo "${OUTPUT}" | grep -Fci 'hwaddr,hwtype,hwaddr_source') || true
-    assert_eq 1 "${count}" "lease6DumpHeader is missing hwaddr columns"
+    count=$(echo "${OUTPUT}" | grep -Fci 'hwaddr') || true
+    assert_eq 1 "${count}" "lease6DumpHeader is missing the hwaddr column"
+    count=$(echo "${OUTPUT}" | grep -Fci 'hwtype') || true
+    assert_eq 1 "${count}" "lease6DumpHeader is missing the hwtype column"
+    count=$(echo "${OUTPUT}" | grep -Fci 'hwaddr_source') || true
+    assert_eq 1 "${count}" "lease6DumpHeader is missing the hwaddr_source column"
 
     # Added hwaddr columns to lease6DumpData
     run_command \
@@ -250,10 +254,10 @@ pgsql_upgrade_2_0_to_3_0() {
 
     # Added order by clause to lease6DumpData
     run_command \
-        pgsql_execute "\sf lease4DumpData"
-    assert_eq 0 "${EXIT_CODE}" "\sf of lease4DumpData failed. (expected status code %d, returned %d)"
-    count=$(echo "${OUTPUT}" | grep -Fci 'order by l.address') || true
-    assert_eq 1 "${count}" "lease4DumpData is missing order by clause"
+        pgsql_execute "\sf lease6DumpData"
+    assert_eq 0 "${EXIT_CODE}" "\sf of lease6DumpData failed. (expected status code %d, returned %d)"
+    count=$(echo "${OUTPUT}" | grep -Eci 'order by [a-z]*[\.]?address') || true
+    assert_eq 1 "${count}" "lease6DumpData is missing order by clause. (expected count %d, returned %d)"
 
     # lease_hardware_source should have row for source = 0
     run_command \
@@ -341,6 +345,64 @@ pgsql_upgrade_7_0_to_8_0() {
     run_command \
         pgsql_execute "select preferred_lifetime, min_preferred_lifetime, max_preferred_lifetime from dhcp6_client_class;"
     assert_eq 0 "${EXIT_CODE}" "dhcp6_client_class is missing preferred lifetime column(s). (expected status code %d, returned %d)"
+
+    # Check the output of colonSeparatedHex().
+    run_command \
+        pgsql_execute "SELECT colonSeparatedHex('f123456789');"
+    assert_eq 0 "${EXIT_CODE}" 'colonSeparatedHex() failed, expected exit code %d, actual %d'
+    assert_str_eq 'f1:23:45:67:89' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT colonSeparatedHex('');"
+    assert_eq 0 "${EXIT_CODE}" 'colonSeparatedHex() failed, expected exit code %d, actual %d'
+    assert_str_eq '' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT colonSeparatedHex('f');"
+    assert_eq 0 "${EXIT_CODE}" 'colonSeparatedHex() failed, expected exit code %d, actual %d'
+    assert_str_eq '0f' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT colonSeparatedHex('f1');"
+    assert_eq 0 "${EXIT_CODE}" 'colonSeparatedHex() failed, expected exit code %d, actual %d'
+    assert_str_eq 'f1' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT colonSeparatedHex('f12');"
+    assert_eq 0 "${EXIT_CODE}" 'colonSeparatedHex() failed, expected exit code %d, actual %d'
+    assert_str_eq '0f:12' "${OUTPUT}"
+
+    # Check lease4Dump*().
+    run_command \
+        pgsql_execute "INSERT INTO lease4 VALUES(10,E'\\\\x3230',E'\\\\x3330',40,TO_TIMESTAMP(1678900000),50,'t','t','one,example,com',0,'{ \"a\": 1, \"b\": 2 }');"
+    assert_eq 0 "${EXIT_CODE}" 'INSERT INTO lease4 failed, expected exit code %d, actual %d'
+    assert_str_eq '' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT * FROM lease4DumpHeader();"
+    assert_eq 0 "${EXIT_CODE}" 'lease4DumpHeader() failed, expected exit code %d, actual %d'
+    assert_str_eq 'address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname,state,user_context' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT * FROM lease4DumpData();" --field-separator=','
+    assert_eq 0 "${EXIT_CODE}" 'lease4DumpData() failed, expected exit code %d, actual %d'
+    assert_str_eq '0.0.0.10,32:30,33:30,40,1678900000,50,1,1,one&#x2cexample&#x2ccom,0,{ "a": 1&#x2c "b": 2 }' "${OUTPUT}"
+
+    # Check lease6Dump*().
+    run_command \
+        pgsql_execute "INSERT INTO lease6 VALUES('::10',E'\\\\x3230',30,TO_TIMESTAMP(1678900000),40,50,1,60,70,'t','t','one,example,com',0,E'\\\\x3830',16,0,'{ \"a\": 1, \"b\": 2 }');"
+    assert_eq 0 "${EXIT_CODE}" 'INSERT INTO lease6 failed, expected exit code %d, actual %d'
+    assert_str_eq '' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT * FROM lease6DumpHeader();"
+    assert_eq 0 "${EXIT_CODE}" 'lease6DumpHeader() failed, expected exit code %d, actual %d'
+    assert_str_eq 'address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname,hwaddr,state,user_context,hwtype,hwaddr_source' "${OUTPUT}"
+
+    run_command \
+        pgsql_execute "SELECT * FROM lease6DumpData();" --field-separator=','
+    assert_eq 0 "${EXIT_CODE}" 'lease6DumpData() failed, expected exit code %d, actual %d'
+    assert_str_eq '::10,32:30,30,1678900000,40,50,1,60,70,1,1,one&#x2cexample&#x2ccom,38:30,0,{ "a": 1&#x2c "b": 2 },16,0' "${OUTPUT}"
 }
 
 pgsql_upgrade_test() {
@@ -358,6 +420,10 @@ pgsql_upgrade_test() {
         "${kea_admin}" db-upgrade pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}"
     assert_eq 0 "${EXIT_CODE}" "db-upgrade failed, expected exit code: %d, actual: %d"
 
+    # Verify upgraded schema reports version 8.0.
+    version=$("${kea_admin}" db-version pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}")
+    assert_str_eq "8.0" "${version}" 'Expected kea-admin to return %s, returned value was %s'
+
     # Check 1.0 to 2.0 upgrade
     pgsql_upgrade_1_0_to_2_0
 
@@ -376,30 +442,12 @@ pgsql_upgrade_test() {
     # Check 7.0 to 8.0 upgrade
     pgsql_upgrade_7_0_to_8_0
 
-    # Verify upgraded schema reports version 8.0.
-    version=$("${kea_admin}" db-version pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}")
-    assert_str_eq "8.0" "${version}" "Expected kea-admin to return %s, returned value was %s"
-
     # Let's wipe the whole database
     pgsql_wipe
 
     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.
-    run_command \
-        pgsql_execute "select timestamptz '$1';"
-    assert_eq 0 "${EXIT_CODE}" "reference time query failed for [$timestamp], expected exit code %d, actual %d"
-    echo "${OUTPUT}"
-}
-
-
 # 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
@@ -421,8 +469,7 @@ pgsql_lease4_dump_test() {
 
     output_file="$output_dir/data/pgsql.lease4_dump_test.output.csv"
 
-    ref_file="$test_dir/data/pgsql.lease4_dump_test.reference.csv"
-    ref_file_tmp="$output_dir/data/pgsql.lease4_dump_test.reference.csv.tmp"
+    ref_file="$test_dir/data/lease4_dump_test.reference.csv"
 
     # wipe out any residuals from prior failed runs
     if [ -e $output_file ]
@@ -430,11 +477,6 @@ pgsql_lease4_dump_test() {
         rm $output_file
     fi
 
-    if [ -e $ref_file_tmp ]
-    then
-        rm $ref_file_tmp
-    fi
-
     # Let's wipe the whole database
     pgsql_wipe
 
@@ -443,15 +485,13 @@ pgsql_lease4_dump_test() {
         "${kea_admin}" db-init pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}"
     assert_eq 0 "${EXIT_CODE}" "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 the reference records. Normally, for the bytea values, you would have two backslashes.
+    # Because shell evaluates the double quoted string one more time, they need to be doubled.
+    # Otherwise, the value is interpreted as ASCII instead of raw bytes.
     insert_sql="\
-insert into lease4 values(10,E'\\x20',E'\\x30',40,'$timestamp1',50,'t','t','one.example.com', 0, '');\
-insert into lease4 values(11,'',E'\\x0123',40,'$timestamp2',50,'t','t','', 1, '');\
-insert into lease4 values(12,E'\\x22','',40,'$timestamp3',50,'t','t','three.example.com', 2, '');"
+insert into lease4 values(10,E'\\\\x3230',E'\\\\x3330',40,TO_TIMESTAMP(1642000000),50,'t','t','one.example.com',0,'');\
+insert into lease4 values(11,'',E'\\\\x313233',40,TO_TIMESTAMP(1643210000),50,'t','t','',1,'{}');\
+insert into lease4 values(12,E'\\\\x3232','',40,TO_TIMESTAMP(1643212345),50,'t','t','three,example,com',2,'{\"a\":1,\"b\":\"c\"}');"
 
     run_command \
         pgsql_execute "$insert_sql"
@@ -462,32 +502,13 @@ insert into lease4 values(12,E'\\x22','',40,'$timestamp3',50,'t','t','three.exam
         "${kea_admin}" lease-dump pgsql -4 -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" -o $output_file
     assert_eq 0 "${EXIT_CODE}" "kea-admin lease-dump -4 failed,  status code %d"
 
-    # 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
-    sed_command="\
-s/<timestamp1>/${local_timestamp1}/g;\
-s/<timestamp2>/${local_timestamp2}/g; \
-s/<timestamp3>/${local_timestamp3}/g"
-
-    run_command \
-        sed "${sed_command}" "${ref_file}"
-    printf '%s\n' "${OUTPUT}" > "${ref_file_tmp}"
-    assert_eq 0 "${EXIT_CODE}" "timestamp replacement failed, expected exit code %d, actual %d"
-
     # Compare the dump output to reference file, they should be identical
     run_command \
-        cmp -s "${output_file}" "${ref_file_tmp}"
-    assert_eq 0 "${EXIT_CODE}" "dump file does not match reference file, expected exit code %d, actual %d"
+        cmp -s "${output_file}" "${ref_file}"
+    assert_eq 0 "${EXIT_CODE}" "dump file does not match reference file, expected exit code %d, actual %d, diff:\n$(diff ${ref_file} ${output_file})"
 
     # Remove the output file and temporary reference file
     rm $output_file
-    rm $ref_file_tmp
 
     # Let's wipe the whole database
     pgsql_wipe
@@ -516,8 +537,7 @@ pgsql_lease6_dump_test() {
 
     output_file="$output_dir/data/pgsql.lease6_dump_test.output.csv"
 
-    ref_file="$test_dir/data/pgsql.lease6_dump_test.reference.csv"
-    ref_file_tmp="$output_dir/data/pgsql.lease6_dump_test.reference.csv.tmp"
+    ref_file="$test_dir/data/lease6_dump_test.reference.csv"
 
     # wipe out any residuals from prior failed runs
     if [ -e $output_file ]
@@ -525,11 +545,6 @@ pgsql_lease6_dump_test() {
         rm $output_file
     fi
 
-    if [ -e $ref_file_tmp ]
-    then
-        rm $ref_file_tmp
-    fi
-
     # Let's wipe the whole database
     pgsql_wipe
 
@@ -538,15 +553,13 @@ pgsql_lease6_dump_test() {
         "${kea_admin}" db-init pgsql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}"
     assert_eq 0 "${EXIT_CODE}" "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 the reference records. Normally, for the bytea values, you would have two backslashes.
+    # Because shell evaluates the double quoted string one more time, they need to be doubled.
+    # Otherwise, the value is interpreted as ASCII instead of raw bytes.
     insert_sql="\
-insert into lease6 values(10,E'\\x20',30,'$timestamp1',40,50,1,60,70,'t','t','one.example.com', 0,decode('80','hex'),90,16,'');\
-insert into lease6 values(11,'',30,'$timestamp2',40,50,1,60,70,'t','t','', 1,decode('80','hex'),90,1,'');\
-insert into lease6 values(12,E'\\x21',30,'$timestamp3',40,50,1,60,70,'t','t','three.example.com', 2,decode('80','hex'),90,4,'');"
+insert into lease6 values('::10',E'\\\\x3230',30,TO_TIMESTAMP(1642000000),40,50,1,60,70,'t','t','one.example.com',0,decode(encode('80','hex'),'hex'),90,16,''); \
+insert into lease6 values('::11','',30,TO_TIMESTAMP(1643210000),40,50,1,60,70,'t','t','',1,decode(encode('80','hex'),'hex'),90,1,'{}'); \
+insert into lease6 values('::12',E'\\\\x3231',30,TO_TIMESTAMP(1643212345),40,50,1,60,70,'t','t','three,example,com',2,decode(encode('80','hex'),'hex'),90,4,'{\"a\":1,\"b\":\"c\"}');"
 
     run_command \
         pgsql_execute "$insert_sql"
@@ -557,32 +570,13 @@ insert into lease6 values(12,E'\\x21',30,'$timestamp3',40,50,1,60,70,'t','t','th
         "${kea_admin}" lease-dump pgsql -6 -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" -o $output_file
     assert_eq 0 "${EXIT_CODE}" "kea-admin lease-dump -6 failed, status code %d"
 
-    # 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
-    sed_command="\
-s/<timestamp1>/${local_timestamp1}/g;\
-s/<timestamp2>/${local_timestamp2}/g; \
-s/<timestamp3>/${local_timestamp3}/g"
-
-    run_command \
-        sed "${sed_command}" "${ref_file}"
-    printf '%s\n' "${OUTPUT}" > "${ref_file_tmp}"
-    assert_eq 0 "${EXIT_CODE}" "timestamp replacement failed, expected exit code %d, actual %d"
-
     # Compare the dump output to reference file, they should be identical
     run_command \
-        cmp -s "${output_file}" "${ref_file_tmp}"
-    assert_eq 0 "${EXIT_CODE}" "dump file does not match reference file"
+        cmp -s "${output_file}" "${ref_file}"
+    assert_eq 0 "${EXIT_CODE}" "dump file does not match reference file, expected exit code %d, actual %d, diff:\n$(diff ${ref_file} ${output_file})"
 
     # Remove the output file and temporary reference file
     rm $output_file
-    rm $ref_file_tmp
 
     # Let's wipe the whole database
     pgsql_wipe