-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathns_test.go
152 lines (138 loc) · 3.81 KB
/
ns_test.go
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
package kubernetes
import (
"context"
"net"
"testing"
"github.com/coredns/coredns/plugin/kubernetes/object"
"github.com/miekg/dns"
api "k8s.io/api/core/v1"
)
type APIConnTest struct{}
func (APIConnTest) HasSynced() bool { return true }
func (APIConnTest) Run() {}
func (APIConnTest) Stop() error { return nil }
func (APIConnTest) PodIndex(string) []*object.Pod { return nil }
func (APIConnTest) SvcIndexReverse(string) []*object.Service { return nil }
func (APIConnTest) EpIndex(string) []*object.Endpoints { return nil }
func (APIConnTest) EndpointsList() []*object.Endpoints { return nil }
func (APIConnTest) Modified() int64 { return 0 }
func (a APIConnTest) SvcIndex(s string) []*object.Service {
switch s {
case "dns-service.kube-system":
return []*object.Service{a.ServiceList()[0]}
case "hdls-dns-service.kube-system":
return []*object.Service{a.ServiceList()[1]}
case "dns6-service.kube-system":
return []*object.Service{a.ServiceList()[2]}
}
return nil
}
func (APIConnTest) ServiceList() []*object.Service {
svcs := []*object.Service{
{
Name: "dns-service",
Namespace: "kube-system",
ClusterIP: "10.0.0.111",
},
{
Name: "hdls-dns-service",
Namespace: "kube-system",
ClusterIP: api.ClusterIPNone,
},
{
Name: "dns6-service",
Namespace: "kube-system",
ClusterIP: "10::111",
},
}
return svcs
}
func (APIConnTest) EpIndexReverse(ip string) []*object.Endpoints {
if ip != "10.244.0.20" {
return nil
}
eps := []*object.Endpoints{
{
Subsets: []object.EndpointSubset{
{
Addresses: []object.EndpointAddress{
{
IP: "10.244.0.20",
},
},
},
},
Name: "dns-service",
Namespace: "kube-system",
},
{
Subsets: []object.EndpointSubset{
{
Addresses: []object.EndpointAddress{
{
IP: "10.244.0.20",
},
},
},
},
Name: "hdls-dns-service",
Namespace: "kube-system",
},
{
Subsets: []object.EndpointSubset{
{
Addresses: []object.EndpointAddress{
{
IP: "10.244.0.20",
},
},
},
},
Name: "dns6-service",
Namespace: "kube-system",
},
}
return eps
}
func (APIConnTest) GetNodeByName(ctx context.Context, name string) (*api.Node, error) {
return &api.Node{}, nil
}
func (APIConnTest) GetNamespaceByName(name string) (*api.Namespace, error) {
return &api.Namespace{}, nil
}
func TestNsAddrs(t *testing.T) {
k := New([]string{"inter.webs.test."})
k.APIConn = &APIConnTest{}
k.localIPs = []net.IP{net.ParseIP("10.244.0.20")}
cdrs := k.nsAddrs(false, k.Zones[0])
if len(cdrs) != 3 {
t.Fatalf("Expected 3 results, got %v", len(cdrs))
}
cdr := cdrs[0]
expected := "10.0.0.111"
if cdr.(*dns.A).A.String() != expected {
t.Errorf("Expected 1st A to be %q, got %q", expected, cdr.(*dns.A).A.String())
}
expected = "dns-service.kube-system.svc.inter.webs.test."
if cdr.Header().Name != expected {
t.Errorf("Expected 1st Header Name to be %q, got %q", expected, cdr.Header().Name)
}
cdr = cdrs[1]
expected = "10.244.0.20"
if cdr.(*dns.A).A.String() != expected {
t.Errorf("Expected 2nd A to be %q, got %q", expected, cdr.(*dns.A).A.String())
}
expected = "10-244-0-20.hdls-dns-service.kube-system.svc.inter.webs.test."
if cdr.Header().Name != expected {
t.Errorf("Expected 2nd Header Name to be %q, got %q", expected, cdr.Header().Name)
}
cdr = cdrs[2]
expected = "10::111"
if cdr.(*dns.AAAA).AAAA.String() != expected {
t.Errorf("Expected AAAA to be %q, got %q", expected, cdr.(*dns.A).A.String())
}
expected = "dns6-service.kube-system.svc.inter.webs.test."
if cdr.Header().Name != expected {
t.Errorf("Expected AAAA Header Name to be %q, got %q", expected, cdr.Header().Name)
}
}