From 52bc568d04da46e448fca62a075f5d92c5f1e47b Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Fri, 26 Jan 2024 19:33:24 +0100 Subject: [PATCH] Add more test cases to TestDependenciesEdgeCases Signed-off-by: Marco Pracucci --- rules/manager_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/rules/manager_test.go b/rules/manager_test.go index 2d1dc6b42..4762ef6f5 100644 --- a/rules/manager_test.go +++ b/rules/manager_test.go @@ -1523,6 +1523,72 @@ func TestDependenciesEdgeCases(t *testing.T) { require.True(t, depMap.isIndependent(rule1)) require.True(t, depMap.isIndependent(rule2)) }) + + t.Run("rule with regexp matcher on metric name", func(t *testing.T) { + expr, err := parser.ParseExpr("sum(requests)") + require.NoError(t, err) + rule1 := NewRecordingRule("first", expr, labels.Labels{}) + + expr, err = parser.ParseExpr(`sum({__name__=~".+"})`) + require.NoError(t, err) + rule2 := NewRecordingRule("second", expr, labels.Labels{}) + + group := NewGroup(GroupOptions{ + Name: "rule_group", + Interval: time.Second, + Rules: []Rule{rule1, rule2}, + Opts: opts, + }) + + depMap := buildDependencyMap(group.rules) + // A rule with regexp matcher on metric name causes the whole group to be indeterminate. + require.False(t, depMap.isIndependent(rule1)) + require.False(t, depMap.isIndependent(rule2)) + }) + + t.Run("rule with not equal matcher on metric name", func(t *testing.T) { + expr, err := parser.ParseExpr("sum(requests)") + require.NoError(t, err) + rule1 := NewRecordingRule("first", expr, labels.Labels{}) + + expr, err = parser.ParseExpr(`sum({__name__!="requests", service="app"})`) + require.NoError(t, err) + rule2 := NewRecordingRule("second", expr, labels.Labels{}) + + group := NewGroup(GroupOptions{ + Name: "rule_group", + Interval: time.Second, + Rules: []Rule{rule1, rule2}, + Opts: opts, + }) + + depMap := buildDependencyMap(group.rules) + // A rule with not equal matcher on metric name causes the whole group to be indeterminate. + require.False(t, depMap.isIndependent(rule1)) + require.False(t, depMap.isIndependent(rule2)) + }) + + t.Run("rule with not regexp matcher on metric name", func(t *testing.T) { + expr, err := parser.ParseExpr("sum(requests)") + require.NoError(t, err) + rule1 := NewRecordingRule("first", expr, labels.Labels{}) + + expr, err = parser.ParseExpr(`sum({__name__!~"requests.+", service="app"})`) + require.NoError(t, err) + rule2 := NewRecordingRule("second", expr, labels.Labels{}) + + group := NewGroup(GroupOptions{ + Name: "rule_group", + Interval: time.Second, + Rules: []Rule{rule1, rule2}, + Opts: opts, + }) + + depMap := buildDependencyMap(group.rules) + // A rule with not regexp matcher on metric name causes the whole group to be indeterminate. + require.False(t, depMap.isIndependent(rule1)) + require.False(t, depMap.isIndependent(rule2)) + }) } func TestNoMetricSelector(t *testing.T) {