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
2dd5b2b1
Commit
2dd5b2b1
authored
Oct 12, 2015
by
Dawn Chen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14579 from derekwaynecarr/testing_rc_strategy
Add testing to ReplicationController strategy
parents
6500fdeb
22dd92f6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
140 additions
and
0 deletions
+140
-0
strategy_test.go
pkg/registry/controller/strategy_test.go
+140
-0
No files found.
pkg/registry/controller/strategy_test.go
0 → 100644
View file @
2dd5b2b1
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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
controller
import
(
"testing"
"k8s.io/kubernetes/pkg/api"
)
func
TestControllerStrategy
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
if
!
Strategy
.
NamespaceScoped
()
{
t
.
Errorf
(
"ReplicationController must be namespace scoped"
)
}
if
Strategy
.
AllowCreateOnUpdate
()
{
t
.
Errorf
(
"ReplicationController should not allow create on update"
)
}
validSelector
:=
map
[
string
]
string
{
"a"
:
"b"
}
validPodTemplate
:=
api
.
PodTemplate
{
Template
:
api
.
PodTemplateSpec
{
ObjectMeta
:
api
.
ObjectMeta
{
Labels
:
validSelector
,
},
Spec
:
api
.
PodSpec
{
RestartPolicy
:
api
.
RestartPolicyAlways
,
DNSPolicy
:
api
.
DNSClusterFirst
,
Containers
:
[]
api
.
Container
{{
Name
:
"abc"
,
Image
:
"image"
,
ImagePullPolicy
:
"IfNotPresent"
}},
},
},
}
rc
:=
&
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"abc"
,
Namespace
:
api
.
NamespaceDefault
},
Spec
:
api
.
ReplicationControllerSpec
{
Selector
:
validSelector
,
Template
:
&
validPodTemplate
.
Template
,
},
Status
:
api
.
ReplicationControllerStatus
{
Replicas
:
1
,
ObservedGeneration
:
int64
(
10
),
},
}
Strategy
.
PrepareForCreate
(
rc
)
if
rc
.
Status
.
Replicas
!=
0
{
t
.
Error
(
"ReplicationController should not allow setting status.replicas on create"
)
}
if
rc
.
Status
.
ObservedGeneration
!=
int64
(
0
)
{
t
.
Error
(
"ReplicationController should not allow setting status.observedGeneration on create"
)
}
errs
:=
Strategy
.
Validate
(
ctx
,
rc
)
if
len
(
errs
)
!=
0
{
t
.
Errorf
(
"Unexpected error validating %v"
,
errs
)
}
invalidRc
:=
&
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
ResourceVersion
:
"4"
},
}
Strategy
.
PrepareForUpdate
(
invalidRc
,
rc
)
errs
=
Strategy
.
ValidateUpdate
(
ctx
,
invalidRc
,
rc
)
if
len
(
errs
)
==
0
{
t
.
Errorf
(
"Expected a validation error"
)
}
if
invalidRc
.
ResourceVersion
!=
"4"
{
t
.
Errorf
(
"Incoming resource version on update should not be mutated"
)
}
}
func
TestControllerStatusStrategy
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
if
!
StatusStrategy
.
NamespaceScoped
()
{
t
.
Errorf
(
"ReplicationController must be namespace scoped"
)
}
if
StatusStrategy
.
AllowCreateOnUpdate
()
{
t
.
Errorf
(
"ReplicationController should not allow create on update"
)
}
validSelector
:=
map
[
string
]
string
{
"a"
:
"b"
}
validPodTemplate
:=
api
.
PodTemplate
{
Template
:
api
.
PodTemplateSpec
{
ObjectMeta
:
api
.
ObjectMeta
{
Labels
:
validSelector
,
},
Spec
:
api
.
PodSpec
{
RestartPolicy
:
api
.
RestartPolicyAlways
,
DNSPolicy
:
api
.
DNSClusterFirst
,
Containers
:
[]
api
.
Container
{{
Name
:
"abc"
,
Image
:
"image"
,
ImagePullPolicy
:
"IfNotPresent"
}},
},
},
}
oldController
:=
&
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"abc"
,
Namespace
:
api
.
NamespaceDefault
,
ResourceVersion
:
"10"
},
Spec
:
api
.
ReplicationControllerSpec
{
Replicas
:
3
,
Selector
:
validSelector
,
Template
:
&
validPodTemplate
.
Template
,
},
Status
:
api
.
ReplicationControllerStatus
{
Replicas
:
1
,
ObservedGeneration
:
int64
(
10
),
},
}
newController
:=
&
api
.
ReplicationController
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"abc"
,
Namespace
:
api
.
NamespaceDefault
,
ResourceVersion
:
"9"
},
Spec
:
api
.
ReplicationControllerSpec
{
Replicas
:
1
,
Selector
:
validSelector
,
Template
:
&
validPodTemplate
.
Template
,
},
Status
:
api
.
ReplicationControllerStatus
{
Replicas
:
3
,
ObservedGeneration
:
int64
(
11
),
},
}
StatusStrategy
.
PrepareForUpdate
(
newController
,
oldController
)
if
newController
.
Status
.
Replicas
!=
3
{
t
.
Errorf
(
"Replication controller status updates should allow change of replicas: %v"
,
newController
.
Status
.
Replicas
)
}
if
newController
.
Spec
.
Replicas
!=
3
{
t
.
Errorf
(
"PrepareForUpdate should have preferred spec"
)
}
errs
:=
StatusStrategy
.
ValidateUpdate
(
ctx
,
newController
,
oldController
)
if
len
(
errs
)
!=
0
{
t
.
Errorf
(
"Unexpected error %v"
,
errs
)
}
}
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