sigs.k8s.io/external-dns@v0.14.1/registry/aws_sd_registry_test.go (about) 1 /* 2 Copyright 2018 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" 30 ) 31 32 type inMemoryProvider struct { 33 provider.BaseProvider 34 endpoints []*endpoint.Endpoint 35 onApplyChanges func(changes *plan.Changes) 36 } 37 38 func (p *inMemoryProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) { 39 return p.endpoints, nil 40 } 41 42 func (p *inMemoryProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error { 43 p.onApplyChanges(changes) 44 return nil 45 } 46 47 func newInMemoryProvider(endpoints []*endpoint.Endpoint, onApplyChanges func(changes *plan.Changes)) *inMemoryProvider { 48 return &inMemoryProvider{ 49 endpoints: endpoints, 50 onApplyChanges: onApplyChanges, 51 } 52 } 53 54 func TestAWSSDRegistry_NewAWSSDRegistry(t *testing.T) { 55 p := newInMemoryProvider(nil, nil) 56 _, err := NewAWSSDRegistry(p, "") 57 require.Error(t, err) 58 59 _, err = NewAWSSDRegistry(p, "owner") 60 require.NoError(t, err) 61 } 62 63 func TestAWSSDRegistryTest_Records(t *testing.T) { 64 p := newInMemoryProvider([]*endpoint.Endpoint{ 65 newEndpointWithOwnerAndDescription("foo1.test-zone.example.org", "1.2.3.4", endpoint.RecordTypeA, "", ""), 66 newEndpointWithOwnerAndDescription("foo2.test-zone.example.org", "1.2.3.4", endpoint.RecordTypeA, "owner", "\"heritage=external-dns,external-dns/owner=owner\""), 67 newEndpointWithOwnerAndDescription("foo3.test-zone.example.org", "my-domain.com", endpoint.RecordTypeCNAME, "", ""), 68 newEndpointWithOwnerAndDescription("foo4.test-zone.example.org", "my-domain.com", endpoint.RecordTypeCNAME, "owner", "\"heritage=external-dns,external-dns/owner=owner\""), 69 }, nil) 70 expectedRecords := []*endpoint.Endpoint{ 71 { 72 DNSName: "foo1.test-zone.example.org", 73 Targets: endpoint.Targets{"1.2.3.4"}, 74 RecordType: endpoint.RecordTypeA, 75 Labels: map[string]string{ 76 endpoint.OwnerLabelKey: "", 77 }, 78 }, 79 { 80 DNSName: "foo2.test-zone.example.org", 81 Targets: endpoint.Targets{"1.2.3.4"}, 82 RecordType: endpoint.RecordTypeA, 83 Labels: map[string]string{ 84 endpoint.OwnerLabelKey: "owner", 85 }, 86 }, 87 { 88 DNSName: "foo3.test-zone.example.org", 89 Targets: endpoint.Targets{"my-domain.com"}, 90 RecordType: endpoint.RecordTypeCNAME, 91 Labels: map[string]string{ 92 endpoint.OwnerLabelKey: "", 93 }, 94 }, 95 { 96 DNSName: "foo4.test-zone.example.org", 97 Targets: endpoint.Targets{"my-domain.com"}, 98 RecordType: endpoint.RecordTypeCNAME, 99 Labels: map[string]string{ 100 endpoint.OwnerLabelKey: "owner", 101 }, 102 }, 103 } 104 105 r, _ := NewAWSSDRegistry(p, "owner") 106 records, _ := r.Records(context.Background()) 107 108 assert.True(t, testutils.SameEndpoints(records, expectedRecords)) 109 } 110 111 func TestAWSSDRegistry_Records_ApplyChanges(t *testing.T) { 112 changes := &plan.Changes{ 113 Create: []*endpoint.Endpoint{ 114 newEndpointWithOwner("new-record-1.test-zone.example.org", "new-loadbalancer-1.lb.com", endpoint.RecordTypeCNAME, "owner"), 115 }, 116 Delete: []*endpoint.Endpoint{ 117 newEndpointWithOwner("foobar.test-zone.example.org", "1.2.3.4", endpoint.RecordTypeA, "owner"), 118 }, 119 UpdateNew: []*endpoint.Endpoint{ 120 newEndpointWithOwner("tar.test-zone.example.org", "new-tar.loadbalancer.com", endpoint.RecordTypeCNAME, "owner"), 121 }, 122 UpdateOld: []*endpoint.Endpoint{ 123 newEndpointWithOwner("tar.test-zone.example.org", "tar.loadbalancer.com", endpoint.RecordTypeCNAME, "owner"), 124 }, 125 } 126 expected := &plan.Changes{ 127 Create: []*endpoint.Endpoint{ 128 newEndpointWithOwnerAndDescription("new-record-1.test-zone.example.org", "new-loadbalancer-1.lb.com", endpoint.RecordTypeCNAME, "owner", "\"heritage=external-dns,external-dns/owner=owner\""), 129 }, 130 Delete: []*endpoint.Endpoint{ 131 newEndpointWithOwnerAndDescription("foobar.test-zone.example.org", "1.2.3.4", endpoint.RecordTypeA, "owner", "\"heritage=external-dns,external-dns/owner=owner\""), 132 }, 133 UpdateNew: []*endpoint.Endpoint{ 134 newEndpointWithOwnerAndDescription("tar.test-zone.example.org", "new-tar.loadbalancer.com", endpoint.RecordTypeCNAME, "owner", "\"heritage=external-dns,external-dns/owner=owner\""), 135 }, 136 UpdateOld: []*endpoint.Endpoint{ 137 newEndpointWithOwnerAndDescription("tar.test-zone.example.org", "tar.loadbalancer.com", endpoint.RecordTypeCNAME, "owner", "\"heritage=external-dns,external-dns/owner=owner\""), 138 }, 139 } 140 p := newInMemoryProvider(nil, func(got *plan.Changes) { 141 mExpected := map[string][]*endpoint.Endpoint{ 142 "Create": expected.Create, 143 "UpdateNew": expected.UpdateNew, 144 "UpdateOld": expected.UpdateOld, 145 "Delete": expected.Delete, 146 } 147 mGot := map[string][]*endpoint.Endpoint{ 148 "Create": got.Create, 149 "UpdateNew": got.UpdateNew, 150 "UpdateOld": got.UpdateOld, 151 "Delete": got.Delete, 152 } 153 assert.True(t, testutils.SamePlanChanges(mGot, mExpected)) 154 }) 155 r, err := NewAWSSDRegistry(p, "owner") 156 require.NoError(t, err) 157 158 err = r.ApplyChanges(context.Background(), changes) 159 require.NoError(t, err) 160 } 161 162 func newEndpointWithOwnerAndDescription(dnsName, target, recordType, ownerID string, description string) *endpoint.Endpoint { 163 e := endpoint.NewEndpoint(dnsName, recordType, target) 164 e.Labels[endpoint.OwnerLabelKey] = ownerID 165 e.Labels[endpoint.AWSSDDescriptionLabel] = description 166 return e 167 }