@@ -188,6 +188,7 @@ func TestMonitor_EventBridgeSuccess(t *testing.T) {
188
188
h .Equals (t , result .NodeName , dnsNodeName )
189
189
h .Assert (t , result .PostDrainTask != nil , "PostDrainTask should have been set" )
190
190
h .Assert (t , result .PreDrainTask != nil , "PreDrainTask should have been set" )
191
+ if event .ID == asgLifecycleEvent .ID { h .Assert (t , result .CancelDrainTask != nil , "CancelDrainTask should have been set" ) }
191
192
err = result .PostDrainTask (result , node.Node {})
192
193
h .Ok (t , err )
193
194
default :
@@ -273,6 +274,7 @@ func TestMonitor_AsgDirectToSqsSuccess(t *testing.T) {
273
274
h .Equals (t , result .NodeName , dnsNodeName )
274
275
h .Assert (t , result .PostDrainTask != nil , "PostDrainTask should have been set" )
275
276
h .Assert (t , result .PreDrainTask != nil , "PreDrainTask should have been set" )
277
+ h .Assert (t , result .CancelDrainTask != nil , "CancelDrainTask should have been set" )
276
278
err = result .PostDrainTask (result , node.Node {})
277
279
h .Ok (t , err )
278
280
default :
@@ -365,6 +367,7 @@ func TestMonitor_DrainTasks(t *testing.T) {
365
367
h .Equals (st , result .NodeName , dnsNodeName )
366
368
h .Assert (st , result .PostDrainTask != nil , "PostDrainTask should have been set" )
367
369
h .Assert (st , result .PreDrainTask != nil , "PreDrainTask should have been set" )
370
+ if event .ID == asgLifecycleEvent .ID { h .Assert (t , result .CancelDrainTask != nil , "CancelDrainTask should have been set" ) }
368
371
err := result .PostDrainTask (result , node.Node {})
369
372
h .Ok (st , err )
370
373
})
@@ -466,6 +469,7 @@ func TestMonitor_DrainTasks_Errors(t *testing.T) {
466
469
h .Equals (t , result .NodeName , dnsNodeName )
467
470
h .Assert (t , result .PostDrainTask != nil , "PostDrainTask should have been set" )
468
471
h .Assert (t , result .PreDrainTask != nil , "PreDrainTask should have been set" )
472
+ if i == 1 { h .Assert (t , result .CancelDrainTask != nil , "CancelDrainTask should have been set" ) }
469
473
err := result .PostDrainTask (result , node.Node {})
470
474
h .Ok (t , err )
471
475
default :
@@ -909,32 +913,39 @@ func TestMonitor_InstanceNotManaged(t *testing.T) {
909
913
}
910
914
911
915
func TestSendHeartbeats_EarlyClosure (t * testing.T ) {
912
- err := heartbeatTestHelper (nil , 3500 , 1 , 5 )
916
+ err := heartbeatTestHelper (nil , 3500 , 1 , 5 , false )
913
917
h .Ok (t , err )
914
918
h .Assert (t , h .HeartbeatCallCount == 3 , "3 Heartbeat Expected, got %d" , h .HeartbeatCallCount )
915
919
}
916
920
917
921
func TestSendHeartbeats_HeartbeatUntilExpire (t * testing.T ) {
918
- err := heartbeatTestHelper (nil , 8000 , 1 , 5 )
922
+ err := heartbeatTestHelper (nil , 8000 , 1 , 5 , false )
919
923
h .Ok (t , err )
920
924
h .Assert (t , h .HeartbeatCallCount == 5 , "5 Heartbeat Expected, got %d" , h .HeartbeatCallCount )
921
925
}
922
926
923
927
func TestSendHeartbeats_ErrThrottlingASG (t * testing.T ) {
924
928
RecordLifecycleActionHeartbeatErr := awserr .New ("Throttling" , "Rate exceeded" , nil )
925
- err := heartbeatTestHelper (RecordLifecycleActionHeartbeatErr , 8000 , 1 , 6 )
929
+ err := heartbeatTestHelper (RecordLifecycleActionHeartbeatErr , 8000 , 1 , 6 , false )
926
930
h .Ok (t , err )
927
931
h .Assert (t , h .HeartbeatCallCount == 6 , "6 Heartbeat Expected, got %d" , h .HeartbeatCallCount )
928
932
}
929
933
930
934
func TestSendHeartbeats_ErrInvalidTarget (t * testing.T ) {
931
935
RecordLifecycleActionHeartbeatErr := awserr .New ("ValidationError" , "No active Lifecycle Action found" , nil )
932
- err := heartbeatTestHelper (RecordLifecycleActionHeartbeatErr , 6000 , 1 , 4 )
936
+ err := heartbeatTestHelper (RecordLifecycleActionHeartbeatErr , 6000 , 1 , 4 , false )
933
937
h .Ok (t , err )
934
938
h .Assert (t , h .HeartbeatCallCount == 1 , "1 Heartbeat Expected, got %d" , h .HeartbeatCallCount )
935
939
}
936
940
937
- func heartbeatTestHelper (RecordLifecycleActionHeartbeatErr error , sleepMilliSeconds int , heartbeatInterval int , heartbeatUntil int ) error {
941
+
942
+ func TestSendHeartbeats_CancelHeartbeat (t * testing.T ) {
943
+ err := heartbeatTestHelper (nil , 6000 , 1 , 4 , true )
944
+ h .Ok (t , err )
945
+ h .Assert (t , h .HeartbeatCallCount == 2 , "2 Heartbeat Expected, got %d" , h .HeartbeatCallCount )
946
+ }
947
+
948
+ func heartbeatTestHelper (RecordLifecycleActionHeartbeatErr error , sleepMilliSeconds int , heartbeatInterval int , heartbeatUntil int , cancelDrain bool ) error {
938
949
h .HeartbeatCallCount = 0
939
950
940
951
msg , err := getSQSMessageFromEvent (asgLifecycleEvent )
@@ -986,6 +997,16 @@ func heartbeatTestHelper(RecordLifecycleActionHeartbeatErr error, sleepMilliSeco
986
997
return err
987
998
}
988
999
1000
+ if cancelDrain == true {
1001
+ if result .CancelDrainTask == nil {
1002
+ return fmt .Errorf ("CancelDrainTask should have been set" )
1003
+ }
1004
+ time .Sleep (2100 * time .Millisecond )
1005
+ if err := result .CancelDrainTask (result , * testNode ); err != nil {
1006
+ return err
1007
+ }
1008
+ }
1009
+
989
1010
if result .PostDrainTask == nil {
990
1011
return fmt .Errorf ("PostDrainTask should have been set" )
991
1012
}
0 commit comments