Merge pull request #54134 from chentao1596/plugin-pkg-scheduler-util-non-zero-unit-test

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Adding unit tests to methods of 'scheduler/algorithm/priorities/util'

What this PR does / why we need it:

 1) Adding unit tests to methods of 'non_zero.go'
 2) Adding unit tests to methods of 'util.go'
 3) Adding/Modifing unit tests to methods of 'topologies.go'

thank you!
pull/6/head
Kubernetes Submit Queue 2017-11-17 01:13:03 -08:00 committed by GitHub
commit a00b766ab0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 335 additions and 4 deletions

View File

@ -8,12 +8,21 @@ load(
go_test( go_test(
name = "go_default_test", name = "go_default_test",
srcs = ["topologies_test.go"], srcs = [
"non_zero_test.go",
"topologies_test.go",
"util_test.go",
],
importpath = "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities/util", importpath = "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/priorities/util",
library = ":go_default_library", library = ":go_default_library",
deps = [ deps = [
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library", "//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/selection:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
], ],
) )

View File

@ -0,0 +1,73 @@
/*
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 util
import (
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
func TestGetNonzeroRequests(t *testing.T) {
tds := []struct {
name string
requests v1.ResourceList
expectedCPU int64
expectedMemory int64
}{
{
"cpu_and_memory_not_found",
v1.ResourceList{},
DefaultMilliCpuRequest,
DefaultMemoryRequest,
},
{
"only_cpu_exist",
v1.ResourceList{
v1.ResourceCPU: resource.MustParse("200m"),
},
200,
DefaultMemoryRequest,
},
{
"only_memory_exist",
v1.ResourceList{
v1.ResourceMemory: resource.MustParse("400Mi"),
},
DefaultMilliCpuRequest,
400 * 1024 * 1024,
},
{
"cpu_memory_exist",
v1.ResourceList{
v1.ResourceCPU: resource.MustParse("200m"),
v1.ResourceMemory: resource.MustParse("400Mi"),
},
200,
400 * 1024 * 1024,
},
}
for _, td := range tds {
realCPU, realMemory := GetNonzeroRequests(&td.requests)
assert.EqualValuesf(t, td.expectedCPU, realCPU, "Failed to test: %s", td.name)
assert.EqualValuesf(t, td.expectedMemory, realMemory, "Failed to test: %s", td.name)
}
}

View File

@ -19,10 +19,93 @@ package util
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/sets"
) )
func fakePod() *v1.Pod {
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "topologies_pod",
Namespace: metav1.NamespaceDefault,
UID: "551f5a43-9f2f-11e7-a589-fa163e148d75",
},
}
}
func TestGetNamespacesFromPodAffinityTerm(t *testing.T) {
tests := []struct {
name string
podAffinityTerm *v1.PodAffinityTerm
expectedValue sets.String
}{
{
"podAffinityTerm_namespace_empty",
&v1.PodAffinityTerm{},
sets.String{metav1.NamespaceDefault: sets.Empty{}},
},
{
"podAffinityTerm_namespace_not_empty",
&v1.PodAffinityTerm{
Namespaces: []string{metav1.NamespacePublic, metav1.NamespaceSystem},
},
sets.String{metav1.NamespacePublic: sets.Empty{}, metav1.NamespaceSystem: sets.Empty{}},
},
}
for _, test := range tests {
realValue := GetNamespacesFromPodAffinityTerm(fakePod(), test.podAffinityTerm)
assert.EqualValuesf(t, test.expectedValue, realValue, "Failed to test: %s", test.name)
}
}
func TestPodMatchesTermsNamespaceAndSelector(t *testing.T) {
fakeNamespaces := sets.String{metav1.NamespacePublic: sets.Empty{}, metav1.NamespaceSystem: sets.Empty{}}
fakeRequirement, _ := labels.NewRequirement("service", selection.In, []string{"topologies_service1", "topologies_service2"})
fakeSelector := labels.NewSelector().Add(*fakeRequirement)
tests := []struct {
name string
podNamespaces string
podLabels map[string]string
expectedResult bool
}{
{
"namespace_not_in",
metav1.NamespaceDefault,
map[string]string{"service": "topologies_service1"},
false,
},
{
"label_not_match",
metav1.NamespacePublic,
map[string]string{"service": "topologies_service3"},
false,
},
{
"normal_case",
metav1.NamespacePublic,
map[string]string{"service": "topologies_service1"},
true,
},
}
for _, test := range tests {
fakeTestPod := fakePod()
fakeTestPod.Namespace = test.podNamespaces
fakeTestPod.Labels = test.podLabels
realValue := PodMatchesTermsNamespaceAndSelector(fakeTestPod, fakeNamespaces, fakeSelector)
assert.EqualValuesf(t, test.expectedResult, realValue, "Faild to test: %s", test.name)
}
}
func TestNodesHaveSameTopologyKey(t *testing.T) { func TestNodesHaveSameTopologyKey(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -113,12 +196,59 @@ func TestNodesHaveSameTopologyKey(t *testing.T) {
expected: false, expected: false,
topologyKey: "b", topologyKey: "b",
}, },
{
name: "topologyKey empty",
nodeA: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"a": "",
},
},
},
nodeB: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"a": "",
},
},
},
expected: false,
topologyKey: "",
},
{
name: "nodeA lable nil vs. nodeB{'a':''} by key('a')",
nodeA: &v1.Node{
ObjectMeta: metav1.ObjectMeta{},
},
nodeB: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"a": "",
},
},
},
expected: false,
topologyKey: "a",
},
{
name: "nodeA{'a':''} vs. nodeB label is nil by key('a')",
nodeA: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"a": "",
},
},
},
nodeB: &v1.Node{
ObjectMeta: metav1.ObjectMeta{},
},
expected: false,
topologyKey: "a",
},
} }
for _, test := range tests { for _, test := range tests {
got := NodesHaveSameTopologyKey(test.nodeA, test.nodeB, test.topologyKey) got := NodesHaveSameTopologyKey(test.nodeA, test.nodeB, test.topologyKey)
if test.expected != got { assert.Equalf(t, test.expected, got, "Failed to test: %s", test.name)
t.Errorf("%v: expected %t, got %t", test.name, test.expected, got)
}
} }
} }

View File

@ -0,0 +1,119 @@
/*
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 util
import (
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestGetControllerRef(t *testing.T) {
fakeBlockOwnerDeletion := true
fakeFalseController := false
fakeTrueController := true
fakeEmptyOwnerReference := metav1.OwnerReference{}
tds := []struct {
name string
pod v1.Pod
expectedNil bool
expectedOR metav1.OwnerReference
}{
{
"ownerreference_not_exist",
v1.Pod{},
true,
fakeEmptyOwnerReference,
},
{
"ownerreference_controller_is_nil",
v1.Pod{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "extensions/v1beta1",
Kind: "ReplicaSet",
Name: "or-unit-test-5b9cffccff",
UID: "a46372ea-b254-11e7-8373-fa163e25bfb5",
BlockOwnerDeletion: &fakeBlockOwnerDeletion,
},
},
},
},
true,
fakeEmptyOwnerReference,
},
{
"ownerreference_controller_is_false",
v1.Pod{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "extensions/v1beta1",
Kind: "ReplicaSet",
Name: "or-unit-test-5b9cffccff",
UID: "a46372ea-b254-11e7-8373-fa163e25bfb5",
Controller: &fakeFalseController,
BlockOwnerDeletion: &fakeBlockOwnerDeletion,
},
},
},
},
true,
fakeEmptyOwnerReference,
},
{
"ownerreference_controller_is_true",
v1.Pod{
ObjectMeta: metav1.ObjectMeta{
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "extensions/v1beta1",
Kind: "ReplicaSet",
Name: "or-unit-test-5b9cffccff",
UID: "a46372ea-b254-11e7-8373-fa163e25bfb5",
BlockOwnerDeletion: &fakeBlockOwnerDeletion,
Controller: &fakeTrueController,
},
},
},
},
false,
metav1.OwnerReference{
APIVersion: "extensions/v1beta1",
Kind: "ReplicaSet",
Name: "or-unit-test-5b9cffccff",
UID: "a46372ea-b254-11e7-8373-fa163e25bfb5",
BlockOwnerDeletion: &fakeBlockOwnerDeletion,
Controller: &fakeTrueController,
},
},
}
for _, td := range tds {
realOR := GetControllerRef(&td.pod)
if td.expectedNil {
assert.Nilf(t, realOR, "Failed to test: %s", td.name)
} else {
assert.Equalf(t, &td.expectedOR, realOR, "Failed to test: %s", td.name)
}
}
}