Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code coverage for scheduler/algorithmprovider/defaults #40269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion plugin/pkg/scheduler/algorithmprovider/defaults/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
3 changes: 2 additions & 1 deletion plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
DefaultMaxAzureDiskVolumes = 16
ClusterAutoscalerProvider = "ClusterAutoscalerProvider"
StatefulSetKind = "StatefulSet"
KubeMaxPDVols = "KUBE_MAX_PD_VOLS"
)

func init() {
Expand Down Expand Up @@ -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 {
Expand Down
62 changes: 62 additions & 0 deletions plugin/pkg/scheduler/algorithmprovider/defaults/defaults_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}