-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_server_test.go
331 lines (290 loc) · 8.34 KB
/
pipeline_server_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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"fmt"
"github.com/miekg/dns"
"github.com/stretchr/testify/mock"
"testing"
"time"
)
func TestQueryHandler(t *testing.T) {
pw := NewPipelineServerWorker()
pw.outboundQueryChannel = make(chan Query, 1)
q := &PipelineQueryHandler{
pipelineServerWorker: pw,
}
msg := &dns.Msg{}
m := &MockResponseWriter{}
m.On("WriteMsg", mock.Anything).Return(nil)
q.HandleDNS(m, msg)
qu := <-q.outboundQueryChannel
if qu.Msg != msg {
t.Fatalf("submitted message [%v] did not match message produced by HandleDNS [%v]", msg, qu.Msg)
}
}
func TestConnectorReuseConn(t *testing.T) {
testClient := &MockClient{}
testConnPool := &MockConnPool{}
testConnEntry := &connEntry{
Conn: &dns.Conn{},
}
c := &PipelineConnector{
client: testClient,
connPool: testConnPool,
}
testConnPool.On("Get", mock.Anything).Return(testConnEntry, Upstream{})
qu := Query{}
qu1, err := c.AssignConnection(qu)
if err != nil {
t.Fatalf("could not assign connection: [%v] [%v]", qu, c)
}
if qu1.Conn == nil {
t.Fatalf("successful return from PipelineConnector.AssignConnection didn't attach a conn to the query struct: [%v]", qu1)
}
}
func TestConnectorFailedAttempt(t *testing.T) {
testClient := &MockClient{}
testConnPool := &MockConnPool{}
c := &PipelineConnector{
client: testClient,
connPool: testConnPool,
}
testConnPool.On("Get", mock.Anything).Return(nil, Upstream{Name: "example.com"}, nil)
testConnPool.On("NewConnection", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("blah blah blah blah"))
GetConfiguration().UpstreamRetries = 5
qu := Query{}
qu1, err := c.AssignConnection(qu)
if err == nil {
t.Fatalf("new connections are supposed to be failing, but no error came back from AssignConnection")
}
if (qu1 != Query{}) {
t.Fatalf("Got populated query struct back from failed connection assignment: q: [%v]", qu1)
}
}
func TestConnectorNewConn(t *testing.T) {
testConnPool := &MockConnPool{}
testConnEntry := &connEntry{
Conn: &dns.Conn{},
}
c := &PipelineConnector{
client: &MockClient{},
connPool: testConnPool,
}
testConnPool.On("Get", mock.Anything).Return(&connEntry{}, Upstream{Name: "example.com"})
testConnPool.On("NewConnection", mock.Anything, mock.Anything).Return(testConnEntry, nil)
qu := Query{}
qu1, err := c.AssignConnection(qu)
if err != nil {
t.Fatalf("could not assign connection: [%v] [%v]", qu, c)
}
if qu1.Conn != testConnEntry {
t.Fatalf("stored connection was not used by PipelineConnector: [%v] [%v]", qu1.Conn, testConnEntry)
}
}
func TestCacher(t *testing.T) {
pw := NewPipelineServerWorker()
cache, err := NewCache()
if err != nil {
t.Fatalf("couldn't do cache: %s", err)
}
c := &PipelineCacher{
pipelineServerWorker: pw,
cache: cache,
}
msg := buildRequest()
qu := Query{}
if cached, ok := c.CheckCache(qu); ok {
t.Fatalf("no error returned when empty query was passed in, cached: [%v]", cached)
}
qu = Query{
Msg: msg,
}
c.CacheQuery(qu)
if cached, ok := c.CheckCache(qu); !ok {
t.Fatalf("failed to find item that shold have been in cache, cached: [%v], qu: [%v], c [%v]", cached, qu, c)
}
}
func TestCacherStart(t *testing.T) {
msg := buildRequest()
cw := NewPipelineServerWorker()
cache := &MockCache{}
cache.On("Get", "", 0).Return(Response{}, false).Once()
cache.On("Get", "example.com", uint16(1)).Return(Response{Entry: *msg}, true)
cache.On("Add", mock.Anything).Return()
cacher := &PipelineCacher{
pipelineServerWorker: cw,
cachingChannel: make(chan Query),
cache: cache,
}
cacher.Start()
defer func() { cacher.cancelChannel <- true }()
q := Query{}
// does the cacher fail empty queries?
cacher.inboundQueryChannel <- q
<-cacher.failedQueryChannel
q = Query{
Msg: msg,
}
// does the cacher dispatch valid queries?
cacher.cachingChannel <- q
cacher.inboundQueryChannel <- q
q = <-cacher.outboundQueryChannel
if q.Reply == nil {
t.Fatalf("did not get valid reply on cache hit, q: [%v]", q)
}
}
func TestQuerier(t *testing.T) {
pw := NewPipelineServerWorker()
mockClient := &MockClient{}
reply := &dns.Msg{}
mockClient.On("ExchangeWithConn", mock.Anything, mock.Anything).Return(reply, time.Duration(1), nil)
q := PipelineQuerier{
pipelineServerWorker: pw,
client: mockClient,
}
testConnEntry := &connEntry{
Conn: &dns.Conn{},
}
qu := Query{
Conn: testConnEntry,
Msg: &dns.Msg{
Question: []dns.Question{
dns.Question{
Name: "example.com",
Qtype: 123,
},
},
},
}
qu1, err := q.Query(qu)
if err != nil {
t.Fatalf("error during query: %s", err.Error())
}
if qu1.Reply.String() != reply.String() {
t.Fatalf("got incorrect reply to dns query: [%v] != [%v]", qu1.Reply, reply)
}
}
func TestQuerierStart(t *testing.T) {
pw := NewPipelineServerWorker()
mockClient := &MockClient{}
a, err := dns.NewRR("example.com. 123 IN A 10.0.0.0")
if err != nil {
t.Fatalf("couldn't create answer for query: %s", err)
}
reply := &dns.Msg{
Answer: []dns.RR{
a,
},
}
mockClient.On("ExchangeWithConn", mock.Anything, mock.Anything).Return(reply, time.Duration(1), nil).Once()
mockClient.On("ExchangeWithConn", mock.Anything, mock.Anything).Return(reply, time.Duration(1), fmt.Errorf("blah blah blah")).Once()
q := PipelineQuerier{
pipelineServerWorker: pw,
client: mockClient,
}
q.Start()
defer func() { q.cancelChannel <- true }()
testConnEntry := &connEntry{
Conn: &dns.Conn{},
}
// First, test a good query
qu := Query{
Conn: testConnEntry,
Msg: &dns.Msg{
Question: []dns.Question{
dns.Question{
Name: "example.com",
Qtype: 123,
},
},
},
}
q.inboundQueryChannel <- qu
outcome := <-q.outboundQueryChannel
if outcome.Reply.String() != reply.String() {
t.Fatalf("got wrong reply from querier: outcome [%v] reply [%v]", outcome, reply)
}
q.inboundQueryChannel <- qu
<-q.failedQueryChannel
}
func TestFinisherStart(t *testing.T) {
pw := NewPipelineServerWorker()
p := PipelineFinisher{
pipelineServerWorker: pw,
}
pw.inboundQueryChannel = make(chan Query)
pw.outboundQueryChannel = make(chan Query)
pw.failedQueryChannel = make(chan Query)
p.servfailsChannel = make(chan Query)
p.Start()
defer func() { p.cancelChannel <- true }()
writer := &MockResponseWriter{}
qdt := &MockQueryDurationTimer{}
writer.On("WriteMsg", mock.Anything).Return(nil)
qdt.On("ObserveDuration").Return(time.Duration(100))
q := Query{
W: writer,
Msg: &dns.Msg{},
Reply: &dns.Msg{},
Timer: qdt,
}
p.inboundQueryChannel <- q
<-p.outboundQueryChannel
p.servfailsChannel <- q
<-p.failedQueryChannel
}
func TestFinisherStartErrors(t *testing.T) {
pw := NewPipelineServerWorker()
p := PipelineFinisher{
pipelineServerWorker: pw,
}
pw.inboundQueryChannel = make(chan Query)
pw.outboundQueryChannel = make(chan Query)
pw.failedQueryChannel = make(chan Query)
p.servfailsChannel = make(chan Query)
p.Start()
defer func() { p.cancelChannel <- true }()
q := buildQuery()
q.W.(*MockResponseWriter).On("WriteMsg", mock.Anything).Return(fmt.Errorf("argh"))
q.Timer.(*MockQueryDurationTimer).On("ObserveDuration").Return(time.Duration(100))
p.inboundQueryChannel <- q
<-p.outboundQueryChannel
p.servfailsChannel <- q
<-p.failedQueryChannel
}
func TestEndToEnd(t *testing.T) {
client := &MockClient{}
cpool := &MockConnPool{}
writer := &MockResponseWriter{}
entry := &MockConnEntry{}
upstream := Upstream{
Name: "example.com",
}
reply := buildAnswer()
writer.On("WriteMsg", mock.Anything).Return(nil)
client.On("ExchangeWithConn", mock.Anything, mock.Anything).Return(reply, time.Duration(100), nil)
cpool.On("Get").Return(&MockConnEntry{}, upstream)
cpool.On("Add", mock.Anything).Return(nil)
cpool.On("NewConnection", mock.Anything, mock.Anything).Return(entry, nil)
entry.On("GetAddress").Return("example.com:853")
entry.On("AddExchange", mock.Anything)
entry.On("GetAddress").Return(upstream.GetAddress())
entry.On("GetConn").Return(&dns.Conn{})
request := buildRequest()
qh, cancels, err := NewPipelineServer(client, cpool)
if err != nil {
t.Fatalf("could not build pipeline server: %s", err)
}
defer func() {
for _, each := range cancels {
each <- true
}
}()
qh.HandleDNS(writer, request)
WaitForCondition(10, func() bool {
return len(cpool.Calls) == len(cpool.ExpectedCalls)
})
entry.AssertExpectations(t)
writer.AssertExpectations(t)
client.AssertExpectations(t)
cpool.AssertExpectations(t)
}