sigs.k8s.io/external-dns@v0.14.1/registry/noop_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 registry
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"sigs.k8s.io/external-dns/endpoint"
    27  	"sigs.k8s.io/external-dns/internal/testutils"
    28  	"sigs.k8s.io/external-dns/plan"
    29  	"sigs.k8s.io/external-dns/provider/inmemory"
    30  )
    31  
    32  var _ Registry = &NoopRegistry{}
    33  
    34  func TestNoopRegistry(t *testing.T) {
    35  	t.Run("NewNoopRegistry", testNoopInit)
    36  	t.Run("Records", testNoopRecords)
    37  	t.Run("ApplyChanges", testNoopApplyChanges)
    38  }
    39  
    40  func testNoopInit(t *testing.T) {
    41  	p := inmemory.NewInMemoryProvider()
    42  	r, err := NewNoopRegistry(p)
    43  	require.NoError(t, err)
    44  	assert.Equal(t, p, r.provider)
    45  }
    46  
    47  func testNoopRecords(t *testing.T) {
    48  	ctx := context.Background()
    49  	p := inmemory.NewInMemoryProvider()
    50  	p.CreateZone("org")
    51  	inmemoryRecords := []*endpoint.Endpoint{
    52  		{
    53  			DNSName:    "example.org",
    54  			Targets:    endpoint.Targets{"example-lb.com"},
    55  			RecordType: endpoint.RecordTypeCNAME,
    56  		},
    57  	}
    58  	p.ApplyChanges(ctx, &plan.Changes{
    59  		Create: inmemoryRecords,
    60  	})
    61  
    62  	r, _ := NewNoopRegistry(p)
    63  
    64  	eps, err := r.Records(ctx)
    65  	require.NoError(t, err)
    66  	assert.True(t, testutils.SameEndpoints(eps, inmemoryRecords))
    67  }
    68  
    69  func testNoopApplyChanges(t *testing.T) {
    70  	// do some prep
    71  	p := inmemory.NewInMemoryProvider()
    72  	p.CreateZone("org")
    73  	inmemoryRecords := []*endpoint.Endpoint{
    74  		{
    75  			DNSName:    "example.org",
    76  			Targets:    endpoint.Targets{"old-lb.com"},
    77  			RecordType: endpoint.RecordTypeCNAME,
    78  		},
    79  	}
    80  	expectedUpdate := []*endpoint.Endpoint{
    81  		{
    82  			DNSName:    "example.org",
    83  			Targets:    endpoint.Targets{"new-example-lb.com"},
    84  			RecordType: endpoint.RecordTypeCNAME,
    85  		},
    86  		{
    87  			DNSName:    "new-record.org",
    88  			Targets:    endpoint.Targets{"new-lb.org"},
    89  			RecordType: endpoint.RecordTypeCNAME,
    90  		},
    91  	}
    92  
    93  	ctx := context.Background()
    94  	p.ApplyChanges(ctx, &plan.Changes{
    95  		Create: inmemoryRecords,
    96  	})
    97  
    98  	// wrong changes
    99  	r, _ := NewNoopRegistry(p)
   100  	err := r.ApplyChanges(ctx, &plan.Changes{
   101  		Create: []*endpoint.Endpoint{
   102  			{
   103  				DNSName:    "example.org",
   104  				Targets:    endpoint.Targets{"lb.com"},
   105  				RecordType: endpoint.RecordTypeCNAME,
   106  			},
   107  		},
   108  	})
   109  	assert.EqualError(t, err, inmemory.ErrRecordAlreadyExists.Error())
   110  
   111  	// correct changes
   112  	require.NoError(t, r.ApplyChanges(ctx, &plan.Changes{
   113  		Create: []*endpoint.Endpoint{
   114  			{
   115  				DNSName:    "new-record.org",
   116  				Targets:    endpoint.Targets{"new-lb.org"},
   117  				RecordType: endpoint.RecordTypeCNAME,
   118  			},
   119  		},
   120  		UpdateNew: []*endpoint.Endpoint{
   121  			{
   122  				DNSName:    "example.org",
   123  				Targets:    endpoint.Targets{"new-example-lb.com"},
   124  				RecordType: endpoint.RecordTypeCNAME,
   125  			},
   126  		},
   127  		UpdateOld: []*endpoint.Endpoint{
   128  			{
   129  				DNSName:    "example.org",
   130  				Targets:    endpoint.Targets{"old-lb.com"},
   131  				RecordType: endpoint.RecordTypeCNAME,
   132  			},
   133  		},
   134  	}))
   135  	res, _ := p.Records(ctx)
   136  	assert.True(t, testutils.SameEndpoints(res, expectedUpdate))
   137  }