grafana/pkg/services/alerting/alert_rule_test.go

86 lines
1.8 KiB
Go

package alerting
import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
. "github.com/smartystreets/goconvey/convey"
)
func TestAlertRuleModel(t *testing.T) {
Convey("Testing alert rule", t, func() {
Convey("Can parse seconds", func() {
seconds := getTimeDurationStringToSeconds("10s")
So(seconds, ShouldEqual, 10)
})
Convey("Can parse minutes", func() {
seconds := getTimeDurationStringToSeconds("10m")
So(seconds, ShouldEqual, 600)
})
Convey("Can parse hours", func() {
seconds := getTimeDurationStringToSeconds("1h")
So(seconds, ShouldEqual, 3600)
})
Convey("defaults to seconds", func() {
seconds := getTimeDurationStringToSeconds("1o")
So(seconds, ShouldEqual, 1)
})
Convey("", func() {
json := `
{
"name": "name2",
"description": "desc2",
"handler": 0,
"enabled": true,
"critical": {
"value": 20,
"op": ">"
},
"warn": {
"value": 10,
"op": ">"
},
"frequency": "60s",
"query": {
"from": "5m",
"refId": "A",
"to": "now",
"query": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)",
"datasourceId": 1
},
"transform": {
"method": "avg",
"name": "aggregation"
}
}
`
alertJSON, jsonErr := simplejson.NewJson([]byte(json))
So(jsonErr, ShouldBeNil)
alert := &models.Alert{
Id: 1,
OrgId: 1,
DashboardId: 1,
PanelId: 1,
Settings: alertJSON,
}
alertRule, err := NewAlertRuleFromDBModel(alert)
So(err, ShouldBeNil)
So(alertRule.Critical.Operator, ShouldEqual, ">")
So(alertRule.Critical.Value, ShouldEqual, 20)
So(alertRule.Warning.Operator, ShouldEqual, ">")
So(alertRule.Warning.Value, ShouldEqual, 10)
})
})
}