Skip to content

Commit

Permalink
fix event_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nix O Live committed Sep 27, 2019
1 parent c18edeb commit ba83a04
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 109 deletions.
31 changes: 12 additions & 19 deletions checker/event.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checker

import (
"fmt"
"github.com/moira-alert/moira"
)

Expand Down Expand Up @@ -33,15 +32,13 @@ func (triggerChecker *TriggerChecker) compareTriggerStates(currentCheck moira.Ch
currentCheck.SuppressedState = lastStateSuppressedValue

maintenanceInfo, maintenanceTimestamp := getMaintenanceInfo(lastCheck, nil)
eventInfo := isStateChanged(currentStateValue, lastStateValue, currentCheckTimestamp, lastCheck.GetEventTimestamp(), lastStateSuppressed, lastStateSuppressedValue, maintenanceInfo)
if eventInfo == nil { //!needSend
eventInfo, needSend := isStateChanged(currentStateValue, lastStateValue, currentCheckTimestamp, lastCheck.GetEventTimestamp(), lastStateSuppressed, lastStateSuppressedValue, maintenanceInfo)
if !needSend {
if maintenanceTimestamp < currentCheckTimestamp {
currentCheck.Suppressed = false
currentCheck.SuppressedState = ""
}
return currentCheck, nil
} else if eventInfo.Interval == nil && eventInfo.Maintenance == nil {
eventInfo = nil
}

currentCheck.EventTimestamp = currentCheckTimestamp
Expand All @@ -57,8 +54,6 @@ func (triggerChecker *TriggerChecker) compareTriggerStates(currentCheck moira.Ch
currentCheck.Suppressed = false
currentCheck.SuppressedState = ""

println("---------------------------------------------------------------")
println("eventInfo is nil:", eventInfo == nil, fmt.Sprintf("%#v", eventInfo))
err := triggerChecker.database.PushNotificationEvent(&moira.NotificationEvent{
IsTriggerEvent: true,
TriggerID: triggerChecker.triggerID,
Expand Down Expand Up @@ -87,15 +82,13 @@ func (triggerChecker *TriggerChecker) compareMetricStates(metric string, current
currentState.SuppressedState = lastState.SuppressedState

maintenanceInfo, maintenanceTimestamp := getMaintenanceInfo(triggerChecker.lastCheck, &currentState)
eventInfo := isStateChanged(currentState.State, lastState.State, currentState.Timestamp, lastState.GetEventTimestamp(), lastState.Suppressed, lastState.SuppressedState, maintenanceInfo)
if eventInfo == nil { //!needSend
eventInfo, needSend := isStateChanged(currentState.State, lastState.State, currentState.Timestamp, lastState.GetEventTimestamp(), lastState.Suppressed, lastState.SuppressedState, maintenanceInfo)
if !needSend {
if maintenanceTimestamp < currentState.Timestamp {
currentState.Suppressed = false
currentState.SuppressedState = ""
}
return currentState, nil
} else if eventInfo.Interval == nil && eventInfo.Maintenance == nil {
eventInfo = nil
}

// State was changed. Set event timestamp. Event will be not sent if it is suppressed
Expand All @@ -112,8 +105,6 @@ func (triggerChecker *TriggerChecker) compareMetricStates(metric string, current
currentState.Suppressed = false
currentState.SuppressedState = ""

println("---------------------------------------------------------------")
println("eventInfo is nil:", eventInfo == nil, fmt.Sprintf("%#v", eventInfo))
err := triggerChecker.database.PushNotificationEvent(&moira.NotificationEvent{
TriggerID: triggerChecker.triggerID,
State: currentState.State,
Expand All @@ -137,21 +128,23 @@ func (triggerChecker *TriggerChecker) isTriggerSuppressed(timestamp int64, maint
return !triggerChecker.trigger.Schedule.IsScheduleAllows(timestamp) || maintenanceTimestamp >= timestamp
}

func isStateChanged(currentStateValue moira.State, lastStateValue moira.State, currentStateTimestamp int64, lastStateEventTimestamp int64, isLastCheckSuppressed bool, lastStateSuppressedValue moira.State, maintenanceInfo moira.MaintenanceInfo) *moira.EventInfo {
func isStateChanged(currentStateValue moira.State, lastStateValue moira.State, currentStateTimestamp int64, lastStateEventTimestamp int64, isLastCheckSuppressed bool, lastStateSuppressedValue moira.State, maintenanceInfo moira.MaintenanceInfo) (*moira.EventInfo, bool) {
println(currentStateTimestamp, lastStateEventTimestamp, lastStateValue)
if !isLastCheckSuppressed && currentStateValue != lastStateValue {
return &moira.EventInfo{}
return nil, true
}

if isLastCheckSuppressed && currentStateValue != lastStateSuppressedValue {
return &moira.EventInfo{Maintenance: &maintenanceInfo}
return &moira.EventInfo{Maintenance: &maintenanceInfo}, true
}

remindInterval, ok := badStateReminder[currentStateValue]
println("remindInterval", remindInterval, ok)
if ok && needRemindAgain(currentStateTimestamp, lastStateEventTimestamp, remindInterval) {
interval := remindInterval / 3600
return &moira.EventInfo{Interval: &interval}
return &moira.EventInfo{Interval: &interval}, true
}

return nil
return nil, false
}

func needRemindAgain(currentStateTimestamp, lastStateEventTimestamp, remindInterval int64) bool {
Expand Down
112 changes: 22 additions & 90 deletions checker/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package checker

import (
"fmt"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/mock/moira-alert"
"github.com/op/go-logging"
. "github.com/smartystreets/goconvey/convey"
"testing"
)

func newMocks(t *testing.T) (dataBase *mock_moira_alert.MockDatabase, mockCtrl *gomock.Controller) {
Expand Down Expand Up @@ -642,11 +640,6 @@ func TestTriggerMaintenance(t *testing.T) {
}

func TestIsStateChanged(t *testing.T) {
startMaintenanceUser := "testStartMtUser"
startMaintenanceTime := int64(123)
stopMaintenanceUser := "testStopMtUser"
stopMaintenanceTime := int64(1230)

Convey("isStateChanged tests", t, func() {
var lastCheckTest = moira.CheckData{
Score: 6000,
Expand All @@ -662,95 +655,34 @@ func TestIsStateChanged(t *testing.T) {
Timestamp: 1504509981,
}

var currentMetricTest = moira.MetricState{
EventTimestamp: 1504449789,
State: moira.StateWARN,
Suppressed: true,
Timestamp: 1504509380,
}

var lastMetricsTest = moira.MetricState{
EventTimestamp: 1504449789,
State: moira.StateNODATA,
Suppressed: true,
Timestamp: 1504509380,
Maintenance: 1552723340,
}

Convey("Test needSendEvents for trigger", func() {
Convey("Start Maintenance not start user and time", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval.")
eventInfo := isStateChanged(currentCheckTest.State, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp(), lastCheckTest.Suppressed, lastCheckTest.SuppressedState, moira.MaintenanceInfo{})
So(eventInfo, ShouldNotBeNil)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(event.CreateMessage(time.Local), ShouldResemble, actual)
})
Convey("Start Maintenance", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval. Maintenance was set by %v at %v.", startMaintenanceUser, time.Unix(startMaintenanceTime, 0).Format("15:04 02.01.2006"))
lastCheckTest.MaintenanceInfo.Set(&startMaintenanceUser, &startMaintenanceTime, nil, nil)
eventInfo := isStateChanged(currentCheckTest.State, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp(), lastCheckTest.Suppressed, lastCheckTest.SuppressedState, lastCheckTest.MaintenanceInfo)
So(eventInfo, ShouldNotBeNil)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(event.CreateMessage(time.Local), ShouldResemble, actual)
})
Convey("Stop Maintenance", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval. Maintenance was set by %v at %v and removed by %v at %v.", startMaintenanceUser, time.Unix(startMaintenanceTime, 0).Format("15:04 02.01.2006"), stopMaintenanceUser, time.Unix(stopMaintenanceTime, 0).Format("15:04 02.01.2006"))
lastCheckTest.MaintenanceInfo.Set(&startMaintenanceUser, &startMaintenanceTime, &stopMaintenanceUser, &stopMaintenanceTime)
eventInfo := isStateChanged(currentCheckTest.State, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp(), lastCheckTest.Suppressed, lastCheckTest.SuppressedState, lastCheckTest.MaintenanceInfo)
So(eventInfo, ShouldNotBeNil)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(event.CreateMessage(time.Local), ShouldResemble, actual)
})
Convey("Stop Maintenance by one user", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval. Maintenance was set by %v at %v and removed at %v.", startMaintenanceUser, time.Unix(startMaintenanceTime, 0).Format("15:04 02.01.2006"), time.Unix(stopMaintenanceTime, 0).Format("15:04 02.01.2006"))
lastCheckTest.MaintenanceInfo.Set(&startMaintenanceUser, &startMaintenanceTime, &startMaintenanceUser, &stopMaintenanceTime)
eventInfo := isStateChanged(currentCheckTest.State, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp(), lastCheckTest.Suppressed, lastCheckTest.SuppressedState, lastCheckTest.MaintenanceInfo)
So(eventInfo, ShouldNotBeNil)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(event.CreateMessage(time.Local), ShouldResemble, actual)
Convey("Test is state changed", func() {
Convey("If is last check suppressed and current state not equal last state", func() {
lastCheckTest.Suppressed = false
eventInfo, needSend := isStateChanged(currentCheckTest.State, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp()-1, lastCheckTest.Suppressed, lastCheckTest.SuppressedState, moira.MaintenanceInfo{})
So(eventInfo, ShouldBeNil)
So(needSend, ShouldBeTrue)
})

Convey("Stop Maintenance not start user and time", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval.")
lastCheckTest.MaintenanceInfo.Set(nil, nil, &stopMaintenanceUser, &stopMaintenanceTime)
eventInfo := isStateChanged(currentCheckTest.State, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp(), lastCheckTest.Suppressed, lastCheckTest.SuppressedState, lastCheckTest.MaintenanceInfo)
Convey("Create EventInfo with MaintenanceInfo", func() {
maintenanceInfo := moira.MaintenanceInfo{}
eventInfo, needSend := isStateChanged(currentCheckTest.State, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp(), lastCheckTest.Suppressed, lastCheckTest.SuppressedState, maintenanceInfo)
So(eventInfo, ShouldNotBeNil)
m := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(m.CreateMessage(time.Local), ShouldResemble, actual)
So(eventInfo, ShouldResemble, &moira.EventInfo{Maintenance: &maintenanceInfo})
So(needSend, ShouldBeTrue)
})
})

Convey("Test needSendEvents for metric", func() {
Convey("Start Maintenance not start user and time", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval.")
eventInfo := isStateChanged(currentMetricTest.State, lastMetricsTest.State, currentMetricTest.Timestamp, lastMetricsTest.GetEventTimestamp(), lastMetricsTest.Suppressed, lastMetricsTest.SuppressedState, moira.MaintenanceInfo{})
So(eventInfo, ShouldNotBeNil)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(event.CreateMessage(time.Local), ShouldResemble, actual)
})
Convey("Start Maintenance", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval. Maintenance was set by %v at %v.", startMaintenanceUser, time.Unix(startMaintenanceTime, 0).Format("15:04 02.01.2006"))
currentMetricTest.MaintenanceInfo.Set(&startMaintenanceUser, &startMaintenanceTime, nil, nil)
eventInfo := isStateChanged(currentMetricTest.State, lastMetricsTest.State, currentMetricTest.Timestamp, lastMetricsTest.GetEventTimestamp(), lastMetricsTest.Suppressed, lastMetricsTest.SuppressedState, currentMetricTest.MaintenanceInfo)
So(eventInfo, ShouldNotBeNil)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(event.CreateMessage(time.Local), ShouldResemble, actual)
})
Convey("Stop Maintenance", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval.")
currentMetricTest.MaintenanceInfo.Set(nil, nil, &stopMaintenanceUser, &stopMaintenanceTime)
eventInfo := isStateChanged(currentMetricTest.State, lastMetricsTest.State, currentMetricTest.Timestamp, lastMetricsTest.GetEventTimestamp(), lastMetricsTest.Suppressed, lastMetricsTest.SuppressedState, currentMetricTest.MaintenanceInfo)
Convey("Create EventInfo with interval", func() {
var interval int64 = 24
eventInfo, needSend := isStateChanged(moira.StateNODATA, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp()-100000, lastCheckTest.Suppressed, moira.StateNODATA, moira.MaintenanceInfo{})
So(eventInfo, ShouldNotBeNil)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(event.CreateMessage(time.Local), ShouldResemble, actual)
So(eventInfo, ShouldResemble, &moira.EventInfo{Interval: &interval})
So(needSend, ShouldBeTrue)
})
Convey("Stop Maintenance not start user and time", func() {
actual := fmt.Sprintf("This metric changed its state during maintenance interval. Maintenance was set by %v at %v and removed by %v at %v.", startMaintenanceUser, time.Unix(startMaintenanceTime, 0).Format("15:04 02.01.2006"), stopMaintenanceUser, time.Unix(stopMaintenanceTime, 0).Format("15:04 02.01.2006"))
currentMetricTest.MaintenanceInfo.Set(&startMaintenanceUser, &startMaintenanceTime, &stopMaintenanceUser, &stopMaintenanceTime)
eventInfo := isStateChanged(currentMetricTest.State, lastMetricsTest.State, currentMetricTest.Timestamp, lastMetricsTest.GetEventTimestamp(), lastMetricsTest.Suppressed, lastMetricsTest.SuppressedState, currentMetricTest.MaintenanceInfo)
event := moira.NotificationEvent{MessageEventInfo: eventInfo}
So(eventInfo, ShouldNotBeNil)
So(event.CreateMessage(time.Local), ShouldResemble, actual)

Convey("No send message", func() {
eventInfo, needSend := isStateChanged(moira.StateNODATA, lastCheckTest.State, currentCheckTest.Timestamp, lastCheckTest.GetEventTimestamp(), lastCheckTest.Suppressed, moira.StateNODATA, moira.MaintenanceInfo{})
So(eventInfo, ShouldBeNil)
So(needSend, ShouldBeFalse)
})
})
})
Expand Down

0 comments on commit ba83a04

Please sign in to comment.