Alerting: Refactor Alert Rule Generators (#86813)

This commit is contained in:
Yuri Tseretyan
2024-04-29 21:52:15 -04:00
committed by GitHub
parent 8bb9b06e48
commit 052082a927
27 changed files with 739 additions and 648 deletions
@@ -9,7 +9,6 @@ import (
"path"
"sort"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
@@ -198,7 +197,6 @@ func TestExportFromPayload(t *testing.T) {
}
func TestExportRules(t *testing.T) {
uids := sync.Map{}
orgID := int64(1)
f1 := randFolder()
f2 := randFolder()
@@ -210,33 +208,20 @@ func TestExportRules(t *testing.T) {
NamespaceUID: f1.UID,
RuleGroup: "HAS-ACCESS-1",
}
accessQuery := ngmodels.GenerateAlertQuery()
noAccessQuery := ngmodels.GenerateAlertQuery()
_, hasAccess1 := ngmodels.GenerateUniqueAlertRules(5,
ngmodels.AlertRuleGen(
ngmodels.WithUniqueUID(&uids),
withGroupKey(hasAccessKey1),
ngmodels.WithQuery(accessQuery),
ngmodels.WithUniqueGroupIndex(),
))
gen := ngmodels.RuleGen
accessQuery := gen.GenerateQuery()
noAccessQuery := gen.GenerateQuery()
hasAccess1 := gen.With(gen.WithGroupKey(hasAccessKey1), gen.WithQuery(accessQuery), gen.WithUniqueGroupIndex()).GenerateManyRef(5)
ruleStore.PutRule(context.Background(), hasAccess1...)
noAccessKey1 := ngmodels.AlertRuleGroupKey{
OrgID: orgID,
NamespaceUID: f1.UID,
RuleGroup: "NO-ACCESS",
}
_, noAccess1 := ngmodels.GenerateUniqueAlertRules(5,
ngmodels.AlertRuleGen(
ngmodels.WithUniqueUID(&uids),
withGroupKey(noAccessKey1),
ngmodels.WithQuery(noAccessQuery),
))
noAccessRule := ngmodels.AlertRuleGen(
ngmodels.WithUniqueUID(&uids),
withGroupKey(noAccessKey1),
ngmodels.WithQuery(accessQuery),
)()
noAccess1 := gen.With(gen.WithGroupKey(noAccessKey1), gen.WithQuery(noAccessQuery)).GenerateManyRef(5)
noAccessRule := gen.With(gen.WithGroupKey(noAccessKey1), gen.WithQuery(accessQuery)).GenerateRef()
noAccess1 = append(noAccess1, noAccessRule)
ruleStore.PutRule(context.Background(), noAccess1...)
@@ -245,21 +230,10 @@ func TestExportRules(t *testing.T) {
NamespaceUID: f2.UID,
RuleGroup: "HAS-ACCESS-2",
}
_, hasAccess2 := ngmodels.GenerateUniqueAlertRules(5,
ngmodels.AlertRuleGen(
ngmodels.WithUniqueUID(&uids),
withGroupKey(hasAccessKey2),
ngmodels.WithQuery(accessQuery),
ngmodels.WithUniqueGroupIndex(),
))
hasAccess2 := gen.With(gen.WithGroupKey(hasAccessKey2), gen.WithQuery(accessQuery), gen.WithUniqueGroupIndex()).GenerateManyRef(5)
ruleStore.PutRule(context.Background(), hasAccess2...)
_, noAccessByFolder := ngmodels.GenerateUniqueAlertRules(10,
ngmodels.AlertRuleGen(
ngmodels.WithUniqueUID(&uids),
ngmodels.WithQuery(accessQuery), // no access because of folder
ngmodels.WithNamespaceUIDNotIn(f1.UID, f2.UID),
))
noAccessByFolder := gen.With(gen.WithQuery(accessQuery), gen.WithNamespaceUIDNotIn(f1.UID, f2.UID)).GenerateManyRef(10)
ruleStore.PutRule(context.Background(), noAccessByFolder...)
// overwrite the folders visible to user because PutRule automatically creates folders in the fake store.