forked from aws/aws-lambda-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudwatch_logs_test.go
100 lines (88 loc) · 2.74 KB
/
cloudwatch_logs_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
package events
import (
"encoding/json"
"reflect"
"testing"
tst "github.com/aws/aws-lambda-go/events/test"
)
func TestCloudwatchLogs(t *testing.T) {
for _, test := range []struct {
name string
eventJSON string
expectError bool
expectCloudwatchEventData CloudwatchLogsEvent
}{
{"Well formed cloudwatch event",
"./testdata/cloudwatch-logs-event.json",
false,
CloudwatchLogsEvent{
AWSLogs: CloudwatchLogsRawData{
Data: "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUgXoLCdMhFtKV3akI8d0bLYmibvPPN3wz00CJxmQnTO41whwWQRIctmEcB6sQbFC3CjW3XW8kxpOpP+OC22d1Wml1qZkQGtoMsScxaczKN3plG8zlaHIta5KqWsozoTYw3/djzwhpLwivWFGHGpAFe7DL68JlBUk+l7KSN7tCOEJ4M3/qOI49vMHj+zCKdlFqLaU2ZHV2a4Ct/an0/ivdX8oYc1UVX860fQDQiMdxRQEAAA==",
},
},
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
inputJSON := tst.ReadJSONFromFile(t, test.eventJSON)
var inputEvent CloudwatchLogsEvent
err := json.Unmarshal(inputJSON, &inputEvent)
if err != nil && !test.expectError {
t.Errorf("could not unmarshal event. details: %v", err)
}
if err == nil && test.expectError {
t.Errorf("expected parse error")
}
if !reflect.DeepEqual(test.expectCloudwatchEventData, inputEvent) {
t.Errorf("expected: %+v, received: %v", test.expectCloudwatchEventData, inputEvent)
}
})
}
}
func TestCloudwatchLogsParse(t *testing.T) {
for _, test := range []struct {
name string
eventJSON string
expectError bool
expectCloudwatchLogsData CloudwatchLogsData
}{
{"Well formed cloudwatch event",
"./testdata/cloudwatch-logs-event.json",
false,
CloudwatchLogsData{
Owner: "123456789123",
LogGroup: "testLogGroup",
LogStream: "testLogStream",
SubscriptionFilters: []string{
"testFilter",
},
MessageType: "DATA_MESSAGE",
LogEvents: []CloudwatchLogsLogEvent{
{ID: "eventId1", Timestamp: 1440442987000, Message: "[ERROR] First test message"},
{ID: "eventId2", Timestamp: 1440442987001, Message: "[ERROR], Second test message"},
},
},
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
inputJSON := tst.ReadJSONFromFile(t, test.eventJSON)
var inputEvent CloudwatchLogsEvent
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
d, err := inputEvent.AWSLogs.Parse()
if err != nil {
if !test.expectError {
t.Errorf("unexpected error: %+v", err)
}
if !reflect.DeepEqual(test.expectCloudwatchLogsData, d) {
t.Errorf("expected: %+v, received: %v", test.expectCloudwatchLogsData, d)
}
}
if err == nil && test.expectError {
t.Errorf("expected error")
}
})
}
}