mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2026-04-03 07:01:50 +02:00
* refactore(source): move EndpointsForHostname to endpoint package Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com> * refactore(source): move EndpointsForHostname to endpoint package Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com> * refactore(source): move EndpointsForHostname to endpoint package Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com> --------- Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
186 lines
5.1 KiB
Go
186 lines
5.1 KiB
Go
/*
|
|
Copyright 2026 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 endpoint
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
type mockObjectMetaAccessor struct {
|
|
namespace string
|
|
name string
|
|
}
|
|
|
|
func (m *mockObjectMetaAccessor) GetObjectMeta() metav1.Object {
|
|
return &metav1.ObjectMeta{
|
|
Namespace: m.namespace,
|
|
Name: m.name,
|
|
}
|
|
}
|
|
|
|
func TestHasEmptyEndpoints(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
endpoints []*Endpoint
|
|
rType string
|
|
entity metav1.ObjectMetaAccessor
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "nil endpoints returns true",
|
|
endpoints: nil,
|
|
rType: "Service",
|
|
entity: &mockObjectMetaAccessor{namespace: "default", name: "my-service"},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "empty slice returns true",
|
|
endpoints: []*Endpoint{},
|
|
rType: "Ingress",
|
|
entity: &mockObjectMetaAccessor{namespace: "kube-system", name: "my-ingress"},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "single endpoint returns false",
|
|
endpoints: []*Endpoint{
|
|
NewEndpoint("example.org", "A", "1.2.3.4"),
|
|
},
|
|
rType: "Service",
|
|
entity: &mockObjectMetaAccessor{namespace: "default", name: "my-service"},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "multiple endpoints returns false",
|
|
endpoints: []*Endpoint{
|
|
NewEndpoint("example.org", "A", "1.2.3.4"),
|
|
NewEndpoint("test.example.org", "CNAME", "example.org"),
|
|
},
|
|
rType: "Ingress",
|
|
entity: &mockObjectMetaAccessor{namespace: "production", name: "frontend"},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
result := HasNoEmptyEndpoints(tc.endpoints, tc.rType, tc.entity)
|
|
assert.Equal(t, tc.expected, result)
|
|
// TODO: Add log capture and verification
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEndpointsForHostname(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
hostname string
|
|
targets Targets
|
|
ttl TTL
|
|
providerSpecific ProviderSpecific
|
|
setIdentifier string
|
|
resource string
|
|
expected []*Endpoint
|
|
}{
|
|
{
|
|
name: "A record targets",
|
|
hostname: "example.com",
|
|
targets: Targets{"192.0.2.1", "192.0.2.2"},
|
|
ttl: TTL(300),
|
|
providerSpecific: ProviderSpecific{
|
|
{Name: "provider", Value: "value"},
|
|
},
|
|
setIdentifier: "identifier",
|
|
resource: "resource",
|
|
expected: []*Endpoint{
|
|
{
|
|
DNSName: "example.com",
|
|
Targets: Targets{"192.0.2.1", "192.0.2.2"},
|
|
RecordType: RecordTypeA,
|
|
RecordTTL: TTL(300),
|
|
ProviderSpecific: ProviderSpecific{{Name: "provider", Value: "value"}},
|
|
SetIdentifier: "identifier",
|
|
Labels: map[string]string{ResourceLabelKey: "resource"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "AAAA record targets",
|
|
hostname: "example.com",
|
|
targets: Targets{"2001:db8::1", "2001:db8::2"},
|
|
ttl: TTL(300),
|
|
providerSpecific: ProviderSpecific{
|
|
{Name: "provider", Value: "value"},
|
|
},
|
|
setIdentifier: "identifier",
|
|
resource: "resource",
|
|
expected: []*Endpoint{
|
|
{
|
|
DNSName: "example.com",
|
|
Targets: Targets{"2001:db8::1", "2001:db8::2"},
|
|
RecordType: RecordTypeAAAA,
|
|
RecordTTL: TTL(300),
|
|
ProviderSpecific: ProviderSpecific{{Name: "provider", Value: "value"}},
|
|
SetIdentifier: "identifier",
|
|
Labels: map[string]string{ResourceLabelKey: "resource"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "CNAME record targets",
|
|
hostname: "example.com",
|
|
targets: Targets{"cname.example.com"},
|
|
ttl: TTL(300),
|
|
providerSpecific: ProviderSpecific{
|
|
{Name: "provider", Value: "value"},
|
|
},
|
|
setIdentifier: "identifier",
|
|
resource: "resource",
|
|
expected: []*Endpoint{
|
|
{
|
|
DNSName: "example.com",
|
|
Targets: Targets{"cname.example.com"},
|
|
RecordType: RecordTypeCNAME,
|
|
RecordTTL: TTL(300),
|
|
ProviderSpecific: ProviderSpecific{{Name: "provider", Value: "value"}},
|
|
SetIdentifier: "identifier",
|
|
Labels: map[string]string{ResourceLabelKey: "resource"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "No targets",
|
|
hostname: "example.com",
|
|
targets: Targets{},
|
|
ttl: TTL(300),
|
|
providerSpecific: ProviderSpecific{},
|
|
setIdentifier: "",
|
|
resource: "",
|
|
expected: []*Endpoint(nil),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := EndpointsForHostname(tt.hostname, tt.targets, tt.ttl, tt.providerSpecific, tt.setIdentifier, tt.resource)
|
|
assert.Equal(t, tt.expected, result)
|
|
})
|
|
}
|
|
}
|