Files
grafana/pkg/setting/setting_unified_storage_test.go
Mustafa Sencer Özcan 088bab8b38 feat: enable auto migration based on resource count (#115619)
* feat(unified): migration at startup based on resource count

-- draft

* feat: introduce auto migration enablement for dashboards & folders

* feat: enable auto migration based on threshold

* fix: improve

* fix: pass in the auto migrate per migration definition

* fix: minor

* fix: only use one options

* fix: test

* fix: test

* fix: tests

* fix: simplify configs

* chore: rename

* fix: add integration test

* fix: add integration test

* fix: integration tests

* chore: add comments

* fix: address comment

* fix: address comments

* fix: test and auto migration flow

* fix: test

---------

Co-authored-by: Rafael Paulovic <rafael.paulovic@grafana.com>
2026-01-08 13:30:40 +01:00

107 lines
3.4 KiB
Go

package setting
import (
"testing"
"time"
"github.com/grafana/grafana/pkg/apiserver/rest"
"github.com/stretchr/testify/assert"
)
func TestCfg_setUnifiedStorageConfig(t *testing.T) {
t.Run("read unified_storage configs", func(t *testing.T) {
cfg := NewCfg()
err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"})
assert.NoError(t, err)
setSectionKey := func(sectionName, key, value string) {
section := cfg.Raw.Section(sectionName) // Gets existing or creates new
_, err := section.NewKey(key, value)
assert.NoError(t, err)
}
setMigratedResourceKey := func(key, value string) {
for migratedResource := range MigratedUnifiedResources {
setSectionKey("unified_storage."+migratedResource, key, value)
}
}
validateMigratedResources := func(optIn bool) {
for migratedResource, enabled := range MigratedUnifiedResources {
resourceCfg, exists := cfg.UnifiedStorage[migratedResource]
isEnabled := enabled
if optIn {
isEnabled = true
}
if !isEnabled {
if exists {
assert.Equal(t, rest.DualWriterMode(1), resourceCfg.DualWriterMode, migratedResource)
}
continue
}
assert.Equal(t, exists, true, migratedResource)
expectedThreshold := 0
if AutoMigratedUnifiedResources[migratedResource] {
expectedThreshold = DefaultAutoMigrationThreshold
}
assert.Equal(t, UnifiedStorageConfig{
DualWriterMode: 5,
DualWriterMigrationDataSyncDisabled: true,
EnableMigration: isEnabled,
AutoMigrationThreshold: expectedThreshold,
}, resourceCfg, migratedResource)
}
}
setMigratedResourceKey("dualWriterMode", "1") // migrated resources enabled by default will change to 5 in setUnifiedStorageConfig
setSectionKey("unified_storage.resource.not_migrated.grafana.app", "dualWriterMode", "2")
setSectionKey("unified_storage.resource.not_migrated.grafana.app", "dualWriterPeriodicDataSyncJobEnabled", "true")
setSectionKey("unified_storage.resource.not_migrated.grafana.app", "dataSyncerRecordsLimit", "1001")
setSectionKey("unified_storage.resource.not_migrated.grafana.app", "dataSyncerInterval", "10m")
// Add unified_storage section for index settings
setSectionKey("unified_storage", "index_min_count", "5")
cfg.setUnifiedStorageConfig()
value, exists := cfg.UnifiedStorage["resource.not_migrated.grafana.app"]
assert.Equal(t, exists, true)
assert.Equal(t, value, UnifiedStorageConfig{
DualWriterMode: 2,
DualWriterPeriodicDataSyncJobEnabled: true,
DataSyncerRecordsLimit: 1001,
DataSyncerInterval: time.Minute * 10,
AutoMigrationThreshold: 0,
})
validateMigratedResources(false)
setMigratedResourceKey("enableMigration", "true") // will be changed to 5 in setUnifiedStorageConfig
cfg.setUnifiedStorageConfig()
validateMigratedResources(true)
// Test that index settings are correctly parsed
assert.Equal(t, 5, cfg.IndexMinCount)
})
t.Run("read unified_storage configs with defaults", func(t *testing.T) {
cfg := NewCfg()
err := cfg.Load(CommandLineArgs{HomePath: "../../", Config: "../../conf/defaults.ini"})
assert.NoError(t, err)
// Don't add any custom index settings, test defaults
cfg.setUnifiedStorageConfig()
// Test that default index settings are applied
assert.Equal(t, 1, cfg.IndexMinCount)
})
}