@@ -6,6 +6,7 @@ package aws
6
6
7
7
import (
8
8
"context"
9
+ "strconv"
9
10
"testing"
10
11
"time"
11
12
@@ -49,6 +50,9 @@ type MockCloudWatchClient struct{}
49
50
// MockCloudwatchClientCrossAccounts struct is used for unit tests.
50
51
type MockCloudwatchClientCrossAccounts struct {}
51
52
53
+ // MockCloudwatchClientMultiplePages struct is used for unit tests.
54
+ type MockCloudwatchClientMultiplePages struct {}
55
+
52
56
// GetMetricData implements cloudwatch.GetMetricDataAPIClient interface
53
57
func (m * MockCloudWatchClient ) GetMetricData (context.Context , * cloudwatch.GetMetricDataInput , ... func (* cloudwatch.Options )) (* cloudwatch.GetMetricDataOutput , error ) {
54
58
emptyString := ""
@@ -158,6 +162,51 @@ func (m *MockCloudwatchClientCrossAccounts) ListMetrics(context.Context, *cloudw
158
162
}, nil
159
163
}
160
164
165
+ func (m * MockCloudwatchClientMultiplePages ) ListMetrics (ctx context.Context , input * cloudwatch.ListMetricsInput , opts ... func (* cloudwatch.Options )) (* cloudwatch.ListMetricsOutput , error ) {
166
+ var allMetrics = []cloudwatchtypes.Metric {
167
+ {
168
+ MetricName : & metricName ,
169
+ Namespace : & namespace ,
170
+ Dimensions : []cloudwatchtypes.Dimension {
171
+ {Name : & dimName , Value : & instanceID1 },
172
+ },
173
+ },
174
+ {
175
+ MetricName : awssdk .String ("NetworkIn" ),
176
+ Namespace : & namespace ,
177
+ Dimensions : []cloudwatchtypes.Dimension {
178
+ {Name : & dimName , Value : & instanceID1 },
179
+ },
180
+ },
181
+ }
182
+
183
+ pageSize := 1 // Change this to control the number of metrics per page
184
+ startIndex := 0
185
+
186
+ if input .NextToken != nil {
187
+ index , err := strconv .Atoi (* input .NextToken )
188
+ if err != nil {
189
+ return nil , err
190
+ }
191
+ startIndex = index
192
+ }
193
+
194
+ endIndex := startIndex + pageSize
195
+ if endIndex > len (allMetrics ) {
196
+ endIndex = len (allMetrics )
197
+ }
198
+
199
+ nextToken := ""
200
+ if endIndex < len (allMetrics ) {
201
+ nextToken = strconv .Itoa (endIndex )
202
+ }
203
+
204
+ return & cloudwatch.ListMetricsOutput {
205
+ Metrics : allMetrics [startIndex :endIndex ],
206
+ NextToken : awssdk .String (nextToken ),
207
+ }, nil
208
+ }
209
+
161
210
// MockResourceGroupsTaggingClient is used for unit tests.
162
211
type MockResourceGroupsTaggingClient struct {}
163
212
@@ -221,6 +270,13 @@ func TestGetListMetricsCrossAccountsOutput(t *testing.T) {
221
270
assert .Equal (t , instanceID2 , * listMetricsOutput [1 ].Metric .Dimensions [0 ].Value )
222
271
}
223
272
273
+ func TestGetListMetricsOutputWithMultiplePages (t * testing.T ) {
274
+ svcCloudwatch := & MockCloudwatchClientMultiplePages {}
275
+ listMetricsOutput , err := GetListMetricsOutput ("AWS/EC2" , "us-west-1" , time .Minute * 5 , false , "123" , svcCloudwatch )
276
+ assert .NoError (t , err )
277
+ assert .Equal (t , 2 , len (listMetricsOutput ))
278
+ }
279
+
224
280
func TestGetListMetricsOutputWithWildcard (t * testing.T ) {
225
281
svcCloudwatch := & MockCloudWatchClient {}
226
282
listMetricsOutput , err := GetListMetricsOutput ("*" , "us-west-1" , time .Minute * 5 , false , "123" , svcCloudwatch )
0 commit comments