From 69ef216139fd6e2124363ff1c6f1925b6f17fa16 Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Mon, 23 May 2022 13:45:32 +0300 Subject: [PATCH] [#2293] tests for kea-admin -y --- ChangeLog | 7 +- src/bin/admin/tests/admin_tests.sh.in | 8 +-- src/bin/admin/tests/mysql_tests.sh.in | 97 ++++++++++++++++++++------- src/bin/admin/tests/pgsql_tests.sh.in | 82 ++++++++++++++++------ 4 files changed, 142 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3b7a6ff671..ba491b8a0a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,10 @@ -2022. [func] andrei +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. + CSV file to avoid a conflict error when inserting the leases in + the database. kea-admin has also 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 diff --git a/src/bin/admin/tests/admin_tests.sh.in b/src/bin/admin/tests/admin_tests.sh.in index a0e5dfdef4..456019d38f 100644 --- a/src/bin/admin/tests/admin_tests.sh.in +++ b/src/bin/admin/tests/admin_tests.sh.in @@ -88,28 +88,28 @@ kea_admin_error_reporting_tests() { test_start 'kea_admin_error_reporting_test.db_init.mysql.i' run_command \ - "${kea_admin}" lease-dump mysql -y -i + "${kea_admin}" lease-dump mysql -i assert_eq 1 "${EXIT_CODE}" assert_str_eq 'ERROR/kea-admin: -i or --input requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)" test_finish ${?} test_start 'kea_admin_error_reporting_test.db_init.mysql.input' run_command \ - "${kea_admin}" lease-dump mysql -y --input + "${kea_admin}" lease-dump mysql --input assert_eq 1 "${EXIT_CODE}" assert_str_eq 'ERROR/kea-admin: -i or --input requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)" test_finish ${?} test_start 'kea_admin_error_reporting_test.db_init.mysql.o' run_command \ - "${kea_admin}" lease-dump mysql -y -o + "${kea_admin}" lease-dump mysql -o assert_eq 1 "${EXIT_CODE}" assert_str_eq 'ERROR/kea-admin: -o or --output requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)" test_finish ${?} test_start 'kea_admin_error_reporting_test.db_init.mysql.output' run_command \ - "${kea_admin}" lease-dump mysql -y --output + "${kea_admin}" lease-dump mysql --output assert_eq 1 "${EXIT_CODE}" assert_str_eq 'ERROR/kea-admin: -o or --output requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)" test_finish ${?} diff --git a/src/bin/admin/tests/mysql_tests.sh.in b/src/bin/admin/tests/mysql_tests.sh.in index 339788041d..2402bce5eb 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,15 +1036,24 @@ 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 @@ -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 -y -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 @@ -1107,7 +1127,8 @@ insert into lease6 values('::12',22,30,(SELECT FROM_UNIXTIME(1643212345)),40,50, # Dump lease4 to output_file run_command \ - "${kea_admin}" lease-dump mysql -y -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',22,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,9 +1155,18 @@ mysql_lease4_upload_test() { # Wipe the whole database. mysql_wipe - # Make sure there's no output file left from previous failures, otherwise - # kea-admin is going to ask you if you want to overwrite which blocks tests. - 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 "${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 \ @@ -1145,16 +1176,16 @@ mysql_lease4_upload_test() { # Upload leases. run_command \ - "${kea_admin}" lease-upload mysql -y -4 -u "${db_user}" \ + "${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 -y -4 -u "${db_user}" \ + "${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. @@ -1171,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" @@ -1180,9 +1212,18 @@ mysql_lease6_upload_test() { # Wipe the whole database. mysql_wipe - # Make sure there's no output file left from previous failures, otherwise - # kea-admin is going to ask you if you want to overwrite which blocks tests. - 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 "${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 \ @@ -1192,16 +1233,16 @@ mysql_lease6_upload_test() { # Upload leases. run_command \ - "${kea_admin}" lease-upload mysql -y -6 -u "${db_user}" \ + "${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 -y -6 -u "${db_user}" \ + "${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. @@ -2000,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 c34668c04d..25268bb1ff 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 @@ -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 -y -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 @@ -624,7 +642,8 @@ insert into lease6 values('::12',E'\\\\x3232',30,TO_TIMESTAMP(1643212345),40,50, # Dump lease6 to output_file run_command \ - "${kea_admin}" lease-dump pgsql -y -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'\\\\x3232',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,9 +670,18 @@ pgsql_lease4_upload_test() { # Wipe the whole database. pgsql_wipe - # Make sure there's no output file left from previous failures, otherwise - # kea-admin is going to ask you if you want to overwrite which blocks tests. - 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 "${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 \ @@ -662,16 +691,16 @@ pgsql_lease4_upload_test() { # Upload leases. run_command \ - "${kea_admin}" lease-upload pgsql -y -4 -u "${db_user}" \ + "${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 -y -4 -u "${db_user}" \ + "${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. @@ -697,9 +726,18 @@ pgsql_lease6_upload_test() { # Wipe the whole database. pgsql_wipe - # Make sure there's no output file left from previous failures, otherwise - # kea-admin is going to ask you if you want to overwrite which blocks tests. - 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 "${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 \ @@ -709,16 +747,16 @@ pgsql_lease6_upload_test() { # Upload leases. run_command \ - "${kea_admin}" lease-upload pgsql -y -6 -u "${db_user}" \ + "${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 -y -6 -u "${db_user}" \ + "${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. @@ -1228,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 -- 2.47.3