notifier_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package alerting
  2. import (
  3. "testing"
  4. "reflect"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting/alertstates"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestAlertNotificationExtraction(t *testing.T) {
  11. Convey("Notifier tests", t, func() {
  12. Convey("rules for sending notifications", func() {
  13. dummieNotifier := NotifierImpl{}
  14. result := &AlertResult{
  15. State: alertstates.Critical,
  16. }
  17. notifier := &Notification{
  18. Name: "Test Notifier",
  19. Type: "TestType",
  20. SendCritical: true,
  21. SendWarning: true,
  22. }
  23. Convey("Should send notification", func() {
  24. So(dummieNotifier.ShouldDispath(result, notifier), ShouldBeTrue)
  25. })
  26. Convey("warn:false and state:warn should not send", func() {
  27. result.State = alertstates.Warn
  28. notifier.SendWarning = false
  29. So(dummieNotifier.ShouldDispath(result, notifier), ShouldBeFalse)
  30. })
  31. })
  32. Convey("Parsing alert notification from settings", func() {
  33. Convey("Parsing email", func() {
  34. Convey("empty settings should return error", func() {
  35. json := `{ }`
  36. settingsJSON, _ := simplejson.NewJson([]byte(json))
  37. model := &m.AlertNotification{
  38. Name: "ops",
  39. Type: "email",
  40. Settings: settingsJSON,
  41. }
  42. _, err := NewNotificationFromDBModel(model)
  43. So(err, ShouldNotBeNil)
  44. })
  45. Convey("from settings", func() {
  46. json := `
  47. {
  48. "to": "ops@grafana.org"
  49. }`
  50. settingsJSON, _ := simplejson.NewJson([]byte(json))
  51. model := &m.AlertNotification{
  52. Name: "ops",
  53. Type: "email",
  54. Settings: settingsJSON,
  55. }
  56. not, err := NewNotificationFromDBModel(model)
  57. So(err, ShouldBeNil)
  58. So(not.Name, ShouldEqual, "ops")
  59. So(not.Type, ShouldEqual, "email")
  60. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.EmailNotifier")
  61. email := not.Notifierr.(*EmailNotifier)
  62. So(email.To, ShouldEqual, "ops@grafana.org")
  63. })
  64. })
  65. Convey("Parsing webhook", func() {
  66. Convey("empty settings should return error", func() {
  67. json := `{ }`
  68. settingsJSON, _ := simplejson.NewJson([]byte(json))
  69. model := &m.AlertNotification{
  70. Name: "ops",
  71. Type: "webhook",
  72. Settings: settingsJSON,
  73. }
  74. _, err := NewNotificationFromDBModel(model)
  75. So(err, ShouldNotBeNil)
  76. })
  77. Convey("from settings", func() {
  78. json := `
  79. {
  80. "url": "http://localhost:3000",
  81. "username": "username",
  82. "password": "password"
  83. }`
  84. settingsJSON, _ := simplejson.NewJson([]byte(json))
  85. model := &m.AlertNotification{
  86. Name: "slack",
  87. Type: "webhook",
  88. Settings: settingsJSON,
  89. }
  90. not, err := NewNotificationFromDBModel(model)
  91. So(err, ShouldBeNil)
  92. So(not.Name, ShouldEqual, "slack")
  93. So(not.Type, ShouldEqual, "webhook")
  94. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.WebhookNotifier")
  95. webhook := not.Notifierr.(*WebhookNotifier)
  96. So(webhook.Url, ShouldEqual, "http://localhost:3000")
  97. })
  98. })
  99. })
  100. })
  101. }