Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
22247068
Commit
22247068
authored
Oct 18, 2017
by
chentao1596
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding unit tests to methods of 'plugin/pkg/scheduler/algorithm/priorities/util'
parent
d462bac7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
335 additions
and
4 deletions
+335
-4
BUILD
plugin/pkg/scheduler/algorithm/priorities/util/BUILD
+10
-1
non_zero_test.go
.../pkg/scheduler/algorithm/priorities/util/non_zero_test.go
+73
-0
topologies_test.go
...kg/scheduler/algorithm/priorities/util/topologies_test.go
+133
-3
util_test.go
plugin/pkg/scheduler/algorithm/priorities/util/util_test.go
+119
-0
No files found.
plugin/pkg/scheduler/algorithm/priorities/util/BUILD
View file @
22247068
...
@@ -8,12 +8,21 @@ load(
...
@@ -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",
],
],
)
)
...
...
plugin/pkg/scheduler/algorithm/priorities/util/non_zero_test.go
0 → 100644
View file @
22247068
/*
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
)
}
}
plugin/pkg/scheduler/algorithm/priorities/util/topologies_test.go
View file @
22247068
...
@@ -19,10 +19,93 @@ package util
...
@@ -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) {
...
@@ -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
)
}
}
}
}
}
plugin/pkg/scheduler/algorithm/priorities/util/util_test.go
0 → 100644
View file @
22247068
/*
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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment