cwlogsiface_mock_test.go 2.8 KB

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