summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrei Pavel <andrei@isc.org>2021-05-25 14:55:28 +0200
committerAndrei Pavel <andrei@isc.org>2021-06-22 10:48:26 +0200
commitd88f45ad297888b3855dbeeada37f8369bc05d50 (patch)
tree309df02cf187dfcc0367af68e2b506a1dacbb10d
parent[#1653] fix cat ___.pid: no such file or dir in UT (diff)
downloadkea-d88f45ad297888b3855dbeeada37f8369bc05d50.tar.xz
kea-d88f45ad297888b3855dbeeada37f8369bc05d50.zip
[#1653] add unit tests for shell error reporting
-rw-r--r--.gitlab-ci.yml1
-rw-r--r--configure.ac2
-rw-r--r--src/bin/admin/tests/.gitignore1
-rw-r--r--src/bin/admin/tests/Makefile.am4
-rw-r--r--src/bin/admin/tests/admin_tests.sh.in105
-rw-r--r--src/bin/keactrl/tests/keactrl_tests.sh.in25
-rw-r--r--src/lib/testutils/dhcp_test_lib.sh.in19
7 files changed, 148 insertions, 9 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3ec8fb82f1..a7359ccbaa 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -21,6 +21,7 @@ shellcheck:
- SCRIPTS=
- SCRIPTS+="src/bin/admin/admin-utils.sh "
- SCRIPTS+="src/bin/admin/kea-admin.in "
+ - SCRIPTS+="src/bin/admin/tests/admin_tests.sh.in "
- SCRIPTS+="src/bin/admin/tests/cql_tests.sh.in "
- SCRIPTS+="src/bin/admin/tests/memfile_tests.sh.in "
- SCRIPTS+="src/bin/admin/tests/mysql_tests.sh.in "
diff --git a/configure.ac b/configure.ac
index 0fa9ac654f..65efde9407 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1654,6 +1654,8 @@ AC_CONFIG_FILES([src/bin/admin/tests/Makefile])
AC_CONFIG_FILES([src/bin/admin/tests/cql_tests.sh],
[chmod +x src/bin/admin/tests/cql_tests.sh])
AC_CONFIG_FILES([src/bin/admin/tests/data/Makefile])
+AC_CONFIG_FILES([src/bin/admin/tests/admin_tests.sh],
+ [chmod +x src/bin/admin/tests/admin_tests.sh])
AC_CONFIG_FILES([src/bin/admin/tests/memfile_tests.sh],
[chmod +x src/bin/admin/tests/memfile_tests.sh])
AC_CONFIG_FILES([src/bin/admin/tests/mysql_tests.sh],
diff --git a/src/bin/admin/tests/.gitignore b/src/bin/admin/tests/.gitignore
index b46230da6f..e54c5c3aa7 100644
--- a/src/bin/admin/tests/.gitignore
+++ b/src/bin/admin/tests/.gitignore
@@ -1,3 +1,4 @@
+/admin_tests.sh
/memfile_tests.sh
/mysql_tests.sh
/pgsql_tests.sh
diff --git a/src/bin/admin/tests/Makefile.am b/src/bin/admin/tests/Makefile.am
index 8092f27bd9..afe2c35332 100644
--- a/src/bin/admin/tests/Makefile.am
+++ b/src/bin/admin/tests/Makefile.am
@@ -1,6 +1,8 @@
SUBDIRS = data .
-SHTESTS = memfile_tests.sh
+SHTESTS =
+SHTESTS += admin_tests.sh
+SHTESTS += memfile_tests.sh
if HAVE_MYSQL
SHTESTS += mysql_tests.sh
diff --git a/src/bin/admin/tests/admin_tests.sh.in b/src/bin/admin/tests/admin_tests.sh.in
new file mode 100644
index 0000000000..4b18f89977
--- /dev/null
+++ b/src/bin/admin/tests/admin_tests.sh.in
@@ -0,0 +1,105 @@
+#!/bin/sh
+
+# Copyright (C) 2021 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
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+# shellcheck disable=SC1091
+# SC1091: Not following: ... was not specified as input (see shellcheck -x).
+
+# Exit with error if commands exit with non-zero and if undefined variables are
+# used.
+set -eu
+
+# Include common test library.
+. "@abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh"
+
+# Include admin utilities
+. "@abs_top_srcdir@/src/bin/admin/admin-utils.sh"
+
+# Set location of the kea-admin.
+kea_admin="@abs_top_builddir@/src/bin/admin/kea-admin"
+
+kea_admin_error_reporting_tests() {
+ test_start 'kea_admin_error_reporting_test.db_init'
+ run_command \
+ "${kea_admin}" db-init
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: missing backend' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.u'
+ run_command \
+ "${kea_admin}" db-init mysql -u
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: -u or --user requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.user'
+ run_command \
+ "${kea_admin}" db-init mysql --user
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: -u or --user requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.h'
+ run_command \
+ "${kea_admin}" db-init mysql -h
+ assert_str_eq 'ERROR/kea-admin: -h or --host requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ assert_eq 1 "${EXIT_CODE}"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.host'
+ run_command \
+ "${kea_admin}" db-init mysql --host
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: -h or --host requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.n'
+ run_command \
+ "${kea_admin}" db-init mysql -n
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: -n or --name requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.name'
+ run_command \
+ "${kea_admin}" db-init mysql --name
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: -n or --name requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.d'
+ run_command \
+ "${kea_admin}" db-init mysql -d
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: -d or --directory requires a parameter' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'kea_admin_error_reporting_test.db_init.mysql.directory'
+ run_command \
+ "${kea_admin}" db-init mysql --directory
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/kea-admin: -d or --directory 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 -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 --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 ${?}
+}
+
+# Run tests.
+kea_admin_error_reporting_tests
diff --git a/src/bin/keactrl/tests/keactrl_tests.sh.in b/src/bin/keactrl/tests/keactrl_tests.sh.in
index 363d12c79a..99fd1a407a 100644
--- a/src/bin/keactrl/tests/keactrl_tests.sh.in
+++ b/src/bin/keactrl/tests/keactrl_tests.sh.in
@@ -1454,6 +1454,30 @@ version_command_test() {
test_finish 0
}
+keactrl_error_reporting_tests() {
+ test_start 'keactrl_error_reporting_test'
+ run_command \
+ "${keactrl}"
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/keactrl: missing command' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'keactrl_error_reporting_test.start.c'
+ run_command \
+ "${keactrl}" start -c
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/keactrl: keactrl-config-file not specified' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+
+ test_start 'keactrl_error_reporting_test.start.s'
+ run_command \
+ "${keactrl}" start -s
+ assert_eq 1 "${EXIT_CODE}"
+ assert_str_eq 'ERROR/keactrl: servers not specified' "$(printf '%s\n' "${OUTPUT}" | head -n 1)"
+ test_finish ${?}
+}
+
+
# shellcheck disable=SC2034
# SC2034: ... appears unused. Verify use (or export if used externally).
# reason: bin and bin_path are used in version_test
@@ -1472,3 +1496,4 @@ late_start_v4_server_test
late_start_v6_server_test
stop_selected_server_test
status_no_config_test
+keactrl_error_reporting_tests
diff --git a/src/lib/testutils/dhcp_test_lib.sh.in b/src/lib/testutils/dhcp_test_lib.sh.in
index 60b3cf8341..4b0508fd63 100644
--- a/src/lib/testutils/dhcp_test_lib.sh.in
+++ b/src/lib/testutils/dhcp_test_lib.sh.in
@@ -21,6 +21,9 @@
# shellcheck disable=SC2154
# SC2154: bin_path is referenced but not assigned.
+# shellcheck disable=SC3043
+# SC3043: In POSIX sh, 'local' is undefined.
+
# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu
@@ -76,12 +79,12 @@ test_lib_info() {
# formatting like this:
# "Expected that some value 1 %d is equal to some other value %d".
assert_eq() {
- val1=${1} # Reference value
- val2=${2} # Tested value
- detailed_err=${3} # Detailed error format string
+ val1=${1} # Reference value
+ val2=${2} # Tested value
+ detailed_err=${3-} # Optional detailed error format string
# If nothing found, present an error an exit.
if [ "${val1}" -ne "${val2}" ]; then
- printf "Assertion failure: %s != %s, for val1=%s, val2=%s\n" \
+ printf 'Assertion failure: %s != %s, expected %s, got %s\n' \
"${val1}" "${val2}" "${val1}" "${val2}"
# shellcheck disable=SC2059
# SC2059: Don't use variables in the printf format string. Use printf '..%s..' "$foo"
@@ -97,12 +100,12 @@ assert_eq() {
# formatting like this:
# "Expected that some value 1 %d is equal to some other value %d".
assert_str_eq() {
- val1=${1} # Reference value
- val2=${2} # Tested value
- detailed_err=${3} # Detailed error format string
+ val1=${1} # Reference value
+ val2=${2} # Tested value
+ detailed_err=${3-} # Optional detailed error format string
# If nothing found, present an error an exit.
if [ "${val1}" != "${val2}" ]; then
- printf 'Assertion failure: %s != %s, for val1=%s, val2=%s\n' \
+ printf 'Assertion failure: %s != %s, expected "%s", got "%s"\n' \
"${val1}" "${val2}" "${val1}" "${val2}"
# shellcheck disable=SC2059
# SC2059: SC2059: Don't use variables in the printf format string. Use printf '..%s..' "$foo".