alert_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. 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. "critical": {
  34. "value": 20,
  35. "op": ">"
  36. },
  37. "warn": {
  38. "value": 10,
  39. "op": ">"
  40. },
  41. "frequency": "60s",
  42. "query": {
  43. "from": "5m",
  44. "refId": "A",
  45. "to": "now",
  46. "query": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)",
  47. "datasourceId": 1
  48. },
  49. "transform": {
  50. "method": "avg",
  51. "name": "aggregation"
  52. }
  53. }
  54. `
  55. alertJSON, jsonErr := simplejson.NewJson([]byte(json))
  56. So(jsonErr, ShouldBeNil)
  57. alert := &models.Alert{
  58. Id: 1,
  59. OrgId: 1,
  60. DashboardId: 1,
  61. PanelId: 1,
  62. Settings: alertJSON,
  63. }
  64. alertRule, err := NewAlertRuleFromDBModel(alert)
  65. So(err, ShouldBeNil)
  66. So(alertRule.Critical.Operator, ShouldEqual, ">")
  67. So(alertRule.Critical.Value, ShouldEqual, 20)
  68. So(alertRule.Warning.Operator, ShouldEqual, ">")
  69. So(alertRule.Warning.Value, ShouldEqual, 10)
  70. })
  71. })
  72. }