summaryrefslogtreecommitdiffstats
path: root/src/bin/dhcp4/tests/dhcp4_process_tests.sh.in
blob: 8cc8d93b6d9257d1991c188b534c2e052b60c511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
240
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/bin/sh

# Copyright (C) 2014-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).

# shellcheck disable=SC2039
# SC2039: In POSIX sh, 'local' is undefined.

# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu

# Path to the temporary configuration file.
CFG_FILE="@abs_top_builddir@/src/bin/dhcp4/tests/test_config.json"
# Path to the Kea log file.
LOG_FILE="@abs_top_builddir@/src/bin/dhcp4/tests/test.log"
# Path to the Kea lease file.
LEASE_FILE="@abs_top_builddir@/src/bin/dhcp4/tests/test_leases.csv"
# Path to the Kea LFC application
export KEA_LFC_EXECUTABLE="@abs_top_builddir@/src/bin/lfc/kea-lfc"
# Kea configuration to be stored in the configuration file.
CONFIG="{
    \"Dhcp4\":
    {
        \"interfaces-config\": {
            \"interfaces\": [ ]
        },
        \"valid-lifetime\": 4000,
        \"renew-timer\": 1000,
        \"rebind-timer\": 2000,
        \"lease-database\":
        {
            \"type\": \"memfile\",
            \"name\": \"$LEASE_FILE\",
            \"persist\": false,
            \"lfc-interval\": 0
        },
        \"subnet4\": [
        {
            \"subnet\": \"10.0.0.0/8\",
            \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]
        } ],
       \"dhcp-ddns\": {
            \"enable-updates\": true,
            \"qualifying-suffix\": \"\"
        },
        \"loggers\": [
        {
            \"name\": \"kea-dhcp4\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"INFO\"
        }
        ]
    }
}"
# Invalid configuration (syntax error) to check that Kea can check syntax.
# This config has following errors:
# - it should be interfaces-config/interfaces, not interfaces
# - it should be subnet4/pools, no subnet4/pool
CONFIG_BAD_SYNTAX="{
    \"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\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]
        } ],
        \"loggers\": [
        {
            \"name\": \"kea-dhcp4\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"INFO\"
        }
        ]
    }
}"

# This config has bad pool values. The pool it out of scope for the subnet
# it is defined in. Syntactically the config is correct, though.
CONFIG_BAD_VALUES="{
    \"Dhcp4\":
    {
        \"interfaces-config\": {
            \"interfaces\": [ ]
        },
        \"valid-lifetime\": 4000,
        \"renew-timer\": 1000,
        \"rebind-timer\": 2000,
        \"lease-database\":
        {
            \"type\": \"memfile\",
            \"persist\": false
        },
        \"subnet4\": [
        {
            \"subnet\": \"10.0.0.0/8\",
            \"pools\": [ { \"pool\": \"192.168.0.10-192.168.0.100\" } ]
        } ],
        \"loggers\": [
        {
            \"name\": \"kea-dhcp4\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"INFO\"
        }
        ]
    }
}"

# Invalid configuration (negative valid-lifetime) to check that Kea
# gracefully handles reconfiguration errors.
CONFIG_INVALID="{
    \"Dhcp4\":
    {
        \"interfaces-config\": {
            \"interfaces\": [ ]
        },
        \"valid-lifetime\": -3,
        \"renew-timer\": 1000,
        \"rebind-timer\": 2000,
        \"lease-database\":
        {
            \"type\": \"memfile\",
            \"persist\": false
        },
        \"subnet4\": [
        {
            \"subnet\": \"10.0.0.0/8\",
            \"pool\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ]
        } ],
        \"loggers\": [
        {
            \"name\": \"kea-dhcp4\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"INFO\"
        }
        ]
    }
}"

# Set the location of the executable.
bin="kea-dhcp4"
bin_path="@abs_top_builddir@/src/bin/dhcp4"

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

# This test verifies that syntax checking works properly. This function
# requires 3 parameters:
# test_name
# config - string with a content of the config (will be written to a file)
# expected_code - expected exit code returned by kea (0 - success, 1 - failure)
syntax_check_test() {
    local test_name="${1}"
    local config="${2}"
    local expected_code="${3}"

    # Log the start of the test and print test name.
    test_start "${test_name}"
    # Create correct configuration file.
    create_config "${config}"
    # Check it
    printf "Running command %s.\n" "\"${bin_path}/${bin} -t ${CFG_FILE}\""
    run_command \
        "${bin_path}/${bin}" -t "${CFG_FILE}"
    if [ "${EXIT_CODE}" -ne "${expected_code}" ]; then
        printf 'ERROR: expected exit code %s, got %s\n' "${expected_code}" "${EXIT_CODE}"
        clean_exit 1
    fi
    test_finish 0
}

