|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2026-Present Datadog, Inc. |
| 5 | + |
| 6 | +package handling |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring_go/internal/config" |
| 13 | + "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring_go/internal/model" |
| 14 | + "github.com/DataDog/datadog-serverless-functions/aws/logs_monitoring_go/internal/testutil" |
| 15 | + "github.com/google/go-cmp/cmp" |
| 16 | +) |
| 17 | + |
| 18 | +func TestEventBridgeHandler_Handle(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + ctx := testutil.LambdaContext(t) |
| 22 | + |
| 23 | + tests := map[string]struct { |
| 24 | + event json.RawMessage |
| 25 | + cfg *config.Config |
| 26 | + want []model.LogEntry |
| 27 | + wantErr bool |
| 28 | + }{ |
| 29 | + "scheduled event": { |
| 30 | + event: json.RawMessage(`{"version":"0","id":"abc","detail-type":"Scheduled Event","source":"aws.events","account":"123456789012","time":"1970-01-01T00:00:00Z","region":"us-east-1","resources":[],"detail":{}}`), |
| 31 | + cfg: testutil.EmptyConfig(), |
| 32 | + want: []model.LogEntry{ |
| 33 | + { |
| 34 | + Message: `{"version":"0","id":"abc","detail-type":"Scheduled Event","source":"aws.events","account":"123456789012","time":"1970-01-01T00:00:00Z","region":"us-east-1","resources":[],"detail":{}}`, |
| 35 | + Source: "events", |
| 36 | + SourceCategory: "aws", |
| 37 | + Service: "events", |
| 38 | + Metadata: testutil.LambdaOrigin(), |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + "ec2 event": { |
| 43 | + event: json.RawMessage(`{"version":"0","id":"abc","detail-type":"EC2 Instance State-change Notification","source":"aws.ec2","account":"123456789012","time":"1970-01-01T00:00:00Z","region":"us-east-1","resources":[],"detail":{"instance-id":"i-123","state":"running"}}`), |
| 44 | + cfg: testutil.EmptyConfig(), |
| 45 | + want: []model.LogEntry{ |
| 46 | + { |
| 47 | + Message: `{"version":"0","id":"abc","detail-type":"EC2 Instance State-change Notification","source":"aws.ec2","account":"123456789012","time":"1970-01-01T00:00:00Z","region":"us-east-1","resources":[],"detail":{"instance-id":"i-123","state":"running"}}`, |
| 48 | + Source: "ec2", |
| 49 | + SourceCategory: "aws", |
| 50 | + Service: "ec2", |
| 51 | + Metadata: testutil.LambdaOrigin(), |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + "custom source override": { |
| 56 | + event: json.RawMessage(`{"version":"0","id":"abc","detail-type":"Scheduled Event","source":"aws.events","account":"123456789012","time":"1970-01-01T00:00:00Z","region":"us-east-1","resources":[],"detail":{}}`), |
| 57 | + cfg: &config.Config{Source: "custom-source"}, |
| 58 | + want: []model.LogEntry{ |
| 59 | + { |
| 60 | + Message: `{"version":"0","id":"abc","detail-type":"Scheduled Event","source":"aws.events","account":"123456789012","time":"1970-01-01T00:00:00Z","region":"us-east-1","resources":[],"detail":{}}`, |
| 61 | + Source: "custom-source", |
| 62 | + SourceCategory: "aws", |
| 63 | + Service: "custom-source", |
| 64 | + Metadata: testutil.LambdaOrigin(), |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + "invalid JSON": { |
| 69 | + event: json.RawMessage(`not json`), |
| 70 | + cfg: testutil.EmptyConfig(), |
| 71 | + wantErr: true, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for name, tc := range tests { |
| 76 | + t.Run(name, func(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + |
| 79 | + handler := NewEventBridge(tc.cfg) |
| 80 | + out := make(chan model.LogEntry, len(tc.want)) |
| 81 | + |
| 82 | + err := handler.Handle(ctx, tc.event, out) |
| 83 | + close(out) |
| 84 | + |
| 85 | + if tc.wantErr { |
| 86 | + if err == nil { |
| 87 | + t.Fatal("expected error, got nil") |
| 88 | + } |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("unexpected error: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + var got []model.LogEntry |
| 97 | + for entry := range out { |
| 98 | + got = append(got, entry) |
| 99 | + } |
| 100 | + |
| 101 | + if diff := cmp.Diff(tc.want, got); diff != "" { |
| 102 | + t.Errorf("entries mismatch (-want +got):\n%s", diff) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestEventBridgeSource(t *testing.T) { |
| 109 | + t.Parallel() |
| 110 | + |
| 111 | + tests := map[string]struct { |
| 112 | + source string |
| 113 | + want string |
| 114 | + }{ |
| 115 | + "aws.events": {source: "aws.events", want: "events"}, |
| 116 | + "aws.ec2": {source: "aws.ec2", want: "ec2"}, |
| 117 | + "aws.s3": {source: "aws.s3", want: "s3"}, |
| 118 | + "custom.app": {source: "custom.app", want: "app"}, |
| 119 | + "no dot": {source: "nodot", want: "cloudwatch"}, |
| 120 | + "empty string": {source: "", want: "cloudwatch"}, |
| 121 | + } |
| 122 | + |
| 123 | + for name, tc := range tests { |
| 124 | + t.Run(name, func(t *testing.T) { |
| 125 | + t.Parallel() |
| 126 | + got := eventBridgeSource(tc.source) |
| 127 | + if got != tc.want { |
| 128 | + t.Errorf("got %q, want %q", got, tc.want) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments