Chore: Remove GoConvey from sqlstore package (#40753)
* refactor login attempt test * refactor tags saving test * refactor transaction tests * refactor temporary user tests * refactor dashboard version tests * refactor dashboard provisioning tests * refactor alert notification test * refactor alert tests * refactor acl tests
This commit is contained in:
@@ -13,400 +13,423 @@ import (
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAlertNotificationSQLAccess(t *testing.T) {
|
||||
Convey("Testing Alert notification sql access", t, func() {
|
||||
var sqlStore *SQLStore
|
||||
setup := func() {
|
||||
sqlStore := InitTestDB(t)
|
||||
|
||||
// Set up bus handlers
|
||||
bus.AddHandler("deleteAlertNotification", func(cmd *models.DeleteAlertNotificationCommand) error {
|
||||
return sqlStore.DeleteAlertNotification(cmd)
|
||||
})
|
||||
}
|
||||
|
||||
Convey("Alert notification state", func() {
|
||||
var alertID int64 = 7
|
||||
var orgID int64 = 5
|
||||
var notifierID int64 = 10
|
||||
oldTimeNow := timeNow
|
||||
now := time.Date(2018, 9, 30, 0, 0, 0, 0, time.UTC)
|
||||
timeNow = func() time.Time { return now }
|
||||
t.Run("Alert notification state", func(t *testing.T) {
|
||||
setup()
|
||||
var alertID int64 = 7
|
||||
var orgID int64 = 5
|
||||
var notifierID int64 = 10
|
||||
oldTimeNow := timeNow
|
||||
now := time.Date(2018, 9, 30, 0, 0, 0, 0, time.UTC)
|
||||
timeNow = func() time.Time { return now }
|
||||
|
||||
Convey("Get no existing state should create a new state", func() {
|
||||
query := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err := sqlStore.GetOrCreateAlertNotificationState(context.Background(), query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldNotBeNil)
|
||||
So(query.Result.State, ShouldEqual, "unknown")
|
||||
So(query.Result.Version, ShouldEqual, 0)
|
||||
So(query.Result.UpdatedAt, ShouldEqual, now.Unix())
|
||||
defer func() { timeNow = oldTimeNow }()
|
||||
|
||||
Convey("Get existing state should not create a new state", func() {
|
||||
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err := sqlStore.GetOrCreateAlertNotificationState(context.Background(), query2)
|
||||
So(err, ShouldBeNil)
|
||||
So(query2.Result, ShouldNotBeNil)
|
||||
So(query2.Result.Id, ShouldEqual, query.Result.Id)
|
||||
So(query2.Result.UpdatedAt, ShouldEqual, now.Unix())
|
||||
t.Run("Get no existing state should create a new state", func(t *testing.T) {
|
||||
query := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err := sqlStore.GetOrCreateAlertNotificationState(context.Background(), query)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, query.Result)
|
||||
require.Equal(t, models.AlertNotificationStateUnknown, query.Result.State)
|
||||
require.Equal(t, int64(0), query.Result.Version)
|
||||
require.Equal(t, now.Unix(), query.Result.UpdatedAt)
|
||||
|
||||
t.Run("Get existing state should not create a new state", func(t *testing.T) {
|
||||
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err := sqlStore.GetOrCreateAlertNotificationState(context.Background(), query2)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, query2.Result)
|
||||
require.Equal(t, query.Result.Id, query2.Result.Id)
|
||||
require.Equal(t, now.Unix(), query2.Result.UpdatedAt)
|
||||
})
|
||||
|
||||
t.Run("Update existing state to pending with correct version should update database", func(t *testing.T) {
|
||||
s := *query.Result
|
||||
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.Id,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
|
||||
}
|
||||
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(1), cmd.ResultVersion)
|
||||
|
||||
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err = sqlStore.GetOrCreateAlertNotificationState(context.Background(), query2)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(1), query2.Result.Version)
|
||||
require.Equal(t, models.AlertNotificationStatePending, query2.Result.State)
|
||||
require.Equal(t, now.Unix(), query2.Result.UpdatedAt)
|
||||
|
||||
t.Run("Update existing state to completed should update database", func(t *testing.T) {
|
||||
s := *query.Result
|
||||
setStateCmd := models.SetAlertNotificationStateToCompleteCommand{
|
||||
Id: s.Id,
|
||||
Version: cmd.ResultVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToCompleteCommand(context.Background(), &setStateCmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err = sqlStore.GetOrCreateAlertNotificationState(context.Background(), query3)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(2), query3.Result.Version)
|
||||
require.Equal(t, models.AlertNotificationStateCompleted, query3.Result.State)
|
||||
require.Equal(t, now.Unix(), query3.Result.UpdatedAt)
|
||||
})
|
||||
|
||||
Convey("Update existing state to pending with correct version should update database", func() {
|
||||
t.Run("Update existing state to completed should update database. regardless of version", func(t *testing.T) {
|
||||
s := *query.Result
|
||||
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.Id,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
|
||||
unknownVersion := int64(1000)
|
||||
cmd := models.SetAlertNotificationStateToCompleteCommand{
|
||||
Id: s.Id,
|
||||
Version: unknownVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(cmd.ResultVersion, ShouldEqual, 1)
|
||||
|
||||
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err = sqlStore.GetOrCreateAlertNotificationState(context.Background(), query2)
|
||||
So(err, ShouldBeNil)
|
||||
So(query2.Result.Version, ShouldEqual, 1)
|
||||
So(query2.Result.State, ShouldEqual, models.AlertNotificationStatePending)
|
||||
So(query2.Result.UpdatedAt, ShouldEqual, now.Unix())
|
||||
|
||||
Convey("Update existing state to completed should update database", func() {
|
||||
s := *query.Result
|
||||
setStateCmd := models.SetAlertNotificationStateToCompleteCommand{
|
||||
Id: s.Id,
|
||||
Version: cmd.ResultVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToCompleteCommand(context.Background(), &setStateCmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err = sqlStore.GetOrCreateAlertNotificationState(context.Background(), query3)
|
||||
So(err, ShouldBeNil)
|
||||
So(query3.Result.Version, ShouldEqual, 2)
|
||||
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
|
||||
So(query3.Result.UpdatedAt, ShouldEqual, now.Unix())
|
||||
})
|
||||
|
||||
Convey("Update existing state to completed should update database. regardless of version", func() {
|
||||
s := *query.Result
|
||||
unknownVersion := int64(1000)
|
||||
cmd := models.SetAlertNotificationStateToCompleteCommand{
|
||||
Id: s.Id,
|
||||
Version: unknownVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err = sqlStore.GetOrCreateAlertNotificationState(context.Background(), query3)
|
||||
So(err, ShouldBeNil)
|
||||
So(query3.Result.Version, ShouldEqual, unknownVersion+1)
|
||||
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
|
||||
So(query3.Result.UpdatedAt, ShouldEqual, now.Unix())
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Update existing state to pending with incorrect version should return version mismatch error", func() {
|
||||
s := *query.Result
|
||||
s.Version = 1000
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.NotifierId,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
|
||||
})
|
||||
|
||||
Convey("Updating existing state to pending with incorrect version since alert rule state update version is higher", func() {
|
||||
s := *query.Result
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.Id,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: 1000,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(cmd.ResultVersion, ShouldEqual, 1)
|
||||
})
|
||||
|
||||
Convey("different version and same alert state change version should return error", func() {
|
||||
s := *query.Result
|
||||
s.Version = 1000
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.Id,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||
err = sqlStore.GetOrCreateAlertNotificationState(context.Background(), query3)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, unknownVersion+1, query3.Result.Version)
|
||||
require.Equal(t, models.AlertNotificationStateCompleted, query3.Result.State)
|
||||
require.Equal(t, now.Unix(), query3.Result.UpdatedAt)
|
||||
})
|
||||
})
|
||||
|
||||
Reset(func() {
|
||||
timeNow = oldTimeNow
|
||||
t.Run("Update existing state to pending with incorrect version should return version mismatch error", func(t *testing.T) {
|
||||
s := *query.Result
|
||||
s.Version = 1000
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.NotifierId,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
require.Equal(t, models.ErrAlertNotificationStateVersionConflict, err)
|
||||
})
|
||||
|
||||
t.Run("Updating existing state to pending with incorrect version since alert rule state update version is higher", func(t *testing.T) {
|
||||
s := *query.Result
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.Id,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: 1000,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Equal(t, int64(1), cmd.ResultVersion)
|
||||
})
|
||||
|
||||
t.Run("different version and same alert state change version should return error", func(t *testing.T) {
|
||||
s := *query.Result
|
||||
s.Version = 1000
|
||||
cmd := models.SetAlertNotificationStateToPendingCommand{
|
||||
Id: s.Id,
|
||||
Version: s.Version,
|
||||
AlertRuleStateUpdatedVersion: s.AlertRuleStateUpdatedVersion,
|
||||
}
|
||||
err := sqlStore.SetAlertNotificationStateToPendingCommand(context.Background(), &cmd)
|
||||
require.Error(t, err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Alert notifications should be empty", func() {
|
||||
cmd := &models.GetAlertNotificationsQuery{
|
||||
OrgId: 2,
|
||||
Name: "email",
|
||||
}
|
||||
t.Run("Alert notifications should be empty", func(t *testing.T) {
|
||||
setup()
|
||||
cmd := &models.GetAlertNotificationsQuery{
|
||||
OrgId: 2,
|
||||
Name: "email",
|
||||
}
|
||||
|
||||
err := sqlStore.GetAlertNotifications(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(cmd.Result, ShouldBeNil)
|
||||
err := sqlStore.GetAlertNotifications(cmd)
|
||||
require.Nil(t, err)
|
||||
require.Nil(t, cmd.Result)
|
||||
})
|
||||
|
||||
t.Run("Cannot save alert notifier with send reminder = true", func(t *testing.T) {
|
||||
setup()
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops",
|
||||
Type: "email",
|
||||
OrgId: 1,
|
||||
SendReminder: true,
|
||||
Settings: simplejson.New(),
|
||||
}
|
||||
|
||||
t.Run("and missing frequency", func(t *testing.T) {
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
require.Equal(t, models.ErrNotificationFrequencyNotFound, err)
|
||||
})
|
||||
|
||||
Convey("Cannot save alert notifier with send reminder = true", func() {
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops",
|
||||
Type: "email",
|
||||
t.Run("invalid frequency", func(t *testing.T) {
|
||||
cmd.Frequency = "invalid duration"
|
||||
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
require.True(t, regexp.MustCompile(`^time: invalid duration "?invalid duration"?$`).MatchString(
|
||||
err.Error()))
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Cannot update alert notifier with send reminder = false", func(t *testing.T) {
|
||||
setup()
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops update",
|
||||
Type: "email",
|
||||
OrgId: 1,
|
||||
SendReminder: false,
|
||||
Settings: simplejson.New(),
|
||||
}
|
||||
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
updateCmd := &models.UpdateAlertNotificationCommand{
|
||||
Id: cmd.Result.Id,
|
||||
SendReminder: true,
|
||||
}
|
||||
|
||||
t.Run("and missing frequency", func(t *testing.T) {
|
||||
err := sqlStore.UpdateAlertNotification(updateCmd)
|
||||
require.Equal(t, models.ErrNotificationFrequencyNotFound, err)
|
||||
})
|
||||
|
||||
t.Run("invalid frequency", func(t *testing.T) {
|
||||
updateCmd.Frequency = "invalid duration"
|
||||
|
||||
err := sqlStore.UpdateAlertNotification(updateCmd)
|
||||
require.Error(t, err)
|
||||
require.True(t, regexp.MustCompile(`^time: invalid duration "?invalid duration"?$`).MatchString(
|
||||
err.Error()))
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Can save Alert Notification", func(t *testing.T) {
|
||||
setup()
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops",
|
||||
Type: "email",
|
||||
OrgId: 1,
|
||||
SendReminder: true,
|
||||
Frequency: "10s",
|
||||
Settings: simplejson.New(),
|
||||
}
|
||||
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
require.Nil(t, err)
|
||||
require.NotEqual(t, 0, cmd.Result.Id)
|
||||
require.NotEqual(t, 0, cmd.Result.OrgId)
|
||||
require.Equal(t, "email", cmd.Result.Type)
|
||||
require.Equal(t, 10*time.Second, cmd.Result.Frequency)
|
||||
require.False(t, cmd.Result.DisableResolveMessage)
|
||||
require.NotEmpty(t, cmd.Result.Uid)
|
||||
|
||||
t.Run("Cannot save Alert Notification with the same name", func(t *testing.T) {
|
||||
err = sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
require.Error(t, err)
|
||||
})
|
||||
t.Run("Cannot save Alert Notification with the same name and another uid", func(t *testing.T) {
|
||||
anotherUidCmd := &models.CreateAlertNotificationCommand{
|
||||
Name: cmd.Name,
|
||||
Type: cmd.Type,
|
||||
OrgId: 1,
|
||||
SendReminder: true,
|
||||
Settings: simplejson.New(),
|
||||
SendReminder: cmd.SendReminder,
|
||||
Frequency: cmd.Frequency,
|
||||
Settings: cmd.Settings,
|
||||
Uid: "notifier1",
|
||||
}
|
||||
|
||||
Convey("and missing frequency", func() {
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
|
||||
})
|
||||
|
||||
Convey("invalid frequency", func() {
|
||||
cmd.Frequency = "invalid duration"
|
||||
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
So(regexp.MustCompile(`^time: invalid duration "?invalid duration"?$`).MatchString(
|
||||
err.Error()), ShouldBeTrue)
|
||||
})
|
||||
err = sqlStore.CreateAlertNotificationCommand(anotherUidCmd)
|
||||
require.Error(t, err)
|
||||
})
|
||||
t.Run("Can save Alert Notification with another name and another uid", func(t *testing.T) {
|
||||
anotherUidCmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "another ops",
|
||||
Type: cmd.Type,
|
||||
OrgId: 1,
|
||||
SendReminder: cmd.SendReminder,
|
||||
Frequency: cmd.Frequency,
|
||||
Settings: cmd.Settings,
|
||||
Uid: "notifier2",
|
||||
}
|
||||
err = sqlStore.CreateAlertNotificationCommand(anotherUidCmd)
|
||||
require.Nil(t, err)
|
||||
})
|
||||
|
||||
Convey("Cannot update alert notifier with send reminder = false", func() {
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops update",
|
||||
Type: "email",
|
||||
OrgId: 1,
|
||||
t.Run("Can update alert notification", func(t *testing.T) {
|
||||
newCmd := &models.UpdateAlertNotificationCommand{
|
||||
Name: "NewName",
|
||||
Type: "webhook",
|
||||
OrgId: cmd.Result.OrgId,
|
||||
SendReminder: true,
|
||||
DisableResolveMessage: true,
|
||||
Frequency: "60s",
|
||||
Settings: simplejson.New(),
|
||||
Id: cmd.Result.Id,
|
||||
}
|
||||
err := sqlStore.UpdateAlertNotification(newCmd)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "NewName", newCmd.Result.Name)
|
||||
require.Equal(t, 60*time.Second, newCmd.Result.Frequency)
|
||||
require.True(t, newCmd.Result.DisableResolveMessage)
|
||||
})
|
||||
|
||||
t.Run("Can update alert notification to disable sending of reminders", func(t *testing.T) {
|
||||
newCmd := &models.UpdateAlertNotificationCommand{
|
||||
Name: "NewName",
|
||||
Type: "webhook",
|
||||
OrgId: cmd.Result.OrgId,
|
||||
SendReminder: false,
|
||||
Settings: simplejson.New(),
|
||||
}
|
||||
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
updateCmd := &models.UpdateAlertNotificationCommand{
|
||||
Id: cmd.Result.Id,
|
||||
SendReminder: true,
|
||||
}
|
||||
err := sqlStore.UpdateAlertNotification(newCmd)
|
||||
require.Nil(t, err)
|
||||
require.False(t, newCmd.Result.SendReminder)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Can search using an array of ids", func(t *testing.T) {
|
||||
setup()
|
||||
cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
|
||||
otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
|
||||
require.Nil(t, sqlStore.CreateAlertNotificationCommand(&cmd1))
|
||||
require.Nil(t, sqlStore.CreateAlertNotificationCommand(&cmd2))
|
||||
require.Nil(t, sqlStore.CreateAlertNotificationCommand(&cmd3))
|
||||
require.Nil(t, sqlStore.CreateAlertNotificationCommand(&cmd4))
|
||||
require.Nil(t, sqlStore.CreateAlertNotificationCommand(&otherOrg))
|
||||
|
||||
t.Run("search", func(t *testing.T) {
|
||||
query := &models.GetAlertNotificationsWithUidToSendQuery{
|
||||
Uids: []string{cmd1.Result.Uid, cmd2.Result.Uid, "112341231"},
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
Convey("and missing frequency", func() {
|
||||
err := sqlStore.UpdateAlertNotification(updateCmd)
|
||||
So(err, ShouldEqual, models.ErrNotificationFrequencyNotFound)
|
||||
})
|
||||
|
||||
Convey("invalid frequency", func() {
|
||||
updateCmd.Frequency = "invalid duration"
|
||||
|
||||
err := sqlStore.UpdateAlertNotification(updateCmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(regexp.MustCompile(`^time: invalid duration "?invalid duration"?$`).MatchString(
|
||||
err.Error()), ShouldBeTrue)
|
||||
})
|
||||
err := sqlStore.GetAlertNotificationsWithUidToSend(query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 3, len(query.Result))
|
||||
})
|
||||
|
||||
Convey("Can save Alert Notification", func() {
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops",
|
||||
Type: "email",
|
||||
OrgId: 1,
|
||||
SendReminder: true,
|
||||
Frequency: "10s",
|
||||
Settings: simplejson.New(),
|
||||
t.Run("all", func(t *testing.T) {
|
||||
query := &models.GetAllAlertNotificationsQuery{
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(cmd.Result.Id, ShouldNotEqual, 0)
|
||||
So(cmd.Result.OrgId, ShouldNotEqual, 0)
|
||||
So(cmd.Result.Type, ShouldEqual, "email")
|
||||
So(cmd.Result.Frequency, ShouldEqual, 10*time.Second)
|
||||
So(cmd.Result.DisableResolveMessage, ShouldBeFalse)
|
||||
So(cmd.Result.Uid, ShouldNotBeEmpty)
|
||||
|
||||
Convey("Cannot save Alert Notification with the same name", func() {
|
||||
err = sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Cannot save Alert Notification with the same name and another uid", func() {
|
||||
anotherUidCmd := &models.CreateAlertNotificationCommand{
|
||||
Name: cmd.Name,
|
||||
Type: cmd.Type,
|
||||
OrgId: 1,
|
||||
SendReminder: cmd.SendReminder,
|
||||
Frequency: cmd.Frequency,
|
||||
Settings: cmd.Settings,
|
||||
Uid: "notifier1",
|
||||
}
|
||||
err = sqlStore.CreateAlertNotificationCommand(anotherUidCmd)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Can save Alert Notification with another name and another uid", func() {
|
||||
anotherUidCmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "another ops",
|
||||
Type: cmd.Type,
|
||||
OrgId: 1,
|
||||
SendReminder: cmd.SendReminder,
|
||||
Frequency: cmd.Frequency,
|
||||
Settings: cmd.Settings,
|
||||
Uid: "notifier2",
|
||||
}
|
||||
err = sqlStore.CreateAlertNotificationCommand(anotherUidCmd)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Can update alert notification", func() {
|
||||
newCmd := &models.UpdateAlertNotificationCommand{
|
||||
Name: "NewName",
|
||||
Type: "webhook",
|
||||
OrgId: cmd.Result.OrgId,
|
||||
SendReminder: true,
|
||||
DisableResolveMessage: true,
|
||||
Frequency: "60s",
|
||||
Settings: simplejson.New(),
|
||||
Id: cmd.Result.Id,
|
||||
}
|
||||
err := sqlStore.UpdateAlertNotification(newCmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(newCmd.Result.Name, ShouldEqual, "NewName")
|
||||
So(newCmd.Result.Frequency, ShouldEqual, 60*time.Second)
|
||||
So(newCmd.Result.DisableResolveMessage, ShouldBeTrue)
|
||||
})
|
||||
|
||||
Convey("Can update alert notification to disable sending of reminders", func() {
|
||||
newCmd := &models.UpdateAlertNotificationCommand{
|
||||
Name: "NewName",
|
||||
Type: "webhook",
|
||||
OrgId: cmd.Result.OrgId,
|
||||
SendReminder: false,
|
||||
Settings: simplejson.New(),
|
||||
Id: cmd.Result.Id,
|
||||
}
|
||||
err := sqlStore.UpdateAlertNotification(newCmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(newCmd.Result.SendReminder, ShouldBeFalse)
|
||||
})
|
||||
err := sqlStore.GetAllAlertNotifications(query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 4, len(query.Result))
|
||||
require.Equal(t, cmd4.Name, query.Result[0].Name)
|
||||
require.Equal(t, cmd1.Name, query.Result[1].Name)
|
||||
require.Equal(t, cmd3.Name, query.Result[2].Name)
|
||||
require.Equal(t, cmd2.Name, query.Result[3].Name)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can search using an array of ids", func() {
|
||||
cmd1 := models.CreateAlertNotificationCommand{Name: "nagios", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
cmd2 := models.CreateAlertNotificationCommand{Name: "slack", Type: "webhook", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
cmd3 := models.CreateAlertNotificationCommand{Name: "ops2", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
cmd4 := models.CreateAlertNotificationCommand{IsDefault: true, Name: "default", Type: "email", OrgId: 1, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
t.Run("Notification Uid by Id Caching", func(t *testing.T) {
|
||||
setup()
|
||||
ss := InitTestDB(t)
|
||||
|
||||
otherOrg := models.CreateAlertNotificationCommand{Name: "default", Type: "email", OrgId: 2, SendReminder: true, Frequency: "10s", Settings: simplejson.New()}
|
||||
notification := &models.CreateAlertNotificationCommand{Uid: "aNotificationUid", OrgId: 1, Name: "aNotificationUid"}
|
||||
err := sqlStore.CreateAlertNotificationCommand(notification)
|
||||
require.Nil(t, err)
|
||||
|
||||
So(sqlStore.CreateAlertNotificationCommand(&cmd1), ShouldBeNil)
|
||||
So(sqlStore.CreateAlertNotificationCommand(&cmd2), ShouldBeNil)
|
||||
So(sqlStore.CreateAlertNotificationCommand(&cmd3), ShouldBeNil)
|
||||
So(sqlStore.CreateAlertNotificationCommand(&cmd4), ShouldBeNil)
|
||||
So(sqlStore.CreateAlertNotificationCommand(&otherOrg), ShouldBeNil)
|
||||
byUidQuery := &models.GetAlertNotificationsWithUidQuery{
|
||||
Uid: notification.Uid,
|
||||
OrgId: notification.OrgId,
|
||||
}
|
||||
|
||||
Convey("search", func() {
|
||||
query := &models.GetAlertNotificationsWithUidToSendQuery{
|
||||
Uids: []string{cmd1.Result.Uid, cmd2.Result.Uid, "112341231"},
|
||||
OrgId: 1,
|
||||
}
|
||||
notificationByUidErr := sqlStore.GetAlertNotificationsWithUid(byUidQuery)
|
||||
require.Nil(t, notificationByUidErr)
|
||||
|
||||
err := sqlStore.GetAlertNotificationsWithUidToSend(query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 3)
|
||||
})
|
||||
|
||||
Convey("all", func() {
|
||||
query := &models.GetAllAlertNotificationsQuery{
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err := sqlStore.GetAllAlertNotifications(query)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(query.Result), ShouldEqual, 4)
|
||||
So(query.Result[0].Name, ShouldEqual, cmd4.Name)
|
||||
So(query.Result[1].Name, ShouldEqual, cmd1.Name)
|
||||
So(query.Result[2].Name, ShouldEqual, cmd3.Name)
|
||||
So(query.Result[3].Name, ShouldEqual, cmd2.Name)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Notification Uid by Id Caching", func() {
|
||||
ss := InitTestDB(t)
|
||||
|
||||
notification := &models.CreateAlertNotificationCommand{Uid: "aNotificationUid", OrgId: 1, Name: "aNotificationUid"}
|
||||
err := sqlStore.CreateAlertNotificationCommand(notification)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
byUidQuery := &models.GetAlertNotificationsWithUidQuery{
|
||||
Uid: notification.Uid,
|
||||
OrgId: notification.OrgId,
|
||||
t.Run("Can cache notification Uid", func(t *testing.T) {
|
||||
byIdQuery := &models.GetAlertNotificationUidQuery{
|
||||
Id: byUidQuery.Result.Id,
|
||||
OrgId: byUidQuery.Result.OrgId,
|
||||
}
|
||||
|
||||
notificationByUidErr := sqlStore.GetAlertNotificationsWithUid(byUidQuery)
|
||||
So(notificationByUidErr, ShouldBeNil)
|
||||
cacheKey := newAlertNotificationUidCacheKey(byIdQuery.OrgId, byIdQuery.Id)
|
||||
|
||||
Convey("Can cache notification Uid", func() {
|
||||
byIdQuery := &models.GetAlertNotificationUidQuery{
|
||||
Id: byUidQuery.Result.Id,
|
||||
OrgId: byUidQuery.Result.OrgId,
|
||||
}
|
||||
resultBeforeCaching, foundBeforeCaching := ss.CacheService.Get(cacheKey)
|
||||
require.False(t, foundBeforeCaching)
|
||||
require.Nil(t, resultBeforeCaching)
|
||||
|
||||
cacheKey := newAlertNotificationUidCacheKey(byIdQuery.OrgId, byIdQuery.Id)
|
||||
notificationByIdErr := ss.GetAlertNotificationUidWithId(byIdQuery)
|
||||
require.Nil(t, notificationByIdErr)
|
||||
|
||||
resultBeforeCaching, foundBeforeCaching := ss.CacheService.Get(cacheKey)
|
||||
So(foundBeforeCaching, ShouldBeFalse)
|
||||
So(resultBeforeCaching, ShouldBeNil)
|
||||
|
||||
notificationByIdErr := ss.GetAlertNotificationUidWithId(byIdQuery)
|
||||
So(notificationByIdErr, ShouldBeNil)
|
||||
|
||||
resultAfterCaching, foundAfterCaching := ss.CacheService.Get(cacheKey)
|
||||
So(foundAfterCaching, ShouldBeTrue)
|
||||
So(resultAfterCaching, ShouldEqual, notification.Uid)
|
||||
})
|
||||
|
||||
Convey("Retrieves from cache when exists", func() {
|
||||
query := &models.GetAlertNotificationUidQuery{
|
||||
Id: 999,
|
||||
OrgId: 100,
|
||||
}
|
||||
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
|
||||
ss.CacheService.Set(cacheKey, "a-cached-uid", -1)
|
||||
|
||||
err := ss.GetAlertNotificationUidWithId(query)
|
||||
So(err, ShouldBeNil)
|
||||
So(query.Result, ShouldEqual, "a-cached-uid")
|
||||
})
|
||||
|
||||
Convey("Returns an error without populating cache when the notification doesn't exist in the database", func() {
|
||||
query := &models.GetAlertNotificationUidQuery{
|
||||
Id: -1,
|
||||
OrgId: 100,
|
||||
}
|
||||
|
||||
err := ss.GetAlertNotificationUidWithId(query)
|
||||
So(query.Result, ShouldEqual, "")
|
||||
So(err, ShouldNotBeNil)
|
||||
So(errors.Is(err, models.ErrAlertNotificationFailedTranslateUniqueID), ShouldBeTrue)
|
||||
|
||||
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
|
||||
result, found := ss.CacheService.Get(cacheKey)
|
||||
So(found, ShouldBeFalse)
|
||||
So(result, ShouldBeNil)
|
||||
})
|
||||
resultAfterCaching, foundAfterCaching := ss.CacheService.Get(cacheKey)
|
||||
require.True(t, foundAfterCaching)
|
||||
require.Equal(t, notification.Uid, resultAfterCaching)
|
||||
})
|
||||
|
||||
Convey("Cannot update non-existing Alert Notification", func() {
|
||||
updateCmd := &models.UpdateAlertNotificationCommand{
|
||||
t.Run("Retrieves from cache when exists", func(t *testing.T) {
|
||||
query := &models.GetAlertNotificationUidQuery{
|
||||
Id: 999,
|
||||
OrgId: 100,
|
||||
}
|
||||
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
|
||||
ss.CacheService.Set(cacheKey, "a-cached-uid", -1)
|
||||
|
||||
err := ss.GetAlertNotificationUidWithId(query)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, "a-cached-uid", query.Result)
|
||||
})
|
||||
|
||||
t.Run("Returns an error without populating cache when the notification doesn't exist in the database", func(t *testing.T) {
|
||||
query := &models.GetAlertNotificationUidQuery{
|
||||
Id: -1,
|
||||
OrgId: 100,
|
||||
}
|
||||
|
||||
err := ss.GetAlertNotificationUidWithId(query)
|
||||
require.Equal(t, "", query.Result)
|
||||
require.Error(t, err)
|
||||
require.True(t, errors.Is(err, models.ErrAlertNotificationFailedTranslateUniqueID))
|
||||
|
||||
cacheKey := newAlertNotificationUidCacheKey(query.OrgId, query.Id)
|
||||
result, found := ss.CacheService.Get(cacheKey)
|
||||
require.False(t, found)
|
||||
require.Nil(t, result)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Cannot update non-existing Alert Notification", func(t *testing.T) {
|
||||
setup()
|
||||
updateCmd := &models.UpdateAlertNotificationCommand{
|
||||
Name: "NewName",
|
||||
Type: "webhook",
|
||||
OrgId: 1,
|
||||
SendReminder: true,
|
||||
DisableResolveMessage: true,
|
||||
Frequency: "60s",
|
||||
Settings: simplejson.New(),
|
||||
Id: 1,
|
||||
}
|
||||
err := sqlStore.UpdateAlertNotification(updateCmd)
|
||||
require.Equal(t, models.ErrAlertNotificationNotFound, err)
|
||||
|
||||
t.Run("using UID", func(t *testing.T) {
|
||||
updateWithUidCmd := &models.UpdateAlertNotificationWithUidCommand{
|
||||
Name: "NewName",
|
||||
Type: "webhook",
|
||||
OrgId: 1,
|
||||
@@ -414,78 +437,65 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
||||
DisableResolveMessage: true,
|
||||
Frequency: "60s",
|
||||
Settings: simplejson.New(),
|
||||
Id: 1,
|
||||
Uid: "uid",
|
||||
NewUid: "newUid",
|
||||
}
|
||||
err := sqlStore.UpdateAlertNotification(updateCmd)
|
||||
So(err, ShouldEqual, models.ErrAlertNotificationNotFound)
|
||||
|
||||
Convey("using UID", func() {
|
||||
updateWithUidCmd := &models.UpdateAlertNotificationWithUidCommand{
|
||||
Name: "NewName",
|
||||
Type: "webhook",
|
||||
OrgId: 1,
|
||||
SendReminder: true,
|
||||
DisableResolveMessage: true,
|
||||
Frequency: "60s",
|
||||
Settings: simplejson.New(),
|
||||
Uid: "uid",
|
||||
NewUid: "newUid",
|
||||
}
|
||||
err := sqlStore.UpdateAlertNotificationWithUid(updateWithUidCmd)
|
||||
So(err, ShouldEqual, models.ErrAlertNotificationNotFound)
|
||||
})
|
||||
err := sqlStore.UpdateAlertNotificationWithUid(updateWithUidCmd)
|
||||
require.Equal(t, models.ErrAlertNotificationNotFound, err)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Can delete Alert Notification", func() {
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops update",
|
||||
Type: "email",
|
||||
OrgId: 1,
|
||||
SendReminder: false,
|
||||
Settings: simplejson.New(),
|
||||
}
|
||||
t.Run("Can delete Alert Notification", func(t *testing.T) {
|
||||
setup()
|
||||
cmd := &models.CreateAlertNotificationCommand{
|
||||
Name: "ops update",
|
||||
Type: "email",
|
||||
OrgId: 1,
|
||||
SendReminder: false,
|
||||
Settings: simplejson.New(),
|
||||
}
|
||||
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
deleteCmd := &models.DeleteAlertNotificationCommand{
|
||||
Id: cmd.Result.Id,
|
||||
OrgId: 1,
|
||||
}
|
||||
err = sqlStore.DeleteAlertNotification(deleteCmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
t.Run("using UID", func(t *testing.T) {
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
require.Nil(t, err)
|
||||
|
||||
deleteCmd := &models.DeleteAlertNotificationCommand{
|
||||
Id: cmd.Result.Id,
|
||||
deleteWithUidCmd := &models.DeleteAlertNotificationWithUidCommand{
|
||||
Uid: cmd.Result.Uid,
|
||||
OrgId: 1,
|
||||
}
|
||||
err = sqlStore.DeleteAlertNotification(deleteCmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("using UID", func() {
|
||||
err := sqlStore.CreateAlertNotificationCommand(cmd)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
deleteWithUidCmd := &models.DeleteAlertNotificationWithUidCommand{
|
||||
Uid: cmd.Result.Uid,
|
||||
OrgId: 1,
|
||||
}
|
||||
|
||||
err = sqlStore.DeleteAlertNotificationWithUid(deleteWithUidCmd)
|
||||
So(err, ShouldBeNil)
|
||||
So(deleteWithUidCmd.DeletedAlertNotificationId, ShouldEqual, cmd.Result.Id)
|
||||
})
|
||||
err = sqlStore.DeleteAlertNotificationWithUid(deleteWithUidCmd)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, cmd.Result.Id, deleteWithUidCmd.DeletedAlertNotificationId)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Cannot delete non-existing Alert Notification", func() {
|
||||
deleteCmd := &models.DeleteAlertNotificationCommand{
|
||||
Id: 1,
|
||||
t.Run("Cannot delete non-existing Alert Notification", func(t *testing.T) {
|
||||
setup()
|
||||
deleteCmd := &models.DeleteAlertNotificationCommand{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
}
|
||||
err := sqlStore.DeleteAlertNotification(deleteCmd)
|
||||
require.Equal(t, models.ErrAlertNotificationNotFound, err)
|
||||
|
||||
t.Run("using UID", func(t *testing.T) {
|
||||
deleteWithUidCmd := &models.DeleteAlertNotificationWithUidCommand{
|
||||
Uid: "uid",
|
||||
OrgId: 1,
|
||||
}
|
||||
err := sqlStore.DeleteAlertNotification(deleteCmd)
|
||||
So(err, ShouldEqual, models.ErrAlertNotificationNotFound)
|
||||
|
||||
Convey("using UID", func() {
|
||||
deleteWithUidCmd := &models.DeleteAlertNotificationWithUidCommand{
|
||||
Uid: "uid",
|
||||
OrgId: 1,
|
||||
}
|
||||
err = sqlStore.DeleteAlertNotificationWithUid(deleteWithUidCmd)
|
||||
So(err, ShouldEqual, models.ErrAlertNotificationNotFound)
|
||||
})
|
||||
err = sqlStore.DeleteAlertNotificationWithUid(deleteWithUidCmd)
|
||||
require.Equal(t, models.ErrAlertNotificationNotFound, err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user