Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 27 additions & 56 deletions provider/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1826,20 +1826,24 @@ func TestCloudFlareProvider_Region(t *testing.T) {
}

func TestCloudFlareProvider_newCloudFlareChange(t *testing.T) {
_ = os.Setenv("CF_API_KEY", "xxxxxxxxxxxxxxxxx")
_ = os.Setenv("CF_API_EMAIL", "test@test.com")
t.Parallel()

p, err := NewCloudFlareProvider(
endpoint.NewDomainFilter([]string{"example.com"}),
provider.ZoneIDFilter{},
true,
false,
RegionalServicesConfig{Enabled: true, RegionKey: "us"},
CustomHostnamesConfig{Enabled: false},
DNSRecordsConfig{PerPage: 50},
)
if err != nil {
t.Fatal(err)
comment := string(make([]byte, paidZoneMaxCommentLength+1))
freeValidComment := comment[:freeZoneMaxCommentLength]
freeInvalidComment := comment[:freeZoneMaxCommentLength+1]
paidValidComment := comment[:paidZoneMaxCommentLength]
paidInvalidComment := comment[:paidZoneMaxCommentLength+1]

freeProvider := &CloudFlareProvider{
Client: NewMockCloudFlareClient(),
domainFilter: endpoint.NewDomainFilter([]string{"example.com"}),
RegionalServicesConfig: RegionalServicesConfig{Enabled: true, RegionKey: "us"},
}
paidProvider := &CloudFlareProvider{
Client: NewMockCloudFlareClient(),
domainFilter: endpoint.NewDomainFilter([]string{"bar.com"}),
RegionalServicesConfig: RegionalServicesConfig{Enabled: true, RegionKey: "us"},
DNSRecordsConfig: DNSRecordsConfig{Comment: paidValidComment},
}

ep := &endpoint.Endpoint{
Expand All @@ -1848,44 +1852,11 @@ func TestCloudFlareProvider_newCloudFlareChange(t *testing.T) {
Targets: []string{"192.0.2.1"},
}

change, _ := p.newCloudFlareChange(cloudFlareCreate, ep, ep.Targets[0], nil)
change, _ := freeProvider.newCloudFlareChange(cloudFlareCreate, ep, ep.Targets[0], nil)
if change.RegionalHostname.regionKey != "us" {
t.Errorf("expected region key to be 'us', but got '%s'", change.RegionalHostname.regionKey)
}

var freeValidCommentBuilder strings.Builder
for range freeZoneMaxCommentLength {
freeValidCommentBuilder.WriteString("x")
}

var freeInvalidCommentBuilder strings.Builder
for range freeZoneMaxCommentLength + 1 {
freeInvalidCommentBuilder.WriteString("x")
}

var paidValidCommentBuilder strings.Builder
for range paidZoneMaxCommentLength {
paidValidCommentBuilder.WriteString("x")
}
var paidInvalidCommentBuilder strings.Builder
for range paidZoneMaxCommentLength + 1 {
paidInvalidCommentBuilder.WriteString("x")
}

paidProvider, err := NewCloudFlareProvider(
endpoint.NewDomainFilter([]string{"bar.com"}),
provider.ZoneIDFilter{},
true,
false,
RegionalServicesConfig{Enabled: true, RegionKey: "us"},
CustomHostnamesConfig{Enabled: false},
DNSRecordsConfig{PerPage: 50, Comment: paidValidCommentBuilder.String()},
)
if err != nil {
t.Fatal(err)
}

paidProvider.Client = NewMockCloudFlareClient()
commentTestCases := []struct {
name string
provider *CloudFlareProvider
Expand All @@ -1894,31 +1865,31 @@ func TestCloudFlareProvider_newCloudFlareChange(t *testing.T) {
}{
{
name: "For free Zone respecting comment length, expect no trimming",
provider: p,
provider: freeProvider,
endpoint: &endpoint.Endpoint{
DNSName: "example.com",
RecordType: "A",
Targets: []string{"192.0.2.1"},
ProviderSpecific: endpoint.ProviderSpecific{
{
Name: annotations.CloudflareRecordCommentKey,
Value: freeValidCommentBuilder.String(),
Value: freeValidComment,
},
},
},
expected: len(freeValidCommentBuilder.String()),
expected: len(freeValidComment),
},
{
name: "For free Zones not respecting comment length, expect trimmed comments",
provider: p,
provider: freeProvider,
endpoint: &endpoint.Endpoint{
DNSName: "example.com",
RecordType: "A",
Targets: []string{"192.0.2.1"},
ProviderSpecific: endpoint.ProviderSpecific{
{
Name: annotations.CloudflareRecordCommentKey,
Value: freeInvalidCommentBuilder.String(),
Value: freeInvalidComment,
},
},
},
Expand All @@ -1934,11 +1905,11 @@ func TestCloudFlareProvider_newCloudFlareChange(t *testing.T) {
ProviderSpecific: endpoint.ProviderSpecific{
{
Name: annotations.CloudflareRecordCommentKey,
Value: paidValidCommentBuilder.String(),
Value: paidValidComment,
},
},
},
expected: len(paidValidCommentBuilder.String()),
expected: len(paidValidComment),
},
{
name: "For paid Zones not respecting comment length, expect trimmed comments",
Expand All @@ -1950,7 +1921,7 @@ func TestCloudFlareProvider_newCloudFlareChange(t *testing.T) {
ProviderSpecific: endpoint.ProviderSpecific{
{
Name: annotations.CloudflareRecordCommentKey,
Value: paidInvalidCommentBuilder.String(),
Value: paidInvalidComment,
},
},
},
Expand All @@ -1960,6 +1931,7 @@ func TestCloudFlareProvider_newCloudFlareChange(t *testing.T) {

for _, test := range commentTestCases {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
change, err := test.provider.newCloudFlareChange(cloudFlareCreate, test.endpoint, test.endpoint.Targets[0], nil)
assert.NoError(t, err)
if len(change.ResourceRecord.Comment) != test.expected {
Expand Down Expand Up @@ -3216,7 +3188,6 @@ func TestConvertCloudflareErrorInContext(t *testing.T) {
})
}
}

func TestCloudFlareZonesDomainFilter(t *testing.T) {
// Set required environment variables for CloudFlare provider
t.Setenv("CF_API_TOKEN", "test-token")
Expand Down
Loading