Skip to content

Commit 38725f4

Browse files
committed
Cleanup dns tests
Signed-off-by: Nino Kodabande <[email protected]>
1 parent 020d7ee commit 38725f4

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

pkg/hostagent/dns/dns_test.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,7 @@ var (
1919
dnsResult *dns.Msg
2020
)
2121

22-
func TestTXTRecords(t *testing.T) {
23-
testDomains := make([]string, 3)
24-
testDomains[0] = "onerecord.com"
25-
testDomains[1] = "multistringrecord.com"
26-
testDomains[2] = "multiplerecords.com"
27-
28-
expectedResults := make([]string, 3)
29-
expectedResults[0] = `onerecord.com.\s+5\s+IN\s+TXT\s+"My txt record"`
30-
expectedResults[1] = `multistringrecord.com.\s+5\s+IN\s+TXT\s+"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" "67890123456789012345678901234567890"`
31-
expectedResults[2] = `multiplerecords.com.\s+5\s+IN\s+TXT\s+"record 1"\nmultiplerecords.com.\s*5\s*IN\s*TXT\s*"record 2"`
22+
func TestDNSRecords(t *testing.T) {
3223

3324
srv, _ := mockdns.NewServerWithLogger(map[string]mockdns.Zone{
3425
"onerecord.com.": {
@@ -52,33 +43,42 @@ func TestTXTRecords(t *testing.T) {
5243
}
5344
srv.PatchNet(net.DefaultResolver)
5445
defer mockdns.UnpatchNet(net.DefaultResolver)
46+
w := new(TestResponseWriter)
47+
options := HandlerOptions{
48+
IPv6: true,
49+
StaticHosts: map[string]string{
50+
"MY.Host": "host.lima.internal",
51+
},
52+
}
5553

54+
h, err := NewHandler(options)
55+
assert.NilError(t, err)
56+
57+
regexMatch := func(value string, pattern string) cmp.Comparison {
58+
return func() cmp.Result {
59+
re := regexp.MustCompile(pattern)
60+
if re.MatchString(value) {
61+
return cmp.ResultSuccess
62+
}
63+
return cmp.ResultFailure(
64+
fmt.Sprintf("%q did not match pattern %q", value, pattern))
65+
}
66+
}
5667
t.Run("test TXT records", func(t *testing.T) {
57-
w := new(TestResponseWriter)
58-
options := HandlerOptions{
59-
IPv6: true,
60-
StaticHosts: map[string]string{
61-
"MY.Host": "host.lima.internal",
62-
},
68+
tests := []struct {
69+
testDomain string
70+
expectedTXTRecord string
71+
}{
72+
{testDomain: "onerecord.com", expectedTXTRecord: `onerecord.com.\s+5\s+IN\s+TXT\s+"My txt record"`},
73+
{testDomain: "multistringrecord.com", expectedTXTRecord: `multistringrecord.com.\s+5\s+IN\s+TXT\s+"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" "67890123456789012345678901234567890"`},
74+
{testDomain: "multiplerecords.com", expectedTXTRecord: `multiplerecords.com.\s+5\s+IN\s+TXT\s+"record 1"\nmultiplerecords.com.\s*5\s*IN\s*TXT\s*"record 2"`},
6375
}
64-
h, err := NewHandler(options)
65-
if err == nil {
66-
for i := 0; i < len(testDomains); i++ {
67-
req := new(dns.Msg)
68-
req.SetQuestion(dns.Fqdn(testDomains[i]), dns.TypeTXT)
69-
h.ServeDNS(w, req)
70-
regexMatch := func(value string, pattern string) cmp.Comparison {
71-
return func() cmp.Result {
72-
re := regexp.MustCompile(pattern)
73-
if re.MatchString(value) {
74-
return cmp.ResultSuccess
75-
}
76-
return cmp.ResultFailure(
77-
fmt.Sprintf("%q did not match pattern %q", value, pattern))
78-
}
79-
}
80-
assert.Assert(t, regexMatch(dnsResult.String(), expectedResults[i]))
81-
}
76+
77+
for _, tc := range tests {
78+
req := new(dns.Msg)
79+
req.SetQuestion(dns.Fqdn(tc.testDomain), dns.TypeTXT)
80+
h.ServeDNS(w, req)
81+
assert.Assert(t, regexMatch(dnsResult.String(), tc.expectedTXTRecord))
8282
}
8383
})
8484
}

0 commit comments

Comments
 (0)