Dependencies: Bump github.com/openfga/openfga from v1.8.6 to v1.8.12 (#105193)

* Dependencies: Bump github.com/openfga/openfga from v1.8.6 to v1.8.12

* Linter: Replace x/exp/rand with math/rand/v2

* NGAlert: Fix test after linter fixes
This commit is contained in:
Matheus Macabu
2025-05-14 11:09:00 +03:00
committed by GitHub
parent ed4680495a
commit 4b426238bd
51 changed files with 489 additions and 515 deletions
@@ -2,6 +2,7 @@ package api
import (
"fmt"
"math/rand/v2"
"path"
"strconv"
"testing"
@@ -10,7 +11,6 @@ import (
"github.com/google/uuid"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder"
@@ -37,10 +37,10 @@ var allExecError = []apimodels.ExecutionErrorState{
func config(t *testing.T) *setting.UnifiedAlertingSettings {
t.Helper()
baseInterval := time.Duration(rand.Intn(99)+1) * time.Second
baseInterval := time.Duration(rand.IntN(99)+1) * time.Second
result := &setting.UnifiedAlertingSettings{
BaseInterval: baseInterval,
DefaultRuleEvaluationInterval: baseInterval * time.Duration(rand.Intn(9)+1),
DefaultRuleEvaluationInterval: baseInterval * time.Duration(rand.IntN(9)+1),
}
t.Logf("Config Base interval is [%v]", result.BaseInterval)
return result
@@ -57,8 +57,8 @@ func allowRecording(lim RuleLimits) *RuleLimits {
}
func validRule() apimodels.PostableExtendedRuleNode {
forDuration := model.Duration(rand.Int63n(1000))
keepFiringForDuration := model.Duration(rand.Int63n(1000))
forDuration := model.Duration(rand.Int64N(1000))
keepFiringForDuration := model.Duration(rand.Int64N(1000))
uid := util.GenerateShortUID()
return apimodels.PostableExtendedRuleNode{
ApiRuleNode: &apimodels.ApiRuleNode{
@@ -87,8 +87,8 @@ func validRule() apimodels.PostableExtendedRuleNode {
},
},
UID: uid,
NoDataState: allNoData[rand.Intn(len(allNoData))],
ExecErrState: allExecError[rand.Intn(len(allExecError))],
NoDataState: allNoData[rand.IntN(len(allNoData))],
ExecErrState: allExecError[rand.IntN(len(allExecError))],
},
}
}
@@ -96,7 +96,7 @@ func validRule() apimodels.PostableExtendedRuleNode {
func validGroup(cfg *setting.UnifiedAlertingSettings, rules ...apimodels.PostableExtendedRuleNode) apimodels.PostableRuleGroupConfig {
return apimodels.PostableRuleGroupConfig{
Name: "TEST-ALERTS-" + util.GenerateShortUID(),
Interval: model.Duration(cfg.BaseInterval * time.Duration(rand.Int63n(10))),
Interval: model.Duration(cfg.BaseInterval * time.Duration(rand.Int64N(10))),
Rules: rules,
}
}
@@ -203,10 +203,10 @@ func TestValidateCondition(t *testing.T) {
}
func TestValidateRuleGroup(t *testing.T) {
orgId := rand.Int63()
orgId := rand.Int64()
folder := randFolder()
rules := make([]apimodels.PostableExtendedRuleNode, 0, rand.Intn(4)+1)
rules := make([]apimodels.PostableExtendedRuleNode, 0, rand.IntN(4)+1)
for i := 0; i < cap(rules); i++ {
rules = append(rules, validRule())
}
@@ -247,7 +247,7 @@ func TestValidateRuleGroup(t *testing.T) {
}
func TestValidateRuleGroupFailures(t *testing.T) {
orgId := rand.Int63()
orgId := rand.Int64()
folder := randFolder()
cfg := config(t)
limits := makeLimits(cfg)
@@ -279,7 +279,7 @@ func TestValidateRuleGroupFailures(t *testing.T) {
name: "fail if interval is negative",
group: func() *apimodels.PostableRuleGroupConfig {
g := validGroup(cfg)
g.Interval = model.Duration(-(rand.Int63n(1000) + 1))
g.Interval = model.Duration(-(rand.Int64N(1000) + 1))
return &g
},
},
@@ -287,7 +287,7 @@ func TestValidateRuleGroupFailures(t *testing.T) {
name: "fail if interval is not aligned with base interval",
group: func() *apimodels.PostableRuleGroupConfig {
g := validGroup(cfg)
g.Interval = model.Duration(cfg.BaseInterval + time.Duration(rand.Intn(10)+1)*time.Second)
g.Interval = model.Duration(cfg.BaseInterval + time.Duration(rand.IntN(10)+1)*time.Second)
return &g
},
},
@@ -351,12 +351,12 @@ func TestValidateRuleGroupFailures(t *testing.T) {
}
func TestValidateRuleNode_NoUID(t *testing.T) {
orgId := rand.Int63()
orgId := rand.Int64()
folder := randFolder()
name := util.GenerateShortUID()
var cfg = config(t)
limits := makeLimits(cfg)
interval := cfg.BaseInterval * time.Duration(rand.Int63n(10)+1)
interval := cfg.BaseInterval * time.Duration(rand.Int64N(10)+1)
testCases := []struct {
name string
@@ -537,7 +537,7 @@ func TestValidateRuleNode_NoUID(t *testing.T) {
}
func TestValidateRuleNodeFailures_NoUID(t *testing.T) {
orgId := rand.Int63()
orgId := rand.Int64()
folder := randFolder()
cfg := config(t)
limits := makeLimits(cfg)
@@ -772,12 +772,12 @@ func TestValidateRuleNodeFailures_NoUID(t *testing.T) {
}
func TestValidateRuleNode_UID(t *testing.T) {
orgId := rand.Int63()
orgId := rand.Int64()
folder := randFolder()
name := util.GenerateShortUID()
var cfg = config(t)
limits := makeLimits(cfg)
interval := cfg.BaseInterval * time.Duration(rand.Int63n(10)+1)
interval := cfg.BaseInterval * time.Duration(rand.Int64N(10)+1)
testCases := []struct {
name string
@@ -823,7 +823,7 @@ func TestValidateRuleNode_UID(t *testing.T) {
r := validRule()
r.GrafanaManagedAlert.Condition = ""
r.GrafanaManagedAlert.Data = nil
if rand.Int63()%2 == 0 {
if rand.Int64()%2 == 0 {
r.GrafanaManagedAlert.Data = make([]apimodels.AlertQuery, 0)
}
return &r
@@ -870,7 +870,7 @@ func TestValidateRuleNode_UID(t *testing.T) {
}
func TestValidateRuleNodeFailures_UID(t *testing.T) {
orgId := rand.Int63()
orgId := rand.Int64()
folder := randFolder()
cfg := config(t)
limits := makeLimits(cfg)
@@ -978,7 +978,7 @@ func TestValidateRuleNodeIntervalFailures(t *testing.T) {
}{
{
name: "fail if interval is negative",
interval: -time.Duration(rand.Int63n(10)+1) * time.Second,
interval: -time.Duration(rand.Int64N(10)+1) * time.Second,
},
{
name: "fail if interval is 0",
@@ -986,14 +986,14 @@ func TestValidateRuleNodeIntervalFailures(t *testing.T) {
},
{
name: "fail if interval is not multiple of base interval",
interval: cfg.BaseInterval + time.Duration(rand.Int63n(int64(cfg.BaseInterval.Seconds())-2)+1)*time.Second,
interval: cfg.BaseInterval + time.Duration(rand.Int64N(int64(cfg.BaseInterval.Seconds())-2)+1)*time.Second,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
r := validRule()
_, err := ValidateRuleNode(&r, util.GenerateShortUID(), testCase.interval, rand.Int63(), randFolder().UID, limits)
_, err := ValidateRuleNode(&r, util.GenerateShortUID(), testCase.interval, rand.Int64(), randFolder().UID, limits)
require.Error(t, err)
})
}
@@ -1084,7 +1084,7 @@ func TestValidateRuleNodeNotificationSettings(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
r := validRule()
r.GrafanaManagedAlert.NotificationSettings = AlertRuleNotificationSettingsFromNotificationSettings([]models.NotificationSettings{tt.notificationSettings})
_, err := ValidateRuleNode(&r, util.GenerateShortUID(), cfg.BaseInterval*time.Duration(rand.Int63n(10)+1), rand.Int63(), randFolder().UID, limits)
_, err := ValidateRuleNode(&r, util.GenerateShortUID(), cfg.BaseInterval*time.Duration(rand.Int64N(10)+1), rand.Int64(), randFolder().UID, limits)
if tt.expErrorContains != "" {
require.Error(t, err)
@@ -1119,7 +1119,7 @@ func TestValidateRuleNodeEditorSettings(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
r := validRule()
r.GrafanaManagedAlert.Metadata = AlertRuleMetadataFromModelMetadata(models.AlertRuleMetadata{EditorSettings: tt.editorSettings})
newRule, err := ValidateRuleNode(&r, util.GenerateShortUID(), cfg.BaseInterval*time.Duration(rand.Int63n(10)+1), rand.Int63(), randFolder().UID, limits)
newRule, err := ValidateRuleNode(&r, util.GenerateShortUID(), cfg.BaseInterval*time.Duration(rand.Int64N(10)+1), rand.Int64(), randFolder().UID, limits)
require.NoError(t, err)
require.Equal(t, tt.editorSettings, newRule.Metadata.EditorSettings)
})
@@ -1136,7 +1136,7 @@ func TestValidateRuleNodeReservedLabels(t *testing.T) {
r.Labels = map[string]string{
label: "true",
}
_, err := ValidateRuleNode(&r, util.GenerateShortUID(), cfg.BaseInterval*time.Duration(rand.Int63n(10)+1), rand.Int63(), randFolder().UID, limits)
_, err := ValidateRuleNode(&r, util.GenerateShortUID(), cfg.BaseInterval*time.Duration(rand.Int64N(10)+1), rand.Int64(), randFolder().UID, limits)
require.Error(t, err)
require.ErrorContains(t, err, label)
})
@@ -3,11 +3,11 @@ package provisioning
import (
"context"
"errors"
"math/rand/v2"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/services/accesscontrol"
+15 -15
View File
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"maps"
"math/rand/v2"
"slices"
"strings"
"testing"
@@ -13,8 +15,6 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
"golang.org/x/exp/rand"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
@@ -43,7 +43,7 @@ func TestIntegrationUpdateAlertRules(t *testing.T) {
t.Skip("skipping integration test")
}
cfg := setting.NewCfg()
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{BaseInterval: time.Duration(rand.Int63n(100)+1) * time.Second}
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{BaseInterval: time.Duration(rand.Int64N(100)+1) * time.Second}
sqlStore := db.InitTestDB(t)
logger := &logtest.Fake{}
folderService := setupFolderService(t, sqlStore, cfg, featuremgmt.WithFeatures())
@@ -238,7 +238,7 @@ func TestIntegration_GetAlertRulesForScheduling(t *testing.T) {
cfg := setting.NewCfg()
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{
BaseInterval: time.Duration(rand.Int63n(100)) * time.Second,
BaseInterval: time.Duration(rand.Int64N(100)) * time.Second,
}
sqlStore := db.InitTestDB(t)
@@ -535,7 +535,7 @@ func TestIntegration_DeleteAlertRulesByUID(t *testing.T) {
})
t.Run("should remove all version and insert one with empty rule_uid when DeletedRuleRetention is set", func(t *testing.T) {
orgID := int64(rand.Intn(1000))
orgID := int64(rand.IntN(1000))
gen = gen.With(gen.WithOrgID(orgID))
// Create a new store to pass the custom bus to check the signal
b := &fakeBus{}
@@ -602,7 +602,7 @@ func TestIntegration_DeleteAlertRulesByUID(t *testing.T) {
})
t.Run("should remove all versions and not keep history if DeletedRuleRetention = 0", func(t *testing.T) {
orgID := int64(rand.Intn(1000))
orgID := int64(rand.IntN(1000))
gen = gen.With(gen.WithOrgID(orgID))
// Create a new store to pass the custom bus to check the signal
b := &fakeBus{}
@@ -655,7 +655,7 @@ func TestIntegration_DeleteAlertRulesByUID(t *testing.T) {
})
t.Run("should remove all versions and not keep history if permanently is true", func(t *testing.T) {
orgID := int64(rand.Intn(1000))
orgID := int64(rand.IntN(1000))
gen = gen.With(gen.WithOrgID(orgID))
// Create a new store to pass the custom bus to check the signal
b := &fakeBus{}
@@ -1285,7 +1285,7 @@ func TestIntegrationListNotificationSettings(t *testing.T) {
TimeIntervalName: timeInterval,
})
require.NoError(t, err)
require.EqualValuesf(t, expected, maps.Keys(actual), "got more rules than expected: %#v", actual)
require.EqualValuesf(t, expected, slices.Collect(maps.Keys(actual)), "got more rules than expected: %#v", actual)
})
}
@@ -1374,7 +1374,7 @@ func TestIntegrationRuleGroupsCaseSensitive(t *testing.T) {
t.Run("GetAlertRulesGroupByRuleUID", func(t *testing.T) {
t.Run("should return rules that belong to only that group", func(t *testing.T) {
result, err := store.GetAlertRulesGroupByRuleUID(context.Background(), &models.GetAlertRulesGroupByRuleUIDQuery{
UID: group1[rand.Intn(len(group1))].UID,
UID: group1[rand.IntN(len(group1))].UID,
OrgID: groupKey1.OrgID,
})
require.NoError(t, err)
@@ -1452,7 +1452,7 @@ func TestIncreaseVersionForAllRulesInNamespaces(t *testing.T) {
t.Skip("skipping integration test")
}
cfg := setting.NewCfg()
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{BaseInterval: time.Duration(rand.Int63n(100)+1) * time.Second}
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{BaseInterval: time.Duration(rand.Int64N(100)+1) * time.Second}
sqlStore := db.InitTestDB(t)
folderService := setupFolderService(t, sqlStore, cfg, featuremgmt.WithFeatures())
b := &fakeBus{}
@@ -1501,7 +1501,7 @@ func TestGetRuleVersions(t *testing.T) {
t.Skip("skipping integration test")
}
cfg := setting.NewCfg()
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{BaseInterval: time.Duration(rand.Int63n(100)+1) * time.Second}
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{BaseInterval: time.Duration(rand.Int64N(100)+1) * time.Second}
sqlStore := db.InitTestDB(t)
folderService := setupFolderService(t, sqlStore, cfg, featuremgmt.WithFeatures())
b := &fakeBus{}
@@ -1534,7 +1534,7 @@ func TestGetRuleVersions(t *testing.T) {
})
t.Run("should not remove versions without diff", func(t *testing.T) {
for i := 0; i < rand.Intn(2)+1; i++ {
for i := 0; i < rand.IntN(2)+1; i++ {
r, err := store.GetAlertRuleByUID(context.Background(), &models.GetAlertRuleByUIDQuery{UID: ruleV2.UID})
require.NoError(t, err)
rn := models.CopyRule(r)
@@ -1633,7 +1633,7 @@ func TestIntegration_AlertRuleVersionsCleanup(t *testing.T) {
}
usr := models.UserUID("test")
cfg := setting.UnifiedAlertingSettings{
BaseInterval: time.Duration(rand.Int63n(100)+1) * time.Second,
BaseInterval: time.Duration(rand.Int64N(100)+1) * time.Second,
}
sqlStore := db.InitTestDB(t)
folderService := setupFolderService(t, sqlStore, setting.NewCfg(), featuremgmt.WithFeatures())
@@ -1733,7 +1733,7 @@ func TestIntegration_ListAlertRules(t *testing.T) {
sqlStore := db.InitTestDB(t)
cfg := setting.NewCfg()
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{
BaseInterval: time.Duration(rand.Int63n(100)) * time.Second,
BaseInterval: time.Duration(rand.Int64N(100)) * time.Second,
}
folderService := setupFolderService(t, sqlStore, cfg, featuremgmt.WithFeatures())
b := &fakeBus{}
@@ -1899,7 +1899,7 @@ func TestIntegration_CleanUpDeletedAlertRules(t *testing.T) {
store.FeatureToggles = featuremgmt.WithFeatures(featuremgmt.FlagAlertRuleRestore)
gen := models.RuleGen
orgID := int64(rand.Intn(1000))
orgID := int64(rand.IntN(1000))
gen = gen.With(gen.WithOrgID(orgID))
+7 -7
View File
@@ -4,13 +4,13 @@ import (
"context"
"errors"
"fmt"
"math/rand/v2"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/ngalert/models"
@@ -20,7 +20,7 @@ import (
)
func TestCalculateChanges(t *testing.T) {
orgId := int64(rand.Int31())
orgId := int64(rand.Int32())
gen := models.RuleGen
t.Run("detects alerts that need to be added", func(t *testing.T) {
@@ -117,8 +117,8 @@ func TestCalculateChanges(t *testing.T) {
r := models.CopyRule(rule)
// Ignore difference in the following fields as submitted models do not have them set
r.ID = int64(rand.Int31())
r.Version = int64(rand.Int31())
r.ID = int64(rand.Int32())
r.Version = int64(rand.Int32())
r.Updated = r.Updated.Add(1 * time.Minute)
submitted = append(submitted, &models.AlertRuleWithOptionals{AlertRule: *r})
@@ -301,7 +301,7 @@ func TestCalculateChanges(t *testing.T) {
}
func TestCalculateAutomaticChanges(t *testing.T) {
orgID := rand.Int63()
orgID := rand.Int64()
gen := models.RuleGen
t.Run("should mark all rules in affected groups", func(t *testing.T) {
@@ -361,7 +361,7 @@ func TestCalculateAutomaticChanges(t *testing.T) {
group2 := models.GenerateGroupKey(orgID)
rules2 := gen.With(gen.WithGroupKey(group2), gen.WithSequentialGroupIndex()).GenerateManyRef(4)
movedIndex := rand.Intn(len(rules2))
movedIndex := rand.IntN(len(rules2))
movedRule := rules2[movedIndex]
copyRule := models.CopyRule(movedRule)
copyRule.RuleGroup = group.RuleGroup
@@ -437,7 +437,7 @@ func TestCalculateAutomaticChanges(t *testing.T) {
}
func TestCalculateRuleGroupsDelete(t *testing.T) {
orgId := int64(rand.Int31())
orgId := int64(rand.Int32())
gen := models.RuleGen
t.Run("returns ErrAlertRuleGroupNotFound when namespace has no rules", func(t *testing.T) {