@@ -2,13 +2,13 @@ package internal_test
2
2
3
3
import (
4
4
"encoding/json"
5
- "testing"
6
-
7
5
"github.com/aws/aws-sdk-go-v2/service/autoscaling/types"
8
6
"github.com/franela/goblin"
9
7
. "github.com/onsi/gomega"
10
8
"github.com/stretchr/testify/assert"
11
9
"github.com/stretchr/testify/require"
10
+ "testing"
11
+ "time"
12
12
13
13
"github.com/spacelift-io/awsautoscalr/internal"
14
14
)
@@ -17,6 +17,7 @@ func TestState_StrayInstances(t *testing.T) {
17
17
const asgName = "asg-name"
18
18
const instanceID = "instance-id"
19
19
const failedToTerminateInstanceID = "instance-id2"
20
+ cfg := internal.RuntimeConfig {}
20
21
asg := & types.AutoScalingGroup {
21
22
AutoScalingGroupName : nullable (asgName ),
22
23
MinSize : nullable (int32 (1 )),
@@ -46,7 +47,7 @@ func TestState_StrayInstances(t *testing.T) {
46
47
},
47
48
}
48
49
49
- state , err := internal .NewState (workerPool , asg )
50
+ state , err := internal .NewState (workerPool , asg , cfg )
50
51
require .NoError (t , err )
51
52
52
53
strayInstances := state .StrayInstances ()
@@ -60,6 +61,7 @@ func TestState(t *testing.T) {
60
61
g .Describe ("State" , func () {
61
62
var asg * types.AutoScalingGroup
62
63
var workerPool * internal.WorkerPool
64
+ var cfg internal.RuntimeConfig
63
65
64
66
var sut * internal.State
65
67
@@ -76,9 +78,10 @@ func TestState(t *testing.T) {
76
78
DesiredCapacity : nullable (int32 (3 )),
77
79
}
78
80
workerPool = & internal.WorkerPool {}
81
+ cfg = internal.RuntimeConfig {}
79
82
})
80
83
81
- g .JustBeforeEach (func () { sut , err = internal .NewState (workerPool , asg ) })
84
+ g .JustBeforeEach (func () { sut , err = internal .NewState (workerPool , asg , cfg ) })
82
85
83
86
g .Describe ("when the ASG is invalid" , func () {
84
87
g .Describe ("when the name is not set" , func () {
@@ -210,10 +213,10 @@ func TestState(t *testing.T) {
210
213
})
211
214
})
212
215
213
- g .Describe ("IdleWorkers " , func () {
214
- var idleWorkers []internal.Worker
216
+ g .Describe ("ScaleableWorkers " , func () {
217
+ var scalableWorkers []internal.Worker
215
218
216
- g .JustBeforeEach (func () { idleWorkers = sut .IdleWorkers () })
219
+ g .JustBeforeEach (func () { scalableWorkers = sut .ScalableWorkers () })
217
220
218
221
g .BeforeEach (func () {
219
222
workerPool .Workers = []internal.Worker {
@@ -223,8 +226,8 @@ func TestState(t *testing.T) {
223
226
})
224
227
225
228
g .It ("should return the idle workers" , func () {
226
- Expect (idleWorkers ).To (HaveLen (1 ))
227
- Expect (idleWorkers [0 ].ID ).To (Equal ("idle" ))
229
+ Expect (scalableWorkers ).To (HaveLen (1 ))
230
+ Expect (scalableWorkers [0 ].ID ).To (Equal ("idle" ))
228
231
})
229
232
})
230
233
@@ -243,10 +246,20 @@ func TestState(t *testing.T) {
243
246
}
244
247
workerPool = & internal.WorkerPool {}
245
248
246
- sut = & internal.State {
247
- WorkerPool : workerPool ,
248
- ASG : asg ,
249
+ asg = & types.AutoScalingGroup {
250
+ AutoScalingGroupName : nullable ("asg-name" ),
251
+ MinSize : nullable (int32 (1 )),
252
+ MaxSize : nullable (int32 (2 )),
253
+ DesiredCapacity : nullable (int32 (2 )),
254
+ }
255
+ workerPool = & internal.WorkerPool {}
256
+ cfg = internal.RuntimeConfig {
257
+ AutoscalingScaleDownDelay : 50 ,
249
258
}
259
+
260
+ var err error
261
+ sut , err = internal .NewState (workerPool , asg , cfg )
262
+ Expect (err ).NotTo (HaveOccurred ())
250
263
})
251
264
252
265
g .JustBeforeEach (func () {
@@ -401,6 +414,24 @@ func TestState(t *testing.T) {
401
414
Expect (decision .Comments ).To (Equal ([]string {"removing idle workers" }))
402
415
})
403
416
})
417
+
418
+ g .Describe ("when waiting some time for idle workers" , func () {
419
+ g .BeforeEach (func () {
420
+ workerPool .Workers = []internal.Worker {
421
+ {ID : "busy" , Busy : true },
422
+ {ID : "idle" , Busy : false , CreatedAt : int32 (time .Now ().Unix ())},
423
+ }
424
+ })
425
+
426
+ g .It ("should not scale down because the worker is busy and not enough time has passed" , func () {
427
+ Expect (decision .ScalingDirection ).To (Equal (internal .ScalingDirectionNone ))
428
+ Expect (decision .ScalingSize ).To (BeZero ())
429
+ Expect (decision .Comments ).To (Equal ([]string {
430
+ "autoscaling group exactly at the right size" ,
431
+ }))
432
+ })
433
+ })
434
+
404
435
})
405
436
})
406
437
})
0 commit comments