aboutsummaryrefslogtreecommitdiff
path: root/middleware/kubernetes/federation_test.go
blob: 410d2a54e4c34e0b69bf7512aa7c4d738b64cd3d (plain) (blame)
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
package kubernetes

import (
	"net"
	"strings"
	"testing"

	"github.com/coredns/coredns/middleware/etcd/msg"
	"github.com/coredns/coredns/request"
	"github.com/miekg/dns"
	"k8s.io/client-go/1.5/pkg/api"
)

func testStripFederation(t *testing.T, k Kubernetes, input []string, expectedFed string, expectedSegs string) {
	fed, segs := k.stripFederation(input)

	if expectedSegs != strings.Join(segs, ".") {
		t.Errorf("For '%v', expected segs result '%v'. Instead got result '%v'.", strings.Join(input, "."), expectedSegs, strings.Join(segs, "."))
	}
	if expectedFed != fed {
		t.Errorf("For '%v', expected fed result '%v'. Instead got result '%v'.", strings.Join(input, "."), expectedFed, fed)
	}
}

func TestStripFederation(t *testing.T) {
	k := Kubernetes{Zones: []string{"inter.webs.test"}}
	k.Federations = []Federation{{name: "fed", zone: "era.tion.com"}}

	testStripFederation(t, k, []string{"service", "ns", "fed", Svc}, "fed", "service.ns.svc")
	testStripFederation(t, k, []string{"service", "ns", "foo", Svc}, "", "service.ns.foo.svc")
	testStripFederation(t, k, []string{"foo", "bar"}, "", "foo.bar")

}

type apiConnFedTest struct{}

func (apiConnFedTest) Run()                          { return }
func (apiConnFedTest) Stop() error                   { return nil }
func (apiConnFedTest) ServiceList() []*api.Service   { return []*api.Service{} }
func (apiConnFedTest) PodIndex(string) []interface{} { return nil }

func (apiConnFedTest) EndpointsList() api.EndpointsList {
	n := "test.node.foo.bar"
	return api.EndpointsList{
		Items: []api.Endpoints{
			{
				Subsets: []api.EndpointSubset{
					{
						Addresses: []api.EndpointAddress{
							{
								IP:       "10.9.8.7",
								NodeName: &n,
							},
						},
					},
				},
			},
		},
	}
}

func (apiConnFedTest) GetNodeByName(name string) (api.Node, error) {
	if name != "test.node.foo.bar" {
		return api.Node{}, nil
	}
	return api.Node{
		ObjectMeta: api.ObjectMeta{
			Name: "test.node.foo.bar",
			Labels: map[string]string{
				labelRegion:           "fd-r",
				labelAvailabilityZone: "fd-az",
			},
		},
	}, nil
}

func testFederationCNAMERecord(t *testing.T, k Kubernetes, input recordRequest, expected msg.Service) {
	svc := k.federationCNAMERecord(input)

	if expected.Host != svc.Host {
		t.Errorf("For '%v', expected Host result '%v'. Instead got result '%v'.", input, expected.Host, svc.Host)
	}
	if expected.Key != svc.Key {
		t.Errorf("For '%v', expected Key result '%v'. Instead got result '%v'.", input, expected.Key, svc.Key)
	}
}

func TestFederationCNAMERecord(t *testing.T) {
	k := Kubernetes{Zones: []string{"inter.webs."}}
	k.Federations = []Federation{{name: "fed", zone: "era.tion.com"}}
	k.APIConn = apiConnFedTest{}
	k.interfaceAddrsFunc = func() net.IP { return net.ParseIP("10.9.8.7") }

	m := new(dns.Msg)
	state := request.Request{Zone: "inter.webs.", Req: m}

	m.SetQuestion("s1.ns.fed.svc.inter.webs.", dns.TypeA)
	r, _ := k.parseRequest(state)
	testFederationCNAMERecord(t, k, r, msg.Service{Key: "/coredns/webs/inter/svc/fed/ns/s1", Host: "s1.ns.fed.svc.fd-az.fd-r.era.tion.com"})

	m.SetQuestion("ep1.s1.ns.fed.svc.inter.webs.", dns.TypeA)
	r, _ = k.parseRequest(state)
	testFederationCNAMERecord(t, k, r, msg.Service{Key: "/coredns/webs/inter/svc/fed/ns/s1/ep1", Host: "ep1.s1.ns.fed.svc.fd-az.fd-r.era.tion.com"})

	m.SetQuestion("ep1.s1.ns.foo.svc.inter.webs.", dns.TypeA)
	r, _ = k.parseRequest(state)
	testFederationCNAMERecord(t, k, r, msg.Service{Key: "", Host: ""})
}