sigs.k8s.io/external-dns@v0.14.1/source/dedupsource_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  	"context"
    21  	"testing"
    22  
    23  	"sigs.k8s.io/external-dns/endpoint"
    24  	"sigs.k8s.io/external-dns/internal/testutils"
    25  )
    26  
    27  // Validates that dedupSource is a Source
    28  var _ Source = &dedupSource{}
    29  
    30  func TestDedup(t *testing.T) {
    31  	t.Run("Endpoints", testDedupEndpoints)
    32  }
    33  
    34  // testDedupEndpoints tests that duplicates from the wrapped source are removed.
    35  func testDedupEndpoints(t *testing.T) {
    36  	for _, tc := range []struct {
    37  		title     string
    38  		endpoints []*endpoint.Endpoint
    39  		expected  []*endpoint.Endpoint
    40  	}{
    41  		{
    42  			"one endpoint returns one endpoint",
    43  			[]*endpoint.Endpoint{
    44  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    45  			},
    46  			[]*endpoint.Endpoint{
    47  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    48  			},
    49  		},
    50  		{
    51  			"two different endpoints return two endpoints",
    52  			[]*endpoint.Endpoint{
    53  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    54  				{DNSName: "bar.example.org", Targets: endpoint.Targets{"4.5.6.7"}},
    55  			},
    56  			[]*endpoint.Endpoint{
    57  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    58  				{DNSName: "bar.example.org", Targets: endpoint.Targets{"4.5.6.7"}},
    59  			},
    60  		},
    61  		{
    62  			"two endpoints with same dnsname and different targets return two endpoints",
    63  			[]*endpoint.Endpoint{
    64  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    65  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"4.5.6.7"}},
    66  			},
    67  			[]*endpoint.Endpoint{
    68  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    69  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"4.5.6.7"}},
    70  			},
    71  		},
    72  		{
    73  			"two endpoints with different dnsname and same target return two endpoints",
    74  			[]*endpoint.Endpoint{
    75  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    76  				{DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    77  			},
    78  			[]*endpoint.Endpoint{
    79  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    80  				{DNSName: "bar.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    81  			},
    82  		},
    83  		{
    84  			"two endpoints with same dnsname and same target return one endpoint",
    85  			[]*endpoint.Endpoint{
    86  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    87  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    88  			},
    89  			[]*endpoint.Endpoint{
    90  				{DNSName: "foo.example.org", Targets: endpoint.Targets{"1.2.3.4"}},
    91  			},
    92  		},
    93  	} {
    94  		t.Run(tc.title, func(t *testing.T) {
    95  			mockSource := new(testutils.MockSource)
    96  			mockSource.On("Endpoints").Return(tc.endpoints, nil)
    97  
    98  			// Create our object under test and get the endpoints.
    99  			source := NewDedupSource(mockSource)
   100  
   101  			endpoints, err := source.Endpoints(context.Background())
   102  			if err != nil {
   103  				t.Fatal(err)
   104  			}
   105  
   106  			// Validate returned endpoints against desired endpoints.
   107  			validateEndpoints(t, endpoints, tc.expected)
   108  
   109  			// Validate that the mock source was called.
   110  			mockSource.AssertExpectations(t)
   111  		})
   112  	}
   113  }