promtool: Add testcase for detecting duplicates

Introduce a basic test for checking for duplicate rules.

Signed-off-by: Holger Hans Peter Freyther <holger@moiji-mobile.com>
This commit is contained in:
Holger Hans Peter Freyther 2021-08-27 13:44:46 +08:00
parent dc586b2495
commit 794937b3d6
2 changed files with 57 additions and 0 deletions

View file

@ -21,6 +21,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/prometheus/prometheus/pkg/rulefmt"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -118,3 +119,35 @@ func TestCheckSDFile(t *testing.T) {
}) })
} }
} }
func TestCheckDuplicates(t *testing.T) {
cases := []struct {
name string
ruleFile string
expectedDups []compareRuleType
}{
{
name: "no duplicates",
ruleFile: "./testdata/rules.yml",
},
{
name: "duplicate in other group",
ruleFile: "./testdata/rules_duplicates.yml",
expectedDups: []compareRuleType{
{
metric: "job:test:count_over_time1m",
},
},
},
}
for _, test := range cases {
c := test
t.Run(c.name, func(t *testing.T) {
rgs, err := rulefmt.ParseFile(c.ruleFile)
require.Empty(t, err)
dups := checkDuplicates(rgs.Groups)
require.Equal(t, c.expectedDups, dups)
})
}
}

View file

@ -0,0 +1,24 @@
# This is a rules file with duplicate expressions
groups:
- name: base
rules:
- record: job:test:count_over_time1m
expr: sum without(instance) (count_over_time(test[1m]))
# A recording rule that doesn't depend on input series.
- record: fixed_data
expr: 1
# Subquery with default resolution test.
- record: suquery_interval_test
expr: count_over_time(up[5m:])
# Duplicating
- record: job:test:count_over_time1m
expr: sum without(instance) (count_over_time(test[1m]))
- name: duplicate
rules:
- record: job:test:count_over_time1m
expr: sum without(instance) (count_over_time(test[1m]))