Skip to content

Commit

Permalink
Fix missing map initialization in ParseData()
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckal777 committed Feb 25, 2022
1 parent a00dd2a commit 88016f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
12 changes: 12 additions & 0 deletions plugin/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ var _ = Describe("NotifyScheduled", func() {
Expect(result).To(BeTrue())
})

It("should trigger after 12:00 with previous time being zero value", func() {
currentDate := time.Date(2022, time.February, 21, 13, 0, 0, 0, time.UTC)
result := makeSchedule().ShouldNotify(NotificationData{
State: "operational",
Time: currentDate,
}, NotificationData{
State: "operational",
Time: time.Time{},
})
Expect(result).To(BeTrue())
})

It("should not trigger more than once a day", func() {
currentDate := time.Date(2022, time.February, 21, 14, 0, 0, 0, time.UTC)
result := makeSchedule().ShouldNotify(NotificationData{
Expand Down
6 changes: 5 additions & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func ParseData(node *v1.Node) (Data, error) {
return Data{}, fmt.Errorf("failed to parse json value in data annotation: %w", err)
}
}
if data.LastNotificationTimes == nil {
data.LastNotificationTimes = make(map[string]time.Time)
}
return data, nil
}

Expand Down Expand Up @@ -164,7 +167,7 @@ func Apply(state NodeState, node *v1.Node, data *Data, params plugin.Parameters)
return state.Label(), nil
}

// notifyDefault is a default NodeState.Transition implementation that checks
// transitionDefault is a default NodeState.Transition implementation that checks
// each specified transition in order and returns the next state. If len(trans)
// is 0, the current state is returned.
func transitionDefault(params plugin.Parameters, current NodeStateLabel, trans []Transition) (NodeStateLabel, error) {
Expand Down Expand Up @@ -207,6 +210,7 @@ func notifyDefault(params plugin.Parameters, data *Data,
return err
}
data.LastNotificationTimes[notifyPlugin.Name] = now
data.LastNotificationState = label
}
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sapcc/maintenance-controller/constants"
"github.com/sapcc/maintenance-controller/plugin"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -246,3 +247,15 @@ var _ = Describe("Apply", func() {
})

})

var _ = Describe("ParseData", func() {

It("should initialize the notification times map", func() {
var node v1.Node
node.Annotations = map[string]string{constants.DataAnnotationKey: "{}"}
data, err := ParseData(&node)
Expect(err).To(Succeed())
Expect(data.LastNotificationTimes).ToNot(BeNil())
})

})

0 comments on commit 88016f6

Please sign in to comment.