-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmetadata_test.go
155 lines (140 loc) · 3.59 KB
/
metadata_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
153
154
155
package kubernetes
import (
"context"
"testing"
"github.com/coredns/coredns/plugin/metadata"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
var metadataCases = []struct {
Qname string
Qtype uint16
RemoteIP string
Md map[string]string
}{
{
Qname: "foo.bar.notapod.cluster.local.", Qtype: dns.TypeA,
Md: map[string]string{
"kubernetes/parse-error": "invalid query name",
},
},
{
Qname: "10-240-0-1.podns.pod.cluster.local.", Qtype: dns.TypeA,
Md: map[string]string{
"kubernetes/endpoint": "",
"kubernetes/kind": "pod",
"kubernetes/namespace": "podns",
"kubernetes/port-name": "*",
"kubernetes/protocol": "*",
"kubernetes/service": "10-240-0-1",
},
},
{
Qname: "s.ns.svc.cluster.local.", Qtype: dns.TypeA,
Md: map[string]string{
"kubernetes/endpoint": "",
"kubernetes/kind": "svc",
"kubernetes/namespace": "ns",
"kubernetes/port-name": "*",
"kubernetes/protocol": "*",
"kubernetes/service": "s",
},
},
{
Qname: "s.ns.svc.cluster.local.", Qtype: dns.TypeA,
RemoteIP: "10.10.10.10",
Md: map[string]string{
"kubernetes/endpoint": "",
"kubernetes/kind": "svc",
"kubernetes/namespace": "ns",
"kubernetes/port-name": "*",
"kubernetes/protocol": "*",
"kubernetes/service": "s",
},
},
{
Qname: "_http._tcp.s.ns.svc.cluster.local.", Qtype: dns.TypeSRV,
RemoteIP: "10.10.10.10",
Md: map[string]string{
"kubernetes/endpoint": "",
"kubernetes/kind": "svc",
"kubernetes/namespace": "ns",
"kubernetes/port-name": "http",
"kubernetes/protocol": "tcp",
"kubernetes/service": "s",
},
},
{
Qname: "ep.s.ns.svc.cluster.local.", Qtype: dns.TypeA,
RemoteIP: "10.10.10.10",
Md: map[string]string{
"kubernetes/endpoint": "ep",
"kubernetes/kind": "svc",
"kubernetes/namespace": "ns",
"kubernetes/port-name": "*",
"kubernetes/protocol": "*",
"kubernetes/service": "s",
},
},
{
Qname: "example.com.", Qtype: dns.TypeA,
RemoteIP: "10.10.10.10",
Md: map[string]string{},
},
}
func mapsDiffer(a, b map[string]string) bool {
if len(a) != len(b) {
return true
}
for k, va := range a {
vb, ok := b[k]
if !ok || va != vb {
return true
}
}
return false
}
func TestMetadata(t *testing.T) {
k := New([]string{"cluster.local."})
k.APIConn = &APIConnServeTest{}
for i, tc := range metadataCases {
ctx := metadata.ContextWithMetadata(context.Background())
state := request.Request{
Req: &dns.Msg{Question: []dns.Question{{Name: tc.Qname, Qtype: tc.Qtype}}},
Zone: ".",
W: &test.ResponseWriter{RemoteIP: tc.RemoteIP},
}
k.Metadata(ctx, state)
md := make(map[string]string)
for _, l := range metadata.Labels(ctx) {
md[l] = metadata.ValueFunc(ctx, l)()
}
if mapsDiffer(tc.Md, md) {
t.Errorf("Case %d expected metadata %v and got %v", i, tc.Md, md)
}
}
}
func TestMetadataPodsVerified(t *testing.T) {
k := New([]string{"cluster.local."})
k.podMode = podModeVerified
k.APIConn = &APIConnServeTest{}
ctx := metadata.ContextWithMetadata(context.Background())
state := request.Request{
Req: &dns.Msg{Question: []dns.Question{{Name: "example.com.", Qtype: dns.TypeA}}},
Zone: ".",
W: &test.ResponseWriter{},
}
k.Metadata(ctx, state)
expect := map[string]string{
"kubernetes/client-namespace": "podns",
"kubernetes/client-pod-name": "foo",
}
md := make(map[string]string)
for _, l := range metadata.Labels(ctx) {
md[l] = metadata.ValueFunc(ctx, l)()
}
if mapsDiffer(expect, md) {
t.Errorf("Expected metadata %v and got %v", expect, md)
}
}