/
cwlogsiface_mock_test.go
78 lines (66 loc) · 2.82 KB
/
cwlogsiface_mock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package awslogs // import "github.com/docker/docker/daemon/logger/awslogs"
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
)
type mockClient struct {
createLogGroupFunc func(context.Context, *cloudwatchlogs.CreateLogGroupInput, ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.CreateLogGroupOutput, error)
createLogStreamFunc func(context.Context, *cloudwatchlogs.CreateLogStreamInput, ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.CreateLogStreamOutput, error)
putLogEventsFunc func(context.Context, *cloudwatchlogs.PutLogEventsInput, ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.PutLogEventsOutput, error)
}
func (m *mockClient) CreateLogGroup(ctx context.Context, input *cloudwatchlogs.CreateLogGroupInput, opts ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.CreateLogGroupOutput, error) {
return m.createLogGroupFunc(ctx, input, opts...)
}
func (m *mockClient) CreateLogStream(ctx context.Context, input *cloudwatchlogs.CreateLogStreamInput, opts ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.CreateLogStreamOutput, error) {
return m.createLogStreamFunc(ctx, input, opts...)
}
func (m *mockClient) PutLogEvents(ctx context.Context, input *cloudwatchlogs.PutLogEventsInput, opts ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.PutLogEventsOutput, error) {
if err := checkPutLogEventsConstraints(input); err != nil {
return nil, err
}
return m.putLogEventsFunc(ctx, input, opts...)
}
func checkPutLogEventsConstraints(input *cloudwatchlogs.PutLogEventsInput) error {
events := input.LogEvents
// 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 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 fmt.Errorf("maximum bytes per put exceeded: Upload too large %d, max allowed: %d", totalBytes, maximumBytesPerPut)
}
return nil
}
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) GetRegion(context.Context, *imds.GetRegionInput, ...func(*imds.Options)) (*imds.GetRegionOutput, error) {
output := <-m.regionResult
err := output.errorResult
if err != nil {
return nil, err
}
return &imds.GetRegionOutput{Region: output.successResult}, err
}