2020-04-06 13:44:03 +02:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-12-14 19:11:45 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
2020-04-06 13:44:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlert_ExecTemplate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2020-05-19 11:59:46 +02:00
|
|
|
name string
|
2020-04-06 13:44:03 +02:00
|
|
|
alert *Alert
|
|
|
|
annotations map[string]string
|
|
|
|
expTpl map[string]string
|
|
|
|
}{
|
|
|
|
{
|
2020-05-19 11:59:46 +02:00
|
|
|
name: "empty-alert",
|
2020-04-06 13:44:03 +02:00
|
|
|
alert: &Alert{},
|
|
|
|
annotations: map[string]string{},
|
|
|
|
expTpl: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
2020-05-18 10:55:16 +02:00
|
|
|
name: "no-template",
|
2020-04-06 13:44:03 +02:00
|
|
|
alert: &Alert{
|
|
|
|
Value: 1e4,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"instance": "localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
annotations: map[string]string{},
|
|
|
|
expTpl: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
2020-05-18 10:55:16 +02:00
|
|
|
name: "label-template",
|
2020-04-06 13:44:03 +02: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 10:55:16 +02:00
|
|
|
{
|
|
|
|
name: "expression-template",
|
|
|
|
alert: &Alert{
|
2020-05-20 21:20:31 +02:00
|
|
|
Expr: `vm_rows{"label"="bar"}>0`,
|
2020-05-18 10:55:16 +02:00
|
|
|
},
|
|
|
|
annotations: map[string]string{
|
2020-05-20 21:20:31 +02:00
|
|
|
"exprEscapedQuery": "{{ $expr|quotesEscape|queryEscape }}",
|
|
|
|
"exprEscapedPath": "{{ $expr|quotesEscape|pathEscape }}",
|
2020-05-18 10:55:16 +02:00
|
|
|
},
|
|
|
|
expTpl: map[string]string{
|
2020-05-20 21:20:31 +02:00
|
|
|
"exprEscapedQuery": "vm_rows%7B%5C%22label%5C%22%3D%5C%22bar%5C%22%7D%3E0",
|
|
|
|
"exprEscapedPath": "vm_rows%7B%5C%22label%5C%22=%5C%22bar%5C%22%7D%3E0",
|
2020-05-18 10:55:16 +02:00
|
|
|
},
|
|
|
|
},
|
2020-12-14 19:11:45 +01:00
|
|
|
{
|
|
|
|
name: "query",
|
|
|
|
alert: &Alert{Expr: `vm_rows{"label"="bar"}>0`},
|
|
|
|
annotations: map[string]string{
|
|
|
|
"summary": `{{ query "foo" | first | value }}`,
|
|
|
|
"desc": `{{ range query "bar" }}{{ . | label "foo" }} {{ . | value }};{{ end }}`,
|
|
|
|
},
|
|
|
|
expTpl: map[string]string{
|
|
|
|
"summary": "1",
|
|
|
|
"desc": "bar 1;garply 2;",
|
|
|
|
},
|
|
|
|
},
|
2020-04-06 13:44:03 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:11:45 +01:00
|
|
|
qFn := func(q string) ([]datasource.Metric, error) {
|
|
|
|
return []datasource.Metric{
|
|
|
|
{
|
|
|
|
Labels: []datasource.Label{
|
|
|
|
{Name: "foo", Value: "bar"},
|
|
|
|
{Name: "baz", Value: "qux"},
|
|
|
|
},
|
2021-06-09 11:20:38 +02:00
|
|
|
Values: []float64{1},
|
|
|
|
Timestamps: []int64{1},
|
2020-12-14 19:11:45 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Labels: []datasource.Label{
|
|
|
|
{Name: "foo", Value: "garply"},
|
|
|
|
{Name: "baz", Value: "fred"},
|
|
|
|
},
|
2021-06-09 11:20:38 +02:00
|
|
|
Values: []float64{2},
|
|
|
|
Timestamps: []int64{1},
|
2020-12-14 19:11:45 +01:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
2020-05-18 10:55:16 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-12-14 19:11:45 +01:00
|
|
|
tpl, err := tc.alert.ExecTemplate(qFn, tc.annotations)
|
2020-04-06 13:44:03 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|