mirror of
https://github.com/grafana/grafana.git
synced 2024-11-27 11:20:27 -06:00
caa1314f44
* Build: use golangci-lint as a make command * Since gometalinter was deprecated in favor of golangci-lint so it was replaced by it. Responsibilities held by the gometalinter was moved to golangci-lint * There was some changes in implementation (that was also mentioned in the code comment) between the tools, which uncovered couple errors in the code. Those issues were either solved or disabled by the inline comments * Introduce the golangci-lint config, to make their configuration more manageable * Build: replace backend-lint.sh script with make
118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
package notifiers
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"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,
|
|
})
|
|
|
|
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/" }`
|
|
|
|
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, ShouldEqual, "http://127.0.0.1:9093/")
|
|
})
|
|
})
|
|
})
|
|
}
|