script/fmt now runs golines -m 77 after gofmt, repeating golines until it changes nothing, since one pass can leave a line the next splits. script/fmt-check fails when either would change a file. script/bootstrap installs golines v0.13.0 with go install, pinned to its commit, and checks the installed version with go version -m. The tree is reformatted. The longer lines this makes are fixed by hand: nolint directives moved onto their own line above the statement, one string split, and 20 functions over funlen's 80 lines shortened or split, RunWAF and limitPercentages among them. Model: opus-5-5
This commit is contained in:
1 parent
63e58e326b
commit
7f8a59de04
118 files changed
+9447
-2670
No files matched your search
@@ -73,7 +73,11 @@ func TestMain(m *testing.M) {
|
||||
return nil, errNoNetwork
|
||||
},
|
||||
}
|
||||
_, _ = resolver.LookupNetIP(context.Background(), "ip4", "warm-up.invalid.")
|
||||
_, _ = resolver.LookupNetIP(
|
||||
context.Background(),
|
||||
"ip4",
|
||||
"warm-up.invalid.",
|
||||
)
|
||||
|
||||
m.Run()
|
||||
}
|
||||
@@ -91,8 +95,10 @@ func TestZonesListOrNotClientsByTheirIPv4AndIPv6Addresses(t *testing.T) {
|
||||
)
|
||||
|
||||
resolver := &resolverStandIn{answers: map[string]answer{
|
||||
"99.2.0.192." + zone + ".": {addrs: []string{listing}},
|
||||
v6Name + otherZone + ".": {addrs: []string{"127.0.0.4", "127.0.0.10"}},
|
||||
"99.2.0.192." + zone + ".": {addrs: []string{listing}},
|
||||
v6Name + otherZone + ".": {
|
||||
addrs: []string{"127.0.0.4", "127.0.0.10"},
|
||||
},
|
||||
"99.2.0.192." + otherZone + ".": {},
|
||||
}}
|
||||
dnsbl := newDNSBL(resolver, dnsblParams(zone, otherZone))
|
||||
@@ -131,8 +137,11 @@ func TestListedByNeverWaitsForAQuery(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
//
|
||||
//nolint:paralleltest // one at a time, as the comment at the top of this file says
|
||||
func TestVerdictUsedUntilTheCacheTTLHasPassedSinceItWasFetched(t *testing.T) {
|
||||
func TestVerdictUsedUntilTheCacheTTLHasPassedSinceItWasFetched(
|
||||
t *testing.T,
|
||||
) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
resolver := &resolverStandIn{answers: map[string]answer{
|
||||
listedName: {addrs: []string{listing}},
|
||||
@@ -198,8 +207,11 @@ func TestQueryNotAnsweredWithinTheTimeoutFails(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
//
|
||||
//nolint:paralleltest // one at a time, as the comment at the top of this file says
|
||||
func TestZoneThatFailsOrRefusesGivesNoVerdictAndIsLeftAloneForAMinute(t *testing.T) {
|
||||
func TestZoneThatFailsOrRefusesGivesNoVerdictAndIsLeftAloneForAMinute(
|
||||
t *testing.T,
|
||||
) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
answer answer
|
||||
@@ -230,9 +242,12 @@ func TestZoneThatFailsOrRefusesGivesNoVerdictAndIsLeftAloneForAMinute(t *testing
|
||||
p := dnsblParams(zone)
|
||||
p.Alerts = queue
|
||||
p.ProcessLog = slog.New(slog.NewJSONHandler(&log, nil))
|
||||
dnsbl := newDNSBL(&resolverStandIn{answers: map[string]answer{
|
||||
listedName: tc.answer,
|
||||
}}, p)
|
||||
dnsbl := newDNSBL(
|
||||
&resolverStandIn{answers: map[string]answer{
|
||||
listedName: tc.answer,
|
||||
}},
|
||||
p,
|
||||
)
|
||||
|
||||
// The failure gives no verdict, and the zone is not asked
|
||||
// again within a minute of it.
|
||||
@@ -258,11 +273,17 @@ func TestZoneThatFailsOrRefusesGivesNoVerdictAndIsLeftAloneForAMinute(t *testing
|
||||
Time: time.Now().Add(-time.Minute),
|
||||
Event: alerts.EventSourceFailure,
|
||||
Reason: "asking a DNSBL zone failed",
|
||||
Detail: map[string]any{"source": zone, "error": tc.error},
|
||||
Detail: map[string]any{
|
||||
"source": zone,
|
||||
"error": tc.error,
|
||||
},
|
||||
})
|
||||
|
||||
if !strings.Contains(log.String(), `"msg":"asking a DNSBL zone failed",`+
|
||||
`"zone":"`+zone+`","error":"`+tc.error) {
|
||||
if !strings.Contains(
|
||||
log.String(),
|
||||
`"msg":"asking a DNSBL zone failed",`+
|
||||
`"zone":"`+zone+`","error":"`+tc.error,
|
||||
) {
|
||||
t.Errorf("logged\n%s\nwant the failures", log.String())
|
||||
}
|
||||
})
|
||||
@@ -302,8 +323,11 @@ func TestMetricsCountEachZonesQueriesAndThoseThatFailed(t *testing.T) {
|
||||
synctest.Wait()
|
||||
|
||||
scraped := httptest.NewRecorder()
|
||||
m.ServeHTTP(scraped, httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
"/", http.NoBody))
|
||||
m.ServeHTTP(
|
||||
scraped,
|
||||
httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
"/", http.NoBody),
|
||||
)
|
||||
|
||||
for series, want := range map[string]string{
|
||||
"queries_total" + `{instance="app",source="` + zone + `"}`: "1",
|
||||
@@ -348,19 +372,32 @@ func TestZoneKeyIsMaskedInTheVerdictsTheFailuresAndTheMetrics(t *testing.T) {
|
||||
synctest.Wait()
|
||||
wantZones(t, dnsbl, listed, masked)
|
||||
|
||||
if got := waiting(queue); len(got) != 1 || got[0].Detail["source"] != masked {
|
||||
t.Errorf("alerts waiting %+v, want the failure's, from %s", got, masked)
|
||||
if got := waiting(queue); len(got) != 1 ||
|
||||
got[0].Detail["source"] != masked {
|
||||
t.Errorf(
|
||||
"alerts waiting %+v, want the failure's, from %s",
|
||||
got,
|
||||
masked,
|
||||
)
|
||||
}
|
||||
|
||||
scraped := httptest.NewRecorder()
|
||||
m.ServeHTTP(scraped, httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
"/", http.NoBody))
|
||||
m.ServeHTTP(
|
||||
scraped,
|
||||
httptest.NewRequestWithContext(t.Context(), http.MethodGet,
|
||||
"/", http.NoBody),
|
||||
)
|
||||
|
||||
for name, shown := range map[string]string{
|
||||
"the log": log.String(), "the metrics": scraped.Body.String(),
|
||||
} {
|
||||
if strings.Contains(shown, key) || !strings.Contains(shown, masked) {
|
||||
t.Errorf("%s shows the key, or does not name the zone:\n%s", name, shown)
|
||||
if strings.Contains(shown, key) ||
|
||||
!strings.Contains(shown, masked) {
|
||||
t.Errorf(
|
||||
"%s shows the key, or does not name the zone:\n%s",
|
||||
name,
|
||||
shown,
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -382,8 +419,17 @@ func TestVerdictsKeptAcrossARestart(t *testing.T) {
|
||||
kept := dnsbl.Snapshot()
|
||||
|
||||
want := []reputation.Verdict{
|
||||
{Zone: zone, Client: netip.MustParseAddr(listed), Listed: true, Fetched: fetched},
|
||||
{Zone: zone, Client: netip.MustParseAddr(unlisted), Fetched: fetched},
|
||||
{
|
||||
Zone: zone,
|
||||
Client: netip.MustParseAddr(listed),
|
||||
Listed: true,
|
||||
Fetched: fetched,
|
||||
},
|
||||
{
|
||||
Zone: zone,
|
||||
Client: netip.MustParseAddr(unlisted),
|
||||
Fetched: fetched,
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(kept, want) {
|
||||
t.Errorf("verdicts %+v, want %+v", kept, want)
|
||||
@@ -433,12 +479,17 @@ func TestNeitherAVerdictOfAZoneNotNamedNorOnePastItsTTLIsKept(t *testing.T) {
|
||||
|
||||
dnsbl.Load([]reputation.Verdict{notNamed, stale, inUse})
|
||||
|
||||
if got := dnsbl.Snapshot(); !reflect.DeepEqual(got, []reputation.Verdict{inUse}) {
|
||||
if got := dnsbl.Snapshot(); !reflect.DeepEqual(
|
||||
got,
|
||||
[]reputation.Verdict{inUse},
|
||||
) {
|
||||
t.Errorf("verdicts %+v, want only %+v", got, inUse)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAtMost100000VerdictsKeptTheOneFetchedLongestAgoDroppedFirst(t *testing.T) {
|
||||
func TestAtMost100000VerdictsKeptTheOneFetchedLongestAgoDroppedFirst(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
now := time.Date(2026, 10, 7, 0, 0, 0, 0, time.UTC)
|
||||
@@ -466,7 +517,10 @@ func TestAtMost100000VerdictsKeptTheOneFetchedLongestAgoDroppedFirst(t *testing.
|
||||
got := dnsbl.Snapshot()
|
||||
if len(got) != count-1 || !slices.Contains(got, verdicts[0]) ||
|
||||
slices.Contains(got, verdicts[count-1]) {
|
||||
t.Errorf("%d verdicts kept, want all but the one fetched longest ago", len(got))
|
||||
t.Errorf(
|
||||
"%d verdicts kept, want all but the one fetched longest ago",
|
||||
len(got),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -476,7 +530,11 @@ func TestQueriesGoToTheResolverSWWDNSBLResolverNames(t *testing.T) {
|
||||
listedName: {addrs: []string{listing}},
|
||||
}}
|
||||
|
||||
conn, err := (&net.ListenConfig{}).ListenPacket(t.Context(), "udp", "127.0.0.1:0")
|
||||
conn, err := (&net.ListenConfig{}).ListenPacket(
|
||||
t.Context(),
|
||||
"udp",
|
||||
"127.0.0.1:0",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
@@ -500,7 +558,8 @@ func TestQueriesGoToTheResolverSWWDNSBLResolverNames(t *testing.T) {
|
||||
// process held up for a whole minute would see the query fail.
|
||||
p.Timeout = time.Minute
|
||||
|
||||
isListed, err := reputation.NewDNSBL(p).LookUp(zone, netip.MustParseAddr(listed))
|
||||
isListed, err := reputation.NewDNSBL(p).
|
||||
LookUp(zone, netip.MustParseAddr(listed))
|
||||
if err != nil || !isListed {
|
||||
t.Errorf("listed %t (%v), want true", isListed, err)
|
||||
}
|
||||
@@ -554,7 +613,11 @@ func (s *resolverStandIn) set(name string, given answer) {
|
||||
// dial connects Go's resolver to the stand-in through an in-memory
|
||||
// connection, on which it sends each query, and reads each reply, after
|
||||
// its length, as over TCP.
|
||||
func (s *resolverStandIn) dial(context.Context, string, string) (net.Conn, error) {
|
||||
func (s *resolverStandIn) dial(
|
||||
context.Context,
|
||||
string,
|
||||
string,
|
||||
) (net.Conn, error) {
|
||||
client, server := net.Pipe()
|
||||
|
||||
go s.serve(server)
|
||||
@@ -590,8 +653,10 @@ func (s *resolverStandIn) serve(conn net.Conn) {
|
||||
}
|
||||
|
||||
//nolint:gosec // a reply of a few dozen bytes
|
||||
_, err = conn.Write(append(binary.BigEndian.AppendUint16(nil, uint16(len(reply))),
|
||||
reply...))
|
||||
_, err = conn.Write(
|
||||
append(binary.BigEndian.AppendUint16(nil, uint16(len(reply))),
|
||||
reply...),
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -692,7 +757,10 @@ func waitForTheResolver() {
|
||||
}
|
||||
|
||||
// newDNSBL returns the DNSBL of p, asking resolver.
|
||||
func newDNSBL(resolver *resolverStandIn, p reputation.DNSBLParams) *reputation.DNSBL {
|
||||
func newDNSBL(
|
||||
resolver *resolverStandIn,
|
||||
p reputation.DNSBLParams,
|
||||
) *reputation.DNSBL {
|
||||
dnsbl := reputation.NewDNSBL(p)
|
||||
dnsbl.SetDial(resolver.dial)
|
||||
|
||||
@@ -701,7 +769,12 @@ func newDNSBL(resolver *resolverStandIn, p reputation.DNSBLParams) *reputation.D
|
||||
|
||||
// wantZones checks the zones whose verdict dnsbl says lists client, as a
|
||||
// request from client finds them.
|
||||
func wantZones(t *testing.T, dnsbl *reputation.DNSBL, client string, want ...string) {
|
||||
func wantZones(
|
||||
t *testing.T,
|
||||
dnsbl *reputation.DNSBL,
|
||||
client string,
|
||||
want ...string,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
got := dnsbl.ListedBy(t.Context(), netip.MustParseAddr(client))
|
||||
@@ -712,12 +785,21 @@ func wantZones(t *testing.T, dnsbl *reputation.DNSBL, client string, want ...str
|
||||
|
||||
// wantQueries checks how many queries dnsbl made to zone, and how many of
|
||||
// them failed.
|
||||
func wantQueries(t *testing.T, dnsbl *reputation.DNSBL, queries, failures int) {
|
||||
func wantQueries(
|
||||
t *testing.T,
|
||||
dnsbl *reputation.DNSBL,
|
||||
queries, failures int,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
if dnsbl.Queries(zone) != queries || dnsbl.Failures(zone) != failures {
|
||||
t.Errorf("%d queries and %d failures, want %d and %d", dnsbl.Queries(zone),
|
||||
dnsbl.Failures(zone), queries, failures)
|
||||
t.Errorf(
|
||||
"%d queries and %d failures, want %d and %d",
|
||||
dnsbl.Queries(zone),
|
||||
dnsbl.Failures(zone),
|
||||
queries,
|
||||
failures,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user