@@ -7,41 +7,44 @@ import (
7
7
"context"
8
8
"encoding/json"
9
9
"fmt"
10
- "io/ioutil"
11
10
"os"
11
+ "path/filepath"
12
+ "strconv"
12
13
"testing"
13
14
"time"
14
15
15
16
"github.com/jessevdk/go-flags"
16
17
"github.com/mackerelio/checkers"
17
18
"github.com/stretchr/testify/assert"
18
19
19
- "github.com/aws/aws-sdk-go/aws"
20
- "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
21
- "github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface "
20
+ "github.com/aws/aws-sdk-go-v2 /aws"
21
+ "github.com/aws/aws-sdk-go-v2 /service/cloudwatchlogs"
22
+ "github.com/aws/aws-sdk-go-v2 /service/cloudwatchlogs/types "
22
23
)
23
24
24
25
type mockAWSCloudWatchLogsClient struct {
25
- cloudwatchlogsiface. CloudWatchLogsAPI
26
+ cloudwatchlogs. FilterLogEventsAPIClient
26
27
outputs []* cloudwatchlogs.FilterLogEventsOutput
27
28
}
28
29
29
- func (c * mockAWSCloudWatchLogsClient ) FilterLogEventsPages (input * cloudwatchlogs.FilterLogEventsInput , fn func (* cloudwatchlogs.FilterLogEventsOutput , bool ) bool ) error {
30
- for i , output := range c .outputs {
31
- lastPage := i == len (c .outputs )- 1
32
- if ! fn (output , lastPage ) {
33
- break
34
- }
30
+ func (c * mockAWSCloudWatchLogsClient ) FilterLogEvents (ctx context.Context , input * cloudwatchlogs.FilterLogEventsInput , _ ... func (* cloudwatchlogs.Options )) (* cloudwatchlogs.FilterLogEventsOutput , error ) {
31
+ if ctx .Err () != nil {
32
+ return nil , ctx .Err ()
33
+ }
34
+
35
+ var pageNo = 0
36
+ if input .NextToken != nil {
37
+ pageNo , _ = strconv .Atoi (* input .NextToken )
35
38
}
36
- return nil
39
+ return c . outputs [ pageNo ], nil
37
40
}
38
41
39
- func createMockService () cloudwatchlogsiface. CloudWatchLogsAPI {
42
+ func createMockService () cloudwatchlogs. FilterLogEventsAPIClient {
40
43
return & mockAWSCloudWatchLogsClient {
41
44
outputs : []* cloudwatchlogs.FilterLogEventsOutput {
42
45
{
43
46
NextToken : aws .String ("1" ),
44
- Events : []* cloudwatchlogs .FilteredLogEvent {
47
+ Events : []types .FilteredLogEvent {
45
48
{
46
49
EventId : aws .String ("event-id-0" ),
47
50
Message : aws .String ("message-0" ),
@@ -56,7 +59,7 @@ func createMockService() cloudwatchlogsiface.CloudWatchLogsAPI {
56
59
},
57
60
{
58
61
NextToken : aws .String ("2" ),
59
- Events : []* cloudwatchlogs .FilteredLogEvent {
62
+ Events : []types .FilteredLogEvent {
60
63
{
61
64
EventId : aws .String ("event-id-2" ),
62
65
Message : aws .String ("message-2" ),
@@ -75,7 +78,7 @@ func createMockService() cloudwatchlogsiface.CloudWatchLogsAPI {
75
78
},
76
79
},
77
80
{
78
- Events : []* cloudwatchlogs .FilteredLogEvent {
81
+ Events : []types .FilteredLogEvent {
79
82
{
80
83
EventId : aws .String ("event-id-5" ),
81
84
Message : aws .String ("message-5" ),
@@ -88,13 +91,11 @@ func createMockService() cloudwatchlogsiface.CloudWatchLogsAPI {
88
91
}
89
92
90
93
func Test_cloudwatchLogsPlugin_collect (t * testing.T ) {
91
- file , _ := ioutil .TempFile ("" , "check-cloudwatch-logs-test-collect" )
92
- os .Remove (file .Name ())
93
- file .Close ()
94
- defer os .Remove (file .Name ())
94
+ stateFile := filepath .Join (t .TempDir (), "check-cloudwatch-logs-test-collect" )
95
+
95
96
p := & awsCloudwatchLogsPlugin {
96
97
Service : createMockService (),
97
- StateFile : file . Name () ,
98
+ StateFile : stateFile ,
98
99
logOpts : & logOpts {
99
100
LogGroupName : "test-group" ,
100
101
},
@@ -104,7 +105,7 @@ func Test_cloudwatchLogsPlugin_collect(t *testing.T) {
104
105
messages , err := p .collect (context .Background (), time .Unix (0 , 0 ))
105
106
assert .Equal (t , err , nil , "err should be nil" )
106
107
assert .Equal (t , len (messages ), 6 )
107
- cnt , _ := ioutil .ReadFile (file . Name () )
108
+ cnt , _ := os .ReadFile (stateFile )
108
109
var s logState
109
110
json .NewDecoder (bytes .NewReader (cnt )).Decode (& s )
110
111
assert .Equal (t , s , logState {StartTime : aws .Int64 (5 + 1 )})
@@ -115,7 +116,7 @@ func Test_cloudwatchLogsPlugin_collect(t *testing.T) {
115
116
cancel ()
116
117
117
118
messages , err := p .collect (ctx , time .Unix (0 , 0 ))
118
- assert .Equal (t , err , nil , "err should be nil " )
119
+ assert .NotEqual (t , err , nil , "err should be someting " )
119
120
assert .Equal (t , len (messages ), 0 )
120
121
})
121
122
}
@@ -221,25 +222,32 @@ func Test_cloudwatchLogsPlugin_options(t *testing.T) {
221
222
}
222
223
}
223
224
224
- func Test_createAWSConfig (t * testing.T ) {
225
+ func Test_createCloudwatchlogsOptions (t * testing.T ) {
225
226
tests := []struct {
226
- opts * logOpts
227
- want * aws.Config
227
+ opts * logOpts
228
+ want cloudwatchlogs.Options
229
+ length int
228
230
}{
229
231
{
230
- opts : & logOpts {MaxRetries : 0 },
231
- want : aws . NewConfig () ,
232
+ opts : & logOpts {MaxRetries : 0 },
233
+ length : 0 ,
232
234
},
233
235
{
234
- opts : & logOpts {MaxRetries : 1 },
235
- want : aws .NewConfig ().WithMaxRetries (1 ),
236
+ opts : & logOpts {MaxRetries : 1 },
237
+ want : cloudwatchlogs.Options {RetryMaxAttempts : 1 },
238
+ length : 1 ,
236
239
},
237
240
}
238
241
239
242
for i , tt := range tests {
240
243
t .Run (fmt .Sprintf ("case:%d" , i ), func (t * testing.T ) {
241
- res := createAWSConfig (tt .opts )
242
- assert .Equal (t , tt .want , res )
244
+ res := createCloudwatchlogsOptions (tt .opts )
245
+ assert .Equal (t , len (res ), tt .length )
246
+ if tt .length > 0 {
247
+ opts := cloudwatchlogs.Options {}
248
+ res [0 ](& opts )
249
+ assert .Equal (t , tt .want , opts )
250
+ }
243
251
})
244
252
}
245
253
}
0 commit comments