d2_process_tests.sh.in 9.2 KB
Newer Older
1
# Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
2
#
3 4 5
# 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/.
6 7 8 9 10

# Path to the temporary configuration file.
CFG_FILE=@abs_top_builddir@/src/bin/d2/tests/test_config.json
# Path to the D2 log file.
LOG_FILE=@abs_top_builddir@/src/bin/d2/tests/test.log
11 12
# Expected version
EXPECTED_VERSION="@PACKAGE_VERSION@"
13 14 15 16
# D2 configuration to be stored in the configuration file.
CONFIG="{
    \"DhcpDdns\":
    {
17
        \"ip-address\": \"127.0.0.1\",
18
        \"port\": 53001,
19 20 21
        \"tsig-keys\": [],
        \"forward-ddns\" : {},
        \"reverse-ddns\" : {}
Tomek Mrugalski's avatar
Tomek Mrugalski committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
    },
    \"Logging\":
    {
        \"loggers\": [
        {
            \"name\": \"kea-dhcp-ddns\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"DEBUG\"
        }
        ]
36 37 38
    }
}"

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
# Invalid configuration (syntax error) to check that Kea can check syntax.
CONFIG_BAD_SYNTAX="{
    \"DhcpDdns\":
    {
        \"ip-address\": \"127.0.0.1\",
        \"port\": BOGUS,
        \"tsig-keys\": [],
        \"forward-ddns\" : {},
        \"reverse-ddns\" : {}
    },
    \"Logging\":
    {
        \"loggers\": [
        {
            \"name\": \"kea-dhcp-ddns\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"INFO\"
        }
        ]
    }
}"

# Invalid configuration (out of range port) to check that Kea can check syntax.
CONFIG_BAD_VALUE="{
    \"DhcpDdns\":
    {
        \"ip-address\": \"127.0.0.1\",
        \"port\": 80000,
        \"tsig-keys\": [],
        \"forward-ddns\" : {},
        \"reverse-ddns\" : {}
    },
    \"Logging\":
    {
        \"loggers\": [
        {
            \"name\": \"kea-dhcp-ddns\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"INFO\"
        }
        ]
    }
}"

# Invalid value configuration (invalid port) to check that D2
92 93 94 95
# gracefully handles reconfiguration errors.
CONFIG_INVALID="{
    \"DhcpDdns\":
    {
96
        \"ip-address\": \"127.0.0.1\",
97
        \"port\": BOGUS,
98 99 100
        \"tsig-keys\": [],
        \"forward-ddns\" : {},
        \"reverse-ddns\" : {}
Tomek Mrugalski's avatar
Tomek Mrugalski committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
    },
    \"Logging\":
    {
        \"loggers\": [
        {
            \"name\": \"kea-dhcp-ddns\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"INFO\"
        }
        ]
115 116 117 118
    }
}"

# Set the location of the executable.
119
bin="kea-dhcp-ddns"
120 121 122 123 124
bin_path=@abs_top_builddir@/src/bin/d2

