mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
update test environment to create folders (#54254)
This commit is contained in:
parent
c66c275342
commit
db09a76125
@ -3,25 +3,32 @@ package tests
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/routing"
|
"github.com/grafana/grafana/pkg/api/routing"
|
||||||
busmock "github.com/grafana/grafana/pkg/bus/mock"
|
busmock "github.com/grafana/grafana/pkg/bus/mock"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||||
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
acmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
||||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||||
databasestore "github.com/grafana/grafana/pkg/services/dashboards/database"
|
databasestore "github.com/grafana/grafana/pkg/services/dashboards/database"
|
||||||
dashboardservice "github.com/grafana/grafana/pkg/services/dashboards/service"
|
dashboardservice "github.com/grafana/grafana/pkg/services/dashboards/service"
|
||||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||||
|
"github.com/grafana/grafana/pkg/services/guardian"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert"
|
"github.com/grafana/grafana/pkg/services/ngalert"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
|
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||||
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
||||||
|
"github.com/grafana/grafana/pkg/services/org"
|
||||||
"github.com/grafana/grafana/pkg/services/secrets/database"
|
"github.com/grafana/grafana/pkg/services/secrets/database"
|
||||||
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
||||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||||
|
"github.com/grafana/grafana/pkg/services/user"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
|
|
||||||
@ -32,6 +39,15 @@ import (
|
|||||||
// SetupTestEnv initializes a store to used by the tests.
|
// SetupTestEnv initializes a store to used by the tests.
|
||||||
func SetupTestEnv(t *testing.T, baseInterval time.Duration) (*ngalert.AlertNG, *store.DBstore) {
|
func SetupTestEnv(t *testing.T, baseInterval time.Duration) (*ngalert.AlertNG, *store.DBstore) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
origNewGuardian := guardian.New
|
||||||
|
guardian.MockDashboardGuardian(&guardian.FakeDashboardGuardian{
|
||||||
|
CanSaveValue: true,
|
||||||
|
CanViewValue: true,
|
||||||
|
CanAdminValue: true,
|
||||||
|
})
|
||||||
|
t.Cleanup(func() {
|
||||||
|
guardian.New = origNewGuardian
|
||||||
|
})
|
||||||
|
|
||||||
cfg := setting.NewCfg()
|
cfg := setting.NewCfg()
|
||||||
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{
|
cfg.UnifiedAlerting = setting.UnifiedAlertingSettings{
|
||||||
@ -49,6 +65,7 @@ func SetupTestEnv(t *testing.T, baseInterval time.Duration) (*ngalert.AlertNG, *
|
|||||||
ac := acmock.New()
|
ac := acmock.New()
|
||||||
features := featuremgmt.WithFeatures()
|
features := featuremgmt.WithFeatures()
|
||||||
folderPermissions := acmock.NewMockedPermissionsService()
|
folderPermissions := acmock.NewMockedPermissionsService()
|
||||||
|
folderPermissions.On("SetPermissions", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]accesscontrol.ResourcePermission{}, nil)
|
||||||
dashboardPermissions := acmock.NewMockedPermissionsService()
|
dashboardPermissions := acmock.NewMockedPermissionsService()
|
||||||
|
|
||||||
dashboardService := dashboardservice.ProvideDashboardService(
|
dashboardService := dashboardservice.ProvideDashboardService(
|
||||||
@ -74,6 +91,7 @@ func SetupTestEnv(t *testing.T, baseInterval time.Duration) (*ngalert.AlertNG, *
|
|||||||
},
|
},
|
||||||
Logger: log.New("ngalert-test"),
|
Logger: log.New("ngalert-test"),
|
||||||
DashboardService: dashboardService,
|
DashboardService: dashboardService,
|
||||||
|
FolderService: folderService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,7 +102,20 @@ func CreateTestAlertRule(t *testing.T, ctx context.Context, dbstore *store.DBsto
|
|||||||
|
|
||||||
func CreateTestAlertRuleWithLabels(t *testing.T, ctx context.Context, dbstore *store.DBstore, intervalSeconds int64, orgID int64, labels map[string]string) *models.AlertRule {
|
func CreateTestAlertRuleWithLabels(t *testing.T, ctx context.Context, dbstore *store.DBstore, intervalSeconds int64, orgID int64, labels map[string]string) *models.AlertRule {
|
||||||
ruleGroup := fmt.Sprintf("ruleGroup-%s", util.GenerateShortUID())
|
ruleGroup := fmt.Sprintf("ruleGroup-%s", util.GenerateShortUID())
|
||||||
_, err := dbstore.InsertAlertRules(ctx, []models.AlertRule{
|
folderUID := "namespace"
|
||||||
|
user := &user.SignedInUser{
|
||||||
|
UserID: 1,
|
||||||
|
OrgID: orgID,
|
||||||
|
OrgRole: org.RoleAdmin,
|
||||||
|
IsGrafanaAdmin: true,
|
||||||
|
}
|
||||||
|
folder, err := dbstore.FolderService.CreateFolder(ctx, user, orgID, "FOLDER-"+util.GenerateShortUID(), folderUID)
|
||||||
|
if errors.Is(err, dashboards.ErrFolderWithSameUIDExists) || errors.Is(err, dashboards.ErrFolderVersionMismatch) {
|
||||||
|
folder, err = dbstore.FolderService.GetFolderByUID(ctx, user, orgID, folderUID)
|
||||||
|
}
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = dbstore.InsertAlertRules(ctx, []models.AlertRule{
|
||||||
{
|
{
|
||||||
|
|
||||||
ID: 0,
|
ID: 0,
|
||||||
@ -108,7 +139,7 @@ func CreateTestAlertRuleWithLabels(t *testing.T, ctx context.Context, dbstore *s
|
|||||||
Labels: labels,
|
Labels: labels,
|
||||||
Annotations: map[string]string{"testAnnoKey": "testAnnoValue"},
|
Annotations: map[string]string{"testAnnoKey": "testAnnoValue"},
|
||||||
IntervalSeconds: intervalSeconds,
|
IntervalSeconds: intervalSeconds,
|
||||||
NamespaceUID: "namespace",
|
NamespaceUID: folder.Uid,
|
||||||
RuleGroup: ruleGroup,
|
RuleGroup: ruleGroup,
|
||||||
NoDataState: models.NoData,
|
NoDataState: models.NoData,
|
||||||
ExecErrState: models.AlertingErrState,
|
ExecErrState: models.AlertingErrState,
|
||||||
@ -118,7 +149,7 @@ func CreateTestAlertRuleWithLabels(t *testing.T, ctx context.Context, dbstore *s
|
|||||||
|
|
||||||
q := models.ListAlertRulesQuery{
|
q := models.ListAlertRulesQuery{
|
||||||
OrgID: orgID,
|
OrgID: orgID,
|
||||||
NamespaceUIDs: []string{"namespace"},
|
NamespaceUIDs: []string{folder.Uid},
|
||||||
RuleGroup: ruleGroup,
|
RuleGroup: ruleGroup,
|
||||||
}
|
}
|
||||||
err = dbstore.ListAlertRules(ctx, &q)
|
err = dbstore.ListAlertRules(ctx, &q)
|
||||||
@ -126,6 +157,6 @@ func CreateTestAlertRuleWithLabels(t *testing.T, ctx context.Context, dbstore *s
|
|||||||
require.NotEmpty(t, q.Result)
|
require.NotEmpty(t, q.Result)
|
||||||
|
|
||||||
rule := q.Result[0]
|
rule := q.Result[0]
|
||||||
t.Logf("alert definition: %v with title: %q interval: %d created", rule.GetKey(), rule.Title, rule.IntervalSeconds)
|
t.Logf("alert definition: %v with title: %q interval: %d folder: %s created", rule.GetKey(), rule.Title, rule.IntervalSeconds, folder.Uid)
|
||||||
return rule
|
return rule
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user