@ -183,8 +183,8 @@ func TestAlertRuleCRUD(t *testing.T) {
Annotations : map [ string ] string { "annotation1" : "val1" } ,
} ,
GrafanaManagedAlert : & apimodels . PostableGrafanaRule {
Title : "AlwaysFiring" ,
Condition : "A" ,
Title : "AlwaysFiring" ,
Data : [ ] ngmodels . AlertQuery { } ,
} ,
} ,
expectedResponse : ` { "error":"invalid alert rule: no queries or expressions are found", "message":"failed to update rule group"} ` ,
@ -310,6 +310,66 @@ func TestAlertRuleCRUD(t *testing.T) {
} ,
expectedResponse : ` { "error":"invalid alert rule: interval (1s) should be divided exactly by scheduler interval: 10s", "message":"failed to update rule group"} ` ,
} ,
{
desc : "alert rule with unknown datasource" ,
rulegroup : "arulegroup" ,
rule : apimodels . PostableExtendedRuleNode {
ApiRuleNode : & apimodels . ApiRuleNode {
For : interval ,
Labels : map [ string ] string { "label1" : "val1" } ,
Annotations : map [ string ] string { "annotation1" : "val1" } ,
} ,
GrafanaManagedAlert : & apimodels . PostableGrafanaRule {
Title : "AlwaysFiring" ,
Condition : "A" ,
Data : [ ] ngmodels . AlertQuery {
{
RefID : "A" ,
RelativeTimeRange : ngmodels . RelativeTimeRange {
From : ngmodels . Duration ( time . Duration ( 5 ) * time . Hour ) ,
To : ngmodels . Duration ( time . Duration ( 3 ) * time . Hour ) ,
} ,
DatasourceUID : "unknown" ,
Model : json . RawMessage ( ` {
"type" : "math" ,
"expression" : "2 + 3 > 1"
} ` ) ,
} ,
} ,
} ,
} ,
expectedResponse : ` { "error":"invalid query A: data source not found: unknown", "message":"failed to validate alert rule AlwaysFiring"} ` ,
} ,
{
desc : "alert rule with invalid condition" ,
rulegroup : "arulegroup" ,
rule : apimodels . PostableExtendedRuleNode {
ApiRuleNode : & apimodels . ApiRuleNode {
For : interval ,
Labels : map [ string ] string { "label1" : "val1" } ,
Annotations : map [ string ] string { "annotation1" : "val1" } ,
} ,
GrafanaManagedAlert : & apimodels . PostableGrafanaRule {
Title : "AlwaysFiring" ,
Condition : "B" ,
Data : [ ] ngmodels . AlertQuery {
{
RefID : "A" ,
RelativeTimeRange : ngmodels . RelativeTimeRange {
From : ngmodels . Duration ( time . Duration ( 5 ) * time . Hour ) ,
To : ngmodels . Duration ( time . Duration ( 3 ) * time . Hour ) ,
} ,
DatasourceUID : "-100" ,
Model : json . RawMessage ( ` {
"type" : "math" ,
"expression" : "2 + 3 > 1"
} ` ) ,
} ,
} ,
} ,
} ,
expectedResponse : ` { "error":"condition B not found in any query or expression: it should be one of: [A]", "message":"failed to validate alert rule AlwaysFiring"} ` ,
} ,
}
for _ , tc := range testCases {
@ -931,7 +991,7 @@ func TestAlertRuleCRUD(t *testing.T) {
}
` ,
expectedStatusCode : http . StatusBadRequest ,
expectedResponse : ` { "error":"condition B not found in any query or expression","message":"invalid condition"} ` ,
expectedResponse : ` { "error":"condition B not found in any query or expression: it should be one of: [A] ","message":"invalid condition"} ` ,
} ,
{
desc : "unknown query datasource" ,
@ -956,7 +1016,7 @@ func TestAlertRuleCRUD(t *testing.T) {
}
` ,
expectedStatusCode : http . StatusBadRequest ,
expectedResponse : ` { "error":"failed to get datasource: unknown: data source not found ","message":"invalid condition"} ` ,
expectedResponse : ` { "error":"invalid query A: data source not found: unknown ","message":"invalid condition"} ` ,
} ,
}
@ -1112,7 +1172,7 @@ func TestAlertRuleCRUD(t *testing.T) {
}
` ,
expectedStatusCode : http . StatusBadRequest ,
expectedResponse : ` { "error":"failed to get datasource: unknown: data source not found ","message":"invalid queries or expressions"} ` ,
expectedResponse : ` { "error":"invalid query A: data source not found: unknown ","message":"invalid queries or expressions"} ` ,
} ,
}