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
|
|
|
|
2022-01-10 21:42:11 -05:00
|
|
|
type mockClient struct {
|
|
|
|
createLogGroupFunc func(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error)
|
|
|
|
createLogStreamFunc func(input *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error)
|
|
|
|
putLogEventsFunc func(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error)
|
2015-08-04 20:35:06 -04:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:42:11 -05:00
|
|
|
func (m *mockClient) CreateLogGroup(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error) {
|
|
|
|
return m.createLogGroupFunc(input)
|
2015-08-04 20:35:06 -04:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:42:11 -05:00
|
|
|
func (m *mockClient) CreateLogStream(input *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error) {
|
|
|
|
return m.createLogStreamFunc(input)
|
2015-08-04 20:35:06 -04:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:42:11 -05:00
|
|
|
func (m *mockClient) PutLogEvents(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) {
|
|
|
|
if err := checkPutLogEventsConstraints(input); err != nil {
|
|
|
|
return nil, err
|
2015-08-04 20:35:06 -04:00
|
|
|
}
|
2022-01-10 21:42:11 -05:00
|
|
|
return m.putLogEventsFunc(input)
|
2015-08-04 20:35:06 -04:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:42:11 -05:00
|
|
|
func checkPutLogEventsConstraints(input *cloudwatchlogs.PutLogEventsInput) error {
|
|
|
|
events := input.LogEvents
|
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
|
2022-01-10 21:42:11 -05:00
|
|
|
return fmt.Errorf("maximum bytes per event exceeded: Event too large %d, max allowed: %d", eventBytes, maximumBytesPerEvent)
|
2017-11-30 19:17:17 -05:00
|
|
|
}
|
|
|
|
// total event bytes including overhead
|
|
|
|
totalBytes += eventBytes + perEventBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
if totalBytes > maximumBytesPerPut {
|
|
|
|
// exceeded per put maximum size limit
|
2022-01-10 21:42:11 -05:00
|
|
|
return fmt.Errorf("maximum bytes per put exceeded: Upload too large %d, max allowed: %d", totalBytes, maximumBytesPerPut)
|
2017-11-30 19:17:17 -05:00
|
|
|
}
|
2022-01-10 21:42:11 -05:00
|
|
|
return nil
|
2015-08-04 20:35:06 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|