summaryrefslogtreecommitdiffstats
path: root/src/bin/agent/tests/ca_process_tests.sh.in
blob: b5331523ec86a841e2101afd05dfb846c8a9807e (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
#!/bin/sh

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

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

# Path to the temporary configuration file.
CFG_FILE="@abs_top_builddir@/src/bin/agent/tests/test_config.json"
# Path to the Control Agent log file.
LOG_FILE="@abs_top_builddir@/src/bin/agent/tests/test.log"

# Control Agent configuration to be stored in the configuration file.
CONFIG="{
    \"Control-agent\":
    {
        \"http-host\": \"127.0.0.1\",
        \"loggers\": [
        {
            \"name\": \"kea-ctrl-agent\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"DEBUG\"
        }
        ]
    }
}"

# Invalid configuration (syntax error) to check that Kea can check syntax.
CONFIG_BAD_SYNTAX="{
    \"Control-agent\":
    {
        \"http-port\": BOGUS
    }
}"

# Invalid configuration (out of range port) to check that Kea can check syntax.
CONFIG_BAD_VALUE="{
    \"Control-agent\":
    {
        \"http-port\": 80000
    }
}"

# Configuration with a password.
CONFIG_PWD="{
    \"Control-agent\":
    {
        \"http-host\": \"127.0.0.1\",
        \"authentication\":
        {
            \"clients\": [
                {
                    \"password\": \"sensitive\",
                    \"user\": \"superadmin\"
                }
            ],
            \"type\": \"basic\"
        }
    }
}"

bin="kea-ctrl-agent"
bin_path="@abs_top_builddir@/src/bin/agent"

# 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 Control Agent 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 Control Agent to log to the specific file.
    set_logger
    # Start Control Agent.
    start_kea ${bin_path}/${bin}
    # Wait up to 20s for Control Agent to start.
    wait_for_kea 20
    if [ "${_WAIT_FOR_KEA}" -eq 0 ]; then
        printf "ERROR: timeout waiting for Control Agent 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 Control Agent 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 been configured %s time(s), but exactly 1 was expected.\n' "${_GET_RECONFIGS}"
        clean_exit 1
    else
        printf "Server successfully configured.\n"
    fi

    # Send signal to Control Agent (SIGTERM, SIGINT etc.)
    send_signal "${signum}" "${bin}"

    # Now wait for process to log that it is exiting.
    wait_for_message 10 "DCTL_SHUTDOWN" 1
    if [ "${_WAIT_FOR_MESSAGE}" -eq 0 ]; then
        printf "ERROR: Control Agent did not log shutdown.\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"

    test_finish 0
}

server_pid_file_test "${CONFIG}" DCTL_ALREADY_RUNNING
shutdown_test "ctrl-agent.sigterm_test" 15
shutdown_test "ctrl-agent.sigint_test" 2
syntax_check_test "ctrl-agent.syntax_check_success" "${CONFIG}" 0
syntax_check_test "ctrl-agent.syntax_check_bad_syntax" "${CONFIG_BAD_SYNTAX}" 1
syntax_check_test "ctrl-agent.syntax_check_bad_values" "${CONFIG_BAD_VALUE}" 1
password_redact_test "ctrl-agent.password_redact_test" "${CONFIG_PWD}" 0