extractor_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package alerting
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestAlertRuleExtraction(t *testing.T) {
  10. Convey("Parsing alert rules from dashboard json", t, func() {
  11. RegisterCondition("query", func(model *simplejson.Json, index int) (Condition, error) {
  12. return &FakeCondition{}, nil
  13. })
  14. Convey("Parsing and validating alerts from dashboards", func() {
  15. json := `{
  16. "id": 57,
  17. "title": "Graphite 4",
  18. "originalTitle": "Graphite 4",
  19. "tags": ["graphite"],
  20. "rows": [
  21. {
  22. "panels": [
  23. {
  24. "title": "Active desktop users",
  25. "editable": true,
  26. "type": "graph",
  27. "id": 3,
  28. "targets": [
  29. {
  30. "refId": "A",
  31. "target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
  32. }
  33. ],
  34. "datasource": null,
  35. "alert": {
  36. "name": "name1",
  37. "message": "desc1",
  38. "handler": 1,
  39. "frequency": "60s",
  40. "conditions": [
  41. {
  42. "type": "query",
  43. "query": {"params": ["A", "5m", "now"]},
  44. "reducer": {"type": "avg", "params": []},
  45. "evaluator": {"type": ">", "params": [100]}
  46. }
  47. ]
  48. }
  49. },
  50. {
  51. "title": "Active mobile users",
  52. "id": 4,
  53. "targets": [
  54. {"refId": "A", "target": ""},
  55. {"refId": "B", "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
  56. ],
  57. "datasource": "graphite2",
  58. "alert": {
  59. "name": "name2",
  60. "message": "desc2",
  61. "handler": 0,
  62. "frequency": "60s",
  63. "severity": "warning",
  64. "conditions": [
  65. {
  66. "type": "query",
  67. "query": {"params": ["B", "5m", "now"]},
  68. "reducer": {"type": "avg", "params": []},
  69. "evaluator": {"type": ">", "params": [100]}
  70. }
  71. ]
  72. }
  73. }
  74. ]
  75. }
  76. ]
  77. }`
  78. dashJson, err := simplejson.NewJson([]byte(json))
  79. So(err, ShouldBeNil)
  80. dash := m.NewDashboardFromJson(dashJson)
  81. extractor := NewDashAlertExtractor(dash, 1)
  82. // mock data
  83. defaultDs := &m.DataSource{Id: 12, OrgId: 2, Name: "I am default", IsDefault: true}
  84. graphite2Ds := &m.DataSource{Id: 15, OrgId: 2, Name: "graphite2"}
  85. bus.AddHandler("test", func(query *m.GetDataSourcesQuery) error {
  86. query.Result = []*m.DataSource{defaultDs, graphite2Ds}
  87. return nil
  88. })
  89. bus.AddHandler("test", func(query *m.GetDataSourceByNameQuery) error {
  90. if query.Name == defaultDs.Name {
  91. query.Result = defaultDs
  92. }
  93. if query.Name == graphite2Ds.Name {
  94. query.Result = graphite2Ds
  95. }
  96. return nil
  97. })
  98. alerts, err := extractor.GetAlerts()
  99. Convey("Get rules without error", func() {
  100. So(err, ShouldBeNil)
  101. })
  102. Convey("all properties have been set", func() {
  103. So(len(alerts), ShouldEqual, 2)
  104. for _, v := range alerts {
  105. So(v.DashboardId, ShouldEqual, 57)
  106. So(v.Name, ShouldNotBeEmpty)
  107. So(v.Message, ShouldNotBeEmpty)
  108. }
  109. Convey("should extract handler property", func() {
  110. So(alerts[0].Handler, ShouldEqual, 1)
  111. So(alerts[1].Handler, ShouldEqual, 0)
  112. })
  113. Convey("should extract frequency in seconds", func() {
  114. So(alerts[0].Frequency, ShouldEqual, 60)
  115. So(alerts[1].Frequency, ShouldEqual, 60)
  116. })
  117. Convey("should extract panel idc", func() {
  118. So(alerts[0].PanelId, ShouldEqual, 3)
  119. So(alerts[1].PanelId, ShouldEqual, 4)
  120. })
  121. Convey("should extract name and desc", func() {
  122. So(alerts[0].Name, ShouldEqual, "name1")
  123. So(alerts[0].Message, ShouldEqual, "desc1")
  124. So(alerts[1].Name, ShouldEqual, "name2")
  125. So(alerts[1].Message, ShouldEqual, "desc2")
  126. })
  127. Convey("should set datasourceId", func() {
  128. condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
  129. query := condition.Get("query")
  130. So(query.Get("datasourceId").MustInt64(), ShouldEqual, 12)
  131. })
  132. Convey("should copy query model to condition", func() {
  133. condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
  134. model := condition.Get("query").Get("model")
  135. So(model.Get("target").MustString(), ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)")
  136. })
  137. })
  138. })
  139. })
  140. }