diff options
author | Tomek Mrugalski <tomasz@isc.org> | 2017-06-16 19:33:03 +0200 |
---|---|---|
committer | Tomek Mrugalski <tomasz@isc.org> | 2017-06-16 19:33:03 +0200 |
commit | 62ba0762e447cdb882d1a7d6ec38a3c9ec5dfa08 (patch) | |
tree | 21288cda5a3ceda1148ed8e9e8fbe0a7f8832961 | |
parent | 61012bf009eabcf0ee6c6d0f815291b1bfc4c733 (diff) | |
download | isc-dhcp-62ba0762e447cdb882d1a7d6ec38a3c9ec5dfa08.tar.gz |
[rt19430] paths no longer hardcoded in unit-tests, loopback lo/lo0 fixed.
-rw-r--r-- | configure.ac | 3 | ||||
-rw-r--r-- | tests/shell/dhclient_tests.sh.in | 200 | ||||
-rwxr-xr-x | tests/shell/example_tests.sh.in | 31 | ||||
-rw-r--r-- | tests/shell/linux_script_tests.sh.in | 74 |
4 files changed, 308 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac index b4d5bc3f..13b5fde7 100644 --- a/configure.ac +++ b/configure.ac @@ -1000,6 +1000,9 @@ AC_CONFIG_FILES([ server/Makefile tests/Makefile.am tests/Makefile + tests/shell/dhclient_tests.sh + tests/shell/example_tests.sh + tests/shell/linux_script_tests.sh tests/unittest.sh server/tests/Makefile doc/devel/doxyfile diff --git a/tests/shell/dhclient_tests.sh.in b/tests/shell/dhclient_tests.sh.in new file mode 100644 index 00000000..7c8c7c6b --- /dev/null +++ b/tests/shell/dhclient_tests.sh.in @@ -0,0 +1,200 @@ +# Copyright (C) 2016-2017 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/. + +EXPECTED_VERSION="isc-dhclient-4.4.0-dev" + +# dhclient configuration. Currently empty, but future tests may use it. +CONFIG="#nothing" + +CFG_FILE=@abs_top_builddir@/tests/dhclient.conf + +# Temporary log file. Make sure this location is writeable. +LOG_FILE=@abs_top_builddir@/tests/dhclient.log + +# Temporary lease file. This file will be created by shell tests. +LEASE_FILE=@abs_top_builddir@/tests/dhclient.leases + +# PID file created by a client, while being run from tests. +PID_FILE=@abs_top_builddir@/tests/dhclient.pid + +# This is a path to our fake script file. This script simply echoes +# command and variables to a file. +SCRIPT_FILE=@abs_top_builddir@/tests/shell/echo.sh + +# This is where the client script will log its output. +SCRIPT_LOG_FILE=@abs_top_builddir@/tests/echo.log + +SYSTEM=`uname -s` +case ${SYSTEM} in + Linux) + IFACE=lo + ;; + FreeBSD | NetBSD | OpenBSD | Darwin | *) + IFACE=lo0 + ;; +esac + +echo "Loopback interface to be used: ${IFACE} (system is ${SYSTEM})" + +bin="dhclient" +bin_path=/home/thomson/devel/dhcp-git/client + +PROCS="dhcpd dhclient" + +# Import common test library. +. /home/thomson/devel/dhcp-git/tests/shell/dhcp_test_lib.sh + +# This test verifies that dhclient shuts down gracefully when it +# receives a SIGINT or SIGTERM signal. +shutdown_test() { + test_name=${1} # Test name + signum=${2} # Signal number + # Log the start of the test and print test name. + test_start ${test_name} + + if [ "$EUID" -ne 0 ]; then + printf "This test requires to be run as root, skipping.\n" + test_finish 2 + return + fi + + # Remove dangling instances and remove log files. + cleanup + + # Create new configuration file. + create_config "${CONFIG}" + + # Start a client. + start_kea ${bin_path}/${bin} + # Wait up to 5s for Control Agent to start. + wait_for_kea 5 + if [ ${_WAIT_FOR_KEA} -eq 0 ]; then + printf "ERROR: timeout waiting for dhclient 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_pid ${bin} + if [ ${_GET_PIDS_NUM} -ne 1 ]; then + printf "ERROR: expected one dhclient 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. + check_client_started + if [ ${_CHECK_CLIENT_STARTED} -ne 1 ]; then + printf "ERROR: client start failed.\n" + clean_exit 1 + else + printf "dhclient started successfully.\n" + fi + + # Send signal to Control Agent (SIGTERM, SIGINT etc.) + send_signal ${signum} ${bin} + + # Make sure the server is down. + wait_for_process_down 5 ${bin} + assert_eq 1 ${_WAIT_FOR_PROCESS_DOWN} \ + "Expected wait_for_server_down return %d, returned %d" + + test_finish 0 +} + +# This test verifies that dhclient actually calls a shell script. +# The first reason to call is PREINIT. It's called before any +# actual DHCP operations are conducted. +script_call_preinit_test() { + test_name="dhclient.script-call.preinit" # Test name + grep_expr="reason=PREINIT" # name of the expression the script should log. + + # Log the start of the test and print test name. + test_start ${test_name} + + # Currently we need root to start a client, hence this check. + if [ "$EUID" -ne 0 ]; then + printf "This test requires to be run as root, skipping.\n" + test_finish 2 + return + fi + + # Remove dangling instances and remove log files. + cleanup + + # Create new configuration file. + create_config "${CONFIG}" + + # Start Control Agent. + start_kea ${bin_path}/${bin} -1 ${IFACE} + # Wait up to 120 for dhclient to fail (there's no server running). + wait_for_kea 5 + + # Check that the script was called and it reported appropriate + # reason. + grep_file ${SCRIPT_LOG_FILE} ${grep_expr} 1 + + # Send SIGTERM signal if the process is still running + send_signal 15 ${bin} + + # Make sure the server is down. + wait_for_process_down 5 ${bin} + assert_eq 1 ${_WAIT_FOR_PROCESS_DOWN} \ + "Expected wait_for_server_down return %d, returned %d" + + test_finish 0 +} + +# This test verifies that dhclient actually calls a shell script. +script_call_1fail_test() { + test_name="dhclient.script-call.onetry-fail" # Test name + grep_expr="reason=FAIL" # name of the expression the script should log. + timeout=120 + + # Log the start of the test and print test name. + test_start ${test_name} + + # Currently we need root to start a client, hence this check. + if [ "$EUID" -ne 0 ]; then + printf "This test requires to be run as root, skipping.\n" + test_finish 2 + return + fi + + printf "This test may take up to ${timeout} seconds to run. Sorry\n" + + # Remove dangling instances and remove log files. + cleanup + + # Create new configuration file. + create_config "${CONFIG}" + + # Start Control Agent. + start_kea ${bin_path}/${bin} -1 ${IFACE} + # Wait up to 120s for dhclient to fail (there's no server running). + wait_for_message ${timeout} "Unable to obtain a lease on first try. Exiting." 1 + + # Check that the script was called and it reported appropriate + # reason. + grep_file ${SCRIPT_LOG_FILE} ${grep_expr} 1 + + # Send SIGTERM signal if the process is still running + get_pid ${proc_name} + if [ ${_GET_PIDS_NUM} -eq 1 ]; then + # Assuming the client worked as expected, the process should be long + # gone. However, if it misbehaved for whatever reason, we will send + # a SIGKILL and wait a second. This should kill that bastard. + send_signal 9 ${bin} + sleep 1 + fi + + test_finish 0 +} + +version_test "dhclient.version" +script_call_preinit_test +script_call_1fail_test diff --git a/tests/shell/example_tests.sh.in b/tests/shell/example_tests.sh.in new file mode 100755 index 00000000..18e0f9b8 --- /dev/null +++ b/tests/shell/example_tests.sh.in @@ -0,0 +1,31 @@ +# Copyright (C) 2016-2017 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/. + +# Import common test library. +. @abs_top_builddir@/tests/shell/dhcp_test_lib.sh + +passed_test() { + + test_start "positive" + + test_finish 0 +} + +skipped_test() { + test_start "skipped" + + test_finish 2 +} + +failed_test() { + test_start "what-a-terrible-failure" + + test_finish 1 +} + +passed_test +skipped_test +failed_test diff --git a/tests/shell/linux_script_tests.sh.in b/tests/shell/linux_script_tests.sh.in new file mode 100644 index 00000000..22d30dd1 --- /dev/null +++ b/tests/shell/linux_script_tests.sh.in @@ -0,0 +1,74 @@ +# Copyright (C) 2016-2017 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/. + + +# This is fake ip tool. The original ip tool in Linux comes from +# ip-route2 suite. This one is a fake one. It simply echoes all +# its parameters to a log file. +IP=@abs_top_builddir@/tests/shell/ip-echo + +IP_LOG_FILE=@abs_top_builddir@/tests/ip-echo.log + +SCRIPT_FILE=@abs_top_builddir@/client/scripts/linux + +TMP_SCRIPT=@abs_top_builddir@/tests/linux + +PROCS="" + +# Import common test library. +. @abs_top_builddir@/tests/shell/dhcp_test_lib.sh + +# Ok, before we start we need to update the script to use our ip tool, not +# the one in /sbin/ip. + + +cp -f ${SCRIPT_FILE} ${TMP_SCRIPT} +sed -ri -e "s,/sbin/ip,${IP},g" ${TMP_SCRIPT} + +echo "Using script ${TMP_SCRIPT}" + +script_preinit_test() { + test_start "script.linux.preinit" + + rm -f ${IP_LOG_FILE} + reason=PREINIT interface=iface0 ${TMP_SCRIPT} + grep_file ${IP_LOG_FILE} "ip link set dev iface0 up" 1 + + rm -f ${IP_LOG_FILE} + reason=PREINIT interface=iface0 alias_ip_address=192.0.2.1 ${TMP_SCRIPT} + grep_file ${IP_LOG_FILE} "ip link set dev iface0 up" 1 + grep_file ${IP_LOG_FILE} "ip -4 addr flush dev iface0 label iface0:0" 1 + + test_finish 0 +} + +script_bound_test() { + test_name=${1} + reason=${2} + test_start ${test_name} + + rm -f ${IP_LOG_FILE} + + reason=${reason} old_ip_address=192.0.2.2 \ + new_ip_address=192.0.2.3 new_routers=192.0.2.4 new_subnet_mask=255.255.255.0 \ + new_broadcast_address=192.0.2.255 interface=iface0 new_interface_mtu=1500 ${TMP_SCRIPT} + + grep_file ${IP_LOG_FILE} "ip -4 addr flush dev iface0 label iface0:0" 0 + grep_file ${IP_LOG_FILE} "ip -4 addr flush dev iface0 label iface0" 1 + grep_file ${IP_LOG_FILE} "ip -4 addr add 192.0.2.3/255.255.255.0 broadcast 192.0.2.255 dev iface0 label iface0" 1 + grep_file ${IP_LOG_FILE} "ip link set dev iface0 mtu 1500" 1 + grep_file ${IP_LOG_FILE} "ip -4 route add default via 192.0.2.4 dev iface0" 1 + + test_finish 0 +} + +script_preinit_test +script_bound_test "script.linux.bound" BOUND +script_bound_test "script.linux.renew" RENEW +script_bound_test "script.linux.rebind" REBIND +script_bound_test "script.linux.reboot" REBOOT + +rm -f ${TMP_SCRIPT} |