101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateRejectsDeprecatedTinodeDatabase(t *testing.T) {
|
|
cfg := defaultConfig()
|
|
cfg.MySQL.DSN = "root:root@tcp(127.0.0.1:3306)/tinode?parseTime=true"
|
|
|
|
err := cfg.validate()
|
|
if err == nil || !strings.Contains(err.Error(), "tinode is deprecated") {
|
|
t.Fatalf("validate() error = %v, want deprecated database error", err)
|
|
}
|
|
}
|
|
|
|
func TestDefaultConfigUsesWorkbenchDatabase(t *testing.T) {
|
|
cfg := defaultConfig()
|
|
if err := cfg.validate(); err != nil {
|
|
t.Fatalf("default config validation failed: %v", err)
|
|
}
|
|
if !strings.Contains(cfg.MySQL.DSN, "/workbench?") {
|
|
t.Fatalf("default mysql dsn = %q, want workbench database", cfg.MySQL.DSN)
|
|
}
|
|
if cfg.MySQL.MaxOpenConns != 15 || cfg.MySQL.MaxIdleConns != 5 {
|
|
t.Fatalf("default mysql pool = %d/%d, want 15/5", cfg.MySQL.MaxOpenConns, cfg.MySQL.MaxIdleConns)
|
|
}
|
|
}
|
|
|
|
func TestApplyEnvOverridesMySQLPool(t *testing.T) {
|
|
t.Setenv("IM_WORKBENCH_MYSQL_MAX_OPEN_CONNS", "12")
|
|
t.Setenv("IM_WORKBENCH_MYSQL_MAX_IDLE_CONNS", "4")
|
|
cfg := defaultConfig()
|
|
|
|
applyEnv(cfg)
|
|
|
|
if cfg.MySQL.MaxOpenConns != 12 || cfg.MySQL.MaxIdleConns != 4 {
|
|
t.Fatalf("mysql pool = %d/%d, want 12/4", cfg.MySQL.MaxOpenConns, cfg.MySQL.MaxIdleConns)
|
|
}
|
|
}
|
|
|
|
func TestLoadFallsBackCoreMySQLToPrimaryDSN(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "workbench.json")
|
|
if err := os.WriteFile(path, []byte(`{"mysql":{"dsn":"root:root@tcp(127.0.0.1:3306)/workbench?parseTime=true"}}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("IM_WORKBENCH_CONFIG", path)
|
|
t.Setenv("IM_WORKBENCH_MYSQL_DSN", "")
|
|
t.Setenv("IM_WORKBENCH_CORE_MYSQL_DSN", "")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.CoreMySQL.DSN != cfg.MySQL.DSN {
|
|
t.Fatalf("core_mysql.dsn = %q, want primary mysql.dsn %q", cfg.CoreMySQL.DSN, cfg.MySQL.DSN)
|
|
}
|
|
}
|
|
|
|
func TestLoadUsesCoreMySQLDSNFromEnvironment(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "workbench.json")
|
|
if err := os.WriteFile(path, []byte(`{"mysql":{"dsn":"root:root@tcp(127.0.0.1:3306)/workbench?parseTime=true"}}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("IM_WORKBENCH_CONFIG", path)
|
|
t.Setenv("IM_WORKBENCH_MYSQL_DSN", "")
|
|
t.Setenv("IM_WORKBENCH_CORE_MYSQL_DSN", "root:root@tcp(127.0.0.1:3306)/meeting_im?parseTime=true")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(cfg.CoreMySQL.DSN, "/meeting_im?") {
|
|
t.Fatalf("core_mysql.dsn = %q, want meeting_im", cfg.CoreMySQL.DSN)
|
|
}
|
|
}
|
|
|
|
func TestLoadPreservesCoreMySQLDSNFromJSON(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "workbench.json")
|
|
if err := os.WriteFile(path, []byte(`{"mysql":{"dsn":"root:root@tcp(127.0.0.1:3306)/workbench?parseTime=true"},"core_mysql":{"dsn":"root:root@tcp(127.0.0.1:3306)/meeting_im?parseTime=true"}}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("IM_WORKBENCH_CONFIG", path)
|
|
t.Setenv("IM_WORKBENCH_MYSQL_DSN", "")
|
|
t.Setenv("IM_WORKBENCH_CORE_MYSQL_DSN", "")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(cfg.MySQL.DSN, "/workbench?") {
|
|
t.Fatalf("mysql.dsn = %q, want workbench", cfg.MySQL.DSN)
|
|
}
|
|
if !strings.Contains(cfg.CoreMySQL.DSN, "/meeting_im?") {
|
|
t.Fatalf("core_mysql.dsn = %q, want meeting_im", cfg.CoreMySQL.DSN)
|
|
}
|
|
}
|