extractor_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. Convey("Parsing and validating alerts from dashboards", func() {
  12. json := `{
  13. "id": 57,
  14. "title": "Graphite 4",
  15. "originalTitle": "Graphite 4",
  16. "tags": ["graphite"],
  17. "rows": [
  18. {
  19. "panels": [
  20. {
  21. "title": "Active desktop users",
  22. "editable": true,
  23. "type": "graph",
  24. "id": 3,
  25. "targets": [
  26. {
  27. "refId": "A",
  28. "target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
  29. }
  30. ],
  31. "datasource": null,
  32. "alert": {
  33. "name": "name1",
  34. "description": "desc1",
  35. "handler": 1,
  36. "enabled": true,
  37. "frequency": "60s",
  38. "severity": "critical",
  39. "conditions": [
  40. {
  41. "type": "query",
  42. "query": {"params": ["A", "5m", "now"]},
  43. "reducer": {"type": "avg", "params": []},
  44. "evaluator": {"type": ">", "params": [100]}
  45. }
  46. ]
  47. }
  48. },
  49. {
  50. "title": "Active mobile users",
  51. "id": 4,
  52. "targets": [
  53. {"refId": "A", "target": ""},
  54. {"refId": "B", "target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"}
  55. ],
  56. "datasource": "graphite2",
  57. "alert": {
  58. "name": "name2",
  59. "description": "desc2",
  60. "handler": 0,
  61. "enabled": true,
  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.Description, 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 Severity property", func() {
  114. So(alerts[0].Severity, ShouldEqual, "critical")
  115. So(alerts[1].Severity, ShouldEqual, "warning")
  116. })
  117. Convey("should extract frequency in seconds", func() {
  118. So(alerts[0].Frequency, ShouldEqual, 60)
  119. So(alerts[1].Frequency, ShouldEqual, 60)
  120. })
  121. Convey("should extract panel idc", func() {
  122. So(alerts[0].PanelId, ShouldEqual, 3)
  123. So(alerts[1].PanelId, ShouldEqual, 4)
  124. })
  125. Convey("should extract name and desc", func() {
  126. So(alerts[0].Name, ShouldEqual, "name1")
  127. So(alerts[0].Description, ShouldEqual, "desc1")
  128. So(alerts[1].Name, ShouldEqual, "name2")
  129. So(alerts[1].Description, ShouldEqual, "desc2")
  130. })
  131. Convey("should set datasourceId", func() {
  132. condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
  133. query := condition.Get("query")
  134. So(query.Get("datasourceId").MustInt64(), ShouldEqual, 12)
  135. })
  136. Convey("should copy query model to condition", func() {
  137. condition := simplejson.NewFromAny(alerts[0].Settings.Get("conditions").MustArray()[0])
  138. model := condition.Get("query").Get("model")
  139. So(model.Get("target").MustString(), ShouldEqual, "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)")
  140. })
  141. })
  142. })
  143. })
  144. }