* 默认监听调整为 0.0.0.0:8080,并要求查询参数 token * 支持首次 run 自动生成含随机 token 的配置文件 * 补充 HTTP 鉴权、配置校验和相关测试 * 完善代码注释,覆盖缓存、上报、服务和平台采集路径 * 新增架构与开发流程文档,补充一键构建脚本
153 lines
4.2 KiB
Go
153 lines
4.2 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestDefaultConfigUsesAllInterfacesAndSaneIntervals 验证默认配置监听所有网卡且采集间隔合理。
|
|
func TestDefaultConfigUsesAllInterfacesAndSaneIntervals(t *testing.T) {
|
|
cfg := Default()
|
|
|
|
if cfg.HTTP.ListenAddress != "0.0.0.0:8080" {
|
|
t.Fatalf("listen address = %q", cfg.HTTP.ListenAddress)
|
|
}
|
|
if cfg.HTTP.Token != "" {
|
|
t.Fatalf("default config should not include token")
|
|
}
|
|
if cfg.Collect.Interval <= 0 || cfg.Collect.Timeout <= 0 {
|
|
t.Fatalf("collect intervals must be positive: %+v", cfg.Collect)
|
|
}
|
|
if cfg.Report.Enabled {
|
|
t.Fatal("reporting should be disabled by default")
|
|
}
|
|
}
|
|
|
|
// TestResolvePathUsesDefaultFileName 验证空路径解析为 config.json。
|
|
func TestResolvePathUsesDefaultFileName(t *testing.T) {
|
|
if got := ResolvePath(""); got != DefaultConfigFileName {
|
|
t.Fatalf("path = %q", got)
|
|
}
|
|
if got := ResolvePath("/etc/agent.json"); got != "/etc/agent.json" {
|
|
t.Fatalf("path = %q", got)
|
|
}
|
|
}
|
|
|
|
// TestLoadOrCreateWritesConfigWithToken 验证缺失配置文件时会生成含 token 的 JSON。
|
|
func TestLoadOrCreateWritesConfigWithToken(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.json")
|
|
|
|
cfg, err := LoadOrCreate(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.HTTP.Token == "" {
|
|
t.Fatal("expected generated token")
|
|
}
|
|
if cfg.HTTP.ListenAddress != "0.0.0.0:8080" {
|
|
t.Fatalf("listen address = %q", cfg.HTTP.ListenAddress)
|
|
}
|
|
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if info.Mode().Perm() != 0o600 {
|
|
t.Fatalf("file mode = %o", info.Mode().Perm())
|
|
}
|
|
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var onDisk Config
|
|
if err := json.Unmarshal(data, &onDisk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if onDisk.HTTP.Token != cfg.HTTP.Token {
|
|
t.Fatalf("token mismatch: disk=%q loaded=%q", onDisk.HTTP.Token, cfg.HTTP.Token)
|
|
}
|
|
}
|
|
|
|
// TestLoadRejectsEmptyToken 验证未配置 token 时无法启动。
|
|
func TestLoadRejectsEmptyToken(t *testing.T) {
|
|
file, err := os.CreateTemp(t.TempDir(), "config-*.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = file.WriteString(`{"http":{"listen_address":"0.0.0.0:8080","token":""}}`)
|
|
_ = file.Close()
|
|
|
|
_, err = Load(file.Name())
|
|
if err == nil {
|
|
t.Fatal("expected error for empty token")
|
|
}
|
|
}
|
|
|
|
// TestLoadAppliesEnvironmentOverrides 验证环境变量可覆盖默认配置。
|
|
func TestLoadAppliesEnvironmentOverrides(t *testing.T) {
|
|
file, err := os.CreateTemp(t.TempDir(), "config-*.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = file.WriteString(`{"http":{"listen_address":"0.0.0.0:8080","token":"base-token"}}`)
|
|
_ = file.Close()
|
|
|
|
t.Setenv("MFD_AGENT_LISTEN", "127.0.0.1:9090")
|
|
t.Setenv("MFD_AGENT_TOKEN", "env-token")
|
|
t.Setenv("MFD_AGENT_REPORT_ENABLED", "true")
|
|
t.Setenv("MFD_AGENT_REPORT_ENDPOINT", "http://center/status")
|
|
t.Setenv("MFD_AGENT_REPORT_INTERVAL", "2s")
|
|
t.Setenv("MFD_AGENT_HOST_ID", "host-a")
|
|
|
|
cfg, err := Load(file.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if cfg.HTTP.ListenAddress != "127.0.0.1:9090" {
|
|
t.Fatalf("listen address = %q", cfg.HTTP.ListenAddress)
|
|
}
|
|
if cfg.HTTP.Token != "env-token" {
|
|
t.Fatalf("token = %q", cfg.HTTP.Token)
|
|
}
|
|
if !cfg.Report.Enabled || cfg.Report.Endpoint != "http://center/status" {
|
|
t.Fatalf("report config not applied: %+v", cfg.Report)
|
|
}
|
|
if cfg.Report.Interval.Duration() != 2*time.Second {
|
|
t.Fatalf("report interval = %s", cfg.Report.Interval.Duration())
|
|
}
|
|
if cfg.HostID != "host-a" {
|
|
t.Fatalf("host id = %q", cfg.HostID)
|
|
}
|
|
}
|
|
|
|
// TestLoadJSONConfigFile 验证可从 JSON 配置文件解析 Config。
|
|
func TestLoadJSONConfigFile(t *testing.T) {
|
|
file, err := os.CreateTemp(t.TempDir(), "config-*.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, _ = file.WriteString(`{"http":{"listen_address":"127.0.0.1:7070","token":"secret"},"collect":{"interval":"3s","timeout":"1s"}}`)
|
|
_ = file.Close()
|
|
|
|
cfg, err := Load(file.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if cfg.HTTP.ListenAddress != "127.0.0.1:7070" {
|
|
t.Fatalf("listen address = %q", cfg.HTTP.ListenAddress)
|
|
}
|
|
if cfg.HTTP.Token != "secret" {
|
|
t.Fatalf("token = %q", cfg.HTTP.Token)
|
|
}
|
|
if cfg.Collect.Interval.Duration() != 3*time.Second || cfg.Collect.Timeout.Duration() != time.Second {
|
|
t.Fatalf("collect config = %+v", cfg.Collect)
|
|
}
|
|
}
|