From 13f7b3171122a234986ed9004cdb67001f18fa17 Mon Sep 17 00:00:00 2001 From: tanshanshan Date: Fri, 3 Feb 2017 11:56:54 +0800 Subject: [PATCH] add unit test for scheduler --- .../algorithmprovider/defaults/BUILD | 5 +- .../algorithmprovider/defaults/defaults.go | 3 +- .../defaults/defaults_test.go | 62 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 plugin/pkg/scheduler/algorithmprovider/defaults/defaults_test.go diff --git a/plugin/pkg/scheduler/algorithmprovider/defaults/BUILD b/plugin/pkg/scheduler/algorithmprovider/defaults/BUILD index 99770b0471..aa05de7b63 100644 --- a/plugin/pkg/scheduler/algorithmprovider/defaults/BUILD +++ b/plugin/pkg/scheduler/algorithmprovider/defaults/BUILD @@ -28,7 +28,10 @@ go_library( go_test( name = "go_default_test", - srcs = ["compatibility_test.go"], + srcs = [ + "compatibility_test.go", + "defaults_test.go", + ], library = ":go_default_library", tags = ["automanaged"], deps = [ diff --git a/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go index e7c9a148ce..5468f7cb6d 100644 --- a/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go +++ b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go @@ -41,6 +41,7 @@ const ( DefaultMaxAzureDiskVolumes = 16 ClusterAutoscalerProvider = "ClusterAutoscalerProvider" StatefulSetKind = "StatefulSet" + KubeMaxPDVols = "KUBE_MAX_PD_VOLS" ) func init() { @@ -217,7 +218,7 @@ func defaultPriorities() sets.String { // getMaxVols checks the max PD volumes environment variable, otherwise returning a default value func getMaxVols(defaultVal int) int { - if rawMaxVols := os.Getenv("KUBE_MAX_PD_VOLS"); rawMaxVols != "" { + if rawMaxVols := os.Getenv(KubeMaxPDVols); rawMaxVols != "" { if parsedMaxVols, err := strconv.Atoi(rawMaxVols); err != nil { glog.Errorf("Unable to parse maxiumum PD volumes value, using default of %v: %v", defaultVal, err) } else if parsedMaxVols <= 0 { diff --git a/plugin/pkg/scheduler/algorithmprovider/defaults/defaults_test.go b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults_test.go new file mode 100644 index 0000000000..7e878d63b0 --- /dev/null +++ b/plugin/pkg/scheduler/algorithmprovider/defaults/defaults_test.go @@ -0,0 +1,62 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package defaults + +import ( + "os" + "testing" +) + +func TestGetMaxVols(t *testing.T) { + previousValue := os.Getenv(KubeMaxPDVols) + defaultValue := 39 + + tests := []struct { + rawMaxVols string + expected int + test string + }{ + { + rawMaxVols: "invalid", + expected: defaultValue, + test: "Unable to parse maxiumum PD volumes value, using default value", + }, + { + rawMaxVols: "-2", + expected: defaultValue, + test: "Maximum PD volumes must be a positive value, using default value", + }, + { + rawMaxVols: "40", + expected: 40, + test: "Parse maximum PD volumes value from env", + }, + } + + for _, test := range tests { + os.Setenv(KubeMaxPDVols, test.rawMaxVols) + result := getMaxVols(defaultValue) + if result != test.expected { + t.Errorf("%s: expected %v got %v", test.test, test.expected, result) + } + } + + os.Unsetenv(KubeMaxPDVols) + if previousValue != "" { + os.Setenv(KubeMaxPDVols, previousValue) + } +}