2016-06-13 03:40:46 -05:00
package alerting
import (
"testing"
2016-06-15 04:39:25 -05:00
"github.com/grafana/grafana/pkg/components/simplejson"
2016-09-06 13:40:12 -05:00
m "github.com/grafana/grafana/pkg/models"
2018-12-20 09:12:47 -06:00
"github.com/grafana/grafana/pkg/services/sqlstore"
2016-06-13 03:40:46 -05:00
. "github.com/smartystreets/goconvey/convey"
)
2016-07-27 09:18:10 -05:00
type FakeCondition struct { }
2016-11-03 09:26:17 -05:00
func ( f * FakeCondition ) Eval ( context * EvalContext ) ( * ConditionResult , error ) {
return & ConditionResult { } , nil
}
2016-07-27 09:18:10 -05:00
2019-01-10 02:35:48 -06:00
func TestAlertRuleFrequencyParsing ( t * testing . T ) {
tcs := [ ] struct {
input string
err error
result int64
} {
{ input : "10s" , result : 10 } ,
{ input : "10m" , result : 600 } ,
{ input : "1h" , result : 3600 } ,
{ input : "1o" , result : 1 } ,
{ input : "0s" , err : ErrFrequencyCannotBeZeroOrLess } ,
{ input : "0m" , err : ErrFrequencyCannotBeZeroOrLess } ,
{ input : "0h" , err : ErrFrequencyCannotBeZeroOrLess } ,
{ input : "0" , err : ErrFrequencyCannotBeZeroOrLess } ,
{ input : "-1s" , err : ErrFrequencyCouldNotBeParsed } ,
}
for _ , tc := range tcs {
r , err := getTimeDurationStringToSeconds ( tc . input )
if err != tc . err {
t . Errorf ( "expected error: '%v' got: '%v'" , tc . err , err )
return
}
if r != tc . result {
t . Errorf ( "expected result: %d got %d" , tc . result , r )
}
}
}
2016-06-13 03:40:46 -05:00
func TestAlertRuleModel ( t * testing . T ) {
2018-12-20 09:12:47 -06:00
sqlstore . InitTestDB ( t )
2016-06-13 03:40:46 -05:00
Convey ( "Testing alert rule" , t , func ( ) {
2016-07-27 09:29:28 -05:00
RegisterCondition ( "test" , func ( model * simplejson . Json , index int ) ( Condition , error ) {
2016-07-27 09:18:10 -05:00
return & FakeCondition { } , nil
} )
2016-10-11 10:36:30 -05:00
Convey ( "should return err for empty string" , func ( ) {
_ , err := getTimeDurationStringToSeconds ( "" )
So ( err , ShouldNotBeNil )
} )
2016-07-19 10:45:37 -05:00
Convey ( "can construct alert rule model" , func ( ) {
2019-01-02 02:34:07 -06:00
firstNotification := m . CreateAlertNotificationCommand { OrgId : 1 , Name : "1" }
2019-01-28 08:37:52 -06:00
err := sqlstore . CreateAlertNotificationCommand ( & firstNotification )
2018-12-20 09:12:47 -06:00
So ( err , ShouldBeNil )
secondNotification := m . CreateAlertNotificationCommand { Uid : "notifier2" , OrgId : 1 , Name : "2" }
err = sqlstore . CreateAlertNotificationCommand ( & secondNotification )
So ( err , ShouldBeNil )
Convey ( "with notification id and uid" , func ( ) {
json := `
{
"name" : "name2" ,
"description" : "desc2" ,
"handler" : 0 ,
"noDataMode" : "critical" ,
"enabled" : true ,
"frequency" : "60s" ,
2019-01-28 08:27:02 -06:00
"conditions" : [
{
"type" : "test" ,
"prop" : 123
2018-12-20 09:12:47 -06:00
}
2019-01-28 08:27:02 -06:00
] ,
"notifications" : [
2018-12-20 09:12:47 -06:00
{ "id" : 1 } ,
{ "uid" : "notifier2" }
]
}
`
alertJSON , jsonErr := simplejson . NewJson ( [ ] byte ( json ) )
So ( jsonErr , ShouldBeNil )
alert := & m . Alert {
Id : 1 ,
OrgId : 1 ,
DashboardId : 1 ,
PanelId : 1 ,
Settings : alertJSON ,
}
alertRule , err := NewRuleFromDBAlert ( alert )
So ( err , ShouldBeNil )
So ( len ( alertRule . Conditions ) , ShouldEqual , 1 )
Convey ( "Can read notifications" , func ( ) {
So ( len ( alertRule . Notifications ) , ShouldEqual , 2 )
2019-01-02 02:34:07 -06:00
So ( alertRule . Notifications , ShouldContain , "000000001" )
2018-12-20 09:12:47 -06:00
So ( alertRule . Notifications , ShouldContain , "notifier2" )
} )
} )
} )
Convey ( "can construct alert rule model with invalid frequency" , func ( ) {
2016-06-15 04:39:25 -05:00
json := `
{
"name" : "name2" ,
"description" : "desc2" ,
2016-09-06 13:40:12 -05:00
"noDataMode" : "critical" ,
2016-06-15 04:39:25 -05:00
"enabled" : true ,
2018-12-20 09:12:47 -06:00
"frequency" : "0s" ,
2019-01-28 08:27:02 -06:00
"conditions" : [ { "type" : "test" , "prop" : 123 } ] ,
"notifications" : [ ]
2018-12-20 09:12:47 -06:00
} `
2016-06-15 04:39:25 -05:00
alertJSON , jsonErr := simplejson . NewJson ( [ ] byte ( json ) )
So ( jsonErr , ShouldBeNil )
2016-09-06 13:40:12 -05:00
alert := & m . Alert {
2016-06-15 04:39:25 -05:00
Id : 1 ,
OrgId : 1 ,
DashboardId : 1 ,
PanelId : 1 ,
2018-12-20 09:12:47 -06:00
Frequency : 0 ,
2016-06-15 04:39:25 -05:00
Settings : alertJSON ,
}
2016-07-27 09:29:28 -05:00
alertRule , err := NewRuleFromDBAlert ( alert )
2016-06-15 04:39:25 -05:00
So ( err , ShouldBeNil )
2018-12-20 09:12:47 -06:00
So ( alertRule . Frequency , ShouldEqual , 60 )
2016-06-15 04:39:25 -05:00
} )
2019-01-10 02:35:48 -06:00
2018-12-20 09:12:47 -06:00
Convey ( "raise error in case of missing notification id and uid" , func ( ) {
2019-01-10 02:35:48 -06:00
json := `
{
"name" : "name2" ,
"description" : "desc2" ,
"noDataMode" : "critical" ,
"enabled" : true ,
2018-12-20 09:12:47 -06:00
"frequency" : "60s" ,
2019-01-28 08:27:02 -06:00
"conditions" : [
{
"type" : "test" ,
"prop" : 123
2018-12-20 09:12:47 -06:00
}
2019-01-28 08:27:02 -06:00
] ,
"notifications" : [
2018-12-20 09:12:47 -06:00
{ "not_id_uid" : "1134" }
]
}
`
2019-01-10 02:35:48 -06:00
alertJSON , jsonErr := simplejson . NewJson ( [ ] byte ( json ) )
So ( jsonErr , ShouldBeNil )
alert := & m . Alert {
Id : 1 ,
OrgId : 1 ,
DashboardId : 1 ,
PanelId : 1 ,
Frequency : 0 ,
Settings : alertJSON ,
}
2018-12-20 09:12:47 -06:00
_ , err := NewRuleFromDBAlert ( alert )
So ( err , ShouldNotBeNil )
2019-01-02 02:34:07 -06:00
So ( err . Error ( ) , ShouldEqual , "Alert validation error: Neither id nor uid is specified, type assertion to string failed AlertId: 1 PanelId: 1 DashboardId: 1" )
2019-01-10 02:35:48 -06:00
} )
2016-06-13 03:40:46 -05:00
} )
}