-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lambda-promtail: cloudwatch: add label tests
- Loading branch information
1 parent
bbddc73
commit e424e20
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/loki/pkg/logproto" | ||
) | ||
|
||
func Test_parseCWEvent(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
b *batch | ||
expectedStream string | ||
keepStream bool | ||
}{ | ||
{ | ||
name: "cloudwatch", | ||
b: &batch{ | ||
streams: map[string]*logproto.Stream{}, | ||
}, | ||
expectedStream: `{__aws_cloudwatch_log_group="testLogGroup", __aws_cloudwatch_owner="123456789123", __aws_log_type="cloudwatch"}`, | ||
keepStream: false, | ||
}, | ||
{ | ||
name: "cloudwatch_keepStream", | ||
b: &batch{ | ||
streams: map[string]*logproto.Stream{}, | ||
}, | ||
expectedStream: `{__aws_cloudwatch_log_group="testLogGroup", __aws_cloudwatch_log_stream="testLogStream", __aws_cloudwatch_owner="123456789123", __aws_log_type="cloudwatch"}`, | ||
keepStream: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
// https://github.com/aws/aws-lambda-go/blob/main/events/cloudwatch_logs_test.go | ||
cwevent := &events.CloudwatchLogsEvent{ | ||
AWSLogs: events.CloudwatchLogsRawData{ | ||
Data: "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUgXoLCdMhFtKV3akI8d0bLYmibvPPN3wz00CJxmQnTO41whwWQRIctmEcB6sQbFC3CjW3XW8kxpOpP+OC22d1Wml1qZkQGtoMsScxaczKN3plG8zlaHIta5KqWsozoTYw3/djzwhpLwivWFGHGpAFe7DL68JlBUk+l7KSN7tCOEJ4M3/qOI49vMHj+zCKdlFqLaU2ZHV2a4Ct/an0/ivdX8oYc1UVX860fQDQiMdxRQEAAA==", | ||
}, | ||
} | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
batchSize = 131072 // Set large enough we don't send to promtail | ||
keepStream = tt.keepStream | ||
err := parseCWEvent(context.Background(), tt.b, cwevent) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
require.Len(t, tt.b.streams, 1) | ||
stream, ok := tt.b.streams[tt.expectedStream] | ||
require.True(t, ok, "batch does not contain stream: %s", tt.expectedStream) | ||
require.NotNil(t, stream) | ||
}) | ||
} | ||
} |