This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
containers_test.go
340 lines (303 loc) · 11 KB
/
containers_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
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
336
337
338
339
340
/******************************************************************************
Cloud Resource Counter
File: containers_test.go
Summary: The Unit test for containers.
******************************************************************************/
package main
import (
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
"github.com/expel-io/cloud-resource-counter/mock"
)
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Task Definitions Data
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// For our tests, we are combining the list of task definition ARNs with the
// descriptions of each.
type TaskInfo struct {
ListOutputs []*ecs.ListTaskDefinitionsOutput
DescribeOutputMap map[string]*ecs.DescribeTaskDefinitionOutput
}
// This is our map of regions and the task definitions in each
var taskDefinitionsPerRegion = map[string]*TaskInfo{
// US-EAST-1 simulations the case when more than one page of ListTaskDefinitionsOutput is
// returned. In this case, there are 2 task definitions in the first page and 1 task
// definition in the other page. Each of the 3 task definitions are listed as well. The
// first task definition uses two containers, each with different images. The second task
// definition uses one container, with a single image. The third task has a single container
// but uses the same image as the first task. In total, there are 3 task definitions, 4
// containers, but with only 3 unique container images.
"us-east-1": &TaskInfo{
ListOutputs: []*ecs.ListTaskDefinitionsOutput{
&ecs.ListTaskDefinitionsOutput{
TaskDefinitionArns: []*string{
aws.String("some-long-name:task-definition/family:1"),
aws.String("some-long-name:task-definition/family:2"),
},
},
&ecs.ListTaskDefinitionsOutput{
TaskDefinitionArns: []*string{
aws.String("some-long-name:task-definition/otherfamily:1"),
},
},
},
DescribeOutputMap: map[string]*ecs.DescribeTaskDefinitionOutput{
"some-long-name:task-definition/family:1": &ecs.DescribeTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
ContainerDefinitions: []*ecs.ContainerDefinition{
&ecs.ContainerDefinition{
Image: aws.String("image1"),
},
&ecs.ContainerDefinition{
Image: aws.String("image2"),
},
},
},
},
"some-long-name:task-definition/family:2": &ecs.DescribeTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
ContainerDefinitions: []*ecs.ContainerDefinition{
&ecs.ContainerDefinition{
Image: aws.String("image3"),
},
},
},
},
"some-long-name:task-definition/otherfamily:1": &ecs.DescribeTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
ContainerDefinitions: []*ecs.ContainerDefinition{
&ecs.ContainerDefinition{
Image: aws.String("image1"),
},
},
},
},
},
},
// US-EAST-2 simulates the case when a single page of ListTaskDefinitionOutput is returned
// to the caller. There are two task definitions. Each has a single container image, which
// are different. In total, there are 2 task definitions, 2 cotnainers and 2 unique container
// images.
"us-east-2": &TaskInfo{
ListOutputs: []*ecs.ListTaskDefinitionsOutput{
&ecs.ListTaskDefinitionsOutput{
TaskDefinitionArns: []*string{
aws.String("some-long-name:task-definition/family:1"),
aws.String("some-long-name:task-definition/anotherfamily:1"),
},
},
},
DescribeOutputMap: map[string]*ecs.DescribeTaskDefinitionOutput{
"some-long-name:task-definition/family:1": &ecs.DescribeTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
ContainerDefinitions: []*ecs.ContainerDefinition{
&ecs.ContainerDefinition{
Image: aws.String("image2"),
},
},
},
},
"some-long-name:task-definition/anotherfamily:1": &ecs.DescribeTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
ContainerDefinitions: []*ecs.ContainerDefinition{
&ecs.ContainerDefinition{
Image: aws.String("image4"),
},
},
},
},
},
},
// AF-SOUTH-1 indicates that no tasks were defined for this regino.
"af-south-1": &TaskInfo{
ListOutputs: []*ecs.ListTaskDefinitionsOutput{
&ecs.ListTaskDefinitionsOutput{},
},
},
// AF-SOUTH-2 simulates a flawed case--what would happen if DescribeTaskDefinition
// ever returned a failure? We simulate that by indicating that there is a Task Definition
// ARN for which there is no description.
"af-south-2": &TaskInfo{
ListOutputs: []*ecs.ListTaskDefinitionsOutput{
&ecs.ListTaskDefinitionsOutput{
TaskDefinitionArns: []*string{
aws.String("this-is-a-non-existent-task-arn-which-triggers-failure"),
},
},
},
},
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Container Service
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This structure simulates a ContainerService by providing both an array of
// pre-canned outputs (for the ListTaskDefinitions method) as well as a map of
// pre-canned outputs (for the InspectTaskDefinition method).
type fakeContainerService struct {
ecsiface.ECSAPI
TaskInfo *TaskInfo
}
// Implement the ListTaskDefinitionsPages method by returning the pre-canned array
// of ListTaskDefinitionsOutput structs.
func (fake *fakeContainerService) ListTaskDefinitionsPages(input *ecs.ListTaskDefinitionsInput,
fn func(page *ecs.ListTaskDefinitionsOutput, lastPage bool) bool) error {
// If there is no TaskInfo, simulate an error...
if fake.TaskInfo == nil {
return errors.New("ListTaskDefinitionsPages encountered an unexpected error: 2468")
}
// Loop through the slice of responses, invoking the supplied function
for index, output := range fake.TaskInfo.ListOutputs {
// Are we looking at the last "page" of our output?
lastPage := index == len(fake.TaskInfo.ListOutputs)-1
// Apply filtering to the supplied response
// NOTE: I have not implemented this feature as our code does not require it.
// To prevent unexpected cases, if the caller supplies an input other then
// the "zero" input, the unit test fails.
if input.FamilyPrefix != nil || input.MaxResults != nil || input.NextToken != nil || input.Sort != nil || input.Status != nil {
return errors.New("The unit test does not support a ListTaskDefinitionsInput other than 'zero' (no parameters)")
}
// Invoke our fn
cont := fn(output, lastPage)
// Shall we exit our loop?
if !cont {
break
}
}
return nil
}
// Implement the DescribeTaskDefinition method by returning a pre-canned response
// keyed by the task definition ARN.
func (fake *fakeContainerService) DescribeTaskDefinition(input *ecs.DescribeTaskDefinitionInput) (*ecs.DescribeTaskDefinitionOutput, error) {
// We ensure that the input does not contain unexpected fields
if input.Include != nil {
return nil, errors.New("The unit test does not support a DescribeTaskDefinitionInput that contains anything other than a TaskDefinition")
}
// Get the response keyed by the task ARN
response, ok := fake.TaskInfo.DescribeOutputMap[*input.TaskDefinition]
// If no match, return an error
if !ok {
return nil, errors.New("DescribeTaskDefinition encountered an unexpected error: 9876")
}
return response, nil
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Fake Service Factory
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// This structure simulates the AWS Service Factory by storing some pregenerated
// responses (that would come from AWS).
type fakeCntrServiceFactory struct {
RegionName string
DRResponse *ec2.DescribeRegionsOutput
}
// Don't need to implement
func (fsf fakeCntrServiceFactory) Init() {}
// Return our current region
func (fsf fakeCntrServiceFactory) GetCurrentRegion() string {
return fsf.RegionName
}
// Don't need to implement
func (fsf fakeCntrServiceFactory) GetAccountIDService() *AccountIDService {
return nil
}
// This implementation of GetEC2InstanceService is limited to supporting DescribeRegions API
// only.
func (fsf fakeCntrServiceFactory) GetEC2InstanceService(string) *EC2InstanceService {
return &EC2InstanceService{
Client: &fakeEC2Service{
DRResponse: fsf.DRResponse,
},
}
}
// Don't need to implement
func (fsf fakeCntrServiceFactory) GetRDSInstanceService(regionName string) *RDSInstanceService {
return nil
}
// Don't need to implement
func (fsf fakeCntrServiceFactory) GetS3Service() *S3Service {
return nil
}
// Don't need to implement
func (fsf fakeCntrServiceFactory) GetLambdaService(string) *LambdaService {
return nil
}
// Implement a way to return a ContainerService instance associated with a specific
// region
func (fsf fakeCntrServiceFactory) GetContainerService(regionName string) *ContainerService {
// If the caller failed to specify a region, then use what is associated with our factory
var resolvedRegionName string
if regionName == "" {
resolvedRegionName = fsf.RegionName
} else {
resolvedRegionName = regionName
}
return &ContainerService{
Client: &fakeContainerService{
TaskInfo: taskDefinitionsPerRegion[resolvedRegionName],
},
}
}
// Don't need to implement
func (fsf fakeCntrServiceFactory) GetLightsailService(string) *LightsailService {
return nil
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Unit Test for UniqueContainerImages
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
func TestUniqueContainerImages(t *testing.T) {
// Describe all of our test cases: 2 failures and 4 success cases
cases := []struct {
RegionName string
AllRegions bool
ExpectedCount int
ExpectError bool
}{
{
RegionName: "us-east-1",
ExpectedCount: 3,
}, {
RegionName: "us-east-2",
ExpectedCount: 2,
}, {
RegionName: "af-south-1",
ExpectedCount: 0,
}, {
RegionName: "af-south-2",
ExpectError: true,
}, {
RegionName: "undefined-region",
ExpectError: true,
}, {
AllRegions: true,
ExpectedCount: 4,
},
}
// Loop through each test case
for _, c := range cases {
// Create our fake service factory
sf := fakeCntrServiceFactory{
RegionName: c.RegionName,
DRResponse: ec2Regions,
}
// Create a mock activity monitor
mon := &mock.ActivityMonitorImpl{}
// Invoke our UniqueContainerImages function
actualCount := UniqueContainerImages(sf, mon, c.AllRegions)
// Did we expect an error?
if c.ExpectError {
// Did it fail to arrive?
if !mon.ErrorOccured {
t.Error("Expected an error to occur, but it did not... :^(")
}
} else if mon.ErrorOccured {
t.Errorf("Unexpected error occurred: %s", mon.ErrorMessage)
} else if actualCount != c.ExpectedCount {
t.Errorf("Error: UniqueContainerImages returned %d; expected %d", actualCount, c.ExpectedCount)
} else if mon.ProgramExited {
t.Errorf("Unexpected Exit: The program unexpected exited with status code=%d", mon.ExitCode)
}
}
}