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
10af4fa9
Commit
10af4fa9
authored
Jul 21, 2017
by
dhilipkumars
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add UT and upgrade this test suite with go sub-test
parent
f17a572e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
127 additions
and
37 deletions
+127
-37
deployment_util_test.go
pkg/controller/deployment/util/deployment_util_test.go
+127
-37
No files found.
pkg/controller/deployment/util/deployment_util_test.go
View file @
10af4fa9
...
...
@@ -246,7 +246,7 @@ func TestGetNewRS(t *testing.T) {
newRC
:=
generateRS
(
newDeployment
)
tests
:=
[]
struct
{
test
string
Name
string
objs
[]
runtime
.
Object
expected
*
extensions
.
ReplicaSet
}{
...
...
@@ -282,6 +282,7 @@ func TestGetNewRS(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
Name
,
func
(
t
*
testing
.
T
)
{
fakeClient
:=
&
fake
.
Clientset
{}
fakeClient
=
addListPodsReactor
(
fakeClient
,
test
.
objs
[
0
])
fakeClient
=
addListRSReactor
(
fakeClient
,
test
.
objs
[
1
])
...
...
@@ -289,11 +290,12 @@ func TestGetNewRS(t *testing.T) {
fakeClient
=
addUpdateRSReactor
(
fakeClient
)
rs
,
err
:=
GetNewReplicaSet
(
&
newDeployment
,
fakeClient
)
if
err
!=
nil
{
t
.
Errorf
(
"In test case %s, got unexpected error %v"
,
test
.
test
,
err
)
t
.
Errorf
(
"In test case %s, got unexpected error %v"
,
test
.
Name
,
err
)
}
if
!
apiequality
.
Semantic
.
DeepEqual
(
rs
,
test
.
expected
)
{
t
.
Errorf
(
"In test case %s, expected %#v, got %#v"
,
test
.
test
,
test
.
expected
,
rs
)
t
.
Errorf
(
"In test case %s, expected %#v, got %#v"
,
test
.
Name
,
test
.
expected
,
rs
)
}
})
}
}
...
...
@@ -318,7 +320,7 @@ func TestGetOldRSs(t *testing.T) {
existedRS
.
Status
.
FullyLabeledReplicas
=
*
(
existedRS
.
Spec
.
Replicas
)
tests
:=
[]
struct
{
test
string
Name
string
objs
[]
runtime
.
Object
expected
[]
*
extensions
.
ReplicaSet
}{
...
...
@@ -354,24 +356,26 @@ func TestGetOldRSs(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
Name
,
func
(
t
*
testing
.
T
)
{
fakeClient
:=
&
fake
.
Clientset
{}
fakeClient
=
addListRSReactor
(
fakeClient
,
test
.
objs
[
0
])
fakeClient
=
addGetRSReactor
(
fakeClient
,
test
.
objs
[
0
])
fakeClient
=
addUpdateRSReactor
(
fakeClient
)
_
,
rss
,
err
:=
GetOldReplicaSets
(
&
newDeployment
,
fakeClient
)
if
err
!=
nil
{
t
.
Errorf
(
"In test case %s, got unexpected error %v"
,
test
.
test
,
err
)
t
.
Errorf
(
"In test case %s, got unexpected error %v"
,
test
.
Name
,
err
)
}
if
!
equal
(
rss
,
test
.
expected
)
{
t
.
Errorf
(
"In test case %q, expected:"
,
test
.
test
)
t
.
Errorf
(
"In test case %q, expected:"
,
test
.
Name
)
for
_
,
rs
:=
range
test
.
expected
{
t
.
Errorf
(
"rs = %#v"
,
rs
)
}
t
.
Errorf
(
"In test case %q, got:"
,
test
.
test
)
t
.
Errorf
(
"In test case %q, got:"
,
test
.
Name
)
for
_
,
rs
:=
range
rss
{
t
.
Errorf
(
"rs = %#v"
,
rs
)
}
}
})
}
}
...
...
@@ -390,7 +394,7 @@ func generatePodTemplateSpec(name, nodeName string, annotations, labels map[stri
func
TestEqualIgnoreHash
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
test
string
Name
string
former
,
latter
v1
.
PodTemplateSpec
expected
bool
}{
...
...
@@ -445,6 +449,7 @@ func TestEqualIgnoreHash(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
Name
,
func
(
t
*
testing
.
T
)
{
runTest
:=
func
(
t1
,
t2
*
v1
.
PodTemplateSpec
,
reversed
bool
)
{
reverseString
:=
""
if
reversed
{
...
...
@@ -453,20 +458,22 @@ func TestEqualIgnoreHash(t *testing.T) {
// Run
equal
,
err
:=
EqualIgnoreHash
(
t1
,
t2
)
if
err
!=
nil
{
t
.
Errorf
(
"%s: unexpected error: %v"
,
err
,
test
.
test
)
t
.
Errorf
(
"%s: unexpected error: %v"
,
err
,
test
.
Name
)
return
}
if
equal
!=
test
.
expected
{
t
.
Errorf
(
"%q%s: expected %v"
,
test
.
test
,
reverseString
,
test
.
expected
)
t
.
Errorf
(
"%q%s: expected %v"
,
test
.
Name
,
reverseString
,
test
.
expected
)
return
}
if
t1
.
Labels
==
nil
||
t2
.
Labels
==
nil
{
t
.
Errorf
(
"%q%s: unexpected labels becomes nil"
,
test
.
test
,
reverseString
)
t
.
Errorf
(
"%q%s: unexpected labels becomes nil"
,
test
.
Name
,
reverseString
)
}
}
runTest
(
&
test
.
former
,
&
test
.
latter
,
false
)
// Test the same case in reverse order
runTest
(
&
test
.
latter
,
&
test
.
former
,
true
)
})
}
}
...
...
@@ -489,25 +496,25 @@ func TestFindNewReplicaSet(t *testing.T) {
oldRS
.
Status
.
FullyLabeledReplicas
=
*
(
oldRS
.
Spec
.
Replicas
)
tests
:=
[]
struct
{
test
string
Name
string
deployment
extensions
.
Deployment
rsList
[]
*
extensions
.
ReplicaSet
expected
*
extensions
.
ReplicaSet
}{
{
test
:
"Get new ReplicaSet with the same template as Deployment spec but different pod-template-hash value"
,
Name
:
"Get new ReplicaSet with the same template as Deployment spec but different pod-template-hash value"
,
deployment
:
deployment
,
rsList
:
[]
*
extensions
.
ReplicaSet
{
&
newRS
,
&
oldRS
},
expected
:
&
newRS
,
},
{
test
:
"Get the oldest new ReplicaSet when there are more than one ReplicaSet with the same template"
,
Name
:
"Get the oldest new ReplicaSet when there are more than one ReplicaSet with the same template"
,
deployment
:
deployment
,
rsList
:
[]
*
extensions
.
ReplicaSet
{
&
newRS
,
&
oldRS
,
&
newRSDup
},
expected
:
&
newRSDup
,
},
{
test
:
"Get nil new ReplicaSet"
,
Name
:
"Get nil new ReplicaSet"
,
deployment
:
deployment
,
rsList
:
[]
*
extensions
.
ReplicaSet
{
&
oldRS
},
expected
:
nil
,
...
...
@@ -515,9 +522,11 @@ func TestFindNewReplicaSet(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
Name
,
func
(
t
*
testing
.
T
)
{
if
rs
,
err
:=
FindNewReplicaSet
(
&
test
.
deployment
,
test
.
rsList
);
!
reflect
.
DeepEqual
(
rs
,
test
.
expected
)
||
err
!=
nil
{
t
.
Errorf
(
"In test case %q, expected %#v, got %#v: %v"
,
test
.
test
,
test
.
expected
,
rs
,
err
)
t
.
Errorf
(
"In test case %q, expected %#v, got %#v: %v"
,
test
.
Name
,
test
.
expected
,
rs
,
err
)
}
})
}
}
...
...
@@ -543,7 +552,7 @@ func TestFindOldReplicaSets(t *testing.T) {
oldRS
.
CreationTimestamp
=
before
tests
:=
[]
struct
{
test
string
Name
string
deployment
extensions
.
Deployment
rsList
[]
*
extensions
.
ReplicaSet
podList
*
v1
.
PodList
...
...
@@ -551,28 +560,28 @@ func TestFindOldReplicaSets(t *testing.T) {
expectedRequire
[]
*
extensions
.
ReplicaSet
}{
{
test
:
"Get old ReplicaSets"
,
Name
:
"Get old ReplicaSets"
,
deployment
:
deployment
,
rsList
:
[]
*
extensions
.
ReplicaSet
{
&
newRS
,
&
oldRS
},
expected
:
[]
*
extensions
.
ReplicaSet
{
&
oldRS
},
expectedRequire
:
nil
,
},
{
test
:
"Get old ReplicaSets with no new ReplicaSet"
,
Name
:
"Get old ReplicaSets with no new ReplicaSet"
,
deployment
:
deployment
,
rsList
:
[]
*
extensions
.
ReplicaSet
{
&
oldRS
},
expected
:
[]
*
extensions
.
ReplicaSet
{
&
oldRS
},
expectedRequire
:
nil
,
},
{
test
:
"Get old ReplicaSets with two new ReplicaSets, only the oldest new ReplicaSet is seen as new ReplicaSet"
,
Name
:
"Get old ReplicaSets with two new ReplicaSets, only the oldest new ReplicaSet is seen as new ReplicaSet"
,
deployment
:
deployment
,
rsList
:
[]
*
extensions
.
ReplicaSet
{
&
oldRS
,
&
newRS
,
&
newRSDup
},
expected
:
[]
*
extensions
.
ReplicaSet
{
&
oldRS
,
&
newRS
},
expectedRequire
:
[]
*
extensions
.
ReplicaSet
{
&
newRS
},
},
{
test
:
"Get empty old ReplicaSets"
,
Name
:
"Get empty old ReplicaSets"
,
deployment
:
deployment
,
rsList
:
[]
*
extensions
.
ReplicaSet
{
&
newRS
},
expected
:
nil
,
...
...
@@ -581,16 +590,18 @@ func TestFindOldReplicaSets(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
Name
,
func
(
t
*
testing
.
T
)
{
requireRS
,
allRS
,
err
:=
FindOldReplicaSets
(
&
test
.
deployment
,
test
.
rsList
)
sort
.
Sort
(
controller
.
ReplicaSetsByCreationTimestamp
(
allRS
))
sort
.
Sort
(
controller
.
ReplicaSetsByCreationTimestamp
(
test
.
expected
))
if
!
reflect
.
DeepEqual
(
allRS
,
test
.
expected
)
||
err
!=
nil
{
t
.
Errorf
(
"In test case %q, expected %#v, got %#v: %v"
,
test
.
test
,
test
.
expected
,
allRS
,
err
)
t
.
Errorf
(
"In test case %q, expected %#v, got %#v: %v"
,
test
.
Name
,
test
.
expected
,
allRS
,
err
)
}
// RSs are getting filtered correctly by rs.spec.replicas
if
!
reflect
.
DeepEqual
(
requireRS
,
test
.
expectedRequire
)
||
err
!=
nil
{
t
.
Errorf
(
"In test case %q, expected %#v, got %#v: %v"
,
test
.
test
,
test
.
expectedRequire
,
requireRS
,
err
)
t
.
Errorf
(
"In test case %q, expected %#v, got %#v: %v"
,
test
.
Name
,
test
.
expectedRequire
,
requireRS
,
err
)
}
})
}
}
...
...
@@ -623,7 +634,7 @@ func TestGetReplicaCountForReplicaSets(t *testing.T) {
rs2
.
Status
.
Replicas
=
3
tests
:=
[]
struct
{
test
string
Name
string
sets
[]
*
extensions
.
ReplicaSet
expectedCount
int32
expectedActual
int32
...
...
@@ -643,14 +654,16 @@ func TestGetReplicaCountForReplicaSets(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
Name
,
func
(
t
*
testing
.
T
)
{
rs
:=
GetReplicaCountForReplicaSets
(
test
.
sets
)
if
rs
!=
test
.
expectedCount
{
t
.
Errorf
(
"In test case %s, expectedCount %+v, got %+v"
,
test
.
test
,
test
.
expectedCount
,
rs
)
t
.
Errorf
(
"In test case %s, expectedCount %+v, got %+v"
,
test
.
Name
,
test
.
expectedCount
,
rs
)
}
rs
=
GetActualReplicaCountForReplicaSets
(
test
.
sets
)
if
rs
!=
test
.
expectedActual
{
t
.
Errorf
(
"In test case %s, expectedActual %+v, got %+v"
,
test
.
test
,
test
.
expectedActual
,
rs
)
t
.
Errorf
(
"In test case %s, expectedActual %+v, got %+v"
,
test
.
Name
,
test
.
expectedActual
,
rs
)
}
})
}
}
...
...
@@ -698,6 +711,7 @@ func TestResolveFenceposts(t *testing.T) {
}
for
num
,
test
:=
range
tests
{
t
.
Run
(
"maxSurge="
+
test
.
maxSurge
,
func
(
t
*
testing
.
T
)
{
maxSurge
:=
intstr
.
FromString
(
test
.
maxSurge
)
maxUnavail
:=
intstr
.
FromString
(
test
.
maxUnavailable
)
surge
,
unavail
,
err
:=
ResolveFenceposts
(
&
maxSurge
,
&
maxUnavail
,
test
.
desired
)
...
...
@@ -710,12 +724,13 @@ func TestResolveFenceposts(t *testing.T) {
if
surge
!=
test
.
expectSurge
||
unavail
!=
test
.
expectUnavailable
{
t
.
Errorf
(
"#%v got %v:%v, want %v:%v"
,
num
,
surge
,
unavail
,
test
.
expectSurge
,
test
.
expectUnavailable
)
}
})
}
}
func
TestNewRSNewReplicas
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
test
string
Name
string
strategyType
extensions
.
DeploymentStrategyType
depReplicas
int32
newRSReplicas
int32
...
...
@@ -744,20 +759,28 @@ func TestNewRSNewReplicas(t *testing.T) {
*
(
rs5
.
Spec
.
Replicas
)
=
5
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
Name
,
func
(
t
*
testing
.
T
)
{
*
(
newDeployment
.
Spec
.
Replicas
)
=
test
.
depReplicas
newDeployment
.
Spec
.
Strategy
=
extensions
.
DeploymentStrategy
{
Type
:
test
.
strategyType
}
newDeployment
.
Spec
.
Strategy
.
RollingUpdate
=
&
extensions
.
RollingUpdateDeployment
{
MaxUnavailable
:
func
(
i
int
)
*
intstr
.
IntOrString
{
x
:=
intstr
.
FromInt
(
i
);
return
&
x
}(
1
),
MaxSurge
:
func
(
i
int
)
*
intstr
.
IntOrString
{
x
:=
intstr
.
FromInt
(
i
);
return
&
x
}(
test
.
maxSurge
),
MaxUnavailable
:
func
(
i
int
)
*
intstr
.
IntOrString
{
x
:=
intstr
.
FromInt
(
i
)
return
&
x
}(
1
),
MaxSurge
:
func
(
i
int
)
*
intstr
.
IntOrString
{
x
:=
intstr
.
FromInt
(
i
)
return
&
x
}(
test
.
maxSurge
),
}
*
(
newRC
.
Spec
.
Replicas
)
=
test
.
newRSReplicas
rs
,
err
:=
NewRSNewReplicas
(
&
newDeployment
,
[]
*
extensions
.
ReplicaSet
{
&
rs5
},
&
newRC
)
if
err
!=
nil
{
t
.
Errorf
(
"In test case %s, got unexpected error %v"
,
test
.
test
,
err
)
t
.
Errorf
(
"In test case %s, got unexpected error %v"
,
test
.
Name
,
err
)
}
if
rs
!=
test
.
expected
{
t
.
Errorf
(
"In test case %s, expected %+v, got %+v"
,
test
.
test
,
test
.
expected
,
rs
)
t
.
Errorf
(
"In test case %s, expected %+v, got %+v"
,
test
.
Name
,
test
.
expected
,
rs
)
}
})
}
}
...
...
@@ -825,11 +848,13 @@ func TestGetCondition(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
cond
:=
GetDeploymentCondition
(
test
.
status
,
test
.
condType
)
exists
:=
cond
!=
nil
if
exists
!=
test
.
expected
{
t
.
Errorf
(
"%s: expected condition to exist: %t, got: %t"
,
test
.
name
,
test
.
expected
,
exists
)
}
})
}
}
...
...
@@ -869,10 +894,12 @@ func TestSetCondition(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
SetDeploymentCondition
(
test
.
status
,
test
.
cond
)
if
!
reflect
.
DeepEqual
(
test
.
status
,
test
.
expectedStatus
)
{
t
.
Errorf
(
"%s: expected status: %v, got: %v"
,
test
.
name
,
test
.
expectedStatus
,
test
.
status
)
}
})
}
}
...
...
@@ -912,10 +939,12 @@ func TestRemoveCondition(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
RemoveDeploymentCondition
(
test
.
status
,
test
.
condType
)
if
!
reflect
.
DeepEqual
(
test
.
status
,
test
.
expectedStatus
)
{
t
.
Errorf
(
"%s: expected status: %v, got: %v"
,
test
.
name
,
test
.
expectedStatus
,
test
.
status
)
}
})
}
}
...
...
@@ -988,11 +1017,11 @@ func TestDeploymentComplete(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Log
(
test
.
name
)
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
if
got
,
exp
:=
DeploymentComplete
(
test
.
d
,
&
test
.
d
.
Status
),
test
.
expected
;
got
!=
exp
{
t
.
Errorf
(
"expected complete: %t, got: %t"
,
exp
,
got
)
}
})
}
}
...
...
@@ -1083,11 +1112,11 @@ func TestDeploymentProgressing(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Log
(
test
.
name
)
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
if
got
,
exp
:=
DeploymentProgressing
(
test
.
d
,
&
test
.
newStatus
),
test
.
expected
;
got
!=
exp
{
t
.
Errorf
(
"expected progressing: %t, got: %t"
,
exp
,
got
)
}
})
}
}
...
...
@@ -1149,12 +1178,12 @@ func TestDeploymentTimedOut(t *testing.T) {
}
for
_
,
test
:=
range
tests
{
t
.
Log
(
test
.
name
)
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
nowFn
=
test
.
nowFn
if
got
,
exp
:=
DeploymentTimedOut
(
&
test
.
d
,
&
test
.
d
.
Status
),
test
.
expected
;
got
!=
exp
{
t
.
Errorf
(
"expected timeout: %t, got: %t"
,
exp
,
got
)
}
})
}
}
...
...
@@ -1228,9 +1257,70 @@ func TestMaxUnavailable(t *testing.T) {
for
_
,
test
:=
range
tests
{
t
.
Log
(
test
.
name
)
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
maxUnavailable
:=
MaxUnavailable
(
test
.
deployment
)
if
test
.
expected
!=
maxUnavailable
{
t
.
Fatalf
(
"expected:%v, got:%v"
,
test
.
expected
,
maxUnavailable
)
}
})
}
}
//Set of simple tests for annotation related util functions
func
TestAnnotationUtils
(
t
*
testing
.
T
)
{
//Setup
tDeployment
:=
generateDeployment
(
"nginx"
)
tRS
:=
generateRS
(
tDeployment
)
tDeployment
.
Annotations
[
RevisionAnnotation
]
=
"1"
//Test Case 1: Check if anotations are copied properly from deployment to RS
t
.
Run
(
"SetNewReplicaSetAnnotations"
,
func
(
t
*
testing
.
T
)
{
//Try to set the increment revision from 1 through 20
for
i
:=
0
;
i
<
20
;
i
++
{
nextRevision
:=
fmt
.
Sprintf
(
"%d"
,
i
+
1
)
SetNewReplicaSetAnnotations
(
&
tDeployment
,
&
tRS
,
nextRevision
,
true
)
//Now the ReplicaSets Revision Annotation should be i+1
if
tRS
.
Annotations
[
RevisionAnnotation
]
!=
nextRevision
{
t
.
Errorf
(
"Revision Expected=%s Obtained=%s"
,
nextRevision
,
tRS
.
Annotations
[
RevisionAnnotation
])
}
}
})
//Test Case 2: Check if annotations are set properly
t
.
Run
(
"SetReplicasAnnotations"
,
func
(
t
*
testing
.
T
)
{
updated
:=
SetReplicasAnnotations
(
&
tRS
,
10
,
11
)
if
!
updated
{
t
.
Errorf
(
"SetReplicasAnnotations() failed"
)
}
value
,
ok
:=
tRS
.
Annotations
[
DesiredReplicasAnnotation
]
if
!
ok
{
t
.
Errorf
(
"SetReplicasAnnotations did not set DesiredReplicasAnnotation"
)
}
if
value
!=
"10"
{
t
.
Errorf
(
"SetReplicasAnnotations did not set DesiredReplicasAnnotation correctly value=%s"
,
value
)
}
if
value
,
ok
=
tRS
.
Annotations
[
MaxReplicasAnnotation
];
!
ok
{
t
.
Errorf
(
"SetReplicasAnnotations did not set DesiredReplicasAnnotation"
)
}
if
value
!=
"11"
{
t
.
Errorf
(
"SetReplicasAnnotations did not set MaxReplicasAnnotation correctly value=%s"
,
value
)
}
})
//Test Case 3: Check if annotations reflect deployments state
tRS
.
Annotations
[
DesiredReplicasAnnotation
]
=
"1"
tRS
.
Status
.
AvailableReplicas
=
1
tRS
.
Spec
.
Replicas
=
new
(
int32
)
*
tRS
.
Spec
.
Replicas
=
1
t
.
Run
(
"IsSaturated"
,
func
(
t
*
testing
.
T
)
{
saturated
:=
IsSaturated
(
&
tDeployment
,
&
tRS
)
if
!
saturated
{
t
.
Errorf
(
"SetReplicasAnnotations Expected=true Obtained=false"
)
}
})
//Tear Down
}
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