2020-04-06 11:44:03 +00:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
2020-05-18 08:55:16 +00:00
|
|
|
"net/url"
|
2020-04-06 11:44:03 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlert_ExecTemplate(t *testing.T) {
|
2020-05-18 08:55:16 +00:00
|
|
|
u, _ := url.Parse("https://victoriametrics.com/path")
|
|
|
|
InitTemplateFunc(u)
|
2020-04-06 11:44:03 +00:00
|
|
|
testCases := []struct {
|
2020-05-19 09:59:46 +00:00
|
|
|
name string
|
2020-04-06 11:44:03 +00:00
|
|
|
alert *Alert
|
|
|
|
annotations map[string]string
|
|
|
|
expTpl map[string]string
|
|
|
|
}{
|
|
|
|
{
|
2020-05-19 09:59:46 +00:00
|
|
|
name: "empty-alert",
|
2020-04-06 11:44:03 +00:00
|
|
|
alert: &Alert{},
|
|
|
|
annotations: map[string]string{},
|
|
|
|
expTpl: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
2020-05-18 08:55:16 +00:00
|
|
|
name: "no-template",
|
2020-04-06 11:44:03 +00:00
|
|
|
alert: &Alert{
|
|
|
|
Value: 1e4,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"instance": "localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
annotations: map[string]string{},
|
|
|
|
expTpl: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
2020-05-18 08:55:16 +00:00
|
|
|
name: "label-template",
|
2020-04-06 11:44:03 +00:00
|
|
|
alert: &Alert{
|
|
|
|
Value: 1e4,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"job": "staging",
|
|
|
|
"instance": "localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
annotations: map[string]string{
|
|
|
|
"summary": "Too high connection number for {{$labels.instance}} for job {{$labels.job}}",
|
|
|
|
"description": "It is {{ $value }} connections for {{$labels.instance}}",
|
|
|
|
},
|
|
|
|
expTpl: map[string]string{
|
|
|
|
"summary": "Too high connection number for localhost for job staging",
|
|
|
|
"description": "It is 10000 connections for localhost",
|
|
|
|
},
|
|
|
|
},
|
2020-05-18 08:55:16 +00:00
|
|
|
{
|
|
|
|
name: "expression-template",
|
|
|
|
alert: &Alert{
|
2020-05-19 09:59:46 +00:00
|
|
|
Expr: "vm_rows>0",
|
2020-05-18 08:55:16 +00:00
|
|
|
},
|
|
|
|
annotations: map[string]string{
|
|
|
|
"exprEscapedQuery": "{{ $expr|queryEscape }}",
|
|
|
|
"exprEscapedPath": "{{ $expr|pathEscape }}",
|
|
|
|
},
|
|
|
|
expTpl: map[string]string{
|
|
|
|
"exprEscapedQuery": "vm_rows%3E0",
|
|
|
|
"exprEscapedPath": "vm_rows%3E0",
|
|
|
|
},
|
|
|
|
},
|
2020-04-06 11:44:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 08:55:16 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-04-06 11:44:03 +00:00
|
|
|
tpl, err := tc.alert.ExecTemplate(tc.annotations)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(tpl) != len(tc.expTpl) {
|
|
|
|
t.Fatalf("expected %d elements; got %d", len(tc.expTpl), len(tpl))
|
|
|
|
}
|
|
|
|
for k := range tc.expTpl {
|
|
|
|
got, exp := tpl[k], tc.expTpl[k]
|
|
|
|
if got != exp {
|
|
|
|
t.Fatalf("expected %q=%q; got %q=%q", k, exp, k, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|