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

# Copyright (C) 2014-2022 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-}" \
        "@abs_top_builddir@/src/bin/admin/tests/kea.log" \
        "@abs_top_builddir@/src/bin/admin/tests/kea.log.lock"
}

# 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,hwtype,hwaddr_source' "$(incomplete_memfile_header_v6)"
}

# Print data copied from mysql_upgrade_12_to_13_test and pgsql_upgrade_7_0_to_8_0.
# @{
memfile_data_v4() {
    printf '0.0.0.10,32:30,33:30,40,1678900000,50,1,1,one&#x2cexample&#x2ccom,0,{"a":1&#x2c"b":2}'
}
memfile_data_v6() {
    printf '::10,32:30,30,1678900000,40,50,1,60,70,1,1,one&#x2cexample&#x2ccom,38:30,0,{"a":1&#x2c"b":2},90,16'
}
# @}

# 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}"
    "$(kea_dhcp "${v}")" -c "${config_file}" >> "@abs_top_builddir@/src/bin/admin/tests/kea.log" 2>&1 &
    PID=${!}
    sleep 1
}

# 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
      for i in no-file some-data; do
        if test -n "${DEBUG+x}"; then
            printf 'TRACE %s, %s\n' "${v}" "${i}"
        fi
        config=$(printf '%s%s%s' '
{
  "Dhcpx": {
    "lease-database": {
      "name": "@abs_top_builddir@/src/bin/admin/tests/kea-dhcpx.csv",
      "persist": true,
      "type": "memfile"
    },
    "loggers": [
      {
        "debuglevel": 99,
        "name": "kea-dhcpx",
        "output_options": [
          {
            "output": "@abs_top_builddir@/src/bin/admin/tests/kea.log"
          }
        ],
        "severity": "DEBUG"
      }
    ]' \
    "$(server_id_v${v})" \
    '
  }
}
')
        config_file="@abs_top_builddir@/src/bin/admin/tests/kea-dhcp${v}.conf"
        csv=$(csv_file "${v}")

        if test "${i}" = 'some-data'; then
            # Test that Kea accepts the output of lease-dump in memfile.
            {
              "memfile_header_v${v}"
              printf '\n'
              "memfile_data_v${v}"
              printf '\n'
            } > "${csv}"
        else
            # Make sure there is no CSV as the test requires.
            rm -f "${csv}"
        fi

        # Set DHCP version in config.
        printf '%s\n' "${config}" | \
            sed "s#Dhcpx#Dhcp${v}#g;
                 s#dhcpx#dhcp${v}#g" \
                > "${config_file}"

        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}" || true  # process may have exited early due to errors
        if ! wait_for_process_to_stop "${PID}"; then
            clean_up
            clean_exit 2
        fi
        content=$(head -n 1 "${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

        if grep -Fi ERROR "@abs_top_builddir@/src/bin/admin/tests/kea.log"; then
            printf 'ERROR: loading memfile failed. See error above.\n' >&2
            cat "${csv}"
            clean_up
            clean_exit 4
        fi

        clean_up
      done
    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