grafana/pkg/services/alerting/notifiers/alertmanager_test.go
Joan López de la Franca Beltran 6415d2802e
Plugins: Requests validator (#30445)
* Introduce PluginRequestValidator abstraction with a NoOp implementation

* Update PluginRequestValidator abstraction to use the dsURL instead

* Inject PluginRequestValidator into the HTTPServer and validate requests going through data source proxy

* Inject PluginRequestValidator into the BackendPluginManager and validate requests going through it

* Validate requests going through QueryMetrics & QueryMetricsV2

* Validate BackendPluginManager health requests

* Fix backend plugins manager tests

* Validate requests going through alerting service

* Fix tests

* fix tests

* goimports

Co-authored-by: Leonard Gram <leo@xlson.com>
2021-02-03 20:47:45 +01:00

138 lines
3.5 KiB
Go

package notifiers
import (
"context"
"testing"
"github.com/grafana/grafana/pkg/services/validations"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/alerting"
. "github.com/smartystreets/goconvey/convey"
)
func TestReplaceIllegalCharswithUnderscore(t *testing.T) {
cases := []struct {
input string
expected string
}{
{
input: "foobar",
expected: "foobar",
},
{
input: `foo.,\][!?#="~*^&+|<>\'bar09_09`,
expected: "foo____________________bar09_09",
},
}
for _, c := range cases {
assert.Equal(t, replaceIllegalCharsInLabelname(c.input), c.expected)
}
}
func TestWhenAlertManagerShouldNotify(t *testing.T) {
tcs := []struct {
prevState models.AlertStateType
newState models.AlertStateType
expect bool
}{
{
prevState: models.AlertStatePending,
newState: models.AlertStateOK,
expect: false,
},
{
prevState: models.AlertStateAlerting,
newState: models.AlertStateOK,
expect: true,
},
{
prevState: models.AlertStateOK,
newState: models.AlertStatePending,
expect: false,
},
{
prevState: models.AlertStateUnknown,
newState: models.AlertStatePending,
expect: false,
},
}
for _, tc := range tcs {
am := &AlertmanagerNotifier{log: log.New("test.logger")}
evalContext := alerting.NewEvalContext(context.Background(), &alerting.Rule{
State: tc.prevState,
}, &validations.OSSPluginRequestValidator{})
evalContext.Rule.State = tc.newState
res := am.ShouldNotify(context.TODO(), evalContext, &models.AlertNotificationState{})
if res != tc.expect {
t.Errorf("got %v expected %v", res, tc.expect)
}
}
}
//nolint:goconst
func TestAlertmanagerNotifier(t *testing.T) {
Convey("Alertmanager notifier tests", t, func() {
Convey("Parsing alert notification from settings", func() {
Convey("empty settings should return error", func() {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "alertmanager",
Type: "alertmanager",
Settings: settingsJSON,
}
_, err := NewAlertmanagerNotifier(model)
So(err, ShouldNotBeNil)
})
Convey("from settings", func() {
json := `{ "url": "http://127.0.0.1:9093/", "basicAuthUser": "user", "basicAuthPassword": "password" }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "alertmanager",
Type: "alertmanager",
Settings: settingsJSON,
}
not, err := NewAlertmanagerNotifier(model)
alertmanagerNotifier := not.(*AlertmanagerNotifier)
So(err, ShouldBeNil)
So(alertmanagerNotifier.BasicAuthUser, ShouldEqual, "user")
So(alertmanagerNotifier.BasicAuthPassword, ShouldEqual, "password")
So(alertmanagerNotifier.URL, ShouldResemble, []string{"http://127.0.0.1:9093/"})
})
Convey("from settings with multiple alertmanager", func() {
json := `{ "url": "http://alertmanager1:9093,http://alertmanager2:9093" }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "alertmanager",
Type: "alertmanager",
Settings: settingsJSON,
}
not, err := NewAlertmanagerNotifier(model)
alertmanagerNotifier := not.(*AlertmanagerNotifier)
So(err, ShouldBeNil)
So(alertmanagerNotifier.URL, ShouldResemble, []string{"http://alertmanager1:9093", "http://alertmanager2:9093"})
})
})
})
}