-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2be95a
commit 9e0d7ac
Showing
6 changed files
with
444 additions
and
17 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
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
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,30 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"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" | ||
) | ||
|
||
type cloudwatchLogsClientAPI interface { | ||
GetLogEvents(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) | ||
} | ||
|
||
type LogDetails struct { | ||
logGroupName string | ||
logStreamName string | ||
} | ||
|
||
func RetrieveLogs(ctx context.Context, cloudwatchLogsClientAPI cloudwatchLogsClientAPI, loggingDetails LogDetails) ([]types.OutputLogEvent, error) { | ||
response, err := cloudwatchLogsClientAPI.GetLogEvents(ctx, &cloudwatchlogs.GetLogEventsInput{ | ||
LogStreamName: &loggingDetails.logStreamName, | ||
LogGroupName: &loggingDetails.logGroupName, | ||
StartFromHead: aws.Bool(true), | ||
}) | ||
if err != nil { | ||
return []types.OutputLogEvent{}, err | ||
} | ||
return response.Events, nil | ||
} |
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,99 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"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/stretchr/testify/assert" | ||
) | ||
|
||
type mockCloudwatchLogsClient struct { | ||
mockGetLogEvents func(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) | ||
} | ||
|
||
func (m mockCloudwatchLogsClient) GetLogEvents(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) { | ||
return m.mockGetLogEvents(ctx, params, optFns...) | ||
} | ||
|
||
func TestRetrieveLogs(t *testing.T) { | ||
input := LogDetails{ | ||
logGroupName: "test-group", | ||
logStreamName: "test-stream/test-container/07cc583696bd44e0be450bff7314ddaf", | ||
} | ||
|
||
events := []types.OutputLogEvent{ | ||
{ | ||
IngestionTime: aws.Int64(0), | ||
Message: aws.String("beans have been detected in the system"), | ||
Timestamp: aws.Int64(0), | ||
}, | ||
{ | ||
IngestionTime: aws.Int64(1), | ||
Message: aws.String("beans have been removed from the system"), | ||
Timestamp: aws.Int64(1), | ||
}, | ||
} | ||
|
||
positiveTests := []struct { | ||
name string | ||
input LogDetails | ||
client mockCloudwatchLogsClient | ||
expected []types.OutputLogEvent | ||
}{ | ||
{ | ||
// This is a regression test to ensure the function signature remains the same | ||
name: "given a valid LogDetails input, return the events of the log stream", | ||
input: input, | ||
client: mockCloudwatchLogsClient{ | ||
mockGetLogEvents: func(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) { | ||
return &cloudwatchlogs.GetLogEventsOutput{Events: events}, nil | ||
}, | ||
}, | ||
expected: events, | ||
}, | ||
} | ||
|
||
for _, tc := range positiveTests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := RetrieveLogs(context.TODO(), tc.client, tc.input) | ||
|
||
t.Logf("result: %v", result) | ||
t.Logf("expected: %v", tc.expected) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, result) | ||
}) | ||
} | ||
|
||
negativeTests := []struct { | ||
name string | ||
input LogDetails | ||
client mockCloudwatchLogsClient | ||
expected []types.OutputLogEvent | ||
}{ | ||
{ | ||
name: "when the underlying cloudwatch client experiences an error, return it in the function ", | ||
input: input, | ||
client: mockCloudwatchLogsClient{ | ||
mockGetLogEvents: func(ctx context.Context, params *cloudwatchlogs.GetLogEventsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.GetLogEventsOutput, error) { | ||
return &cloudwatchlogs.GetLogEventsOutput{}, errors.New("generic cloudwatch error") | ||
}, | ||
}, | ||
expected: []types.OutputLogEvent{}, | ||
}, | ||
} | ||
|
||
for _, tc := range negativeTests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := RetrieveLogs(context.TODO(), tc.client, tc.input) | ||
|
||
t.Logf("result: %v", result) | ||
t.Logf("expected: %v", tc.expected) | ||
assert.Error(t, err) | ||
assert.Equal(t, tc.expected, result) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.