-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
422 lines (394 loc) · 10.7 KB
/
main_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
package main
import (
"fmt"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/autoscaling"
)
const (
asgName = "asgName"
)
type MockAutoscalingClient struct {
DesiredCapacity int
EmulatedErr string
}
func (m *MockAutoscalingClient) SetDesiredCapacity(input *autoscaling.SetDesiredCapacityInput) (*autoscaling.SetDesiredCapacityOutput, error) {
if m.EmulatedErr != "" {
return nil, awserr.New(m.EmulatedErr, "error", nil)
}
m.DesiredCapacity = int(*input.DesiredCapacity)
return nil, nil
}
func (m *MockAutoscalingClient) getMockCapacity() int {
return m.DesiredCapacity
}
func (m *MockAutoscalingClient) DescribeAutoScalingInstances(input *autoscaling.DescribeAutoScalingInstancesInput) (*autoscaling.DescribeAutoScalingInstancesOutput, error) {
return &autoscaling.DescribeAutoScalingInstancesOutput{AutoScalingInstances: []*autoscaling.InstanceDetails{&autoscaling.InstanceDetails{AutoScalingGroupName: aws.String(asgName)}}}, nil
}
func (m *MockAutoscalingClient) DescribeAutoScalingGroups(input *autoscaling.DescribeAutoScalingGroupsInput) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
return &autoscaling.DescribeAutoScalingGroupsOutput{AutoScalingGroups: []*autoscaling.Group{&autoscaling.Group{DesiredCapacity: aws.Int64(int64(m.DesiredCapacity))}}}, nil
}
func NewMockASG() *ASG {
asg := &ASG{
Name: asgName,
}
return asg
}
func TestSetCapacity(t *testing.T) {
client := &MockAutoscalingClient{}
asg := NewMockASG()
asg.Client = client
newCap := 3
err := asg.SetCapacity(int64(newCap))
if err != nil {
t.Fatalf("error while setting capacity: %v", err)
}
cap := client.getMockCapacity()
if cap != newCap {
t.Fatalf("expected %d, got %d", 3, cap)
}
}
func TestSetCapacityWithActivityInProgress(t *testing.T) {
client := &MockAutoscalingClient{EmulatedErr: autoscaling.ErrCodeScalingActivityInProgressFault}
asg := NewMockASG()
asg.Client = client
newCap := 3
err := asg.SetCapacity(int64(newCap))
if err == nil {
t.Fatalf("expected error, got nil")
}
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() != autoscaling.ErrCodeScalingActivityInProgressFault {
t.Fatalf("expecting %v, got %v", autoscaling.ErrCodeScalingActivityInProgressFault, aerr.Code())
}
}
}
func TestSetCapacityWithContentionFault(t *testing.T) {
client := &MockAutoscalingClient{EmulatedErr: autoscaling.ErrCodeResourceContentionFault}
asg := NewMockASG()
asg.Client = client
newCap := 3
err := asg.SetCapacity(int64(newCap))
if err == nil {
t.Fatalf("expected error, got nil")
}
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() != autoscaling.ErrCodeResourceContentionFault {
t.Fatalf("expecting %v, got %v", autoscaling.ErrCodeResourceContentionFault, aerr.Code())
}
}
}
func TestGetCurrentCapacity(t *testing.T) {
refCap := 2
client := &MockAutoscalingClient{DesiredCapacity: refCap}
asg := NewMockASG()
asg.Client = client
cap, err := asg.GetCurrentCapacity()
if err != nil {
t.Fatalf("error while getting capacity: %v", err)
}
if cap != refCap {
t.Fatalf("expected %d, got %d", refCap, cap)
}
}
func TestAutodetectASGName(t *testing.T) {
instanceName := "i-123456789"
client := &MockAutoscalingClient{}
name, err := autodetectASGName(client, &instanceName)
if err != nil {
t.Fatalf("error while getting ASG name: %v", err)
}
if name != asgName {
t.Fatalf("expected %s, got %s", asgName, name)
}
}
func TestDetermineNewCapacity(tt *testing.T) {
for _, test := range []struct {
name string
startTime int
endTime int
cap int
previousCap int
day time.Weekday
currentHour int
consultantMode bool
expectedCap int
}{
{
name: "During working hours in the mid of the week the cluster should keep its capacity.",
startTime: 9,
endTime: 18,
cap: 2,
previousCap: 2,
day: time.Wednesday,
currentHour: 12,
consultantMode: false,
expectedCap: 2,
},
{
name: "After working hours in the mid of the week the cluster should scale down.",
startTime: 9,
endTime: 18,
cap: 2,
previousCap: 2,
day: time.Wednesday,
currentHour: 19,
consultantMode: false,
expectedCap: 0,
},
{
name: "After working hours in the mid of the week the cluster should stay scaled down.",
startTime: 7,
endTime: 16,
cap: 0,
previousCap: 2,
day: time.Thursday,
currentHour: 2,
consultantMode: false,
expectedCap: 0,
},
{
name: "Before working hours I should scale down!",
startTime: 11,
endTime: 16,
cap: 2,
previousCap: 2,
day: time.Thursday,
currentHour: 8,
consultantMode: false,
expectedCap: 0,
},
{
name: "During the weekend during the day, the cluster should stay scaled down.",
startTime: 9,
endTime: 18,
cap: 0,
previousCap: 2,
day: time.Saturday,
currentHour: 19,
consultantMode: false,
expectedCap: 0,
},
{
name: "During the weekend with consultant mode the cluster should scale up.",
startTime: 9,
endTime: 18,
cap: 0,
previousCap: 2,
day: time.Saturday,
currentHour: 10,
consultantMode: true,
expectedCap: 2,
},
{
name: "During the weekend after working hours with consultant mode the cluster should scale down.",
startTime: 9,
endTime: 18,
cap: 2,
previousCap: 2,
day: time.Saturday,
currentHour: 19,
consultantMode: true,
expectedCap: 0,
},
{
name: "On sunday I should stay off if consuntalt mode is not enabled.",
startTime: 7,
endTime: 17,
cap: 2,
previousCap: 2,
day: time.Sunday,
currentHour: 16,
consultantMode: false,
expectedCap: 0,
},
{
name: "The start time is exactly at the same time I specified (expected comparison).",
startTime: 7,
endTime: 17,
cap: 0,
previousCap: 2,
day: time.Monday,
currentHour: 7,
consultantMode: false,
expectedCap: 2,
},
{
name: "The end time is exactly at the same time I specified (expected comparison).",
startTime: 6,
endTime: 17,
cap: 2,
previousCap: 2,
day: time.Monday,
currentHour: 7,
consultantMode: false,
expectedCap: 2,
},
{
name: "Start time equal to end time is a valid choice, out of hours.",
startTime: 17,
endTime: 17,
cap: 2,
previousCap: 2,
day: time.Monday,
currentHour: 7,
consultantMode: false,
expectedCap: 0,
},
{
name: "Start time equal to end time is a valid choice, at that time.",
startTime: 17,
endTime: 17,
cap: 2,
previousCap: 2,
day: time.Monday,
currentHour: 17,
consultantMode: false,
expectedCap: 2,
},
{
name: "Scale the nodes to 2 if during the day and teh initial size is 0.",
startTime: 7,
endTime: 17,
cap: 0,
previousCap: 2,
day: time.Monday,
currentHour: 12,
consultantMode: false,
expectedCap: 2,
},
} {
tt.Run(fmt.Sprintf("%v", test.name), func(t *testing.T) {
tt.Log(test.name)
cap := determineNewCapacity(test.startTime, test.endTime, test.cap, test.previousCap, test.day, test.currentHour, test.consultantMode)
if cap != test.expectedCap {
t.Errorf("expected %d, got %d", test.expectedCap, cap)
}
})
}
}
func TestValidateParams(tt *testing.T) {
for _, test := range []struct {
name string
startTime int
endTime int
expectedErr bool
}{
{
name: "Normal working ours are valid.",
startTime: 9,
endTime: 18,
expectedErr: false,
},
{
name: "Negative start is invalid.",
startTime: -1,
endTime: 18,
expectedErr: true,
},
{
name: "Negative end is invalid.",
startTime: 0,
endTime: -1,
expectedErr: true,
},
{
name: "0 is not a valid start.",
startTime: 0,
endTime: 18,
expectedErr: true,
},
{
name: "24 as end is valid.",
startTime: 1,
endTime: 24,
expectedErr: false,
},
{
name: "more than 24 as end is invalid.",
startTime: 1,
endTime: 25,
expectedErr: true,
},
} {
tt.Run(fmt.Sprintf("%v", test.name), func(t *testing.T) {
tt.Log(test.name)
err := validateParams(test.startTime, test.endTime)
if (err != nil) != test.expectedErr {
t.Errorf("expected %v, got %v", test.expectedErr, err != nil)
}
})
}
}
func TestSubsequentRun(t *testing.T) {
client := &MockAutoscalingClient{}
asg := NewMockASG()
asg.Client = client
maxCapacity := 3
cap := determineNewCapacity(7, 20, 0, maxCapacity, time.Monday, 10, false)
if cap != maxCapacity {
t.Fatalf("expected %d, got %d", 2, cap)
}
err := updateCapacity(0, cap, asg)
if err != nil {
t.Fatalf("cannot update capacity: %v", err)
}
cap = determineNewCapacity(7, 20, 2, maxCapacity, time.Monday, 20, false)
if cap != maxCapacity {
t.Fatalf("expected %d, got %d", 2, cap)
}
err = updateCapacity(0, cap, asg)
if err != nil {
t.Fatalf("cannot update capacity: %v", err)
}
}
func TestDo(t *testing.T) {
client := &MockAutoscalingClient{}
asg := NewMockASG()
asg.Client = client
d := &downscaler{
startTime: 7,
endTime: 17,
interval: 1 * time.Second,
debug: true,
lastASGSize: 3,
consultantMode: false,
asg: asg,
}
_ = asg.SetCapacity(0) // we start from a size 0 autoscaler
ta := time.Date(2000, 12, 14, 12, 8, 00, 0, time.UTC).Local()
d.do(&ta)
c, _ := asg.GetCurrentCapacity()
// it's during the day so we expect to scale up
if c != 3 {
t.Fatalf("wrong capacity, expected %d have %d", 3, c)
}
_ = asg.SetCapacity(4) // something/somebody scales up to 4 nodes
d.do(&ta)
c, _ = asg.GetCurrentCapacity()
if c != 4 {
t.Fatalf("wrong capacity, expected %d have %d", 4, c)
}
ta = time.Date(2000, 12, 14, 22, 8, 00, 0, time.UTC).Local() // it's late :-)
d.do(&ta)
c, _ = asg.GetCurrentCapacity()
if c != 0 {
t.Fatalf("wrong capacity, expected %d have %d", 0, c)
}
ta = time.Date(2000, 12, 15, 10, 8, 00, 0, time.UTC).Local() // good morning :wave:
d.do(&ta)
c, _ = asg.GetCurrentCapacity()
if c != 4 {
t.Fatalf("wrong capacity, expected %d have %d", 4, c)
}
}
func TestMax(t *testing.T) {
if max(1, 2) != 2 {
t.Fatalf("expected max to be 2")
}
if max(2, 1) != 2 {
t.Fatalf("expected max to be 2")
}
}