Skip to content

Commit e061a62

Browse files
Default perf ports when the Perf block is partially specified
The ports were only defaulted when Perf was absent entirely, so a config that set direct/service but left the ports out reached the dataplane with port 0 and the apiserver rejected the test policy. Move the range checks out of the external-only branch too, so a bad port fails at config load rather than 20 seconds into a run.
1 parent d094158 commit e061a62

2 files changed

Lines changed: 80 additions & 9 deletions

File tree

pkg/config/config.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,33 @@ func defaultAndValidate(cfg *Config) error {
287287
if tcfg.Perf.ExternalIPOrFQDN == "" {
288288
return fmt.Errorf("ExternalIPOrFQDN is required for an external thruput-latency test")
289289
}
290-
if tcfg.TestKind == "thruput-latency" {
291-
if tcfg.Perf.ControlPort == 0 {
292-
return fmt.Errorf("ControlPort is required for an external thruput-latency test")
293-
}
294-
if tcfg.Perf.ControlPort > 65535 || tcfg.Perf.ControlPort < 1 {
295-
return fmt.Errorf("ControlPort must be between 1 and 65535")
296-
}
290+
291+
// External ports can't be defaulted. The test tools don't work through NAT, so
292+
// these have to match the ports the service is actually exposed on.
293+
if tcfg.TestKind == "thruput-latency" && tcfg.Perf.ControlPort == 0 {
294+
return fmt.Errorf("ControlPort is required for an external thruput-latency test")
297295
}
298296
if tcfg.Perf.TestPort == 0 {
299297
return fmt.Errorf("TestPort is required for an external thruput-latency test")
300298
}
301-
if tcfg.Perf.TestPort > 65535 || tcfg.Perf.TestPort < 1 {
302-
return fmt.Errorf("TestPort must be between 1 and 65535")
299+
} else {
300+
// Default the ports per-field rather than only when Perf is absent entirely. A
301+
// config that sets some of Perf but leaves the ports out otherwise reaches the
302+
// dataplane with port 0, and the apiserver rejects the test policy.
303+
if tcfg.Perf.ControlPort == 0 {
304+
tcfg.Perf.ControlPort = 32000
305+
}
306+
if tcfg.Perf.TestPort == 0 {
307+
tcfg.Perf.TestPort = 32001
303308
}
304309
}
310+
311+
if tcfg.TestKind == "thruput-latency" && (tcfg.Perf.ControlPort > 65535 || tcfg.Perf.ControlPort < 1) {
312+
return fmt.Errorf("ControlPort must be between 1 and 65535")
313+
}
314+
if tcfg.Perf.TestPort > 65535 || tcfg.Perf.TestPort < 1 {
315+
return fmt.Errorf("TestPort must be between 1 and 65535")
316+
}
305317
}
306318
}
307319
return nil

pkg/config/config_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,62 @@ func TestTargetURLCustomValue(t *testing.T) {
604604
require.NoError(t, err)
605605
assert.Equal(t, "https://custom.example.org", cfg.TestConfigs[0].DNSPerf.TargetURL)
606606
}
607+
608+
func TestPerfPortsDefaultedWhenPerfPartiallySpecified(t *testing.T) {
609+
fileContent := `
610+
- testKind: thruput-latency
611+
Perf:
612+
direct: true
613+
service: true
614+
external: false
615+
`
616+
filePath := "/tmp/test_configs.yaml"
617+
err := os.WriteFile(filePath, []byte(fileContent), 0o644)
618+
require.NoError(t, err)
619+
defer os.Remove(filePath)
620+
621+
var cfg Config
622+
cfg.TestConfigFile = filePath
623+
err = loadTestConfigsFromFile(&cfg)
624+
require.NoError(t, err)
625+
assert.Equal(t, 32000, cfg.TestConfigs[0].Perf.ControlPort)
626+
assert.Equal(t, 32001, cfg.TestConfigs[0].Perf.TestPort)
627+
}
628+
629+
func TestPerfPortsStillRequiredForExternalTest(t *testing.T) {
630+
fileContent := `
631+
- testKind: thruput-latency
632+
Perf:
633+
external: true
634+
ExternalIPOrFQDN: perf.example.com
635+
`
636+
filePath := "/tmp/test_configs.yaml"
637+
err := os.WriteFile(filePath, []byte(fileContent), 0o644)
638+
require.NoError(t, err)
639+
defer os.Remove(filePath)
640+
641+
var cfg Config
642+
cfg.TestConfigFile = filePath
643+
err = loadTestConfigsFromFile(&cfg)
644+
require.Error(t, err)
645+
assert.Contains(t, err.Error(), "ControlPort is required")
646+
}
647+
648+
func TestPerfPortOutOfRangeRejectedForNonExternalTest(t *testing.T) {
649+
fileContent := `
650+
- testKind: thruput-latency
651+
Perf:
652+
external: false
653+
TestPort: 99999
654+
`
655+
filePath := "/tmp/test_configs.yaml"
656+
err := os.WriteFile(filePath, []byte(fileContent), 0o644)
657+
require.NoError(t, err)
658+
defer os.Remove(filePath)
659+
660+
var cfg Config
661+
cfg.TestConfigFile = filePath
662+
err = loadTestConfigsFromFile(&cfg)
663+
require.Error(t, err)
664+
assert.Contains(t, err.Error(), "TestPort must be between 1 and 65535")
665+
}

0 commit comments

Comments
 (0)