From 794937b3d6e5a9a75c71ad57600ebbe40722cf3c Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Fri, 27 Aug 2021 13:44:46 +0800 Subject: [PATCH] promtool: Add testcase for detecting duplicates Introduce a basic test for checking for duplicate rules. Signed-off-by: Holger Hans Peter Freyther --- cmd/promtool/main_test.go | 33 ++++++++++++++++++++++ cmd/promtool/testdata/rules_duplicates.yml | 24 ++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 cmd/promtool/testdata/rules_duplicates.yml diff --git a/cmd/promtool/main_test.go b/cmd/promtool/main_test.go index 4949c467c1..6d8c02af6f 100644 --- a/cmd/promtool/main_test.go +++ b/cmd/promtool/main_test.go @@ -21,6 +21,7 @@ import ( "testing" "time" + "github.com/prometheus/prometheus/pkg/rulefmt" "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) + }) + } +} diff --git a/cmd/promtool/testdata/rules_duplicates.yml b/cmd/promtool/testdata/rules_duplicates.yml new file mode 100644 index 0000000000..757006817b --- /dev/null +++ b/cmd/promtool/testdata/rules_duplicates.yml @@ -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]))