Files
fancy-dns/internal/config/config_test.go
T
developer 9881d2a095 Make the API response contract a test; fix what it found
The console's TypeScript types are hand-written and apiFetch ends in
`JSON.parse(text) as T`, so nothing in the toolchain compared them with the
Go structs. A field that lost its json tag, or a response a struct never had,
showed up as an empty table in a browser and nowhere else: the Go suite was
green and the console was wrong. internal/app/contract_test.go now asserts the
exact JSON key set of every admin endpoint, driven through the real router
with the real hook implementations, plus a test that drives the two write
endpoints with the exact payload web/src builds.

The drift it found, all four fixed here:

blocklist.Source and SourceMeta had no json tags at all, so GET /api/blocklists
sent Go field names ("URL"/"Format") where the console expects snake_case: the
Sources table rendered blank rows and its per-row toggle posted
`name: undefined`. Tagged to match ParseStats.

acl.GroupSummary was flat where the console reads a nested `match` object — the
same shape config.Group has — so every group card read "matches nothing". The
four match fields are nested now, under a new acl.Match.

PUT /api/clients answered 400 `json: unknown field "ptr_suffixes"` to the
console's payload, because config.Group and config.Match carried yaml tags only
and the decoder runs with DisallowUnknownFields. Both structs have json tags
now; the same applies to config.UpstreamServer and UpstreamBootstrap.

PUT /api/upstreams answered 400 `json: unknown field "health"`, because the
console round-trips the whole server object it read, runtime health fields
included. They are accepted and ignored, because the server owns them, and they
are `yaml:"-"` so settings.yaml does not gain runtime state.

Also fixed, each with a test:

A duration on the wire was a nanosecond count. config.Duration had a
MarshalYAML but no MarshalJSON, so bootstraps[].refresh arrived as 30000000000
while the console typed the field as a string, and healthcheck_interval was a
float of seconds in one place and a string in another. Durations on the wire
are duration strings everywhere now — the same spelling config.yaml uses — and a
bare JSON number is still accepted and means seconds, not nanoseconds.
TestDurationJSONForm used to pin the old behaviour and now pins the new one,
saying in its comment that the old behaviour was the bug.

A log row's key set depended on the row. api.LogEntry was omitempty on
client_group, response, upstream and list, so a query with no ACL group
arrived without the key and the console rendered "undefined" where the column
should be empty. The contract test can only pin one shape if the server emits
one, so nothing on that struct is omitempty any more.

A dead peer stayed in the cluster peer list forever. Cluster.seen was only ever
added to and deleted from on an explicit `bye`, so a node that crashed — or
whose Valkey connection died — was reported as a live peer for the life of the
process, and its stale blocklist digest was flagged as divergence forever.
Peers() now reaps peers unheard for peer_ttl*4 and stamps each state with the
receiver's own clock, so clock skew between two machines can neither reap a
healthy peer nor keep a dead one. Discover() ages out the same way on the
publisher's own last_seen stamp, because the keyed Valkey record outlives a
crash: a live node keeps refreshing the key's TTL, so a dead node's field stayed
readable for the life of the fleet.

peer_ttl: 0 would have crashed the process. time.NewTicker(PeerTTL/2) panics on
a non-positive period and `EXPIRE key 0` deletes the heartbeat key, so a config
that left peer_ttl unset would have taken the process down at Start, or made
every instance believe it is alone in the fleet. Both go through peerTTL() and
peerDeadline(), which fall back to the default.

Two cluster tests raced on the wire, not on the product, and one was
environment-dependent rather than wrong. A node that joined after another's
heartbeat was in flight hears about it on the next tick, so "b does not see a"
read immediately after "a sees b" is a coin flip; the divergence test waited for
a digest on the wrong node's view of the fleet; and 127.0.0.1:1 is only
unbindable outside a container that sets
net.ipv4.ip_unprivileged_port_start=0, where it binds and the partial-bind test
fails with both transports up. That test now occupies a port with a live
listener in the test process, which is a deterministic EADDRINUSE on every
machine and every uid.

internal/server/counters_test.go covers the counter-key layer that no other
test touched: the "<transport>.<field>" keyspace, that the aggregate folds only
the known fields, and that the snapshot is safe while counters move.

