github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/awslogs/cwlogsiface_mock_test.go (about) 1 package awslogs // import "github.com/docker/docker/daemon/logger/awslogs" 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/service/cloudwatchlogs" 7 ) 8 9 type mockClient struct { 10 createLogGroupFunc func(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error) 11 createLogStreamFunc func(input *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error) 12 putLogEventsFunc func(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) 13 } 14 15 func (m *mockClient) CreateLogGroup(input *cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error) { 16 return m.createLogGroupFunc(input) 17 } 18 19 func (m *mockClient) CreateLogStream(input *cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error) { 20 return m.createLogStreamFunc(input) 21 } 22 23 func (m *mockClient) PutLogEvents(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) { 24 if err := checkPutLogEventsConstraints(input); err != nil { 25 return nil, err 26 } 27 return m.putLogEventsFunc(input) 28 } 29 30 func checkPutLogEventsConstraints(input *cloudwatchlogs.PutLogEventsInput) error { 31 events := input.LogEvents 32 // Checked enforced limits in mock 33 totalBytes := 0 34 for _, evt := range events { 35 if evt.Message == nil { 36 continue 37 } 38 eventBytes := len([]byte(*evt.Message)) 39 if eventBytes > maximumBytesPerEvent { 40 // exceeded per event message size limits 41 return fmt.Errorf("maximum bytes per event exceeded: Event too large %d, max allowed: %d", eventBytes, maximumBytesPerEvent) 42 } 43 // total event bytes including overhead 44 totalBytes += eventBytes + perEventBytes 45 } 46 47 if totalBytes > maximumBytesPerPut { 48 // exceeded per put maximum size limit 49 return fmt.Errorf("maximum bytes per put exceeded: Upload too large %d, max allowed: %d", totalBytes, maximumBytesPerPut) 50 } 51 return nil 52 } 53 54 type mockmetadataclient struct { 55 regionResult chan *regionResult 56 } 57 58 type regionResult struct { 59 successResult string 60 errorResult error 61 } 62 63 func newMockMetadataClient() *mockmetadataclient { 64 return &mockmetadataclient{ 65 regionResult: make(chan *regionResult, 1), 66 } 67 } 68 69 func (m *mockmetadataclient) Region() (string, error) { 70 output := <-m.regionResult 71 return output.successResult, output.errorResult 72 }