coredns_test.go 9.77 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
/*
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 (
20
	"context"
Stan Lagun's avatar
Stan Lagun committed
21 22 23 24 25 26 27
	"strings"
	"testing"

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

xunpan's avatar
xunpan committed
28 29
const defaultCoreDNSPrefix = "/skydns/"

Stan Lagun's avatar
Stan Lagun committed
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
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},
		},
	}
xunpan's avatar
xunpan committed
65 66 67 68
	provider := coreDNSProvider{
		client:        client,
		coreDNSPrefix: defaultCoreDNSPrefix,
	}
Stan Lagun's avatar
Stan Lagun committed
69 70 71 72 73 74 75 76 77 78
	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)
	}
79 80
	if endpoints[0].Targets[0] != expectedTarget {
		t.Errorf("got unexpected DNS target: %s != %s", endpoints[0].Targets[0], expectedTarget)
Stan Lagun's avatar
Stan Lagun committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	}
	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},
		},
	}
xunpan's avatar
xunpan committed
97 98 99 100
	provider := coreDNSProvider{
		client:        client,
		coreDNSPrefix: defaultCoreDNSPrefix,
	}
Stan Lagun's avatar
Stan Lagun committed
101 102 103 104 105 106 107 108 109 110
	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)
	}
111 112
	if endpoints[0].Targets[0] != expectedTarget {
		t.Errorf("got unexpected DNS target: %s != %s", endpoints[0].Targets[0], expectedTarget)
Stan Lagun's avatar
Stan Lagun committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	}
	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},
		},
	}
xunpan's avatar
xunpan committed
129 130 131 132
	provider := coreDNSProvider{
		client:        client,
		coreDNSPrefix: defaultCoreDNSPrefix,
	}
Stan Lagun's avatar
Stan Lagun committed
133 134 135 136 137 138 139 140 141 142
	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)
	}
143 144
	if endpoints[0].Targets[0] != expectedTarget {
		t.Errorf("got unexpected DNS target: %s != %s", endpoints[0].Targets[0], expectedTarget)
Stan Lagun's avatar
Stan Lagun committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	}
	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"},
		},
	}
xunpan's avatar
xunpan committed
163 164 165 166
	provider := coreDNSProvider{
		client:        client,
		coreDNSPrefix: defaultCoreDNSPrefix,
	}
Stan Lagun's avatar
Stan Lagun committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	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)
		}

187 188
		if ep.Targets[0] != expectedTarget {
			t.Errorf("got unexpected DNS target: %s != %s", ep.Targets[0], expectedTarget)
Stan Lagun's avatar
Stan Lagun committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
		}
	}
}

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"},
		},
	}
xunpan's avatar
xunpan committed
205 206 207 208
	provider := coreDNSProvider{
		client:        client,
		coreDNSPrefix: defaultCoreDNSPrefix,
	}
Stan Lagun's avatar
Stan Lagun committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	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)
		}

229 230
		if ep.Targets[0] != expectedTarget {
			t.Errorf("got unexpected DNS target: %s != %s", ep.Targets[0], expectedTarget)
Stan Lagun's avatar
Stan Lagun committed
231 232 233 234 235 236 237 238
		}
	}
}

func TestCoreDNSApplyChanges(t *testing.T) {
	client := fakeETCDClient{
		map[string]*Service{},
	}
xunpan's avatar
xunpan committed
239 240 241 242
	coredns := coreDNSProvider{
		client:        client,
		coreDNSPrefix: defaultCoreDNSPrefix,
	}
Stan Lagun's avatar
Stan Lagun committed
243 244 245

	changes1 := &plan.Changes{
		Create: []*endpoint.Endpoint{
246 247 248
			endpoint.NewEndpoint("domain1.local", endpoint.RecordTypeA, "5.5.5.5"),
			endpoint.NewEndpoint("domain1.local", endpoint.RecordTypeTXT, "string1"),
			endpoint.NewEndpoint("domain2.local", endpoint.RecordTypeCNAME, "site.local"),
Stan Lagun's avatar
Stan Lagun committed
249 250
		},
	}
251
	coredns.ApplyChanges(context.Background(), changes1)
Stan Lagun's avatar
Stan Lagun committed
252 253 254 255 256 257 258 259 260

	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)

	changes2 := &plan.Changes{
		Create: []*endpoint.Endpoint{
261
			endpoint.NewEndpoint("domain3.local", endpoint.RecordTypeA, "7.7.7.7"),
Stan Lagun's avatar
Stan Lagun committed
262
		},
Stan Lagun's avatar
Stan Lagun committed
263
		UpdateNew: []*endpoint.Endpoint{
264
			endpoint.NewEndpoint("domain1.local", "A", "6.6.6.6"),
Stan Lagun's avatar
Stan Lagun committed
265
		},
Stan Lagun's avatar
Stan Lagun committed
266
	}
267 268 269 270 271 272
	records, _ := coredns.Records()
	for _, ep := range records {
		if ep.DNSName == "domain1.local" {
			changes2.UpdateOld = append(changes2.UpdateOld, ep)
		}
	}
Stan Lagun's avatar
Stan Lagun committed
273
	applyServiceChanges(coredns, changes2)
Stan Lagun's avatar
Stan Lagun committed
274 275 276 277 278 279 280 281 282 283

	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{
284 285 286
			endpoint.NewEndpoint("domain1.local", endpoint.RecordTypeA, "6.6.6.6"),
			endpoint.NewEndpoint("domain1.local", endpoint.RecordTypeTXT, "string"),
			endpoint.NewEndpoint("domain3.local", endpoint.RecordTypeA, "7.7.7.7"),
Stan Lagun's avatar
Stan Lagun committed
287 288 289
		},
	}

Stan Lagun's avatar
Stan Lagun committed
290
	applyServiceChanges(coredns, changes3)
Stan Lagun's avatar
Stan Lagun committed
291 292 293 294 295 296 297

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

Stan Lagun's avatar
Stan Lagun committed
298 299 300 301 302 303
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 {
304
					mergeLabels(record, existingRecord.Labels)
Stan Lagun's avatar
Stan Lagun committed
305 306 307 308
				}
			}
		}
	}
309
	provider.ApplyChanges(context.Background(), changes)
Stan Lagun's avatar
Stan Lagun committed
310 311
}

Stan Lagun's avatar
Stan Lagun committed
312 313 314 315 316
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
317 318 319
		keyParts := strings.Split(key, "/")
		expectedKey := strings.Join(keyParts[:len(keyParts)-value.TargetStrip], "/")
		expectedService := expectedServices[expectedKey]
Stan Lagun's avatar
Stan Lagun committed
320 321 322 323 324 325 326 327 328 329 330 331 332
		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)
		}
	}
}
333 334 335 336 337 338 339 340 341

// 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
		}
	}
}