@ -4,6 +4,7 @@ import (
"context"
"fmt"
"testing"
"time"
"github.com/go-openapi/strfmt"
alertingModels "github.com/grafana/alerting/models"
@ -17,13 +18,42 @@ import (
)
var (
timeNow = time . Now ( )
simpleAlert = amv2 . PostableAlert {
Alert : amv2 . Alert {
Labels : amv2 . LabelSet { "__alert_rule_uid__" : "rule uid" , "alertname" : "alert1" , "lbl1" : "val1" } ,
Labels : amv2 . LabelSet {
alertingModels . RuleUIDLabel : "rule uid" ,
prometheusModel . AlertNameLabel : "alert1" ,
"lbl1" : "val1" ,
} ,
} ,
Annotations : amv2 . LabelSet { "ann1" : "annv1" , "__dashboardUid__" : "abcd" , "__panelId__" : "efgh" , "__alertImageToken__" : "test-image-1" } ,
StartsAt : strfmt . DateTime { } ,
EndsAt : strfmt . DateTime { } ,
Annotations : amv2 . LabelSet {
"ann1" : "annv1" ,
alertingModels . DashboardUIDAnnotation : "abcd" ,
alertingModels . PanelIDAnnotation : "42" ,
alertingModels . ImageTokenAnnotation : "test-image-1" ,
alertingModels . OrgIDAnnotation : "1" ,
} ,
StartsAt : strfmt . DateTime ( timeNow ) ,
EndsAt : strfmt . DateTime ( timeNow . Add ( time . Hour ) ) , // Firing.
}
resolvedAlert = amv2 . PostableAlert {
Alert : amv2 . Alert {
Labels : amv2 . LabelSet {
alertingModels . RuleUIDLabel : "rule uid" ,
prometheusModel . AlertNameLabel : "alert1" ,
"lbl1" : "val1" ,
} ,
} ,
Annotations : amv2 . LabelSet {
"ann1" : "annv1" ,
alertingModels . DashboardUIDAnnotation : "abcd" ,
alertingModels . PanelIDAnnotation : "42" ,
alertingModels . ImageTokenAnnotation : "test-image-1" ,
alertingModels . OrgIDAnnotation : "1" ,
} ,
StartsAt : strfmt . DateTime ( timeNow . Add ( - 30 * time . Minute ) ) ,
EndsAt : strfmt . DateTime ( timeNow ) , // Resolved.
}
)
@ -207,6 +237,45 @@ CommonAnnotations: {{ range .CommonAnnotations.SortedPairs }}{{ .Name }}={{ .Val
} } ,
Errors : nil ,
} ,
} , {
name : "DashboardURL generation contains from and to time" ,
input : apimodels . TestTemplatesConfigBodyParams {
Alerts : [ ] * amv2 . PostableAlert { & resolvedAlert } , // We specifically use a resolved alert as otherwise the `to` time will be the current time and difficult to test.
Name : "slack.title" ,
Template : ` {{ define "slack.title" }} {{ ( index .Alerts 0 ) .DashboardURL }} {{ end }} ` ,
} ,
expected : TestTemplatesResults {
Results : [ ] alertingNotify . TestTemplatesResult { {
Name : "slack.title" ,
Text : fmt . Sprintf ( "http://localhost:9093/d/%s?from=%d&orgId=%s&to=%d" ,
resolvedAlert . Annotations [ alertingModels . DashboardUIDAnnotation ] ,
timeNow . Add ( - 90 * time . Minute ) . UnixMilli ( ) , // StartsAt - 1hr.
resolvedAlert . Annotations [ alertingModels . OrgIDAnnotation ] ,
timeNow . UnixMilli ( ) ) ,
Scope : alertingNotify . TemplateScope ( apimodels . RootScope ) ,
} } ,
Errors : nil ,
} ,
} , {
name : "PanelURL generation contains from and to time " ,
input : apimodels . TestTemplatesConfigBodyParams {
Alerts : [ ] * amv2 . PostableAlert { & resolvedAlert } , // We specifically use a resolved alert as otherwise the `to` time will be the current time and difficult to test.
Name : "slack.title" ,
Template : ` {{ define "slack.title" }} {{ ( index .Alerts 0 ) .PanelURL }} {{ end }} ` ,
} ,
expected : TestTemplatesResults {
Results : [ ] alertingNotify . TestTemplatesResult { {
Name : "slack.title" ,
Text : fmt . Sprintf ( "http://localhost:9093/d/%s?from=%d&orgId=%s&to=%d&viewPanel=%s" ,
resolvedAlert . Annotations [ alertingModels . DashboardUIDAnnotation ] ,
timeNow . Add ( - 90 * time . Minute ) . UnixMilli ( ) , // StartsAt - 1hr.
resolvedAlert . Annotations [ alertingModels . OrgIDAnnotation ] ,
timeNow . UnixMilli ( ) ,
resolvedAlert . Annotations [ alertingModels . PanelIDAnnotation ] ) ,
Scope : alertingNotify . TemplateScope ( apimodels . RootScope ) ,
} } ,
Errors : nil ,
} ,
} ,
}