coredns_test.go 9.3 KB
Newer Older
Stan Lagun's avatar
Stan Lagun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package provider

import (
	"strings"
	"testing"

	"github.com/kubernetes-incubator/external-dns/endpoint"
	"github.com/kubernetes-incubator/external-dns/plan"
)

type fakeETCDClient struct {
	services map[string]*Service
}

func (c fakeETCDClient) GetServices(prefix string) ([]*Service, error) {
	var result []*Service
	for key, value := range c.services {
		if strings.HasPrefix(key, prefix) {
			value.Key = key
			result = append(result, value)
		}
	}
	return result, nil
}

func (c fakeETCDClient) SaveService(service *Service) error {
	c.services[service.Key] = service
	return nil
}

func (c fakeETCDClient) DeleteService(key string) error {
	delete(c.services, key)
	return nil
}

func TestAServiceTranslation(t *testing.T) {
	expectedTarget := "1.2.3.4"
	expectedDNSName := "example.com"
	expectedRecordType := endpoint.RecordTypeA

	client := fakeETCDClient{
		map[string]*Service{
			"/skydns/com/example": {Host: expectedTarget},
		},
	}
	provider := coreDNSProvider{client: client}
	endpoints, err := provider.Records()
	if err != nil {
		t.Fatal(err)
	}
	if len(endpoints) != 1 {
		t.Fatalf("got unexpected number of endpoints: %d", len(endpoints))
	}
	if endpoints[0].DNSName != expectedDNSName {
		t.Errorf("got unexpected DNS name: %s != %s", endpoints[0].DNSName, expectedDNSName)
	}
	if endpoints[0].Target != expectedTarget {
		t.Errorf("got unexpected DNS target: %s != %s", endpoints[0].Target, expectedTarget)
	}
	if endpoints[0].RecordType != expectedRecordType {
		t.Errorf("got unexpected DNS record type: %s != %s", endpoints[0].RecordType, expectedRecordType)
	}
}

func TestCNAMEServiceTranslation(t *testing.T) {
	expectedTarget := "example.net"
	expectedDNSName := "example.com"
	expectedRecordType := endpoint.RecordTypeCNAME

	client := fakeETCDClient{
		map[string]*Service{
			"/skydns/com/example": {Host: expectedTarget},
		},
	}
	provider := coreDNSProvider{client: client}
	endpoints, err := provider.Records()
	if err != nil {
		t.Fatal(err)
	}
	if len(endpoints) != 1 {
		t.Fatalf("got unexpected number of endpoints: %d", len(endpoints))
	}
	if endpoints[0].DNSName != expectedDNSName {
		t.Errorf("got unexpected DNS name: %s != %s", endpoints[0].DNSName, expectedDNSName)
	}
	if endpoints[0].Target != expectedTarget {
		t.Errorf("got unexpected DNS target: %s != %s", endpoints[0].Target, expectedTarget)
	}
	if endpoints[0].RecordType != expectedRecordType {
		t.Errorf("got unexpected DNS record type: %s != %s", endpoints[0].RecordType, expectedRecordType)
	}
}

func TestTXTServiceTranslation(t *testing.T) {
	expectedTarget := "string"
	expectedDNSName := "example.com"
	expectedRecordType := endpoint.RecordTypeTXT

	client := fakeETCDClient{
		map[string]*Service{
			"/skydns/com/example": {Text: expectedTarget},
		},
	}
	provider := coreDNSProvider{client: client}
	endpoints, err := provider.Records()
	if err != nil {
		t.Fatal(err)
	}
	if len(endpoints) != 1 {
		t.Fatalf("got unexpected number of endpoints: %d", len(endpoints))
	}
	if endpoints[0].DNSName != expectedDNSName {
		t.Errorf("got unexpected DNS name: %s != %s", endpoints[0].DNSName, expectedDNSName)
	}
	if endpoints[0].Target != expectedTarget {
		t.Errorf("got unexpected DNS target: %s != %s", endpoints[0].Target, expectedTarget)
	}
	if endpoints[0].RecordType != expectedRecordType {
		t.Errorf("got unexpected DNS record type: %s != %s", endpoints[0].RecordType, expectedRecordType)
	}
}

