generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogs_test.go
More file actions
335 lines (310 loc) · 9.18 KB
/
logs_test.go
File metadata and controls
335 lines (310 loc) · 9.18 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"context"
"reflect"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
)
// CloudWatchLogsAPI defines the interface for CloudWatch Logs client operations
type CloudWatchLogsAPI interface {
DescribeLogGroups(ctx context.Context, params *cloudwatchlogs.DescribeLogGroupsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogGroupsOutput, error)
DescribeFieldIndexes(ctx context.Context, params *cloudwatchlogs.DescribeFieldIndexesInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeFieldIndexesOutput, error)
DescribeSubscriptionFilters(ctx context.Context, params *cloudwatchlogs.DescribeSubscriptionFiltersInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeSubscriptionFiltersOutput, error)
ListLogAnomalyDetectors(ctx context.Context, params *cloudwatchlogs.ListLogAnomalyDetectorsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.ListLogAnomalyDetectorsOutput, error)
}
// Mock CloudWatchLogs client for testing
type mockCloudWatchLogsClient struct {
CloudWatchLogsAPI
describeLogGroupsOutput *cloudwatchlogs.DescribeLogGroupsOutput
describeLogGroupsErr error
describeFieldIndexesOutput *cloudwatchlogs.DescribeFieldIndexesOutput
describeFieldIndexesErr error
describeSubscriptionFiltersOutput *cloudwatchlogs.DescribeSubscriptionFiltersOutput
describeSubscriptionFiltersErr error
listLogAnomalyDetectorsOutput *cloudwatchlogs.ListLogAnomalyDetectorsOutput
listLogAnomalyDetectorsErr error
}
func (m *mockCloudWatchLogsClient) DescribeLogGroups(ctx context.Context, params *cloudwatchlogs.DescribeLogGroupsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogGroupsOutput, error) {
return m.describeLogGroupsOutput, m.describeLogGroupsErr
}
func (m *mockCloudWatchLogsClient) DescribeFieldIndexes(ctx context.Context, params *cloudwatchlogs.DescribeFieldIndexesInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeFieldIndexesOutput, error) {
return m.describeFieldIndexesOutput, m.describeFieldIndexesErr
}
func (m *mockCloudWatchLogsClient) DescribeSubscriptionFilters(ctx context.Context, params *cloudwatchlogs.DescribeSubscriptionFiltersInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeSubscriptionFiltersOutput, error) {
return m.describeSubscriptionFiltersOutput, m.describeSubscriptionFiltersErr
}
func (m *mockCloudWatchLogsClient) ListLogAnomalyDetectors(ctx context.Context, params *cloudwatchlogs.ListLogAnomalyDetectorsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.ListLogAnomalyDetectorsOutput, error) {
return m.listLogAnomalyDetectorsOutput, m.listLogAnomalyDetectorsErr
}
func TestCheckLogGroup(t *testing.T) {
tests := []struct {
name string
logGroup types.LogGroup
expected bool
}{
{
name: "Log group with metric filter",
logGroup: types.LogGroup{
LogGroupName: aws.String("test-log-group"),
MetricFilterCount: aws.Int32(1),
},
expected: true,
},
{
name: "Log group with data protection policy",
logGroup: types.LogGroup{
LogGroupName: aws.String("test-log-group"),
DataProtectionStatus: types.DataProtectionStatusActivated,
},
expected: true,
},
{
name: "Log group already in IA",
logGroup: types.LogGroup{
LogGroupName: aws.String("test-log-group"),
LogGroupClass: types.LogGroupClassInfrequentAccess,
},
expected: true,
},
{
name: "Log group with insights in name",
logGroup: types.LogGroup{
LogGroupName: aws.String("lambda-insights-log-group"),
},
expected: true,
},
{
name: "Log group with containerinsights in name",
logGroup: types.LogGroup{
LogGroupName: aws.String("containerinsights-log-group"),
},
expected: true,
},
{
name: "Log group with no special conditions",
logGroup: types.LogGroup{
LogGroupName: aws.String("regular-log-group"),
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := checkLogGroup(tt.logGroup)
if result != tt.expected {
t.Errorf("checkLogGroup() = %v, want %v", result, tt.expected)
}
})
}
}
func TestIsIA(t *testing.T) {
tests := []struct {
name string
logGroup types.LogGroup
expected bool
}{
{
name: "Log group in IA class",
logGroup: types.LogGroup{
LogGroupClass: types.LogGroupClassInfrequentAccess,
},
expected: true,
},
{
name: "Log group in standard class",
logGroup: types.LogGroup{
LogGroupClass: types.LogGroupClassStandard,
},
expected: false,
},
{
name: "Log group with no class specified",
logGroup: types.LogGroup{},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isIA(tt.logGroup)
if result != tt.expected {
t.Errorf("isIA() = %v, want %v", result, tt.expected)
}
})
}
}
func TestHasMetricFilter(t *testing.T) {
tests := []struct {
name string
logGroup types.LogGroup
expected bool
}{
{
name: "Log group with metric filters",
logGroup: types.LogGroup{
MetricFilterCount: aws.Int32(2),
},
expected: true,
},
{
name: "Log group with no metric filters",
logGroup: types.LogGroup{
MetricFilterCount: aws.Int32(0),
},
expected: false,
},
{
name: "Log group with nil metric filter count",
logGroup: types.LogGroup{},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := hasMetricFilter(tt.logGroup)
if result != tt.expected {
t.Errorf("hasMetricFilter() = %v, want %v", result, tt.expected)
}
})
}
}
func TestHasDataProtectionPolicy(t *testing.T) {
tests := []struct {
name string
logGroup types.LogGroup
expected bool
}{
{
name: "Log group with activated data protection",
logGroup: types.LogGroup{
DataProtectionStatus: types.DataProtectionStatusActivated,
},
expected: true,
},
{
name: "Log group with no data protection",
logGroup: types.LogGroup{
// No DataProtectionStatus set
},
expected: false,
},
{
name: "Log group with no data protection status",
logGroup: types.LogGroup{},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := hasDataProtectionPolicy(tt.logGroup)
if result != tt.expected {
t.Errorf("hasDataProtectionPolicy() = %v, want %v", result, tt.expected)
}
})
}
}
func TestHasInsights(t *testing.T) {
tests := []struct {
name string
logGroup types.LogGroup
expected bool
}{
{
name: "Log group with lambda-insights in name",
logGroup: types.LogGroup{
LogGroupName: aws.String("/aws/lambda-insights/my-function"),
},
expected: true,
},
{
name: "Log group with containerinsights in name",
logGroup: types.LogGroup{
LogGroupName: aws.String("/aws/containerinsights/my-cluster"),
},
expected: true,
},
{
name: "Regular log group",
logGroup: types.LogGroup{
LogGroupName: aws.String("/aws/lambda/my-function"),
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := hasInsights(tt.logGroup)
if result != tt.expected {
t.Errorf("hasInsights() = %v, want %v", result, tt.expected)
}
})
}
}
func TestFetchIndexPoliciesForBatch(t *testing.T) {
tests := []struct {
name string
batch []string
mockResponse *cloudwatchlogs.DescribeFieldIndexesOutput
mockError error
expectedResult []string
}{
{
name: "No log groups have index policies",
batch: []string{"log1", "log2", "log3"},
mockResponse: &cloudwatchlogs.DescribeFieldIndexesOutput{
FieldIndexes: []types.FieldIndex{},
},
mockError: nil,
expectedResult: []string{"log1", "log2", "log3"},
},
{
name: "Some log groups have index policies",
batch: []string{"log1", "log2", "log3"},
mockResponse: &cloudwatchlogs.DescribeFieldIndexesOutput{
FieldIndexes: []types.FieldIndex{
{
LogGroupIdentifier: aws.String("log1"),
},
},
},
mockError: nil,
expectedResult: []string{"log2", "log3"},
},
{
name: "All log groups have index policies",
batch: []string{"log1", "log2"},
mockResponse: &cloudwatchlogs.DescribeFieldIndexesOutput{
FieldIndexes: []types.FieldIndex{
{
LogGroupIdentifier: aws.String("log1"),
},
{
LogGroupIdentifier: aws.String("log2"),
},
},
},
mockError: nil,
expectedResult: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create mock client
mockClient := &mockCloudWatchLogsClient{
describeFieldIndexesOutput: tt.mockResponse,
describeFieldIndexesErr: tt.mockError,
}
result := fetchIndexPoliciesForBatch(tt.batch, mockClient)
// Sort both slices to ensure consistent comparison
if !reflect.DeepEqual(result, tt.expectedResult) {
// Special case for empty slices
if len(result) == 0 && len(tt.expectedResult) == 0 {
// Both are empty, so they're equal
return
}
t.Errorf("fetchIndexPoliciesForBatch() = %v, want %v", result, tt.expectedResult)
}
})
}
}