-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector_test.go
438 lines (409 loc) · 16.3 KB
/
collector_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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package snitch
import (
"errors"
"strconv"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
)
// FakeContainerInstance creates a mock container instance for testing.
//
// Without "ecs.instance-type" attribute, MetricData validation will fail since
// InstanceType is a required dimension.
func NewFakeContainerInstance(reg, rem []*ecs.Resource) *ecs.ContainerInstance {
return &ecs.ContainerInstance{
Attributes: []*ecs.Attribute{
{
Name: aws.String("ecs.instance-type"),
Value: aws.String("fake.2xlarge"),
},
},
RegisteredResources: reg,
RemainingResources: rem,
}
}
// FakeCloudWatch mocks CloudWatch for testing, with some fields added.
type FakeCloudWatch struct {
cloudwatchiface.CloudWatchAPI
payload []*cloudwatch.PutMetricDataInput // Stores supplied `*PutMetricDataInput`.
errorToReturn error // `error` to return from fake methods.
}
// PutMetricDataInput fake-publishes metrics to CloudWatch.
func (fake *FakeCloudWatch) PutMetricData(input *cloudwatch.PutMetricDataInput) (*cloudwatch.PutMetricDataOutput, error) {
fake.payload = append(fake.payload, input)
return nil, fake.errorToReturn
}
// FakeECS mocks AWS ECS to give us the responses we need.
type FakeECS struct {
ecsiface.ECSAPI
checkCluster bool // Check that expectedCluster name matches.
errorToReturn error // `error` to return from fake methods.
expectedCluster *string // Cluster name we expect during testing.
expectedClusterArns []string // Expected ECS Cluster ARNs.
expectedCPU int // Expected CPU Unit count for LCM container size.
expectedDescribeTasksOutput *ecs.DescribeTasksOutput // Expected response by DescribeTasks.
expectedMemory int // Expected Memory (RAM in MiB) for LCM container size.
expectedContainerInstanceArns []string // Expected ECS Container Instance ARNs.
expectedContainerInstances []*ecs.ContainerInstance // Expected ECS Container Instance ARNs.
expectedRegistered []*ecs.Resource // Expected registered ECS Cluster resources.
expectedRemaining []*ecs.Resource // Expected remaining ECS Cluster resources.
expectedTaskArns []string // Expected ECS Task ARNs.
expectedRegisteredPossible int // Expected number of schedulable containers w/ "RegisteredResources".
expectedRemainingPossible int // Expected number of schedulable containers w/ "RemainingResources".
t *testing.T // Enable logging and failure in mock.
}
// NewFakeECS constructs a new mock ECS "service" with pre-populated data.
func NewFakeECS(t *testing.T) *FakeECS {
fake := &FakeECS{
checkCluster: true,
expectedCluster: aws.String("fake-ecs-cluster"),
expectedClusterArns: []string{
"arn:aws:ecs:us-east-1:123456789012:cluster/fake-ecs-cluster",
"arn:aws:ecs:us-east-1:123456789012:cluster/another-fake-ecs-cluster",
"arn:aws:ecs:us-east-1:123456789012:cluster/who-even-uses-fargate",
},
expectedContainerInstanceArns: []string{
"arn:aws:ecs:us-east-1:123456789012:container-instance/30ed79a6-8ecd-4d7e-89ed-1415960b679a",
"arn:aws:ecs:us-east-1:123456789012:container-instance/31b326d2-2d50-4203-b000-44deabe3487a",
"arn:aws:ecs:us-east-1:123456789012:container-instance/4c684147-c27f-478d-9b22-111c85648f6f",
},
expectedTaskArns: []string{
"arn:aws:ecs:us-east-1:123456789012:task/1394beef-718f-42d7-b37b-97363e9ac917",
"arn:aws:ecs:us-east-1:123456789012:task/6649bf9d-7b1d-4ed7-9920-e0404ed4f2e5",
"arn:aws:ecs:us-east-1:123456789012:task/b9cfa5da-e760-457a-8673-1b61eb668b33",
},
t: t,
}
instanceCPU := 8192
instanceMemory := 15468
fake.expectedRegistered = []*ecs.Resource{
{
DoubleValue: aws.Float64(0),
IntegerValue: aws.Int64(int64(instanceCPU)),
LongValue: aws.Int64(0),
Name: aws.String("CPU"),
Type: aws.String("INTEGER"),
},
{
DoubleValue: aws.Float64(0),
IntegerValue: aws.Int64(int64(instanceMemory)),
LongValue: aws.Int64(0),
Name: aws.String("MEMORY"),
Type: aws.String("INTEGER"),
},
}
fake.expectedCPU = 2560
fake.expectedMemory = 3072
fake.expectedRemaining = []*ecs.Resource{
{
DoubleValue: aws.Float64(0),
IntegerValue: aws.Int64(int64(instanceCPU - fake.expectedCPU)),
LongValue: aws.Int64(0),
Name: aws.String("CPU"),
Type: aws.String("INTEGER"),
},
{
DoubleValue: aws.Float64(0),
IntegerValue: aws.Int64(int64(instanceMemory - fake.expectedMemory)),
LongValue: aws.Int64(0),
Name: aws.String("MEMORY"),
Type: aws.String("INTEGER"),
},
}
fake.expectedContainerInstances = []*ecs.ContainerInstance{
NewFakeContainerInstance(fake.expectedRegistered, fake.expectedRemaining),
NewFakeContainerInstance(fake.expectedRegistered, fake.expectedRemaining),
NewFakeContainerInstance(fake.expectedRegistered, fake.expectedRemaining),
}
fake.expectedRegisteredPossible = len(fake.expectedContainerInstances) * ContainersPossible(fake.expectedCPU, fake.expectedMemory, fake.expectedContainerInstances[0].RegisteredResources)
fake.expectedRemainingPossible = len(fake.expectedContainerInstances) * ContainersPossible(fake.expectedCPU, fake.expectedMemory, fake.expectedContainerInstances[0].RemainingResources)
fake.expectedDescribeTasksOutput = &ecs.DescribeTasksOutput{
Tasks: []*ecs.Task{
{Cpu: aws.String(strconv.Itoa(fake.expectedCPU)), Memory: aws.String("1440")},
{Cpu: aws.String("1024"), Memory: aws.String(strconv.Itoa(fake.expectedMemory))},
{Cpu: aws.String("invalidCPU"), Memory: aws.String("invalidMemory")},
},
}
return fake
}
// ListTasksPages fake-paginates listing of ECS Tasks.
func (fake *FakeECS) ListTasksPages(input *ecs.ListTasksInput, pager func(*ecs.ListTasksOutput, bool) bool) error {
if fake.checkCluster && *fake.expectedCluster != *input.Cluster {
fake.t.Errorf("expected cluster name %q but got %q", *fake.expectedCluster, *input.Cluster)
}
output := &ecs.ListTasksOutput{
TaskArns: aws.StringSlice(fake.expectedTaskArns),
}
pager(output, true)
return fake.errorToReturn
}
// DescribeTasks fake-describes ECS Tasks.
//
// Although in reality it's supposed to be related to the Task ARN and all...
// it's actually not. We care just for a few of the fields embedded in each
// Task.
func (fake *FakeECS) DescribeTasks(input *ecs.DescribeTasksInput) (*ecs.DescribeTasksOutput, error) {
return fake.expectedDescribeTasksOutput, fake.errorToReturn
}
func (fake *FakeECS) ListContainerInstances(input *ecs.ListContainerInstancesInput) (*ecs.ListContainerInstancesOutput, error) {
if "ACTIVE" != *input.Status {
fake.t.Errorf("ListContainerInstances should look for ACTIVE only, got: %q", *input.Status)
}
output := &ecs.ListContainerInstancesOutput{
ContainerInstanceArns: aws.StringSlice(fake.expectedContainerInstanceArns),
}
return output, fake.errorToReturn
}
func (fake *FakeECS) DescribeContainerInstances(input *ecs.DescribeContainerInstancesInput) (*ecs.DescribeContainerInstancesOutput, error) {
if fake.checkCluster && *fake.expectedCluster != *input.Cluster {
fake.t.Errorf("expected cluster name %q but got %q", *fake.expectedCluster, *input.Cluster)
}
output := &ecs.DescribeContainerInstancesOutput{
ContainerInstances: fake.expectedContainerInstances,
}
return output, fake.errorToReturn
}
func (fake *FakeECS) ListClustersPages(input *ecs.ListClustersInput, pager func(*ecs.ListClustersOutput, bool) bool) error {
for i := 0; i < len(fake.expectedClusterArns); i++ {
output := &ecs.ListClustersOutput{
ClusterArns: aws.StringSlice(fake.expectedClusterArns[i : i+1]),
}
pager(output, i+1 == len(fake.expectedClusterArns))
}
return fake.errorToReturn
}
// TestSnitcherPublish attempts to fake-publish to CloudWatch.
func TestSnitcher_Publish(t *testing.T) {
fake := &FakeCloudWatch{}
sn := &Snitcher{
Namespace: aws.String("Testable/Namespace"),
CloudWatch: fake,
}
cr := NewClusterResources(aws.String("ecs-self-publishing-cluster"))
cr.Registered["fake.instanceType"] += 5
cr.Registered["another.fakeInstanceType"] += 10
sn.Publish(cr.ToMetricData())
published := fake.payload[0]
metricData := published.MetricData
numMetrics := 0
for _, instanceCounts := range cr.Resources {
for range instanceCounts {
numMetrics++
}
}
if *sn.Namespace != *published.Namespace {
t.Errorf("Expected %q as Namespace, but got %q", *sn.Namespace, *published.Namespace)
}
if numMetrics != len(metricData) {
t.Errorf("Expected %d inputs, but got %d", numMetrics, len(metricData))
}
// Force traversal of err logging.
sn.Publish(metricData)
}
// TestSnitcher_PublishValidate forces Validate() failure (in
// service/cloudwatch/api.go), in this case by missing namespace.
//
// TODO(shatil): add some form of comparison test here.
func TestSnitcher_PublishValidate(t *testing.T) {
sn := &Snitcher{}
cr := NewClusterResources(aws.String("ecs-publish-validate-failure"))
cr.Registered["fake.publishValidateFailure"] += 5
cr.Registered["another.publishValidateFailure"] += 10
sn.Publish(cr.ToMetricData())
}
// TestSnitcher_PublishError traverses error-handling code path.
//
// TODO(shatil): add some form of comparison test here.
func TestSnitcher_PublishError(t *testing.T) {
fake := &FakeCloudWatch{
errorToReturn: errors.New("triggering CloudWatch PutMetricData error"),
}
sn := &Snitcher{
Namespace: aws.String("Publish/Error"),
CloudWatch: fake,
}
cr := NewClusterResources(aws.String("ecs-publish-error"))
cr.Registered["fake.publishError"] += 5
cr.Registered["another.publishError"] += 10
sn.Publish(cr.ToMetricData())
}
func TestSnitcher_DiscoverTasks(t *testing.T) {
fake := NewFakeECS(t)
sn := &Snitcher{
ECS: fake,
}
for page := range sn.DiscoverTasks(fake.expectedCluster) {
for index, arn := range fake.expectedTaskArns {
if arn != *page[index] {
t.Errorf("Expected %q ECS Task ARN but got %q", arn, *page[index])
}
}
}
fake.errorToReturn = errors.New("chan should close even when there's an error")
<-sn.DiscoverTasks(fake.expectedCluster)
}
func TestSnitcher_MeasureResources(t *testing.T) {
fake := NewFakeECS(t)
sn := &Snitcher{ECS: fake}
cpu, memory := sn.MeasureResources(fake.expectedCluster, <-sn.DiscoverTasks(fake.expectedCluster))
if fake.expectedCPU != cpu {
t.Errorf("expected %d CPU Units but got %d", fake.expectedCPU, cpu)
}
if fake.expectedMemory != memory {
t.Errorf("expected %d memory but got %d", fake.expectedMemory, memory)
}
}
func TestSnitcher_MeasureResourcesError(t *testing.T) {
fake := NewFakeECS(t)
fake.errorToReturn = errors.New("cpu, memory ought to be zero when DiscoverTasks errors")
sn := &Snitcher{ECS: fake}
if cpu, memory := sn.MeasureResources(fake.expectedCluster, <-sn.DiscoverTasks(fake.expectedCluster)); cpu+memory != 0 {
t.Errorf("expected cpu, memory to be 0, 0 during error, but got %d, %d", cpu, memory)
}
}
func TestSnitcher_ListContainerInstances(t *testing.T) {
fake := NewFakeECS(t)
sn := &Snitcher{ECS: fake}
for index, arn := range aws.StringValueSlice(sn.ListContainerInstances(fake.expectedCluster)) {
if fake.expectedContainerInstanceArns[index] != arn {
t.Errorf("expected %q among Container Instance ARNs in place of %q", fake.expectedContainerInstanceArns[index], arn)
}
}
fake.errorToReturn = errors.New("during error there should be no Container Instance ARNs")
if actual := len(sn.ListContainerInstances(fake.expectedCluster)); actual != 0 {
t.Errorf("expected 0 Container Instance ARNs but got %d", actual)
}
}
func TestSnitcher_DescribeContainerInstances(t *testing.T) {
fake := NewFakeECS(t)
sn := &Snitcher{ECS: fake}
containerInstances := sn.DescribeContainerInstances(fake.expectedCluster, sn.ListContainerInstances(fake.expectedCluster))
if len(containerInstances) == 0 {
t.Error("expected some containers but got", containerInstances)
}
for index, containerInstance := range containerInstances {
if fake.expectedContainerInstances[index] != containerInstance {
t.Errorf("unexpected order of container instances (%s expected; got %s)", fake.expectedContainerInstances[index], containerInstance)
}
}
fake.errorToReturn = errors.New("there should be no containers returned on error")
if containerInstances := sn.DescribeContainerInstances(fake.expectedCluster, sn.ListContainerInstances(fake.expectedCluster)); len(containerInstances) != 0 {
t.Error(fake.errorToReturn)
}
}
func TestSnitcher_DescribeResourcesByInstanceType(t *testing.T) {
fake := NewFakeECS(t)
sn := &Snitcher{ECS: fake}
measurements := sn.DescribeResourcesByInstanceType(
fake.expectedCluster,
aws.StringSlice(fake.expectedContainerInstanceArns),
fake.expectedCPU,
fake.expectedMemory,
)
if len(measurements) == 0 {
t.Error("expectd some measurements but got:", measurements)
}
}
func Test_getInstanceType(t *testing.T) {
expected := "wanted.2xl"
attributes := []*ecs.Attribute{
{Name: aws.String("ecs.missing-instance-type"), Value: aws.String("missing")},
{Name: aws.String("ecs.instance-type"), Value: aws.String(expected)},
}
if got := getInstanceType(attributes); got != expected {
t.Errorf("getInstanceType() = %v, want %v", got, expected)
}
if got := getInstanceType([]*ecs.Attribute{}); got != "" {
t.Errorf("getInstanceType() = %v, want empty string", got)
}
}
// TestContainersPossible ensures calculation for number of containers possible
// to schedule is correct.
//
// Hardcoding values to ensure accuracy of logic.
func TestContainersPossible(t *testing.T) {
nameCPU := aws.String("CPU")
nameMemory := aws.String("MEMORY")
type args struct {
possible int
cpu int
memory int
resources []*ecs.Resource
}
for _, arg := range []args{
{4, 1024, 2048, []*ecs.Resource{{Name: nameCPU, IntegerValue: aws.Int64(8192)}, {Name: nameMemory, IntegerValue: aws.Int64(8192)}}},
{0, 1024, 2048, []*ecs.Resource{{Name: nameCPU, IntegerValue: aws.Int64(0)}, {Name: nameMemory, IntegerValue: aws.Int64(8192)}}},
{3, 1024, 1024, []*ecs.Resource{{Name: nameCPU, IntegerValue: aws.Int64(3072)}, {Name: nameMemory, IntegerValue: aws.Int64(8192)}}},
} {
if got := ContainersPossible(arg.cpu, arg.memory, arg.resources); got != arg.possible {
t.Errorf("expected ContainersPossible() = %d; got %d", arg.possible, got)
}
}
}
func TestSnitcher_DiscoverClusters(t *testing.T) {
fake := NewFakeECS(t)
sn := &Snitcher{ECS: fake}
clusterNames := sn.DiscoverClusters()
for _, arn := range fake.expectedClusterArns {
name := aws.StringValue(<-clusterNames)
if !strings.HasSuffix(arn, name) {
t.Errorf("expected cluster ARN %q to end with cluster name %q", arn, name)
}
}
}
func TestSnitcher_DiscoverClustersError(t *testing.T) {
// For some reason errorToReturn doesn't work right if NewFakeECS constructor is used here like this:
// fake = NewFakeECS(t)
// fake.errorToReturn = errors.New("traverse if err != nil")
fake := &FakeECS{
errorToReturn: errors.New("traverse if err != nil"),
}
sn := &Snitcher{ECS: fake}
<-sn.DiscoverClusters()
}
func TestSnitcher_WithAWS(t *testing.T) {
sn := &Snitcher{}
if sn != sn.WithAWS() {
t.Errorf("expected Snitcher to modify and return itself")
}
if sn.CloudWatch == nil {
t.Errorf("expected Snitcher to have CloudWatch client")
}
if sn.ECS == nil {
t.Errorf("expected Snitcher to have ECS client")
}
}
func TestRun(t *testing.T) {
cw := &FakeCloudWatch{}
ecs := NewFakeECS(t)
// ListTasksPages and DescribeContainerInstances check for matching cluster
// name, which in this case we don't want.
ecs.checkCluster = false
sn := &Snitcher{
CloudWatch: cw,
ECS: ecs,
Namespace: aws.String("Collector/Test"),
ShouldPublish: aws.Bool(true),
}
Run(sn)
if len(cw.payload) == 0 {
t.Error("missing FakeCloudWatch payload after test")
}
}
func TestSnitcher_MeasureClusterEmpty(t *testing.T) {
// Ensure empty response from FakeECS.
ecs := &FakeECS{
expectedDescribeTasksOutput: &ecs.DescribeTasksOutput{},
}
sn := &Snitcher{
ECS: ecs,
}
actual := sn.MeasureCluster(aws.String("this cluster doesn't exist"))
if len(actual) != 0 {
t.Errorf("expected 0 data points but got %d", len(actual))
}
}