From 85425b219427567d85a22f144073f0fe3d2b398e Mon Sep 17 00:00:00 2001 From: Yuri Tseretyan Date: Wed, 1 Nov 2023 15:35:04 -0400 Subject: [PATCH] Alerting: Fix flaky test TestExportRules (#77519) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix test to correclty mock data store * Update pkg/services/ngalert/api/api_ruler_export_test.go Co-authored-by: Jean-Philippe Quéméner * Update pkg/services/ngalert/api/api_ruler_export_test.go --------- Co-authored-by: Jean-Philippe Quéméner --- pkg/services/ngalert/api/api_ruler_export_test.go | 11 +++++++---- pkg/services/ngalert/models/testing.go | 13 +++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/services/ngalert/api/api_ruler_export_test.go b/pkg/services/ngalert/api/api_ruler_export_test.go index 6cbeb65c893..32d254d2dd2 100644 --- a/pkg/services/ngalert/api/api_ruler_export_test.go +++ b/pkg/services/ngalert/api/api_ruler_export_test.go @@ -202,7 +202,6 @@ func TestExportRules(t *testing.T) { f2 := randFolder() ruleStore := fakes.NewRuleStore(t) - ruleStore.Folders[orgID] = append(ruleStore.Folders[orgID], f1, f2) hasAccessKey1 := ngmodels.AlertRuleGroupKey{ OrgID: orgID, @@ -253,12 +252,16 @@ func TestExportRules(t *testing.T) { )) ruleStore.PutRule(context.Background(), hasAccess2...) - _, noAccess2 := ngmodels.GenerateUniqueAlertRules(10, + _, noAccessByFolder := ngmodels.GenerateUniqueAlertRules(10, ngmodels.AlertRuleGen( ngmodels.WithUniqueUID(&uids), ngmodels.WithQuery(accessQuery), // no access because of folder + ngmodels.WithNamespaceUIDNotIn(f1.UID, f2.UID), )) - ruleStore.PutRule(context.Background(), noAccess2...) + + ruleStore.PutRule(context.Background(), noAccessByFolder...) + // overwrite the folders visible to user because PutRule automatically creates folders in the fake store. + ruleStore.Folders[orgID] = []*folder2.Folder{f1, f2} srv := createService(ruleStore) @@ -351,7 +354,7 @@ func TestExportRules(t *testing.T) { { title: "unauthorized if folders are not accessible", params: url.Values{ - "folderUid": []string{noAccess2[0].NamespaceUID}, + "folderUid": []string{noAccessByFolder[0].NamespaceUID}, }, expectedStatus: 401, expectedRules: nil, diff --git a/pkg/services/ngalert/models/testing.go b/pkg/services/ngalert/models/testing.go index 7f7746adcf0..280dfa30269 100644 --- a/pkg/services/ngalert/models/testing.go +++ b/pkg/services/ngalert/models/testing.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "math/rand" + "slices" "sync" "time" @@ -158,6 +159,18 @@ func WithUniqueOrgID() AlertRuleMutator { } } +// WithNamespaceUIDNotIn generates a random namespace UID if it is among excluded +func WithNamespaceUIDNotIn(exclude ...string) AlertRuleMutator { + return func(rule *AlertRule) { + for { + if !slices.Contains(exclude, rule.NamespaceUID) { + return + } + rule.NamespaceUID = uuid.NewString() + } + } +} + func WithNamespace(namespace *folder.Folder) AlertRuleMutator { return func(rule *AlertRule) { rule.NamespaceUID = namespace.UID