watcher: keep port state when no nameserver of a name answered (closes #193)
check / check (push) Failing after 2m13s

The port check removed the saved port state of every address no
configured name resolves to. A name whose nameservers all timed out
or failed is saved with no records, so its addresses looked gone and
their port state was removed; when the nameservers answered again the
port state was recorded afresh, and a port that opened or closed in
the meantime was not notified.

The removal now keeps an entry when one of the names saved on it is
configured and none of its nameservers answered on its last check. A
name whose nameservers answer with no addresses still loses it, and so
does a name no longer configured, whose saved state is never checked
again.

Model: opus-5-5
This commit is contained in:
2026-10-01 22:04:12 +00:00
parent b5814b2451
commit 48684f9e69
5 changed files with 104 additions and 4 deletions
+59
View File
@@ -331,3 +331,62 @@ func TestNameserverThatRefuses(t *testing.T) {
)
}
}
// TestPortStateWhenNoNameserverAnswered runs the port checks on
// hostname state built here, which gives the hostname no address. The
// port state saved for its old address is kept only when the hostname
// is configured and none of its nameservers answered.
func TestPortStateWhenNoNameserverAnswered(t *testing.T) {
t.Parallel()
noneAnswered := saved(map[string]*state.NameserverRecordState{
nsA: failed(), nsB: failed(),
})
oneAnsweredNoAddress := saved(map[string]*state.NameserverRecordState{
nsA: answered(map[string][]string{}), nsB: failed(),
})
tests := []struct {
name string
hostname *state.HostnameState
configured bool
wantKept bool
}{
{"no nameserver answered", noneAnswered, true, true},
{"one answered with no address", oneAnsweredNoAddress, true, false},
{"no nameserver answered, not configured", noneAnswered, false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
cfg := defaultTestConfig(t)
if tt.configured {
cfg.Hostnames = []string{host}
}
// The port checks read the saved hostname state and look
// nothing up, so the watcher has no resolver.
deps := newTestDeps(t, cfg)
w := watcher.NewForTest(
cfg, deps.state, nil,
deps.portChecker, deps.tlsChecker, deps.notifier,
)
key := ip1 + ":443"
deps.state.SetHostnameState(host, tt.hostname)
deps.state.SetPortState(key, &state.PortState{
Open: true, Hostnames: []string{host},
})
w.CheckAllPorts(t.Context())
_, kept := deps.state.GetPortState(key)
if kept != tt.wantKept {
t.Errorf("port state %s kept: %v, want %v", key, kept, tt.wantKept)
}
})
}
}