base_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package notifiers
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestShouldSendAlertNotification(t *testing.T) {
  12. tnow := time.Now()
  13. tcs := []struct {
  14. name string
  15. prevState m.AlertStateType
  16. newState m.AlertStateType
  17. sendReminder bool
  18. frequency time.Duration
  19. state *m.AlertNotificationState
  20. expect bool
  21. }{
  22. {
  23. name: "pending -> ok should not trigger an notification",
  24. newState: m.AlertStateOK,
  25. prevState: m.AlertStatePending,
  26. sendReminder: false,
  27. expect: false,
  28. },
  29. {
  30. name: "ok -> alerting should trigger an notification",
  31. newState: m.AlertStateAlerting,
  32. prevState: m.AlertStateOK,
  33. sendReminder: false,
  34. expect: true,
  35. },
  36. {
  37. name: "ok -> pending should not trigger an notification",
  38. newState: m.AlertStatePending,
  39. prevState: m.AlertStateOK,
  40. sendReminder: false,
  41. expect: false,
  42. },
  43. {
  44. name: "ok -> ok should not trigger an notification",
  45. newState: m.AlertStateOK,
  46. prevState: m.AlertStateOK,
  47. sendReminder: false,
  48. expect: false,
  49. },
  50. {
  51. name: "ok -> ok with reminder should not trigger an notification",
  52. newState: m.AlertStateOK,
  53. prevState: m.AlertStateOK,
  54. sendReminder: true,
  55. expect: false,
  56. },
  57. {
  58. name: "alerting -> ok should trigger an notification",
  59. newState: m.AlertStateOK,
  60. prevState: m.AlertStateAlerting,
  61. sendReminder: false,
  62. expect: true,
  63. },
  64. {
  65. name: "alerting -> ok should trigger an notification when reminders enabled",
  66. newState: m.AlertStateOK,
  67. prevState: m.AlertStateAlerting,
  68. frequency: time.Minute * 10,
  69. sendReminder: true,
  70. state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
  71. expect: true,
  72. },
  73. {
  74. name: "alerting -> alerting with reminder and no state should trigger",
  75. newState: m.AlertStateAlerting,
  76. prevState: m.AlertStateAlerting,
  77. frequency: time.Minute * 10,
  78. sendReminder: true,
  79. expect: true,
  80. },
  81. {
  82. name: "alerting -> alerting with reminder and last notification sent 1 minute ago should not trigger",
  83. newState: m.AlertStateAlerting,
  84. prevState: m.AlertStateAlerting,
  85. frequency: time.Minute * 10,
  86. sendReminder: true,
  87. state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
  88. expect: false,
  89. },
  90. {
  91. name: "alerting -> alerting with reminder and last notifciation sent 11 minutes ago should trigger",
  92. newState: m.AlertStateAlerting,
  93. prevState: m.AlertStateAlerting,
  94. frequency: time.Minute * 10,
  95. sendReminder: true,
  96. state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-11 * time.Minute).Unix()},
  97. expect: true,
  98. },
  99. {
  100. name: "OK -> alerting with notifciation state pending and updated 30 seconds ago should not trigger",
  101. newState: m.AlertStateAlerting,
  102. prevState: m.AlertStateOK,
  103. state: &m.AlertNotificationState{State: m.AlertNotificationStatePending, UpdatedAt: tnow.Add(-30 * time.Second).Unix()},
  104. expect: false,
  105. },
  106. {
  107. name: "OK -> alerting with notifciation state pending and updated 2 minutes ago should trigger",
  108. newState: m.AlertStateAlerting,
  109. prevState: m.AlertStateOK,
  110. state: &m.AlertNotificationState{State: m.AlertNotificationStatePending, UpdatedAt: tnow.Add(-2 * time.Minute).Unix()},
  111. expect: true,
  112. },
  113. {
  114. name: "unknown -> ok",
  115. prevState: m.AlertStateUnknown,
  116. newState: m.AlertStateOK,
  117. expect: false,
  118. },
  119. {
  120. name: "unknown -> pending",
  121. prevState: m.AlertStateUnknown,
  122. newState: m.AlertStatePending,
  123. expect: false,
  124. },
  125. {
  126. name: "unknown -> alerting",
  127. prevState: m.AlertStateUnknown,
  128. newState: m.AlertStateAlerting,
  129. expect: true,
  130. },
  131. }
  132. for _, tc := range tcs {
  133. evalContext := alerting.NewEvalContext(context.TODO(), &alerting.Rule{
  134. State: tc.prevState,
  135. })
  136. if tc.state == nil {
  137. tc.state = &m.AlertNotificationState{}
  138. }
  139. evalContext.Rule.State = tc.newState
  140. nb := &NotifierBase{SendReminder: tc.sendReminder, Frequency: tc.frequency}
  141. if nb.ShouldNotify(evalContext.Ctx, evalContext, tc.state) != tc.expect {
  142. t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect)
  143. }
  144. }
  145. }
  146. func TestBaseNotifier(t *testing.T) {
  147. Convey("default constructor for notifiers", t, func() {
  148. bJson := simplejson.New()
  149. model := &m.AlertNotification{
  150. Id: 1,
  151. Name: "name",
  152. Type: "email",
  153. Settings: bJson,
  154. }
  155. Convey("can parse false value", func() {
  156. bJson.Set("uploadImage", false)
  157. base := NewNotifierBase(model)
  158. So(base.UploadImage, ShouldBeFalse)
  159. })
  160. Convey("can parse true value", func() {
  161. bJson.Set("uploadImage", true)
  162. base := NewNotifierBase(model)
  163. So(base.UploadImage, ShouldBeTrue)
  164. })
  165. Convey("default value should be true for backwards compatibility", func() {
  166. base := NewNotifierBase(model)
  167. So(base.UploadImage, ShouldBeTrue)
  168. })
  169. Convey("default value should be false for backwards compatibility", func() {
  170. base := NewNotifierBase(model)
  171. So(base.DisableResolveMessage, ShouldBeFalse)
  172. })
  173. })
  174. }