@ -3,6 +3,7 @@ package state_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
@ -1254,6 +1255,187 @@ func TestProcessEvalResults(t *testing.T) {
} ,
} ,
} ,
{
desc : "normal -> alerting -> error -> alerting - it should clear the error" ,
alertRule : & models . AlertRule {
OrgID : 1 ,
Title : "test_title" ,
UID : "test_alert_rule_uid_2" ,
NamespaceUID : "test_namespace_uid" ,
Annotations : map [ string ] string { "annotation" : "test" } ,
Labels : map [ string ] string { "label" : "test" } ,
IntervalSeconds : 10 ,
For : 30 * time . Second ,
} ,
evalResults : [ ] eval . Results {
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . Normal ,
EvaluatedAt : evaluationTime ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . Alerting ,
EvaluatedAt : evaluationTime . Add ( 30 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . Error ,
Error : fmt . Errorf ( "Failed to query data" ) ,
EvaluatedAt : evaluationTime . Add ( 40 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . Alerting ,
EvaluatedAt : evaluationTime . Add ( 70 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
} ,
expectedStates : map [ string ] * state . State {
` [["__alert_rule_namespace_uid__","test_namespace_uid"],["__alert_rule_uid__","test_alert_rule_uid_2"],["alertname","test_title"],["instance_label","test"],["label","test"]] ` : {
AlertRuleUID : "test_alert_rule_uid_2" ,
OrgID : 1 ,
CacheId : ` [["__alert_rule_namespace_uid__","test_namespace_uid"],["__alert_rule_uid__","test_alert_rule_uid_2"],["alertname","test_title"],["instance_label","test"],["label","test"]] ` ,
Labels : data . Labels {
"__alert_rule_namespace_uid__" : "test_namespace_uid" ,
"__alert_rule_uid__" : "test_alert_rule_uid_2" ,
"alertname" : "test_title" ,
"label" : "test" ,
"instance_label" : "test" ,
} ,
State : eval . Alerting ,
Results : [ ] state . Evaluation {
{
EvaluationTime : evaluationTime ,
EvaluationState : eval . Normal ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
{
EvaluationTime : evaluationTime . Add ( 30 * time . Second ) ,
EvaluationState : eval . Alerting ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
{
EvaluationTime : evaluationTime . Add ( 40 * time . Second ) ,
EvaluationState : eval . Error ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
{
EvaluationTime : evaluationTime . Add ( 70 * time . Second ) ,
EvaluationState : eval . Alerting ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
} ,
StartsAt : evaluationTime . Add ( 70 * time . Second ) ,
EndsAt : evaluationTime . Add ( 70 * time . Second ) . Add ( state . ResendDelay * 3 ) ,
LastEvaluationTime : evaluationTime . Add ( 70 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
Annotations : map [ string ] string { "annotation" : "test" } ,
} ,
} ,
} ,
{
desc : "normal -> alerting -> error -> no data - it should clear the error" ,
alertRule : & models . AlertRule {
OrgID : 1 ,
Title : "test_title" ,
UID : "test_alert_rule_uid_2" ,
NamespaceUID : "test_namespace_uid" ,
Annotations : map [ string ] string { "annotation" : "test" } ,
Labels : map [ string ] string { "label" : "test" } ,
IntervalSeconds : 10 ,
For : 30 * time . Second ,
NoDataState : models . NoData ,
} ,
evalResults : [ ] eval . Results {
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . Normal ,
EvaluatedAt : evaluationTime ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . Alerting ,
EvaluatedAt : evaluationTime . Add ( 30 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . Error ,
Error : fmt . Errorf ( "Failed to query data" ) ,
EvaluatedAt : evaluationTime . Add ( 40 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
{
eval . Result {
Instance : data . Labels { "instance_label" : "test" } ,
State : eval . NoData ,
EvaluatedAt : evaluationTime . Add ( 50 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
} ,
} ,
} ,
expectedStates : map [ string ] * state . State {
` [["__alert_rule_namespace_uid__","test_namespace_uid"],["__alert_rule_uid__","test_alert_rule_uid_2"],["alertname","test_title"],["instance_label","test"],["label","test"]] ` : {
AlertRuleUID : "test_alert_rule_uid_2" ,
OrgID : 1 ,
CacheId : ` [["__alert_rule_namespace_uid__","test_namespace_uid"],["__alert_rule_uid__","test_alert_rule_uid_2"],["alertname","test_title"],["instance_label","test"],["label","test"]] ` ,
Labels : data . Labels {
"__alert_rule_namespace_uid__" : "test_namespace_uid" ,
"__alert_rule_uid__" : "test_alert_rule_uid_2" ,
"alertname" : "test_title" ,
"label" : "test" ,
"instance_label" : "test" ,
} ,
State : eval . NoData ,
Results : [ ] state . Evaluation {
{
EvaluationTime : evaluationTime ,
EvaluationState : eval . Normal ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
{
EvaluationTime : evaluationTime . Add ( 30 * time . Second ) ,
EvaluationState : eval . Alerting ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
{
EvaluationTime : evaluationTime . Add ( 40 * time . Second ) ,
EvaluationState : eval . Error ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
{
EvaluationTime : evaluationTime . Add ( 50 * time . Second ) ,
EvaluationState : eval . NoData ,
Values : make ( map [ string ] state . EvaluationValue ) ,
} ,
} ,
StartsAt : evaluationTime . Add ( 30 * time . Second ) ,
EndsAt : evaluationTime . Add ( 50 * time . Second ) . Add ( state . ResendDelay * 3 ) ,
LastEvaluationTime : evaluationTime . Add ( 50 * time . Second ) ,
EvaluationDuration : evaluationDuration ,
Annotations : map [ string ] string { "annotation" : "test" } ,
} ,
} ,
} ,
{
desc : "template is correctly expanded" ,
alertRule : & models . AlertRule {