sigs.k8s.io/external-dns@v0.14.1/source/targetfiltersource_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package source 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 "golang.org/x/net/context" 24 25 "sigs.k8s.io/external-dns/endpoint" 26 ) 27 28 type mockTargetNetFilter struct { 29 targets map[string]bool 30 } 31 32 func NewMockTargetNetFilter(targets []string) endpoint.TargetFilterInterface { 33 targetMap := make(map[string]bool) 34 for _, target := range targets { 35 targetMap[target] = true 36 } 37 return &mockTargetNetFilter{targets: targetMap} 38 } 39 40 func (m *mockTargetNetFilter) Match(target string) bool { 41 return m.targets[target] 42 } 43 44 // echoSource is a Source that returns the endpoints passed in on creation. 45 type echoSource struct { 46 endpoints []*endpoint.Endpoint 47 } 48 49 func (e *echoSource) AddEventHandler(ctx context.Context, handler func()) { 50 } 51 52 // Endpoints returns all of the endpoints passed in on creation 53 func (e *echoSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) { 54 return e.endpoints, nil 55 } 56 57 // NewEchoSource creates a new echoSource. 58 func NewEchoSource(endpoints []*endpoint.Endpoint) Source { 59 return &echoSource{endpoints: endpoints} 60 } 61 62 func TestEchoSourceReturnGivenSources(t *testing.T) { 63 startEndpoints := []*endpoint.Endpoint{{ 64 DNSName: "foo.bar.com", 65 RecordType: "A", 66 Targets: endpoint.Targets{"1.2.3.4"}, 67 RecordTTL: endpoint.TTL(300), 68 Labels: endpoint.Labels{}, 69 }} 70 e := NewEchoSource(startEndpoints) 71 72 endpoints, err := e.Endpoints(context.Background()) 73 if err != nil { 74 t.Errorf("Expected no error but got %s", err.Error()) 75 } 76 77 for i, endpoint := range endpoints { 78 if endpoint != startEndpoints[i] { 79 t.Errorf("Expected %s but got %s", startEndpoints[i], endpoint) 80 } 81 } 82 } 83 84 func TestTargetFilterSource(t *testing.T) { 85 t.Parallel() 86 87 t.Run("Interface", TestTargetFilterSourceImplementsSource) 88 t.Run("Endpoints", TestTargetFilterSourceEndpoints) 89 } 90 91 // TestTargetFilterSourceImplementsSource tests that targetFilterSource is a valid Source. 92 func TestTargetFilterSourceImplementsSource(t *testing.T) { 93 var _ Source = &targetFilterSource{} 94 } 95 96 func TestTargetFilterSourceEndpoints(t *testing.T) { 97 t.Parallel() 98 99 tests := []struct { 100 title string 101 filters endpoint.TargetFilterInterface 102 endpoints []*endpoint.Endpoint 103 expected []*endpoint.Endpoint 104 }{ 105 { 106 title: "filter exclusion all", 107 filters: NewMockTargetNetFilter([]string{}), 108 endpoints: []*endpoint.Endpoint{ 109 endpoint.NewEndpoint("foo", "A", "1.2.3.4"), 110 endpoint.NewEndpoint("foo", "A", "1.2.3.5"), 111 endpoint.NewEndpoint("foo", "A", "1.2.3.6"), 112 endpoint.NewEndpoint("foo", "A", "1.3.4.5"), 113 endpoint.NewEndpoint("foo", "A", "1.4.4.5")}, 114 expected: []*endpoint.Endpoint{}, 115 }, 116 { 117 title: "filter exclude internal net", 118 filters: NewMockTargetNetFilter([]string{"8.8.8.8"}), 119 endpoints: []*endpoint.Endpoint{ 120 endpoint.NewEndpoint("foo", "A", "10.0.0.1"), 121 endpoint.NewEndpoint("foo", "A", "8.8.8.8")}, 122 expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "8.8.8.8")}, 123 }, 124 { 125 title: "filter only internal", 126 filters: NewMockTargetNetFilter([]string{"10.0.0.1"}), 127 endpoints: []*endpoint.Endpoint{ 128 endpoint.NewEndpoint("foo", "A", "10.0.0.1"), 129 endpoint.NewEndpoint("foo", "A", "8.8.8.8")}, 130 expected: []*endpoint.Endpoint{endpoint.NewEndpoint("foo", "A", "10.0.0.1")}, 131 }, 132 } 133 for _, tt := range tests { 134 tt := tt 135 t.Run(tt.title, func(t *testing.T) { 136 t.Parallel() 137 138 echo := NewEchoSource(tt.endpoints) 139 src := NewTargetFilterSource(echo, tt.filters) 140 141 endpoints, err := src.Endpoints(context.Background()) 142 require.NoError(t, err, "failed to get Endpoints") 143 validateEndpoints(t, endpoints, tt.expected) 144 }) 145 } 146 }