Alerting: Fix flaky test TestExportRules (#77519)

* 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 <JohnnyQQQQ@users.noreply.github.com>

* Update pkg/services/ngalert/api/api_ruler_export_test.go

---------

Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com>
pull/77529/head
Yuri Tseretyan 2 years ago committed by GitHub
parent e3641d925c
commit 85425b2194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      pkg/services/ngalert/api/api_ruler_export_test.go
  2. 13
      pkg/services/ngalert/models/testing.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,

@ -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

Loading…
Cancel
Save