Two pinned behaviours changed on purpose, each with a test that says so:

An RPZ record did not cover the name it names. rpzRule gave a bare owner name
the subdomain form (*.name), so analytics.example.org. in a policy zone blocked
cdn.analytics.example.org. and left analytics.example.org. resolving normally —
and an rpz-passthru. record did not allowlist its own domain either. RPZ
semantics say the record applies to the domain and everything under it, so a
bare owner now takes the apex form **.name; an owner written with an explicit *.
is still strict, so a zone that wants subdomain-only behaviour can still ask for
it. This changes which queries get blocked for an existing policy zone, which is
why it wanted a decision. TestExampleRPZZoneCoversTheApexItNames pins both
halves and the example zone now says which half it relies on.

metrics.OutcomeLocal had no producer. The engine reported a local-zone answer as
qlog.ActionAllow, which outcomeFor mapped to OutcomeUpstream, so every locally
answered query was counted as an upstream one and the outcome="local" series was
never published — the dashboard's local column was permanently empty.
Response.Local is now set on the local-zone path and outcomeFor consults it
first. That moves published counters, so the README's metrics section says so
too.
2026-09-27 18:42:59 +00:00

374 lines
13 KiB
Go

package config
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"gopkg.in/yaml.v3"
)
func TestDurationUnmarshalYAMLAccepted(t *testing.T) {
tests := []struct {
name string
node string
want time.Duration
}{
{"seconds", `5s`, 5 * time.Second},
{"compound minutes seconds", `1m30s`, 90 * time.Second},
{"milliseconds", `250ms`, 250 * time.Millisecond},
{"hours minutes", `1h30m`, 90 * time.Minute},
{"sub second", `1.5s`, 1500 * time.Millisecond},
{"microseconds", `10us`, 10 * time.Microsecond},
{"zero", `0s`, 0},
{"negative", `-5s`, -5 * time.Second},
{"padded", `" 30s "`, 30 * time.Second},
{"empty string is zero", `""`, 0},
{"null is zero", `null`, 0},
{"tilde is zero", `~`, 0},
{"int seconds", `7`, 7 * time.Second},
{"float seconds", `1.5`, 1500 * time.Millisecond},
{"negative int seconds", `-7`, -7 * time.Second},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var got struct {
D Duration `yaml:"d"`
}
if err := yaml.Unmarshal([]byte("d: "+tc.node), &got); err != nil {
t.Fatalf("unmarshal %s: %v", tc.node, err)
}
if got.D.D() != tc.want {
t.Errorf("d: %s = %v, want %v", tc.node, got.D.D(), tc.want)
}
})
}
}
func TestDurationUnmarshalYAMLRejected(t *testing.T) {
tests := []struct {
name string
node string
mention string
}{
{"nonsense", `abc`, `"abc"`},
{"nonsense", `abc`, `"abc"`},
{"space before unit", `"5 s"`, `"5 s"`},
{"unknown unit", `10x`, `"10x"`},
{"overflow", `2562048h`, `"2562048h"`},
{"float overflow", `1e400s`, `1e400s`},
{"numeric seconds overflow", `1e30`, `"1e30"`},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var got struct {
D Duration `yaml:"d"`
}
err := yaml.Unmarshal([]byte("d: "+tc.node), &got)
if err == nil {
t.Fatalf("unmarshal %s: expected error, got %v", tc.node, got.D.D())
}
if !strings.Contains(err.Error(), tc.mention) {
t.Errorf("error %q does not mention offending value %s", err, tc.mention)
}
})
}
}
func TestDurationUnmarshalYAMLNonScalar(t *testing.T) {
for _, node := range []string{`{a: 1}`, `[1,2]`} {
var got struct {
D Duration `yaml:"d"`
}
if err := yaml.Unmarshal([]byte("d: "+node), &got); err == nil {
t.Errorf("unmarshal d: %s: expected error, got %v", node, got.D.D())
}
}
}
func TestDurationMarshalYAMLRoundTrip(t *testing.T) {
for _, d := range []Duration{
Duration(5 * time.Second),
Duration(90 * time.Second),
Duration(250 * time.Millisecond),
Duration(12 * time.Hour),
Duration(0),
} {
out, err := yaml.Marshal(map[string]Duration{"d": d})
if err != nil {
t.Fatalf("marshal %v: %v", d.D(), err)
}
var back struct {
D Duration `yaml:"d"`
}
if err := yaml.Unmarshal(out, &back); err != nil {
t.Fatalf("unmarshal %q: %v", out, err)
}
if back.D != d {
t.Errorf("round trip %v via %q: got %v", d.D(), strings.TrimSpace(string(out)), back.D.D())
}
}
}
// TestDurationJSONForm pins the wire form of a Duration. This test used to
// pin the encoding/json default — a bare nanosecond count, and a quoted
// duration as an error — because a config.Duration had no custom JSON
// marshaller at all.
//
// DECIDED: that default was wrong on both sides. The admin console declares
// these fields as strings (`bootstraps[].refresh`, `healthcheck_interval`), the
// same spelling config.yaml uses, and a nanosecond count on the wire is
// something no client means. Duration now marshals to "5s" and unmarshals from
// a duration string; a bare number is accepted as SECONDS rather than
// nanoseconds, so a hand-written client asking for `{"healthcheck_interval":30}`
// gets 30s and not 30ns.
func TestDurationJSONForm(t *testing.T) {
var got struct {
D Duration `json:"d"`
}
if err := json.Unmarshal([]byte(`{"d":5}`), &got); err != nil {
t.Fatalf("bare number: %v", err)
}
if got.D.D() != 5*time.Second {
t.Errorf("bare JSON number = %v, want 5s (a number is seconds, not nanoseconds)", got.D.D())
}
if err := json.Unmarshal([]byte(`{"d":"5s"}`), &got); err != nil {
t.Fatalf("quoted duration: %v", err)
}
if got.D.D() != 5*time.Second {
t.Errorf("quoted JSON duration = %v, want 5s", got.D.D())
}
if err := json.Unmarshal([]byte(`{"d":"nonsense"}`), &got); err == nil {
t.Error("an unparseable duration string decoded without an error")
}
if err := json.Unmarshal([]byte(`{"d":true}`), &got); err == nil {
t.Error("a boolean decoded as a duration without an error")
}
out, err := json.Marshal(Duration(5 * time.Second))
if err != nil {
t.Fatalf("marshal: %v", err)
}
if string(out) != `"5s"` {
t.Errorf("json.Marshal = %s, want \"5s\"", out)
}
// The yaml form is unchanged, so a config file and the API agree.
y, err := Duration(90 * time.Second).MarshalYAML()
if err != nil {
t.Fatalf("MarshalYAML: %v", err)
}
if y != "1m30s" {
t.Errorf("MarshalYAML = %v, want 1m30s", y)
}
}
// clearEnv removes name for the rest of the test, restoring it afterwards.
func clearEnv(t *testing.T, name string) {
t.Helper()
if old, ok := os.LookupEnv(name); ok {
t.Setenv(name, old) // registers the restore-on-cleanup
}
if err := os.Unsetenv(name); err != nil {
t.Fatalf("unset %s: %v", name, err)
}
}
func TestExpandEnvSubstitution(t *testing.T) {
t.Setenv("FD_TEST_KEY", "s3cr3t")
t.Setenv("FD_TEST_EMPTY", "")
t.Setenv("FD_TEST_NESTED", "${FD_TEST_KEY}")
clearEnv(t, "FD_TEST_MISSING")
tests := []struct {
name, in, want string
}{
{"double quoted scalar", `api_keys: ["${FD_TEST_KEY}"]` + "\n", `api_keys: ["s3cr3t"]` + "\n"},
{"plain unquoted scalar", "cert_file: /etc/${FD_TEST_KEY}.pem\n", "cert_file: /etc/s3cr3t.pem\n"},
{"single quoted scalar", "password: '${FD_TEST_KEY}'\n", "password: 's3cr3t'\n"},
{"whole value", "password: ${FD_TEST_KEY}\n", "password: s3cr3t\n"},
{"two references on one line", "x: ${FD_TEST_KEY}/${FD_TEST_KEY}\n", "x: s3cr3t/s3cr3t\n"},
{"value is not rescanned", "x: ${FD_TEST_NESTED}\n", "x: ${FD_TEST_KEY}\n"},
{"reference line comment", "# api_keys: ${FD_TEST_KEY}\n", "# api_keys: ${FD_TEST_KEY}\n"},
{"reference trailing comment", "level: info # set ${FD_TEST_KEY} later\n", "level: info # set ${FD_TEST_KEY} later\n"},
{"indented comment", "dns:\n # port ${FD_TEST_KEY}\n max_udp_size: 1232\n", "dns:\n # port ${FD_TEST_KEY}\n max_udp_size: 1232\n"},
{"bare dollar in url", "url: https://x.invalid/?a=$b\n", "url: https://x.invalid/?a=$b\n"},
{"bare dollar blocklist line", " url: https://h.invalid/hosts?$format=raw$other\n", " url: https://h.invalid/hosts?$format=raw$other\n"},
{"bare dollar at end of line", "url: https://x.invalid/$\n", "url: https://x.invalid/$\n"},
{"colon default when unset", "x: ${FD_TEST_MISSING:-fallback}\n", "x: fallback\n"},
{"hyphen default when unset", "x: [${FD_TEST_MISSING-fallback}]\n", "x: [fallback]\n"},
{"colon default when empty", "x: ${FD_TEST_EMPTY:-fallback}\n", "x: fallback\n"},
{"hyphen default when empty stays empty", "x: [${FD_TEST_EMPTY-fallback}]\n", "x: []\n"},
{"empty default", "x: [${FD_TEST_MISSING:-}]\n", "x: []\n"},
{"empty value without default", "x: [${FD_TEST_EMPTY}]\n", "x: []\n"},
{"hash without leading space is data", "x: a#${FD_TEST_KEY}\n", "x: a#s3cr3t\n"},
{"hash inside double quotes is data", "x: \"# ${FD_TEST_KEY}\"\n", "x: \"# s3cr3t\"\n"},
{"hash inside single quotes is data", "x: '# ${FD_TEST_KEY}'\n", "x: '# s3cr3t'\n"},
{"crlf line ending", "x: ${FD_TEST_KEY}\r\ny: 1\r\n", "x: s3cr3t\r\ny: 1\r\n"},
{"crlf comment", "# ${FD_TEST_KEY}\r\n", "# ${FD_TEST_KEY}\r\n"},
{"no trailing newline", "x: ${FD_TEST_KEY}", "x: s3cr3t"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := expandEnv([]byte(tc.in))
if err != nil {
t.Fatalf("expandEnv(%q): %v", tc.in, err)
}
if string(got) != tc.want {
t.Errorf("expandEnv(%q)\n got %q\nwant %q", tc.in, got, tc.want)
}
})
}
}
func TestExpandEnvMultiLine(t *testing.T) {
t.Setenv("FD_TEST_KEY", "abc123")
clearEnv(t, "FD_TEST_MISSING")
in := strings.Join([]string{
"# leading comment ${FD_TEST_KEY}",
"api:",
" auth:",
` api_keys: ["${FD_TEST_KEY}"] # trailing ${FD_TEST_KEY}`,
" username: ${FD_TEST_MISSING:-admin}",
" password: ${FD_TEST_MISSING-none}",
"",
"metrics:",
" enabled: false # ${FD_TEST_MISSING} in a comment is fine",
"",
}, "\n")
want := strings.Join([]string{
"# leading comment ${FD_TEST_KEY}",
"api:",
" auth:",
` api_keys: ["abc123"] # trailing ${FD_TEST_KEY}`,
" username: admin",
" password: none",
"",
"metrics:",
" enabled: false # ${FD_TEST_MISSING} in a comment is fine",
"",
}, "\n")
got, err := expandEnv([]byte(in))
if err != nil {
t.Fatalf("expandEnv: %v", err)
}
if string(got) != want {
t.Errorf("expandEnv multi-line\n got %q\nwant %q", got, want)
}
}
func TestExpandEnvWithoutReferencesIsByteIdentical(t *testing.T) {
// A bare "$" is not a reference: a blocklist URL with a query string must
// survive untouched. The "#" cases are comments, and a comment must not be
// scanned for references at all — documenting "${VAR}" in a config comment
// must not make the config unloadable.
// A bare "$" is not a reference: a blocklist URL with a query string must
// survive untouched. A real comment must not be scanned for references at
// all — documenting "${VAR}" in a config comment must not make the config
// unloadable. (A "#" inside quotes is data, not a comment, so a reference
// there *is* live; that is covered by TestExpandEnvSubstitution.)
raw := []byte("# no references here\nurl: https://x.invalid/?a=$b&c=$d\n# and ${FD_TEST_MISSING} on a comment line\n")
clearEnv(t, "FD_TEST_MISSING")
got, err := expandEnv(raw)
if err != nil {
t.Fatalf("expandEnv: %v", err)
}
if !bytes.Equal(got, raw) {
t.Errorf("expandEnv changed a document with no references:\n got %q\nwant %q", got, raw)
}
}
func TestExpandEnvErrors(t *testing.T) {
clearEnv(t, "FD_TEST_MISSING")
t.Setenv("FD_TEST_KEY", "abc123")
tests := []struct {
name, in string
mentions []string
}{
{"unset without default", "x: ${FD_TEST_MISSING}\n", []string{"FD_TEST_MISSING", "line 1:"}},
{"unset on third line", "a: 1\nb: 2\nc: ${FD_TEST_MISSING}\n", []string{"FD_TEST_MISSING", "line 3:"}},
{"empty-but-set without default", "x: [${FD_TEST_MISSING}]\n", []string{"FD_TEST_MISSING"}},
{"unterminated reference", "x: ${FD_TEST_KEY\n", []string{"unterminated ${"}},
{"unterminated on later line", "a: 1\nb: ${FD_TEST_KEY\n", []string{"unterminated ${", "line 2:"}},
{"empty name", "x: ${}\n", []string{"empty variable name in ${}"}},
{"empty name with colon default", "x: ${:-fallback}\n", []string{"empty variable name"}},
{"unterminated inside comment is ignored", "# x: ${FD_TEST_KEY\ny: 1\n", nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := expandEnv([]byte(tc.in))
if len(tc.mentions) == 0 {
if err != nil {
t.Fatalf("expandEnv(%q) = %v, want success", tc.in, err)
}
if string(got) != tc.in {
t.Errorf("expandEnv(%q) = %q, want it copied verbatim", tc.in, got)
}
return
}
if err == nil {
t.Fatalf("expandEnv(%q) = %q, want error", tc.in, got)
}
for _, m := range tc.mentions {
if !strings.Contains(err.Error(), m) {
t.Errorf("error %q does not mention %q", err, m)
}
}
})
}
}
func TestLoadSubstitutesEnvInFile(t *testing.T) {
t.Setenv("FD_TEST_KEY", "long-random-api-key")
t.Setenv("FD_TEST_PW", "p@ss")
path := writeTempConfig(t, `api:
auth:
mode: apikey
api_keys: ["${FD_TEST_KEY}"]
username: 'admin-${FD_TEST_PW}'
password: prefix-${FD_TEST_PW}
# the key is documented here: ${FD_TEST_KEY}
blocklist:
block_entries:
- "example.com"
`)
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
want := []string{"long-random-api-key"}
if len(cfg.API.Auth.APIKeys) != 1 || cfg.API.Auth.APIKeys[0] != want[0] {
t.Errorf("api_keys = %q, want %q", cfg.API.Auth.APIKeys, want)
}
if cfg.API.Auth.Username != "admin-p@ss" {
t.Errorf("username = %q, want admin-p@ss", cfg.API.Auth.Username)
}
if cfg.API.Auth.Password != "prefix-p@ss" {
t.Errorf("password = %q, want prefix-p@ss", cfg.API.Auth.Password)
}
}
func TestLoadUnsetEnvIsFatalAndNamesVariable(t *testing.T) {
clearEnv(t, "FD_TEST_MISSING")
path := writeTempConfig(t, "api:\n auth:\n mode: apikey\n api_keys: [\"${FD_TEST_MISSING}\"]\nblocklist:\n block_entries:\n - \"example.com\"\n")
_, err := Load(path)
if err == nil {
t.Fatal("Load: want error for unset variable")
}
for _, want := range []string{"FD_TEST_MISSING", path} {
if !strings.Contains(err.Error(), want) {
t.Errorf("error %q does not mention %q", err, want)
}
}
}
func writeTempConfig(t *testing.T, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "config.yaml")
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
t.Fatalf("write config: %v", err)
}
return path
}