forked from mec07/cloudwatchwriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudwatch_writer_test.go
525 lines (427 loc) · 13.6 KB
/
cloudwatch_writer_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
package cloudwatchwriter_test
import (
"context"
"encoding/json"
"fmt"
"io"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/tracmo/cloudwatchwriter"
)
const sequenceToken = "next-sequence-token"
type mockClient struct {
sync.RWMutex
putLogEventsShouldError bool
logEvents []types.InputLogEvent
logGroupName *string
logStreamName *string
expectedSequenceToken *string
}
func (c *mockClient) DescribeLogStreams(ctx context.Context, params *cloudwatchlogs.DescribeLogStreamsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogStreamsOutput, error) {
c.RLock()
defer c.RUnlock()
if c.logGroupName == nil {
return nil, errors.Wrap(&types.ResourceNotFoundException{}, "blah")
}
var streams []types.LogStream
if c.logStreamName != nil {
streams = append(streams, types.LogStream{
LogStreamName: c.logStreamName,
UploadSequenceToken: c.expectedSequenceToken,
})
}
return &cloudwatchlogs.DescribeLogStreamsOutput{
LogStreams: streams,
}, nil
}
func (c *mockClient) CreateLogGroup(ctx context.Context, input *cloudwatchlogs.CreateLogGroupInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.CreateLogGroupOutput, error) {
c.Lock()
defer c.Unlock()
c.logGroupName = input.LogGroupName
return nil, nil
}
func (c *mockClient) CreateLogStream(ctx context.Context, input *cloudwatchlogs.CreateLogStreamInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.CreateLogStreamOutput, error) {
c.Lock()
defer c.Unlock()
c.logStreamName = input.LogStreamName
return nil, nil
}
func (c *mockClient) PutLogEvents(ctx context.Context, putLogEvents *cloudwatchlogs.PutLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.PutLogEventsOutput, error) {
c.Lock()
defer c.Unlock()
if c.putLogEventsShouldError {
return nil, errors.New("should error")
}
if putLogEvents == nil {
return nil, errors.New("received nil *cloudwatchlogs.PutLogEventsInput")
}
// At the first PutLogEvents c.expectedSequenceToken should be nil, as we
// set it in this call. If it is not nil then we can compare the received
// sequence token and the expected one.
if c.expectedSequenceToken != nil {
if putLogEvents.SequenceToken == nil || *putLogEvents.SequenceToken != *c.expectedSequenceToken {
return nil, &types.InvalidSequenceTokenException{
ExpectedSequenceToken: c.expectedSequenceToken,
}
}
} else {
c.expectedSequenceToken = aws.String(sequenceToken)
}
c.logEvents = append(c.logEvents, putLogEvents.LogEvents...)
output := &cloudwatchlogs.PutLogEventsOutput{
NextSequenceToken: c.expectedSequenceToken,
}
return output, nil
}
func (c *mockClient) getLogEvents() []types.InputLogEvent {
c.RLock()
defer c.RUnlock()
logEvents := make([]types.InputLogEvent, len(c.logEvents))
copy(logEvents, c.logEvents)
return logEvents
}
func (c *mockClient) setExpectedSequenceToken(token *string) {
c.Lock()
defer c.Unlock()
c.expectedSequenceToken = token
}
func (c *mockClient) waitForLogs(numberOfLogs int, timeout time.Duration) error {
endTime := time.Now().Add(timeout)
for {
if c.numLogs() >= numberOfLogs {
return nil
}
if time.Now().After(endTime) {
return errors.New("ran out of time waiting for logs")
}
time.Sleep(time.Millisecond)
}
}
func (c *mockClient) numLogs() int {
c.RLock()
defer c.RUnlock()
return len(c.logEvents)
}
type exampleLog struct {
Time, Message, Filename string
Port uint16
}
type logsContainer struct {
sync.Mutex
logs []exampleLog
}
func (l *logsContainer) addLog(log exampleLog) {
l.Lock()
defer l.Unlock()
l.logs = append(l.logs, log)
}
func (l *logsContainer) getLogEvents() ([]types.InputLogEvent, error) {
l.Lock()
defer l.Unlock()
var logEvents []types.InputLogEvent
for _, log := range l.logs {
message, err := json.Marshal(log)
if err != nil {
return nil, errors.Wrap(err, "json.Marshal")
}
logEvents = append(logEvents, types.InputLogEvent{
Message: aws.String(string(message)),
// Timestamps for CloudWatch Logs should be in milliseconds since the epoch.
Timestamp: aws.Int64(time.Now().UTC().UnixNano() / int64(time.Millisecond)),
})
}
return logEvents, nil
}
func helperWriteLogs(t *testing.T, writer io.Writer, logs ...interface{}) {
for _, log := range logs {
message, err := json.Marshal(log)
if err != nil {
t.Fatalf("json.Marshal: %v", err)
}
_, err = writer.Write(message)
if err != nil {
t.Fatalf("writer.Write(%s): %v", string(message), err)
}
}
}
// assertEqualLogMessages asserts that the log messages are all the same, ignoring the timestamps.
func assertEqualLogMessages(t *testing.T, expectedLogs []types.InputLogEvent, logs []types.InputLogEvent) {
if !assert.Equal(t, len(expectedLogs), len(logs), "expected to have the same number of logs") {
return
}
for index, log := range logs {
assert.Equal(t, *expectedLogs[index].Message, *log.Message)
}
}
func TestCloudWatchWriter(t *testing.T) {
client := &mockClient{}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
// give the queueMonitor goroutine time to start up
time.Sleep(time.Millisecond)
log1 := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message 1",
Filename: "filename",
Port: 666,
}
log2 := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message 2",
Filename: "filename",
Port: 666,
}
log3 := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message 3",
Filename: "filename",
Port: 666,
}
log4 := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message 4",
Filename: "filename",
Port: 666,
}
helperWriteLogs(t, cloudWatchWriter, log1, log2, log3, log4)
logs := logsContainer{}
logs.addLog(log1)
logs.addLog(log2)
logs.addLog(log3)
logs.addLog(log4)
expectedLogs, err := logs.getLogEvents()
if err != nil {
t.Fatal(err)
}
if err = client.waitForLogs(2, 201*time.Millisecond); err != nil {
t.Fatal(err)
}
assertEqualLogMessages(t, expectedLogs, client.getLogEvents())
}
func TestCloudWatchWriterBatchInterval(t *testing.T) {
client := &mockClient{}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 5*time.Second, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
// can't set anything below 200 milliseconds
err = cloudWatchWriter.SetBatchInterval(199 * time.Millisecond)
if err == nil {
t.Fatal("expected an error")
}
// setting it to a value greater than or equal to 200 is OK
err = cloudWatchWriter.SetBatchInterval(200 * time.Millisecond)
if err != nil {
t.Fatalf("CloudWatchWriter.SetBatchInterval: %v", err)
}
// give the queueMonitor goroutine time to start up
time.Sleep(time.Millisecond)
aLog := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message",
Filename: "filename",
Port: 666,
}
helperWriteLogs(t, cloudWatchWriter, aLog)
// The client shouldn't have received any logs at this time
assert.Equal(t, 0, client.numLogs())
// Still no logs after 100 milliseconds
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 0, client.numLogs())
// The client should have received the log after another 101 milliseconds
// (as that is a total sleep time of 201 milliseconds)
time.Sleep(101 * time.Millisecond)
assert.Equal(t, 1, client.numLogs())
}
// Hit the 1MB limit on batch size of logs to trigger an earlier batch
func TestCloudWatchWriterHit1MBLimit(t *testing.T) {
client := &mockClient{}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
// give the queueMonitor goroutine time to start up
time.Sleep(time.Millisecond)
logs := logsContainer{}
numLogs := 9999
for i := 0; i < numLogs; i++ {
aLog := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: fmt.Sprintf("longggggggggggggggggggggggggggg test message %d", i),
Filename: "/home/deadpool/src/github.com/superlongfilenameblahblahblahblah.txt",
Port: 666,
}
helperWriteLogs(t, cloudWatchWriter, aLog)
logs.addLog(aLog)
}
// Main assertion is that we are triggering a batch early as we're sending
// so much data
assert.True(t, client.numLogs() > 0)
if err = client.waitForLogs(numLogs, 400*time.Millisecond); err != nil {
t.Fatal(err)
}
expectedLogs, err := logs.getLogEvents()
if err != nil {
t.Fatal(err)
}
assertEqualLogMessages(t, expectedLogs, client.getLogEvents())
}
// Hit the 10k limit on number of logs to trigger an earlier batch
func TestCloudWatchWriterHit10kLimit(t *testing.T) {
client := &mockClient{}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
// give the queueMonitor goroutine time to start up
time.Sleep(time.Millisecond)
var expectedLogs []types.InputLogEvent
numLogs := 10000
for i := 0; i < numLogs; i++ {
message := fmt.Sprintf("hello %d", i)
_, err = cloudWatchWriter.Write([]byte(message))
if err != nil {
t.Fatalf("cloudWatchWriter.Write: %v", err)
}
expectedLogs = append(expectedLogs, types.InputLogEvent{
Message: aws.String(message),
Timestamp: aws.Int64(time.Now().UTC().UnixNano() / int64(time.Millisecond)),
})
}
// give the queueMonitor goroutine time to catch-up (sleep is far less than
// the minimum of 200 milliseconds)
time.Sleep(10 * time.Millisecond)
// Main assertion is that we are triggering a batch early as we're sending
// so many logs
assert.True(t, client.numLogs() > 0)
if err = client.waitForLogs(numLogs, 200*time.Millisecond); err != nil {
t.Fatal(err)
}
assertEqualLogMessages(t, expectedLogs, client.getLogEvents())
}
func TestCloudWatchWriterParallel(t *testing.T) {
client := &mockClient{}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
logs := logsContainer{}
numLogs := 8000
for i := 0; i < numLogs; i++ {
go func() {
aLog := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message",
Filename: "filename",
Port: 666,
}
logs.addLog(aLog)
helperWriteLogs(t, cloudWatchWriter, aLog)
}()
}
// allow more time as there are a lot of goroutines to set off!
if err = client.waitForLogs(numLogs, 4*time.Second); err != nil {
t.Fatal(err)
}
expectedLogs, err := logs.getLogEvents()
if err != nil {
t.Fatal(err)
}
assertEqualLogMessages(t, expectedLogs, client.getLogEvents())
}
func TestCloudWatchWriterClose(t *testing.T) {
client := &mockClient{}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
numLogs := 99
for i := 0; i < numLogs; i++ {
aLog := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: fmt.Sprintf("Test message %d", i),
Filename: "filename",
Port: 666,
}
helperWriteLogs(t, cloudWatchWriter, aLog)
}
// The logs shouldn't have come through yet
assert.Equal(t, 0, client.numLogs())
// Close should block until the queue is empty
cloudWatchWriter.Close()
// The logs should have all come through now
assert.Equal(t, numLogs, client.numLogs())
}
func TestCloudWatchWriterReportError(t *testing.T) {
client := &mockClient{
putLogEventsShouldError: true,
}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
// give the queueMonitor goroutine time to start up
time.Sleep(time.Millisecond)
log1 := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message 1",
Filename: "filename",
Port: 666,
}
helperWriteLogs(t, cloudWatchWriter, log1)
// sleep until the batch should have been sent
time.Sleep(201 * time.Millisecond)
_, err = cloudWatchWriter.Write([]byte("hello world"))
if err == nil {
t.Fatal("expected the last error from PutLogEvents to appear here")
}
}
func TestCloudWatchWriterReceiveInvalidSequenceTokenException(t *testing.T) {
// Setup
client := &mockClient{}
cloudWatchWriter, err := cloudwatchwriter.NewWithClient(client, 200*time.Millisecond, "logGroup", "logStream")
if err != nil {
t.Fatalf("NewWithClient: %v", err)
}
defer cloudWatchWriter.Close()
// give the queueMonitor goroutine time to start up
time.Sleep(time.Millisecond)
// At this point the cloudWatchWriter should have the normal sequence token.
// So we change the mock cloudwatch client to expect a different sequence
// token:
client.setExpectedSequenceToken(aws.String("new sequence token"))
// Action
logs := logsContainer{}
log := exampleLog{
Time: "2009-11-10T23:00:02.043123061Z",
Message: "Test message 1",
Filename: "filename",
Port: 666,
}
helperWriteLogs(t, cloudWatchWriter, log)
logs.addLog(log)
// Result
if err = client.waitForLogs(1, 201*time.Millisecond); err != nil {
t.Fatal(err)
}
expectedLogs, err := logs.getLogEvents()
if err != nil {
t.Fatal(err)
}
assertEqualLogMessages(t, expectedLogs, client.getLogEvents())
}