rule_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package alerting
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. type FakeCondition struct{}
  9. func (f *FakeCondition) Eval(context *EvalContext) (*ConditionResult, error) {
  10. return &ConditionResult{}, nil
  11. }
  12. func TestAlertRuleFrequencyParsing(t *testing.T) {
  13. tcs := []struct {
  14. input string
  15. err error
  16. result int64
  17. }{
  18. {input: "10s", result: 10},
  19. {input: "10m", result: 600},
  20. {input: "1h", result: 3600},
  21. {input: "1o", result: 1},
  22. {input: "0s", err: ErrFrequencyCannotBeZeroOrLess},
  23. {input: "0m", err: ErrFrequencyCannotBeZeroOrLess},
  24. {input: "0h", err: ErrFrequencyCannotBeZeroOrLess},
  25. {input: "0", err: ErrFrequencyCannotBeZeroOrLess},
  26. {input: "-1s", err: ErrFrequencyCouldNotBeParsed},
  27. }
  28. for _, tc := range tcs {
  29. r, err := getTimeDurationStringToSeconds(tc.input)
  30. if err != tc.err {
  31. t.Errorf("expected error: '%v' got: '%v'", tc.err, err)
  32. return
  33. }
  34. if r != tc.result {
  35. t.Errorf("expected result: %d got %d", tc.result, r)
  36. }
  37. }
  38. }
  39. func TestAlertRuleModel(t *testing.T) {
  40. Convey("Testing alert rule", t, func() {
  41. RegisterCondition("test", func(model *simplejson.Json, index int) (Condition, error) {
  42. return &FakeCondition{}, nil
  43. })
  44. Convey("should return err for empty string", func() {
  45. _, err := getTimeDurationStringToSeconds("")
  46. So(err, ShouldNotBeNil)
  47. })
  48. Convey("can construct alert rule model", func() {
  49. json := `
  50. {
  51. "name": "name2",
  52. "description": "desc2",
  53. "handler": 0,
  54. "noDataMode": "critical",
  55. "enabled": true,
  56. "frequency": "60s",
  57. "conditions": [
  58. {
  59. "type": "test",
  60. "prop": 123
  61. }
  62. ],
  63. "notifications": [
  64. {"id": 1134},
  65. {"id": 22}
  66. ]
  67. }
  68. `
  69. alertJSON, jsonErr := simplejson.NewJson([]byte(json))
  70. So(jsonErr, ShouldBeNil)
  71. alert := &m.Alert{
  72. Id: 1,
  73. OrgId: 1,
  74. DashboardId: 1,
  75. PanelId: 1,
  76. Settings: alertJSON,
  77. }
  78. alertRule, err := NewRuleFromDBAlert(alert)
  79. So(err, ShouldBeNil)
  80. So(len(alertRule.Conditions), ShouldEqual, 1)
  81. Convey("Can read notifications", func() {
  82. So(len(alertRule.Notifications), ShouldEqual, 2)
  83. })
  84. })
  85. Convey("can construct alert rule model with invalid frequency", func() {
  86. json := `
  87. {
  88. "name": "name2",
  89. "description": "desc2",
  90. "noDataMode": "critical",
  91. "enabled": true,
  92. "frequency": "0s",
  93. "conditions": [ { "type": "test", "prop": 123 } ],
  94. "notifications": []
  95. }`
  96. alertJSON, jsonErr := simplejson.NewJson([]byte(json))
  97. So(jsonErr, ShouldBeNil)
  98. alert := &m.Alert{
  99. Id: 1,
  100. OrgId: 1,
  101. DashboardId: 1,
  102. PanelId: 1,
  103. Frequency: 0,
  104. Settings: alertJSON,
  105. }
  106. alertRule, err := NewRuleFromDBAlert(alert)
  107. So(err, ShouldBeNil)
  108. So(alertRule.Frequency, ShouldEqual, 60)
  109. })
  110. })
  111. }