func TestAWithTXTServiceTranslation(t *testing.T) {
	expectedTargets := map[string]string{
		endpoint.RecordTypeA:   "1.2.3.4",
		endpoint.RecordTypeTXT: "string",
	}
	expectedDNSName := "example.com"

	client := fakeETCDClient{
		map[string]*Service{
			"/skydns/com/example": {Host: "1.2.3.4", Text: "string"},
		},
	}
	provider := coreDNSProvider{client: client}
	endpoints, err := provider.Records()
	if err != nil {
		t.Fatal(err)
	}
	if len(endpoints) != len(expectedTargets) {
		t.Fatalf("got unexpected number of endpoints: %d", len(endpoints))
	}

	for _, ep := range endpoints {
		expectedTarget := expectedTargets[ep.RecordType]
		if expectedTarget == "" {
			t.Errorf("got unexpected DNS record type: %s", ep.RecordType)
			continue
		}
		delete(expectedTargets, ep.RecordType)

		if ep.DNSName != expectedDNSName {
			t.Errorf("got unexpected DNS name: %s != %s", ep.DNSName, expectedDNSName)
		}

		if ep.Target != expectedTarget {
			t.Errorf("got unexpected DNS target: %s != %s", ep.Target, expectedTarget)
		}
	}
}

func TestCNAMEWithTXTServiceTranslation(t *testing.T) {
	expectedTargets := map[string]string{
		endpoint.RecordTypeCNAME: "example.net",
		endpoint.RecordTypeTXT:   "string",
	}
	expectedDNSName := "example.com"

	client := fakeETCDClient{
		map[string]*Service{
			"/skydns/com/example": {Host: "example.net", Text: "string"},
		},
	}
	provider := coreDNSProvider{client: client}
	endpoints, err := provider.Records()
	if err != nil {
		t.Fatal(err)
	}
	if len(endpoints) != len(expectedTargets) {
		t.Fatalf("got unexpected number of endpoints: %d", len(endpoints))
	}

	for _, ep := range endpoints {
		expectedTarget := expectedTargets[ep.RecordType]
		if expectedTarget == "" {
			t.Errorf("got unexpected DNS record type: %s", ep.RecordType)
			continue
		}
		delete(expectedTargets, ep.RecordType)

		if ep.DNSName != expectedDNSName {
			t.Errorf("got unexpected DNS name: %s != %s", ep.DNSName, expectedDNSName)
		}

		if ep.Target != expectedTarget {
			t.Errorf("got unexpected DNS target: %s != %s", ep.Target, expectedTarget)
		}
	}
}

