grafana/pkg/services/alerting/notifiers/base_test.go

222 lines
6.0 KiB
Go
Raw Normal View History

package notifiers
import (
"context"
"testing"
2018-05-20 11:12:10 -05:00
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/alerting"
"github.com/grafana/grafana/pkg/services/alerting/models"
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
Encryption: Refactor securejsondata.SecureJsonData to stop relying on global functions (#38865) * Encryption: Add support to encrypt/decrypt sjd * Add datasources.Service as a proxy to datasources db operations * Encrypt ds.SecureJsonData before calling SQLStore * Move ds cache code into ds service * Fix tlsmanager tests * Fix pluginproxy tests * Remove some securejsondata.GetEncryptedJsonData usages * Add pluginsettings.Service as a proxy for plugin settings db operations * Add AlertNotificationService as a proxy for alert notification db operations * Remove some securejsondata.GetEncryptedJsonData usages * Remove more securejsondata.GetEncryptedJsonData usages * Fix lint errors * Minor fixes * Remove encryption global functions usages from ngalert * Fix lint errors * Minor fixes * Minor fixes * Remove securejsondata.DecryptedValue usage * Refactor the refactor * Remove securejsondata.DecryptedValue usage * Move securejsondata to migrations package * Move securejsondata to migrations package * Minor fix * Fix integration test * Fix integration tests * Undo undesired changes * Fix tests * Add context.Context into encryption methods * Fix tests * Fix tests * Fix tests * Trigger CI * Fix test * Add names to params of encryption service interface * Remove bus from CacheServiceImpl * Add logging * Add keys to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Add missing key to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Undo changes in markdown files * Fix formatting * Add context to secrets service * Rename decryptSecureJsonData to decryptSecureJsonDataFn * Name args in GetDecryptedValueFn * Add template back to NewAlertmanagerNotifier * Copy GetDecryptedValueFn to ngalert * Add logging to pluginsettings * Fix pluginsettings test Co-authored-by: Tania B <yalyna.ts@gmail.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
2021-10-07 09:33:50 -05:00
"github.com/grafana/grafana/pkg/services/validations"
)
func TestShouldSendAlertNotification(t *testing.T) {
tnow := time.Now()
tcs := []struct {
2018-06-15 09:27:20 -05:00
name string
prevState models.AlertStateType
newState models.AlertStateType
2018-06-15 09:27:20 -05:00
sendReminder bool
frequency time.Duration
state *models.AlertNotificationState
expect bool
}{
{
name: "pending -> ok should not trigger an notification",
newState: models.AlertStateOK,
prevState: models.AlertStatePending,
sendReminder: false,
expect: false,
},
{
name: "ok -> alerting should trigger an notification",
newState: models.AlertStateAlerting,
prevState: models.AlertStateOK,
sendReminder: false,
expect: true,
},
2018-06-15 09:27:20 -05:00
{
name: "ok -> pending should not trigger an notification",
newState: models.AlertStatePending,
prevState: models.AlertStateOK,
sendReminder: false,
expect: false,
2018-06-15 09:27:20 -05:00
},
{
name: "ok -> ok should not trigger an notification",
newState: models.AlertStateOK,
prevState: models.AlertStateOK,
2018-06-15 09:27:20 -05:00
sendReminder: false,
expect: false,
2018-06-15 09:27:20 -05:00
},
{
name: "ok -> ok with reminder should not trigger an notification",
newState: models.AlertStateOK,
prevState: models.AlertStateOK,
2018-06-15 09:27:20 -05:00
sendReminder: true,
expect: false,
},
{
name: "alerting -> ok should trigger an notification",
newState: models.AlertStateOK,
prevState: models.AlertStateAlerting,
sendReminder: false,
expect: true,
2018-06-15 09:27:20 -05:00
},
{
name: "alerting -> ok should trigger an notification when reminders enabled",
newState: models.AlertStateOK,
prevState: models.AlertStateAlerting,
frequency: time.Minute * 10,
2018-06-15 09:27:20 -05:00
sendReminder: true,
state: &models.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
expect: true,
},
{
name: "alerting -> alerting with reminder and no state should trigger",
newState: models.AlertStateAlerting,
prevState: models.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
expect: true,
},
{
name: "alerting -> alerting with reminder and last notification sent 1 minute ago should not trigger",
newState: models.AlertStateAlerting,
prevState: models.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
state: &models.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
expect: false,
},
{
name: "alerting -> alerting with reminder and last notification sent 11 minutes ago should trigger",
newState: models.AlertStateAlerting,
prevState: models.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
state: &models.AlertNotificationState{UpdatedAt: tnow.Add(-11 * time.Minute).Unix()},
expect: true,
},
{
name: "OK -> alerting with notification state pending and updated 30 seconds ago should not trigger",
newState: models.AlertStateAlerting,
prevState: models.AlertStateOK,
state: &models.AlertNotificationState{State: models.AlertNotificationStatePending, UpdatedAt: tnow.Add(-30 * time.Second).Unix()},
expect: false,
},
{
name: "OK -> alerting with notification state pending and updated 2 minutes ago should trigger",
newState: models.AlertStateAlerting,
prevState: models.AlertStateOK,
state: &models.AlertNotificationState{State: models.AlertNotificationStatePending, UpdatedAt: tnow.Add(-2 * time.Minute).Unix()},
expect: true,
},
{
name: "unknown -> ok",
prevState: models.AlertStateUnknown,
newState: models.AlertStateOK,
expect: false,
},
{
name: "unknown -> pending",
prevState: models.AlertStateUnknown,
newState: models.AlertStatePending,
expect: false,
},
{
name: "unknown -> alerting",
prevState: models.AlertStateUnknown,
newState: models.AlertStateAlerting,
expect: true,
2018-06-15 09:27:20 -05:00
},
{
name: "no_data -> pending",
prevState: models.AlertStateNoData,
newState: models.AlertStatePending,
expect: false,
},
{
name: "no_data -> ok",
prevState: models.AlertStateNoData,
newState: models.AlertStateOK,
expect: true,
},
}
for _, tc := range tcs {
evalContext := alerting.NewEvalContext(context.Background(), &alerting.Rule{
State: tc.prevState,
}, &validations.OSSPluginRequestValidator{}, nil, nil, nil, annotationstest.NewFakeAnnotationsRepo())
2018-06-26 07:13:45 -05:00
if tc.state == nil {
tc.state = &models.AlertNotificationState{}
}
evalContext.Rule.State = tc.newState
nb := &NotifierBase{SendReminder: tc.sendReminder, Frequency: tc.frequency}
r := nb.ShouldNotify(evalContext.Ctx, evalContext, tc.state)
assert.Equal(t, r, tc.expect, "failed test %s. expected %+v to return: %v", tc.name, tc, tc.expect)
}
}
func TestBaseNotifier(t *testing.T) {
bJSON := simplejson.New()
model := &models.AlertNotification{
Uid: "1",
Name: "name",
Type: "email",
Settings: bJSON,
}
t.Run("can parse false value", func(t *testing.T) {
bJSON.Set("uploadImage", false)
base := NewNotifierBase(model, nil)
require.False(t, base.UploadImage)
})
t.Run("can parse true value", func(t *testing.T) {
bJSON.Set("uploadImage", true)
base := NewNotifierBase(model, nil)
require.True(t, base.UploadImage)
})
t.Run("default value should be true for backwards compatibility", func(t *testing.T) {
base := NewNotifierBase(model, nil)
require.True(t, base.UploadImage)
})
t.Run("default value should be false for backwards compatibility", func(t *testing.T) {
base := NewNotifierBase(model, nil)
require.False(t, base.DisableResolveMessage)
})
}