2020-05-10 16:58:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math/rand"
|
2020-06-01 10:46:37 +00:00
|
|
|
"os"
|
2021-11-29 23:18:48 +00:00
|
|
|
"strings"
|
2020-05-10 16:58:17 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-06-01 10:46:37 +00:00
|
|
|
|
2021-05-25 13:27:22 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/config"
|
2020-06-01 10:46:37 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2021-11-29 23:18:48 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/remotewrite"
|
2022-05-14 09:38:44 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/templates"
|
2020-05-10 16:58:17 +00:00
|
|
|
)
|
|
|
|
|
2020-06-01 10:46:37 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2022-05-14 09:38:44 +00:00
|
|
|
if err := templates.Load([]string{"testdata/templates/*good.tmpl"}, true); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-06-01 10:46:37 +00:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2020-09-03 08:04:42 +00:00
|
|
|
// TestManagerEmptyRulesDir tests
|
|
|
|
// successful cases of
|
|
|
|
// starting with empty rules folder
|
|
|
|
func TestManagerEmptyRulesDir(t *testing.T) {
|
2020-05-10 16:58:17 +00:00
|
|
|
m := &manager{groups: make(map[uint64]*Group)}
|
2021-05-25 13:27:22 +00:00
|
|
|
cfg := loadCfg(t, []string{"foo/bar"}, true, true)
|
|
|
|
if err := m.update(context.Background(), cfg, false); err != nil {
|
2022-07-18 09:02:51 +00:00
|
|
|
t.Fatalf("expected to load successfully with empty rules dir; got err instead: %v", err)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestManagerUpdateConcurrent supposed to test concurrent
|
|
|
|
// execution of configuration update.
|
|
|
|
// Should be executed with -race flag
|
|
|
|
func TestManagerUpdateConcurrent(t *testing.T) {
|
2020-05-17 14:12:09 +00:00
|
|
|
m := &manager{
|
2021-04-28 20:41:15 +00:00
|
|
|
groups: make(map[uint64]*Group),
|
|
|
|
querierBuilder: &fakeQuerier{},
|
2022-02-02 12:11:41 +00:00
|
|
|
notifiers: func() []notifier.Notifier { return []notifier.Notifier{&fakeNotifier{}} },
|
2020-05-17 14:12:09 +00:00
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
paths := []string{
|
2020-06-01 10:46:37 +00:00
|
|
|
"config/testdata/dir/rules0-good.rules",
|
|
|
|
"config/testdata/dir/rules0-bad.rules",
|
|
|
|
"config/testdata/dir/rules1-good.rules",
|
|
|
|
"config/testdata/dir/rules1-bad.rules",
|
2022-05-14 09:38:44 +00:00
|
|
|
"config/testdata/rules/rules0-good.rules",
|
|
|
|
"config/testdata/rules/rules1-good.rules",
|
|
|
|
"config/testdata/rules/rules2-good.rules",
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
2021-06-09 09:20:38 +00:00
|
|
|
evalInterval := *evaluationInterval
|
|
|
|
defer func() { *evaluationInterval = evalInterval }()
|
2020-05-17 14:12:09 +00:00
|
|
|
*evaluationInterval = time.Millisecond
|
2021-05-25 13:27:22 +00:00
|
|
|
cfg := loadCfg(t, []string{paths[0]}, true, true)
|
|
|
|
if err := m.start(context.Background(), cfg); err != nil {
|
2020-05-17 14:12:09 +00:00
|
|
|
t.Fatalf("failed to start: %s", err)
|
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
|
2020-05-17 14:12:09 +00:00
|
|
|
const workers = 500
|
|
|
|
const iterations = 10
|
2020-05-10 16:58:17 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2020-05-17 14:12:09 +00:00
|
|
|
wg.Add(workers)
|
|
|
|
for i := 0; i < workers; i++ {
|
2023-01-24 03:25:08 +00:00
|
|
|
go func(n int) {
|
2020-05-10 16:58:17 +00:00
|
|
|
defer wg.Done()
|
2023-01-24 03:25:08 +00:00
|
|
|
r := rand.New(rand.NewSource(int64(n)))
|
2020-05-17 14:12:09 +00:00
|
|
|
for i := 0; i < iterations; i++ {
|
2023-01-24 03:25:08 +00:00
|
|
|
rnd := r.Intn(len(paths))
|
2022-07-22 11:50:41 +00:00
|
|
|
cfg, err := config.Parse([]string{paths[rnd]}, notifier.ValidateTemplates, true)
|
2021-05-25 13:27:22 +00:00
|
|
|
if err != nil { // update can fail and this is expected
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_ = m.update(context.Background(), cfg, false)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
2023-01-24 03:25:08 +00:00
|
|
|
}(i)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestManagerUpdate tests sequential configuration
|
|
|
|
// updates.
|
|
|
|
func TestManagerUpdate(t *testing.T) {
|
2020-06-01 10:46:37 +00:00
|
|
|
const defaultEvalInterval = time.Second * 30
|
|
|
|
currentEvalInterval := *evaluationInterval
|
|
|
|
*evaluationInterval = defaultEvalInterval
|
|
|
|
defer func() {
|
|
|
|
*evaluationInterval = currentEvalInterval
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
VMRows = &AlertingRule{
|
|
|
|
Name: "VMRows",
|
|
|
|
Expr: "vm_rows > 0",
|
|
|
|
For: 10 * time.Second,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"label": "bar",
|
|
|
|
"host": "{{ $labels.instance }}",
|
|
|
|
},
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"summary": "{{ $value|humanize }}",
|
|
|
|
"description": "{{$labels}}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Conns = &AlertingRule{
|
|
|
|
Name: "Conns",
|
|
|
|
Expr: "sum(vm_tcplistener_conns) by(instance) > 1",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"summary": "Too high connection number for {{$labels.instance}}",
|
|
|
|
"description": "It is {{ $value }} connections for {{$labels.instance}}",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ExampleAlertAlwaysFiring = &AlertingRule{
|
|
|
|
Name: "ExampleAlertAlwaysFiring",
|
|
|
|
Expr: "sum by(job) (up == 1)",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-05-10 16:58:17 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
initPath string
|
|
|
|
updatePath string
|
|
|
|
want []*Group
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "update good rules",
|
2022-05-14 09:38:44 +00:00
|
|
|
initPath: "config/testdata/rules/rules0-good.rules",
|
2020-06-01 10:46:37 +00:00
|
|
|
updatePath: "config/testdata/dir/rules1-good.rules",
|
2020-05-10 16:58:17 +00:00
|
|
|
want: []*Group{
|
|
|
|
{
|
2020-06-01 10:46:37 +00:00
|
|
|
File: "config/testdata/dir/rules1-good.rules",
|
|
|
|
Name: "duplicatedGroupDiffFiles",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: config.NewPrometheusType(),
|
2020-06-01 10:46:37 +00:00
|
|
|
Interval: defaultEvalInterval,
|
|
|
|
Rules: []Rule{
|
|
|
|
&AlertingRule{
|
|
|
|
Name: "VMRows",
|
|
|
|
Expr: "vm_rows > 0",
|
|
|
|
For: 5 * time.Minute,
|
2021-08-31 11:52:34 +00:00
|
|
|
Labels: map[string]string{"dc": "gcp", "label": "bar"},
|
2020-06-01 10:46:37 +00:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"summary": "{{ $value }}",
|
|
|
|
"description": "{{$labels}}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-10 16:58:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update good rules from 1 to 2 groups",
|
2022-05-14 09:38:44 +00:00
|
|
|
initPath: "config/testdata/dir/rules/rules1-good.rules",
|
|
|
|
updatePath: "config/testdata/rules/rules0-good.rules",
|
2020-05-10 16:58:17 +00:00
|
|
|
want: []*Group{
|
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
File: "config/testdata/rules/rules0-good.rules",
|
2020-06-01 10:46:37 +00:00
|
|
|
Name: "groupGorSingleAlert",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: config.NewPrometheusType(),
|
2020-06-01 10:46:37 +00:00
|
|
|
Rules: []Rule{VMRows},
|
|
|
|
Interval: defaultEvalInterval,
|
|
|
|
},
|
2020-05-10 16:58:17 +00:00
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
File: "config/testdata/rules/rules0-good.rules",
|
2020-06-01 10:46:37 +00:00
|
|
|
Interval: defaultEvalInterval,
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: config.NewPrometheusType(),
|
2020-06-01 10:46:37 +00:00
|
|
|
Name: "TestGroup", Rules: []Rule{
|
|
|
|
Conns,
|
|
|
|
ExampleAlertAlwaysFiring,
|
2020-05-10 16:58:17 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "update with one bad rule file",
|
2022-05-14 09:38:44 +00:00
|
|
|
initPath: "config/testdata/rules/rules0-good.rules",
|
2020-06-01 10:46:37 +00:00
|
|
|
updatePath: "config/testdata/dir/rules2-bad.rules",
|
2020-05-10 16:58:17 +00:00
|
|
|
want: []*Group{
|
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
File: "config/testdata/rules/rules0-good.rules",
|
2020-06-01 10:46:37 +00:00
|
|
|
Name: "groupGorSingleAlert",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: config.NewPrometheusType(),
|
2020-06-01 10:46:37 +00:00
|
|
|
Interval: defaultEvalInterval,
|
|
|
|
Rules: []Rule{VMRows},
|
|
|
|
},
|
2020-05-10 16:58:17 +00:00
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
File: "config/testdata/rules/rules0-good.rules",
|
2020-06-01 10:46:37 +00:00
|
|
|
Interval: defaultEvalInterval,
|
2021-02-01 13:02:44 +00:00
|
|
|
Name: "TestGroup",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: config.NewPrometheusType(),
|
2021-02-01 13:02:44 +00:00
|
|
|
Rules: []Rule{
|
2020-06-01 10:46:37 +00:00
|
|
|
Conns,
|
|
|
|
ExampleAlertAlwaysFiring,
|
2020-05-10 16:58:17 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
2020-09-03 08:04:42 +00:00
|
|
|
{
|
|
|
|
name: "update empty dir rules from 0 to 2 groups",
|
|
|
|
initPath: "config/testdata/empty/*",
|
2022-05-14 09:38:44 +00:00
|
|
|
updatePath: "config/testdata/rules/rules0-good.rules",
|
2020-09-03 08:04:42 +00:00
|
|
|
want: []*Group{
|
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
File: "config/testdata/rules/rules0-good.rules",
|
2020-09-03 08:04:42 +00:00
|
|
|
Name: "groupGorSingleAlert",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: config.NewPrometheusType(),
|
2020-09-03 08:04:42 +00:00
|
|
|
Interval: defaultEvalInterval,
|
|
|
|
Rules: []Rule{VMRows},
|
|
|
|
},
|
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
File: "config/testdata/rules/rules0-good.rules",
|
2020-09-03 08:04:42 +00:00
|
|
|
Interval: defaultEvalInterval,
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: config.NewPrometheusType(),
|
2020-09-03 08:04:42 +00:00
|
|
|
Name: "TestGroup", Rules: []Rule{
|
|
|
|
Conns,
|
|
|
|
ExampleAlertAlwaysFiring,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
2021-11-29 23:18:48 +00:00
|
|
|
m := &manager{
|
|
|
|
groups: make(map[uint64]*Group),
|
|
|
|
querierBuilder: &fakeQuerier{},
|
2022-02-02 12:11:41 +00:00
|
|
|
notifiers: func() []notifier.Notifier { return []notifier.Notifier{&fakeNotifier{}} },
|
2021-11-29 23:18:48 +00:00
|
|
|
}
|
2021-05-25 13:27:22 +00:00
|
|
|
|
|
|
|
cfgInit := loadCfg(t, []string{tc.initPath}, true, true)
|
|
|
|
if err := m.update(ctx, cfgInit, false); err != nil {
|
2020-05-10 16:58:17 +00:00
|
|
|
t.Fatalf("failed to complete initial rules update: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-07-22 11:50:41 +00:00
|
|
|
cfgUpdate, err := config.Parse([]string{tc.updatePath}, notifier.ValidateTemplates, true)
|
2021-05-25 13:27:22 +00:00
|
|
|
if err == nil { // update can fail and that's expected
|
|
|
|
_ = m.update(ctx, cfgUpdate, false)
|
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
if len(tc.want) != len(m.groups) {
|
|
|
|
t.Fatalf("\nwant number of groups: %d;\ngot: %d ", len(tc.want), len(m.groups))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, wantG := range tc.want {
|
|
|
|
gotG, ok := m.groups[wantG.ID()]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected to have group %q", wantG.Name)
|
|
|
|
}
|
2020-06-01 10:46:37 +00:00
|
|
|
compareGroups(t, wantG, gotG)
|
2020-05-10 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
m.close()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 13:27:22 +00:00
|
|
|
|
2021-11-29 23:18:48 +00:00
|
|
|
func TestManagerUpdateNegative(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
notifiers []notifier.Notifier
|
|
|
|
rw *remotewrite.Client
|
|
|
|
cfg config.Group
|
|
|
|
expErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
config.Group{Name: "Recording rule only",
|
|
|
|
Rules: []config.Rule{
|
|
|
|
{Record: "record", Expr: "max(up)"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"contains recording rules",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
config.Group{Name: "Alerting rule only",
|
|
|
|
Rules: []config.Rule{
|
|
|
|
{Alert: "alert", Expr: "up > 0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"contains alerting rules",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]notifier.Notifier{&fakeNotifier{}},
|
|
|
|
nil,
|
|
|
|
config.Group{Name: "Recording and alerting rules",
|
|
|
|
Rules: []config.Rule{
|
|
|
|
{Alert: "alert1", Expr: "up > 0"},
|
|
|
|
{Alert: "alert2", Expr: "up > 0"},
|
|
|
|
{Record: "record", Expr: "max(up)"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"contains recording rules",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
&remotewrite.Client{},
|
|
|
|
config.Group{Name: "Recording and alerting rules",
|
|
|
|
Rules: []config.Rule{
|
|
|
|
{Record: "record1", Expr: "max(up)"},
|
|
|
|
{Record: "record2", Expr: "max(up)"},
|
|
|
|
{Alert: "alert", Expr: "up > 0"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"contains alerting rules",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.cfg.Name, func(t *testing.T) {
|
|
|
|
m := &manager{
|
|
|
|
groups: make(map[uint64]*Group),
|
|
|
|
querierBuilder: &fakeQuerier{},
|
|
|
|
rw: tc.rw,
|
|
|
|
}
|
2022-02-02 12:11:41 +00:00
|
|
|
if tc.notifiers != nil {
|
|
|
|
m.notifiers = func() []notifier.Notifier { return tc.notifiers }
|
|
|
|
}
|
2021-11-29 23:18:48 +00:00
|
|
|
err := m.update(context.Background(), []config.Group{tc.cfg}, false)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected to get error; got nil")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), tc.expErr) {
|
|
|
|
t.Fatalf("expected err to contain %q; got %q", tc.expErr, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 13:27:22 +00:00
|
|
|
func loadCfg(t *testing.T, path []string, validateAnnotations, validateExpressions bool) []config.Group {
|
|
|
|
t.Helper()
|
2022-07-22 11:50:41 +00:00
|
|
|
var validateTplFn config.ValidateTplFn
|
|
|
|
if validateAnnotations {
|
|
|
|
validateTplFn = notifier.ValidateTemplates
|
|
|
|
}
|
|
|
|
cfg, err := config.Parse(path, validateTplFn, validateExpressions)
|
2021-05-25 13:27:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return cfg
|
|
|
|
}
|
2023-07-18 13:06:19 +00:00
|
|
|
|
|
|
|
func TestUrlValuesToStrings(t *testing.T) {
|
|
|
|
mapQueryParams := map[string][]string{
|
|
|
|
"param1": {"param1"},
|
|
|
|
"param2": {"anotherparam"},
|
|
|
|
}
|
|
|
|
expectedRes := []string{"param1=param1", "param2=anotherparam"}
|
|
|
|
res := urlValuesToStrings(mapQueryParams)
|
|
|
|
|
|
|
|
if len(res) != len(expectedRes) {
|
|
|
|
t.Errorf("Expected length %d, but got %d", len(expectedRes), len(res))
|
|
|
|
}
|
|
|
|
for ind, val := range expectedRes {
|
|
|
|
if val != res[ind] {
|
|
|
|
t.Errorf("Expected %v; but got %v", val, res[ind])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|