From 9abf4b1d732e063826261ee60a72bd47b17f048b Mon Sep 17 00:00:00 2001 From: xiangpengzhao Date: Tue, 31 Oct 2017 16:22:05 +0800 Subject: [PATCH] Add test case for validateIPVSSchedulerMethod. --- cmd/kube-proxy/app/validation.go | 26 ++++++++--------- cmd/kube-proxy/app/validation_test.go | 42 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/cmd/kube-proxy/app/validation.go b/cmd/kube-proxy/app/validation.go index 3eead9da06b..79dc7011940 100644 --- a/cmd/kube-proxy/app/validation.go +++ b/cmd/kube-proxy/app/validation.go @@ -38,7 +38,7 @@ func Validate(config *componentconfig.KubeProxyConfiguration) field.ErrorList { allErrs = append(allErrs, validateKubeProxyConntrackConfiguration(config.Conntrack, newPath.Child("KubeProxyConntrackConfiguration"))...) allErrs = append(allErrs, validateProxyMode(config.Mode, newPath.Child("Mode"))...) allErrs = append(allErrs, validateClientConnectionConfiguration(config.ClientConnection, newPath.Child("ClientConnection"))...) - allErrs = append(allErrs, validateIPVSSchedulerMethod(config.IPVS.Scheduler, newPath.Child("KubeProxyIPVSConfiguration").Child("Scheduler"))...) + allErrs = append(allErrs, validateIPVSSchedulerMethod(componentconfig.IPVSSchedulerMethod(config.IPVS.Scheduler), newPath.Child("KubeProxyIPVSConfiguration").Child("Scheduler"))...) if config.OOMScoreAdj != nil && (*config.OOMScoreAdj < -1000 || *config.OOMScoreAdj > 1000) { allErrs = append(allErrs, field.Invalid(newPath.Child("OOMScoreAdj"), *config.OOMScoreAdj, "must be within the range [-1000, 1000]")) @@ -159,18 +159,18 @@ func validateHostPort(input string, fldPath *field.Path) field.ErrorList { return allErrs } -func validateIPVSSchedulerMethod(scheduler string, fldPath *field.Path) field.ErrorList { - supportedMethod := []string{ - string(componentconfig.RoundRobin), - string(componentconfig.WeightedRoundRobin), - string(componentconfig.LeastConnection), - string(componentconfig.WeightedLeastConnection), - string(componentconfig.LocalityBasedLeastConnection), - string(componentconfig.LocalityBasedLeastConnectionWithReplication), - string(componentconfig.SourceHashing), - string(componentconfig.DestinationHashing), - string(componentconfig.ShortestExpectedDelay), - string(componentconfig.NeverQueue), +func validateIPVSSchedulerMethod(scheduler componentconfig.IPVSSchedulerMethod, fldPath *field.Path) field.ErrorList { + supportedMethod := []componentconfig.IPVSSchedulerMethod{ + componentconfig.RoundRobin, + componentconfig.WeightedRoundRobin, + componentconfig.LeastConnection, + componentconfig.WeightedLeastConnection, + componentconfig.LocalityBasedLeastConnection, + componentconfig.LocalityBasedLeastConnectionWithReplication, + componentconfig.SourceHashing, + componentconfig.DestinationHashing, + componentconfig.ShortestExpectedDelay, + componentconfig.NeverQueue, "", } allErrs := field.ErrorList{} diff --git a/cmd/kube-proxy/app/validation_test.go b/cmd/kube-proxy/app/validation_test.go index 7aea6109e80..51d22b3299b 100644 --- a/cmd/kube-proxy/app/validation_test.go +++ b/cmd/kube-proxy/app/validation_test.go @@ -489,3 +489,45 @@ func TestValidateHostPort(t *testing.T) { } } } + +func TestValidateIPVSSchedulerMethod(t *testing.T) { + newPath := field.NewPath("KubeProxyConfiguration") + + successCases := []componentconfig.IPVSSchedulerMethod{ + componentconfig.RoundRobin, + componentconfig.WeightedRoundRobin, + componentconfig.LeastConnection, + componentconfig.WeightedLeastConnection, + componentconfig.LocalityBasedLeastConnection, + componentconfig.LocalityBasedLeastConnectionWithReplication, + componentconfig.SourceHashing, + componentconfig.DestinationHashing, + componentconfig.ShortestExpectedDelay, + componentconfig.NeverQueue, + "", + } + + for _, successCase := range successCases { + if errs := validateIPVSSchedulerMethod(successCase, newPath.Child("Scheduler")); len(errs) != 0 { + t.Errorf("expected success: %v", errs) + } + } + + errorCases := []struct { + mode componentconfig.IPVSSchedulerMethod + msg string + }{ + { + mode: componentconfig.IPVSSchedulerMethod("non-existing"), + msg: "blank means the default algorithm method (currently rr)", + }, + } + + for _, errorCase := range errorCases { + if errs := validateIPVSSchedulerMethod(errorCase.mode, newPath.Child("ProxyMode")); 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) + } + } +}