2018-02-05 16:05:59 -05:00
|
|
|
package awslogs // import "github.com/docker/docker/daemon/logger/awslogs"
|
2015-08-04 20:35:06 -04:00
|
|
|
|
2017-11-30 19:17:17 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
|
|
|
)
|
2015-08-04 20:35:06 -04:00
|
|
|
|
|
|
|
type mockcwlogsclient struct {
|
2016-12-17 17:36:41 -05:00
|
|
|
createLogGroupArgument chan *cloudwatchlogs.CreateLogGroupInput
|
|
|
|
createLogGroupResult chan *createLogGroupResult
|
2015-08-04 20:35:06 -04:00
|
|
|
createLogStreamArgument chan *cloudwatchlogs.CreateLogStreamInput
|
|
|
|
createLogStreamResult chan *createLogStreamResult
|
|
|
|
putLogEventsArgument chan *cloudwatchlogs.PutLogEventsInput
|
|
|
|
putLogEventsResult chan *putLogEventsResult
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:36:41 -05:00
|
|
|
type createLogGroupResult struct {
|
|
|
|
successResult *cloudwatchlogs.CreateLogGroupOutput
|
|
|
|
errorResult error
|
|
|
|
}
|
|
|
|
|
2015-08-04 20:35:06 -04:00
|
|
|
type createLogStreamResult struct {
|
|
|
|
successResult *cloudwatchlogs.CreateLogStreamOutput
|
|
|
|
errorResult error
|
|
|
|
}
|
|
|
|
|
|
|
|
type putLogEventsResult struct {
|
|
|
|
successResult *cloudwatchlogs.PutLogEventsOutput
|
|
|
|
errorResult error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockClient() *mockcwlogsclient {
|
|
|
|
return &mockcwlogsclient{
|
2016-12-17 17:36:41 -05:00
|
|
|
createLogGroupArgument: make(chan *cloudwatchlogs.CreateLogGroupInput, 1),
|
|
|
|
createLogGroupResult: make(chan *createLogGroupResult, 1),
|
2015-08-04 20:35:06 -04:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:36:41 -05:00
|
|
|
func (m *mockcwlogsclient) CreateLogGroup(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error) {
|
|
|
|
m.createLogGroupArgument <- input
|
|
|
|
output := <-m.createLogGroupResult
|
|
|
|
return output.successResult, output.errorResult
|
|
|
|
}
|
|
|
|
|
2015-08-04 20:35:06 -04:00
|
|
|
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) {
|
2016-05-25 14:50:22 -04:00
|
|
|
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,
|
|
|
|
}
|
2017-11-30 19:17:17 -05:00
|
|
|
|
|
|
|
// Intended mock output
|
2015-08-04 20:35:06 -04:00
|
|
|
output := <-m.putLogEventsResult
|
2017-11-30 19:17:17 -05:00
|
|
|
|
|
|
|
// Checked enforced limits in mock
|
|
|
|
totalBytes := 0
|
|
|
|
for _, evt := range events {
|
|
|
|
if evt.Message == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
eventBytes := len([]byte(*evt.Message))
|
|
|
|
if eventBytes > maximumBytesPerEvent {
|
|
|
|
// exceeded per event message size limits
|
|
|
|
return nil, fmt.Errorf("maximum bytes per event exceeded: Event too large %d, max allowed: %d", eventBytes, maximumBytesPerEvent)
|
|
|
|
}
|
|
|
|
// total event bytes including overhead
|
|
|
|
totalBytes += eventBytes + perEventBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
if totalBytes > maximumBytesPerPut {
|
|
|
|
// exceeded per put maximum size limit
|
|
|
|
return nil, fmt.Errorf("maximum bytes per put exceeded: Upload too large %d, max allowed: %d", totalBytes, maximumBytesPerPut)
|
|
|
|
}
|
|
|
|
|
2015-08-04 20:35:06 -04:00
|
|
|
return output.successResult, output.errorResult
|
|
|
|
}
|
|
|
|
|
2015-09-28 02:40:44 -04:00
|
|
|
type mockmetadataclient struct {
|
|
|
|
regionResult chan *regionResult
|
|
|
|
}
|
|
|
|
|
|
|
|
type regionResult struct {
|
|
|
|
successResult string
|
|
|
|
errorResult error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockMetadataClient() *mockmetadataclient {
|
|
|
|
return &mockmetadataclient{
|
|
|
|
regionResult: make(chan *regionResult, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockmetadataclient) Region() (string, error) {
|
|
|
|
output := <-m.regionResult
|
|
|
|
return output.successResult, output.errorResult
|
|
|
|
}
|