diff --git a/cmd/kube-proxy/app/validation_test.go b/cmd/kube-proxy/app/validation_test.go index 1e1f8384cf9..7aea6109e80 100644 --- a/cmd/kube-proxy/app/validation_test.go +++ b/cmd/kube-proxy/app/validation_test.go @@ -112,7 +112,7 @@ func TestValidateKubeProxyConfiguration(t *testing.T) { config: componentconfig.KubeProxyConfiguration{ BindAddress: "10.10.12.11", HealthzBindAddress: "0.0.0.0:12345", - // only HealthzBindAddress is invalid + // only MetricsBindAddress is invalid MetricsBindAddress: "127.0.0.1", ClusterCIDR: "192.168.59.0/24", UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second}, @@ -439,3 +439,53 @@ func TestValidateClientConnectionConfiguration(t *testing.T) { } } } + +func TestValidateHostPort(t *testing.T) { + newPath := field.NewPath("KubeProxyConfiguration") + + successCases := []string{ + "0.0.0.0:10256", + "127.0.0.1:10256", + "10.10.10.10:10256", + } + + for _, successCase := range successCases { + if errs := validateHostPort(successCase, newPath.Child("HealthzBindAddress")); len(errs) != 0 { + t.Errorf("expected success: %v", errs) + } + } + + errorCases := []struct { + ccc string + msg string + }{ + { + ccc: "10.10.10.10", + msg: "must be IP:port", + }, + { + ccc: "123.456.789.10:12345", + msg: "must be a valid IP", + }, + { + ccc: "10.10.10.10:foo", + msg: "must be a valid port", + }, + { + ccc: "10.10.10.10:0", + msg: "must be a valid port", + }, + { + ccc: "10.10.10.10:65536", + msg: "must be a valid port", + }, + } + + for _, errorCase := range errorCases { + if errs := validateHostPort(errorCase.ccc, newPath.Child("HealthzBindAddress")); len(errs) == 0 { + t.Errorf("expected failure for %s", errorCase.msg) + } else if !strings.Contains(errs[0].Error(), errorCase.msg) { + t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg) + } + } +}