Skip to content

Commit

Permalink
Handle scheduled events immediately in IMDS mode, the same as queue p…
Browse files Browse the repository at this point in the history
…rocessor 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()
  • Loading branch information
snay2 authored Jul 21, 2022
1 parent 9a985a6 commit 4ae48b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/monitor/scheduledevent/scheduled-event-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (m ScheduledEventMonitor) checkForScheduledEvents() ([]monitor.Interruption
Description: fmt.Sprintf("%s will occur between %s and %s because %s\n", scheduledEvent.Code, scheduledEvent.NotBefore, scheduledEvent.NotAfter, scheduledEvent.Description),
State: scheduledEvent.State,
NodeName: m.NodeName,
StartTime: notBefore,
StartTime: time.Now(),
EndTime: notAfter,
PreDrainTask: preDrainFunc,
})
Expand Down
12 changes: 9 additions & 3 deletions pkg/monitor/scheduledevent/scheduled-event-monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

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

// oneSecondAgo returns a time.Time object representing one second prior to now
func oneSecondAgo() time.Time {
return time.Now().Add(time.Second * -1)
}

func TestMonitor_Success(t *testing.T) {
var requestPath string = ec2metadata.ScheduledEventPath

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

h.Assert(t, strings.Contains(result.Description, scheduledEventCode),
Expand Down Expand Up @@ -126,7 +132,7 @@ func TestMonitor_CanceledEvent(t *testing.T) {
h.Equals(t, scheduledEventId, result.EventID)
h.Equals(t, scheduledevent.ScheduledEventKind, result.Kind)
h.Equals(t, state, result.State)
h.Equals(t, expScheduledEventStartTimeFmt, result.StartTime.String())
h.TimeWithinRange(t, result.StartTime, oneSecondAgo(), time.Now())
h.Equals(t, expScheduledEventEndTimeFmt, result.EndTime.String())

h.Assert(t, strings.Contains(result.Description, scheduledEventCode),
Expand Down Expand Up @@ -253,7 +259,7 @@ func TestMonitor_EndTimeParseFail(t *testing.T) {
h.Equals(t, scheduledEventId, result.EventID)
h.Equals(t, scheduledevent.ScheduledEventKind, result.Kind)
h.Equals(t, scheduledEventState, result.State)
h.Equals(t, expScheduledEventStartTimeFmt, result.StartTime.String())
h.TimeWithinRange(t, result.StartTime, oneSecondAgo(), time.Now())
h.Equals(t, expScheduledEventStartTimeFmt, result.EndTime.String())

h.Assert(t, strings.Contains(result.Description, scheduledEventCode),
Expand Down
10 changes: 10 additions & 0 deletions pkg/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"reflect"
"runtime"
"testing"
"time"
)

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

}

// TimeWithinRange fails the test if act is not after lowerBound or not before upperBound
func TimeWithinRange(tb testing.TB, act time.Time, lowerBound time.Time, upperBound time.Time) {
if !(act.After(lowerBound) && act.Before(upperBound)) {
_, file, line, _ := runtime.Caller(1)
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)
tb.FailNow()
}
}

0 comments on commit 4ae48b4

Please sign in to comment.