Update ddns_test.go

This commit is contained in:
77-QiQi authored and GitHub committed 2026-09-14 08:39:52 +08:00
1 parent cbc583dd3a
commit 892619c95c
1 file changed
+101 -34
+101 -34
View File
@@ -3,45 +3,112 @@ package ddns
import ( import (
"context" "context"
"testing" "testing"
"time"
"github.com/libdns/he"
"github.com/libdns/libdns"
"github.com/nezhahq/nezha/model"
) )
type testSt struct { type MockSetter struct {
domain string Records []libdns.Record
zone string SetCalled bool
prefix string GetCalled bool
DelCalled bool
DeleteError error
} }
func TestSplitDomainSOA(t *testing.T) { func (m *MockSetter) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
cases := []testSt{ m.SetCalled = true
{ m.Records = append(m.Records, records...)
domain: "www.example.co.uk", return records, nil
zone: "example.co.uk.", }
prefix: "www",
}, func (m *MockSetter) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
{ m.GetCalled = true
domain: "abc.example.com", return m.Records, nil
zone: "example.com.", }
prefix: "abc",
}, func (m *MockSetter) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
{ m.DelCalled = true
domain: "example.com", if m.DeleteError != nil {
zone: "example.com.", return nil, m.DeleteError
prefix: "", }
}, m.Records = nil
return records, nil
}
type MockUnsupportedSetter struct{}
func (m *MockUnsupportedSetter) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
return records, nil
}
func TestUnsupportedGetterDeleterSkip(t *testing.T) {
provider := &Provider{
DDNSProfile: &model.DDNSProfile{MaxRetries: 1},
IPAddrs: &model.IP{IPv4Addr: ""},
Setter: &MockUnsupportedSetter{},
} }
ctx := context.WithValue(context.Background(), DNSServerKey{}, []string{"1.1.1.1:53"}) err := provider.deleteDomainRecord(context.Background(), "sub", "example.com", "A")
provider := &Provider{} if err != nil {
for _, c := range cases { t.Fatalf("expected no error for unsupported setter deletion skip, got %v", err)
prefix, zone, err := provider.splitDomainSOA(ctx, c.domain) }
if err != nil { }
t.Fatalf("Error: %s", err)
} func TestIndependentDualStackProcessing(t *testing.T) {
if prefix != c.prefix { enableIPv4 := true
t.Fatalf("Expected prefix %s, but got %s", c.prefix, prefix) enableIPv6 := true
} provider := &Provider{
if zone != c.zone { DDNSProfile: &model.DDNSProfile{
t.Fatalf("Expected zone %s, but got %s", c.zone, zone) MaxRetries: 1,
} EnableIPv4: &enableIPv4,
EnableIPv6: &enableIPv6,
},
IPAddrs: &model.IP{
IPv4Addr: "1.1.1.1",
IPv6Addr: "",
},
Setter: &MockSetter{},
}
if provider.DDNSProfile.EnableIPv4 == nil || !*provider.DDNSProfile.EnableIPv4 {
t.Fatalf("IPv4 should be enabled")
}
}
func TestHEDirectDeletionPath(t *testing.T) {
heProvider := &he.Provider{APIKey: "test-key"}
provider := &Provider{
DDNSProfile: &model.DDNSProfile{MaxRetries: 1},
IPAddrs: &model.IP{IPv4Addr: ""},
Setter: heProvider,
}
err := provider.deleteDomainRecord(context.Background(), "test", "example.com", "A")
if err == nil {
t.Fatalf("expected an error due to invalid test API key in HE direct path, but got nil")
}
}
func TestTransientSOAFailureAndRetry(t *testing.T) {
maxRetries := 3
provider := &Provider{
DDNSProfile: &model.DDNSProfile{
MaxRetries: int32(maxRetries),
Domains: []string{"notfound.invalid"},
},
IPAddrs: &model.IP{IPv4Addr: "1.1.1.1"},
Setter: &MockSetter{},
}
ctx := context.WithValue(context.Background(), DNSServerKey{}, []string{"192.0.2.1:53"})
start := time.Now()
provider.UpdateDomain(ctx)
duration := time.Since(start)
if duration > 35*time.Second {
t.Fatalf("SOA retry loop took too long: %v", duration)
} }
} }