diff --git a/ChangeLog b/ChangeLog index 57d6e518efeaa0d90c6b26333b29d7642581273b..0a266dee191af92f952a4a127c60c6c207802d74 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2022. [func] andrei, djt + kea-admin lease-upload will now call the lease file cleanup (LFC) + process to clean up entries with duplicate addresses in the input + CSV file to avoid a conflict error when inserting the leases in + the database. kea-admin also doesn't ask for input on + non-interactive shells anymore and it gained a new -y|--yes flag + that enables automatic overwriting of any file that it writes to, + when dumping or uploading leases. + (Gitlab #2293) + 2021. [build] razvan Library version numbers bumped for Kea 2.1.6 development version. diff --git a/src/bin/admin/kea-admin.in b/src/bin/admin/kea-admin.in index d374d699c67ad0e406d84eb56f47b2c7230c73ef..7e4388b2abb4173eaf51684610468c98d5909757 100644 --- a/src/bin/admin/kea-admin.in +++ b/src/bin/admin/kea-admin.in @@ -31,26 +31,36 @@ # used. set -eu -# Get the location of the kea-admin scripts +# Shell ${variables} derived from autoconf @variables@. Some depend on others, so mind the order. prefix="@prefix@" export prefix +exec_prefix="@exec_prefix@" +export exec_prefix SCRIPTS_DIR_DEFAULT="@datarootdir@/@PACKAGE@/scripts" scripts_dir="${SCRIPTS_DIR_DEFAULT}" VERSION="@PACKAGE_VERSION@" +assume_yes=0 + # lease dump parameters dhcp_version=0 dump_file="" dump_qry="" -# Include utilities. Use installed version if available and -# use build version if it isn't. -if [ -e @datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh ]; then +# Include the installed admin-utils.sh if available. Fallback to sources otherwise. +if test -d "@datarootdir@/@PACKAGE_NAME@"; then . "@datarootdir@/@PACKAGE_NAME@/scripts/admin-utils.sh" else . "@abs_top_srcdir@/src/bin/admin/admin-utils.sh" fi +# Find the installed kea-lfc if available. Fallback to sources otherwise. +if test -d "@sbindir@"; then + kea_lfc="@sbindir@/kea-lfc" +else + kea_lfc="@abs_top_srcdir@/src/bin/lfc/kea-lfc" +fi + # Prints out usage version. usage() { printf \ @@ -86,6 +96,7 @@ PARAMETERS: Parameters are optional in general, but may be required -d or --directory - path to upgrade scripts (default: %s) -v or --version - print kea-admin version and quit. -x or --extra - specifies extra argument(s) to pass to the database command + -y or --yes - assume yes on overwriting temporary files Parameters specific to lease-dump, lease-upload: -4 to dump IPv4 leases to file @@ -397,16 +408,38 @@ pgsql_upgrade() { exit 0 } +# Remove a file if it exists +remove_file () { + local file="${1}" + if [ -e "${file}" ] + then + log_info "Removing file ${file}..." + rm -f "${file}" + fi +} + # Utility function which tests if the given file exists and # if so notifies the user and provides them the opportunity # to abort the current command. check_file_overwrite () { local file="${1}" - if [ -e "${file}" ] + if [ $assume_yes -eq 1 ] + then + remove_file "${file}" + elif [ -e "${file}" ] then echo "Output file, $file, exists and will be overwritten." - echo "Do you wish to continue? (y/n)" - read -r ans + echo "Do you wish to continue? (y/N)" + + # Ask for an answer only on an interactive shell to prevent blocking in + # automated or non-interactive scenarios where the answer defaults to no. + if test -t 0; then + read -r ans + else + log_warning 'Non-interactive tty detected. Assuming no.' + ans='N' + fi + if [ "${ans}" != "y" ] then echo "$command aborted by user." @@ -480,7 +513,7 @@ mysql_dump() { check_file_overwrite "$dump_file" # Check the temp file too - tmp_file="$dump_file.tmp" + tmp_file="/tmp/$(basename "${dump_file}").tmp" check_file_overwrite $tmp_file # Run the sql to output tab-delimited lease data to a temp file. @@ -499,9 +532,11 @@ mysql_dump() { exit 1 fi - # delete the tmp file on success - rm $tmp_file - echo lease${dhcp_version} successfully dumped to "${dump_file}" + # Clean up the temporary file. + rm -f "${tmp_file}" + log_info "Removed temporary file ${tmp_file}." + + log_info "Successfully dumped lease${dhcp_version} to ${dump_file}." exit 0 } @@ -632,11 +667,24 @@ lease_upload() { fi # Check that the input file has at least one row of values. - if test "$(wc -l < "${input_file}")" -le 1; then + input_file_line_length=$(wc -l < "${input_file}") + if test "${input_file_line_length}" -le 1; then log_error 'CSV file has no leases' exit 1 fi + # Invoke LFC on the input file. + log_info "Looking at ${input_file_line_length} lines of CSV in ${input_file}..." + cleaned_up_csv="/tmp/$(basename "${input_file}").tmp" + check_file_overwrite "${cleaned_up_csv}" + cp "${input_file}" "${cleaned_up_csv}" + "${kea_lfc}" "-${dhcp_version}" -x "${cleaned_up_csv}" \ + -i "${cleaned_up_csv}.1" -o "${cleaned_up_csv}.output" \ + -f "${cleaned_up_csv}.completed" -p "${cleaned_up_csv}.pid" \ + -cignored-path + cleaned_up_csv_line_length=$(wc -l < "${cleaned_up_csv}") + log_info "Reduced to ${cleaned_up_csv_line_length} lines in ${cleaned_up_csv}." + # Determine the columns whose values need to be stringified to avoid syntax # errors in the MySQL client. These are columns which are VARCHARs or need # to be further processed by a procedure. @@ -673,7 +721,7 @@ lease_upload() { else header_parsed=true fi - done < "${input_file}" + done < "${cleaned_up_csv}" sql_statement="${sql_statement} COMMIT;" # Execute the SQL insert statements. @@ -686,8 +734,12 @@ lease_upload() { exit 1 fi + # Clean up the temporary CSV. + rm -f "${cleaned_up_csv}" + log_info "Removed temporary file ${cleaned_up_csv}." + # Print a confirmation message. - printf 'lease%s successfully updated.\n' "${dhcp_version}" + log_info "Successfully updated table lease${dhcp_version}." } ### Functions used for recounting statistics @@ -814,13 +866,18 @@ do shift db_password=${1} else - # Otherwise read from standard input while hiding feedback to - # the terminal. - printf 'Password: ' - stty -echo - read -r db_password - stty echo - printf '\n' + # If it's an interactive shell... + if test -t 0; then + # Read from standard input while hiding feedback to the terminal. + printf 'Password: ' + stty -echo + read -r db_password + stty echo + printf '\n' + else + log_warning 'Non-interactive tty detected. Assuming empty password.' + db_password='' + fi fi ;; # Specify database name @@ -894,6 +951,9 @@ do extra_arguments="${extra_arguments} ${1}" fi ;; + -y|--yes) + assume_yes=1 + ;; *) log_error "invalid option: ${option}" usage diff --git a/src/bin/admin/tests/data/lease4_dump_test.reference.csv b/src/bin/admin/tests/data/lease4_dump_test.reference.csv index 86f91c4ae127a0b55619b6bb0fea17723506ee1d..265b81b10b3e6e66bc624e51c693d5baa94641b8 100644 --- a/src/bin/admin/tests/data/lease4_dump_test.reference.csv +++ b/src/bin/admin/tests/data/lease4_dump_test.reference.csv @@ -1,4 +1,4 @@ address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname,state,user_context 0.0.0.10,32:30,33:30,40,1642000000,50,1,1,one.example.com,0, -0.0.0.11,,31:32:33,40,1643210000,50,1,1,,1,{} -0.0.0.12,32:32,,40,1643212345,50,1,1,threeˎxampleˌom,2,{"a":1,"b":"c"} +0.0.0.11,,31:32:33,40,1643210000,50,1,1,,1,{ } +0.0.0.12,32:32,,40,1643212345,50,1,1,threeˎxampleˌom,2,{ "a": 1, "b": "c" } diff --git a/src/bin/admin/tests/data/lease6_dump_test.reference.csv b/src/bin/admin/tests/data/lease6_dump_test.reference.csv index 43e3620bd1604eebd6988930c7ad5e44f9cbb2ad..1e85864d296e48b55257f8bbfb3cd07c24e491d4 100644 --- a/src/bin/admin/tests/data/lease6_dump_test.reference.csv +++ b/src/bin/admin/tests/data/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,state,user_context,hwtype,hwaddr_source ::10,32:30,30,1642000000,40,50,1,60,70,1,1,one.example.com,38:30,0,,90,16 -::11,,30,1643210000,40,50,1,60,70,1,1,,38:30,1,{},90,1 -::12,32:31,30,1643212345,40,50,1,60,70,1,1,threeˎxampleˌom,38:30,2,{"a":1,"b":"c"},90,4 +::11,32:31,30,1643210000,40,50,1,60,70,1,1,,38:30,1,{ },90,1 +::12,32:32,30,1643212345,40,50,1,60,70,1,1,threeˎxampleˌom,38:30,2,{ "a": 1, "b": "c" },90,4 diff --git a/src/bin/admin/tests/memfile_tests.sh.in b/src/bin/admin/tests/memfile_tests.sh.in index b8e827f0025de9d5baa837bff49b6ff2e7be1970..f5b06c75106c546a3773e3c5ef5ed1f59785e7f3 100644 --- a/src/bin/admin/tests/memfile_tests.sh.in +++ b/src/bin/admin/tests/memfile_tests.sh.in @@ -68,10 +68,10 @@ memfile_header_v6() { # Print data copied from mysql_upgrade_12_to_13_test and pgsql_upgrade_7_0_to_8_0. # @{ memfile_data_v4() { - printf '0.0.0.10,32:30,33:30,40,1678900000,50,1,1,oneˎxampleˌom,0,{ "a": 1, "b": 2 }' + printf '0.0.0.10,32:30,33:30,40,1678900000,50,1,1,oneˎxampleˌom,0,{"a":1,"b":2}' } memfile_data_v6() { - printf '::10,32:30,30,1678900000,40,50,1,60,70,1,1,oneˎxampleˌom,38:30,0,{ "a": 1, "b": 2 },90,16' + printf '::10,32:30,30,1678900000,40,50,1,60,70,1,1,oneˎxampleˌom,38:30,0,{"a":1,"b":2},90,16' } # @} diff --git a/src/bin/admin/tests/mysql_tests.sh.in b/src/bin/admin/tests/mysql_tests.sh.in index 1d288774cd6385e3b629d8db5a901484fdc26d70..2402bce5eb71d3a7f6bf7c67ac18af7a82d679d7 100644 --- a/src/bin/admin/tests/mysql_tests.sh.in +++ b/src/bin/admin/tests/mysql_tests.sh.in @@ -1026,8 +1026,9 @@ SET @disable_audit = 0;" test_finish 0 } +# May accept additional parameters to be passed to lease-dump. mysql_lease4_dump_test() { - test_start "mysql.lease4_dump_test" + test_start "mysql.lease4_dump_test${1-}" test_dir="@abs_top_srcdir@/src/bin/admin/tests" output_dir="@abs_top_builddir@/src/bin/admin/tests" @@ -1035,22 +1036,31 @@ mysql_lease4_dump_test() { output_file="$output_dir/data/mysql.lease4_dump_test.output.csv" ref_file="$test_dir/data/lease4_dump_test.reference.csv" - # Clean up any test files left from prior failed runs. - rm -f "${output_file}" + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi # Let's wipe the whole database mysql_wipe # Ok, now let's initialize the database run_command \ - "${kea_admin}" db-init mysql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" + "${kea_admin}" db-init mysql -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" # Insert the reference record insert_sql="\ insert into lease4 values(10,20,30,40,(SELECT FROM_UNIXTIME(1642000000)),50,1,1,'one.example.com',0,NULL); -insert into lease4 values(11,NULL,123,40,(SELECT FROM_UNIXTIME(1643210000)),50,1,1,'',1,'{}');\ -insert into lease4 values(12,22,NULL,40,(SELECT FROM_UNIXTIME(1643212345)),50,1,1,'three,example,com',2,'{\"a\":1,\"b\":\"c\"}');" +insert into lease4 values(11,NULL,123,40,(SELECT FROM_UNIXTIME(1643210000)),50,1,1,'',1,'{ }');\ +insert into lease4 values(12,22,NULL,40,(SELECT FROM_UNIXTIME(1643212345)),50,1,1,'three,example,com',2,'{ \"a\": 1, \"b\": \"c\" }');" run_command \ mysql_execute "$insert_sql" @@ -1058,7 +1068,8 @@ insert into lease4 values(12,22,NULL,40,(SELECT FROM_UNIXTIME(1643212345)),50,1, # Dump lease4 to output_file run_command \ - "${kea_admin}" lease-dump mysql -4 -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" -o $output_file + "${kea_admin}" lease-dump mysql -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, expected exit code %d, actual %d" # Compare the dump output to reference file, they should be identical @@ -1075,8 +1086,9 @@ insert into lease4 values(12,22,NULL,40,(SELECT FROM_UNIXTIME(1643212345)),50,1, test_finish 0 } +# May accept additional parameters to be passed to lease-dump. mysql_lease6_dump_test() { - test_start "mysql.lease6_dump_test" + test_start "mysql.lease6_dump_test${1-}" test_dir="@abs_top_srcdir@/src/bin/admin/tests" output_dir="@abs_top_builddir@/src/bin/admin/tests" @@ -1084,8 +1096,16 @@ mysql_lease6_dump_test() { output_file="$output_dir/data/mysql.lease6_dump_test.output.csv" ref_file="$test_dir/data/lease6_dump_test.reference.csv" - # Clean up any test files left from prior failed runs. - rm -f "${output_file}" + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi # Let's wipe the whole database mysql_wipe @@ -1098,8 +1118,8 @@ mysql_lease6_dump_test() { # Insert the reference record insert_sql="\ insert into lease6 values('::10',20,30,(SELECT FROM_UNIXTIME(1642000000)),40,50,1,60,70,1,1,'one.example.com',80,90,16,0,NULL);\ -insert into lease6 values('::11',NULL,30,(SELECT FROM_UNIXTIME(1643210000)),40,50,1,60,70,1,1,'',80,90,1,1,'{}');\ -insert into lease6 values('::12',21,30,(SELECT FROM_UNIXTIME(1643212345)),40,50,1,60,70,1,1,'three,example,com',80,90,4,2,'{\"a\":1,\"b\":\"c\"}');" +insert into lease6 values('::11',21,30,(SELECT FROM_UNIXTIME(1643210000)),40,50,1,60,70,1,1,'',80,90,1,1,'{ }');\ +insert into lease6 values('::12',22,30,(SELECT FROM_UNIXTIME(1643212345)),40,50,1,60,70,1,1,'three,example,com',80,90,4,2,'{ \"a\": 1, \"b\": \"c\" }');" run_command \ mysql_execute "$insert_sql" @@ -1107,7 +1127,8 @@ insert into lease6 values('::12',21,30,(SELECT FROM_UNIXTIME(1643212345)),40,50, # Dump lease4 to output_file run_command \ - "${kea_admin}" lease-dump mysql -6 -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" -o $output_file + "${kea_admin}" lease-dump mysql -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, expected exit code %d, actual %d" # Compare the dump output to reference file, they should be identical @@ -1124,8 +1145,9 @@ insert into lease6 values('::12',21,30,(SELECT FROM_UNIXTIME(1643212345)),40,50, test_finish 0 } +# May accept additional parameters to be passed to lease-dump or to lease-upload. mysql_lease4_upload_test() { - test_start "mysql.lease4_upload_test" + test_start "mysql.lease4_upload_test${1-}" input_file="@abs_top_srcdir@/src/bin/admin/tests/data/lease4_dump_test.reference.csv" output_file="@abs_top_srcdir@/src/bin/admin/tests/data/lease4_dump_test.output.csv" @@ -1133,23 +1155,37 @@ mysql_lease4_upload_test() { # Wipe the whole database. mysql_wipe + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${input_file}.tmp" + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${input_file}.tmp" + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi + # Initialize the database. run_command \ - "${kea_admin}" db-init mysql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" + "${kea_admin}" db-init mysql -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" # Upload leases. run_command \ "${kea_admin}" lease-upload mysql -4 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -i "${input_file}" + -i "${input_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-upload -4 failed, expected exit code %d, actual %d" # Dump leases. run_command \ "${kea_admin}" lease-dump mysql -4 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -o "${output_file}" + -o "${output_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-dump -4 failed, expected exit code %d, actual %d" # Compare the initial file used for upload to the file retrieved from dump, they should be identical. @@ -1166,8 +1202,9 @@ mysql_lease4_upload_test() { test_finish 0 } +# May accept additional parameters to be passed to lease-dump or to lease-upload. mysql_lease6_upload_test() { - test_start "mysql.lease6_upload_test" + test_start "mysql.lease6_upload_test${1-}" input_file="@abs_top_srcdir@/src/bin/admin/tests/data/lease6_dump_test.reference.csv" output_file="@abs_top_srcdir@/src/bin/admin/tests/data/lease6_dump_test.output.csv" @@ -1175,23 +1212,37 @@ mysql_lease6_upload_test() { # Wipe the whole database. mysql_wipe + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${input_file}.tmp" + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${input_file}.tmp" + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi + # Initialize the database. run_command \ - "${kea_admin}" db-init mysql -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" + "${kea_admin}" db-init mysql -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" # Upload leases. run_command \ "${kea_admin}" lease-upload mysql -6 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -i "${input_file}" + -i "${input_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-upload -6 failed, expected exit code %d, actual %d" # Dump leases. run_command \ "${kea_admin}" lease-dump mysql -6 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -o "${output_file}" + -o "${output_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-dump -6 failed, expected exit code %d, actual %d" # Compare the initial file used for upload to the file retrieved from dump, they should be identical. @@ -1990,11 +2041,15 @@ mysql_db_version_test mysql_db_version_with_extra_test mysql_upgrade_test mysql_lease4_dump_test +mysql_lease4_dump_test -y mysql_lease6_dump_test +mysql_lease6_dump_test -y mysql_lease4_upload_test +mysql_lease4_upload_test -y mysql_lease6_upload_test -mysql_lease4_stat_test +mysql_lease6_upload_test -y mysql_lease6_stat_test +mysql_lease4_stat_test mysql_lease_stat_upgrade_test mysql_lease_stat_recount_test mysql_unused_subnet_id_test diff --git a/src/bin/admin/tests/pgsql_tests.sh.in b/src/bin/admin/tests/pgsql_tests.sh.in index ecb8c121d75031288e35cadff404dcad6a278189..25268bb1ff511bf05cd4e67e27cb16115c536c61 100644 --- a/src/bin/admin/tests/pgsql_tests.sh.in +++ b/src/bin/admin/tests/pgsql_tests.sh.in @@ -526,6 +526,7 @@ pgsql_upgrade_test() { # determined by querying the PostgreSQL server. This # updated reference data is captured in a temporary file # which is used for the actual comparison. +# May accept additional parameters to be passed to lease-dump. pgsql_lease4_dump_test() { test_start "pgsql.lease4_dump_test" @@ -535,8 +536,16 @@ pgsql_lease4_dump_test() { output_file="$output_dir/data/pgsql.lease4_dump_test.output.csv" ref_file="$test_dir/data/lease4_dump_test.reference.csv" - # Clean up any test files left from prior failed runs. - rm -f "${output_file}" + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi # Let's wipe the whole database pgsql_wipe @@ -551,8 +560,8 @@ pgsql_lease4_dump_test() { # Otherwise, the value is interpreted as ASCII instead of raw bytes. insert_sql="\ 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\"}');" +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" @@ -560,7 +569,8 @@ insert into lease4 values(12,E'\\\\x3232','',40,TO_TIMESTAMP(1643212345),50,'t', # Dump lease4 to output_file run_command \ - "${kea_admin}" lease-dump pgsql -4 -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" -o $output_file + "${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, expected exit code %d, actual %d" # Compare the dump output to reference file, they should be identical @@ -599,8 +609,16 @@ pgsql_lease6_dump_test() { output_file="$output_dir/data/pgsql.lease6_dump_test.output.csv" ref_file="$test_dir/data/lease6_dump_test.reference.csv" - # Clean up any test files left from prior failed runs. - rm -f "${output_file}" + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi # Let's wipe the whole database pgsql_wipe @@ -615,8 +633,8 @@ pgsql_lease6_dump_test() { # Otherwise, the value is interpreted as ASCII instead of raw bytes. insert_sql="\ 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\"}');" +insert into lease6 values('::11',E'\\\\x3231',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'\\\\x3232',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" @@ -624,7 +642,8 @@ insert into lease6 values('::12',E'\\\\x3231',30,TO_TIMESTAMP(1643212345),40,50, # Dump lease6 to output_file run_command \ - "${kea_admin}" lease-dump pgsql -6 -u "${db_user}" -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" -o $output_file + "${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, expected exit code %d, actual %d" # Compare the dump output to reference file, they should be identical @@ -641,6 +660,7 @@ insert into lease6 values('::12',E'\\\\x3231',30,TO_TIMESTAMP(1643212345),40,50, test_finish 0 } +# May accept additional parameters to be passed to lease-dump or to lease-upload. pgsql_lease4_upload_test() { test_start "pgsql.lease4_upload_test" @@ -650,24 +670,37 @@ pgsql_lease4_upload_test() { # Wipe the whole database. pgsql_wipe + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${input_file}.tmp" + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${input_file}.tmp" + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi + # Initialize the database. run_command \ "${kea_admin}" db-init pgsql -u "${db_user}" -p "${db_password}" \ - -n "${db_name}" -d "${db_scripts_dir}" + -n "${db_name}" -d "${db_scripts_dir}" assert_eq 0 "${EXIT_CODE}" "could not create database, expected exit code %d, actual %d" # Upload leases. run_command \ "${kea_admin}" lease-upload pgsql -4 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -i "${input_file}" + -i "${input_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-upload -4 failed, expected exit code %d, actual %d" # Dump leases. run_command \ "${kea_admin}" lease-dump pgsql -4 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -o "${output_file}" + -o "${output_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-dump -4 failed, expected exit code %d, actual %d" # Compare the initial file used for upload to the file retrieved from dump, they should be identical. @@ -693,24 +726,37 @@ pgsql_lease6_upload_test() { # Wipe the whole database. pgsql_wipe + # Clean up any test files left from prior failed runs unless -y was provided in which case + # explicitly create the file to check that it will be automatically deleted. + # files should be removed by kea-admin itself. + if printf '%s' "${@}" | grep 'y' > /dev/null; then + touch "${input_file}.tmp" + touch "${output_file}" + touch "${output_file}.tmp" + else + rm -f "${input_file}.tmp" + rm -f "${output_file}" + rm -f "${output_file}.tmp" + fi + # Initialize the database. run_command \ "${kea_admin}" db-init pgsql -u "${db_user}" -p "${db_password}" \ - -n "${db_name}" -d "${db_scripts_dir}" + -n "${db_name}" -d "${db_scripts_dir}" assert_eq 0 "${EXIT_CODE}" "could not create database, expected exit code %d, actual %d" # Upload leases. run_command \ "${kea_admin}" lease-upload pgsql -6 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -i "${input_file}" + -i "${input_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-upload -6 failed, expected exit code %d, actual %d" # Dump leases. run_command \ "${kea_admin}" lease-dump pgsql -6 -u "${db_user}" \ -p "${db_password}" -n "${db_name}" -d "${db_scripts_dir}" \ - -o "${output_file}" + -o "${output_file}" "${@}" assert_eq 0 "${EXIT_CODE}" "kea-admin lease-dump -6 failed, expected exit code %d, actual %d" # Compare the initial file used for upload to the file retrieved from dump, they should be identical. @@ -1220,9 +1266,13 @@ pgsql_db_init_test pgsql_db_version_test pgsql_upgrade_test pgsql_lease4_dump_test +pgsql_lease4_dump_test -y pgsql_lease6_dump_test +pgsql_lease6_dump_test -y pgsql_lease4_upload_test +pgsql_lease4_upload_test -y pgsql_lease6_upload_test +pgsql_lease6_upload_test -y pgsql_lease4_stat_test pgsql_lease6_stat_test pgsql_lease_stat_upgrade_test