Update ddns_test.go

This commit is contained in:
77-QiQi authored and GitHub committed 2026-09-14 10:29:08 +08:00
1 parent ed9f21e4ea
commit 09db8b4361
1 file changed
+23 -12
+23 -12
View File
@@ -3,6 +3,7 @@ package ddns
import ( import (
"context" "context"
"net" "net"
"strings"
"testing" "testing"
"github.com/libdns/libdns" "github.com/libdns/libdns"
@@ -24,18 +25,16 @@ func TestSplitDomainSOA(t *testing.T) {
m.SetReply(r) m.SetReply(r)
if len(r.Question) > 0 { if len(r.Question) > 0 {
qname := r.Question[0].Name qname := r.Question[0].Name
if strings.HasSuffix(qname, "example.co.uk.") || strings.HasSuffix(qname, "example.com.") {
soa := &dns.SOA{ soa := &dns.SOA{
Hdr: dns.RR_Header{Name: qname, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 300}, Hdr: dns.RR_Header{Name: qname, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 300},
Ns: "ns.example.com.", Ns: "ns.example.com.",
Mbox: "admin.example.com.", Mbox: "admin.example.com.",
Serial: 1, Serial: 1,
Refresh: 3600,
Retry: 600,
Expire: 86400,
Minttl: 60,
} }
m.Answer = append(m.Answer, soa) m.Answer = append(m.Answer, soa)
} }
}
_ = w.WriteMsg(m) _ = w.WriteMsg(m)
}) })
@@ -149,9 +148,8 @@ func TestIndependentDualStackProcessing(t *testing.T) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
if len(r.Question) > 0 { if len(r.Question) > 0 {
qname := r.Question[0].Name
soa := &dns.SOA{ soa := &dns.SOA{
Hdr: dns.RR_Header{Name: qname, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 300}, Hdr: dns.RR_Header{Name: r.Question[0].Name, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 300},
} }
m.Answer = append(m.Answer, soa) m.Answer = append(m.Answer, soa)
} }
@@ -177,8 +175,21 @@ func TestIndependentDualStackProcessing(t *testing.T) {
t.Fatalf("expected SetRecords to have been called for dual stack updates") t.Fatalf("expected SetRecords to have been called for dual stack updates")
} }
if len(mockSetter.Records) != 2 { foundIPv4 := false
t.Fatalf("expected 2 records (IPv4 and IPv6) to be set, got %d", len(mockSetter.Records)) foundIPv6 := false
for _, rec := range mockSetter.Records {
if addrRec, ok := rec.(libdns.Address); ok {
if addrRec.IP.String() == "1.1.1.1" {
foundIPv4 = true
}
if addrRec.IP.String() == "2001:db8::1" {
foundIPv6 = true
}
}
}
if !foundIPv4 || !foundIPv6 {
t.Fatalf("expected both IPv4 and IPv6 records to be processed independently, got records: %v", mockSetter.Records)
} }
} }
@@ -204,11 +215,11 @@ func TestTransientSOAFailureAndRetry(t *testing.T) {
}() }()
defer server.Shutdown() defer server.Shutdown()
maxRetries := 3 var maxRetries uint64 = 3
provider := &Provider{ provider := &Provider{
DDNSProfile: &model.DDNSProfile{ DDNSProfile: &model.DDNSProfile{
MaxRetries: uint64(maxRetries), MaxRetries: maxRetries,
Domains: []string{"fail.invalid"}, Domains: []string{"fail"},
}, },
IPAddrs: &model.IP{IPv4Addr: "1.1.1.1"}, IPAddrs: &model.IP{IPv4Addr: "1.1.1.1"},
Setter: &MockSetter{}, Setter: &MockSetter{},
@@ -217,7 +228,7 @@ func TestTransientSOAFailureAndRetry(t *testing.T) {
ctx := context.WithValue(context.Background(), DNSServerKey{}, []string{pc.LocalAddr().String()}) ctx := context.WithValue(context.Background(), DNSServerKey{}, []string{pc.LocalAddr().String()})
provider.UpdateDomain(ctx) provider.UpdateDomain(ctx)
if attempts != maxRetries { if uint64(attempts) != maxRetries {
t.Fatalf("expected exact attempt count of %d, got %d", maxRetries, attempts) t.Fatalf("expected exact attempt count of %d, got %d", maxRetries, attempts)
} }
} }