122 lines
4.1 KiB
Go
122 lines
4.1 KiB
Go
package config
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// testConfigFile 单元测试专用配置(位于 testdata,与开发配置隔离)。
|
|
const testConfigFile = "config.test.yaml"
|
|
|
|
func loadTestConfig(t *testing.T) *Config {
|
|
t.Helper()
|
|
cfg, err := Load(filepath.Join("testdata", testConfigFile))
|
|
if err != nil {
|
|
t.Fatalf("加载测试配置失败: %v", err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// TestLoad_FieldsMapped 验证 YAML 各层级字段被正确映射到结构体。
|
|
func TestLoad_FieldsMapped(t *testing.T) {
|
|
cfg := loadTestConfig(t)
|
|
|
|
if cfg.Server.Name != "go-sms-test" {
|
|
t.Errorf("server.name = %q, 期望 %q", cfg.Server.Name, "go-sms-test")
|
|
}
|
|
if cfg.Server.HTTP.Addr != ":9800" {
|
|
t.Errorf("server.http.addr = %q, 期望 %q", cfg.Server.HTTP.Addr, ":9800")
|
|
}
|
|
if cfg.Server.HTTP.ReadTimeout != 10*time.Second {
|
|
t.Errorf("server.http.read_timeout = %s, 期望 10s", cfg.Server.HTTP.ReadTimeout)
|
|
}
|
|
if cfg.MySQL.MaxOpenConns != 10 {
|
|
t.Errorf("mysql.max_open_conns = %d, 期望 10", cfg.MySQL.MaxOpenConns)
|
|
}
|
|
if cfg.Redis.Mode != "cluster" {
|
|
t.Errorf("redis.mode = %q, 期望 cluster", cfg.Redis.Mode)
|
|
}
|
|
if len(cfg.Redis.Addrs) != 2 || cfg.Redis.Addrs[0] != "127.0.0.1:16380" {
|
|
t.Errorf("redis.addrs = %v, 期望 [127.0.0.1:16380 127.0.0.1:16381]", cfg.Redis.Addrs)
|
|
}
|
|
if cfg.Kafka.Topics["submit"] != "test-sms-submit" {
|
|
t.Errorf("kafka.topics.submit = %q, 期望 test-sms-submit", cfg.Kafka.Topics["submit"])
|
|
}
|
|
if cfg.ETCD.DialTimeout != 5*time.Second {
|
|
t.Errorf("etcd.dial_timeout = %s, 期望 5s", cfg.ETCD.DialTimeout)
|
|
}
|
|
if cfg.MinIO.Bucket != "go-sms-test" {
|
|
t.Errorf("minio.bucket = %q, 期望 go-sms-test", cfg.MinIO.Bucket)
|
|
}
|
|
}
|
|
|
|
// TestLoad_DefaultsApplied 验证未在 YAML 中出现的键回退到默认值。
|
|
func TestLoad_DefaultsApplied(t *testing.T) {
|
|
cfg := loadTestConfig(t)
|
|
|
|
// 测试配置未定义以下键,应命中 setDefaults
|
|
if !cfg.Log.File.Compress {
|
|
t.Error("log.file.compress 默认应为 true")
|
|
}
|
|
if cfg.Server.CMPP.WindowSize != 32 {
|
|
t.Errorf("server.cmpp.window_size 默认应为 32, 实际 %d", cfg.Server.CMPP.WindowSize)
|
|
}
|
|
if cfg.MySQL.MaxIdleConns != 20 {
|
|
t.Errorf("mysql.max_idle_conns 默认应为 20, 实际 %d", cfg.MySQL.MaxIdleConns)
|
|
}
|
|
if cfg.Log.Level != "info" {
|
|
t.Errorf("log.level 默认应为 info, 实际 %q", cfg.Log.Level)
|
|
}
|
|
}
|
|
|
|
// TestLoad_EnvOverride 验证 GOSMS_ 前缀环境变量可覆盖 YAML 值。
|
|
func TestLoad_EnvOverride(t *testing.T) {
|
|
t.Setenv("GOSMS_MYSQL_DSN", "env-user:env-pass@tcp(127.0.0.1:13306)/env_db")
|
|
t.Setenv("GOSMS_REDIS_ADDRS", "10.0.0.1:6379,10.0.0.2:6379")
|
|
|
|
cfg := loadTestConfig(t)
|
|
|
|
if !strings.Contains(cfg.MySQL.DSN, "env_db") {
|
|
t.Errorf("mysql.dsn 环境变量覆盖失败, 实际 %q", cfg.MySQL.DSN)
|
|
}
|
|
if len(cfg.Redis.Addrs) != 2 || cfg.Redis.Addrs[0] != "10.0.0.1:6379" {
|
|
t.Errorf("redis.addrs 环境变量覆盖失败, 实际 %v", cfg.Redis.Addrs)
|
|
}
|
|
}
|
|
|
|
// TestLoad_FileNotFound 验证配置文件不存在时返回错误。
|
|
func TestLoad_FileNotFound(t *testing.T) {
|
|
if _, err := Load(filepath.Join("testdata", "not-exists.yaml")); err == nil {
|
|
t.Fatal("配置文件不存在时应返回错误")
|
|
}
|
|
}
|
|
|
|
// TestLoadWithOverrides 验证覆盖加载优先级(文件 < 显式覆盖)。
|
|
func TestLoadWithOverrides(t *testing.T) {
|
|
t.Setenv("GOSMS_LOG_LEVEL", "warn") // 环境变量也应被显式覆盖压过
|
|
cfg, err := LoadWithOverrides(filepath.Join("testdata", testConfigFile),
|
|
map[string]string{"log.level": "error", "mysql.max_open_conns": "7"})
|
|
if err != nil {
|
|
t.Fatalf("LoadWithOverrides 失败: %v", err)
|
|
}
|
|
if cfg.Log.Level != "error" {
|
|
t.Errorf("log.level = %q, 期望 error(覆盖最高优先级)", cfg.Log.Level)
|
|
}
|
|
if cfg.MySQL.MaxOpenConns != 7 {
|
|
t.Errorf("mysql.max_open_conns = %d, 期望 7", cfg.MySQL.MaxOpenConns)
|
|
}
|
|
}
|
|
|
|
// TestLoad_ValidateFailed 验证非法配置被 Validate 拦截。
|
|
func TestLoad_ValidateFailed(t *testing.T) {
|
|
_, err := Load(filepath.Join("testdata", "invalid.yaml"))
|
|
if err == nil {
|
|
t.Fatal("非法配置应返回校验错误")
|
|
}
|
|
if !strings.Contains(err.Error(), "配置校验失败") {
|
|
t.Errorf("错误信息应包含 \"配置校验失败\", 实际: %v", err)
|
|
}
|
|
}
|