30 lines
726 B
Go
30 lines
726 B
Go
package management
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func TestLoadConfigReadsEnvironmentOverrides(t *testing.T) {
|
|
t.Setenv("MANAGEMENT_ALLOWED_ORIGINS", "http://127.0.0.1:13000")
|
|
t.Setenv("MANAGEMENT_DB_PATH", "/tmp/config-test.db")
|
|
v := viper.New()
|
|
v.SetEnvPrefix("MANAGEMENT")
|
|
v.AutomaticEnv()
|
|
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
for _, key := range []string{"db_path", "allowed_origins"} {
|
|
if err := v.BindEnv(key); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cfg := LoadConfig(v)
|
|
if !cfg.AllowedOrigins["http://127.0.0.1:13000"] {
|
|
t.Fatalf("allowed origins = %#v", cfg.AllowedOrigins)
|
|
}
|
|
if cfg.DBPath != "/tmp/config-test.db" {
|
|
t.Fatalf("db path = %q", cfg.DBPath)
|
|
}
|
|
}
|