summaryrefslogtreecommitdiffstats
path: root/src/bin/admin/tests/memfile_tests.sh.in
blob: 6591297ebfc7dac71211d2e3ceda25f7f37f3124 (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
#!/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

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

# Locations of memfile tools
kea_admin="@abs_top_builddir@/src/bin/admin/kea-admin"
kea_lfc="@abs_top_builddir@/src/bin/lfc/kea-lfc"

# Clean up any files used in testing.
clean_up() {
    remove_if_exists \
        "${config_file-}" \
        "${csv-}" \
        "${csv_2-}" \
        "${KEA_LOCKFILE_DIR-}" \
        "${KEA_PIDFILE_DIR-}"
}

# Print location of CSV file. Accepts 4 or 6 as parameter.
csv_file() {
    local v=${1}
    printf '%s' "@abs_top_builddir@/src/bin/admin/tests/kea-dhcp${v}.csv"
}

# Print location of kea-dhcp[46] binaries. Accepts 4 or 6 as parameter.
kea_dhcp() {
    local v=${1}
    printf '%s' "@abs_top_builddir@/src/bin/dhcp${v}/kea-dhcp${v}"
}

# Print the minimum allowed number of header columns for v4.
incomplete_memfile_header_v4() {
    printf 'address,hwaddr,client_id,valid_lifetime,expire,subnet_id,fqdn_fwd,fqdn_rev,hostname'
}

# Print the minimum allowed number of header columns for v6.
incomplete_memfile_header_v6() {
    printf 'address,duid,valid_lifetime,expire,subnet_id,pref_lifetime,lease_type,iaid,prefix_len,fqdn_fwd,fqdn_rev,hostname'
}

# Print the entire header for v4.
memfile_header_v4() {
    printf '%s,state,user_context' "$(incomplete_memfile_header_v4)"
}

# Print the entire header for v6.
memfile_header_v6() {
    printf '%s,hwaddr,state,user_context' "$(incomplete_memfile_header_v6)"
}

# Print "server-id" configuration. Not available for v4.
server_id_v4() {
    :
}

# Print "server-id" configuration. Not available for v4.
server_id_v6() {
    printf ',
    "server-id": {
        "persist": false,
        "type": "EN"
    }
'
}

# Starts Kea and sets PID. It logs to stdout and stderr if DEBUG is enabled.
# Accepts 4 or 6 as parameter.
start_kea_dhcp() {
    local v=${1}
    if test -n "${DEBUG+x}"; then
        "$(kea_dhcp "${v}")" -c "${config_file}" &
    else
        "$(kea_dhcp "${v}")" -c "${config_file}" > /dev/null 2>&1 &
    fi
    PID=${!}
}

# Test that Kea creates a correctly populated CSV file if configured with
# persisting memfile.
memfile_init_test() {
    test_start 'memfile.init'

    for v in 4 6; do
        config=$(printf '%s%s%s' '
{
  "Dhcpx": {
    "lease-database": {
      "name": "@abs_top_builddir@/src/bin/admin/tests/kea-dhcpx.csv",
      "persist": true,
      "type": "memfile"
    }' \
    "$(server_id_v${v})" \
    '
  }
}
')
        config_file="@abs_top_builddir@/src/bin/admin/tests/kea-dhcp${v}.conf"
        csv=$(csv_file "${v}")
        printf '%s\n' "${config}" | \
            sed "s#Dhcpx#Dhcp${v}#g;
                 s#\"name\":.*,#\"name\": \"${csv}\",#g" \
                > "${config_file}"
        if test -e "${csv}"; then
            printf 'ERROR: garbage file "%s" should not exist.\n' "${csv}" >&2
            clean_up
            clean_exit 1
        fi
        mkdir -p "${KEA_LOCKFILE_DIR}"
        mkdir -p "${KEA_PIDFILE_DIR}"
        start_kea_dhcp "${v}"
        # This assumes that the CSV creation + writing to CSV is atomic. Not
        # sure if it is, but if this ever fails on the comparison further below,
        # consider waiting here for line DHCPSRV_MEMFILE_LFC_SETUP in logs, even
        # though it doesn't clearly signal end of CSV writing.
        if ! wait_for_file "${csv}"; then
            clean_up
            clean_exit 1
        fi
        kill "${PID}"
        if ! wait_for_process_to_stop "${PID}"; then
            clean_up
            clean_exit 2
        fi
        content=$(cat "${csv}")
        expected=$(memfile_header_v${v})
        if test "${content}" != "${expected}"; then
            printf 'ERROR: %s does not contain expected header.\n< %s\n> %s\n' \
                "${csv}" "${content}" "${expected}" >&2
            clean_up
            clean_exit 3
        fi

        clean_up
    done

    test_finish 0
}

# Test that kea-lfc is able to upgrade a CSV file with incomplete header.
memfile_upgrade_test() {
    test_start 'memfile.upgrade'

    for v in 4 6; do
        csv=$(csv_file "${v}")
        "incomplete_memfile_header_v${v}" > "${csv}"
        printf '\n' >> "${csv}"
        csv_2="${csv}.2"

        "${kea_lfc}" "-${v}" \
            -c 'ignored-path' \
            -f "${csv}.completed" \
            -i "${csv}" \
            -o "${csv}.output" \
            -p "${csv}.pid" \
            -x "${csv_2}"

        content=$(cat "${csv_2}")
        expected=$(memfile_header_v${v})
        if test "${content}" != "${expected}"; then
            printf 'ERROR: %s does not contain expected header.\n< %s\n> %s\n' \
                "${csv}" "${content}" "${expected}" >&2
            clean_up
            clean_exit 1
        fi

        clean_up
    done

    test_finish 0
}

# shellcheck disable=SC2034
# SC2034: ... appears unused. Verify use (or export if used externally).
# reason: bin and bin_path are used in version_test
{
bin=$(basename "${kea_admin}")
bin_path=$(dirname "${kea_admin}")
version_test 'memfile.version' 'long_version_too_please'
}

memfile_init_test
memfile_upgrade_test