19
19
dnsResult * dns.Msg
20
20
)
21
21
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 ) {
32
23
33
24
srv , _ := mockdns .NewServerWithLogger (map [string ]mockdns.Zone {
34
25
"onerecord.com." : {
@@ -52,33 +43,79 @@ func TestTXTRecords(t *testing.T) {
52
43
}
53
44
srv .PatchNet (net .DefaultResolver )
54
45
defer mockdns .UnpatchNet (net .DefaultResolver )
46
+ w := new (TestResponseWriter )
47
+ options := HandlerOptions {
48
+ IPv6 : true ,
49
+ StaticHosts : map [string ]string {
50
+ "MY.DOMAIN.COM" : "192.168.0.23" ,
51
+ "host.lima.internal" : "10.10.0.34" ,
52
+ "my.host" : "host.lima.internal" ,
53
+ "default" : "my.domain.com" ,
54
+ },
55
+ }
55
56
57
+ h , err := NewHandler (options )
58
+ assert .NilError (t , err )
59
+
60
+ regexMatch := func (value string , pattern string ) cmp.Comparison {
61
+ return func () cmp.Result {
62
+ re := regexp .MustCompile (pattern )
63
+ if re .MatchString (value ) {
64
+ return cmp .ResultSuccess
65
+ }
66
+ return cmp .ResultFailure (
67
+ fmt .Sprintf ("%q did not match pattern %q" , value , pattern ))
68
+ }
69
+ }
56
70
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
- },
71
+ tests := []struct {
72
+ testDomain string
73
+ expectedTXTRecord string
74
+ }{
75
+ {testDomain : "onerecord.com" , expectedTXTRecord : `onerecord.com.\s+5\s+IN\s+TXT\s+"My txt record"` },
76
+ {testDomain : "multistringrecord.com" , expectedTXTRecord : `multistringrecord.com.\s+5\s+IN\s+TXT\s+"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" "67890123456789012345678901234567890"` },
77
+ {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"` },
63
78
}
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
- }
79
+
80
+ for _ , tc := range tests {
81
+ req := new (dns.Msg )
82
+ req .SetQuestion (dns .Fqdn (tc .testDomain ), dns .TypeTXT )
83
+ h .ServeDNS (w , req )
84
+ assert .Assert (t , regexMatch (dnsResult .String (), tc .expectedTXTRecord ))
85
+ }
86
+ })
87
+
88
+ t .Run ("test A records" , func (t * testing.T ) {
89
+ tests := []struct {
90
+ testDomain string
91
+ expectedARecord string
92
+ }{
93
+ {testDomain : "my.domain.com" , expectedARecord : `my.domain.com.\s+5\s+IN\s+A\s+192.168.0.23` },
94
+ {testDomain : "host.lima.internal" , expectedARecord : `host.lima.internal.\s+5\s+IN\s+A\s+10.10.0.34` },
95
+ }
96
+
97
+ for _ , tc := range tests {
98
+ req := new (dns.Msg )
99
+ req .SetQuestion (dns .Fqdn (tc .testDomain ), dns .TypeA )
100
+ h .ServeDNS (w , req )
101
+ assert .Assert (t , regexMatch (dnsResult .String (), tc .expectedARecord ))
102
+ }
103
+ })
104
+
105
+ t .Run ("test CNAME records" , func (t * testing.T ) {
106
+ tests := []struct {
107
+ testDomain string
108
+ expectedCNAME string
109
+ }{
110
+ {testDomain : "my.host" , expectedCNAME : `my.host.\s+5\s+IN\s+CNAME\s+host.lima.internal.` },
111
+ {testDomain : "default" , expectedCNAME : `default.\s+5\s+IN\s+CNAME\s+my.domain.com.` },
112
+ }
113
+
114
+ for _ , tc := range tests {
115
+ req := new (dns.Msg )
116
+ req .SetQuestion (dns .Fqdn (tc .testDomain ), dns .TypeCNAME )
117
+ h .ServeDNS (w , req )
118
+ assert .Assert (t , regexMatch (dnsResult .String (), tc .expectedCNAME ))
82
119
}
83
120
})
84
121
}
0 commit comments