2016-06-13 03:40:46 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-06-15 04:39:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-09-06 13:40:12 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-06-13 03:40:46 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
2016-07-27 09:18:10 -05:00
|
|
|
type FakeCondition struct{}
|
|
|
|
|
2016-11-03 09:26:17 -05:00
|
|
|
func (f *FakeCondition) Eval(context *EvalContext) (*ConditionResult, error) {
|
|
|
|
return &ConditionResult{}, nil
|
|
|
|
}
|
2016-07-27 09:18:10 -05:00
|
|
|
|
2016-06-13 03:40:46 -05:00
|
|
|
func TestAlertRuleModel(t *testing.T) {
|
|
|
|
Convey("Testing alert rule", t, func() {
|
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
RegisterCondition("test", func(model *simplejson.Json, index int) (Condition, error) {
|
2016-07-27 09:18:10 -05:00
|
|
|
return &FakeCondition{}, nil
|
|
|
|
})
|
|
|
|
|
2016-06-13 03:40:46 -05:00
|
|
|
Convey("Can parse seconds", func() {
|
2016-10-11 10:36:30 -05:00
|
|
|
seconds, _ := getTimeDurationStringToSeconds("10s")
|
2016-06-13 03:40:46 -05:00
|
|
|
So(seconds, ShouldEqual, 10)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can parse minutes", func() {
|
2016-10-11 10:36:30 -05:00
|
|
|
seconds, _ := getTimeDurationStringToSeconds("10m")
|
2016-06-13 03:40:46 -05:00
|
|
|
So(seconds, ShouldEqual, 600)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can parse hours", func() {
|
2016-10-11 10:36:30 -05:00
|
|
|
seconds, _ := getTimeDurationStringToSeconds("1h")
|
2016-06-13 03:40:46 -05:00
|
|
|
So(seconds, ShouldEqual, 3600)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("defaults to seconds", func() {
|
2016-10-11 10:36:30 -05:00
|
|
|
seconds, _ := getTimeDurationStringToSeconds("1o")
|
2016-06-13 03:40:46 -05:00
|
|
|
So(seconds, ShouldEqual, 1)
|
|
|
|
})
|
2016-06-15 04:39:25 -05:00
|
|
|
|
2016-10-11 10:36:30 -05:00
|
|
|
Convey("should return err for empty string", func() {
|
|
|
|
_, err := getTimeDurationStringToSeconds("")
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2016-07-19 10:45:37 -05:00
|
|
|
Convey("can construct alert rule model", func() {
|
2016-06-15 04:39:25 -05:00
|
|
|
json := `
|
|
|
|
{
|
|
|
|
"name": "name2",
|
|
|
|
"description": "desc2",
|
|
|
|
"handler": 0,
|
2016-09-06 13:40:12 -05:00
|
|
|
"noDataMode": "critical",
|
2016-06-15 04:39:25 -05:00
|
|
|
"enabled": true,
|
|
|
|
"frequency": "60s",
|
2016-07-19 09:15:26 -05:00
|
|
|
"conditions": [
|
|
|
|
{
|
2016-07-27 09:18:10 -05:00
|
|
|
"type": "test",
|
|
|
|
"prop": 123
|
2016-07-26 05:29:52 -05:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"notifications": [
|
|
|
|
{"id": 1134},
|
|
|
|
{"id": 22}
|
|
|
|
]
|
2016-06-15 04:39:25 -05:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
|
|
|
|
So(jsonErr, ShouldBeNil)
|
|
|
|
|
2016-09-06 13:40:12 -05:00
|
|
|
alert := &m.Alert{
|
2016-06-15 04:39:25 -05:00
|
|
|
Id: 1,
|
|
|
|
OrgId: 1,
|
|
|
|
DashboardId: 1,
|
|
|
|
PanelId: 1,
|
|
|
|
|
|
|
|
Settings: alertJSON,
|
|
|
|
}
|
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
alertRule, err := NewRuleFromDBAlert(alert)
|
2016-06-15 04:39:25 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2016-08-11 09:55:08 -05:00
|
|
|
So(len(alertRule.Conditions), ShouldEqual, 1)
|
2016-07-19 10:45:37 -05:00
|
|
|
|
2016-07-26 05:29:52 -05:00
|
|
|
Convey("Can read notifications", func() {
|
|
|
|
So(len(alertRule.Notifications), ShouldEqual, 2)
|
|
|
|
})
|
2016-06-15 04:39:25 -05:00
|
|
|
})
|
2016-06-13 03:40:46 -05:00
|
|
|
})
|
|
|
|
}
|