forked from honeycombio/honeytail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leash_test.go
727 lines (682 loc) · 22.5 KB
/
leash_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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"sync"
"syscall"
"testing"
"time"
"github.com/honeycombio/honeytail/parsers/htjson"
"github.com/klauspost/compress/zstd"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
"github.com/honeycombio/honeytail/event"
"github.com/honeycombio/honeytail/tail"
)
var tailOptions = tail.TailOptions{
ReadFrom: "start",
Stop: true,
}
// defaultOptions is a fully populated GlobalOptions with good defaults to start from
var defaultOptions = GlobalOptions{
// each test will have to populate APIHost with the location of its test server
APIHost: "",
SampleRate: 1,
NumSenders: 1,
BatchFrequencyMs: 1000, // Longer batch sends to accommodate for slower CI machines
Reqs: RequiredOptions{
// using the json parser for everything because we're not testing parsers here.
ParserName: "json",
WriteKey: "abcabc123123",
// each test will specify its own logfile
// LogFiles: []string{tmpdir + ""},
Dataset: "pika",
},
Tail: tailOptions,
StatusInterval: 1,
}
// test testing framework
func TestHTTPtest(t *testing.T) {
ts := &testSetup{}
ts.start(t, &GlobalOptions{})
defer ts.close()
ts.rsp.responseBody = "whatup pikachu"
res, err := http.Get(ts.server.URL)
if err != nil {
log.Fatal(err)
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
assert.Equal(t, res.StatusCode, 200)
assert.Equal(t, string(greeting), "whatup pikachu")
assert.Equal(t, ts.rsp.reqCounter, 1)
}
func TestBasicSend(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/first.log"
fh, err := os.Create(logFileName)
if err != nil {
t.Fatal(err)
}
defer fh.Close()
fmt.Fprintf(fh, `{"format":"json"}`)
opts.Reqs.LogFiles = []string{logFileName}
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 1)
assert.Equal(t, ts.rsp.evtCounter, 1)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json"}`)
teamID := ts.rsp.req.Header.Get("X-Honeycomb-Team")
assert.Equal(t, teamID, "abcabc123123")
requestURL := ts.rsp.req.URL.Path
assert.Equal(t, requestURL, "/1/batch/pika")
}
func TestMultipleFiles(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFile1 := ts.tmpdir + "/first.log"
fh1, err := os.Create(logFile1)
if err != nil {
t.Fatal(err)
}
logFile2 := ts.tmpdir + "/second.log"
fh2, err := os.Create(logFile2)
if err != nil {
t.Fatal(err)
}
defer fh1.Close()
fmt.Fprintf(fh1, `{"key1":"val1"}`)
defer fh2.Close()
fmt.Fprintf(fh2, `{"key2":"val2"}`)
opts.Reqs.LogFiles = []string{logFile1, logFile2}
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 1)
assert.Equal(t, ts.rsp.evtCounter, 2)
assert.Contains(t, ts.rsp.reqBody, `{"key1":"val1"}`)
assert.Contains(t, ts.rsp.reqBody, `{"key2":"val2"}`)
teamID := ts.rsp.req.Header.Get("X-Honeycomb-Team")
assert.Equal(t, teamID, "abcabc123123")
requestURL := ts.rsp.req.URL.Path
assert.Equal(t, requestURL, "/1/batch/pika")
}
func TestMultiLineMultiFile(t *testing.T) {
opts := GlobalOptions{
NumSenders: 1,
Reqs: RequiredOptions{
ParserName: "mysql",
WriteKey: "----",
Dataset: "---",
},
Tail: tailOptions,
}
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFile1 := ts.tmpdir + "/first.log"
fh1, err := os.Create(logFile1)
if err != nil {
t.Fatal(err)
}
fmt.Fprintf(fh1, `# Time: 2016-04-01T00:29:09.817887Z",
# administrator command: Close stmt;
# User@Host: root[root] @ [10.0.72.76] Id: 432399
# Query_time: 0.000114 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0
SET timestamp=1459470669;
SELECT *
FROM orders WHERE
total > 1000;
# Time: 2016-04-01T00:31:09.817887Z
SET timestamp=1459470669;
show status like 'Uptime';`)
logFile2 := ts.tmpdir + "/second.log"
fh2, err := os.Create(logFile2)
if err != nil {
t.Fatal(err)
}
fmt.Fprintf(fh2, `# User@Host: rails[rails] @ [10.252.9.33]
# Query_time: 0.002280 Lock_time: 0.000023 Rows_sent: 0 Rows_examined: 921
SET timestamp=1444264264;
SELECT certs.* FROM certs WHERE (certs.app_id = 993089) LIMIT 1;
# administrator command: Prepare;
# User@Host: root[root] @ [10.0.99.122] Id: 432407
# Query_time: 0.000122 Lock_time: 0.000033 Rows_sent: 1 Rows_examined: 1
SET timestamp=1476702000;
SELECT
id, team_id, name, description, slug, limit_kb, created_at, updated_at
FROM datasets WHERE team_id=17 AND slug='api-prod';`)
opts.Reqs.LogFiles = []string{logFile1, logFile2}
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 1)
assert.Equal(t, ts.rsp.evtCounter, 4)
assert.Contains(t, ts.rsp.reqBody, `"query":"SELECT * FROM orders`)
assert.Contains(t, ts.rsp.reqBody, `"tables":"orders"`)
assert.Contains(t, ts.rsp.reqBody, `"query":"show status like 'Uptime'`)
assert.Contains(t, ts.rsp.reqBody, `"query":"SELECT certs.* FROM`)
assert.Contains(t, ts.rsp.reqBody, `"tables":"certs"`)
assert.Contains(t, ts.rsp.reqBody, `"query":"SELECT id, team_id, name`)
assert.Contains(t, ts.rsp.reqBody, `"tables":"datasets"`)
}
func TestSetVersion(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/setv.log"
fh, _ := os.Create(logFileName)
defer fh.Close()
fmt.Fprintf(fh, `{"format":"json"}`)
opts.Reqs.LogFiles = []string{logFileName}
run(context.Background(), opts)
userAgent := ts.rsp.req.Header.Get("User-Agent")
assert.Contains(t, userAgent, "libhoney-go")
setVersionUserAgent(false, "fancyParser")
run(context.Background(), opts)
userAgent = ts.rsp.req.Header.Get("User-Agent")
assert.Contains(t, userAgent, "libhoney-go")
assert.Contains(t, userAgent, "fancyParser")
BuildID = "test"
setVersionUserAgent(false, "fancyParser")
run(context.Background(), opts)
userAgent = ts.rsp.req.Header.Get("User-Agent")
assert.Contains(t, userAgent, " honeytail/test")
setVersionUserAgent(true, "fancyParser")
run(context.Background(), opts)
userAgent = ts.rsp.req.Header.Get("User-Agent")
assert.Contains(t, userAgent, " honeytail/test")
assert.Contains(t, userAgent, "fancyParser backfill")
}
func TestAugmentField(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/augment.log"
logfh, _ := os.Create(logFileName)
defer logfh.Close()
damapFileName := ts.tmpdir + "/damap.json"
damapfh, _ := os.Create(damapFileName)
defer damapfh.Close()
fmt.Fprintf(logfh, `{"format":"json"}
{"format":"freetext"}
{"format":"csv","delimiter":"comma"}`)
fmt.Fprintf(damapfh, `{"format":{
"json":{"structured":true},
"freetext":{"structured":false,"annoyance":5}
},
"color":{
"red":{"nomatch":"wontappear"}
}
}`)
opts.Reqs.LogFiles = []string{logFileName}
opts.DAMapFile = damapFileName
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 1, "failed count")
// json should be identified as structured
assert.Contains(t, ts.rsp.reqBody, `{"format":"json","structured":true}`, "faild content")
// free text gets two additional fields
assert.Contains(t, ts.rsp.reqBody, `{"annoyance":5,"format":"freetext","structured":false}`, "faild content")
// csv doesn't exist in the damap, so no change
assert.Contains(t, ts.rsp.reqBody, `{"delimiter":"comma","format":"csv"}`, "faild content")
}
func TestDropField(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/drop.log"
fh, _ := os.Create(logFileName)
defer fh.Close()
fmt.Fprintf(fh, `{"dropme":"chew","format":"json","reallygone":"notyet"}`)
opts.Reqs.LogFiles = []string{logFileName}
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 1)
assert.Contains(t, ts.rsp.reqBody, `{"dropme":"chew","format":"json","reallygone":"notyet"}`)
opts.DropFields = []string{"dropme"}
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 2)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json","reallygone":"notyet"}`)
opts.DropFields = []string{"dropme", "reallygone"}
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 3)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json"}`)
}
func TestScrubField(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/scrub.log"
fh, _ := os.Create(logFileName)
defer fh.Close()
fmt.Fprintf(fh, `{"format":"json","name":"hidden"}`)
opts.Reqs.LogFiles = []string{logFileName}
opts.ScrubFields = []string{"name"}
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 1)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json","name":"e564b4081d7a9ea4b00dada53bdae70c99b87b6fce869f0c3dd4d2bfa1e53e1c"}`)
}
func TestAddField(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/add.log"
logfh, _ := os.Create(logFileName)
defer logfh.Close()
fmt.Fprintf(logfh, `{"format":"json"}`)
opts.Reqs.LogFiles = []string{logFileName}
opts.AddFields = []string{`newfield=newval`}
run(context.Background(), opts)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json","newfield":"newval"}`)
opts.AddFields = []string{"newfield=newval", "second=new"}
run(context.Background(), opts)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json","newfield":"newval","second":"new"}`)
}
func TestLinePrefix(t *testing.T) {
opts := defaultOptions
// linePrefix of "Nov 13 10:19:31 app23 process.port[pid]: "
// let's capture timestamp and hostname, skip process.port and pid
opts.PrefixRegex = `(?P<server_timestamp>... .. ..:..:..) (?P<hostname>[a-zA-Z0-9]+) [^:]*: `
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/linePrefix.log"
logfh, _ := os.Create(logFileName)
defer logfh.Close()
fmt.Fprintf(logfh, `Nov 13 10:19:31 app23 process.port[pid]: {"format":"json"}`)
opts.Reqs.LogFiles = []string{logFileName}
run(context.Background(), opts)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json","hostname":"app23","server_timestamp":"Nov 13 10:19:31"}`)
}
func TestRequestShapeRaw(t *testing.T) {
reqField := "request"
opts := defaultOptions
opts.RequestShape = []string{"request"}
opts.RequestPattern = []string{"/about", "/about/:lang", "/about/:lang/books"}
urlsWhitelistQuery := map[string]map[string]interface{}{
"GET /about/en/books HTTP/1.1": {
"request_method": "GET",
"request_protocol_version": "HTTP/1.1",
"request_uri": "/about/en/books",
"request_path": "/about/en/books",
"request_query": nil, // field missing instead of empty
"request_path_lang": "en",
"request_shape": "/about/:lang/books",
"request_pathshape": "/about/:lang/books",
"request_queryshape": nil, // field missing instead of empty
},
"GET /about?foo=bar HTTP/1.0": {
"request_method": "GET",
"request_protocol_version": "HTTP/1.0",
"request_uri": "/about?foo=bar",
"request_path": "/about",
"request_query": "foo=bar",
"request_query_foo": "bar",
"request_shape": "/about?foo=?",
"request_pathshape": "/about",
"request_queryshape": "foo=?",
},
"/about/en/books": {
"request_uri": "/about/en/books",
"request_path": "/about/en/books",
"request_query": nil, // field missing instead of empty
"request_path_lang": "en",
"request_shape": "/about/:lang/books",
"request_pathshape": "/about/:lang/books",
"request_queryshape": nil, // field missing instead of empty
},
"/about/en?foo=bar&bar=bar2": {
"request_uri": "/about/en?foo=bar&bar=bar2",
"request_path": "/about/en",
"request_query": "foo=bar&bar=bar2",
"request_query_foo": "bar",
"request_path_lang": "en",
"request_shape": "/about/:lang?bar=?&foo=?",
"request_pathshape": "/about/:lang",
"request_queryshape": "bar=?&foo=?",
},
"/about/en?foo=bar&baz&foo=bend&foo=alpha&bend=beta": {
"request_uri": "/about/en?foo=bar&baz&foo=bend&foo=alpha&bend=beta",
"request_path": "/about/en",
"request_query": "foo=bar&baz&foo=bend&foo=alpha&bend=beta",
"request_query_foo": "alpha, bar, bend",
"request_query_bend": "beta",
"request_path_lang": "en",
"request_shape": "/about/:lang?baz=?&bend=?&foo=?&foo=?&foo=?",
"request_pathshape": "/about/:lang",
"request_queryshape": "baz=?&bend=?&foo=?&foo=?&foo=?",
},
}
urlsAllQuery := map[string]map[string]interface{}{
"/about/en?foo=bar&bar=bar2": {
"request_uri": "/about/en?foo=bar&bar=bar2",
"request_path": "/about/en",
"request_query": "foo=bar&bar=bar2",
"request_query_foo": "bar",
"request_query_bar": "bar2",
"request_path_lang": "en",
"request_shape": "/about/:lang?bar=?&foo=?",
"request_pathshape": "/about/:lang",
"request_queryshape": "bar=?&foo=?",
},
}
// test whitelisting keys foo, baz, and bend but not bar
opts.RequestQueryKeys = []string{"foo", "baz", "bend"}
tbs := make(chan event.Event)
output := modifyEventContents(tbs, opts)
for input, expectedResult := range urlsWhitelistQuery {
ev := event.Event{
Data: map[string]interface{}{
reqField: input,
},
}
// feed it the sample event
tbs <- ev
// get the munged event out
res := <-output
for evKey, expectedVal := range expectedResult {
assert.Equal(t, res.Data[evKey], expectedVal)
}
}
close(tbs)
// change the query parsing rules and get a new output channel - bar should be
// included
opts.RequestParseQuery = "all"
tbs = make(chan event.Event)
output = modifyEventContents(tbs, opts)
for input, expectedResult := range urlsAllQuery {
ev := event.Event{
Data: map[string]interface{}{
reqField: input,
},
}
// feed it the sample event
tbs <- ev
// get the munged event out
res := <-output
for evKey, expectedVal := range expectedResult {
assert.Equal(t, res.Data[evKey], expectedVal)
}
}
close(tbs)
}
func TestSampleRate(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
rand.Seed(1)
sampleLogFile := ts.tmpdir + "/sample.log"
logfh, _ := os.Create(sampleLogFile)
defer logfh.Close()
for i := 0; i < 50; i++ {
fmt.Fprintf(logfh, `{"format":"json%d"}`+"\n", i)
}
opts.Reqs.LogFiles = []string{sampleLogFile}
opts.TailSample = false
run(context.Background(), opts)
// with no sampling, 50 lines -> 50 events
assert.Equal(t, ts.rsp.evtCounter, 50)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json49"}`)
ts.rsp.reset()
opts.SampleRate = 3
opts.TailSample = true
run(context.Background(), opts)
// setting a sample rate of 3 gets 17 requests.
// tail does the sampling
assert.Equal(t, ts.rsp.evtCounter, 17)
assert.Contains(t, ts.rsp.reqBody, `{"format":"json49"},"samplerate":3,`)
}
func TestReadFromOffset(t *testing.T) {
opts := defaultOptions
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
offsetLogFile := ts.tmpdir + "/offset.log"
offsetStateFile := ts.tmpdir + "/offset.leash.state"
logfh, _ := os.Create(offsetLogFile)
defer logfh.Close()
logStat := unix.Stat_t{}
unix.Stat(offsetLogFile, &logStat)
for i := 0; i < 10; i++ {
fmt.Fprintf(logfh, `{"format":"json%d"}`+"\n", i)
}
opts.Reqs.LogFiles = []string{offsetLogFile}
opts.Tail.ReadFrom = "last"
opts.Tail.StateFile = offsetStateFile
osf, _ := os.Create(offsetStateFile)
defer osf.Close()
fmt.Fprintf(osf, `{"INode":%d,"Offset":38}`, logStat.Ino)
run(context.Background(), opts)
assert.Equal(t, ts.rsp.reqCounter, 1)
assert.Equal(t, ts.rsp.evtCounter, 8)
}
// TestLogRotation tests that honeytail continues tailing after log rotation,
// with different possible configurations:
// * when honeytail polls or uses inotify
// * when logs are rotated using rename/reopen, or using copy/truncate.
func TestLogRotation(t *testing.T) {
for _, poll := range []bool{true, false} {
for _, copyTruncate := range []bool{true, false} {
t.Run(fmt.Sprintf("polling: %t; copyTruncate: %t", poll, copyTruncate), func(t *testing.T) {
wg := &sync.WaitGroup{}
opts := defaultOptions
opts.BatchFrequencyMs = 1
opts.Tail.Stop = false
opts.Tail.Poll = poll
ts := &testSetup{}
ts.start(t, &opts)
defer ts.close()
logFileName := ts.tmpdir + "/test.log"
fh, err := os.Create(logFileName)
if err != nil {
t.Fatal(err)
}
opts.Reqs.LogFiles = []string{logFileName}
// Run honeytail in the background
ctx, cancel := context.WithCancel(context.Background())
wg.Add(1)
go func() {
run(ctx, opts)
wg.Done()
}()
// Write a line to the log file, and check that honeytail reads it.
fmt.Fprint(fh, "{\"key\":1}\n")
fh.Close()
sent := expectWithTimeout(func() bool { return ts.rsp.evtCounter == 1 }, time.Second)
assert.True(t, sent, "Failed to read first log line")
// Simulate log rotation
if copyTruncate {
err = exec.Command("cp", logFileName, ts.tmpdir+"/test.log.1").Run()
} else {
err = os.Rename(logFileName, ts.tmpdir+"/test.log.1")
}
assert.NoError(t, err)
// Older versions of the inotify implementation in
// github.com/hpcloud/tail would fail to reopen a log file
// after a rename/reopen (https://github.com/hpcloud/tail/pull/115),
// but this delay is necessary to provoke the issue. Don't know why.
time.Sleep(100 * time.Millisecond)
// Write lines to the new log file, and check that honeytail reads them.
fh, err = os.Create(logFileName)
assert.NoError(t, err)
fmt.Fprint(fh, "{\"key\":2}\n")
fmt.Fprint(fh, "{\"key\":3}\n")
fh.Close()
// TODO: when logs are rotated using copy/truncate, we lose the
// first line of the new log file.
sent = expectWithTimeout(func() bool { return ts.rsp.evtCounter >= 2 }, time.Second)
assert.True(t, sent, "Failed to read log lines after rotation")
// Stop honeytail.
cancel()
wg.Wait()
})
}
}
}
// boilerplate to spin up a httptest server, create tmpdir, etc.
// to create an environment in which to run these tests
type testSetup struct {
server *httptest.Server
rsp *responder
tmpdir string
}
func (t *testSetup) start(tst *testing.T, opts *GlobalOptions) {
logrus.SetOutput(ioutil.Discard)
t.rsp = &responder{}
t.server = httptest.NewServer(http.HandlerFunc(t.rsp.serveResponse))
tmpdir, err := ioutil.TempDir(os.TempDir(), "test")
if err != nil {
tst.Fatal(err)
}
t.tmpdir = tmpdir
opts.APIHost = t.server.URL
t.rsp.responseCode = 200
}
func (t *testSetup) close() {
t.server.Close()
os.RemoveAll(t.tmpdir)
}
type responder struct {
req *http.Request // the most recent request answered by the server
reqBody string // the body sent along with the request
reqCounter int // the number of requests answered since last reset
evtCounter int // the number of events (<= reqCounter, will be < if events are batched)
responseCode int // the http status code with which to respond
responseBody string // the body to send as the response
}
func (r *responder) serveResponse(w http.ResponseWriter, req *http.Request) {
r.req = req
r.reqCounter += 1
var reader io.ReadCloser
switch req.Header.Get("Content-Encoding") {
case "gzip":
buf := bytes.Buffer{}
if _, err := io.Copy(&buf, req.Body); err != nil {
panic(err)
}
gzReader, err := gzip.NewReader(&buf)
if err != nil {
panic(err)
}
req.Body.Close()
reader = gzReader
case "zstd":
zReader, _ := zstd.NewReader(
nil,
zstd.WithDecoderConcurrency(1),
zstd.WithDecoderLowmem(true),
zstd.WithDecoderMaxMemory(8*1024*1024),
)
zReader.Reset(req.Body)
reader = zReader.IOReadCloser()
default:
reader = req.Body
}
defer reader.Close()
body, err := ioutil.ReadAll(reader)
if err != nil {
panic(err)
}
payload := []map[string]interface{}{}
if len(body) > 0 {
if err := json.Unmarshal(body, &payload); err != nil {
r.evtCounter++ // likely not a batch request
} else {
r.evtCounter += len(payload)
}
}
r.reqBody = string(body)
w.WriteHeader(r.responseCode)
fmt.Fprintf(w, r.responseBody)
}
func (r *responder) reset() {
r.reqCounter = 0
r.evtCounter = 0
r.responseCode = 200
}
func expectWithTimeout(condition func() bool, timeout time.Duration) bool {
deadline := time.Now().Add(timeout)
for deadline.After(time.Now()) {
if condition() {
return true
}
}
return false
}
func TestGetEndLine(t *testing.T) {
fileContents := `
{"key1": "value1"}
{"key1": "value2"}
{"key1": "value3"}
{"key1": "END"}`
f, err := ioutil.TempFile(os.TempDir(), "honeytail-test")
assert.Nil(t, err, "failed to open temp file")
_, err = f.WriteString(fileContents)
assert.Nil(t, err, "failed to write temp file")
f.Close()
defer syscall.Unlink(f.Name())
line := getEndLine(f.Name())
assert.Equal(t, `{"key1": "END"}`, line)
}
func TestRebaseTime(t *testing.T) {
baseTime, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Wed Jul 3 15:04:05 -0700 PDT 2018")
assert.Nil(t, err)
nowTime, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Wed Jul 4 12:00:00 -0700 PDT 2018")
assert.Nil(t, err)
timestamp, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Wed Jul 3 12:00:05 -0700 PDT 2018")
assert.Nil(t, err)
// should be three hours, four minutes behind our nowTime
expected, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Wed Jul 4 08:56:00 -0700 PDT 2018")
assert.Nil(t, err)
rebasedTime := rebaseTime(baseTime, nowTime, timestamp)
assert.Equal(t, expected, rebasedTime)
}
func TestGetBaseTime(t *testing.T) {
fileContents := `
{"key1": "value1", "timestamp": "Wed Jul 3 12:00:05 -0700 PDT 2018"}
{"key1": "value2", "timestamp": "Wed Jul 3 13:00:05 -0700 PDT 2018"}
{"key1": "value3", "timestamp": "Wed Jul 3 14:00:05 -0700 PDT 2018"}
{"key1": "value4", "timestamp": "Wed Jul 3 15:04:05 -0700 PDT 2018"}
`
f, err := ioutil.TempFile(os.TempDir(), "honeytail-test")
assert.Nil(t, err, "failed to open temp file")
_, err = f.WriteString(fileContents)
assert.Nil(t, err, "failed to write temp file")
f.Close()
defer syscall.Unlink(f.Name())
options := GlobalOptions{
Reqs: RequiredOptions{
LogFiles: []string{f.Name()},
ParserName: "json",
},
JSON: htjson.Options{
TimeFieldFormat: "Mon Jan 2 15:04:05 -0700 MST 2006",
TimeFieldName: "timestamp",
},
}
expected, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Wed Jul 3 15:04:05 -0700 PDT 2018")
assert.Nil(t, err)
baseTime, err := getBaseTime(options)
assert.Nil(t, err)
assert.Equal(t, expected.UTC(), baseTime.UTC())
}