Skip to content

Commit

Permalink
Run tests via a Mock targetNetFilter instead of the actual one
Browse files Browse the repository at this point in the history
  • Loading branch information
BadLiveware committed Jun 29, 2023
1 parent d7d5a2d commit 05bb165
Showing 1 changed file with 26 additions and 56 deletions.
82 changes: 26 additions & 56 deletions source/targetfiltersource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ import (
"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.
type echoSource struct {
endpoints []*endpoint.Endpoint
Expand Down Expand Up @@ -66,7 +86,6 @@ func TestEchoSourceReturnGivenSources(t *testing.T) {
}
}


func TestTargetFilterSource(t *testing.T) {
t.Parallel()

Expand All @@ -89,54 +108,8 @@ func TestTargetFilterSourceEndpoints(t *testing.T) {
expected []*endpoint.Endpoint
}{
{
title: "no filters",
filters: endpoint.NewTargetNetFilter(nil),
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"}),
title: "filter exclusion all",
filters: NewMockTargetNetFilter([]string{}),
endpoints: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo", "A", "1.2.3.4"),
endpoint.NewEndpoint("foo", "A", "1.2.3.5"),
Expand All @@ -146,19 +119,16 @@ func TestTargetFilterSourceEndpoints(t *testing.T) {
expected: []*endpoint.Endpoint{},
},
{
title: "filter exclude internal net",
filters: endpoint.NewTargetNetFilterWithExclusions(
[]string{""},
[]string{"10.0.0.0/8"}),
title: "filter exclude internal net",
filters: NewMockTargetNetFilter([]string{"8.8.8.8"}),
endpoints: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo", "A", "10.0.0.1"),
endpoint.NewEndpoint("foo", "A", "8.8.8.8")},
expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "8.8.8.8")},
},
{
title: "filter only internal",
filters: endpoint.NewTargetNetFilterWithExclusions(
[]string{"10.0.0.0/8"}, []string{}),
title: "filter only internal",
filters: NewMockTargetNetFilter([]string{"10.0.0.1"}),
endpoints: []*endpoint.Endpoint{
endpoint.NewEndpoint("foo", "A", "10.0.0.1"),
endpoint.NewEndpoint("foo", "A", "8.8.8.8")},
Expand Down

0 comments on commit 05bb165

Please sign in to comment.