This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from marcinwyszynski/cloudwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_test.go
113 lines (87 loc) · 3 KB
/
group_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cloudwatch
import (
"context"
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/stretchr/testify/suite"
)
type groupTestSuite struct {
suite.Suite
api *mockAPI
ctx context.Context
groupName, streamName string
sut Group
}
func (gs *groupTestSuite) SetupTest() {
gs.api = new(mockAPI)
gs.ctx = context.Background()
gs.groupName = "groupName"
gs.streamName = "streamName"
gs.sut = NewGroup(gs.api, gs.groupName)
}
func (gs *groupTestSuite) TestCreateWithNoExistingStream_OK() {
gs.creatingLogStreamReturns(nil)
writer, err := gs.sut.Create(gs.ctx, gs.streamName)
gs.Require().NotNil(writer)
gs.NoError(err)
gs.Nil(writer.(*writerImpl).sequenceToken)
}
func (gs *groupTestSuite) TestCreateWithExistingStream_OK() {
const sequenceToken = "sequenceToken"
gs.creatingLogStreamReturns(new(cloudwatchlogs.ResourceAlreadyExistsException))
gs.describingStreamsReturns([]*cloudwatchlogs.LogStream{
{UploadSequenceToken: aws.String(sequenceToken)},
}, nil)
writer, err := gs.sut.Create(gs.ctx, gs.streamName)
gs.Require().NotNil(writer)
gs.NoError(err)
gs.Equal(sequenceToken, *writer.(*writerImpl).sequenceToken)
}
func (gs *groupTestSuite) TestCreateWithExistingStream_UnexpectedFailure() {
gs.creatingLogStreamReturns(errors.New("bacon"))
writer, err := gs.sut.Create(gs.ctx, gs.streamName)
gs.Nil(writer)
gs.EqualError(err, "could not create the log stream: bacon")
}
func (gs *groupTestSuite) TestCreateDescribingStreamFails() {
gs.creatingLogStreamReturns(new(cloudwatchlogs.ResourceAlreadyExistsException))
gs.describingStreamsReturns(nil, errors.New("bacon"))
writer, err := gs.sut.Create(gs.ctx, gs.streamName)
gs.EqualError(err, "couldn't get log stream description: bacon")
gs.Nil(writer)
}
func (gs *groupTestSuite) TestCreateDescribingStream_MissingLogStreamData() {
gs.creatingLogStreamReturns(new(cloudwatchlogs.ResourceAlreadyExistsException))
gs.describingStreamsReturns(nil, nil)
writer, err := gs.sut.Create(gs.ctx, gs.streamName)
gs.EqualError(err, "logs streams data missing for streamName")
gs.Nil(writer)
}
func (gs *groupTestSuite) describingStreamsReturns(result []*cloudwatchlogs.LogStream, err error) {
gs.api.On(
"DescribeLogStreamsWithContext",
gs.ctx,
&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(gs.groupName),
LogStreamNamePrefix: aws.String(gs.streamName),
},
[]request.Option(nil),
).Return(&cloudwatchlogs.DescribeLogStreamsOutput{LogStreams: result}, err)
}
func (gs *groupTestSuite) creatingLogStreamReturns(err error) {
gs.api.On(
"CreateLogStreamWithContext",
gs.ctx,
&cloudwatchlogs.CreateLogStreamInput{
LogGroupName: aws.String(gs.groupName),
LogStreamName: aws.String(gs.streamName),
},
[]request.Option(nil),
).Return(&cloudwatchlogs.CreateLogStreamOutput{}, err)
}
func TestGroup(t *testing.T) {
suite.Run(t, new(groupTestSuite))
}