rule_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package alerting
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. type FakeCondition struct{}
  9. func (f *FakeCondition) Eval(context *EvalContext) {}
  10. func TestAlertRuleModel(t *testing.T) {
  11. Convey("Testing alert rule", t, func() {
  12. RegisterCondition("test", func(model *simplejson.Json, index int) (Condition, error) {
  13. return &FakeCondition{}, nil
  14. })
  15. Convey("Can parse seconds", func() {
  16. seconds := getTimeDurationStringToSeconds("10s")
  17. So(seconds, ShouldEqual, 10)
  18. })
  19. Convey("Can parse minutes", func() {
  20. seconds := getTimeDurationStringToSeconds("10m")
  21. So(seconds, ShouldEqual, 600)
  22. })
  23. Convey("Can parse hours", func() {
  24. seconds := getTimeDurationStringToSeconds("1h")
  25. So(seconds, ShouldEqual, 3600)
  26. })
  27. Convey("defaults to seconds", func() {
  28. seconds := getTimeDurationStringToSeconds("1o")
  29. So(seconds, ShouldEqual, 1)
  30. })
  31. Convey("can construct alert rule model", func() {
  32. json := `
  33. {
  34. "name": "name2",
  35. "description": "desc2",
  36. "handler": 0,
  37. "enabled": true,
  38. "frequency": "60s",
  39. "conditions": [
  40. {
  41. "type": "test",
  42. "prop": 123
  43. }
  44. ],
  45. "notifications": [
  46. {"id": 1134},
  47. {"id": 22}
  48. ]
  49. }
  50. `
  51. alertJSON, jsonErr := simplejson.NewJson([]byte(json))
  52. So(jsonErr, ShouldBeNil)
  53. alert := &models.Alert{
  54. Id: 1,
  55. OrgId: 1,
  56. DashboardId: 1,
  57. PanelId: 1,
  58. Settings: alertJSON,
  59. }
  60. alertRule, err := NewRuleFromDBAlert(alert)
  61. So(err, ShouldBeNil)
  62. So(len(alertRule.Conditions), ShouldEqual, 1)
  63. Convey("Can read notifications", func() {
  64. So(len(alertRule.Notifications), ShouldEqual, 2)
  65. })
  66. })
  67. })
  68. }