fix(config): add NormalizeHomePort function and update home port handling

This commit is contained in:
hkfires
2026-08-30 08:59:04 +08:00
parent 02e3d33c49
commit 7ab999a80f
4 changed files with 78 additions and 1 deletions

View File

@@ -307,7 +307,7 @@ func main() {
parsed = &config.Config{}
}
parsed.Home = homeCfg
parsed.Port = 8317 // Default to 8317 for home mode, can be overridden by home config
parsed.Port = config.NormalizeHomePort(parsed.Port)
parsed.UsageStatisticsEnabled = true
pluginSyncCfg := *parsed
parsed.Plugins.StoreAuth = nil

View File

@@ -135,3 +135,48 @@ func TestModelCatalogUpdaterPlan(t *testing.T) {
})
}
}
func TestHomeConfigPayloadPortApplication(t *testing.T) {
tests := []struct {
name string
yamlBody string
wantPort int
}{
{
name: "custom port honored",
yamlBody: "port: 9090\n",
wantPort: 9090,
},
{
name: "custom port 8327 honored",
yamlBody: "port: 8327\n",
wantPort: 8327,
},
{
name: "missing port defaults to 8317",
yamlBody: "debug: true\n",
wantPort: 8317,
},
{
name: "standard port 8317 preserved",
yamlBody: "port: 8317\n",
wantPort: 8317,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parsed, errParse := config.ParseConfigBytes([]byte(tt.yamlBody))
if errParse != nil {
t.Fatalf("ParseConfigBytes() error = %v", errParse)
}
if parsed == nil {
parsed = &config.Config{}
}
parsed.Port = config.NormalizeHomePort(parsed.Port)
if parsed.Port != tt.wantPort {
t.Fatalf("parsed.Port = %d, want %d", parsed.Port, tt.wantPort)
}
})
}
}

View File

@@ -20,3 +20,12 @@ type HomeTLSConfig struct {
ClientKey string `yaml:"-" json:"-"`
UseTargetServerName bool `yaml:"-" json:"-"`
}
// NormalizeHomePort ensures that the CPA server port received from Home is valid,
// defaulting to 8317 when omitted or non-positive.
func NormalizeHomePort(port int) int {
if port <= 0 {
return 8317
}
return port
}

View File

@@ -44,3 +44,26 @@ home:
t.Fatal("Home.TLS.InsecureSkipVerify = true, want false")
}
}
func TestNormalizeHomePort(t *testing.T) {
tests := []struct {
name string
port int
want int
}{
{name: "zero defaults to 8317", port: 0, want: 8317},
{name: "negative defaults to 8317", port: -1, want: 8317},
{name: "port 8327 preserved", port: 8327, want: 8327},
{name: "standard 8317 preserved", port: 8317, want: 8317},
{name: "custom port 8080 preserved", port: 8080, want: 8080},
{name: "custom port 9090 preserved", port: 9090, want: 9090},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NormalizeHomePort(tt.port); got != tt.want {
t.Fatalf("NormalizeHomePort(%d) = %d, want %d", tt.port, got, tt.want)
}
})
}
}