-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b748116
commit 877f346
Showing
1 changed file
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package mikrotik | ||
|
||
import ( | ||
"testing" | ||
|
||
"sigs.k8s.io/external-dns/endpoint" | ||
"sigs.k8s.io/external-dns/plan" | ||
) | ||
|
||
func TestCleanupChanges(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputChanges *plan.Changes | ||
expectedChanges *plan.Changes | ||
}{ | ||
{ | ||
name: "Matching ProviderSpecific properties - should clean up", | ||
inputChanges: &plan.Changes{ | ||
UpdateOld: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
ProviderSpecific: endpoint.ProviderSpecific{ | ||
{Name: "comment", Value: "test comment"}, | ||
{Name: "match-subdomain", Value: "true"}, | ||
{Name: "address-list", Value: "main"}, | ||
{Name: "regexp", Value: ".*"}, | ||
}, | ||
}, | ||
}, | ||
UpdateNew: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
ProviderSpecific: endpoint.ProviderSpecific{ | ||
{Name: "webhook/comment", Value: "test comment"}, | ||
{Name: "webhook/match-subdomain", Value: "true"}, | ||
{Name: "webhook/address-list", Value: "main"}, | ||
{Name: "webhook/regexp", Value: ".*"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedChanges: &plan.Changes{ | ||
UpdateOld: []*endpoint.Endpoint{}, // should be empty after cleanup | ||
UpdateNew: []*endpoint.Endpoint{}, // should be empty after cleanup | ||
}, | ||
}, | ||
{ | ||
name: "Different ProviderSpecific - should not clean up", | ||
inputChanges: &plan.Changes{ | ||
UpdateOld: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
ProviderSpecific: endpoint.ProviderSpecific{ | ||
{Name: "comment", Value: "old comment"}, | ||
{Name: "match-subdomain", Value: "true"}, | ||
{Name: "address-list", Value: "main"}, | ||
{Name: "regexp", Value: ".*"}, | ||
}, | ||
}, | ||
}, | ||
UpdateNew: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
ProviderSpecific: endpoint.ProviderSpecific{ | ||
{Name: "webhook/comment", Value: "new comment"}, | ||
{Name: "webhook/match-subdomain", Value: "true"}, | ||
{Name: "webhook/address-list", Value: "main"}, | ||
{Name: "webhook/regexp", Value: ".*"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedChanges: &plan.Changes{ | ||
UpdateOld: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
ProviderSpecific: endpoint.ProviderSpecific{ | ||
{Name: "comment", Value: "old comment"}, | ||
{Name: "match-subdomain", Value: "true"}, | ||
{Name: "address-list", Value: "main"}, | ||
{Name: "regexp", Value: ".*"}, | ||
}, | ||
}, | ||
}, | ||
UpdateNew: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
ProviderSpecific: endpoint.ProviderSpecific{ | ||
{Name: "webhook/comment", Value: "new comment"}, | ||
{Name: "webhook/match-subdomain", Value: "true"}, | ||
{Name: "webhook/address-list", Value: "main"}, | ||
{Name: "webhook/regexp", Value: ".*"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Different DNSName, same Targets and TTL - should not clean up", | ||
inputChanges: &plan.Changes{ | ||
UpdateOld: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "different.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
}, | ||
}, | ||
UpdateNew: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
}, | ||
}, | ||
}, | ||
expectedChanges: &plan.Changes{ | ||
UpdateOld: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "different.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
}, | ||
}, | ||
UpdateNew: []*endpoint.Endpoint{ | ||
{ | ||
DNSName: "example.com", | ||
Targets: endpoint.NewTargets("1.1.1.1"), | ||
RecordTTL: endpoint.TTL(3600), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Execute the cleanup function | ||
outputChanges := cleanupChanges(tt.inputChanges) | ||
|
||
// Compare UpdateOld | ||
if len(outputChanges.UpdateOld) != len(tt.expectedChanges.UpdateOld) { | ||
t.Errorf("Expected UpdateOld length %d, got %d", len(tt.expectedChanges.UpdateOld), len(outputChanges.UpdateOld)) | ||
} else { | ||
for i := range tt.expectedChanges.UpdateOld { | ||
if outputChanges.UpdateOld[i].DNSName != tt.expectedChanges.UpdateOld[i].DNSName { | ||
t.Errorf("Expected UpdateOld[%d].DNSName %s, got %s", i, tt.expectedChanges.UpdateOld[i].DNSName, outputChanges.UpdateOld[i].DNSName) | ||
} | ||
} | ||
} | ||
|
||
// Compare UpdateNew | ||
if len(outputChanges.UpdateNew) != len(tt.expectedChanges.UpdateNew) { | ||
t.Errorf("Expected UpdateNew length %d, got %d", len(tt.expectedChanges.UpdateNew), len(outputChanges.UpdateNew)) | ||
} else { | ||
for i := range tt.expectedChanges.UpdateNew { | ||
if outputChanges.UpdateNew[i].DNSName != tt.expectedChanges.UpdateNew[i].DNSName { | ||
t.Errorf("Expected UpdateNew[%d].DNSName %s, got %s", i, tt.expectedChanges.UpdateNew[i].DNSName, outputChanges.UpdateNew[i].DNSName) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |