shell_process_tests.sh.in 7.02 KB
Newer Older
1
# Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
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
#
# 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/.

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

# Control Agent configuration to be stored in the configuration file.
# todo: use actual configuration once we support it.
CONFIG="{
    \"Control-agent\":
    {
        \"http-host\": \"127.0.0.1\",
        \"http-port\": 8081
    },
    \"Logging\":
    {
        \"loggers\": [
        {
            \"name\": \"kea-ctrl-agent\",
            \"output_options\": [
                {
                    \"output\": \"$LOG_FILE\"
                }
            ],
            \"severity\": \"DEBUG\"
        }
        ]
    }
}"

38
# In these tests we need to use two binaries: Control Agent and Kea shell.
39 40
# Using bin and bin_path would be confusing, so we omit defining bin
# and bin_path on purpose.
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
ca_bin="kea-ctrl-agent"
ca_bin_path=@abs_top_builddir@/src/bin/agent

shell_bin="kea-shell"
shell_bin_path=@abs_top_builddir@/src/bin/shell

tmpfile_path=@abs_top_builddir@/src/bin/shell/tests

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

# This test verifies that Control Agent is shut down gracefully when it
# receives a SIGINT or SIGTERM signal.
shell_command_test() {
    test_name=${1}  # Test name
    cmd=${2}        # Command to be sent
    exp_rsp=${3}    # Expected response
    params=${4}     # Any extra parameters

    # Setup phase: start CA.

    # Log the start of the test and print test name.
    test_start ${test_name}
    
    # Remove any dangling CA instances and remove log files.
    cleanup

    # Create new configuration file.
    create_config "${CONFIG}"

    # Instruct Control Agent to log to the specific file.
    set_logger
    # Start Control Agent.
    start_kea ${ca_bin_path}/${ca_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 ${ca_bin}
    if [ ${_GET_PIDS_NUM} -ne 1 ]; then
86 87
        printf "ERROR: expected one Control Agent process to be started.\
 Found %d processes started.\n" ${_GET_PIDS_NUM}
88 89 90 91 92 93 94
        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
95 96
        printf "ERROR: server been configured ${_GET_RECONFIGS} time(s),\
 but exactly 1 was expected.\n"
97 98 99 100 101 102
        clean_exit 1
    else
        printf "Server successfully configured.\n"
    fi

    # Main test phase: send command, check response.
103 104
    tmp="echo \"${params}\" | ${shell_bin_path}/${shell_bin} --host \
 127.0.0.1 --port 8081 ${cmd} > ${tmpfile_path}/shell-stdout.txt"
105 106
    echo "Executing kea-shell ($tmp)"
    
107 108
    echo "${params}" | ${shell_bin_path}/${shell_bin} --host 127.0.0.1 \
 --port 8081 ${cmd} > ${tmpfile_path}/shell-stdout.txt
109 110 111 112

    # Check the exit code
    shell_exit_code=$?
    if [ ${shell_exit_code} -ne 0 ]; then
113
        echo "ERROR:" \
114
        "kea-shell returned ${shell_exit_code} exit code,  expected 0."
115 116 117 118 119 120 121 122 123 124
    else
        echo "kea-shell returned ${shell_exit_code} exit code as expected."
    fi

    # Now check the response
    rm -f ${tmpfile_path}/shell-expected.txt
    echo ${exp_rsp} > ${tmpfile_path}/shell-expected.txt
    diff ${tmpfile_path}/shell-stdout.txt ${tmpfile_path}/shell-expected.txt
    diff_code=$?
    if [ ${diff_code} -ne 0 ]; then
125
        echo "ERROR:" \
126 127
        "content returned is different than expected." \
        "See ${tmpfile_path}/shell-*.txt"
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
        echo "EXPECTED:"
        cat ${tmpfile_path}/shell-expected.txt
        echo "ACTUAL RESULT:"
        cat ${tmpfile_path}/shell-stdout.txt
        clean_exit 1
    else
        echo "Content returned by kea-shell meets expectation."
        rm ${tmpfile_path}/shell-*.txt
    fi
    # Main test phase ends.

    # Cleanup phase: shutdown CA
    # Send SIGTERM signal to Control Agent
    send_signal 15 ${ca_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 ${ca_bin}
    assert_eq 1 ${_WAIT_FOR_SERVER_DOWN} \
        "Expected wait_for_server_down return %d, returned %d"

    test_finish 0
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
# This test verifies that the binary is reporting its version properly.
version_test() {
    test_name=${1}  # Test name

    # Log the start of the test and print test name.
    test_start ${test_name}

    # Remove dangling Kea instances and remove log files.
    cleanup

    REPORTED_VERSION="`${shell_bin_path}/${shell_bin} -v`"

    if test "${REPORTED_VERSION}" == "${EXPECTED_VERSION}"; then
        test_finish 0
    else
173
        echo "ERROR:" \
174
        "Expected version ${EXPECTED_VERSION}, got ${REPORTED_VERSION}"
175 176 177 178 179 180
        test_finish 1
    fi
}

version_test "shell.version"
shell_command_test "shell.list-commands" "list-commands" \
181
    "[ { \"arguments\": [ \"build-report\", \"config-get\", \"config-set\", \"config-test\", \"config-write\", \"list-commands\", \"shutdown\", \"version-get\" ], \"result\": 0 } ]" ""
182
shell_command_test "shell.bogus" "give-me-a-beer" \
183
"[ { \"result\": 2, \"text\": \"'give-me-a-beer' command not supported. You did not include \\\"service\\\" parameter in the command, which indicates that Kea Control Agent should process this command rather than forward it to one or more DHCP servers. If you aimed to send this command to one of the DHCP servers you should include the \\\"service\\\" parameter in your request, e.g. \\\"service\\\": [ \\\"dhcp4\\\" ] to forward the command to the DHCPv4 server, or \\\"service\\\": [ \\\"dhcp4\\\", \\\"dhcp6\\\" ] to forward it to both DHCPv4 and DHCPv6 servers etc.\" } ]" ""
184 185 186 187 188 189 190 191 192
shell_command_test "shell.empty-config-test" "config-test" \
    "[ { \"result\": 1, \"text\": \"Missing mandatory 'arguments' parameter.\" } ]" ""
shell_command_test "shell.no-app-config-test" "config-test" \
    "[ { \"result\": 1, \"text\": \"Missing mandatory 'Control-agent' parameter.\" } ]" \
    "\"FooBar\": { }"
shell_command_test "shell.no-map-config-test" "config-test" \
    "[ { \"result\": 1, \"text\": \"'Control-agent' parameter expected to be a map.\" } ]" \
    "\"Control-agent\": [ ]"
shell_command_test "shell.bad-value-config-test" "config-test" \
193
    "[ { \"result\": 1, \"text\": \"out of range value (80000) specified for parameter 'http-port' (<string>:1:76)\" } ]" \
194
    "\"Control-agent\": { \"http-port\": 80000 }"