mirror of
https://github.com/coredns/coredns.git
synced 2026-05-25 19:30:23 -04:00
feat(secondary): add fallthrough support (#8041)
This commit is contained in:
@@ -10,6 +10,143 @@ import (
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
func TestSecondaryFallthrough(t *testing.T) {
|
||||
// Create zone file for primary - has www.example.org A 127.0.0.1
|
||||
primaryZone, rm1, err := test.TempFile(".", `$ORIGIN example.org.
|
||||
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. (
|
||||
2017042745 ; serial
|
||||
7200 ; refresh (2 hours)
|
||||
3600 ; retry (1 hour)
|
||||
1209600 ; expire (2 weeks)
|
||||
3600 ; minimum (1 hour)
|
||||
)
|
||||
|
||||
3600 IN NS a.iana-servers.net.
|
||||
3600 IN NS b.iana-servers.net.
|
||||
|
||||
www IN A 127.0.0.1
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create primary zone: %s", err)
|
||||
}
|
||||
defer rm1()
|
||||
|
||||
// Create zone file for fallback server - has other.example.org A 10.10.10.10
|
||||
fallbackZone, rm2, err := test.TempFile(".", `$ORIGIN example.org.
|
||||
@ 3600 IN SOA sns.dns.icann.org. noc.dns.icann.org. (
|
||||
2017042745 ; serial
|
||||
7200 ; refresh (2 hours)
|
||||
3600 ; retry (1 hour)
|
||||
1209600 ; expire (2 weeks)
|
||||
3600 ; minimum (1 hour)
|
||||
)
|
||||
|
||||
3600 IN NS a.iana-servers.net.
|
||||
3600 IN NS b.iana-servers.net.
|
||||
|
||||
other IN A 10.10.10.10
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create fallback zone: %s", err)
|
||||
}
|
||||
defer rm2()
|
||||
|
||||
// Start primary server (serves zone via AXFR)
|
||||
primaryCorefile := `example.org:0 {
|
||||
file ` + primaryZone + `
|
||||
transfer {
|
||||
to *
|
||||
}
|
||||
}`
|
||||
primary, _, primaryTCP, err := CoreDNSServerAndPorts(primaryCorefile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not get primary CoreDNS instance: %s", err)
|
||||
}
|
||||
defer primary.Stop()
|
||||
|
||||
// Start fallback server (answers queries forwarded by secondary)
|
||||
fallbackCorefile := `example.org:0 {
|
||||
file ` + fallbackZone + `
|
||||
}`
|
||||
fallback, fallbackUDP, _, err := CoreDNSServerAndPorts(fallbackCorefile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not get fallback CoreDNS instance: %s", err)
|
||||
}
|
||||
defer fallback.Stop()
|
||||
|
||||
// Start secondary with fallthrough + forward to fallback
|
||||
secondaryCorefile := `example.org:0 {
|
||||
secondary {
|
||||
transfer from ` + primaryTCP + `
|
||||
fallthrough
|
||||
}
|
||||
forward . ` + fallbackUDP + `
|
||||
}`
|
||||
sec, secUDP, _, err := CoreDNSServerAndPorts(secondaryCorefile)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not get secondary CoreDNS instance: %s", err)
|
||||
}
|
||||
defer sec.Stop()
|
||||
|
||||
// Wait for zone transfer to complete
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion("example.org.", dns.TypeSOA)
|
||||
var r *dns.Msg
|
||||
for range 10 {
|
||||
r, _ = dns.Exchange(m, secUDP)
|
||||
if r != nil && len(r.Answer) != 0 {
|
||||
break
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
if r == nil || len(r.Answer) == 0 {
|
||||
t.Fatal("Zone transfer did not complete")
|
||||
}
|
||||
|
||||
// Test 1: www.example.org exists in secondary zone - should return answer from zone
|
||||
m = new(dns.Msg)
|
||||
m.SetQuestion("www.example.org.", dns.TypeA)
|
||||
r, err = dns.Exchange(m, secUDP)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected to receive reply for www.example.org, but got error: %s", err)
|
||||
}
|
||||
if r.Rcode != dns.RcodeSuccess {
|
||||
t.Fatalf("Expected NOERROR for www.example.org, got %s", dns.RcodeToString[r.Rcode])
|
||||
}
|
||||
if len(r.Answer) != 1 {
|
||||
t.Fatalf("Expected 1 answer for www.example.org, got %d", len(r.Answer))
|
||||
}
|
||||
a, ok := r.Answer[0].(*dns.A)
|
||||
if !ok {
|
||||
t.Fatalf("Expected A record for www.example.org, got %T", r.Answer[0])
|
||||
}
|
||||
if a.A.String() != "127.0.0.1" {
|
||||
t.Fatalf("Expected www.example.org to be 127.0.0.1, got %s", a.A.String())
|
||||
}
|
||||
|
||||
// Test 2: other.example.org does NOT exist in secondary zone
|
||||
// With fallthrough, query should pass to forward plugin which queries fallback server
|
||||
m = new(dns.Msg)
|
||||
m.SetQuestion("other.example.org.", dns.TypeA)
|
||||
r, err = dns.Exchange(m, secUDP)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected to receive reply for other.example.org, but got error: %s", err)
|
||||
}
|
||||
if r.Rcode != dns.RcodeSuccess {
|
||||
t.Fatalf("Expected NOERROR for fallthrough query other.example.org, got %s", dns.RcodeToString[r.Rcode])
|
||||
}
|
||||
if len(r.Answer) != 1 {
|
||||
t.Fatalf("Expected 1 answer from fallback for other.example.org, got %d", len(r.Answer))
|
||||
}
|
||||
a, ok = r.Answer[0].(*dns.A)
|
||||
if !ok {
|
||||
t.Fatalf("Expected A record from fallback for other.example.org, got %T", r.Answer[0])
|
||||
}
|
||||
if a.A.String() != "10.10.10.10" {
|
||||
t.Fatalf("Expected fallback answer 10.10.10.10, got %s", a.A.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptySecondaryZone(t *testing.T) {
|
||||
// Corefile that fails to transfer example.org.
|
||||
corefile := `example.org:0 {
|
||||
|
||||
Reference in New Issue
Block a user