alertmanager_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package notifiers
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func TestReplaceIllegalCharswithUnderscore(t *testing.T) {
  13. cases := []struct {
  14. input string
  15. expected string
  16. }{
  17. {
  18. input: "foobar",
  19. expected: "foobar",
  20. },
  21. {
  22. input: `foo.,\][!?#="~*^&+|<>\'bar09_09`,
  23. expected: "foo____________________bar09_09",
  24. },
  25. }
  26. for _, c := range cases {
  27. assert.Equal(t, replaceIllegalCharsInLabelname(c.input), c.expected)
  28. }
  29. }
  30. func TestWhenAlertManagerShouldNotify(t *testing.T) {
  31. tcs := []struct {
  32. prevState models.AlertStateType
  33. newState models.AlertStateType
  34. expect bool
  35. }{
  36. {
  37. prevState: models.AlertStatePending,
  38. newState: models.AlertStateOK,
  39. expect: false,
  40. },
  41. {
  42. prevState: models.AlertStateAlerting,
  43. newState: models.AlertStateOK,
  44. expect: true,
  45. },
  46. {
  47. prevState: models.AlertStateOK,
  48. newState: models.AlertStatePending,
  49. expect: false,
  50. },
  51. {
  52. prevState: models.AlertStateUnknown,
  53. newState: models.AlertStatePending,
  54. expect: false,
  55. },
  56. }
  57. for _, tc := range tcs {
  58. am := &AlertmanagerNotifier{log: log.New("test.logger")}
  59. evalContext := alerting.NewEvalContext(context.Background(), &alerting.Rule{
  60. State: tc.prevState,
  61. })
  62. evalContext.Rule.State = tc.newState
  63. res := am.ShouldNotify(context.TODO(), evalContext, &models.AlertNotificationState{})
  64. if res != tc.expect {
  65. t.Errorf("got %v expected %v", res, tc.expect)
  66. }
  67. }
  68. }
  69. //nolint:goconst
  70. func TestAlertmanagerNotifier(t *testing.T) {
  71. Convey("Alertmanager notifier tests", t, func() {
  72. Convey("Parsing alert notification from settings", func() {
  73. Convey("empty settings should return error", func() {
  74. json := `{ }`
  75. settingsJSON, _ := simplejson.NewJson([]byte(json))
  76. model := &models.AlertNotification{
  77. Name: "alertmanager",
  78. Type: "alertmanager",
  79. Settings: settingsJSON,
  80. }
  81. _, err := NewAlertmanagerNotifier(model)
  82. So(err, ShouldNotBeNil)
  83. })
  84. Convey("from settings", func() {
  85. json := `{ "url": "http://127.0.0.1:9093/" }`
  86. settingsJSON, _ := simplejson.NewJson([]byte(json))
  87. model := &models.AlertNotification{
  88. Name: "alertmanager",
  89. Type: "alertmanager",
  90. Settings: settingsJSON,
  91. }
  92. not, err := NewAlertmanagerNotifier(model)
  93. alertmanagerNotifier := not.(*AlertmanagerNotifier)
  94. So(err, ShouldBeNil)
  95. So(alertmanagerNotifier.URL, ShouldEqual, "http://127.0.0.1:9093/")
  96. })
  97. })
  98. })
  99. }