external-dns/source/utils_test.go
ivan katliarchuk 8c7af680c0
feat(source/istio): speed up processing with indexers
Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
2025-08-01 13:48:47 +01:00

105 lines
2.3 KiB
Go

/*
Copyright 2025 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 source
import (
"testing"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/external-dns/endpoint"
)
func TestSuitableType(t *testing.T) {
tests := []struct {
name string
target string
expected string
}{
{
name: "valid IPv4 address",
target: "192.168.1.1",
expected: endpoint.RecordTypeA,
},
{
name: "valid IPv6 address",
target: "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
expected: endpoint.RecordTypeAAAA,
},
{
name: "invalid IP address, should return CNAME",
target: "example.com",
expected: endpoint.RecordTypeCNAME,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := suitableType(tt.target)
assert.Equal(t, tt.expected, result)
})
}
}
func TestParseIngress(t *testing.T) {
tests := []struct {
name string
ingress string
wantNS string
wantName string
wantError bool
}{
{
name: "valid namespace and name",
ingress: "default/test-ingress",
wantNS: "default",
wantName: "test-ingress",
wantError: false,
},
{
name: "only name provided",
ingress: "test-ingress",
wantNS: "",
wantName: "test-ingress",
wantError: false,
},
{
name: "invalid format",
ingress: "default/test/ingress",
wantNS: "",
wantName: "",
wantError: true,
},
{
name: "empty string",
ingress: "",
wantNS: "",
wantName: "",
wantError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotNS, gotName, err := ParseIngress(tt.ingress)
if tt.wantError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.wantNS, gotNS)
assert.Equal(t, tt.wantName, gotName)
})
}
}