From 1bd33355b0eec4d1169942f7d94e99421b88f5f0 Mon Sep 17 00:00:00 2001 From: Brandon Ewing Date: Fri, 6 Dec 2024 10:24:04 -0600 Subject: [PATCH] Test additional DNS recursion scenarios Test decrement of recursion counter Test chained CNAMEs in single answer Add suppor to bypass name check in FakeDNS handler function Signed-off-by: Brandon Ewing --- controllers/utils/dns_test.go | 41 ++++++++++++++++++++++++++++++----- controllers/utils/fakedns.go | 3 ++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/controllers/utils/dns_test.go b/controllers/utils/dns_test.go index f9be1dd49c..0e0d8734db 100644 --- a/controllers/utils/dns_test.go +++ b/controllers/utils/dns_test.go @@ -114,14 +114,13 @@ func TestValidDigButMaxRecursion(t *testing.T) { Port: port, } NewFakeDNS(testSettings). + AddCNAMERecord("foo.cloud.example.com.", "bar.cloud.example.com."). AddARecord("bar.cloud.example.com.", net.IPv4(10, 1, 0, 3)). Start(). RunTestFunc(func() { - result, err := Dig("foo.cloud.example.com", 0, testServer) - if err != nil { - panic(err) - } - assert.Equal(t, result, []string{}) + result, err := Dig("foo.cloud.example.com", 1, testServer) + assert.Error(t, err) + assert.Nil(t, result) }) } @@ -215,6 +214,12 @@ func TestDigCNAME(t *testing.T) { Host: server, Port: port, } + testSettings := FakeDNSSettings{ + FakeDNSPort: port, + EdgeDNSZoneFQDN: "example.com.", + DNSZoneFQDN: "cloud.example.com.", + Dump: true, + } NewFakeDNS(testSettings). AddCNAMERecord("foo.cloud.example.com.", "bar.cloud.example.com."). AddARecord("bar.cloud.example.com.", net.IPv4(10, 1, 0, 3)). @@ -229,6 +234,32 @@ func TestDigCNAME(t *testing.T) { } +func TestDigCNAMERecursion(t *testing.T) { + testServer := DNSServer{ + Host: server, + Port: port, + } + testSettings := FakeDNSSettings{ + FakeDNSPort: port, + EdgeDNSZoneFQDN: "example.com.", + DNSZoneFQDN: "cloud.example.com.", + Dump: true, + } + NewFakeDNS(testSettings). + AddCNAMERecord("foo.cloud.example.com.", "bar.cloud.example.com."). + AddCNAMERecord("bar.cloud.example.com.", "baz.cloud.example.com."). + AddARecord("baz.cloud.example.com.", net.IPv4(10, 1, 0, 3)). + Start(). + RunTestFunc(func() { + result, err := Dig("foo.cloud.example.com", 8, testServer) + if err != nil { + panic(err) + } + assert.Equal(t, result, []string{"10.1.0.3"}) + }) + +} + func connected() (ok bool) { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) diff --git a/controllers/utils/fakedns.go b/controllers/utils/fakedns.go index 72145bffbf..52d3fd79de 100644 --- a/controllers/utils/fakedns.go +++ b/controllers/utils/fakedns.go @@ -36,6 +36,7 @@ type FakeDNSSettings struct { FakeDNSPort int EdgeDNSZoneFQDN string DNSZoneFQDN string + Dump bool } // DNSMock acts as DNS server but returns mock values @@ -199,7 +200,7 @@ func (m *DNSMock) handleReflect(w dns.ResponseWriter, r *dns.Msg) { if m.records[r.Question[0].Qtype] != nil { for _, rr := range m.records[r.Question[0].Qtype] { fqdn := strings.Split(rr.String(), "\t")[0] - if fqdn == r.Question[0].Name { + if fqdn == r.Question[0].Name || m.settings.Dump { msg.Answer = append(msg.Answer, rr) } }