# This test verifies that DHCPv4 can be reconfigured with a SIGHUP signal.
dynamic_reconfiguration_test() {
    # Log the start of the test and print test name.
    test_start "dhcpv4_srv.dynamic_reconfiguration"
    # Create new configuration file.
    create_config "${CONFIG}"
    # Instruct Kea to log to the specific file.
    set_logger
    # Start Kea.
    start_kea ${bin_path}/${bin}
    # 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_pid ${bin}
    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 ${bin}

    # 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_pid ${bin}
    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 ${bin}

    # 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_pid ${bin}
    if [ "${_GET_PIDS_NUM}" -ne 1 ]; then
        printf "ERROR: Kea process was killed when attempting reconfiguration.\n"
        clean_exit 1
    fi

    # When the server receives a signal the call to select() function is
    # interrupted. This should not be logged as an error.
    get_log_messages "DHCP4_PACKET_RECEIVE_FAIL"
    assert_eq 0 "${_GET_LOG_MESSAGES}" \
        "Expected get_log_messages DHCP4_PACKET_RECEIVE_FAIL return %d, \
returned %d."

    # All ok. Shut down Kea 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}"
    # Create new configuration file.
    create_config "${CONFIG}"
    # Instruct Kea to log to the specific file.
    set_logger
    # Start Kea.
    start_kea ${bin_path}/${bin}
    # 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_pid ${bin}
    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 "${signum}" "${bin}"

    # 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

    # 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"

    # When the server receives a signal the call to select() function is
    # interrupted. This should not be logged as an error.
    get_log_messages "DHCP4_PACKET_RECEIVE_FAIL"
    assert_eq 0 "${_GET_LOG_MESSAGES}" \
        "Expected get_log_messages return %d, returned %d."

    test_finish 0
}

# This test verifies that DHCPv4 can be configured to run lease file cleanup
# periodically.
lfc_timer_test() {
    # Log the start of the test and print test name.
    test_start "dhcpv4_srv.lfc_timer_test"
    # Create a configuration with the LFC enabled, by replacing the section
    # with the lfc-interval and persist parameters.
    LFC_CONFIG=$(printf '%s' "${CONFIG}" | sed -e 's/\"lfc-interval\": 0/\"lfc-interval\": 3/g' \
                        | sed -e 's/\"persist\": false/\"persist\": true/g')
    # Create new configuration file.
    create_config "${LFC_CONFIG}"
    # Instruct Kea to log to the specific file.
    set_logger
    # Start Kea.
    start_kea ${bin_path}/${bin}
    # 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_pid ${bin}
    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 if Kea emits the log message indicating that LFC is started.
    wait_for_message 10 "DHCPSRV_MEMFILE_LFC_EXECUTE" 1
    if [ "${_WAIT_FOR_MESSAGE}" -eq 0 ]; then
        printf "ERROR: Server did not execute LFC.\n"
        clean_exit 1
    fi

    # Give it a short time to run.
    sleep 1

    # Modify the interval.
    LFC_CONFIG=$(printf '%s' "${LFC_CONFIG}" | sed -e 's/\"lfc-interval\": 3/\"lfc-interval\": 4/g')
    # Create new configuration file.
    create_config "${LFC_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 "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_pid ${bin}
    if [ "${_GET_PIDS_NUM}" -ne 1 ]; then
        printf "ERROR: Kea process was killed when attempting reconfiguration.\n"
        clean_exit 1
    fi

    # Wait for the LFC to run the second time.
    wait_for_message 10 "DHCPSRV_MEMFILE_LFC_EXECUTE" 2
    if [ "${_WAIT_FOR_MESSAGE}" -eq 0 ]; then
        printf "ERROR: Server did not execute LFC.\n"
        clean_exit 1
    fi

    # Send signal to Kea SIGTERM
    send_signal 15 ${bin}

    # 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

    # 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"

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

server_pid_file_test "${CONFIG}" DHCP4_ALREADY_RUNNING
dynamic_reconfiguration_test
shutdown_test "dhcpv4.sigterm_test" 15
shutdown_test "dhcpv4.sigint_test" 2
version_test "dhcpv4.version"
logger_vars_test "dhcpv4.variables"
lfc_timer_test
syntax_check_test "dhcpv4.syntax_check_success" "${CONFIG}" 0
syntax_check_test "dhcpv4.syntax_check_bad_syntax" "${CONFIG_BAD_SYNTAX}" 1
syntax_check_test "dhcpv4.syntax_check_bad_values" "${CONFIG_BAD_VALUES}" 1
password_redact_test "dhcpv4.password_redact_test" "$(kea_dhcp_config 4)" 0