github.com/Foodji/aws-lambda-go@v1.20.2/events/cloudwatch_logs_test.go (about) 1 package events 2 3 import ( 4 "encoding/json" 5 "reflect" 6 "testing" 7 8 tst "github.com/aws/aws-lambda-go/events/test" 9 ) 10 11 func TestCloudwatchLogs(t *testing.T) { 12 for _, test := range []struct { 13 name string 14 eventJson string 15 expectError bool 16 expectCloudwatchEventData CloudwatchLogsEvent 17 }{ 18 {"Well formed cloudwatch event", 19 "./testdata/cloudwatch-logs-event.json", 20 false, 21 CloudwatchLogsEvent{ 22 AWSLogs: CloudwatchLogsRawData{ 23 Data: "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUgXoLCdMhFtKV3akI8d0bLYmibvPPN3wz00CJxmQnTO41whwWQRIctmEcB6sQbFC3CjW3XW8kxpOpP+OC22d1Wml1qZkQGtoMsScxaczKN3plG8zlaHIta5KqWsozoTYw3/djzwhpLwivWFGHGpAFe7DL68JlBUk+l7KSN7tCOEJ4M3/qOI49vMHj+zCKdlFqLaU2ZHV2a4Ct/an0/ivdX8oYc1UVX860fQDQiMdxRQEAAA==", 24 }, 25 }, 26 }, 27 } { 28 test := test 29 t.Run(test.name, func(t *testing.T) { 30 inputJson := tst.ReadJSONFromFile(t, test.eventJson) 31 32 var inputEvent CloudwatchLogsEvent 33 err := json.Unmarshal(inputJson, &inputEvent) 34 35 if err != nil && !test.expectError { 36 t.Errorf("could not unmarshal event. details: %v", err) 37 } 38 39 if err == nil && test.expectError { 40 t.Errorf("expected parse error") 41 } 42 43 if !reflect.DeepEqual(test.expectCloudwatchEventData, inputEvent) { 44 t.Errorf("expected: %+v, received: %v", test.expectCloudwatchEventData, inputEvent) 45 } 46 }) 47 } 48 } 49 50 func TestCloudwatchLogsParse(t *testing.T) { 51 for _, test := range []struct { 52 name string 53 eventJson string 54 expectError bool 55 expectCloudwatchLogsData CloudwatchLogsData 56 }{ 57 {"Well formed cloudwatch event", 58 "./testdata/cloudwatch-logs-event.json", 59 false, 60 CloudwatchLogsData{ 61 Owner: "123456789123", 62 LogGroup: "testLogGroup", 63 LogStream: "testLogStream", 64 SubscriptionFilters: []string{ 65 "testFilter", 66 }, 67 MessageType: "DATA_MESSAGE", 68 LogEvents: []CloudwatchLogsLogEvent{ 69 {ID: "eventId1", Timestamp: 1440442987000, Message: "[ERROR] First test message"}, 70 {ID: "eventId2", Timestamp: 1440442987001, Message: "[ERROR], Second test message"}, 71 }, 72 }, 73 }, 74 } { 75 test := test 76 t.Run(test.name, func(t *testing.T) { 77 inputJson := tst.ReadJSONFromFile(t, test.eventJson) 78 79 var inputEvent CloudwatchLogsEvent 80 if err := json.Unmarshal(inputJson, &inputEvent); err != nil { 81 t.Errorf("could not unmarshal event. details: %v", err) 82 } 83 84 d, err := inputEvent.AWSLogs.Parse() 85 if err != nil { 86 if !test.expectError { 87 t.Errorf("unexpected error: %+v", err) 88 } 89 90 if !reflect.DeepEqual(test.expectCloudwatchLogsData, d) { 91 t.Errorf("expected: %+v, received: %v", test.expectCloudwatchLogsData, d) 92 } 93 } 94 95 if err == nil && test.expectError { 96 t.Errorf("expected error") 97 } 98 }) 99 } 100 }