Run tests via a Mock targetNetFilter instead of the actual one

This commit is contained in:
Fredrik Larsson 2023-05-22 16:17:36 +02:00
parent d7d5a2d404
commit 05bb165ffa

View File

@ -26,6 +26,26 @@ import (
"sigs.k8s.io/external-dns/endpoint" "sigs.k8s.io/external-dns/endpoint"
) )
type mockTargetNetFilter struct {
targets map[string]bool
}
func NewMockTargetNetFilter(targets []string) endpoint.TargetFilterInterface {
targetMap := make(map[string]bool)
for _, target := range targets {
targetMap[target] = true
}
return &mockTargetNetFilter{targets: targetMap}
}
func (m *mockTargetNetFilter) Match(target string) bool {
return m.targets[target]
}
func (m *mockTargetNetFilter) IsConfigured() bool {
return true
}
// echoSource is a Source that returns the endpoints passed in on creation. // echoSource is a Source that returns the endpoints passed in on creation.
type echoSource struct { type echoSource struct {
endpoints []*endpoint.Endpoint endpoints []*endpoint.Endpoint
@ -66,7 +86,6 @@ func TestEchoSourceReturnGivenSources(t *testing.T) {
} }
} }
func TestTargetFilterSource(t *testing.T) { func TestTargetFilterSource(t *testing.T) {
t.Parallel() t.Parallel()
@ -89,54 +108,8 @@ func TestTargetFilterSourceEndpoints(t *testing.T) {
expected []*endpoint.Endpoint expected []*endpoint.Endpoint
}{ }{
{ {
title: "no filters", title: "filter exclusion all",
filters: endpoint.NewTargetNetFilter(nil), filters: NewMockTargetNetFilter([]string{}),
endpoints: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
},
{
title: "filter matched specific",
filters: endpoint.NewTargetNetFilter([]string{"1.2.3.4"}),
endpoints: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
},
{
title: "filter matched net",
filters: endpoint.NewTargetNetFilter([]string{"1.2.3.0/24"}),
endpoints: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
},
{
title: "filter not matched specific",
filters: endpoint.NewTargetNetFilter([]string{"1.2.3.5/32"}),
endpoints: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
expected: []*endpoint.Endpoint{},
},
{
title: "filter not matched net",
filters: endpoint.NewTargetNetFilter([]string{"1.2.4.0/24"}),
endpoints: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.3.4")},
expected: []*endpoint.Endpoint{},
},
{
title: "filter not matched CNAME endpoint",
filters: endpoint.NewTargetNetFilter([]string{"1.2.4.0/24"}),
endpoints: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "CNAME", "foo.bar")},
expected: []*endpoint.Endpoint{},
},
{
title: "filter matched one of two",
filters: endpoint.NewTargetNetFilter([]string{"1.2.4.0/24"}),
endpoints: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo", "CNAME", "foo.bar"),
endpoint.NewEndpoint("foo", "A", "1.2.4.1")},
expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "1.2.4.1")},
},
{
title: "filter exclusion all",
filters: endpoint.NewTargetNetFilterWithExclusions(
[]string{""},
[]string{"1.0.0.0/8"}),
endpoints: []*endpoint.Endpoint{ endpoints: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo", "A", "1.2.3.4"), endpoint.NewEndpoint("foo", "A", "1.2.3.4"),
endpoint.NewEndpoint("foo", "A", "1.2.3.5"), endpoint.NewEndpoint("foo", "A", "1.2.3.5"),
@ -146,19 +119,16 @@ func TestTargetFilterSourceEndpoints(t *testing.T) {
expected: []*endpoint.Endpoint{}, expected: []*endpoint.Endpoint{},
}, },
{ {
title: "filter exclude internal net", title: "filter exclude internal net",
filters: endpoint.NewTargetNetFilterWithExclusions( filters: NewMockTargetNetFilter([]string{"8.8.8.8"}),
[]string{""},
[]string{"10.0.0.0/8"}),
endpoints: []*endpoint.Endpoint{ endpoints: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo", "A", "10.0.0.1"), endpoint.NewEndpoint("foo", "A", "10.0.0.1"),
endpoint.NewEndpoint("foo", "A", "8.8.8.8")}, endpoint.NewEndpoint("foo", "A", "8.8.8.8")},
expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "8.8.8.8")}, expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "8.8.8.8")},
}, },
{ {
title: "filter only internal", title: "filter only internal",
filters: endpoint.NewTargetNetFilterWithExclusions( filters: NewMockTargetNetFilter([]string{"10.0.0.1"}),
[]string{"10.0.0.0/8"}, []string{}),
endpoints: []*endpoint.Endpoint{ endpoints: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo", "A", "10.0.0.1"), endpoint.NewEndpoint("foo", "A", "10.0.0.1"),
endpoint.NewEndpoint("foo", "A", "8.8.8.8")}, endpoint.NewEndpoint("foo", "A", "8.8.8.8")},