2016-06-13 09:39:00 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-06-13 09:39:00 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertNotificationSQLAccess(t *testing.T) {
|
|
|
|
Convey("Testing Alert notification sql access", t, func() {
|
|
|
|
InitTestDB(t)
|
2016-06-14 01:33:50 -05:00
|
|
|
var err error
|
2016-06-13 09:39:00 -05:00
|
|
|
|
|
|
|
Convey("Alert notifications should be empty", func() {
|
|
|
|
cmd := &m.GetAlertNotificationQuery{
|
|
|
|
OrgID: FakeOrgId,
|
|
|
|
Name: "email",
|
|
|
|
}
|
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
err := AlertNotificationQuery(cmd)
|
2016-06-13 09:39:00 -05:00
|
|
|
fmt.Printf("errror %v", err)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(cmd.Result), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
Convey("Can save Alert Notification", func() {
|
|
|
|
cmd := &m.CreateAlertNotificationCommand{
|
2016-06-20 08:24:48 -05:00
|
|
|
Name: "ops",
|
|
|
|
Type: "email",
|
|
|
|
OrgID: 1,
|
|
|
|
Settings: simplejson.New(),
|
|
|
|
AlwaysExecute: true,
|
2016-06-14 01:33:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = CreateAlertNotificationCommand(cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(cmd.Result.Id, ShouldNotEqual, 0)
|
2016-06-14 09:56:14 -05:00
|
|
|
So(cmd.Result.OrgId, ShouldNotEqual, 0)
|
|
|
|
So(cmd.Result.Type, ShouldEqual, "email")
|
2016-06-20 08:24:48 -05:00
|
|
|
So(cmd.Result.AlwaysExecute, ShouldEqual, true)
|
2016-06-13 09:39:00 -05:00
|
|
|
|
2016-06-14 01:33:50 -05:00
|
|
|
Convey("Cannot save Alert Notification with the same name", func() {
|
|
|
|
err = CreateAlertNotificationCommand(cmd)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
Convey("Can update alert notification", func() {
|
2016-06-14 01:33:50 -05:00
|
|
|
newCmd := &m.UpdateAlertNotificationCommand{
|
2016-06-20 08:24:48 -05:00
|
|
|
Name: "NewName",
|
|
|
|
Type: "webhook",
|
|
|
|
OrgID: cmd.Result.OrgId,
|
|
|
|
Settings: simplejson.New(),
|
|
|
|
Id: cmd.Result.Id,
|
|
|
|
AlwaysExecute: true,
|
2016-06-14 01:33:50 -05:00
|
|
|
}
|
2016-06-14 09:56:14 -05:00
|
|
|
err := UpdateAlertNotification(newCmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(newCmd.Result.Name, ShouldEqual, "NewName")
|
2016-06-14 01:33:50 -05:00
|
|
|
})
|
2016-06-14 09:56:14 -05:00
|
|
|
})
|
2016-06-14 01:33:50 -05:00
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
Convey("Can search using an array of ids", func() {
|
2016-06-20 08:24:48 -05:00
|
|
|
So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
|
|
|
|
Name: "nagios",
|
|
|
|
Type: "webhook",
|
|
|
|
OrgID: 1,
|
|
|
|
Settings: simplejson.New(),
|
|
|
|
AlwaysExecute: true,
|
|
|
|
}), ShouldBeNil)
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
|
|
|
|
Name: "ops2",
|
|
|
|
Type: "email",
|
|
|
|
OrgID: 1,
|
|
|
|
Settings: simplejson.New(),
|
|
|
|
}), ShouldBeNil)
|
|
|
|
|
|
|
|
So(CreateAlertNotificationCommand(&m.CreateAlertNotificationCommand{
|
|
|
|
Name: "slack",
|
|
|
|
Type: "webhook",
|
|
|
|
OrgID: 1,
|
|
|
|
Settings: simplejson.New(),
|
|
|
|
}), ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("search", func() {
|
2016-06-20 08:24:48 -05:00
|
|
|
existingNotification := int64(2)
|
|
|
|
missingThatSholdNotCauseerrors := int64(99)
|
|
|
|
|
2016-06-14 09:56:14 -05:00
|
|
|
query := &m.GetAlertNotificationQuery{
|
2016-06-20 08:24:48 -05:00
|
|
|
Ids: []int64{existingNotification, missingThatSholdNotCauseerrors},
|
|
|
|
OrgID: 1,
|
|
|
|
IncludeAlwaysExecute: true,
|
2016-06-14 01:33:50 -05:00
|
|
|
}
|
2016-06-14 09:56:14 -05:00
|
|
|
|
|
|
|
err := AlertNotificationQuery(query)
|
2016-06-13 09:39:00 -05:00
|
|
|
So(err, ShouldBeNil)
|
2016-06-14 09:56:14 -05:00
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
2016-06-20 08:24:48 -05:00
|
|
|
defaultNotifications := 0
|
|
|
|
for _, not := range query.Result {
|
|
|
|
if not.AlwaysExecute {
|
|
|
|
defaultNotifications++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
So(defaultNotifications, ShouldEqual, 1)
|
2016-06-14 01:33:50 -05:00
|
|
|
})
|
|
|
|
})
|
2016-06-13 09:39:00 -05:00
|
|
|
})
|
|
|
|
}
|