Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle scheduled events immediately in IMDS mode, the same as queue processor mode #661

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}