test: add t.Helper() calls to test helper functions (#7351)

This commit is contained in:
Ville Vesilehto 2025-06-05 00:36:04 +03:00 committed by GitHub
parent b4abdcacf0
commit 9f9aed31cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 52 additions and 13 deletions

View File

@ -10,6 +10,7 @@ linters:
- nolintlint
- protogetter
- staticcheck
- thelper
- unconvert
- unused
- usetesting

View File

@ -17,6 +17,7 @@ var (
)
func testServerHTTPS(t *testing.T, path string, validator func(*http.Request) bool) *http.Response {
t.Helper()
c := Config{
Zone: "example.com.",
Transport: "https",

View File

@ -62,6 +62,7 @@ func TestWalkNonExistent(t *testing.T) {
}
func createFiles(t *testing.T) (string, error) {
t.Helper()
dir := t.TempDir()
for _, name := range dbFiles {

View File

@ -30,6 +30,7 @@ func TestReset(t *testing.T) {
}
func hitsCheck(t *testing.T, f *Freq, expected int) {
t.Helper()
if x := f.Hits(); x != expected {
t.Fatalf("Expected hits to be %d, got %d", expected, x)
}

View File

@ -248,6 +248,7 @@ func testEmptyMsg() *dns.Msg {
}
func newDnssec(t *testing.T, zones []string) (Dnssec, func(), func()) {
t.Helper()
k, rm1, rm2 := newKey(t)
c := cache.New(defaultCap)
d := New(zones, []*DNSKEY{k}, false, nil, c)
@ -255,6 +256,7 @@ func newDnssec(t *testing.T, zones []string) (Dnssec, func(), func()) {
}
func newKey(t *testing.T) (*DNSKEY, func(), func()) {
t.Helper()
fPriv, rmPriv, _ := test.TempFile(".", privKey)
fPub, rmPub, _ := test.TempFile(".", pubKey)

View File

@ -14,6 +14,7 @@ import (
)
func testCase(t *testing.T, tapq, tapr *tap.Dnstap, q, r *dns.Msg, extraFormat string) {
t.Helper()
w := writer{t: t}
w.queue = append(w.queue, tapq, tapr)
h := Dnstap{

View File

@ -18,6 +18,7 @@ var (
)
func accept(t *testing.T, l net.Listener, count int) {
t.Helper()
server, err := l.Accept()
if err != nil {
t.Fatalf("Server accepted: %s", err)

View File

@ -170,6 +170,7 @@ func TestLookupSecureDelegation(t *testing.T) {
}
func testDelegation(t *testing.T, z, origin string, testcases []test.Case) {
t.Helper()
zone, err := Parse(strings.NewReader(z), origin, "stdin", 0)
if err != nil {
t.Fatalf("Expect no error when reading zone, got %q", err)

View File

@ -71,6 +71,7 @@ func TestMetadata(t *testing.T) {
}
func testMetadata(t *testing.T, state request.Request, geoIP *GeoIP, label, expectedValue string) {
t.Helper()
ctx := metadata.ContextWithMetadata(context.Background())
rCtx := geoIP.Metadata(ctx, state)
if fmt.Sprintf("%p", ctx) != fmt.Sprintf("%p", rCtx) {

View File

@ -120,6 +120,7 @@ func TestLookupStaticHost(t *testing.T) {
}
func testStaticHost(t *testing.T, ent staticHostEntry, h *Hostsfile) {
t.Helper()
ins := []string{ent.in, plugin.Name(ent.in).Normalize(), strings.ToLower(ent.in), strings.ToUpper(ent.in)}
for k, in := range ins {
addrsV4 := h.LookupStaticHostV4(in)
@ -206,6 +207,7 @@ func TestLookupStaticAddr(t *testing.T) {
}
func testStaticAddr(t *testing.T, ent staticIPEntry, h *Hostsfile) {
t.Helper()
hosts := h.LookupStaticAddr(ent.in)
for i := range ent.out {
ent.out[i] = plugin.Name(ent.out[i]).Normalize()

View File

@ -21,6 +21,7 @@ func TestDefaultProcessor(t *testing.T) {
}
func testProcessor(t *testing.T, processor cache.ProcessFunc, idx cache.Indexer) {
t.Helper()
obj := &api.Service{
ObjectMeta: metav1.ObjectMeta{Name: "service1", Namespace: "test1"},
Spec: api.ServiceSpec{
@ -129,6 +130,7 @@ func TestDefaultProcessorWithPod(t *testing.T) {
}
func testProcessorWithPod(t *testing.T, processor cache.ProcessFunc, idx cache.Indexer) {
t.Helper()
container := api.Container{}
obj := &api.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "test1"},

View File

@ -105,6 +105,7 @@ func TestKubernetesIXFRCurrent(t *testing.T) {
}
func validateAXFR(t *testing.T, ch <-chan []dns.RR, multicluster bool) {
t.Helper()
xfr := []dns.RR{}
for rrs := range ch {
xfr = append(xfr, rrs...)

View File

@ -136,6 +136,7 @@ func TestWeightFileUpdate(t *testing.T) {
}
func checkDomainsWRR(t *testing.T, testIndex int, expectedDomains, domains map[string]weights) error {
t.Helper()
var ret error
retError := errors.New("Check domains failed")
for dname, expectedWeights := range expectedDomains {
@ -410,6 +411,7 @@ func TestLoadBalanceWRR(t *testing.T) {
}
func checkTopIP(t *testing.T, i, j int, result []dns.RR, expectedTopIP string) {
t.Helper()
expected := net.ParseIP(expectedTopIP)
for _, r := range result {
switch r.Header().Rrtype {

View File

@ -43,6 +43,7 @@ func TestMultipleListenerMock(t *testing.T) {
}
func testListenersCalled(t *testing.T, listenerNames []string, outputs []string) {
t.Helper()
for _, name := range listenerNames {
err := RegisterListener(NewMockListener(name))
if err != nil {

View File

@ -8,6 +8,7 @@ import (
)
func getPEMFiles(t *testing.T) (cert, key, ca string) {
t.Helper()
tempDir, err := test.WritePEMFiles(t)
if err != nil {
t.Fatalf("Could not write PEM files: %s", err)

View File

@ -196,6 +196,7 @@ func TestQuicSetup(t *testing.T) {
// assertMaxStreamsValue compares the actual MaxQUICStreams value with the expected one
func assertMaxStreamsValue(t *testing.T, testIndex int, testInput string, actual, expected *int) {
t.Helper()
if actual == nil && expected == nil {
return
}
@ -214,6 +215,7 @@ func assertMaxStreamsValue(t *testing.T, testIndex int, testInput string, actual
// assertWorkerPoolSizeValue compares the actual MaxQUICWorkerPoolSize value with the expected one
func assertWorkerPoolSizeValue(t *testing.T, testIndex int, testInput string, actual, expected *int) {
t.Helper()
if actual == nil && expected == nil {
return
}

View File

@ -88,10 +88,11 @@ func TestCNameTargetRewrite(t *testing.T) {
cnameTargetRule.Upstream = &MockedUpstream{}
rules = append(rules, rule)
}
doTestCNameTargetTests(rules, t)
doTestCNameTargetTests(t, rules)
}
func doTestCNameTargetTests(rules []Rule, t *testing.T) {
func doTestCNameTargetTests(t *testing.T, rules []Rule) {
t.Helper()
tests := []struct {
from string
fromType uint16

View File

@ -35,16 +35,17 @@ func TestResponseReverter(t *testing.T) {
r, _ := newNameRule("stop", "regex", `(core)\.(dns)\.(rocks)`, "{2}.{1}.{3}", "answer", "name", `(dns)\.(core)\.(rocks)`, "{2}.{1}.{3}")
rules = append(rules, r)
doReverterTests(rules, t)
doReverterTests(t, rules)
rules = []Rule{}
r, _ = newNameRule("continue", "regex", `(core)\.(dns)\.(rocks)`, "{2}.{1}.{3}", "answer", "name", `(dns)\.(core)\.(rocks)`, "{2}.{1}.{3}")
rules = append(rules, r)
doReverterTests(rules, t)
doReverterTests(t, rules)
}
func doReverterTests(rules []Rule, t *testing.T) {
func doReverterTests(t *testing.T, rules []Rule) {
t.Helper()
ctx := context.TODO()
for i, tc := range tests {
m := new(dns.Msg)
@ -105,7 +106,7 @@ func TestValueResponseReverter(t *testing.T) {
}
rules = append(rules, r)
doValueReverterTests("stop", rules, t)
doValueReverterTests(t, "stop", rules)
rules = []Rule{}
r, err = newNameRule("continue", "regex", `(.*)\.domain\.uk`, "{1}.cluster.local", "answer", "name", `(.*)\.cluster\.local`, "{1}.domain.uk", "answer", "value", `(.*)\.cluster\.local`, "{1}.domain.uk")
@ -115,7 +116,7 @@ func TestValueResponseReverter(t *testing.T) {
}
rules = append(rules, r)
doValueReverterTests("continue", rules, t)
doValueReverterTests(t, "continue", rules)
rules = []Rule{}
r, err = newNameRule("stop", "suffix", `.domain.uk`, ".cluster.local", "answer", "auto", "answer", "value", `(.*)\.cluster\.local`, "{1}.domain.uk")
@ -125,7 +126,7 @@ func TestValueResponseReverter(t *testing.T) {
}
rules = append(rules, r)
doValueReverterTests("suffix", rules, t)
doValueReverterTests(t, "suffix", rules)
// multiple rules
rules = []Rule{}
@ -143,10 +144,11 @@ func TestValueResponseReverter(t *testing.T) {
}
rules = append(rules, r)
doValueReverterTests("suffix_multiple", rules, t)
doValueReverterTests(t, "suffix_multiple", rules)
}
func doValueReverterTests(name string, rules []Rule, t *testing.T) {
func doValueReverterTests(t *testing.T, name string, rules []Rule) {
t.Helper()
ctx := context.TODO()
for i, tc := range valueTests {
m := new(dns.Msg)

View File

@ -99,10 +99,11 @@ func TestTtlRewrite(t *testing.T) {
}
rules = append(rules, rule)
}
doTTLTests(rules, t)
doTTLTests(t, rules)
}
func doTTLTests(rules []Rule, t *testing.T) {
func doTTLTests(t *testing.T, rules []Rule) {
t.Helper()
tests := []struct {
from string
fromType uint16

View File

@ -21,6 +21,7 @@ func TempFile(dir, content string) (string, func(), error) {
// WritePEMFiles creates a tmp dir with ca.pem, cert.pem, and key.pem
func WritePEMFiles(t *testing.T) (string, error) {
t.Helper()
tempDir := t.TempDir()
data := `-----BEGIN CERTIFICATE-----

View File

@ -1,6 +1,8 @@
package test
import "testing"
import (
"testing"
)
func TestTempFile(t *testing.T) {
_, f, e := TempFile(".", "test")

View File

@ -218,6 +218,7 @@ func TestTransferIXFRFallback(t *testing.T) {
}
func validateAXFRResponse(t *testing.T, w *dnstest.MultiRecorder) {
t.Helper()
if len(w.Msgs) == 0 {
t.Fatal("Did not get back a zone response")
}

View File

@ -56,6 +56,7 @@ func TestLookupCache(t *testing.T) {
}
func testCase(t *testing.T, name, addr string, expectAnsLen int, expectTTL uint32) {
t.Helper()
m := new(dns.Msg)
m.SetQuestion(name, dns.TypeA)
resp, err := dns.Exchange(m, addr)
@ -74,6 +75,7 @@ func testCase(t *testing.T, name, addr string, expectAnsLen int, expectTTL uint3
}
func testCaseDNSSEC(t *testing.T, name, addr string, bufsize int) {
t.Helper()
m := new(dns.Msg)
m.SetQuestion(name, dns.TypeA)

View File

@ -9,6 +9,7 @@ import (
)
func setupProxyTargetCoreDNS(t *testing.T, fn func(string)) {
t.Helper()
tmpdir := t.TempDir()
content := `

View File

@ -117,6 +117,7 @@ func getBucketCount(mf *test.MetricFamily, bucketLabel string) (int, error) {
// extractRequestSizeBucketCounts extracts bucket counts from DNS request size metrics
func extractRequestSizeBucketCounts(t *testing.T, metrics []*test.MetricFamily, label string) (int, int, error) {
t.Helper()
var countBelow100, countAbove100 int
var err error

View File

@ -55,6 +55,7 @@ func TestLookupBalanceRewriteCacheDnssec(t *testing.T) {
}
func createKeyFile(t *testing.T) func() {
t.Helper()
os.WriteFile(base+".key",
[]byte(`example.org. IN DNSKEY 256 3 13 tDyI0uEIDO4SjhTJh1AVTFBLpKhY3He5BdAlKztewiZ7GecWj94DOodg ovpN73+oJs+UfZ+p9zOSN5usGAlHrw==`),
0644)

View File

@ -44,6 +44,7 @@ func TestReload(t *testing.T) {
}
func send(t *testing.T, server string) {
t.Helper()
m := new(dns.Msg)
m.SetQuestion("whoami.example.org.", dns.TypeSRV)

View File

@ -57,6 +57,7 @@ func TestRewrite(t *testing.T) {
}
func testMX(t *testing.T, server string) {
t.Helper()
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeMX)
@ -78,6 +79,7 @@ func testMX(t *testing.T, server string) {
}
func testEdns0(t *testing.T, server string) {
t.Helper()
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)

View File

@ -45,6 +45,7 @@ func TestProxyToChaosServer(t *testing.T) {
}
func chaosTest(t *testing.T, server string) {
t.Helper()
m := new(dns.Msg)
m.Question = make([]dns.Question, 1)
m.Question[0] = dns.Question{Qclass: dns.ClassCHAOS, Name: "version.bind.", Qtype: dns.TypeTXT}

View File

@ -140,6 +140,7 @@ func TestView(t *testing.T) {
}
func viewTest(t *testing.T, testName, addr, qname string, qtype uint16, expectRcode int, expectAnswers []dns.RR) {
t.Helper()
t.Run(testName, func(t *testing.T) {
m := new(dns.Msg)