Skip to content

Commit 39cf565

Browse files
committed
Add tests for AggregateBy
1 parent 40d392b commit 39cf565

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

pkg/rules/rules_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
"github.com/prometheus/prometheus/pkg/rulefmt"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestAggregateBy(t *testing.T) {
12+
tt := []struct {
13+
name string
14+
rn RuleNamespace
15+
expectedExpr []string
16+
count, modified int
17+
expect error
18+
}{
19+
{
20+
name: "with no rules",
21+
rn: RuleNamespace{},
22+
count: 0, modified: 0, expect: nil,
23+
},
24+
{
25+
name: "no modifcation",
26+
rn: RuleNamespace{
27+
Groups: []rulefmt.RuleGroup{rulefmt.RuleGroup{Name: "WithoutAggregation", Rules: []rulefmt.Rule{
28+
{Alert: "WithoutAggregation", Expr: "up != 1"},
29+
}}},
30+
},
31+
expectedExpr: []string{"up != 1"},
32+
count: 1, modified: 0, expect: nil,
33+
},
34+
{
35+
name: "no change in the query but lints with 'without' in the aggregation",
36+
rn: RuleNamespace{
37+
Groups: []rulefmt.RuleGroup{rulefmt.RuleGroup{Name: "SkipWithout", Rules: []rulefmt.Rule{
38+
{Alert: "SkipWithout", Expr: `
39+
min without(alertmanager) (
40+
rate(prometheus_notifications_errors_total{job="default/prometheus"}[5m])
41+
/
42+
rate(prometheus_notifications_sent_total{job="default/prometheus"}[5m])
43+
)
44+
* 100
45+
> 3
46+
`},
47+
}}},
48+
},
49+
expectedExpr: []string{`min without(alertmanager) (rate(prometheus_notifications_errors_total{job="default/prometheus"}[5m]) / rate(prometheus_notifications_sent_total{job="default/prometheus"}[5m])) * 100 > 3`},
50+
count: 1, modified: 1, expect: nil,
51+
},
52+
{
53+
name: "with an aggregation modification",
54+
rn: RuleNamespace{
55+
Groups: []rulefmt.RuleGroup{rulefmt.RuleGroup{Name: "WithAggregation", Rules: []rulefmt.Rule{
56+
{Alert: "WithAggregation", Expr: `
57+
sum(rate(cortex_prometheus_rule_evaluation_failures_total[1m])) by (namespace, job)
58+
/
59+
sum(rate(cortex_prometheus_rule_evaluations_total[1m])) by (namespace, job)
60+
> 0.01
61+
`},
62+
}}},
63+
},
64+
expectedExpr: []string{"sum by(namespace, job, cluster) (rate(cortex_prometheus_rule_evaluation_failures_total[1m])) / sum by(namespace, job, cluster) (rate(cortex_prometheus_rule_evaluations_total[1m])) > 0.01"},
65+
count: 1, modified: 1, expect: nil,
66+
},
67+
{
68+
name: "with 'count' as the aggregation",
69+
rn: RuleNamespace{
70+
Groups: []rulefmt.RuleGroup{rulefmt.RuleGroup{Name: "CountAggregation", Rules: []rulefmt.Rule{
71+
{Alert: "CountAggregation", Expr: `
72+
count(count by (gitVersion) (label_replace(kubernetes_build_info{job!~"kube-dns|coredns"},"gitVersion","$1","gitVersion","(v[0-9]*.[0-9]*.[0-9]*).*"))) > 1
73+
`},
74+
}}},
75+
},
76+
expectedExpr: []string{`count by(cluster) (count by(gitVersion, cluster) (label_replace(kubernetes_build_info{job!~"kube-dns|coredns"}, "gitVersion", "$1", "gitVersion", "(v[0-9]*.[0-9]*.[0-9]*).*"))) > 1`},
77+
count: 1, modified: 1, expect: nil,
78+
},
79+
}
80+
81+
for _, tc := range tt {
82+
t.Run(tc.name, func(t *testing.T) {
83+
c, m, err := tc.rn.AggregateBy("cluster")
84+
85+
require.Equal(t, tc.expect, err)
86+
assert.Equal(t, tc.count, c)
87+
assert.Equal(t, tc.modified, m)
88+
89+
// Only verify the PromQL expression if it has been modified
90+
for _, g := range tc.rn.Groups {
91+
for i, r := range g.Rules {
92+
require.Equal(t, tc.expectedExpr[i], r.Expr)
93+
}
94+
}
95+
})
96+
}
97+
}

0 commit comments

Comments
 (0)