check / check (push) Canceled after 0s
Stopping dnswatcher during a DNS check logged every lookup the stop cut short as an error, "context canceled". The four places in the watcher that log a failed lookup, checkDomain, checkHostname, resolveNameserverAddresses and resolveCNAMEAddresses, now do it through logFailedLookup, which logs nothing when the watcher's context was cancelled. It asks the context, not the lookup's error: the resolver reports a cancelled lookup with its own error, which does not wrap context.Canceled. A context whose deadline passed is not cancelled, so a lookup that ran out of time is still logged. The tests run each of these lookups on a cancelled context and on one whose deadline passed; neither sends a query. Model: opus-5-5
116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package watcher
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"sneak.berlin/go/dnswatcher/internal/config"
|
|
"sneak.berlin/go/dnswatcher/internal/resolver"
|
|
"sneak.berlin/go/dnswatcher/internal/state"
|
|
)
|
|
|
|
// NewForTest creates a Watcher without fx for unit testing.
|
|
func NewForTest(
|
|
cfg *config.Config,
|
|
st *state.State,
|
|
res DNSResolver,
|
|
pc PortChecker,
|
|
tc TLSChecker,
|
|
n Notifier,
|
|
) *Watcher {
|
|
return &Watcher{
|
|
log: slog.Default(),
|
|
config: cfg,
|
|
state: st,
|
|
resolver: res,
|
|
portCheck: pc,
|
|
tlsCheck: tc,
|
|
notify: n,
|
|
firstRun: true,
|
|
}
|
|
}
|
|
|
|
// SetLogger replaces the watcher's logger, so a test can read what it
|
|
// logs.
|
|
func (w *Watcher) SetLogger(log *slog.Logger) {
|
|
w.log = log
|
|
}
|
|
|
|
// NewlyDisagreeingPairs exports newlyDisagreeingPairs for testing.
|
|
func NewlyDisagreeingPairs(
|
|
prev, current *state.HostnameState,
|
|
) [][2]string {
|
|
return newlyDisagreeingPairs(prev, current)
|
|
}
|
|
|
|
// SetFirstRun sets whether the watcher is on its first check, in which
|
|
// nothing is compared with the previous check. NewForTest's watcher is.
|
|
func (w *Watcher) SetFirstRun(firstRun bool) {
|
|
w.firstRun = firstRun
|
|
}
|
|
|
|
// UpdateHostnameState exports updateHostnameState for testing.
|
|
func (w *Watcher) UpdateHostnameState(
|
|
ctx context.Context,
|
|
hostname string,
|
|
newState *state.HostnameState,
|
|
) {
|
|
w.updateHostnameState(ctx, hostname, newState)
|
|
}
|
|
|
|
// DetectHostnameChanges exports detectHostnameChanges for testing.
|
|
func (w *Watcher) DetectHostnameChanges(
|
|
ctx context.Context,
|
|
hostname string,
|
|
prev, current *state.HostnameState,
|
|
) {
|
|
w.detectHostnameChanges(ctx, hostname, prev, current)
|
|
}
|
|
|
|
// ResolveNameserverAddresses exports resolveNameserverAddresses for
|
|
// testing.
|
|
func (w *Watcher) ResolveNameserverAddresses(
|
|
ctx context.Context,
|
|
nameservers []string,
|
|
prev map[string][]string,
|
|
) map[string][]string {
|
|
return w.resolveNameserverAddresses(ctx, nameservers, prev)
|
|
}
|
|
|
|
// ResolveCNAMEAddresses exports resolveCNAMEAddresses for testing.
|
|
func (w *Watcher) ResolveCNAMEAddresses(
|
|
ctx context.Context,
|
|
hostname string,
|
|
current, prev *state.HostnameState,
|
|
) {
|
|
w.resolveCNAMEAddresses(ctx, hostname, current, prev)
|
|
}
|
|
|
|
// DetectNSAddressChanges exports detectNSAddressChanges for testing.
|
|
func (w *Watcher) DetectNSAddressChanges(
|
|
ctx context.Context,
|
|
domain string,
|
|
prev, current map[string][]string,
|
|
) {
|
|
w.detectNSAddressChanges(ctx, domain, prev, current)
|
|
}
|
|
|
|
// CheckAllPorts exports checkAllPorts for testing.
|
|
func (w *Watcher) CheckAllPorts(ctx context.Context) {
|
|
w.checkAllPorts(ctx)
|
|
}
|
|
|
|
// RunTLSChecks exports runTLSChecks for testing.
|
|
func (w *Watcher) RunTLSChecks(ctx context.Context) {
|
|
w.runTLSChecks(ctx)
|
|
}
|
|
|
|
// BuildHostnameState exports buildHostnameState for testing.
|
|
func BuildHostnameState(
|
|
results map[string]*resolver.NameserverResponse,
|
|
now time.Time,
|
|
) *state.HostnameState {
|
|
return buildHostnameState(results, now)
|
|
}
|