From: Marcin Siodelski Date: Tue, 3 Jun 2014 09:54:57 +0000 (+0200) Subject: [3405] Added unit tests for signal handling in DHCPv4 server. X-Git-Tag: trac3434_base~11^2~13 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=566039e4e41ac195fcc1b31628a5e132456a32bc;p=thirdparty%2Fkea.git [3405] Added unit tests for signal handling in DHCPv4 server. --- diff --git a/src/bin/dhcp4/tests/Makefile.am b/src/bin/dhcp4/tests/Makefile.am index c835aaa1bf..1739de78d0 100644 --- a/src/bin/dhcp4/tests/Makefile.am +++ b/src/bin/dhcp4/tests/Makefile.am @@ -1,7 +1,15 @@ PYCOVERAGE_RUN = @PYCOVERAGE_RUN@ PYTESTS = dhcp4_test.py -EXTRA_DIST = $(PYTESTS) +SHTESTS = +# The test of dynamic reconfiguration based on signals will work only +# if we are using file based configuration approach. +if CONFIG_BACKEND_JSON +SHTESTS += dhcp4_reconfigure_test.sh +SHTESTS += dhcp4_sigterm_test.sh +SHTESTS += dhcp4_sigint_test.sh +endif +EXTRA_DIST = $(PYTESTS) $(SHTESTS) # Explicitly specify paths to dynamic libraries required by loadable python # modules. That is required on Mac OS systems. Otherwise we will get exception @@ -21,6 +29,13 @@ check-local: $(PYCOVERAGE_RUN) $(abs_srcdir)/$$pytest || exit ; \ done + for shtest in $(SHTESTS) ; do \ + echo Running test: $$shtest ; \ + export B10_LOCKFILE_DIR_FROM_BUILD=$(abs_top_builddir); \ + $(abs_srcdir)/$$shtest || exit ; \ + done + + AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib AM_CPPFLAGS += -I$(top_builddir)/src/bin # for generated spec_config.h header AM_CPPFLAGS += -I$(top_srcdir)/src/bin diff --git a/src/bin/dhcp4/tests/dhcp4_reconfigure_test.sh b/src/bin/dhcp4/tests/dhcp4_reconfigure_test.sh new file mode 100755 index 0000000000..f8f5ca541d --- /dev/null +++ b/src/bin/dhcp4/tests/dhcp4_reconfigure_test.sh @@ -0,0 +1,161 @@ +# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. + +# Test name +TEST_NAME="DHCPv4.DynamicReconfiguration" +# Path to the temporary configuration file. +CFG_FILE="test_config.json" +# Path to the Kea log file. +LOG_FILE="test.log" +# Kea configuration to be stored in the configuration file. +CONFIG="{ + \"Dhcp4\": + { + \"interfaces\": [ ], + \"valid-lifetime\": 4000, + \"renew-timer\": 1000, + \"rebind-timer\": 2000, + \"lease-database\": + { + \"type\": \"memfile\", + \"persist\": false + }, + \"subnet4\": [ + { + \"subnet\": \"10.0.0.0/8\", + \"pool\": [ \"10.0.0.10-10.0.0.100\" ] + } ] + } +}" +# Invalid configuration (negative valid-lifetime) to check that Kea +# gracefully handles reconfiguration errors. +CONFIG_INVALID="{ + \"Dhcp4\": + { + \"interfaces\": [ ], + \"valid-lifetime\": -3, + \"renew-timer\": 1000, + \"rebind-timer\": 2000, + \"lease-database\": + { + \"type\": \"memfile\", + \"persist\": false + }, + \"subnet4\": [ + { + \"subnet\": \"10.0.0.0/8\", + \"pool\": [ \"10.0.0.10-10.0.0.100\" ] + } ] + } +}" + +# Set the location of the executable. +BIN="b10-dhcp4" +BIN_PATH=".." + +# Import common test library. +. $(dirname $0)/../../../lib/testutils/dhcp_test_lib.sh + +# Log the start of the test and print test name. +test_start +# Remove dangling Kea instances and remove log files. +cleanup +# Create new configuration file. +create_config "${CONFIG}" +# Instruct Kea to log to the specific file. +set_logger +# Start Kea. +start_kea +# Wait up to 20s for Kea to start. +wait_for_kea 20 +if [ ${_WAIT_FOR_KEA} -eq 0 ]; then + printf "ERROR: timeout waiting for Kea to start.\n" + clean_exit 1 +fi + +# Check if it is still running. It could have terminated (e.g. as a result +# of configuration failure). +get_pids +if [ ${_GET_PIDS_NUM} -ne 1 ]; then + printf "ERROR: expected one Kea process to be started. Found %d processes started.\n" ${_GET_PIDS_NUM} + clean_exit 1 +fi + +# Check in the log file, how many times server has been configured. It should +# be just once on startup. +get_reconfigs +if [ ${_GET_RECONFIGS} -ne 1 ]; then + printf "ERROR: server hasn't been configured.\n" + clean_exit 1 +else + printf "Server successfully configured.\n" +fi + +# Now use invalid configuration. +create_config "${CONFIG_INVALID}" + +# Try to reconfigure by sending SIGHUP +send_signal 1 + +# The configuration should fail and the error message should be there. +wait_for_message 10 "DHCP4_CONFIG_LOAD_FAIL" 1 + +# After receiving SIGHUP the server should try to reconfigure itself. +# The configuration provided is invalid so it should result in +# reconfiguration failure but the server should still be running. +get_reconfigs +if [ ${_GET_RECONFIGS} -ne 1 ]; then + printf "ERROR: server has been reconfigured despite bogus configuration.\n" + clean_exit 1 +elif [ ${_GET_RECONFIG_ERRORS} -ne 1 ]; then + printf "ERROR: server did not report reconfiguration error despite attempt to configure it with invalid configuration.\n" + clean_exit 1 +fi + +# Make sure the server is still operational. +get_pids +if [ ${_GET_PIDS_NUM} -ne 1 ]; then + printf "ERROR: Kea process was killed when attempting reconfiguration.\n" + clean_exit 1 +fi + +# Restore the good configuration. +create_config "${CONFIG}" + +# Reconfigure the server with SIGHUP. +send_signal 1 + +# There should be two occurrences of the DHCP4_CONFIG_COMPLETE messages. +# Wait for it up to 10s. +wait_for_message 10 "DHCP4_CONFIG_COMPLETE" 2 + +# After receiving SIGHUP the server should get reconfigured and the +# reconfiguration should be noted in the log file. We should now +# have two configurations logged in the log file. +if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then + printf "ERROR: server hasn't been reconfigured.\n" + clean_exit 1 +else + printf "Server successfully reconfigured.\n" +fi + +# Make sure the server is still operational. +get_pids +if [ ${_GET_PIDS_NUM} -ne 1 ]; then + printf "ERROR: Kea process was killed when attempting reconfiguration.\n" + clean_exit 1 +fi + +# All ok. Shut down Kea and exit. +clean_exit 0 diff --git a/src/bin/dhcp4/tests/dhcp4_shutdown_test.sh b/src/bin/dhcp4/tests/dhcp4_shutdown_test.sh new file mode 100755 index 0000000000..9f043350b8 --- /dev/null +++ b/src/bin/dhcp4/tests/dhcp4_shutdown_test.sh @@ -0,0 +1,109 @@ +# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. + +if [ $# -ne 2 ]; then + printf "USAGE: dhcp4_shutdown_test.sh \n" + exit 1 +fi + +# Test name +TEST_NAME=$1 +# Signal number to be used for this test. +SIG_NUM=$2 +# Path to the temporary configuration file. +CFG_FILE="test_config.json" +# Path to the Kea log file. +LOG_FILE="test.log" +# Kea configuration to be stored in the configuration file. +CONFIG="{ + \"Dhcp4\": + { + \"interfaces\": [ ], + \"valid-lifetime\": 4000, + \"renew-timer\": 1000, + \"rebind-timer\": 2000, + \"lease-database\": + { + \"type\": \"memfile\", + \"persist\": false + }, + \"subnet4\": [ + { + \"subnet\": \"10.0.0.0/8\", + \"pool\": [ \"10.0.0.10-10.0.0.100\" ] + } ] + } +}" + +# Set the location of the executable. +BIN="b10-dhcp4" +BIN_PATH=".." + +# Import common test library. +. $(dirname $0)/../../../lib/testutils/dhcp_test_lib.sh + +# Log the start of the test and print test name. +test_start +# Remove dangling Kea instances and remove log files. +cleanup +# Create new configuration file. +create_config "${CONFIG}" +# Instruct Kea to log to the specific file. +set_logger +# Start Kea. +start_kea +# Wait up to 20s for Kea to start. +wait_for_kea 20 +if [ ${_WAIT_FOR_KEA} -eq 0 ]; then + printf "ERROR: timeout waiting for Kea to start.\n" + clean_exit 1 +fi + +# Check if it is still running. It could have terminated (e.g. as a result +# of configuration failure). +get_pids +if [ ${_GET_PIDS_NUM} -ne 1 ]; then + printf "ERROR: expected one Kea process to be started. Found %d processes started.\n" ${_GET_PIDS_NUM} + clean_exit 1 +fi + +# Check in the log file, how many times server has been configured. It should +# be just once on startup. +get_reconfigs +if [ ${_GET_RECONFIGS} -ne 1 ]; then + printf "ERROR: server hasn't been configured.\n" + clean_exit 1 +else + printf "Server successfully configured.\n" +fi + +# Send signal to Kea (SIGTERM, SIGINT etc.) +send_signal ${SIG_NUM} + +# Wait up to 10s for the server's graceful shutdown. The graceful shut down +# should be recorded in the log file with the appropriate message. +wait_for_message 10 "DHCP4_SHUTDOWN" 1 +if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then + printf "ERROR: Server did not record shutdown in the log.\n" + clean_exit 1 +fi + +# Server should have shut down. +get_pids +if [ ${_GET_PIDS_NUM} -ne 0 ]; then + printf "ERROR: Kea did not shut down after receiving signal.\n" ${_GET_PIDS_NUM} + clean_exit 1 +fi + +clean_exit 0 diff --git a/src/bin/dhcp4/tests/dhcp4_sigint_test.sh b/src/bin/dhcp4/tests/dhcp4_sigint_test.sh new file mode 100755 index 0000000000..80c8691826 --- /dev/null +++ b/src/bin/dhcp4/tests/dhcp4_sigint_test.sh @@ -0,0 +1,16 @@ +# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. + +# Run a test that sends SIGINT to Kea and checks if it shuts down gracefully. +$(dirname $0)/dhcp4_shutdown_test.sh "Sigint" 2 diff --git a/src/bin/dhcp4/tests/dhcp4_sigterm_test.sh b/src/bin/dhcp4/tests/dhcp4_sigterm_test.sh new file mode 100755 index 0000000000..2c5f837d1b --- /dev/null +++ b/src/bin/dhcp4/tests/dhcp4_sigterm_test.sh @@ -0,0 +1,16 @@ +# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +# PERFORMANCE OF THIS SOFTWARE. + +# Run a test that sends SIGTERM to Kea and checks if it shuts down gracefully. +$(dirname $0)/dhcp4_shutdown_test.sh "Sigterm" 15 diff --git a/src/lib/testutils/dhcp_test_lib.sh b/src/lib/testutils/dhcp_test_lib.sh index b1ad956152..0fc2e01dd9 100755 --- a/src/lib/testutils/dhcp_test_lib.sh +++ b/src/lib/testutils/dhcp_test_lib.sh @@ -63,10 +63,10 @@ _GET_RECONFIG_ERRORS= # Return value: number of configuration errors. get_reconfigs() { # Grep log file for DHCP6_CONFIG_COMPLETE occurences. There should # be one occurence per (re)configuration. - _GET_RECONFIGS=`grep -o DHCP6_CONFIG_COMPLETE ${LOG_FILE} | wc -w` + _GET_RECONFIGS=`grep -o CONFIG_COMPLETE ${LOG_FILE} | wc -w` # Grep log file for DHCP6_CONFIG_LOAD_FAIL to check for configuration # failures. - _GET_RECONFIG_ERRORS=`grep -o DHCP6_CONFIG_LOAD_FAIL ${LOG_FILE} | wc -w` + _GET_RECONFIG_ERRORS=`grep -o CONFIG_LOAD_FAIL ${LOG_FILE} | wc -w` # Remove whitespaces ${_GET_RECONFIGS##*[! ]} ${_GET_RECONFIG_ERRORS##*[! ]}