feat: add mode selection
This commit is contained in:
115
pkg/realDynDns/realDynDns_test.go
Normal file
115
pkg/realDynDns/realDynDns_test.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package realDynDns
|
||||
|
||||
import (
|
||||
"net"
|
||||
"realdnydns/model/common"
|
||||
"realdnydns/pkg/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type MockExternalIpProvider struct {
|
||||
ExternalIp string
|
||||
}
|
||||
|
||||
func (m *MockExternalIpProvider) GetExternalIp() (net.IP, error) {
|
||||
return net.ParseIP(m.ExternalIp), nil
|
||||
}
|
||||
|
||||
type MockDNSProvider interface {
|
||||
UpdateRecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
|
||||
GetRecord(tld string, subdomain string) (*common.ARecord, error)
|
||||
}
|
||||
|
||||
type MockDNSProviderImpl struct {
|
||||
GetRecordIpResponse string
|
||||
UpdateRecordIpResponse string
|
||||
}
|
||||
|
||||
func (m *MockDNSProviderImpl) GetRecord(tld string, subdomain string) (*common.ARecord, error) {
|
||||
return &common.ARecord{
|
||||
IP: m.GetRecordIpResponse,
|
||||
TTL: 10,
|
||||
Prio: 20,
|
||||
Disabled: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *MockDNSProviderImpl) UpdateRecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error) {
|
||||
return &common.ARecord{
|
||||
IP: m.UpdateRecordIpResponse,
|
||||
TTL: 10,
|
||||
Prio: 20,
|
||||
Disabled: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type MockedNotificationProvider struct{}
|
||||
|
||||
type MockedNotificationProviderImpl struct{}
|
||||
|
||||
func (m *MockedNotificationProviderImpl) SendNotification(title string, message string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestDetectAndApplyChanges(t *testing.T) {
|
||||
t.Run("with changes", testDetectAndApplyChangesWithChanges())
|
||||
t.Run("without changes", testDetectAndApplyChangesWithoutChanges())
|
||||
}
|
||||
|
||||
func testDetectAndApplyChangesWithChanges() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
changeDetector := New(&MockExternalIpProvider{
|
||||
ExternalIp: "127.0.0.1",
|
||||
}, &MockDNSProviderImpl{
|
||||
GetRecordIpResponse: "127.0.0.2",
|
||||
UpdateRecordIpResponse: "127.0.0.1",
|
||||
}, &MockedNotificationProviderImpl{},
|
||||
[]config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
numberUpdated, err := changeDetector.RunOnce()
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if numberUpdated != 2 {
|
||||
t.Errorf("expected numberUpdated to be 2, got %v", numberUpdated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testDetectAndApplyChangesWithoutChanges() func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
changeDetector := New(&MockExternalIpProvider{
|
||||
ExternalIp: "127.0.0.1",
|
||||
}, &MockDNSProviderImpl{
|
||||
GetRecordIpResponse: "127.0.0.1",
|
||||
UpdateRecordIpResponse: "127.0.0.1",
|
||||
}, &MockedNotificationProviderImpl{},
|
||||
[]config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
numberUpdated, err := changeDetector.RunOnce()
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if numberUpdated != 0 {
|
||||
t.Errorf("expected numberUpdated to be 0, got %v", numberUpdated)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user