refactor(gandi): improve tests to be more readable, robusts, and extensible

This commit is contained in:
Gaëtan Faugère 2023-08-24 14:02:51 +02:00
parent c5b488df4e
commit 81e17fd122
No known key found for this signature in database
GPG Key ID: 78C917DD1AFC5A78

View File

@ -17,16 +17,15 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"reflect"
"testing" "testing"
"github.com/go-gandi/go-gandi/domain" "github.com/go-gandi/go-gandi/domain"
"github.com/go-gandi/go-gandi/livedns" "github.com/go-gandi/go-gandi/livedns"
"github.com/maxatome/go-testdeep/td" "github.com/maxatome/go-testdeep/td"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"sigs.k8s.io/external-dns/endpoint" "sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/internal/testutils"
"sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/plan"
) )
@ -38,61 +37,16 @@ type MockAction struct {
type mockGandiClient struct { type mockGandiClient struct {
Actions []MockAction Actions []MockAction
FunctionToFail string FunctionToFail string `default:""`
RecordsToReturn []livedns.DomainRecord RecordsToReturn []livedns.DomainRecord
} }
func mockGandiClientNew() *mockGandiClient {
return &mockGandiClient{
RecordsToReturn: testRecords(),
}
}
func mockGandiClientNewWithRecords(recordsToReturn []livedns.DomainRecord) *mockGandiClient {
return &mockGandiClient{
RecordsToReturn: recordsToReturn,
}
}
func mockGandiClientNewWithFailure(functionToFail string) *mockGandiClient {
return &mockGandiClient{
FunctionToFail: functionToFail,
RecordsToReturn: testRecords(),
}
}
const ( const (
domainUriPrefix = "https://api.gandi.net/v5/domain/domains/" domainUriPrefix = "https://api.gandi.net/v5/domain/domains/"
exampleDotComUri = domainUriPrefix + "example.com" exampleDotComUri = domainUriPrefix + "example.com"
exampleDotNetUri = domainUriPrefix + "example.net" exampleDotNetUri = domainUriPrefix + "example.net"
) )
func testRecords() []livedns.DomainRecord {
return []livedns.DomainRecord{
{
RrsetType: endpoint.RecordTypeCNAME,
RrsetTTL: 600,
RrsetName: "@",
RrsetHref: exampleDotComUri + "/records/%40/A",
RrsetValues: []string{"192.168.0.1"},
},
{
RrsetType: endpoint.RecordTypeCNAME,
RrsetTTL: 600,
RrsetName: "www",
RrsetHref: exampleDotComUri + "/records/www/CNAME",
RrsetValues: []string{"lb.example.com"},
},
{
RrsetType: endpoint.RecordTypeA,
RrsetTTL: 600,
RrsetName: "test",
RrsetHref: exampleDotComUri + "/records/test/A",
RrsetValues: []string{"192.168.0.2"},
},
}
}
// Mock all methods // Mock all methods
func (m *mockGandiClient) GetDomainRecords(fqdn string) (records []livedns.DomainRecord, err error) { func (m *mockGandiClient) GetDomainRecords(fqdn string) (records []livedns.DomainRecord, err error) {
@ -100,11 +54,12 @@ func (m *mockGandiClient) GetDomainRecords(fqdn string) (records []livedns.Domai
Name: "GetDomainRecords", Name: "GetDomainRecords",
FQDN: fqdn, FQDN: fqdn,
}) })
if m.FunctionToFail == "GetDomainRecords" { if m.FunctionToFail == "GetDomainRecords" {
return nil, fmt.Errorf("injected error") return nil, fmt.Errorf("injected error")
} }
return m.RecordsToReturn, err return m.RecordsToReturn, nil
} }
func (m *mockGandiClient) CreateDomainRecord(fqdn, name, recordtype string, ttl int, values []string) (response standardResponse, err error) { func (m *mockGandiClient) CreateDomainRecord(fqdn, name, recordtype string, ttl int, values []string) (response standardResponse, err error) {
@ -118,6 +73,7 @@ func (m *mockGandiClient) CreateDomainRecord(fqdn, name, recordtype string, ttl
RrsetValues: values, RrsetValues: values,
}, },
}) })
if m.FunctionToFail == "CreateDomainRecord" { if m.FunctionToFail == "CreateDomainRecord" {
return standardResponse{}, fmt.Errorf("injected error") return standardResponse{}, fmt.Errorf("injected error")
} }
@ -171,6 +127,7 @@ func (m *mockGandiClient) ListDomains() (domains []domain.ListResponse, err erro
} }
return []domain.ListResponse{ return []domain.ListResponse{
// Tests are using example.com
{ {
FQDN: "example.com", FQDN: "example.com",
FQDNUnicode: "example.com", FQDNUnicode: "example.com",
@ -181,6 +138,7 @@ func (m *mockGandiClient) ListDomains() (domains []domain.ListResponse, err erro
}, },
TLD: "com", TLD: "com",
}, },
// example.net returns "other" as NameServer, so it is ignored
{ {
FQDN: "example.net", FQDN: "example.net",
FQDNUnicode: "example.net", FQDNUnicode: "example.net",
@ -218,44 +176,9 @@ func TestNewGandiProvider(t *testing.T) {
} }
} }
func TestGandiProvider_TestData(t *testing.T) { func TestGandiProvider_RecordsReturnsCorrectEndpoints(t *testing.T) {
mockedClient := mockGandiClientNew() mockedClient := &mockGandiClient{
RecordsToReturn: []livedns.DomainRecord{
// Check test zone data is ok
expectedZonesAnswer := []domain.ListResponse{
{
FQDN: "example.com",
FQDNUnicode: "example.com",
Href: exampleDotComUri,
ID: "b3e9c271-1c29-4441-97d9-bc021a7ac7c3",
NameServer: &domain.NameServerConfig{
Current: gandiLiveDNSProvider,
},
TLD: "com",
},
{
FQDN: "example.net",
FQDNUnicode: "example.net",
Href: exampleDotNetUri,
ID: "dc78c1d8-6143-4edb-93bc-3a20d8bc3570",
NameServer: &domain.NameServerConfig{
Current: "other",
},
TLD: "net",
},
}
testingZonesAnswer, err := mockedClient.ListDomains()
if err != nil {
t.Errorf("should not fail, %s", err)
}
if !reflect.DeepEqual(expectedZonesAnswer, testingZonesAnswer) {
t.Errorf("should be equal, %s", err)
}
// Check test record data is ok
expectedRecordsAnswer := []livedns.DomainRecord{
{ {
RrsetType: endpoint.RecordTypeCNAME, RrsetType: endpoint.RecordTypeCNAME,
RrsetTTL: 600, RrsetTTL: 600,
@ -277,49 +200,62 @@ func TestGandiProvider_TestData(t *testing.T) {
RrsetHref: exampleDotComUri + "/records/test/A", RrsetHref: exampleDotComUri + "/records/test/A",
RrsetValues: []string{"192.168.0.2"}, RrsetValues: []string{"192.168.0.2"},
}, },
},
} }
testingRecordsAnswer, err := mockedClient.GetDomainRecords("example.com")
if err != nil {
t.Errorf("should not fail, %s", err)
}
if !reflect.DeepEqual(expectedRecordsAnswer, testingRecordsAnswer) {
t.Errorf("should be equal, %s", err)
}
}
func TestGandiProvider_Records(t *testing.T) {
mockedClient := mockGandiClientNew()
mockedProvider := &GandiProvider{ mockedProvider := &GandiProvider{
DomainClient: mockedClient, DomainClient: mockedClient,
LiveDNSClient: mockedClient, LiveDNSClient: mockedClient,
} }
expectedActions := []MockAction{ actualEndpoints, err := mockedProvider.Records(context.Background())
{
Name: "ListDomains",
},
{
Name: "GetDomainRecords",
FQDN: "example.com",
},
}
endpoints, err := mockedProvider.Records(context.Background())
if err != nil { if err != nil {
t.Errorf("should not fail, %s", err) t.Errorf("should not fail, %s", err)
} }
assert.Equal(t, 3, len(endpoints))
fmt.Printf("%+v\n", endpoints[0].DNSName) expectedEndpoints := []*endpoint.Endpoint{
assert.Equal(t, "example.com", endpoints[0].DNSName) {
assert.Equal(t, endpoint.RecordTypeCNAME, endpoints[0].RecordType) RecordType: endpoint.RecordTypeCNAME,
td.Cmp(t, expectedActions, mockedClient.Actions) DNSName: "example.com",
Targets: endpoint.Targets{"192.168.0.1"},
RecordTTL: 600,
},
{
RecordType: endpoint.RecordTypeCNAME,
DNSName: "www.example.com",
Targets: endpoint.Targets{"lb.example.com"},
RecordTTL: 600,
},
{
RecordType: endpoint.RecordTypeA,
DNSName: "test.example.com",
Targets: endpoint.Targets{"192.168.0.2"},
RecordTTL: 600,
},
} }
func TestGandiProvider_RecordsAppliesDomainFilter(t *testing.T) { assert.Equal(t, len(expectedEndpoints), len(actualEndpoints))
mockedClient := mockGandiClientNew() // we could use testutils.SameEndpoints (plural), but this makes it easier to identify which case is failing
for i := range actualEndpoints {
if !testutils.SameEndpoint(expectedEndpoints[i], actualEndpoints[i]) {
t.Errorf("should be equal, expected:%v <> actual:%v", expectedEndpoints[i], actualEndpoints[i])
}
}
}
func TestGandiProvider_RecordsOnFilteredDomainsShouldYieldNoEndpoints(t *testing.T) {
mockedClient := &mockGandiClient{
RecordsToReturn: []livedns.DomainRecord{
{
RrsetType: endpoint.RecordTypeCNAME,
RrsetTTL: 600,
RrsetName: "@",
RrsetHref: exampleDotComUri + "/records/test/MX",
RrsetValues: []string{"192.168.0.1"},
},
},
}
mockedProvider := &GandiProvider{ mockedProvider := &GandiProvider{
DomainClient: mockedClient, DomainClient: mockedClient,
@ -327,70 +263,70 @@ func TestGandiProvider_RecordsAppliesDomainFilter(t *testing.T) {
domainFilter: endpoint.NewDomainFilterWithExclusions([]string{}, []string{"example.com"}), domainFilter: endpoint.NewDomainFilterWithExclusions([]string{}, []string{"example.com"}),
} }
expectedActions := []MockAction{ endpoints, _ := mockedProvider.Records(context.Background())
assert.Empty(t, endpoints)
}
func TestGandiProvider_RecordsWithUnsupportedTypesAreNotReturned(t *testing.T) {
mockedClient := &mockGandiClient{
RecordsToReturn: []livedns.DomainRecord{
{ {
Name: "ListDomains", RrsetType: "MX",
RrsetTTL: 360,
RrsetName: "@",
RrsetHref: exampleDotComUri + "/records/%40/A",
RrsetValues: []string{"smtp.example.com"},
},
}, },
} }
endpoints, err := mockedProvider.Records(context.Background()) mockedProvider := &GandiProvider{
if err != nil { DomainClient: mockedClient,
t.Errorf("should not fail, %s", err) LiveDNSClient: mockedClient,
}
assert.Equal(t, 0, len(endpoints))
td.Cmp(t, expectedActions, mockedClient.Actions)
} }
func TestGandiProvider_RecordsWithMultipleValues(t *testing.T) { endpoints, _ := mockedProvider.Records(context.Background())
mockedClient := mockGandiClientNewWithRecords([]livedns.DomainRecord{ assert.Empty(t, endpoints)
}
func TestGandiProvider_ApplyChangesMakesExpectedAPICalls(t *testing.T) {
changes := &plan.Changes{}
mockedClient := &mockGandiClient{}
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
changes.Create = []*endpoint.Endpoint{
{ {
RrsetValues: []string{"foo", "bar"}, DNSName: "test2.example.com",
RrsetType: endpoint.RecordTypeCNAME, Targets: endpoint.Targets{"192.168.0.1"},
RecordType: "A",
RecordTTL: 666,
}, },
})
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
} }
changes.UpdateNew = []*endpoint.Endpoint{
endpoints, err := mockedProvider.Records(context.Background()) {
if err != nil { DNSName: "test3.example.com",
t.Errorf("should not fail, %s", err) Targets: endpoint.Targets{"192.168.0.2"},
RecordType: "A",
RecordTTL: 777,
},
{
DNSName: "example.com.example.com",
Targets: endpoint.Targets{"lb-2.example.net"},
RecordType: "CNAME",
RecordTTL: 777,
},
} }
assert.Equal(t, 2, len(endpoints)) changes.Delete = []*endpoint.Endpoint{
{
DNSName: "test4.example.com",
Targets: endpoint.Targets{"192.168.0.3"},
RecordType: "A",
},
} }
func TestGandiProvider_ApplyChangesEmpty(t *testing.T) {
changes := &plan.Changes{}
mockedClient := mockGandiClientNew()
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
err := mockedProvider.ApplyChanges(context.Background(), changes)
if err != nil {
t.Errorf("should not fail, %s", err)
}
if mockedClient.Actions != nil {
t.Error("expected no changes")
}
}
func TestGandiProvider_ApplyChanges(t *testing.T) {
changes := &plan.Changes{}
mockedClient := mockGandiClientNew()
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
changes.Create = []*endpoint.Endpoint{{DNSName: "test2.example.com", Targets: endpoint.Targets{"target"}, RecordType: "A", RecordTTL: 666}}
changes.UpdateNew = []*endpoint.Endpoint{{DNSName: "test3.example.com", Targets: endpoint.Targets{"target-new"}, RecordType: "A", RecordTTL: 777}}
changes.Delete = []*endpoint.Endpoint{{DNSName: "test4.example.com", Targets: endpoint.Targets{"target-other"}, RecordType: "A"}}
err := mockedProvider.ApplyChanges(context.Background(), changes) err := mockedProvider.ApplyChanges(context.Background(), changes)
if err != nil { if err != nil {
t.Errorf("should not fail, %s", err) t.Errorf("should not fail, %s", err)
@ -406,7 +342,7 @@ func TestGandiProvider_ApplyChanges(t *testing.T) {
Record: livedns.DomainRecord{ Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeA, RrsetType: endpoint.RecordTypeA,
RrsetName: "test2", RrsetName: "test2",
RrsetValues: []string{"target"}, RrsetValues: []string{"192.168.0.1"},
RrsetTTL: 666, RrsetTTL: 666,
}, },
}, },
@ -416,7 +352,17 @@ func TestGandiProvider_ApplyChanges(t *testing.T) {
Record: livedns.DomainRecord{ Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeA, RrsetType: endpoint.RecordTypeA,
RrsetName: "test3", RrsetName: "test3",
RrsetValues: []string{"target-new"}, RrsetValues: []string{"192.168.0.2"},
RrsetTTL: 777,
},
},
{
Name: "UpdateDomainRecordByNameAndType",
FQDN: "example.com",
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeCNAME,
RrsetName: "example.com",
RrsetValues: []string{"lb-2.example.net."},
RrsetTTL: 777, RrsetTTL: 777,
}, },
}, },
@ -431,133 +377,20 @@ func TestGandiProvider_ApplyChanges(t *testing.T) {
}) })
} }
func TestGandiProvider_ApplyChangesSkipsNonManaged(t *testing.T) {
changes := &plan.Changes{}
mockedClient := mockGandiClientNew()
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
changes.Create = []*endpoint.Endpoint{{DNSName: "example.net", Targets: endpoint.Targets{"target"}}}
changes.UpdateNew = []*endpoint.Endpoint{{DNSName: "test.example.net", Targets: endpoint.Targets{"target-new"}, RecordType: "A", RecordTTL: 777}}
changes.Delete = []*endpoint.Endpoint{{DNSName: "test2.example.net", Targets: endpoint.Targets{"target"}, RecordType: "A"}}
err := mockedProvider.ApplyChanges(context.Background(), changes)
if err != nil {
t.Errorf("should not fail, %s", err)
}
td.Cmp(t, mockedClient.Actions, []MockAction{
{
Name: "ListDomains",
},
})
}
func TestGandiProvider_ApplyChangesCreateUpdateCname(t *testing.T) {
changes := &plan.Changes{}
mockedClient := mockGandiClientNew()
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
changes.Create = []*endpoint.Endpoint{
{DNSName: "test-cname.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "CNAME"},
}
changes.UpdateNew = []*endpoint.Endpoint{{DNSName: "test-cname2.example.com", Targets: endpoint.Targets{"target-new"}, RecordType: "CNAME", RecordTTL: 777}}
err := mockedProvider.ApplyChanges(context.Background(), changes)
if err != nil {
t.Errorf("should not fail, %s", err)
}
td.Cmp(t, mockedClient.Actions, []MockAction{
{
Name: "ListDomains",
},
{
Name: "CreateDomainRecord",
FQDN: "example.com",
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeCNAME,
RrsetName: "test-cname",
RrsetValues: []string{"target."},
RrsetTTL: 666,
},
},
{
Name: "UpdateDomainRecordByNameAndType",
FQDN: "example.com",
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeCNAME,
RrsetName: "test-cname2",
RrsetValues: []string{"target-new."},
RrsetTTL: 777,
},
},
})
}
func TestGandiProvider_ApplyChangesCreateEmpty(t *testing.T) {
changes := &plan.Changes{}
mockedClient := mockGandiClientNew()
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
changes.Create = []*endpoint.Endpoint{
{DNSName: "example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.UpdateNew = []*endpoint.Endpoint{}
err := mockedProvider.ApplyChanges(context.Background(), changes)
if err != nil {
t.Errorf("should not fail, %s", err)
}
td.Cmp(t, mockedClient.Actions, []MockAction{
{
Name: "ListDomains",
},
{
Name: "CreateDomainRecord",
FQDN: "example.com",
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeA,
RrsetName: "@",
RrsetValues: []string{"target"},
RrsetTTL: 666,
},
},
})
}
func TestGandiProvider_ApplyChangesRespectsDryRun(t *testing.T) { func TestGandiProvider_ApplyChangesRespectsDryRun(t *testing.T) {
changes := &plan.Changes{} changes := &plan.Changes{}
mockedClient := mockGandiClientNew() mockedClient := &mockGandiClient{}
mockedProvider := &GandiProvider{ mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
DryRun: true, DryRun: true,
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
} }
changes.Create = []*endpoint.Endpoint{ changes.Create = []*endpoint.Endpoint{{DNSName: "test2.example.com", Targets: endpoint.Targets{"192.168.0.1"}, RecordType: "A", RecordTTL: 666}}
{DNSName: "foo.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"}, changes.UpdateNew = []*endpoint.Endpoint{{DNSName: "test3.example.com", Targets: endpoint.Targets{"192.168.0.2"}, RecordType: "A", RecordTTL: 777}}
} changes.Delete = []*endpoint.Endpoint{{DNSName: "test4.example.com", Targets: endpoint.Targets{"192.168.0.3"}, RecordType: "A"}}
changes.UpdateNew = []*endpoint.Endpoint{
{DNSName: "bar.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.Delete = []*endpoint.Endpoint{
{DNSName: "baz.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
err := mockedProvider.ApplyChanges(context.Background(), changes) mockedProvider.ApplyChanges(context.Background(), changes)
if err != nil {
t.Errorf("should not fail, %s", err)
}
td.Cmp(t, mockedClient.Actions, []MockAction{ td.Cmp(t, mockedClient.Actions, []MockAction{
{ {
@ -566,28 +399,37 @@ func TestGandiProvider_ApplyChangesRespectsDryRun(t *testing.T) {
}) })
} }
func TestGandiProvider_ApplyChangesErrorListDomains(t *testing.T) { func TestGandiProvider_ApplyChangesWithEmptyResultDoesNothing(t *testing.T) {
changes := &plan.Changes{} changes := &plan.Changes{}
mockedClient := mockGandiClientNewWithFailure("ListDomains") mockedClient := &mockGandiClient{}
mockedProvider := &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
mockedProvider.ApplyChanges(context.Background(), changes)
assert.Empty(t, mockedClient.Actions)
}
func TestGandiProvider_ApplyChangesWithUnknownDomainDoesNoUpdate(t *testing.T) {
changes := &plan.Changes{}
mockedClient := &mockGandiClient{}
mockedProvider := &GandiProvider{ mockedProvider := &GandiProvider{
DomainClient: mockedClient, DomainClient: mockedClient,
LiveDNSClient: mockedClient, LiveDNSClient: mockedClient,
} }
changes.Create = []*endpoint.Endpoint{ changes.Create = []*endpoint.Endpoint{
{DNSName: "foo.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"}, {
} DNSName: "test.example.net",
changes.UpdateNew = []*endpoint.Endpoint{ Targets: endpoint.Targets{"192.168.0.1"},
{DNSName: "bar.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"}, RecordType: "A",
} RecordTTL: 666,
changes.Delete = []*endpoint.Endpoint{ },
{DNSName: "baz.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
} }
err := mockedProvider.ApplyChanges(context.Background(), changes) mockedProvider.ApplyChanges(context.Background(), changes)
if err == nil {
t.Error("should have failed")
}
td.Cmp(t, mockedClient.Actions, []MockAction{ td.Cmp(t, mockedClient.Actions, []MockAction{
{ {
@ -596,150 +438,93 @@ func TestGandiProvider_ApplyChangesErrorListDomains(t *testing.T) {
}) })
} }
func TestGandiProvider_ApplyChangesErrorCreate(t *testing.T) { func TestGandiProvider_FailingCases(t *testing.T) {
changes := &plan.Changes{} changes := &plan.Changes{}
mockedClient := mockGandiClientNewWithFailure("CreateDomainRecord") changes.Create = []*endpoint.Endpoint{{DNSName: "test2.example.com", Targets: endpoint.Targets{"192.168.0.1"}, RecordType: "A", RecordTTL: 666}}
changes.UpdateNew = []*endpoint.Endpoint{{DNSName: "test3.example.com", Targets: endpoint.Targets{"192.168.0.2"}, RecordType: "A", RecordTTL: 777}}
changes.Delete = []*endpoint.Endpoint{{DNSName: "test4.example.com", Targets: endpoint.Targets{"192.168.0.3"}, RecordType: "A"}}
// Failing ListDomains API call creates an error when calling Records
mockedClient := &mockGandiClient{
FunctionToFail: "ListDomains",
}
mockedProvider := &GandiProvider{ mockedProvider := &GandiProvider{
DomainClient: mockedClient, DomainClient: mockedClient,
LiveDNSClient: mockedClient, LiveDNSClient: mockedClient,
} }
changes.Create = []*endpoint.Endpoint{ _, err := mockedProvider.Records(context.Background())
{DNSName: "foo.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.UpdateNew = []*endpoint.Endpoint{
{DNSName: "bar.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.Delete = []*endpoint.Endpoint{
{DNSName: "baz.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
err := mockedProvider.ApplyChanges(context.Background(), changes)
if err == nil { if err == nil {
t.Error("should have failed") t.Error("should have failed")
} }
td.Cmp(t, mockedClient.Actions, []MockAction{ // Failing GetDomainRecords API call creates an error when calling Records
{ mockedClient = &mockGandiClient{
Name: "ListDomains", FunctionToFail: "GetDomainRecords",
},
{
Name: "CreateDomainRecord",
FQDN: "example.com",
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeA,
RrsetName: "foo",
RrsetValues: []string{"target"},
RrsetTTL: 666,
},
},
})
} }
mockedProvider = &GandiProvider{
func TestGandiProvider_ApplyChangesErrorUpdate(t *testing.T) {
changes := &plan.Changes{}
mockedClient := mockGandiClientNewWithFailure("UpdateDomainRecordByNameAndType")
mockedProvider := &GandiProvider{
DomainClient: mockedClient, DomainClient: mockedClient,
LiveDNSClient: mockedClient, LiveDNSClient: mockedClient,
} }
changes.Create = []*endpoint.Endpoint{ _, err = mockedProvider.Records(context.Background())
{DNSName: "foo.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.UpdateNew = []*endpoint.Endpoint{
{DNSName: "bar.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.Delete = []*endpoint.Endpoint{
{DNSName: "baz.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
err := mockedProvider.ApplyChanges(context.Background(), changes)
if err == nil { if err == nil {
t.Error("should have failed") t.Error("should have failed")
} }
td.Cmp(t, mockedClient.Actions, []MockAction{ // Failing ListDomains API call creates an error when calling ApplyChanges
{ mockedClient = &mockGandiClient{
Name: "ListDomains", FunctionToFail: "ListDomains",
},
{
Name: "CreateDomainRecord",
FQDN: "example.com",
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeA,
RrsetName: "foo",
RrsetValues: []string{"target"},
RrsetTTL: 666,
},
},
{
Name: "UpdateDomainRecordByNameAndType",
FQDN: "example.com",
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeA,
RrsetName: "bar",
RrsetValues: []string{"target"},
RrsetTTL: 666,
},
},
})
} }
mockedProvider = &GandiProvider{
func TestGandiProvider_ApplyChangesErrorDelete(t *testing.T) {
changes := &plan.Changes{}
mockedClient := mockGandiClientNewWithFailure("DeleteDomainRecord")
mockedProvider := &GandiProvider{
DomainClient: mockedClient, DomainClient: mockedClient,
LiveDNSClient: mockedClient, LiveDNSClient: mockedClient,
} }
changes.Create = []*endpoint.Endpoint{ err = mockedProvider.ApplyChanges(context.Background(), changes)
{DNSName: "foo.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.UpdateNew = []*endpoint.Endpoint{
{DNSName: "bar.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
changes.Delete = []*endpoint.Endpoint{
{DNSName: "baz.example.com", Targets: endpoint.Targets{"target"}, RecordTTL: 666, RecordType: "A"},
}
err := mockedProvider.ApplyChanges(context.Background(), changes)
if err == nil { if err == nil {
t.Error("should have failed") t.Error("should have failed")
} }
td.Cmp(t, mockedClient.Actions, []MockAction{ // Failing CreateDomainRecord API call creates an error when calling ApplyChanges
{ mockedClient = &mockGandiClient{
Name: "ListDomains", FunctionToFail: "CreateDomainRecord",
}, }
{ mockedProvider = &GandiProvider{
Name: "CreateDomainRecord", DomainClient: mockedClient,
FQDN: "example.com", LiveDNSClient: mockedClient,
Record: livedns.DomainRecord{ }
RrsetType: endpoint.RecordTypeA,
RrsetName: "foo", err = mockedProvider.ApplyChanges(context.Background(), changes)
RrsetValues: []string{"target"}, if err == nil {
RrsetTTL: 666, t.Error("should have failed")
}, }
},
{ // Failing DeleteDomainRecord API call creates an error when calling ApplyChanges
Name: "UpdateDomainRecordByNameAndType", mockedClient = &mockGandiClient{
FQDN: "example.com", FunctionToFail: "DeleteDomainRecord",
Record: livedns.DomainRecord{ }
RrsetType: endpoint.RecordTypeA, mockedProvider = &GandiProvider{
RrsetName: "bar", DomainClient: mockedClient,
RrsetValues: []string{"target"}, LiveDNSClient: mockedClient,
RrsetTTL: 666, }
},
}, err = mockedProvider.ApplyChanges(context.Background(), changes)
{ if err == nil {
Name: "DeleteDomainRecord", t.Error("should have failed")
FQDN: "example.com", }
Record: livedns.DomainRecord{
RrsetType: endpoint.RecordTypeA, // Failing UpdateDomainRecordByNameAndType API call creates an error when calling ApplyChanges
RrsetName: "baz", mockedClient = &mockGandiClient{
}, FunctionToFail: "UpdateDomainRecordByNameAndType",
}, }
}) mockedProvider = &GandiProvider{
DomainClient: mockedClient,
LiveDNSClient: mockedClient,
}
err = mockedProvider.ApplyChanges(context.Background(), changes)
if err == nil {
t.Error("should have failed")
}
} }