From 54d4b7842b64681170934af7dcfa6504efe277e2 Mon Sep 17 00:00:00 2001 From: Moustafa Baiou Date: Tue, 15 Apr 2025 01:15:29 -0400 Subject: [PATCH] Alerting: Fix flaky tests (#104017) Alerting: fix flaky tests Some test conditions introduced in #103403 are flaky because they rely on random behavior of the generator. Sometimes rules are generated with an updated by (which warrants the lookup of the users). This makes it so those tests which are checking the user lookup always have rules with updated by. --- pkg/services/ngalert/api/api_ruler_test.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/services/ngalert/api/api_ruler_test.go b/pkg/services/ngalert/api/api_ruler_test.go index 8fc7e7212da..42755296f6c 100644 --- a/pkg/services/ngalert/api/api_ruler_test.go +++ b/pkg/services/ngalert/api/api_ruler_test.go @@ -666,8 +666,10 @@ func TestRouteGetRulesConfig(t *testing.T) { group2Key := models.GenerateGroupKey(orgID) group2Key.NamespaceUID = folder2.UID - group1 := gen.With(gen.WithGroupKey(group1Key)).GenerateManyRef(2, 6) - group2 := gen.With(gen.WithGroupKey(group2Key)).GenerateManyRef(2, 6) + ruleUpdatedBy := util.Pointer(models.UserUID(util.GenerateShortUID())) + + group1 := gen.With(gen.WithGroupKey(group1Key), gen.WithUpdatedBy(ruleUpdatedBy)).GenerateManyRef(2, 6) + group2 := gen.With(gen.WithGroupKey(group2Key), gen.WithUpdatedBy(ruleUpdatedBy)).GenerateManyRef(2, 6) ruleStore.PutRule(context.Background(), append(group1, group2...)...) t.Run("and do not return group if user does not have access to one of rules", func(t *testing.T) { @@ -704,7 +706,9 @@ func TestRouteGetRulesConfig(t *testing.T) { groupKey := models.GenerateGroupKey(orgID) groupKey.NamespaceUID = folder.UID - expectedRules := gen.With(gen.WithGroupKey(groupKey), gen.WithUniqueGroupIndex()).GenerateManyRef(5, 10) + ruleUpdatedBy := util.Pointer(models.UserUID(util.GenerateShortUID())) + + expectedRules := gen.With(gen.WithGroupKey(groupKey), gen.WithUniqueGroupIndex(), gen.WithUpdatedBy(ruleUpdatedBy)).GenerateManyRef(5, 10) ruleStore.PutRule(context.Background(), expectedRules...) perms := createPermissionsForRules(expectedRules, orgID) @@ -753,7 +757,9 @@ func TestRouteGetRulesGroupConfig(t *testing.T) { groupKey := models.GenerateGroupKey(orgID) groupKey.NamespaceUID = folder.UID - expectedRules := gen.With(gen.WithGroupKey(groupKey), gen.WithUniqueGroupIndex()).GenerateManyRef(5, 10) + ruleUpdatedBy := util.Pointer(models.UserUID(util.GenerateShortUID())) + + expectedRules := gen.With(gen.WithGroupKey(groupKey), gen.WithUniqueGroupIndex(), gen.WithUpdatedBy(ruleUpdatedBy)).GenerateManyRef(5, 10) ruleStore.PutRule(context.Background(), expectedRules...) perms := createPermissionsForRules(expectedRules, orgID)