# Import common test library.
. @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
# This test verifies that syntax checking works properly. This function
# requires 3 parameters:
# testname
# config - string with a content of the config (will be written to a file)
# exp_code - expected exit code returned by kea (0 - success, 1 - failure)
syntax_check_test() {
    local TESTNAME="${1}"
    local CONFIG="${2}"
    local EXP_CODE="${3}"

    # Log the start of the test and print test name.
    test_start $TESTNAME
    # Remove dangling Kea instances and remove log files.
    cleanup
    # Create correct configuration file.
    create_config "${CONFIG}"
    # Check it
    printf "Running command %s.\n" "\"${bin_path}/${bin} -t ${CFG_FILE}\""
    ${bin_path}/${bin} -t ${CFG_FILE}
    exit_code=$?
    if [ ${exit_code} -ne $EXP_CODE ]; then
        printf "ERROR: expected exit code ${EXP_CODE}, got ${exit_code}\n"
        clean_exit 1
    fi
    test_finish 0
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
# This test verifies that D2 can be reconfigured with a SIGHUP signal.
dynamic_reconfiguration_test() {
    # Log the start of the test and print test name.
    test_start "dhcp_ddns.dynamic_reconfiguration"
    # Remove dangling D2 instances and remove log files.
    cleanup
    # Create new configuration file.
    create_config "${CONFIG}"
    # Instruct D2 to log to the specific file.
    set_logger
    # Start D2.
    start_kea ${bin_path}/${bin}
    # Wait up to 20s for D2 to start.
    wait_for_kea 20
    if [ ${_WAIT_FOR_KEA} -eq 0 ]; then
        printf "ERROR: timeout waiting for D2 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).
173
    get_pid ${bin}
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    if [ ${_GET_PIDS_NUM} -ne 1 ]; then
        printf "ERROR: expected one D2 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: D2 hasn't been configured.\n"
        clean_exit 1
    else
        printf "D2 successfully configured.\n"
    fi

    # Now use invalid configuration.
    create_config "${CONFIG_INVALID}"

    # Try to reconfigure by sending SIGHUP
    send_signal 1 ${bin}

    # Wait up to 10s for the D2Controller to log reload signal received.
197
    wait_for_message 10 "DCTL_CFG_FILE_RELOAD_SIGNAL_RECVD"  1
198 199 200 201 202 203 204 205
    if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
        printf "ERROR: D2 did report the reload signal receipt.\n"
        clean_exit 1
    fi

    # 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.
206
    wait_for_message 10 "DCTL_CFG_FILE_RELOAD_ERROR" 1
207 208 209 210 211 212
    if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
        printf "ERROR: D2 did not report reload error.\n"
        clean_exit 1
    fi

    # Make sure the server is still operational.
213
    get_pid ${bin}
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    if [ ${_GET_PIDS_NUM} -ne 1 ]; then
        printf "ERROR: D2 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 ${bin}

    # There should be two occurrences of the DHCP4_CONFIG_COMPLETE messages.
    # Wait for it up to 10s.
    wait_for_message 10 "DCTL_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: D2 hasn't been reconfigured.\n"
        clean_exit 1
    else
        printf "D2 successfully reconfigured.\n"
    fi

    # Make sure the server is still operational.
240
    get_pid ${bin}
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    if [ ${_GET_PIDS_NUM} -ne 1 ]; then
        printf "ERROR: D2 was killed when attempting reconfiguration.\n"
        clean_exit 1
    fi

    # All ok. Shut down D2 and exit.
    test_finish 0
}

# This test verifies that DHCPv4 server is shut 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}
    # Remove dangling D2 instances and remove log files.
    cleanup
    # Create new configuration file.
    create_config "${CONFIG}"
    # Instruct D2 to log to the specific file.
    set_logger
    # Start D2.
    start_kea ${bin_path}/${bin}
    # Wait up to 20s for D2 to start.
    wait_for_kea 20
    if [ ${_WAIT_FOR_KEA} -eq 0 ]; then
        printf "ERROR: timeout waiting for D2 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).
274
    get_pid ${bin}
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    if [ ${_GET_PIDS_NUM} -ne 1 ]; then
        printf "ERROR: expected one D2 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 D2 (SIGTERM, SIGINT etc.)
    send_signal ${signum} ${bin}

294
    # Now wait for process to log that it is exiting.
295
    wait_for_message 10 "DCTL_SHUTDOWN" 1
296
    if [ ${_WAIT_FOR_MESSAGE} -eq 0 ]; then
297
        printf "ERROR: DHCP-DDNS did not log shutdown.\n"
298 299 300
        clean_exit 1
    fi

301 302 303 304
    # Make sure the server is down.
    wait_for_server_down 5 ${bin}
    assert_eq 1 ${_WAIT_FOR_SERVER_DOWN} \
        "Expected wait_for_server_down return %d, returned %d"
305 306 307 308

    test_finish 0
}

309
server_pid_file_test "${CONFIG}" DCTL_ALREADY_RUNNING
310 311 312
dynamic_reconfiguration_test
shutdown_test "dhcp-ddns.sigterm_test" 15
shutdown_test "dhcp-ddns.sigint_test" 2
313
version_test "dhcp-ddns.version"
Tomek Mrugalski's avatar
Tomek Mrugalski committed
314
logger_vars_test "dhcp-ddns.variables"
315 316 317
syntax_check_test "dhcp-ddns.syntax_check_success" "${CONFIG}" 0
syntax_check_test "dhcp-ddns.syntax_check_bad_syntax" "${CONFIG_BAD_SYNTAX}" 1
syntax_check_test "dhcp-ddns.syntax_check_bad_values" "${CONFIG_BAD_VALUE}" 1