Skip to content

Commit 4ae48b4

Browse files
authored
Handle scheduled events immediately in IMDS mode, the same as queue processor mode (#661)
* Handle scheduled events immediately in IMDS mode, the same as queue processor mode * Update unit tests to expect StartTime to be roughly the same as time.Now()
1 parent 9a985a6 commit 4ae48b4

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

pkg/monitor/scheduledevent/scheduled-event-monitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (m ScheduledEventMonitor) checkForScheduledEvents() ([]monitor.Interruption
105105
Description: fmt.Sprintf("%s will occur between %s and %s because %s\n", scheduledEvent.Code, scheduledEvent.NotBefore, scheduledEvent.NotAfter, scheduledEvent.Description),
106106
State: scheduledEvent.State,
107107
NodeName: m.NodeName,
108-
StartTime: notBefore,
108+
StartTime: time.Now(),
109109
EndTime: notAfter,
110110
PreDrainTask: preDrainFunc,
111111
})

pkg/monitor/scheduledevent/scheduled-event-monitor_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"net/http/httptest"
1919
"strings"
2020
"testing"
21+
"time"
2122

2223
"github.com/aws/aws-node-termination-handler/pkg/ec2metadata"
2324
"github.com/aws/aws-node-termination-handler/pkg/monitor"
@@ -47,6 +48,11 @@ var scheduledEventResponse = []byte(`[{
4748
"State": "` + scheduledEventState + `"
4849
}]`)
4950

51+
// oneSecondAgo returns a time.Time object representing one second prior to now
52+
func oneSecondAgo() time.Time {
53+
return time.Now().Add(time.Second * -1)
54+
}
55+
5056
func TestMonitor_Success(t *testing.T) {
5157
var requestPath string = ec2metadata.ScheduledEventPath
5258

@@ -71,7 +77,7 @@ func TestMonitor_Success(t *testing.T) {
7177
h.Equals(t, scheduledEventId, result.EventID)
7278
h.Equals(t, scheduledevent.ScheduledEventKind, result.Kind)
7379
h.Equals(t, scheduledEventState, result.State)
74-
h.Equals(t, expScheduledEventStartTimeFmt, result.StartTime.String())
80+
h.TimeWithinRange(t, result.StartTime, oneSecondAgo(), time.Now())
7581
h.Equals(t, expScheduledEventEndTimeFmt, result.EndTime.String())
7682

7783
h.Assert(t, strings.Contains(result.Description, scheduledEventCode),
@@ -126,7 +132,7 @@ func TestMonitor_CanceledEvent(t *testing.T) {
126132
h.Equals(t, scheduledEventId, result.EventID)
127133
h.Equals(t, scheduledevent.ScheduledEventKind, result.Kind)
128134
h.Equals(t, state, result.State)
129-
h.Equals(t, expScheduledEventStartTimeFmt, result.StartTime.String())
135+
h.TimeWithinRange(t, result.StartTime, oneSecondAgo(), time.Now())
130136
h.Equals(t, expScheduledEventEndTimeFmt, result.EndTime.String())
131137

132138
h.Assert(t, strings.Contains(result.Description, scheduledEventCode),
@@ -253,7 +259,7 @@ func TestMonitor_EndTimeParseFail(t *testing.T) {
253259
h.Equals(t, scheduledEventId, result.EventID)
254260
h.Equals(t, scheduledevent.ScheduledEventKind, result.Kind)
255261
h.Equals(t, scheduledEventState, result.State)
256-
h.Equals(t, expScheduledEventStartTimeFmt, result.StartTime.String())
262+
h.TimeWithinRange(t, result.StartTime, oneSecondAgo(), time.Now())
257263
h.Equals(t, expScheduledEventStartTimeFmt, result.EndTime.String())
258264

259265
h.Assert(t, strings.Contains(result.Description, scheduledEventCode),

pkg/test/helpers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"reflect"
2020
"runtime"
2121
"testing"
22+
"time"
2223
)
2324

2425
// Assert fails the test if the condition is false.
@@ -57,3 +58,12 @@ func Equals(tb testing.TB, exp, act interface{}) {
5758
}
5859

5960
}
61+
62+
// TimeWithinRange fails the test if act is not after lowerBound or not before upperBound
63+
func TimeWithinRange(tb testing.TB, act time.Time, lowerBound time.Time, upperBound time.Time) {
64+
if !(act.After(lowerBound) && act.Before(upperBound)) {
65+
_, file, line, _ := runtime.Caller(1)
66+
fmt.Printf("\033[31m%s:%d:\n\n\tlower bound: %#v\n\n\tgot: %#v\n\n\tupper bound: %#v\033[39m\n\n", filepath.Base(file), line, lowerBound, act, upperBound)
67+
tb.FailNow()
68+
}
69+
}

0 commit comments

Comments
 (0)