Files
seaweedfs/weed/admin/dash/config_toml_test.go
T
Chris Lu 37962e2445 admin: configure maintenance tasks via admin.toml (#9926)
* admin: configure maintenance tasks via admin.toml

Maintenance task settings could only be edited in the admin UI and live
under <dataDir>/conf, so they silently reverted to defaults whenever the
data directory was recreated. An optional admin.toml now declares vacuum,
balance, and erasure coding settings; keys set there are written through
to the persisted task configs at every startup, overriding UI edits, so
the configuration stays declarative. Generate an example with
"weed scaffold -config=admin".

* vacuum: round min volume age up to whole hours

MinVolumeAgeSeconds was truncated by integer division when converted to
the hour-granular protobuf field, so a sub-hour setting silently became
0 and disabled the age guard.

* admin: split and normalize preferred_tags from admin.toml

A comma-separated string, as set via environment variable, came through
viper as a single slice element. Split on commas and reuse
util.NormalizeTagList, matching the plugin config path.

* scaffold: clarify admin.toml wording
2026-06-11 11:04:52 -07:00

110 lines
3.2 KiB
Go

package dash
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/seaweedfs/seaweedfs/weed/worker/tasks/balance"
"github.com/seaweedfs/seaweedfs/weed/worker/tasks/erasure_coding"
"github.com/seaweedfs/seaweedfs/weed/worker/tasks/vacuum"
"github.com/spf13/viper"
)
func tomlConfig(t *testing.T, content string) *viper.Viper {
t.Helper()
v := viper.New()
v.SetConfigType("toml")
if err := v.ReadConfig(strings.NewReader(content)); err != nil {
t.Fatalf("read toml: %v", err)
}
return v
}
func TestApplyMaintenanceConfigFromToml(t *testing.T) {
dir := t.TempDir()
cp := NewConfigPersistence(dir)
// simulate an earlier UI edit
saved := vacuum.NewDefaultConfig()
saved.MaxConcurrent = 5
saved.GarbageThreshold = 0.5
if err := cp.SaveVacuumTaskPolicy(saved.ToTaskPolicy()); err != nil {
t.Fatalf("save vacuum policy: %v", err)
}
balanceBefore := balance.LoadConfigFromPersistence(cp)
v := tomlConfig(t, `
[maintenance.vacuum]
garbage_threshold = 0.03
min_volume_age_seconds = 1800
[maintenance.erasure_coding]
enabled = false
fullness_ratio = 0.8
preferred_tags = "Fast, ssd"
`)
if err := cp.ApplyMaintenanceConfigFromToml(v); err != nil {
t.Fatalf("apply: %v", err)
}
vacuumConf := vacuum.LoadConfigFromPersistence(cp)
if vacuumConf.GarbageThreshold != 0.03 {
t.Errorf("garbage threshold = %v, want 0.03", vacuumConf.GarbageThreshold)
}
// sub-hour values round up to a whole hour instead of truncating to 0
if vacuumConf.MinVolumeAgeSeconds != 3600 {
t.Errorf("min volume age = %v, want 3600", vacuumConf.MinVolumeAgeSeconds)
}
if vacuumConf.MaxConcurrent != 5 {
t.Errorf("max concurrent = %v, want UI-saved 5 preserved", vacuumConf.MaxConcurrent)
}
ecConf := erasure_coding.LoadConfigFromPersistence(cp)
if ecConf.Enabled {
t.Errorf("ec enabled = true, want false")
}
if ecConf.FullnessRatio != 0.8 {
t.Errorf("fullness ratio = %v, want 0.8", ecConf.FullnessRatio)
}
if !reflect.DeepEqual(ecConf.PreferredTags, []string{"fast", "ssd"}) {
t.Errorf("preferred tags = %v, want [fast ssd]", ecConf.PreferredTags)
}
if ecConf.QuietForSeconds != 3600 {
t.Errorf("quiet for = %v, want default 3600 preserved", ecConf.QuietForSeconds)
}
// balance section absent: nothing written
if _, err := os.Stat(filepath.Join(dir, ConfigSubdir, BalanceTaskConfigFile)); !os.IsNotExist(err) {
t.Errorf("balance config written without [maintenance.balance] section")
}
if got := balance.LoadConfigFromPersistence(cp); !reflect.DeepEqual(got, balanceBefore) {
t.Errorf("balance config changed without [maintenance.balance] section")
}
}
func TestApplyMaintenanceConfigFromTomlNoKeys(t *testing.T) {
dir := t.TempDir()
cp := NewConfigPersistence(dir)
if err := cp.ApplyMaintenanceConfigFromToml(viper.New()); err != nil {
t.Fatalf("apply: %v", err)
}
if _, err := os.Stat(filepath.Join(dir, ConfigSubdir)); !os.IsNotExist(err) {
t.Errorf("conf dir created without any maintenance keys set")
}
}
func TestApplyMaintenanceConfigFromTomlRequiresDataDir(t *testing.T) {
cp := NewConfigPersistence("")
v := tomlConfig(t, `
[maintenance.vacuum]
garbage_threshold = 0.03
`)
if err := cp.ApplyMaintenanceConfigFromToml(v); err == nil {
t.Errorf("expected error when maintenance keys are set without a data dir")
}
}