mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #43139 from samuelkarp/awslogs-tests
awslogs: replace channel-based mocks
This commit is contained in:
commit
bf051447b9
2 changed files with 346 additions and 424 deletions
File diff suppressed because it is too large
Load diff
|
@ -6,75 +6,29 @@ import (
|
||||||
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockcwlogsclient struct {
|
type mockClient struct {
|
||||||
createLogGroupArgument chan *cloudwatchlogs.CreateLogGroupInput
|
createLogGroupFunc func(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error)
|
||||||
createLogGroupResult chan *createLogGroupResult
|
createLogStreamFunc func(input *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error)
|
||||||
createLogStreamArgument chan *cloudwatchlogs.CreateLogStreamInput
|
putLogEventsFunc func(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error)
|
||||||
createLogStreamResult chan *createLogStreamResult
|
|
||||||
putLogEventsArgument chan *cloudwatchlogs.PutLogEventsInput
|
|
||||||
putLogEventsResult chan *putLogEventsResult
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type createLogGroupResult struct {
|
func (m *mockClient) CreateLogGroup(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error) {
|
||||||
successResult *cloudwatchlogs.CreateLogGroupOutput
|
return m.createLogGroupFunc(input)
|
||||||
errorResult error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type createLogStreamResult struct {
|
func (m *mockClient) CreateLogStream(input *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error) {
|
||||||
successResult *cloudwatchlogs.CreateLogStreamOutput
|
return m.createLogStreamFunc(input)
|
||||||
errorResult error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type putLogEventsResult struct {
|
func (m *mockClient) PutLogEvents(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) {
|
||||||
successResult *cloudwatchlogs.PutLogEventsOutput
|
if err := checkPutLogEventsConstraints(input); err != nil {
|
||||||
errorResult error
|
return nil, err
|
||||||
|
}
|
||||||
|
return m.putLogEventsFunc(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMockClient() *mockcwlogsclient {
|
func checkPutLogEventsConstraints(input *cloudwatchlogs.PutLogEventsInput) error {
|
||||||
return &mockcwlogsclient{
|
events := input.LogEvents
|
||||||
createLogGroupArgument: make(chan *cloudwatchlogs.CreateLogGroupInput, 1),
|
|
||||||
createLogGroupResult: make(chan *createLogGroupResult, 1),
|
|
||||||
createLogStreamArgument: make(chan *cloudwatchlogs.CreateLogStreamInput, 1),
|
|
||||||
createLogStreamResult: make(chan *createLogStreamResult, 1),
|
|
||||||
putLogEventsArgument: make(chan *cloudwatchlogs.PutLogEventsInput, 1),
|
|
||||||
putLogEventsResult: make(chan *putLogEventsResult, 1),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMockClientBuffered(buflen int) *mockcwlogsclient {
|
|
||||||
return &mockcwlogsclient{
|
|
||||||
createLogStreamArgument: make(chan *cloudwatchlogs.CreateLogStreamInput, buflen),
|
|
||||||
createLogStreamResult: make(chan *createLogStreamResult, buflen),
|
|
||||||
putLogEventsArgument: make(chan *cloudwatchlogs.PutLogEventsInput, buflen),
|
|
||||||
putLogEventsResult: make(chan *putLogEventsResult, buflen),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockcwlogsclient) CreateLogGroup(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error) {
|
|
||||||
m.createLogGroupArgument <- input
|
|
||||||
output := <-m.createLogGroupResult
|
|
||||||
return output.successResult, output.errorResult
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockcwlogsclient) CreateLogStream(input *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error) {
|
|
||||||
m.createLogStreamArgument <- input
|
|
||||||
output := <-m.createLogStreamResult
|
|
||||||
return output.successResult, output.errorResult
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockcwlogsclient) PutLogEvents(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) {
|
|
||||||
events := make([]*cloudwatchlogs.InputLogEvent, len(input.LogEvents))
|
|
||||||
copy(events, input.LogEvents)
|
|
||||||
m.putLogEventsArgument <- &cloudwatchlogs.PutLogEventsInput{
|
|
||||||
LogEvents: events,
|
|
||||||
SequenceToken: input.SequenceToken,
|
|
||||||
LogGroupName: input.LogGroupName,
|
|
||||||
LogStreamName: input.LogStreamName,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Intended mock output
|
|
||||||
output := <-m.putLogEventsResult
|
|
||||||
|
|
||||||
// Checked enforced limits in mock
|
// Checked enforced limits in mock
|
||||||
totalBytes := 0
|
totalBytes := 0
|
||||||
for _, evt := range events {
|
for _, evt := range events {
|
||||||
|
@ -84,7 +38,7 @@ func (m *mockcwlogsclient) PutLogEvents(input *cloudwatchlogs.PutLogEventsInput)
|
||||||
eventBytes := len([]byte(*evt.Message))
|
eventBytes := len([]byte(*evt.Message))
|
||||||
if eventBytes > maximumBytesPerEvent {
|
if eventBytes > maximumBytesPerEvent {
|
||||||
// exceeded per event message size limits
|
// exceeded per event message size limits
|
||||||
return nil, fmt.Errorf("maximum bytes per event exceeded: Event too large %d, max allowed: %d", eventBytes, maximumBytesPerEvent)
|
return fmt.Errorf("maximum bytes per event exceeded: Event too large %d, max allowed: %d", eventBytes, maximumBytesPerEvent)
|
||||||
}
|
}
|
||||||
// total event bytes including overhead
|
// total event bytes including overhead
|
||||||
totalBytes += eventBytes + perEventBytes
|
totalBytes += eventBytes + perEventBytes
|
||||||
|
@ -92,10 +46,9 @@ func (m *mockcwlogsclient) PutLogEvents(input *cloudwatchlogs.PutLogEventsInput)
|
||||||
|
|
||||||
if totalBytes > maximumBytesPerPut {
|
if totalBytes > maximumBytesPerPut {
|
||||||
// exceeded per put maximum size limit
|
// exceeded per put maximum size limit
|
||||||
return nil, fmt.Errorf("maximum bytes per put exceeded: Upload too large %d, max allowed: %d", totalBytes, maximumBytesPerPut)
|
return fmt.Errorf("maximum bytes per put exceeded: Upload too large %d, max allowed: %d", totalBytes, maximumBytesPerPut)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return output.successResult, output.errorResult
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockmetadataclient struct {
|
type mockmetadataclient struct {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue