diff --git a/controller/controller_test.go b/controller/controller_test.go index 5abd5677e5..ecba8c2b07 100644 --- a/controller/controller_test.go +++ b/controller/controller_test.go @@ -32,6 +32,7 @@ import ( "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider" "sigs.k8s.io/external-dns/registry" + "sigs.k8s.io/external-dns/source" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -130,9 +131,9 @@ func newMockProvider(endpoints []*endpoint.Endpoint, changes *plan.Changes) prov return dnsProvider } -func getTestSource() *testutils.MockSource { +func getTestSource() *source.MockSource { // Fake some desired endpoints coming from our source. - source := new(testutils.MockSource) + source := new(source.MockSource) source.On("Endpoints").Return([]*endpoint.Endpoint{ { DNSName: "create-record", @@ -339,7 +340,7 @@ func testControllerFiltersDomains(t *testing.T, configuredEndpoints []*endpoint. cfg := externaldns.NewConfig() cfg.ManagedDNSRecordTypes = []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA, endpoint.RecordTypeCNAME} - source := new(testutils.MockSource) + source := new(source.MockSource) source.On("Endpoints").Return(configuredEndpoints, nil) // Fake some existing records in our DNS provider and validate some desired changes. diff --git a/controller/metrics_test.go b/controller/metrics_test.go index 8a468c32bb..af3320fd67 100644 --- a/controller/metrics_test.go +++ b/controller/metrics_test.go @@ -26,6 +26,7 @@ import ( "sigs.k8s.io/external-dns/pkg/apis/externaldns" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/registry" + "sigs.k8s.io/external-dns/source" ) func TestRecordKnownEndpointType(t *testing.T) { @@ -322,7 +323,7 @@ func TestAAAARecords(t *testing.T) { } func TestGaugeMetricsWithMixedRecords(t *testing.T) { - configuredEndpoints := testutils.GenerateTestEndpointsByType(map[string]int{ + configuredEndpoints := endpoint.GenerateTestEndpointsByType(map[string]int{ endpoint.RecordTypeA: 534, endpoint.RecordTypeAAAA: 324, endpoint.RecordTypeCNAME: 2, @@ -331,7 +332,7 @@ func TestGaugeMetricsWithMixedRecords(t *testing.T) { endpoint.RecordTypeNS: 3, }) - providerEndpoints := testutils.GenerateTestEndpointsByType(map[string]int{ + providerEndpoints := endpoint.GenerateTestEndpointsByType(map[string]int{ endpoint.RecordTypeA: 5334, endpoint.RecordTypeAAAA: 324, endpoint.RecordTypeCNAME: 23, @@ -344,7 +345,7 @@ func TestGaugeMetricsWithMixedRecords(t *testing.T) { cfg := externaldns.NewConfig() cfg.ManagedDNSRecordTypes = endpoint.KnownRecordTypes - source := new(testutils.MockSource) + source := new(source.MockSource) source.On("Endpoints").Return(configuredEndpoints, nil) provider := &filteredMockProvider{ diff --git a/endpoint/endpoint_test.go b/endpoint/endpoint_test.go index 31b26a159b..398c11e1be 100644 --- a/endpoint/endpoint_test.go +++ b/endpoint/endpoint_test.go @@ -18,10 +18,13 @@ package endpoint import ( "fmt" + "net/netip" "reflect" + "sort" "testing" "github.com/stretchr/testify/assert" + "sigs.k8s.io/external-dns/pkg/events" ) @@ -982,3 +985,466 @@ func TestEndpoint_WithRefObject(t *testing.T) { assert.Equal(t, ref, ep.RefObject(), "refObject should be set") assert.Equal(t, ep, result, "should return the same Endpoint pointer") } + +func TestExampleSameEndpoints(t *testing.T) { + eps := []*Endpoint{ + { + DNSName: "example.org", + Targets: Targets{"load-balancer.org"}, + }, + { + DNSName: "example.org", + Targets: Targets{"load-balancer.org"}, + RecordType: RecordTypeTXT, + }, + { + DNSName: "abc.com", + Targets: Targets{"something"}, + RecordType: RecordTypeTXT, + }, + { + DNSName: "abc.com", + Targets: Targets{"1.2.3.4"}, + RecordType: RecordTypeA, + SetIdentifier: "test-set-1", + }, + { + DNSName: "bbc.com", + Targets: Targets{"foo.com"}, + RecordType: RecordTypeCNAME, + }, + { + DNSName: "cbc.com", + Targets: Targets{"foo.com"}, + RecordType: "CNAME", + RecordTTL: TTL(60), + }, + { + DNSName: "example.org", + Targets: Targets{"load-balancer.org"}, + ProviderSpecific: ProviderSpecific{ + ProviderSpecificProperty{Name: "foo", Value: "bar"}, + }, + }, + } + sort.Sort(byAllFields(eps)) + for _, ep := range eps { + fmt.Println(ep) + } + // Output: + // abc.com 0 IN A test-set-1 1.2.3.4 [] + // abc.com 0 IN TXT something [] + // bbc.com 0 IN CNAME foo.com [] + // cbc.com 60 IN CNAME foo.com [] + // example.org 0 IN load-balancer.org [] + // example.org 0 IN load-balancer.org [{foo bar}] + // example.org 0 IN TXT load-balancer.org [] +} + +func makeEndpoint(DNSName string) *Endpoint { + return &Endpoint{ + DNSName: DNSName, + Targets: Targets{"target.com"}, + RecordType: "A", + SetIdentifier: "set1", + RecordTTL: 300, + Labels: map[string]string{ + OwnerLabelKey: "owner", + ResourceLabelKey: "resource", + OwnedRecordLabelKey: "owned", + }, + ProviderSpecific: ProviderSpecific{ + {Name: "key", Value: "val"}, + }, + } +} + +func TestSameEndpoint(t *testing.T) { + tests := []struct { + name string + a *Endpoint + b *Endpoint + isSameEndpoint bool + }{ + { + name: "DNSName is not equal", + a: &Endpoint{DNSName: "example.org"}, + b: &Endpoint{DNSName: "example.com"}, + isSameEndpoint: false, + }, + { + name: "All fields are equal", + a: &Endpoint{ + DNSName: "example.org", + Targets: Targets{"lb.example.com"}, + RecordType: "A", + SetIdentifier: "set-1", + RecordTTL: 300, + Labels: map[string]string{ + OwnerLabelKey: "owner-1", + ResourceLabelKey: "resource-1", + OwnedRecordLabelKey: "owned-true", + }, + ProviderSpecific: ProviderSpecific{ + {Name: "key1", Value: "val1"}, + }, + }, + b: &Endpoint{ + DNSName: "example.org", + Targets: Targets{"lb.example.com"}, + RecordType: "A", + SetIdentifier: "set-1", + RecordTTL: 300, + Labels: map[string]string{ + OwnerLabelKey: "owner-1", + ResourceLabelKey: "resource-1", + OwnedRecordLabelKey: "owned-true", + }, + ProviderSpecific: ProviderSpecific{ + {Name: "key1", Value: "val1"}, + }, + }, + isSameEndpoint: true, + }, + { + name: "Different Targets", + a: &Endpoint{DNSName: "example.org", Targets: Targets{"a.com"}}, + b: &Endpoint{DNSName: "example.org", Targets: Targets{"b.com"}}, + isSameEndpoint: false, + }, + { + name: "Different RecordType", + a: &Endpoint{DNSName: "example.org", RecordType: "A"}, + b: &Endpoint{DNSName: "example.org", RecordType: "CNAME"}, + isSameEndpoint: false, + }, + { + name: "Different SetIdentifier", + a: &Endpoint{DNSName: "example.org", SetIdentifier: "id1"}, + b: &Endpoint{DNSName: "example.org", SetIdentifier: "id2"}, + isSameEndpoint: false, + }, + { + name: "Different OwnerLabelKey", + a: &Endpoint{ + DNSName: "example.org", + Labels: map[string]string{ + OwnerLabelKey: "owner1", + }, + }, + b: &Endpoint{ + DNSName: "example.org", + Labels: map[string]string{ + OwnerLabelKey: "owner2", + }, + }, + isSameEndpoint: false, + }, + { + name: "Different RecordTTL", + a: &Endpoint{DNSName: "example.org", RecordTTL: 300}, + b: &Endpoint{DNSName: "example.org", RecordTTL: 400}, + isSameEndpoint: false, + }, + { + name: "Different ProviderSpecific", + a: &Endpoint{ + DNSName: "example.org", + ProviderSpecific: ProviderSpecific{ + {Name: "key1", Value: "val1"}, + }, + }, + b: &Endpoint{ + DNSName: "example.org", + ProviderSpecific: ProviderSpecific{ + {Name: "key1", Value: "val2"}, + }, + }, + isSameEndpoint: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + isSameEndpoint := SameEndpoint(tt.a, tt.b) + assert.Equal(t, tt.isSameEndpoint, isSameEndpoint) + }) + } +} +func TestSameEndpoints(t *testing.T) { + tests := []struct { + name string + a, b []*Endpoint + want bool + }{ + { + name: "Both slices nil", + a: nil, + b: nil, + want: true, + }, + { + name: "One nil, one empty", + a: []*Endpoint{}, + b: nil, + want: true, + }, + { + name: "Different lengths", + a: []*Endpoint{makeEndpoint("a.com")}, + b: []*Endpoint{}, + want: false, + }, + { + name: "Same endpoints in same order", + a: []*Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, + b: []*Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, + want: true, + }, + { + name: "Same endpoints in different order", + a: []*Endpoint{makeEndpoint("b.com"), makeEndpoint("a.com")}, + b: []*Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, + want: true, + }, + { + name: "One endpoint differs", + a: []*Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, + b: []*Endpoint{makeEndpoint("a.com"), makeEndpoint("c.com")}, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + isSameEndpoints := SameEndpoints(tt.a, tt.b) + assert.Equal(t, tt.want, isSameEndpoints) + }) + } +} + +func TestSameEndpointLabel(t *testing.T) { + tests := []struct { + name string + a []*Endpoint + b []*Endpoint + want bool + }{ + { + name: "length of a and b are not same", + a: []*Endpoint{makeEndpoint("a.com")}, + b: []*Endpoint{makeEndpoint("b.com"), makeEndpoint("c.com")}, + want: false, + }, + { + name: "endpoint's labels are same in a and b", + a: []*Endpoint{makeEndpoint("a.com"), makeEndpoint("c.com")}, + b: []*Endpoint{makeEndpoint("b.com"), makeEndpoint("c.com")}, + want: true, + }, + { + name: "endpoint's labels are not same in a and b", + a: []*Endpoint{ + { + DNSName: "a.com", + Labels: Labels{ + OwnerLabelKey: "owner1", + ResourceLabelKey: "resource1", + }, + }, + { + DNSName: "b.com", + Labels: Labels{ + OwnerLabelKey: "owner2", + ResourceLabelKey: "resource2", + }, + }, + }, + b: []*Endpoint{ + { + DNSName: "a.com", + Labels: Labels{ + OwnerLabelKey: "owner", + ResourceLabelKey: "resource", + }, + }, + { + DNSName: "b.com", + Labels: Labels{ + OwnerLabelKey: "owner1", + ResourceLabelKey: "resource1", + }, + }, + }, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + isSameEndpointLabels := SameEndpointLabels(tt.a, tt.b) + assert.Equal(t, tt.want, isSameEndpointLabels) + }) + } +} + +func TestSamePlanChanges(t *testing.T) { + tests := []struct { + name string + a map[string][]*Endpoint + b map[string][]*Endpoint + want bool + }{ + { + name: "endpoints with all operations in a and b are same", + a: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + b: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + want: true, + }, + { + name: "endpoints for create operations in a and b are not same", + a: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + b: map[string][]*Endpoint{ + "Create": {makeEndpoint("x.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + want: false, + }, + { + name: "endpoints for delete operations in a and b are not same", + a: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + b: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("g.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + want: false, + }, + { + name: "endpoints for updateOld operations in a and b are not same", + a: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("b.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + b: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("c.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + want: false, + }, + { + name: "endpoints for updateNew operations in a and b are same", + a: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("d.com")}, + }, + b: map[string][]*Endpoint{ + "Create": {makeEndpoint("a.com")}, + "Delete": {makeEndpoint("b.com")}, + "UpdateOld": {makeEndpoint("a.com")}, + "UpdateNew": {makeEndpoint("c.com")}, + }, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + checkPlanChanges := SamePlanChanges(tt.a, tt.b) + assert.Equal(t, tt.want, checkPlanChanges) + }) + } +} +func TestNewTargetsFromAddr(t *testing.T) { + tests := []struct { + name string + input []netip.Addr + expected Targets + }{ + { + name: "empty slice", + input: []netip.Addr{}, + expected: Targets{}, + }, + { + name: "single IPv4 address", + input: []netip.Addr{ + netip.MustParseAddr("192.0.2.1"), + }, + expected: Targets{"192.0.2.1"}, + }, + { + name: "multiple IP addresses", + input: []netip.Addr{ + netip.MustParseAddr("192.0.2.1"), + netip.MustParseAddr("2001:db8::1"), + }, + expected: Targets{"192.0.2.1", "2001:db8::1"}, + }, + { + name: "IPv6 address only", + input: []netip.Addr{ + netip.MustParseAddr("::1"), + }, + expected: Targets{"::1"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewTargetsFromAddr(tt.input) + if !reflect.DeepEqual(got, tt.expected) { + t.Errorf("NewTargetsFromAddr() = %v, want %v", got, tt.expected) + } + }) + } +} + +func TestWithLabel(t *testing.T) { + e := &Endpoint{} + // should initialize Labels and set the key + returned := e.WithLabel("foo", "bar") + assert.Equal(t, e, returned) + assert.NotNil(t, e.Labels) + assert.Equal(t, "bar", e.Labels["foo"]) + + // overriding an existing key + e2 := e.WithLabel("foo", "baz") + assert.Equal(t, e, e2) + assert.Equal(t, "baz", e.Labels["foo"]) + + // adding a new key without wiping others + e.Labels["existing"] = "orig" + e.WithLabel("new", "val") + assert.Equal(t, "orig", e.Labels["existing"]) + assert.Equal(t, "val", e.Labels["new"]) +} diff --git a/internal/testutils/endpoint.go b/endpoint/test_utils.go similarity index 79% rename from internal/testutils/endpoint.go rename to endpoint/test_utils.go index 0a33d3a218..06444a0708 100644 --- a/internal/testutils/endpoint.go +++ b/endpoint/test_utils.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package testutils +package endpoint import ( "fmt" @@ -23,19 +23,17 @@ import ( "reflect" "sort" "strings" - - "sigs.k8s.io/external-dns/endpoint" ) /** test utility functions for endpoints verifications */ -type byNames endpoint.ProviderSpecific +type byNames ProviderSpecific func (p byNames) Len() int { return len(p) } func (p byNames) Swap(i, j int) { p[i], p[j] = p[j], p[i] } func (p byNames) Less(i, j int) bool { return p[i].Name < p[j].Name } -type byAllFields []*endpoint.Endpoint +type byAllFields []*Endpoint func (b byAllFields) Len() int { return len(b) } func (b byAllFields) Swap(i, j int) { b[i], b[j] = b[j], b[i] } @@ -62,11 +60,11 @@ func (b byAllFields) Less(i, j int) bool { // SameEndpoint returns true if two endpoints are same // considers example.org. and example.org DNSName/Target as different endpoints -func SameEndpoint(a, b *endpoint.Endpoint) bool { +func SameEndpoint(a, b *Endpoint) bool { return a.DNSName == b.DNSName && a.Targets.Same(b.Targets) && a.RecordType == b.RecordType && a.SetIdentifier == b.SetIdentifier && - a.Labels[endpoint.OwnerLabelKey] == b.Labels[endpoint.OwnerLabelKey] && a.RecordTTL == b.RecordTTL && - a.Labels[endpoint.ResourceLabelKey] == b.Labels[endpoint.ResourceLabelKey] && - a.Labels[endpoint.OwnedRecordLabelKey] == b.Labels[endpoint.OwnedRecordLabelKey] && + a.Labels[OwnerLabelKey] == b.Labels[OwnerLabelKey] && a.RecordTTL == b.RecordTTL && + a.Labels[ResourceLabelKey] == b.Labels[ResourceLabelKey] && + a.Labels[OwnedRecordLabelKey] == b.Labels[OwnedRecordLabelKey] && SameProviderSpecific(a.ProviderSpecific, b.ProviderSpecific) } @@ -75,7 +73,7 @@ func SameEndpoint(a, b *endpoint.Endpoint) bool { // [x,x,z] == [x,z,x] // [x,y,y] != [x,x,y] // [x,x,x] != [x,x,z] -func SameEndpoints(a, b []*endpoint.Endpoint) bool { +func SameEndpoints(a, b []*Endpoint) bool { if len(a) != len(b) { return false } @@ -94,7 +92,7 @@ func SameEndpoints(a, b []*endpoint.Endpoint) bool { } // SameEndpointLabels verifies that labels of the two slices of endpoints are the same -func SameEndpointLabels(a, b []*endpoint.Endpoint) bool { +func SameEndpointLabels(a, b []*Endpoint) bool { if len(a) != len(b) { return false } @@ -113,13 +111,13 @@ func SameEndpointLabels(a, b []*endpoint.Endpoint) bool { } // SamePlanChanges verifies that two set of changes are the same -func SamePlanChanges(a, b map[string][]*endpoint.Endpoint) bool { +func SamePlanChanges(a, b map[string][]*Endpoint) bool { return SameEndpoints(a["Create"], b["Create"]) && SameEndpoints(a["Delete"], b["Delete"]) && SameEndpoints(a["UpdateOld"], b["UpdateOld"]) && SameEndpoints(a["UpdateNew"], b["UpdateNew"]) } // SameProviderSpecific verifies that two maps contain the same string/string key/value pairs -func SameProviderSpecific(a, b endpoint.ProviderSpecific) bool { +func SameProviderSpecific(a, b ProviderSpecific) bool { sa := a sb := b sort.Sort(byNames(sa)) @@ -128,8 +126,8 @@ func SameProviderSpecific(a, b endpoint.ProviderSpecific) bool { } // NewTargetsFromAddr convert an array of netip.Addr to Targets (array of string) -func NewTargetsFromAddr(targets []netip.Addr) endpoint.Targets { - t := make(endpoint.Targets, len(targets)) +func NewTargetsFromAddr(targets []netip.Addr) Targets { + t := make(Targets, len(targets)) for i, target := range targets { t[i] = target.String() } @@ -141,14 +139,14 @@ func NewTargetsFromAddr(targets []netip.Addr) endpoint.Targets { // // endpoints := GenerateTestEndpointsByType(map[string]int{"A": 2, "CNAME": 1}) // // endpoints will contain 2 A records and 1 CNAME record with unique DNS names and targets. -func GenerateTestEndpointsByType(typeCounts map[string]int) []*endpoint.Endpoint { - var result []*endpoint.Endpoint +func GenerateTestEndpointsByType(typeCounts map[string]int) []*Endpoint { + var result []*Endpoint idx := 0 for rt, count := range typeCounts { for i := 0; i < count; i++ { - result = append(result, &endpoint.Endpoint{ + result = append(result, &Endpoint{ DNSName: fmt.Sprintf("%s-%d.example.com", strings.ToLower(rt), idx), - Targets: endpoint.Targets{fmt.Sprintf("192.0.2.%d", idx)}, + Targets: Targets{fmt.Sprintf("192.0.2.%d", idx)}, RecordType: rt, RecordTTL: 300, }) diff --git a/internal/testutils/endpoint_test.go b/internal/testutils/endpoint_test.go deleted file mode 100644 index 6f109def0e..0000000000 --- a/internal/testutils/endpoint_test.go +++ /dev/null @@ -1,491 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package testutils - -import ( - "fmt" - "net/netip" - "reflect" - "sort" - "testing" - - "github.com/stretchr/testify/assert" - "sigs.k8s.io/external-dns/endpoint" -) - -func TestExampleSameEndpoints(t *testing.T) { - eps := []*endpoint.Endpoint{ - { - DNSName: "example.org", - Targets: endpoint.Targets{"load-balancer.org"}, - }, - { - DNSName: "example.org", - Targets: endpoint.Targets{"load-balancer.org"}, - RecordType: endpoint.RecordTypeTXT, - }, - { - DNSName: "abc.com", - Targets: endpoint.Targets{"something"}, - RecordType: endpoint.RecordTypeTXT, - }, - { - DNSName: "abc.com", - Targets: endpoint.Targets{"1.2.3.4"}, - RecordType: endpoint.RecordTypeA, - SetIdentifier: "test-set-1", - }, - { - DNSName: "bbc.com", - Targets: endpoint.Targets{"foo.com"}, - RecordType: endpoint.RecordTypeCNAME, - }, - { - DNSName: "cbc.com", - Targets: endpoint.Targets{"foo.com"}, - RecordType: "CNAME", - RecordTTL: endpoint.TTL(60), - }, - { - DNSName: "example.org", - Targets: endpoint.Targets{"load-balancer.org"}, - ProviderSpecific: endpoint.ProviderSpecific{ - endpoint.ProviderSpecificProperty{Name: "foo", Value: "bar"}, - }, - }, - } - sort.Sort(byAllFields(eps)) - for _, ep := range eps { - fmt.Println(ep) - } - // Output: - // abc.com 0 IN A test-set-1 1.2.3.4 [] - // abc.com 0 IN TXT something [] - // bbc.com 0 IN CNAME foo.com [] - // cbc.com 60 IN CNAME foo.com [] - // example.org 0 IN load-balancer.org [] - // example.org 0 IN load-balancer.org [{foo bar}] - // example.org 0 IN TXT load-balancer.org [] -} - -func makeEndpoint(DNSName string) *endpoint.Endpoint { - return &endpoint.Endpoint{ - DNSName: DNSName, - Targets: endpoint.Targets{"target.com"}, - RecordType: "A", - SetIdentifier: "set1", - RecordTTL: 300, - Labels: map[string]string{ - endpoint.OwnerLabelKey: "owner", - endpoint.ResourceLabelKey: "resource", - endpoint.OwnedRecordLabelKey: "owned", - }, - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "key", Value: "val"}, - }, - } -} - -func TestSameEndpoint(t *testing.T) { - tests := []struct { - name string - a *endpoint.Endpoint - b *endpoint.Endpoint - isSameEndpoint bool - }{ - { - name: "DNSName is not equal", - a: &endpoint.Endpoint{DNSName: "example.org"}, - b: &endpoint.Endpoint{DNSName: "example.com"}, - isSameEndpoint: false, - }, - { - name: "All fields are equal", - a: &endpoint.Endpoint{ - DNSName: "example.org", - Targets: endpoint.Targets{"lb.example.com"}, - RecordType: "A", - SetIdentifier: "set-1", - RecordTTL: 300, - Labels: map[string]string{ - endpoint.OwnerLabelKey: "owner-1", - endpoint.ResourceLabelKey: "resource-1", - endpoint.OwnedRecordLabelKey: "owned-true", - }, - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "key1", Value: "val1"}, - }, - }, - b: &endpoint.Endpoint{ - DNSName: "example.org", - Targets: endpoint.Targets{"lb.example.com"}, - RecordType: "A", - SetIdentifier: "set-1", - RecordTTL: 300, - Labels: map[string]string{ - endpoint.OwnerLabelKey: "owner-1", - endpoint.ResourceLabelKey: "resource-1", - endpoint.OwnedRecordLabelKey: "owned-true", - }, - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "key1", Value: "val1"}, - }, - }, - isSameEndpoint: true, - }, - { - name: "Different Targets", - a: &endpoint.Endpoint{DNSName: "example.org", Targets: endpoint.Targets{"a.com"}}, - b: &endpoint.Endpoint{DNSName: "example.org", Targets: endpoint.Targets{"b.com"}}, - isSameEndpoint: false, - }, - { - name: "Different RecordType", - a: &endpoint.Endpoint{DNSName: "example.org", RecordType: "A"}, - b: &endpoint.Endpoint{DNSName: "example.org", RecordType: "CNAME"}, - isSameEndpoint: false, - }, - { - name: "Different SetIdentifier", - a: &endpoint.Endpoint{DNSName: "example.org", SetIdentifier: "id1"}, - b: &endpoint.Endpoint{DNSName: "example.org", SetIdentifier: "id2"}, - isSameEndpoint: false, - }, - { - name: "Different OwnerLabelKey", - a: &endpoint.Endpoint{ - DNSName: "example.org", - Labels: map[string]string{ - endpoint.OwnerLabelKey: "owner1", - }, - }, - b: &endpoint.Endpoint{ - DNSName: "example.org", - Labels: map[string]string{ - endpoint.OwnerLabelKey: "owner2", - }, - }, - isSameEndpoint: false, - }, - { - name: "Different RecordTTL", - a: &endpoint.Endpoint{DNSName: "example.org", RecordTTL: 300}, - b: &endpoint.Endpoint{DNSName: "example.org", RecordTTL: 400}, - isSameEndpoint: false, - }, - { - name: "Different ProviderSpecific", - a: &endpoint.Endpoint{ - DNSName: "example.org", - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "key1", Value: "val1"}, - }, - }, - b: &endpoint.Endpoint{ - DNSName: "example.org", - ProviderSpecific: endpoint.ProviderSpecific{ - {Name: "key1", Value: "val2"}, - }, - }, - isSameEndpoint: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - isSameEndpoint := SameEndpoint(tt.a, tt.b) - assert.Equal(t, tt.isSameEndpoint, isSameEndpoint) - }) - } -} -func TestSameEndpoints(t *testing.T) { - tests := []struct { - name string - a, b []*endpoint.Endpoint - want bool - }{ - { - name: "Both slices nil", - a: nil, - b: nil, - want: true, - }, - { - name: "One nil, one empty", - a: []*endpoint.Endpoint{}, - b: nil, - want: true, - }, - { - name: "Different lengths", - a: []*endpoint.Endpoint{makeEndpoint("a.com")}, - b: []*endpoint.Endpoint{}, - want: false, - }, - { - name: "Same endpoints in same order", - a: []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, - b: []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, - want: true, - }, - { - name: "Same endpoints in different order", - a: []*endpoint.Endpoint{makeEndpoint("b.com"), makeEndpoint("a.com")}, - b: []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, - want: true, - }, - { - name: "One endpoint differs", - a: []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("b.com")}, - b: []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("c.com")}, - want: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - isSameEndpoints := SameEndpoints(tt.a, tt.b) - assert.Equal(t, tt.want, isSameEndpoints) - }) - } -} - -func TestSameEndpointLabel(t *testing.T) { - tests := []struct { - name string - a []*endpoint.Endpoint - b []*endpoint.Endpoint - want bool - }{ - { - name: "length of a and b are not same", - a: []*endpoint.Endpoint{makeEndpoint("a.com")}, - b: []*endpoint.Endpoint{makeEndpoint("b.com"), makeEndpoint("c.com")}, - want: false, - }, - { - name: "endpoint's labels are same in a and b", - a: []*endpoint.Endpoint{makeEndpoint("a.com"), makeEndpoint("c.com")}, - b: []*endpoint.Endpoint{makeEndpoint("b.com"), makeEndpoint("c.com")}, - want: true, - }, - { - name: "endpoint's labels are not same in a and b", - a: []*endpoint.Endpoint{ - { - DNSName: "a.com", - Labels: endpoint.Labels{ - endpoint.OwnerLabelKey: "owner1", - endpoint.ResourceLabelKey: "resource1", - }, - }, - { - DNSName: "b.com", - Labels: endpoint.Labels{ - endpoint.OwnerLabelKey: "owner2", - endpoint.ResourceLabelKey: "resource2", - }, - }, - }, - b: []*endpoint.Endpoint{ - { - DNSName: "a.com", - Labels: endpoint.Labels{ - endpoint.OwnerLabelKey: "owner", - endpoint.ResourceLabelKey: "resource", - }, - }, - { - DNSName: "b.com", - Labels: endpoint.Labels{ - endpoint.OwnerLabelKey: "owner1", - endpoint.ResourceLabelKey: "resource1", - }, - }, - }, - want: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - isSameEndpointLabels := SameEndpointLabels(tt.a, tt.b) - assert.Equal(t, tt.want, isSameEndpointLabels) - }) - } -} - -func TestSamePlanChanges(t *testing.T) { - tests := []struct { - name string - a map[string][]*endpoint.Endpoint - b map[string][]*endpoint.Endpoint - want bool - }{ - { - name: "endpoints with all operations in a and b are same", - a: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - b: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - want: true, - }, - { - name: "endpoints for create operations in a and b are not same", - a: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - b: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("x.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - want: false, - }, - { - name: "endpoints for delete operations in a and b are not same", - a: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - b: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("g.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - want: false, - }, - { - name: "endpoints for updateOld operations in a and b are not same", - a: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("b.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - b: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("c.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - want: false, - }, - { - name: "endpoints for updateNew operations in a and b are same", - a: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("d.com")}, - }, - b: map[string][]*endpoint.Endpoint{ - "Create": {makeEndpoint("a.com")}, - "Delete": {makeEndpoint("b.com")}, - "UpdateOld": {makeEndpoint("a.com")}, - "UpdateNew": {makeEndpoint("c.com")}, - }, - want: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - checkPlanChanges := SamePlanChanges(tt.a, tt.b) - assert.Equal(t, tt.want, checkPlanChanges) - }) - } -} -func TestNewTargetsFromAddr(t *testing.T) { - tests := []struct { - name string - input []netip.Addr - expected endpoint.Targets - }{ - { - name: "empty slice", - input: []netip.Addr{}, - expected: endpoint.Targets{}, - }, - { - name: "single IPv4 address", - input: []netip.Addr{ - netip.MustParseAddr("192.0.2.1"), - }, - expected: endpoint.Targets{"192.0.2.1"}, - }, - { - name: "multiple IP addresses", - input: []netip.Addr{ - netip.MustParseAddr("192.0.2.1"), - netip.MustParseAddr("2001:db8::1"), - }, - expected: endpoint.Targets{"192.0.2.1", "2001:db8::1"}, - }, - { - name: "IPv6 address only", - input: []netip.Addr{ - netip.MustParseAddr("::1"), - }, - expected: endpoint.Targets{"::1"}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := NewTargetsFromAddr(tt.input) - if !reflect.DeepEqual(got, tt.expected) { - t.Errorf("NewTargetsFromAddr() = %v, want %v", got, tt.expected) - } - }) - } -} - -func TestWithLabel(t *testing.T) { - e := &endpoint.Endpoint{} - // should initialize Labels and set the key - returned := e.WithLabel("foo", "bar") - assert.Equal(t, e, returned) - assert.NotNil(t, e.Labels) - assert.Equal(t, "bar", e.Labels["foo"]) - - // overriding an existing key - e2 := e.WithLabel("foo", "baz") - assert.Equal(t, e, e2) - assert.Equal(t, "baz", e.Labels["foo"]) - - // adding a new key without wiping others - e.Labels["existing"] = "orig" - e.WithLabel("new", "val") - assert.Equal(t, "orig", e.Labels["existing"]) - assert.Equal(t, "val", e.Labels["new"]) -} diff --git a/plan/plan_test.go b/plan/plan_test.go index 10ce5a992b..11cc64c88a 100644 --- a/plan/plan_test.go +++ b/plan/plan_test.go @@ -27,7 +27,6 @@ import ( "github.com/stretchr/testify/suite" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" ) type PlanTestSuite struct { @@ -1023,7 +1022,7 @@ func TestPlan(t *testing.T) { // validateEntries validates that the list of entries matches expected. func validateEntries(t *testing.T, entries, expected []*endpoint.Endpoint) { - if !testutils.SameEndpoints(entries, expected) { + if !endpoint.SameEndpoints(entries, expected) { t.Fatalf("expected %q to match %q", entries, expected) } } diff --git a/provider/aws/aws_test.go b/provider/aws/aws_test.go index a269c3ede8..2a7570d6f1 100644 --- a/provider/aws/aws_test.go +++ b/provider/aws/aws_test.go @@ -36,7 +36,6 @@ import ( "github.com/stretchr/testify/require" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider" ) @@ -2008,11 +2007,11 @@ func TestAWSBatchChangeSetExceedingValuesLimitUpsert(t *testing.T) { } func validateEndpoints(t *testing.T, provider *AWSProvider, endpoints []*endpoint.Endpoint, expected []*endpoint.Endpoint) { - assert.True(t, testutils.SameEndpoints(endpoints, expected), "actual and expected endpoints don't match. %+v:%+v", endpoints, expected) + assert.True(t, endpoint.SameEndpoints(endpoints, expected), "actual and expected endpoints don't match. %+v:%+v", endpoints, expected) normalized, err := provider.AdjustEndpoints(endpoints) assert.NoError(t, err) - assert.True(t, testutils.SameEndpoints(normalized, expected), "normalized and expected endpoints don't match. %+v:%+v", normalized, expected) + assert.True(t, endpoint.SameEndpoints(normalized, expected), "normalized and expected endpoints don't match. %+v:%+v", normalized, expected) } func validateAWSZones(t *testing.T, zones map[string]*route53types.HostedZone, expected map[string]*route53types.HostedZone) { diff --git a/provider/awssd/aws_sd_test.go b/provider/awssd/aws_sd_test.go index 3cb4e75841..a0d6447643 100644 --- a/provider/awssd/aws_sd_test.go +++ b/provider/awssd/aws_sd_test.go @@ -169,7 +169,7 @@ func TestAWSSDProvider_Records(t *testing.T) { endpoints, _ := provider.Records(context.Background()) - assert.True(t, testutils.SameEndpoints(expectedEndpoints, endpoints), "expected and actual endpoints don't match, expected=%v, actual=%v", expectedEndpoints, endpoints) + assert.True(t, endpoint.SameEndpoints(expectedEndpoints, endpoints), "expected and actual endpoints don't match, expected=%v, actual=%v", expectedEndpoints, endpoints) } func TestAWSSDProvider_ApplyChanges(t *testing.T) { @@ -212,7 +212,7 @@ func TestAWSSDProvider_ApplyChanges(t *testing.T) { // make sure instances were registered endpoints, _ := provider.Records(ctx) - assert.True(t, testutils.SameEndpoints(expectedEndpoints, endpoints), "expected and actual endpoints don't match, expected=%v, actual=%v", expectedEndpoints, endpoints) + assert.True(t, endpoint.SameEndpoints(expectedEndpoints, endpoints), "expected and actual endpoints don't match, expected=%v, actual=%v", expectedEndpoints, endpoints) ctx = context.Background() // apply deletes @@ -273,7 +273,7 @@ func TestAWSSDProvider_ApplyChanges_Update(t *testing.T) { // make sure instances were registered endpoints, _ := provider.Records(ctx) - assert.True(t, testutils.SameEndpoints(newEndpoints, endpoints), "expected and actual endpoints don't match, expected=%v, actual=%v", newEndpoints, endpoints) + assert.True(t, endpoint.SameEndpoints(newEndpoints, endpoints), "expected and actual endpoints don't match, expected=%v, actual=%v", newEndpoints, endpoints) // make sure only one instance is de-registered assert.Len(t, api.deregistered, 1) diff --git a/provider/azure/azure_test.go b/provider/azure/azure_test.go index a55070e264..461834d27a 100644 --- a/provider/azure/azure_test.go +++ b/provider/azure/azure_test.go @@ -26,7 +26,6 @@ import ( "github.com/stretchr/testify/assert" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider" ) @@ -260,7 +259,7 @@ func newAzureProvider(domainFilter *endpoint.DomainFilter, zoneNameFilter *endpo } func validateAzureEndpoints(t *testing.T, endpoints []*endpoint.Endpoint, expected []*endpoint.Endpoint) { - assert.True(t, testutils.SameEndpoints(endpoints, expected), "actual and expected endpoints don't match. %s:%s", endpoints, expected) + assert.True(t, endpoint.SameEndpoints(endpoints, expected), "actual and expected endpoints don't match. %s:%s", endpoints, expected) } func TestAzureRecord(t *testing.T) { diff --git a/provider/gandi/gandi_test.go b/provider/gandi/gandi_test.go index 8e564b8e1a..3958f134fc 100644 --- a/provider/gandi/gandi_test.go +++ b/provider/gandi/gandi_test.go @@ -25,7 +25,6 @@ import ( "github.com/stretchr/testify/assert" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" ) @@ -251,7 +250,7 @@ func TestGandiProvider_RecordsReturnsCorrectEndpoints(t *testing.T) { assert.Len(t, actualEndpoints, len(expectedEndpoints)) // 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]) { + if !endpoint.SameEndpoint(expectedEndpoints[i], actualEndpoints[i]) { t.Errorf("should be equal, expected:%v <> actual:%v", expectedEndpoints[i], actualEndpoints[i]) } diff --git a/provider/google/google_test.go b/provider/google/google_test.go index 2596325446..508ad86435 100644 --- a/provider/google/google_test.go +++ b/provider/google/google_test.go @@ -31,7 +31,6 @@ import ( "google.golang.org/api/googleapi" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider" ) @@ -839,5 +838,5 @@ func clearGoogleRecords(t *testing.T, provider *GoogleProvider, zone string) { } func validateEndpoints(t *testing.T, endpoints []*endpoint.Endpoint, expected []*endpoint.Endpoint) { - assert.True(t, testutils.SameEndpoints(endpoints, expected), "actual and expected endpoints don't match. %s:%s", endpoints, expected) + assert.True(t, endpoint.SameEndpoints(endpoints, expected), "actual and expected endpoints don't match. %s:%s", endpoints, expected) } diff --git a/provider/inmemory/inmemory_test.go b/provider/inmemory/inmemory_test.go index df648205ac..8149359cc2 100644 --- a/provider/inmemory/inmemory_test.go +++ b/provider/inmemory/inmemory_test.go @@ -23,7 +23,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider" ) @@ -105,7 +104,7 @@ func testInMemoryRecords(t *testing.T) { assert.EqualError(t, err, ErrZoneNotFound.Error()) } else { require.NoError(t, err) - assert.True(t, testutils.SameEndpoints(ti.expected, records), "Endpoints not the same: Expected: %+v Records: %+v", ti.expected, records) + assert.True(t, endpoint.SameEndpoints(ti.expected, records), "Endpoints not the same: Expected: %+v Records: %+v", ti.expected, records) } }) } diff --git a/provider/plural/plural_test.go b/provider/plural/plural_test.go index c2ad4e47d1..a9b58adddf 100644 --- a/provider/plural/plural_test.go +++ b/provider/plural/plural_test.go @@ -23,7 +23,6 @@ import ( "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "github.com/stretchr/testify/assert" @@ -374,5 +373,5 @@ func TestPluralApplyChangesDelete(t *testing.T) { } func validateEndpoints(t *testing.T, endpoints []*endpoint.Endpoint, expected []*endpoint.Endpoint) { - assert.True(t, testutils.SameEndpoints(endpoints, expected), "expected and actual endpoints don't match. %s:%s", endpoints, expected) + assert.True(t, endpoint.SameEndpoints(endpoints, expected), "expected and actual endpoints don't match. %s:%s", endpoints, expected) } diff --git a/registry/aws_sd_registry_test.go b/registry/aws_sd_registry_test.go index 94054ce513..3c34655288 100644 --- a/registry/aws_sd_registry_test.go +++ b/registry/aws_sd_registry_test.go @@ -24,7 +24,6 @@ import ( "github.com/stretchr/testify/require" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider" ) @@ -105,7 +104,7 @@ func TestAWSSDRegistryTest_Records(t *testing.T) { r, _ := NewAWSSDRegistry(p, "owner") records, _ := r.Records(context.Background()) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func TestAWSSDRegistry_Records_ApplyChanges(t *testing.T) { @@ -150,7 +149,7 @@ func TestAWSSDRegistry_Records_ApplyChanges(t *testing.T) { "UpdateOld": got.UpdateOld, "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) }) r, err := NewAWSSDRegistry(p, "owner") require.NoError(t, err) diff --git a/registry/dynamodb_test.go b/registry/dynamodb_test.go index 091927fabd..f0a4969344 100644 --- a/registry/dynamodb_test.go +++ b/registry/dynamodb_test.go @@ -31,7 +31,6 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider" "sigs.k8s.io/external-dns/provider/inmemory" @@ -252,7 +251,7 @@ func TestDynamoDBRegistryRecords(t *testing.T) { records, err := r.Records(ctx) require.NoError(t, err) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func TestDynamoDBRegistryApplyChanges(t *testing.T) { @@ -1095,12 +1094,12 @@ func TestDynamoDBRegistryApplyChanges(t *testing.T) { records, err := r.Records(ctx) require.NoError(t, err) - assert.True(t, testutils.SameEndpoints(records, tc.expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, tc.expectedRecords)) r.recordsCache = nil records, err = r.Records(ctx) require.NoError(t, err) - assert.True(t, testutils.SameEndpoints(records, tc.expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, tc.expectedRecords)) if tc.expectedError == "" { assert.Empty(t, r.orphanedLabels) } diff --git a/registry/noop_test.go b/registry/noop_test.go index 8d644ece62..79bfe880cc 100644 --- a/registry/noop_test.go +++ b/registry/noop_test.go @@ -24,7 +24,6 @@ import ( "github.com/stretchr/testify/require" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/plan" "sigs.k8s.io/external-dns/provider/inmemory" ) @@ -63,7 +62,7 @@ func testNoopRecords(t *testing.T) { eps, err := r.Records(ctx) require.NoError(t, err) - assert.True(t, testutils.SameEndpoints(eps, inmemoryRecords)) + assert.True(t, endpoint.SameEndpoints(eps, inmemoryRecords)) } func testNoopApplyChanges(t *testing.T) { @@ -133,5 +132,5 @@ func testNoopApplyChanges(t *testing.T) { }, })) res, _ := p.Records(ctx) - assert.True(t, testutils.SameEndpoints(res, expectedUpdate)) + assert.True(t, endpoint.SameEndpoints(res, expectedUpdate)) } diff --git a/registry/txt_test.go b/registry/txt_test.go index 05cdd030d2..d39c92cec6 100644 --- a/registry/txt_test.go +++ b/registry/txt_test.go @@ -231,13 +231,13 @@ func testTXTRegistryRecordsPrefixed(t *testing.T) { r, _ := NewTXTRegistry(p, "txt.", "", "owner", time.Hour, "wc", []string{}, []string{}, false, nil) records, _ := r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) // Ensure prefix is case-insensitive r, _ = NewTXTRegistry(p, "TxT.", "", "owner", time.Hour, "wc", []string{}, []string{}, false, nil) records, _ = r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func testTXTRegistryRecordsSuffixed(t *testing.T) { @@ -366,13 +366,13 @@ func testTXTRegistryRecordsSuffixed(t *testing.T) { r, _ := NewTXTRegistry(p, "", "-txt", "owner", time.Hour, "", []string{}, []string{}, false, nil) records, _ := r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) // Ensure prefix is case-insensitive r, _ = NewTXTRegistry(p, "", "-TxT", "owner", time.Hour, "", []string{}, []string{}, false, nil) records, _ = r.Records(ctx) - assert.True(t, testutils.SameEndpointLabels(records, expectedRecords)) + assert.True(t, endpoint.SameEndpointLabels(records, expectedRecords)) } func testTXTRegistryRecordsNoPrefix(t *testing.T) { @@ -493,7 +493,7 @@ func testTXTRegistryRecordsNoPrefix(t *testing.T) { r, _ := NewTXTRegistry(p, "", "", "owner", time.Hour, "", []string{}, []string{}, false, nil) records, _ := r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func testTXTRegistryRecordsPrefixedTemplated(t *testing.T) { @@ -530,12 +530,12 @@ func testTXTRegistryRecordsPrefixedTemplated(t *testing.T) { r, _ := NewTXTRegistry(p, "txt-%{record_type}.", "", "owner", time.Hour, "wc", []string{}, []string{}, false, nil) records, _ := r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) r, _ = NewTXTRegistry(p, "TxT-%{record_type}.", "", "owner", time.Hour, "wc", []string{}, []string{}, false, nil) records, _ = r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func testTXTRegistryRecordsSuffixedTemplated(t *testing.T) { @@ -572,12 +572,12 @@ func testTXTRegistryRecordsSuffixedTemplated(t *testing.T) { r, _ := NewTXTRegistry(p, "", "txt%{record_type}", "owner", time.Hour, "wc", []string{}, []string{}, false, nil) records, _ := r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) r, _ = NewTXTRegistry(p, "", "TxT%{record_type}", "owner", time.Hour, "wc", []string{}, []string{}, false, nil) records, _ = r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func testTXTRegistryApplyChanges(t *testing.T) { @@ -680,7 +680,7 @@ func testTXTRegistryApplyChangesWithPrefix(t *testing.T) { "UpdateOld": got.UpdateOld, "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) assert.Nil(t, ctx.Value(provider.RecordsContextKey)) } err := r.ApplyChanges(ctx, changes) @@ -726,7 +726,7 @@ func testTXTRegistryApplyChangesWithTemplatedPrefix(t *testing.T) { "UpdateOld": got.UpdateOld, "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) assert.Nil(t, ctx.Value(provider.RecordsContextKey)) } err := r.ApplyChanges(ctx, changes) @@ -769,7 +769,7 @@ func testTXTRegistryApplyChangesWithTemplatedSuffix(t *testing.T) { "UpdateOld": got.UpdateOld, "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) assert.Nil(t, ctx.Value(provider.RecordsContextKey)) } err := r.ApplyChanges(ctx, changes) @@ -871,7 +871,7 @@ func testTXTRegistryApplyChangesWithSuffix(t *testing.T) { "UpdateOld": got.UpdateOld, "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) assert.Nil(t, ctx.Value(provider.RecordsContextKey)) } err := r.ApplyChanges(ctx, changes) @@ -947,7 +947,7 @@ func testTXTRegistryApplyChangesNoPrefix(t *testing.T) { "UpdateOld": got.UpdateOld, "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) assert.Nil(t, ctx.Value(provider.RecordsContextKey)) } err := r.ApplyChanges(ctx, changes) @@ -1061,7 +1061,7 @@ func testTXTRegistryMissingRecordsNoPrefix(t *testing.T) { r, _ := NewTXTRegistry(p, "", "", "owner", time.Hour, "wc", []string{endpoint.RecordTypeCNAME, endpoint.RecordTypeA, endpoint.RecordTypeNS}, []string{}, false, nil) records, _ := r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func testTXTRegistryMissingRecordsWithPrefix(t *testing.T) { @@ -1171,7 +1171,7 @@ func testTXTRegistryMissingRecordsWithPrefix(t *testing.T) { r, _ := NewTXTRegistry(p, "txt.", "", "owner", time.Hour, "wc", []string{endpoint.RecordTypeCNAME, endpoint.RecordTypeA, endpoint.RecordTypeNS, endpoint.RecordTypeTXT}, []string{}, false, nil) records, _ := r.Records(ctx) - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) } func TestCacheMethods(t *testing.T) { @@ -1507,7 +1507,7 @@ func TestNewTXTScheme(t *testing.T) { "UpdateOld": got.UpdateOld, "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) assert.Nil(t, ctx.Value(provider.RecordsContextKey)) } err := r.ApplyChanges(ctx, changes) @@ -1603,7 +1603,7 @@ func TestTXTRegistryApplyChangesEncrypt(t *testing.T) { mGot := map[string][]*endpoint.Endpoint{ "Delete": got.Delete, } - assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) + assert.True(t, endpoint.SamePlanChanges(mGot, mExpected)) assert.Nil(t, ctx.Value(provider.RecordsContextKey)) } err := r.ApplyChanges(ctx, changes) @@ -1663,7 +1663,7 @@ func TestMultiClusterDifferentRecordTypeOwnership(t *testing.T) { "UpdateOld": {}, "Delete": {}, } - testutils.SamePlanChanges(got, expected) + endpoint.SamePlanChanges(got, expected) } err := r.ApplyChanges(ctx, changes.Changes) @@ -1804,7 +1804,7 @@ func TestTXTRegistryRecordsWithEmptyTargets(t *testing.T) { }, } - assert.True(t, testutils.SameEndpoints(records, expectedRecords)) + assert.True(t, endpoint.SameEndpoints(records, expectedRecords)) testutils.TestHelperLogContains("TXT record has no targets empty-targets.test-zone.example.org", hook, t) } @@ -1982,7 +1982,7 @@ func TestTXTRegistryRecreatesMissingRecords(t *testing.T) { assert.Empty(t, changes.Create, "ApplyChanges should not be called multiple times with new changes") } else { assert.True(t, - testutils.SameEndpoints(changes.Create, expectedCreate), + endpoint.SameEndpoints(changes.Create, expectedCreate), "Expected create changes: %v, but got: %v", expectedCreate, changes.Create, ) } @@ -2018,7 +2018,7 @@ func TestTXTRegistryRecreatesMissingRecords(t *testing.T) { // Then: Verify that the missing records are recreated or the existing records are not modified records, err = p.Records(ctx) assert.NoError(t, err) - assert.True(t, testutils.SameEndpoints(records, expectedRecords), + assert.True(t, endpoint.SameEndpoints(records, expectedRecords), "Expected records after reconciliation loop #%d: %v, but got: %v", i, expectedRecords, records, ) diff --git a/source/service_test.go b/source/service_test.go index 26e4320f6c..3b7462d3a6 100644 --- a/source/service_test.go +++ b/source/service_test.go @@ -437,8 +437,8 @@ func testServiceSourceEndpoints(t *testing.T) { serviceTypesFilter: []string{}, resolveLoadBalancerHostname: true, expected: []*endpoint.Endpoint{ - {DNSName: "foo.example.org", RecordType: endpoint.RecordTypeA, Targets: testutils.NewTargetsFromAddr(exampleDotComIP4)}, - {DNSName: "foo.example.org", RecordType: endpoint.RecordTypeAAAA, Targets: testutils.NewTargetsFromAddr(exampleDotComIP6)}, + {DNSName: "foo.example.org", RecordType: endpoint.RecordTypeA, Targets: endpoint.NewTargetsFromAddr(exampleDotComIP4)}, + {DNSName: "foo.example.org", RecordType: endpoint.RecordTypeAAAA, Targets: endpoint.NewTargetsFromAddr(exampleDotComIP6)}, }, }, { diff --git a/internal/testutils/mock_source.go b/source/test_utils.go similarity index 98% rename from internal/testutils/mock_source.go rename to source/test_utils.go index 99644cb086..b3c7ac3130 100644 --- a/internal/testutils/mock_source.go +++ b/source/test_utils.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package testutils +package source import ( "context" diff --git a/source/wrappers/dedupsource_test.go b/source/wrappers/dedupsource_test.go index 3f9ac58bad..08673194cd 100644 --- a/source/wrappers/dedupsource_test.go +++ b/source/wrappers/dedupsource_test.go @@ -21,7 +21,6 @@ import ( "testing" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/source" ) @@ -125,7 +124,7 @@ func testDedupEndpoints(t *testing.T) { }, } { t.Run(tc.title, func(t *testing.T) { - mockSource := new(testutils.MockSource) + mockSource := new(source.MockSource) mockSource.On("Endpoints").Return(tc.endpoints, nil) // Create our object under test and get the endpoints. @@ -159,7 +158,7 @@ func TestDedupSource_AddEventHandler(t *testing.T) { for _, tt := range tests { t.Run(tt.title, func(t *testing.T) { - mockSource := testutils.NewMockSource() + mockSource := source.NewMockSource() src := NewDedupSource(mockSource) src.AddEventHandler(t.Context(), func() {}) diff --git a/source/wrappers/multisource_test.go b/source/wrappers/multisource_test.go index ea45093f8d..ac8dd6dd81 100644 --- a/source/wrappers/multisource_test.go +++ b/source/wrappers/multisource_test.go @@ -26,7 +26,6 @@ import ( "sigs.k8s.io/external-dns/source" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" ) func TestMultiSource(t *testing.T) { @@ -83,17 +82,17 @@ func testMultiSourceEndpoints(t *testing.T) { // Populate the nested mock sources. for _, endpoints := range tc.nestedEndpoints { - src := new(testutils.MockSource) + src := new(source.MockSource) src.On("Endpoints").Return(endpoints, nil) sources = append(sources, src) } // Create our object under test and get the endpoints. - source := NewMultiSource(sources, nil, false) + source_ := NewMultiSource(sources, nil, false) // Get endpoints from the source. - endpoints, err := source.Endpoints(context.Background()) + endpoints, err := source_.Endpoints(context.Background()) require.NoError(t, err) // Validate returned endpoints against desired endpoints. @@ -101,7 +100,7 @@ func testMultiSourceEndpoints(t *testing.T) { // Validate that the nested sources were called. for _, src := range sources { - src.(*testutils.MockSource).AssertExpectations(t) + src.(*source.MockSource).AssertExpectations(t) } }) } @@ -113,7 +112,7 @@ func testMultiSourceEndpointsWithError(t *testing.T) { errSomeError := errors.New("some error") // Create a mocked source returning that error. - src := new(testutils.MockSource) + src := new(source.MockSource) src.On("Endpoints").Return(nil, errSomeError) // Create our object under test and get the endpoints. @@ -152,7 +151,7 @@ func testMultiSourceEndpointsDefaultTargets(t *testing.T) { {DNSName: "bar", Targets: defaultTargetsCName, RecordType: "CNAME", Labels: labels}, } - src := new(testutils.MockSource) + src := new(source.MockSource) src.On("Endpoints").Return(sourceEndpoints, nil) // Test with forceDefaultTargets=false (default behavior) @@ -182,7 +181,7 @@ func testMultiSourceEndpointsDefaultTargets(t *testing.T) { {DNSName: "bar", Targets: endpoint.Targets{"8.8.4.4"}, Labels: labels}, } - src := new(testutils.MockSource) + src := new(source.MockSource) src.On("Endpoints").Return(sourceEndpoints, nil) // Test with forceDefaultTargets=false (default behavior) @@ -220,7 +219,7 @@ func testMultiSourceEndpointsDefaultTargets(t *testing.T) { {DNSName: "bar", Targets: defaultTargetsCName, RecordType: "CNAME", Labels: labels}, } - src := new(testutils.MockSource) + src := new(source.MockSource) src.On("Endpoints").Return(sourceEndpoints, nil) // Test with forceDefaultTargets=true (legacy behavior) @@ -255,7 +254,7 @@ func testMultiSourceEndpointsDefaultTargets(t *testing.T) { {DNSName: "empty-target-test", Targets: defaultTargetsCName, RecordType: "CNAME", Labels: labels}, } - src := new(testutils.MockSource) + src := new(source.MockSource) src.On("Endpoints").Return(sourceEndpoints, nil) // Test with forceDefaultTargets=true @@ -284,9 +283,9 @@ func TestMultiSource_AddEventHandler(t *testing.T) { { title: "should add event handler when sources not empty", sources: []source.Source{ - testutils.NewMockSource(), - testutils.NewMockSource(), - testutils.NewMockSource(), + source.NewMockSource(), + source.NewMockSource(), + source.NewMockSource(), }, times: 3, }, @@ -300,7 +299,7 @@ func TestMultiSource_AddEventHandler(t *testing.T) { count := 0 for _, mockSource := range tt.sources { - mSource := mockSource.(*testutils.MockSource) + mSource := mockSource.(*source.MockSource) mSource.AssertNumberOfCalls(t, "AddEventHandler", 1) count += 1 } diff --git a/source/wrappers/nat64source_test.go b/source/wrappers/nat64source_test.go index 2401d3f366..dfb26b549c 100644 --- a/source/wrappers/nat64source_test.go +++ b/source/wrappers/nat64source_test.go @@ -21,7 +21,6 @@ import ( "testing" "sigs.k8s.io/external-dns/endpoint" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/source" ) @@ -70,7 +69,7 @@ func testNat64Source(t *testing.T) { }, } { t.Run(tc.title, func(t *testing.T) { - mockSource := new(testutils.MockSource) + mockSource := new(source.MockSource) mockSource.On("Endpoints").Return(tc.endpoints, nil) // Create our object under test and get the endpoints. @@ -110,7 +109,7 @@ func TestNat64Source_AddEventHandler(t *testing.T) { for _, tt := range tests { t.Run(tt.title, func(t *testing.T) { - mockSource := testutils.NewMockSource() + mockSource := source.NewMockSource() src := NewNAT64Source(mockSource, tt.input) src.AddEventHandler(t.Context(), func() {}) diff --git a/source/wrappers/targetfiltersource_test.go b/source/wrappers/targetfiltersource_test.go index 7298c1e0ac..fe3a8d8b47 100644 --- a/source/wrappers/targetfiltersource_test.go +++ b/source/wrappers/targetfiltersource_test.go @@ -22,7 +22,6 @@ import ( "github.com/stretchr/testify/require" "golang.org/x/net/context" - "sigs.k8s.io/external-dns/internal/testutils" "sigs.k8s.io/external-dns/source" "sigs.k8s.io/external-dns/endpoint" @@ -56,7 +55,7 @@ func TestEchoSourceReturnGivenSources(t *testing.T) { RecordTTL: endpoint.TTL(300), Labels: endpoint.Labels{}, }} - e := testutils.NewMockSource(startEndpoints...) + e := source.NewMockSource(startEndpoints...) endpoints, err := e.Endpoints(context.Background()) if err != nil { @@ -124,7 +123,7 @@ func TestTargetFilterSourceEndpoints(t *testing.T) { t.Run(tt.title, func(t *testing.T) { t.Parallel() - echo := testutils.NewMockSource(tt.endpoints...) + echo := source.NewMockSource(tt.endpoints...) src := NewTargetFilterSource(echo, tt.filters) endpoints, err := src.Endpoints(context.Background()) @@ -206,7 +205,7 @@ func TestTargetFilterConcreteTargetFilter(t *testing.T) { } for _, tt := range tests { t.Run(tt.title, func(t *testing.T) { - echo := testutils.NewMockSource(tt.endpoints...) + echo := source.NewMockSource(tt.endpoints...) src := NewTargetFilterSource(echo, tt.filters) endpoints, err := src.Endpoints(context.Background()) @@ -237,7 +236,7 @@ func TestTargetFilterSource_AddEventHandler(t *testing.T) { for _, tt := range tests { t.Run(tt.title, func(t *testing.T) { - m := testutils.NewMockSource() + m := source.NewMockSource() src := NewTargetFilterSource(m, tt.filters) src.AddEventHandler(t.Context(), func() {})