alert_rule_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. func TestAlertRuleModel(t *testing.T) {
  9. Convey("Testing alert rule", t, func() {
  10. Convey("Can parse seconds", func() {
  11. seconds := getTimeDurationStringToSeconds("10s")
  12. So(seconds, ShouldEqual, 10)
  13. })
  14. Convey("Can parse minutes", func() {
  15. seconds := getTimeDurationStringToSeconds("10m")
  16. So(seconds, ShouldEqual, 600)
  17. })
  18. Convey("Can parse hours", func() {
  19. seconds := getTimeDurationStringToSeconds("1h")
  20. So(seconds, ShouldEqual, 3600)
  21. })
  22. Convey("defaults to seconds", func() {
  23. seconds := getTimeDurationStringToSeconds("1o")
  24. So(seconds, ShouldEqual, 1)
  25. })
  26. Convey("", func() {
  27. json := `
  28. {
  29. "name": "name2",
  30. "description": "desc2",
  31. "handler": 0,
  32. "enabled": true,
  33. "frequency": "60s",
  34. "conditions": [
  35. {
  36. "type": "query",
  37. "query": {
  38. "params": ["A", "5m", "now"],
  39. "datasourceId": 1,
  40. "query": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
  41. },
  42. "reducer": {"type": "avg", "params": []},
  43. "evaluator": {"type": ">", "params": [100]}
  44. }
  45. ]
  46. }
  47. `
  48. alertJSON, jsonErr := simplejson.NewJson([]byte(json))
  49. So(jsonErr, ShouldBeNil)
  50. alert := &models.Alert{
  51. Id: 1,
  52. OrgId: 1,
  53. DashboardId: 1,
  54. PanelId: 1,
  55. Settings: alertJSON,
  56. }
  57. alertRule, err := NewAlertRuleFromDBModel2(alert)
  58. So(err, ShouldBeNil)
  59. So(alertRule.Conditions, ShouldHaveLength, 1)
  60. })
  61. })
  62. }