-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathshuttle_test.go
378 lines (315 loc) · 8.51 KB
/
shuttle_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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package shuttle
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"regexp"
"sync"
"testing"
"time"
)
var longerTestData = []byte(`Lebowski ipsum what in God's holy name are you blathering about?
Dolor sit amet, consectetur adipiscing elit praesent ac magna justo.
They're nihilists.
Pellentesque ac lectus quis elit blandit fringilla a ut turpis praesent.
Mein nommen iss Karl.
Is hard to verk in zese clozes.
Felis ligula, malesuada suscipit malesuada non, ultrices non.
Shomer shabbos.
Urna sed orci ipsum, placerat id condimentum rutrum, rhoncus.
Yeah man, it really tied the room together.
Ac lorem aliquam placerat.
`)
func newTestConfig() Config {
// Defaults should be good for most tests
config := NewConfig()
config.LogsURL = "http://"
return config
}
type loopingBuffer struct {
b []byte
close chan struct{}
p int
mu sync.Mutex
r int
}
func newLoopingBuffer(b []byte) *loopingBuffer {
return &loopingBuffer{
b: b,
close: make(chan struct{}),
}
}
func (b *loopingBuffer) Read(p []byte) (n int, err error) {
for n < len(p) && err == nil {
select {
case <-b.close:
return n, io.EOF
default:
}
c := copy(p[n:], b.b[b.p:])
n += c
b.p += c
b.mu.Lock()
b.r += c
b.mu.Unlock()
if b.p >= len(b.b) {
b.p = 0
return
}
}
return
}
func (b *loopingBuffer) Close() error {
close(b.close)
return nil
}
// bytesRead since last time this was called
func (b *loopingBuffer) bytesRead() int {
b.mu.Lock()
defer b.mu.Unlock()
v := b.r
b.r = 0
return v
}
func NewTestInput() io.ReadCloser {
data := []byte(`Hello World
Test Line 2
`)
return ioutil.NopCloser(bytes.NewReader(data))
}
func NewTestInputWithHeaders() io.ReadCloser {
data := []byte(`<13>1 2013-09-25T01:16:49.371356+00:00 host token web.1 - [meta sequenceId="1"] message 1
<13>1 2013-09-25T01:16:49.402923+00:00 host token web.1 - [meta sequenceId="2"] message 2
`)
return ioutil.NopCloser(bytes.NewReader(data))
}
type noopTestHelper struct{}
func (th *noopTestHelper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
}
type testHelper struct {
Actual []byte
Headers http.Header
Called int
sync.Mutex
}
func (ts *testHelper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
d, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
ts.Mutex.Lock()
defer ts.Mutex.Unlock()
ts.Called++
// Last request wins the race
ts.Actual = d
ts.Headers = r.Header
}
func TestIntegration(t *testing.T) {
th := new(testHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
shut := NewShuttle(config)
input := NewTestInput()
shut.LoadReader(input)
shut.Launch()
shut.WaitForReadersToFinish()
shut.Land()
pat1 := regexp.MustCompile(`78 <190>1 [0-9T:\+\-\.]+ shuttle token shuttle - - Hello World`)
pat2 := regexp.MustCompile(`78 <190>1 [0-9T:\+\-\.]+ shuttle token shuttle - - Test Line 2`)
if !pat1.Match(th.Actual) {
t.Fatalf("actual=%s\n", string(th.Actual))
}
if !pat2.Match(th.Actual) {
t.Fatalf("actual=%s\n", string(th.Actual))
}
if afterDrops, _ := shut.Drops.ReadAndReset(); afterDrops != 0 {
t.Fatalf("afterDrops=%d\n", afterDrops)
}
}
func TestInputFormatRFC5424Integration(t *testing.T) {
th := new(testHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
config.InputFormat = InputFormatRFC5424
shut := NewShuttle(config)
input := NewTestInputWithHeaders()
shut.LoadReader(input)
shut.Launch()
shut.WaitForReadersToFinish()
shut.Land()
pat1 := regexp.MustCompile(`90 <13>1 2013-09-25T01:16:49\.371356\+00:00 host token web\.1 - \[meta sequenceId="1"\] message 1`)
pat2 := regexp.MustCompile(`90 <13>1 2013-09-25T01:16:49\.402923\+00:00 host token web\.1 - \[meta sequenceId="2"\] message 2`)
if !pat1.Match(th.Actual) {
t.Fatalf("actual=%s\n", string(th.Actual))
}
if !pat2.Match(th.Actual) {
t.Fatalf("actual=%s\n", string(th.Actual))
}
}
func TestDrops(t *testing.T) {
th := new(testHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
config.InputFormat = InputFormatRaw
shut := NewShuttle(config)
input := NewTestInput()
shut.LoadReader(input)
shut.Launch()
shut.Drops.Add(1)
shut.Drops.Add(1)
shut.WaitForReadersToFinish()
shut.Land()
pat1 := regexp.MustCompile(`138 <172>1 [0-9T:\+\-\.]+ heroku token log-shuttle - - Error L12: 2 messages dropped since [0-9T:\+\-\.]+\n`)
if !pat1.Match(th.Actual) {
t.Fatalf("actual=%s\n", string(th.Actual))
}
dropHeader, ok := th.Headers["Logplex-Drop-Count"]
if !ok {
t.Fatalf("Header Logplex-Drop-Count not found in response")
}
if dropHeader[0] != "2" {
t.Fatalf("Logplex-Drop-Count=%s\n", dropHeader[0])
}
//Should be 0 because it was reset during delivery to the testHelper
if afterDrops, _ := shut.Drops.ReadAndReset(); afterDrops != 0 {
t.Fatalf("afterDrops=%d\n", afterDrops)
}
}
func TestLost(t *testing.T) {
th := new(testHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
config.InputFormat = InputFormatRaw
shut := NewShuttle(config)
input := NewTestInput()
shut.LoadReader(input)
shut.Launch()
shut.Lost.Add(1)
shut.Lost.Add(1)
shut.WaitForReadersToFinish()
shut.Land()
pat1 := regexp.MustCompile(`135 <172>1 [0-9T:\+\-\.]+ heroku token log-shuttle - - Error L13: 2 messages lost since [0-9T:\+\-\.]+\n`)
if !pat1.Match(th.Actual) {
t.Fatalf("actual=%s\n", string(th.Actual))
}
lostHeader, ok := th.Headers["Logplex-Lost-Count"]
if !ok {
t.Fatalf("Header Logplex-Lost-Count not found in response")
}
if lostHeader[0] != "2" {
t.Fatalf("Logplex-Lost-Count=%s\n", lostHeader[0])
}
//Should be 0 because it was reset during delivery to the testHelper
if afterLost, _ := shut.Lost.ReadAndReset(); afterLost != 0 {
t.Fatalf("afterLost=%d\n", afterLost)
}
}
func TestUserAgentHeader(t *testing.T) {
th := new(testHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
config.InputFormat = InputFormatRaw
config.ID = "0.1-abcde"
shut := NewShuttle(config)
input := NewTestInput()
shut.LoadReader(input)
shut.Launch()
shut.WaitForReadersToFinish()
shut.Land()
uaHeader, ok := th.Headers["User-Agent"]
if !ok {
t.Fatalf("Header User-Agent not found in response")
}
uaPattern := regexp.MustCompile(`^^log-shuttle/[0-9a-z-\.]+ \(go\d+(\.\d+){0,2}((beta|rc)\d)?; \w+; \w+; \w+\)$`)
if !uaPattern.MatchString(uaHeader[0]) {
t.Fatalf("Header User-Agent doesn't match expected pattern. Actual: %s\n", uaHeader[0])
}
}
func TestRequestId(t *testing.T) {
th := new(testHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
config.InputFormat = InputFormatRaw
shut := NewShuttle(config)
input := NewTestInput()
shut.LoadReader(input)
shut.Launch()
shut.WaitForReadersToFinish()
shut.Land()
_, ok := th.Headers["X-Request-Id"]
if !ok {
t.Fatalf("Header X-Request-ID not found in response")
}
}
func TestBearerAuthHeader(t *testing.T) {
th := new(testHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
config.InputFormat = InputFormatRaw
config.BearerAuthToken = "my-secret-token"
shut := NewShuttle(config)
input := NewTestInput()
shut.LoadReader(input)
shut.Launch()
shut.WaitForReadersToFinish()
shut.Land()
token, ok := th.Headers["Authorization"]
if !ok {
t.Fatalf("Header Authorization not found in req")
}
if token[0] != fmt.Sprintf("Bearer %s", config.BearerAuthToken) {
t.Fatalf("Header Authorization did not match expected. Actual: %s\n", token[0])
}
}
func BenchmarkPipeline(b *testing.B) {
th := new(noopTestHelper)
ts := httptest.NewServer(th)
defer ts.Close()
config := newTestConfig()
config.LogsURL = ts.URL
config.InputFormat = InputFormatRaw
shut := NewShuttle(config)
input := newLoopingBuffer(longerTestData)
shut.LoadReader(input)
shut.Launch()
var tb int
for i := 0; i < b.N; i++ {
// This sleep is here to allow the reading goroutines to make
// progress reading. Open to better ideas.
time.Sleep(10 * time.Microsecond)
tb += input.bytesRead()
}
b.SetBytes(int64(tb / b.N))
shut.Land()
}
func ExampleShuttle() {
config := NewConfig()
// Modulate the config as needed before creating a new shuttle
s := NewShuttle(config)
s.LoadReader(os.Stdin)
s.Launch() // Start up the batching/delivering go routines
s.Land() // Spin down the batching/delivering go routines
}