2020-05-10 18:58:17 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
|
2021-07-12 11:34:10 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/datasource"
|
2020-05-10 18:58:17 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2021-07-12 11:34:10 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/utils"
|
2020-05-10 18:58:17 +02:00
|
|
|
)
|
|
|
|
|
2020-09-03 00:00:55 +02:00
|
|
|
func init() {
|
|
|
|
// Disable rand sleep on group start during tests in order to speed up test execution.
|
|
|
|
// Rand sleep is needed only in prod code.
|
|
|
|
skipRandSleepOnGroupStart = true
|
|
|
|
}
|
|
|
|
|
2020-05-10 18:58:17 +02:00
|
|
|
func TestUpdateWith(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2020-06-15 21:15:47 +02:00
|
|
|
currentRules []config.Rule
|
|
|
|
newRules []config.Rule
|
2020-05-10 18:58:17 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"new rule",
|
2020-06-15 21:15:47 +02:00
|
|
|
nil,
|
|
|
|
[]config.Rule{{Alert: "bar"}},
|
2020-05-10 18:58:17 +02:00
|
|
|
},
|
|
|
|
{
|
2020-06-01 12:46:37 +02:00
|
|
|
"update alerting rule",
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{{
|
|
|
|
Alert: "foo",
|
|
|
|
Expr: "up > 0",
|
2021-07-12 11:34:10 +02:00
|
|
|
For: utils.NewPromDuration(time.Second),
|
2020-05-10 18:58:17 +02:00
|
|
|
Labels: map[string]string{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"summary": "{{ $value|humanize }}",
|
|
|
|
"description": "{{$labels}}",
|
|
|
|
},
|
|
|
|
}},
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{{
|
|
|
|
Alert: "foo",
|
|
|
|
Expr: "up > 10",
|
2021-07-12 11:34:10 +02:00
|
|
|
For: utils.NewPromDuration(time.Second),
|
2020-05-10 18:58:17 +02:00
|
|
|
Labels: map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
},
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"summary": "none",
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
2020-06-01 12:46:37 +02:00
|
|
|
{
|
|
|
|
"update recording rule",
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{{
|
|
|
|
Record: "foo",
|
|
|
|
Expr: "max(up)",
|
2020-06-01 12:46:37 +02:00
|
|
|
Labels: map[string]string{
|
|
|
|
"bar": "baz",
|
|
|
|
},
|
|
|
|
}},
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{{
|
|
|
|
Record: "foo",
|
|
|
|
Expr: "min(up)",
|
2020-06-01 12:46:37 +02:00
|
|
|
Labels: map[string]string{
|
|
|
|
"baz": "bar",
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
},
|
2020-05-10 18:58:17 +02:00
|
|
|
{
|
|
|
|
"empty rule",
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{{Alert: "foo"}, {Record: "bar"}},
|
|
|
|
nil,
|
2020-05-10 18:58:17 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"multiple rules",
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{
|
|
|
|
{Alert: "bar"},
|
|
|
|
{Alert: "baz"},
|
|
|
|
{Alert: "foo"},
|
2020-06-01 12:46:37 +02:00
|
|
|
},
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{
|
|
|
|
{Alert: "baz"},
|
|
|
|
{Record: "foo"},
|
2020-06-01 12:46:37 +02:00
|
|
|
},
|
2020-05-15 08:55:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"replace rule",
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{{Alert: "foo1"}},
|
|
|
|
[]config.Rule{{Alert: "foo2"}},
|
2020-05-15 08:55:22 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"replace multiple rules",
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{
|
|
|
|
{Alert: "foo1"},
|
|
|
|
{Record: "foo2"},
|
|
|
|
{Alert: "foo3"},
|
2020-06-01 12:46:37 +02:00
|
|
|
},
|
2020-06-15 21:15:47 +02:00
|
|
|
[]config.Rule{
|
|
|
|
{Alert: "foo3"},
|
|
|
|
{Alert: "foo4"},
|
|
|
|
{Record: "foo5"},
|
2020-06-01 12:46:37 +02:00
|
|
|
},
|
2020-05-10 18:58:17 +02:00
|
|
|
},
|
2021-04-28 22:41:15 +02:00
|
|
|
{
|
|
|
|
"update datasource type",
|
|
|
|
[]config.Rule{
|
|
|
|
{Alert: "foo1", Type: datasource.NewPrometheusType()},
|
|
|
|
{Alert: "foo3", Type: datasource.NewGraphiteType()},
|
|
|
|
},
|
|
|
|
[]config.Rule{
|
|
|
|
{Alert: "foo1", Type: datasource.NewGraphiteType()},
|
|
|
|
{Alert: "foo10", Type: datasource.NewPrometheusType()},
|
|
|
|
},
|
|
|
|
},
|
2020-05-10 18:58:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2020-06-15 21:15:47 +02:00
|
|
|
g := &Group{Name: "test"}
|
2021-04-28 22:41:15 +02:00
|
|
|
qb := &fakeQuerier{}
|
2020-06-15 21:15:47 +02:00
|
|
|
for _, r := range tc.currentRules {
|
|
|
|
r.ID = config.HashRule(r)
|
2021-04-28 22:41:15 +02:00
|
|
|
g.Rules = append(g.Rules, g.newRule(qb, r))
|
2020-06-15 21:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ng := &Group{Name: "test"}
|
|
|
|
for _, r := range tc.newRules {
|
|
|
|
r.ID = config.HashRule(r)
|
2021-04-28 22:41:15 +02:00
|
|
|
ng.Rules = append(ng.Rules, ng.newRule(qb, r))
|
2020-06-15 21:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := g.updateWith(ng)
|
2020-06-01 12:46:37 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
|
|
|
|
if len(g.Rules) != len(tc.newRules) {
|
|
|
|
t.Fatalf("expected to have %d rules; got: %d",
|
|
|
|
len(g.Rules), len(tc.newRules))
|
|
|
|
}
|
2020-05-15 08:55:22 +02:00
|
|
|
sort.Slice(g.Rules, func(i, j int) bool {
|
2020-06-01 12:46:37 +02:00
|
|
|
return g.Rules[i].ID() < g.Rules[j].ID()
|
2020-05-15 08:55:22 +02:00
|
|
|
})
|
2020-06-15 21:15:47 +02:00
|
|
|
sort.Slice(ng.Rules, func(i, j int) bool {
|
|
|
|
return ng.Rules[i].ID() < ng.Rules[j].ID()
|
|
|
|
})
|
2020-05-10 18:58:17 +02:00
|
|
|
for i, r := range g.Rules {
|
2020-06-15 21:15:47 +02:00
|
|
|
got, want := r, ng.Rules[i]
|
2020-06-01 12:46:37 +02:00
|
|
|
if got.ID() != want.ID() {
|
|
|
|
t.Fatalf("expected to have rule %q; got %q", want, got)
|
2020-05-10 18:58:17 +02:00
|
|
|
}
|
2020-06-01 12:46:37 +02:00
|
|
|
if err := compareRules(t, got, want); err != nil {
|
|
|
|
t.Fatalf("comparsion error: %s", err)
|
2020-05-10 18:58:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroupStart(t *testing.T) {
|
|
|
|
// TODO: make parsing from string instead of file
|
2020-06-06 22:27:09 +02:00
|
|
|
groups, err := config.Parse([]string{"config/testdata/rules1-good.rules"}, true, true)
|
2020-05-10 18:58:17 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse rules: %s", err)
|
|
|
|
}
|
2020-06-01 12:46:37 +02:00
|
|
|
const evalInterval = time.Millisecond
|
2020-05-10 18:58:17 +02:00
|
|
|
fs := &fakeQuerier{}
|
2021-04-28 22:41:15 +02:00
|
|
|
fn := &fakeNotifier{}
|
|
|
|
|
|
|
|
g := newGroup(groups[0], fs, evalInterval, map[string]string{"cluster": "east-1"})
|
|
|
|
g.Concurrency = 2
|
2020-05-10 18:58:17 +02:00
|
|
|
|
|
|
|
const inst1, inst2, job = "foo", "bar", "baz"
|
|
|
|
m1 := metricWithLabels(t, "instance", inst1, "job", job)
|
|
|
|
m2 := metricWithLabels(t, "instance", inst2, "job", job)
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
r := g.Rules[0].(*AlertingRule)
|
2020-12-14 19:11:45 +01:00
|
|
|
alert1, err := r.newAlert(m1, time.Now(), nil)
|
2020-05-10 18:58:17 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("faield to create alert: %s", err)
|
|
|
|
}
|
|
|
|
alert1.State = notifier.StateFiring
|
2020-11-09 23:27:32 +01:00
|
|
|
// add external label
|
|
|
|
alert1.Labels["cluster"] = "east-1"
|
|
|
|
// add rule labels - see config/testdata/rules1-good.rules
|
|
|
|
alert1.Labels["label"] = "bar"
|
|
|
|
alert1.Labels["host"] = inst1
|
2020-05-10 18:58:17 +02:00
|
|
|
alert1.ID = hash(m1)
|
|
|
|
|
2020-12-14 19:11:45 +01:00
|
|
|
alert2, err := r.newAlert(m2, time.Now(), nil)
|
2020-05-10 18:58:17 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("faield to create alert: %s", err)
|
|
|
|
}
|
|
|
|
alert2.State = notifier.StateFiring
|
2020-11-09 23:27:32 +01:00
|
|
|
// add external label
|
|
|
|
alert2.Labels["cluster"] = "east-1"
|
|
|
|
// add rule labels - see config/testdata/rules1-good.rules
|
|
|
|
alert2.Labels["label"] = "bar"
|
|
|
|
alert2.Labels["host"] = inst2
|
2020-05-10 18:58:17 +02:00
|
|
|
alert2.ID = hash(m2)
|
|
|
|
|
|
|
|
finished := make(chan struct{})
|
|
|
|
fs.add(m1)
|
|
|
|
fs.add(m2)
|
|
|
|
go func() {
|
2021-04-28 22:41:15 +02:00
|
|
|
g.start(context.Background(), []notifier.Notifier{fn}, nil)
|
2020-05-10 18:58:17 +02:00
|
|
|
close(finished)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for multiple evals
|
|
|
|
time.Sleep(20 * evalInterval)
|
|
|
|
|
|
|
|
gotAlerts := fn.getAlerts()
|
|
|
|
expectedAlerts := []notifier.Alert{*alert1, *alert2}
|
|
|
|
compareAlerts(t, expectedAlerts, gotAlerts)
|
|
|
|
|
|
|
|
// reset previous data
|
|
|
|
fs.reset()
|
|
|
|
// and set only one datapoint for response
|
|
|
|
fs.add(m1)
|
|
|
|
|
|
|
|
// wait for multiple evals
|
|
|
|
time.Sleep(20 * evalInterval)
|
|
|
|
|
|
|
|
gotAlerts = fn.getAlerts()
|
|
|
|
expectedAlerts = []notifier.Alert{*alert1}
|
|
|
|
compareAlerts(t, expectedAlerts, gotAlerts)
|
|
|
|
|
|
|
|
g.close()
|
|
|
|
<-finished
|
|
|
|
}
|