2020-06-01 10:46:37 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-05-08 11:31:54 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-06-01 10:46:37 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-06-15 19:15:47 +00:00
|
|
|
"time"
|
2020-06-01 10:46:37 +00:00
|
|
|
|
2021-12-02 12:45:08 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
2022-07-22 11:50:41 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2022-05-14 09:38:44 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/templates"
|
2022-02-11 14:17:00 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseGood(t *testing.T) {
|
2022-07-22 11:50:41 +00:00
|
|
|
if _, err := Parse([]string{"testdata/rules/*good.rules", "testdata/dir/*good.*"}, notifier.ValidateTemplates, true); err != nil {
|
2020-06-01 10:46:37 +00:00
|
|
|
t.Errorf("error parsing files %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 11:31:54 +00:00
|
|
|
func TestParseFromURL(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/bad", func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Write([]byte("foo bar"))
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/good-alert", func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Write([]byte(`
|
|
|
|
groups:
|
|
|
|
- name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: Conns
|
|
|
|
expr: vm_tcplistener_conns > 0`))
|
|
|
|
})
|
|
|
|
mux.HandleFunc("/good-rr", func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Write([]byte(`
|
|
|
|
groups:
|
|
|
|
- name: TestGroup
|
|
|
|
rules:
|
|
|
|
- record: conns
|
|
|
|
expr: max(vm_tcplistener_conns)`))
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
if _, err := Parse([]string{srv.URL + "/good-alert", srv.URL + "/good-rr"}, notifier.ValidateTemplates, true); err != nil {
|
|
|
|
t.Errorf("error parsing URLs %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := Parse([]string{srv.URL + "/bad"}, notifier.ValidateTemplates, true); err == nil {
|
|
|
|
t.Errorf("expected parsing error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:46:37 +00:00
|
|
|
func TestParseBad(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
path []string
|
|
|
|
expErr string
|
|
|
|
}{
|
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
[]string{"testdata/rules/rules0-bad.rules"},
|
2020-06-01 10:46:37 +00:00
|
|
|
"unexpected token",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"testdata/dir/rules0-bad.rules"},
|
|
|
|
"error parsing annotation",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"testdata/dir/rules1-bad.rules"},
|
|
|
|
"duplicate in file",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"testdata/dir/rules2-bad.rules"},
|
2020-12-14 18:11:45 +00:00
|
|
|
"function \"unknown\" not defined",
|
2020-06-01 10:46:37 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"testdata/dir/rules3-bad.rules"},
|
|
|
|
"either `record` or `alert` must be set",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"testdata/dir/rules4-bad.rules"},
|
|
|
|
"either `record` or `alert` must be set",
|
|
|
|
},
|
2021-02-01 13:02:44 +00:00
|
|
|
{
|
2022-05-14 09:38:44 +00:00
|
|
|
[]string{"testdata/rules/rules1-bad.rules"},
|
2021-02-01 13:02:44 +00:00
|
|
|
"bad graphite expr",
|
|
|
|
},
|
2022-07-21 13:59:55 +00:00
|
|
|
{
|
|
|
|
[]string{"testdata/dir/rules6-bad.rules"},
|
|
|
|
"missing ':' in header",
|
|
|
|
},
|
2023-05-08 07:52:57 +00:00
|
|
|
{
|
|
|
|
[]string{"http://unreachable-url"},
|
2023-05-10 12:38:13 +00:00
|
|
|
"failed to read",
|
2023-05-08 07:52:57 +00:00
|
|
|
},
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2022-07-22 11:50:41 +00:00
|
|
|
_, err := Parse(tc.path, notifier.ValidateTemplates, true)
|
2020-06-01 10:46:37 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected to get error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), tc.expErr) {
|
|
|
|
t.Errorf("expected err to contain %q; got %q instead", tc.expErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRule_Validate(t *testing.T) {
|
|
|
|
if err := (&Rule{}).Validate(); err == nil {
|
2020-07-02 15:05:36 +00:00
|
|
|
t.Errorf("expected empty name error")
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
|
|
|
if err := (&Rule{Alert: "alert"}).Validate(); err == nil {
|
2020-07-02 15:05:36 +00:00
|
|
|
t.Errorf("expected empty expr error")
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
|
|
|
if err := (&Rule{Alert: "alert", Expr: "test>0"}).Validate(); err != nil {
|
2020-07-02 15:05:36 +00:00
|
|
|
t.Errorf("expected valid rule; got %s", err)
|
2020-06-06 20:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroup_Validate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
group *Group
|
|
|
|
rules []Rule
|
|
|
|
validateAnnotations bool
|
|
|
|
validateExpressions bool
|
|
|
|
expErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
group: &Group{},
|
|
|
|
expErr: "group name must be set",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-06 20:27:09 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Record: "record",
|
|
|
|
Expr: "up | 0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-06 20:27:09 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Record: "record",
|
|
|
|
Expr: "up | 0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "invalid expression",
|
|
|
|
validateExpressions: true,
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-06 20:27:09 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Alert: "alert",
|
|
|
|
Expr: "up == 1",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-06 20:27:09 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Alert: "alert",
|
|
|
|
Expr: "up == 1",
|
|
|
|
Labels: map[string]string{
|
2020-12-14 18:11:45 +00:00
|
|
|
"summary": `
|
|
|
|
{{ with printf "node_memory_MemTotal{job='node',instance='%s'}" "localhost" | query }}
|
|
|
|
{{ . | first | value | humanize1024 }}B
|
|
|
|
{{ end }}`,
|
2020-06-06 20:27:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateAnnotations: true,
|
|
|
|
},
|
2020-06-15 19:15:47 +00:00
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-15 19:15:47 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Alert: "alert",
|
|
|
|
Expr: "up == 1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Alert: "alert",
|
|
|
|
Expr: "up == 1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "duplicate",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-15 19:15:47 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "duplicate",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-15 19:15:47 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{Record: "record", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
{Record: "record", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "duplicate",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-15 19:15:47 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"description": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test",
|
2020-06-15 19:15:47 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{Record: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"summary": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "",
|
|
|
|
},
|
2021-02-01 13:02:44 +00:00
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test thanos",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: NewRawType("thanos"),
|
2021-02-01 13:02:44 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"description": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateExpressions: true,
|
|
|
|
expErr: "unknown datasource type",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test graphite",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: NewGraphiteType(),
|
2021-02-01 13:02:44 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"description": "some-description",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateExpressions: true,
|
|
|
|
expErr: "",
|
|
|
|
},
|
|
|
|
{
|
2023-05-08 07:52:57 +00:00
|
|
|
group: &Group{
|
|
|
|
Name: "test prometheus",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: NewPrometheusType(),
|
2021-02-01 13:02:44 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"description": "{{ value|query }}",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
validateExpressions: true,
|
|
|
|
expErr: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: &Group{
|
|
|
|
Name: "test graphite inherit",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: NewGraphiteType(),
|
2021-02-01 13:02:44 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Expr: "sumSeries(time('foo.bar',10))",
|
2022-02-11 14:17:00 +00:00
|
|
|
For: promutils.NewDuration(10 * time.Millisecond),
|
2021-02-01 13:02:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Expr: "sum(up == 0 ) by (host)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
group: &Group{
|
|
|
|
Name: "test graphite prometheus bad expr",
|
2022-07-22 08:44:55 +00:00
|
|
|
Type: NewGraphiteType(),
|
2021-02-01 13:02:44 +00:00
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Expr: "sum(up == 0 ) by (host)",
|
2022-02-11 14:17:00 +00:00
|
|
|
For: promutils.NewDuration(10 * time.Millisecond),
|
2021-02-01 13:02:44 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Expr: "sumSeries(time('foo.bar',10))",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expErr: "invalid rule",
|
|
|
|
},
|
2020-06-06 20:27:09 +00:00
|
|
|
}
|
2022-07-22 11:50:41 +00:00
|
|
|
|
2020-06-06 20:27:09 +00:00
|
|
|
for _, tc := range testCases {
|
2022-07-22 11:50:41 +00:00
|
|
|
var validateTplFn ValidateTplFn
|
|
|
|
if tc.validateAnnotations {
|
|
|
|
validateTplFn = notifier.ValidateTemplates
|
|
|
|
}
|
|
|
|
err := tc.group.Validate(validateTplFn, tc.validateExpressions)
|
2020-06-06 20:27:09 +00:00
|
|
|
if err == nil {
|
|
|
|
if tc.expErr != "" {
|
|
|
|
t.Errorf("expected to get err %q; got nil insted", tc.expErr)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), tc.expErr) {
|
|
|
|
t.Errorf("expected err to contain %q; got %q instead", tc.expErr, err)
|
|
|
|
}
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 19:15:47 +00:00
|
|
|
|
|
|
|
func TestHashRule(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
a, b Rule
|
|
|
|
equal bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Rule{Record: "record", Expr: "up == 1"},
|
|
|
|
Rule{Record: "record", Expr: "up == 1"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1"},
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
}},
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
}},
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"baz": "foo",
|
|
|
|
"foo": "bar",
|
|
|
|
}},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "record", Expr: "up == 1"},
|
|
|
|
Rule{Alert: "record", Expr: "up == 1"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2022-02-11 14:17:00 +00:00
|
|
|
Rule{Alert: "alert", Expr: "up == 1", For: promutils.NewDuration(time.Minute)},
|
2020-06-15 19:15:47 +00:00
|
|
|
Rule{Alert: "alert", Expr: "up == 1"},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "record", Expr: "up == 1"},
|
|
|
|
Rule{Record: "record", Expr: "up == 1"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Record: "record", Expr: "up == 1"},
|
|
|
|
Rule{Record: "record", Expr: "up == 2"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
}},
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"baz": "foo",
|
|
|
|
"foo": "baz",
|
|
|
|
}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
}},
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"baz": "foo",
|
|
|
|
}},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1", Labels: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
}},
|
|
|
|
Rule{Alert: "alert", Expr: "up == 1"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tc := range testCases {
|
|
|
|
aID, bID := HashRule(tc.a), HashRule(tc.b)
|
|
|
|
if tc.equal != (aID == bID) {
|
|
|
|
t.Fatalf("missmatch for rule %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 19:14:30 +00:00
|
|
|
|
|
|
|
func TestGroupChecksum(t *testing.T) {
|
2020-11-29 07:48:42 +00:00
|
|
|
f := func(t *testing.T, data, newData string) {
|
|
|
|
t.Helper()
|
|
|
|
var g Group
|
|
|
|
if err := yaml.Unmarshal([]byte(data), &g); err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal: %s", err)
|
|
|
|
}
|
|
|
|
if g.Checksum == "" {
|
|
|
|
t.Fatalf("expected to get non-empty checksum")
|
|
|
|
}
|
|
|
|
|
|
|
|
var ng Group
|
|
|
|
if err := yaml.Unmarshal([]byte(newData), &ng); err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal: %s", err)
|
|
|
|
}
|
|
|
|
if g.Checksum == ng.Checksum {
|
|
|
|
t.Fatalf("expected to get different checksums")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Run("Ok", func(t *testing.T) {
|
|
|
|
f(t, `
|
2020-09-11 19:14:30 +00:00
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertAlwaysFiring
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
- record: handler:requests:rate5m
|
|
|
|
expr: sum(rate(prometheus_http_requests_total[5m])) by (handler)
|
2020-11-29 07:48:42 +00:00
|
|
|
`, `
|
2020-09-11 19:14:30 +00:00
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- record: handler:requests:rate5m
|
|
|
|
expr: sum(rate(prometheus_http_requests_total[5m])) by (handler)
|
|
|
|
- alert: ExampleAlertAlwaysFiring
|
|
|
|
expr: sum by(job) (up == 1)
|
2020-11-29 07:48:42 +00:00
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
2021-09-23 14:55:59 +00:00
|
|
|
t.Run("`for` change", func(t *testing.T) {
|
2020-11-29 07:48:42 +00:00
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertWithFor
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
for: 5m
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertWithFor
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`)
|
|
|
|
})
|
2021-09-23 14:55:59 +00:00
|
|
|
t.Run("`interval` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
interval: 2s
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertWithFor
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
interval: 4s
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertWithFor
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
t.Run("`concurrency` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
concurrency: 2
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertWithFor
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
concurrency: 16
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertWithFor
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`)
|
|
|
|
})
|
2021-12-02 12:45:08 +00:00
|
|
|
|
|
|
|
t.Run("`params` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
params:
|
|
|
|
nocache: ["1"]
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
params:
|
|
|
|
nocache: ["0"]
|
2022-06-09 06:58:25 +00:00
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("`limit` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
limit: 5
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
limit: 10
|
2022-07-21 13:59:55 +00:00
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("`headers` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
headers:
|
|
|
|
- "TenantID: foo"
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
headers:
|
|
|
|
- "TenantID: bar"
|
2021-12-02 12:45:08 +00:00
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
2022-09-13 13:25:43 +00:00
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
2023-04-27 11:02:21 +00:00
|
|
|
t.Run("`notifier_headers` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
notifier_headers:
|
|
|
|
- "TenantID: foo"
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
notifier_headers:
|
|
|
|
- "TenantID: bar"
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
2022-09-13 13:25:43 +00:00
|
|
|
t.Run("`debug` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
debug: true
|
2022-12-29 11:36:44 +00:00
|
|
|
`)
|
|
|
|
})
|
|
|
|
t.Run("`update_entries_limit` change", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, `
|
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: foo
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
update_entries_limit: 33
|
2021-12-02 12:45:08 +00:00
|
|
|
`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroupParams(t *testing.T) {
|
|
|
|
f := func(t *testing.T, data string, expParams url.Values) {
|
|
|
|
t.Helper()
|
|
|
|
var g Group
|
|
|
|
if err := yaml.Unmarshal([]byte(data), &g); err != nil {
|
|
|
|
t.Fatalf("failed to unmarshal: %s", err)
|
|
|
|
}
|
|
|
|
got, exp := g.Params.Encode(), expParams.Encode()
|
|
|
|
if got != exp {
|
|
|
|
t.Fatalf("expected to have %q; got %q", exp, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("no params", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertAlwaysFiring
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, url.Values{})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("params", func(t *testing.T) {
|
|
|
|
f(t, `
|
|
|
|
name: TestGroup
|
|
|
|
params:
|
|
|
|
nocache: ["1"]
|
|
|
|
denyPartialResponse: ["true"]
|
|
|
|
rules:
|
|
|
|
- alert: ExampleAlertAlwaysFiring
|
|
|
|
expr: sum by(job) (up == 1)
|
|
|
|
`, url.Values{"nocache": {"1"}, "denyPartialResponse": {"true"}})
|
|
|
|
})
|
2020-09-11 19:14:30 +00:00
|
|
|
}
|