#!/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
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 \
# 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 \
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ˎxampleˌom,0,{ "a": 1, "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ˎxampleˌom,38:30,0,{ "a": 1, "b": 2 },16,0' "${OUTPUT}"
}
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
# 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
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 ]
rm $output_file
fi
- if [ -e $ref_file_tmp ]
- then
- rm $ref_file_tmp
- fi
-
# Let's wipe the whole database
pgsql_wipe
"${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"
"${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
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 ]
rm $output_file
fi
- if [ -e $ref_file_tmp ]
- then
- rm $ref_file_tmp
- fi
-
# Let's wipe the whole database
pgsql_wipe
"${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"
"${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