mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 06:32:21 +08:00

* Revert "chore: add replDB to team service (#91799)" This reverts commit c6ae2d7999aa6fc797db39e9d66c6fea70278f83. * Revert "experiment: use read replica for Get and Find Dashboards (#91706)" This reverts commit 54177ca619dbb5ded2dcb158405802d8dbdbc982. * Revert "QuotaService: refactor to use ReplDB for Get queries (#91333)" This reverts commit 299c142f6a6e8c5673cfdea9f87b56ac304f9834. * Revert "refactor replCfg to look more like plugins/plugin config (#91142)" This reverts commit ac0b4bb34d495914cbe8daad85b7c75c31e8070d. * Revert "chore (replstore): fix registration with multiple sql drivers, again (#90990)" This reverts commit daedb358dded00d349d9fac6106aaaa6bf18322e. * Revert "Chore (sqlstore): add validation and testing for repl config (#90683)" This reverts commit af19f039b62d9945377292a8e679ee258fd56b3d. * Revert "ReplStore: Add support for round robin load balancing between multiple read replicas (#90530)" This reverts commit 27b52b1507f5218a7b38046b4d96bc004d949d46. * Revert "DashboardStore: Use ReplDB and get dashboard quotas from the ReadReplica (#90235)" This reverts commit 8a6107cd35f6444c0674ee4230d3d6bcfbbd4a58. * Revert "accesscontrol service read replica (#89963)" This reverts commit 77a4869fcadf13827d76d5767d4de74812d6dd6d. * Revert "Fix: add mapping for the new mysqlRepl driver (#89551)" This reverts commit ab5a079bcc5b0f0a6929f0a3742eb2859d4a3498. * Revert "fix: sql instrumentation dual registration error (#89508)" This reverts commit d988f5c3b064fade6e96511e0024190c22d48e50. * Revert "Experimental Feature Toggle: databaseReadReplica (#89232)" This reverts commit 50244ed4a1435cbf3e3c87d4af34fd7937f7c259.
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package migrator
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
"github.com/grafana/grafana/pkg/tests/testsuite"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testsuite.Run(m)
|
|
}
|
|
|
|
func batchInsertPermissions(cnt int, sqlStore db.DB) error {
|
|
now := time.Now()
|
|
|
|
return batch(cnt, batchSize, func(start, end int) error {
|
|
n := end - start
|
|
permissions := make([]ac.Permission, 0, n)
|
|
for i := start + 1; i < end+1; i++ {
|
|
permissions = append(permissions, ac.Permission{
|
|
RoleID: 1,
|
|
Action: "action",
|
|
Scope: fmt.Sprintf("resource:uid:%v", i),
|
|
Created: now,
|
|
Updated: now,
|
|
})
|
|
}
|
|
return sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
|
|
_, err := sess.Insert(permissions)
|
|
return err
|
|
})
|
|
})
|
|
}
|
|
|
|
// TestIntegrationMigrateScopeSplit tests the scope split migration
|
|
// also tests the scope split truncation logic
|
|
func TestIntegrationMigrateScopeSplitTruncation(t *testing.T) {
|
|
sqlStore := db.InitTestDB(t)
|
|
logger := log.New("accesscontrol.migrator.test")
|
|
|
|
batchSize = 20
|
|
// Populate permissions
|
|
require.NoError(t, batchInsertPermissions(3*batchSize, sqlStore), "could not insert permissions")
|
|
|
|
// Insert a permission with a scope longer than 240 characters
|
|
longScope := strings.Repeat("a", 60) + ":" + strings.Repeat("b", 60) + ":" + strings.Repeat("c", 60)
|
|
permission := ac.Permission{
|
|
RoleID: 1,
|
|
Action: "action",
|
|
Scope: longScope,
|
|
Created: time.Now(),
|
|
Updated: time.Now(),
|
|
}
|
|
require.NoError(t, sqlStore.WithDbSession(context.Background(), func(sess *db.Session) error {
|
|
_, err := sess.Insert(permission)
|
|
return err
|
|
}), "could not insert permission with long scope")
|
|
|
|
// Migrate
|
|
require.NoError(t, MigrateScopeSplit(sqlStore, logger))
|
|
|
|
// Check migration result
|
|
permissions := make([]ac.Permission, 0, 3*batchSize+1)
|
|
errFind := sqlStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
|
return sess.Find(&permissions)
|
|
})
|
|
require.NoError(t, errFind, "could not find permissions in store")
|
|
|
|
for i := range permissions {
|
|
if permissions[i].Scope == longScope {
|
|
assert.Equal(t, strings.Repeat("a", 40), permissions[i].Kind)
|
|
assert.Equal(t, strings.Repeat("b", 40), permissions[i].Attribute)
|
|
assert.Equal(t, strings.Repeat("c", 40), permissions[i].Identifier)
|
|
}
|
|
}
|
|
}
|