mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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>
This commit is contained in:
parent
e3641d925c
commit
85425b2194
@ -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…
Reference in New Issue
Block a user