func TestCoreDNSApplyChanges(t *testing.T) {
	client := fakeETCDClient{
		map[string]*Service{},
	}
	coredns := coreDNSProvider{client: client}

	changes1 := &plan.Changes{
		Create: []*endpoint.Endpoint{
			endpoint.NewEndpoint("domain1.local", "5.5.5.5", endpoint.RecordTypeA),
			endpoint.NewEndpoint("domain1.local", "string1", endpoint.RecordTypeTXT),
			endpoint.NewEndpoint("domain2.local", "site.local", endpoint.RecordTypeCNAME),
		},
	}
	coredns.ApplyChanges(changes1)

	expectedServices1 := map[string]*Service{
		"/skydns/local/domain1": {Host: "5.5.5.5", Text: "string1"},
		"/skydns/local/domain2": {Host: "site.local"},
	}
	validateServices(client.services, expectedServices1, t, 1)

	updatedEp := endpoint.NewEndpoint("domain1.local", "6.6.6.6", endpoint.RecordTypeA)
	updatedEp.Labels["originalText"] = "string1"
	changes2 := &plan.Changes{
		Create: []*endpoint.Endpoint{
			endpoint.NewEndpoint("domain3.local", "7.7.7.7", endpoint.RecordTypeA),
		},
Stan Lagun's avatar
Stan Lagun committed
244 245 246
		UpdateNew: []*endpoint.Endpoint{
			endpoint.NewEndpoint("domain1.local", "6.6.6.6", "A"),
		},
Stan Lagun's avatar
Stan Lagun committed
247
	}
Stan Lagun's avatar
Stan Lagun committed
248
	applyServiceChanges(coredns, changes2)
Stan Lagun's avatar
Stan Lagun committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	expectedServices2 := map[string]*Service{
		"/skydns/local/domain1": {Host: "6.6.6.6", Text: "string1"},
		"/skydns/local/domain2": {Host: "site.local"},
		"/skydns/local/domain3": {Host: "7.7.7.7"},
	}
	validateServices(client.services, expectedServices2, t, 2)

	changes3 := &plan.Changes{
		Delete: []*endpoint.Endpoint{
			endpoint.NewEndpoint("domain1.local", "6.6.6.6", endpoint.RecordTypeA),
			endpoint.NewEndpoint("domain1.local", "string", endpoint.RecordTypeTXT),
			endpoint.NewEndpoint("domain3.local", "7.7.7.7", endpoint.RecordTypeA),
		},
	}

Stan Lagun's avatar
Stan Lagun committed
265
	applyServiceChanges(coredns, changes3)
Stan Lagun's avatar
Stan Lagun committed
266 267 268 269 270 271 272

	expectedServices3 := map[string]*Service{
		"/skydns/local/domain2": {Host: "site.local"},
	}
	validateServices(client.services, expectedServices3, t, 3)
}

Stan Lagun's avatar
Stan Lagun committed
273 274 275 276 277 278
func applyServiceChanges(provider coreDNSProvider, changes *plan.Changes) {
	records, _ := provider.Records()
	for _, col := range [][]*endpoint.Endpoint{changes.Create, changes.UpdateNew, changes.Delete} {
		for _, record := range col {
			for _, existingRecord := range records {
				if existingRecord.DNSName == record.DNSName && existingRecord.RecordType == record.RecordType {
279
					mergeLabels(record, existingRecord.Labels)
Stan Lagun's avatar
Stan Lagun committed
280 281 282 283 284 285 286
				}
			}
		}
	}
	provider.ApplyChanges(changes)
}

Stan Lagun's avatar
Stan Lagun committed
287 288 289 290 291
func validateServices(services, expectedServices map[string]*Service, t *testing.T, step int) {
	if len(services) != len(expectedServices) {
		t.Errorf("wrong number of records on step %d: %d != %d", step, len(services), len(expectedServices))
	}
	for key, value := range services {
Stan Lagun's avatar
Stan Lagun committed
292 293 294
		keyParts := strings.Split(key, "/")
		expectedKey := strings.Join(keyParts[:len(keyParts)-value.TargetStrip], "/")
		expectedService := expectedServices[expectedKey]
Stan Lagun's avatar
Stan Lagun committed
295 296 297 298 299 300 301 302 303 304 305 306 307
		if expectedService == nil {
			t.Errorf("unexpected service %s", key)
			continue
		}
		delete(expectedServices, key)
		if value.Host != expectedService.Host {
			t.Errorf("wrong host for service %s: %s != %s on step %d", key, value.Host, expectedService.Host, step)
		}
		if value.Text != expectedService.Text {
			t.Errorf("wrong text for service %s: %s != %s on step %d", key, value.Text, expectedService.Text, step)
		}
	}
}
308 309 310 311 312 313 314 315 316

// mergeLabels adds keys to labels if not defined for the endpoint
func mergeLabels(e *endpoint.Endpoint, labels map[string]string) {
	for k, v := range labels {
		if e.Labels[k] == "" {
			e.Labels[k] = v
		}
	}
}