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
781de2f5
Commit
781de2f5
authored
Sep 02, 2015
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13432 from wojtek-t/etcd_test_framework
Create registrytest support for etcd test to reduce amount of boilerplate in tests
parents
0eee5c8b
a4baf4c2
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
344 additions
and
858 deletions
+344
-858
etcd_test.go
pkg/registry/controller/etcd/etcd_test.go
+10
-43
etcd_test.go
pkg/registry/daemon/etcd/etcd_test.go
+11
-55
etcd_test.go
pkg/registry/deployment/etcd/etcd_test.go
+29
-224
etcd_test.go
pkg/registry/endpoint/etcd/etcd_test.go
+8
-33
etcd_test.go
pkg/registry/horizontalpodautoscaler/etcd/etcd_test.go
+6
-31
etcd_test.go
pkg/registry/limitrange/etcd/etcd_test.go
+14
-18
strategy.go
pkg/registry/limitrange/strategy.go
+16
-4
etcd.go
pkg/registry/minion/etcd/etcd.go
+1
-1
etcd_test.go
pkg/registry/minion/etcd/etcd_test.go
+9
-44
etcd_test.go
pkg/registry/namespace/etcd/etcd_test.go
+5
-37
etcd_test.go
pkg/registry/persistentvolume/etcd/etcd_test.go
+8
-33
etcd_test.go
pkg/registry/persistentvolumeclaim/etcd/etcd_test.go
+8
-33
etcd.go
pkg/registry/pod/etcd/etcd.go
+7
-7
etcd_test.go
pkg/registry/pod/etcd/etcd_test.go
+9
-42
etcd.go
pkg/registry/podtemplate/etcd/etcd.go
+2
-2
etcd_test.go
pkg/registry/podtemplate/etcd/etcd_test.go
+14
-18
etcd.go
pkg/registry/registrytest/etcd.go
+125
-33
etcd_test.go
pkg/registry/resourcequota/etcd/etcd_test.go
+8
-42
etcd_test.go
pkg/registry/secret/etcd/etcd_test.go
+14
-18
etcd.go
pkg/registry/service/etcd/etcd.go
+2
-2
etcd_test.go
pkg/registry/service/etcd/etcd_test.go
+15
-29
etcd_test.go
pkg/registry/serviceaccount/etcd/etcd_test.go
+14
-18
etcd_test.go
pkg/registry/thirdpartyresource/etcd/etcd_test.go
+9
-91
No files found.
pkg/registry/controller/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -79,18 +79,12 @@ var validController = validNewController()
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
controller
:=
validNewController
()
controller
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
controller
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid (invalid selector)
&
api
.
ReplicationController
{
Spec
:
api
.
ReplicationControllerSpec
{
...
...
@@ -104,20 +98,11 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewController
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
// valid updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
ReplicationController
)
object
.
Spec
.
Replicas
=
object
.
Spec
.
Replicas
+
1
...
...
@@ -191,40 +176,23 @@ func TestGenerationNumber(t *testing.T) {
}
}
func
Test
EtcdGetController
(
t
*
testing
.
T
)
{
func
Test
Get
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewController
())
}
func
Test
EtcdListControllers
(
t
*
testing
.
T
)
{
func
Test
List
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
test
.
TestList
(
validNewController
(),
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewController
())
}
func
TestWatch
Controllers
(
t
*
testing
.
T
)
{
func
TestWatch
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestWatch
(
validController
,
func
()
{
fakeClient
.
WaitForWatchCompletion
()
},
func
(
err
error
)
{
fakeClient
.
WatchInjectError
<-
err
},
func
(
obj
runtime
.
Object
,
action
string
)
error
{
return
registrytest
.
EmitObject
(
fakeClient
,
obj
,
action
)
},
// matching labels
[]
labels
.
Set
{
{
"a"
:
"b"
},
...
...
@@ -248,7 +216,6 @@ func TestWatchControllers(t *testing.T) {
{
"status.replicas"
:
"10"
,
"metadata.name"
:
"foo"
},
{
"status.replicas"
:
"0"
,
"metadata.name"
:
"bar"
},
},
registrytest
.
WatchActions
,
)
}
...
...
pkg/registry/daemon/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -37,17 +37,6 @@ func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
return
NewREST
(
etcdStorage
),
fakeClient
}
// createController is a helper function that returns a controller with the updated resource version.
func
createController
(
storage
*
REST
,
dc
expapi
.
Daemon
,
t
*
testing
.
T
)
(
expapi
.
Daemon
,
error
)
{
ctx
:=
api
.
WithNamespace
(
api
.
NewContext
(),
dc
.
Namespace
)
obj
,
err
:=
storage
.
Create
(
ctx
,
&
dc
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to create controller, %v"
,
err
)
}
newDc
:=
obj
.
(
*
expapi
.
Daemon
)
return
*
newDc
,
nil
}
func
validNewDaemon
()
*
expapi
.
Daemon
{
return
&
expapi
.
Daemon
{
ObjectMeta
:
api
.
ObjectMeta
{
...
...
@@ -80,18 +69,12 @@ var validDaemon = validNewDaemon()
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
daemon
:=
validNewDaemon
()
daemon
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
daemon
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid (invalid selector)
&
expapi
.
Daemon
{
Spec
:
expapi
.
DaemonSpec
{
...
...
@@ -104,19 +87,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewDaemon
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
expapi
.
Daemon
)
...
...
@@ -147,40 +121,23 @@ func TestUpdate(t *testing.T) {
)
}
func
Test
EtcdGetController
(
t
*
testing
.
T
)
{
func
Test
Get
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewDaemon
())
}
func
Test
EtcdListControllers
(
t
*
testing
.
T
)
{
func
Test
List
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
test
.
TestList
(
validNewDaemon
(),
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewDaemon
())
}
func
TestWatch
Controllers
(
t
*
testing
.
T
)
{
func
TestWatch
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestWatch
(
validDaemon
,
func
()
{
fakeClient
.
WaitForWatchCompletion
()
},
func
(
err
error
)
{
fakeClient
.
WatchInjectError
<-
err
},
func
(
obj
runtime
.
Object
,
action
string
)
error
{
return
registrytest
.
EmitObject
(
fakeClient
,
obj
,
action
)
},
// matching labels
[]
labels
.
Set
{
{
"a"
:
"b"
},
...
...
@@ -199,11 +156,10 @@ func TestWatchControllers(t *testing.T) {
{
"metadata.name"
:
"bar"
},
{
"name"
:
"foo"
},
},
registrytest
.
WatchActions
,
)
}
func
TestEtcdDelete
Controller
(
t
*
testing
.
T
)
{
func
TestEtcdDelete
Daemon
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
storage
,
fakeClient
:=
newStorage
(
t
)
key
,
err
:=
storage
.
KeyFunc
(
ctx
,
validDaemon
.
Name
)
...
...
@@ -248,7 +204,7 @@ func TestDelete(t *testing.T) {
return
dc
}
gracefulSetFn
:=
func
()
bool
{
// If the
controller
is still around after trying to delete either the delete
// If the
daemon
is still around after trying to delete either the delete
// failed, or we're deleting it gracefully.
if
fakeClient
.
Data
[
key
]
.
R
.
Node
!=
nil
{
return
true
...
...
pkg/registry/deployment/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -18,7 +18,6 @@ package etcd
import
(
"testing"
"time"
"github.com/coreos/go-etcd/etcd"
"k8s.io/kubernetes/pkg/api"
...
...
@@ -27,19 +26,12 @@ import (
"k8s.io/kubernetes/pkg/expapi"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
etcdgeneric
"k8s.io/kubernetes/pkg/registry/generic/etcd"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
etcdstorage
"k8s.io/kubernetes/pkg/storage/etcd"
"k8s.io/kubernetes/pkg/tools"
"k8s.io/kubernetes/pkg/tools/etcdtest"
)
const
(
PASS
=
iota
FAIL
)
func
newStorage
(
t
*
testing
.
T
)
(
*
REST
,
*
tools
.
FakeEtcdClient
)
{
etcdStorage
,
fakeClient
:=
registrytest
.
NewEtcdStorage
(
t
)
return
NewREST
(
etcdStorage
),
fakeClient
...
...
@@ -77,18 +69,12 @@ var validDeployment = *validNewDeployment()
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
deployment
:=
validNewDeployment
()
deployment
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
deployment
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid (invalid selector)
&
expapi
.
Deployment
{
Spec
:
expapi
.
DeploymentSpec
{
...
...
@@ -101,19 +87,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewDeployment
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
expapi
.
Deployment
)
...
...
@@ -144,24 +121,40 @@ func TestUpdate(t *testing.T) {
)
}
func
Test
Etcd
Get
(
t
*
testing
.
T
)
{
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewDeployment
())
}
func
Test
Etcd
List
(
t
*
testing
.
T
)
{
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
test
.
TestList
(
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewDeployment
())
}
func
TestWatch
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestWatch
(
validNewDeployment
(),
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
// matching labels
[]
labels
.
Set
{},
// not matching labels
[]
labels
.
Set
{
{
"a"
:
"c"
},
{
"foo"
:
"bar"
},
},
// matching fields
[]
fields
.
Set
{
{
"metadata.name"
:
"foo"
},
},
// not matchin fields
[]
fields
.
Set
{
{
"metadata.name"
:
"bar"
},
{
"name"
:
"foo"
},
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
)
}
func
TestEtcdDelete
(
t
*
testing
.
T
)
{
...
...
@@ -188,194 +181,6 @@ func TestEtcdDelete(t *testing.T) {
}
}
func
TestEtcdWatch
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
storage
,
fakeClient
:=
newStorage
(
t
)
watching
,
err
:=
storage
.
Watch
(
ctx
,
labels
.
Everything
(),
fields
.
Everything
(),
"1"
,
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
fakeClient
.
WaitForWatchCompletion
()
select
{
case
_
,
ok
:=
<-
watching
.
ResultChan
()
:
if
!
ok
{
t
.
Errorf
(
"watching channel should be open"
)
}
default
:
}
fakeClient
.
WatchInjectError
<-
nil
if
_
,
ok
:=
<-
watching
.
ResultChan
();
ok
{
t
.
Errorf
(
"watching channel should be closed"
)
}
watching
.
Stop
()
}
// Tests that we can watch for the creation of Deployment with specified labels.
func
TestEtcdWatchWithLabels
(
t
*
testing
.
T
)
{
ctx
:=
api
.
WithNamespace
(
api
.
NewDefaultContext
(),
validDeployment
.
Namespace
)
storage
,
fakeClient
:=
newStorage
(
t
)
fakeClient
.
ExpectNotFoundGet
(
etcdgeneric
.
NamespaceKeyRootFunc
(
ctx
,
"/registry/pods"
))
watching
,
err
:=
storage
.
Watch
(
ctx
,
labels
.
SelectorFromSet
(
validDeployment
.
Spec
.
Selector
),
fields
.
Everything
(),
"1"
,
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
fakeClient
.
WaitForWatchCompletion
()
// The watcher above is waiting for these Labels, on receiving them it should
// apply the deploymentStatus decorator, which lists pods, causing a query against
// the /registry/pods endpoint of the etcd client.
deployment
:=
&
expapi
.
Deployment
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Labels
:
validDeployment
.
Spec
.
Selector
,
Namespace
:
"default"
,
},
}
deploymentBytes
,
_
:=
testapi
.
Codec
()
.
Encode
(
deployment
)
fakeClient
.
WatchResponse
<-
&
etcd
.
Response
{
Action
:
"create"
,
Node
:
&
etcd
.
Node
{
Value
:
string
(
deploymentBytes
),
},
}
select
{
case
_
,
ok
:=
<-
watching
.
ResultChan
()
:
if
!
ok
{
t
.
Errorf
(
"watching channel should be open"
)
}
case
<-
time
.
After
(
time
.
Millisecond
*
100
)
:
t
.
Error
(
"unexpected timeout from result channel"
)
}
watching
.
Stop
()
}
// Tests that we can watch for Deployment with specified fields.
func
TestEtcdWatchWithFields
(
t
*
testing
.
T
)
{
ctx
:=
api
.
WithNamespace
(
api
.
NewDefaultContext
(),
validDeployment
.
Namespace
)
storage
,
fakeClient
:=
newStorage
(
t
)
fakeClient
.
ExpectNotFoundGet
(
etcdgeneric
.
NamespaceKeyRootFunc
(
ctx
,
"/registry/pods"
))
testFieldMap
:=
map
[
int
][]
fields
.
Set
{
PASS
:
{
{
"metadata.name"
:
"foo"
},
},
FAIL
:
{
{
"metadata.name"
:
"bar"
},
{
"name"
:
"foo"
},
},
}
testEtcdActions
:=
[]
string
{
etcdstorage
.
EtcdCreate
,
etcdstorage
.
EtcdSet
,
etcdstorage
.
EtcdCAS
,
etcdstorage
.
EtcdDelete
}
deployment
:=
&
expapi
.
Deployment
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Labels
:
validDeployment
.
Spec
.
Selector
,
Namespace
:
"default"
,
},
Status
:
expapi
.
DeploymentStatus
{
Replicas
:
1
,
UpdatedReplicas
:
4
,
},
}
deploymentBytes
,
_
:=
testapi
.
Codec
()
.
Encode
(
deployment
)
for
expectedResult
,
fieldSet
:=
range
testFieldMap
{
for
_
,
field
:=
range
fieldSet
{
for
_
,
action
:=
range
testEtcdActions
{
watching
,
err
:=
storage
.
Watch
(
ctx
,
labels
.
Everything
(),
field
.
AsSelector
(),
"1"
,
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
var
prevNode
*
etcd
.
Node
=
nil
node
:=
&
etcd
.
Node
{
Value
:
string
(
deploymentBytes
),
}
if
action
==
etcdstorage
.
EtcdDelete
{
prevNode
=
node
}
fakeClient
.
WaitForWatchCompletion
()
fakeClient
.
WatchResponse
<-
&
etcd
.
Response
{
Action
:
action
,
Node
:
node
,
PrevNode
:
prevNode
,
}
select
{
case
r
,
ok
:=
<-
watching
.
ResultChan
()
:
if
expectedResult
==
FAIL
{
t
.
Errorf
(
"Unexpected result from channel %#v. Field: %v"
,
r
,
field
)
}
if
!
ok
{
t
.
Errorf
(
"watching channel should be open"
)
}
case
<-
time
.
After
(
time
.
Millisecond
*
100
)
:
if
expectedResult
==
PASS
{
t
.
Error
(
"unexpected timeout from result channel"
)
}
}
watching
.
Stop
()
}
}
}
}
func
TestEtcdWatchNotMatch
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
storage
,
fakeClient
:=
newStorage
(
t
)
fakeClient
.
ExpectNotFoundGet
(
etcdgeneric
.
NamespaceKeyRootFunc
(
ctx
,
"/registry/pods"
))
watching
,
err
:=
storage
.
Watch
(
ctx
,
labels
.
SelectorFromSet
(
labels
.
Set
{
"name"
:
"foo"
}),
fields
.
Everything
(),
"1"
,
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
fakeClient
.
WaitForWatchCompletion
()
deployment
:=
&
expapi
.
Deployment
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
Labels
:
map
[
string
]
string
{
"name"
:
"bar"
,
},
},
}
deploymentBytes
,
_
:=
testapi
.
Codec
()
.
Encode
(
deployment
)
fakeClient
.
WatchResponse
<-
&
etcd
.
Response
{
Action
:
"create"
,
Node
:
&
etcd
.
Node
{
Value
:
string
(
deploymentBytes
),
},
}
select
{
case
<-
watching
.
ResultChan
()
:
t
.
Error
(
"unexpected result from result channel"
)
case
<-
time
.
After
(
time
.
Millisecond
*
100
)
:
// expected case
}
}
func
TestDelete
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
storage
,
fakeClient
:=
newStorage
(
t
)
...
...
pkg/registry/endpoint/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -61,18 +61,12 @@ func validChangedEndpoints() *api.Endpoints {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
endpoints
:=
validNewEndpoints
()
endpoints
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
endpoints
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
Endpoints
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"_-a123-a_"
},
...
...
@@ -82,19 +76,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
AllowCreateOnUpdate
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
AllowCreateOnUpdate
()
test
.
TestUpdate
(
// valid
validNewEndpoints
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
Endpoints
)
...
...
@@ -135,26 +120,16 @@ func TestDelete(t *testing.T) {
test
.
TestDelete
(
createFn
,
gracefulSetFn
)
}
func
Test
EtcdGetEndpoints
(
t
*
testing
.
T
)
{
func
Test
Get
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
endpoints
:=
validNewEndpoints
()
test
.
TestGet
(
endpoints
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewEndpoints
())
}
func
Test
EtcdListEndpoints
(
t
*
testing
.
T
)
{
func
Test
List
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
endpoints
:=
validNewEndpoints
()
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
test
.
TestList
(
endpoints
,
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewEndpoints
())
}
func
TestEndpointsDecode
(
t
*
testing
.
T
)
{
...
...
pkg/registry/horizontalpodautoscaler/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -58,18 +58,12 @@ func validNewHorizontalPodAutoscaler(name string) *expapi.HorizontalPodAutoscale
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
autoscaler
:=
validNewHorizontalPodAutoscaler
(
"foo"
)
autoscaler
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
autoscaler
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
expapi
.
HorizontalPodAutoscaler
{},
)
...
...
@@ -77,19 +71,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewHorizontalPodAutoscaler
(
"foo"
),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
expapi
.
HorizontalPodAutoscaler
)
...
...
@@ -128,22 +113,12 @@ func TestDelete(t *testing.T) {
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
autoscaler
:=
validNewHorizontalPodAutoscaler
(
"foo"
)
test
.
TestGet
(
autoscaler
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewHorizontalPodAutoscaler
(
"foo"
))
}
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
autoscaler
:=
validNewHorizontalPodAutoscaler
(
"foo"
)
test
.
TestList
(
autoscaler
,
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewHorizontalPodAutoscaler
(
"foo"
))
}
pkg/registry/limitrange/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -21,7 +21,6 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/tools"
...
...
@@ -58,18 +57,12 @@ func validNewLimitRange() *api.LimitRange {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
GeneratesName
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
GeneratesName
()
validLimitRange
:=
validNewLimitRange
()
validLimitRange
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
validLimitRange
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
LimitRange
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"_-a123-a_"
},
...
...
@@ -79,19 +72,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
AllowCreateOnUpdate
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
AllowCreateOnUpdate
()
test
.
TestUpdate
(
// valid
validNewLimitRange
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
LimitRange
)
...
...
@@ -112,3 +96,15 @@ func TestUpdate(t *testing.T) {
},
)
}
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewLimitRange
())
}
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewLimitRange
())
}
pkg/registry/limitrange/strategy.go
View file @
781de2f5
...
...
@@ -17,6 +17,8 @@ limitations under the License.
package
limitrange
import
(
"fmt"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/fields"
...
...
@@ -68,10 +70,20 @@ func (limitrangeStrategy) AllowUnconditionalUpdate() bool {
return
true
}
func
MatchLimitRange
(
label
labels
.
Selector
,
field
fields
.
Selector
)
generic
.
Matcher
{
return
&
generic
.
SelectionPredicate
{
Label
:
label
,
Field
:
field
,
GetAttrs
:
getAttrs
}
func
LimitRangeToSelectableFields
(
limitRange
*
api
.
LimitRange
)
fields
.
Set
{
return
fields
.
Set
{
}
}
func
getAttrs
(
obj
runtime
.
Object
)
(
objLabels
labels
.
Set
,
objFields
fields
.
Set
,
err
error
)
{
return
labels
.
Set
{},
fields
.
Set
{},
nil
func
MatchLimitRange
(
label
labels
.
Selector
,
field
fields
.
Selector
)
generic
.
Matcher
{
return
&
generic
.
SelectionPredicate
{
Label
:
label
,
Field
:
field
,
GetAttrs
:
func
(
obj
runtime
.
Object
)
(
labels
.
Set
,
fields
.
Set
,
error
)
{
lr
,
ok
:=
obj
.
(
*
api
.
LimitRange
)
if
!
ok
{
return
nil
,
nil
,
fmt
.
Errorf
(
"given object is not a limit range."
)
}
return
labels
.
Set
(
lr
.
ObjectMeta
.
Labels
),
LimitRangeToSelectableFields
(
lr
),
nil
},
}
}
pkg/registry/minion/etcd/etcd.go
View file @
781de2f5
...
...
@@ -48,7 +48,7 @@ func (r *StatusREST) Update(ctx api.Context, obj runtime.Object) (runtime.Object
return
r
.
store
.
Update
(
ctx
,
obj
)
}
// New
Storage
returns a RESTStorage object that will work against nodes.
// New
REST
returns a RESTStorage object that will work against nodes.
func
NewREST
(
s
storage
.
Interface
,
useCacher
bool
,
connection
client
.
ConnectionInfoGetter
)
(
*
REST
,
*
StatusREST
)
{
prefix
:=
"/minions"
...
...
pkg/registry/minion/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -75,18 +75,12 @@ func validChangedNode() *api.Node {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
node
:=
validNewNode
()
node
.
ObjectMeta
=
api
.
ObjectMeta
{
GenerateName
:
"foo"
}
test
.
TestCreate
(
// valid
node
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
Node
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"_-a123-a_"
},
...
...
@@ -96,19 +90,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestUpdate
(
// valid
validNewNode
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
Node
)
...
...
@@ -146,42 +131,23 @@ func TestDelete(t *testing.T) {
test
.
TestDelete
(
createFn
,
gracefulSetFn
)
}
func
Test
EtcdGetNode
(
t
*
testing
.
T
)
{
func
Test
Get
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
node
:=
validNewNode
()
test
.
TestGet
(
node
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestGet
(
validNewNode
())
}
func
Test
EtcdListNodes
(
t
*
testing
.
T
)
{
func
Test
List
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
node
:=
validNewNode
()
test
.
TestList
(
node
,
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestList
(
validNewNode
())
}
func
TestWatchNodes
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestWatch
(
validNewNode
(),
func
()
{
fakeClient
.
WaitForWatchCompletion
()
},
func
(
err
error
)
{
fakeClient
.
WatchInjectError
<-
err
},
func
(
obj
runtime
.
Object
,
action
string
)
error
{
return
registrytest
.
EmitObject
(
fakeClient
,
obj
,
action
)
},
// matching labels
[]
labels
.
Set
{
{
"name"
:
"foo"
},
...
...
@@ -199,7 +165,6 @@ func TestWatchNodes(t *testing.T) {
[]
fields
.
Set
{
{
"metadata.name"
:
"bar"
},
},
registrytest
.
WatchActions
,
)
}
...
...
pkg/registry/namespace/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -20,9 +20,7 @@ import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/registry/namespace"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/tools"
...
...
@@ -46,34 +44,14 @@ func validNewNamespace() *api.Namespace {
}
}
func
validChangedNamespace
()
*
api
.
Namespace
{
namespace
:=
validNewNamespace
()
namespace
.
ResourceVersion
=
"1"
namespace
.
Labels
=
map
[
string
]
string
{
"foo"
:
"bar"
,
}
return
namespace
}
func
TestStorage
(
t
*
testing
.
T
)
{
storage
,
_
:=
newStorage
(
t
)
namespace
.
NewRegistry
(
storage
)
}
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
namespace
:=
validNewNamespace
()
namespace
.
ObjectMeta
=
api
.
ObjectMeta
{
GenerateName
:
"foo"
}
test
.
TestCreate
(
// valid
namespace
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
Namespace
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bad value"
},
...
...
@@ -137,24 +115,14 @@ func TestNamespaceDecode(t *testing.T) {
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
namespace
:=
validNewNamespace
()
test
.
TestGet
(
namespace
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestGet
(
validNewNamespace
())
}
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
namespace
:=
validNewNamespace
()
test
.
TestList
(
namespace
,
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestList
(
validNewNamespace
())
}
func
TestDeleteNamespace
(
t
*
testing
.
T
)
{
...
...
pkg/registry/persistentvolume/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -70,18 +70,12 @@ func validChangedPersistentVolume() *api.PersistentVolume {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
pv
:=
validNewPersistentVolume
(
"foo"
)
pv
.
ObjectMeta
=
api
.
ObjectMeta
{
GenerateName
:
"foo"
}
test
.
TestCreate
(
// valid
pv
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
PersistentVolume
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"*BadName!"
},
...
...
@@ -91,19 +85,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestUpdate
(
// valid
validNewPersistentVolume
(
"foo"
),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
PersistentVolume
)
...
...
@@ -143,26 +128,16 @@ func TestDelete(t *testing.T) {
test
.
TestDelete
(
createFn
,
gracefulSetFn
)
}
func
Test
EtcdGetPersistentVolumes
(
t
*
testing
.
T
)
{
func
Test
Get
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
persistentVolume
:=
validNewPersistentVolume
(
"foo"
)
test
.
TestGet
(
persistentVolume
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestGet
(
validNewPersistentVolume
(
"foo"
))
}
func
Test
EtcdListPersistentVolumes
(
t
*
testing
.
T
)
{
func
Test
List
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
ClusterScope
()
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
persistentVolume
:=
validNewPersistentVolume
(
"foo"
)
test
.
TestList
(
persistentVolume
,
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
ClusterScope
()
test
.
TestList
(
validNewPersistentVolume
(
"foo"
))
}
func
TestPersistentVolumesDecode
(
t
*
testing
.
T
)
{
...
...
pkg/registry/persistentvolumeclaim/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -67,18 +67,12 @@ func validChangedPersistentVolumeClaim() *api.PersistentVolumeClaim {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
pv
:=
validNewPersistentVolumeClaim
(
"foo"
,
api
.
NamespaceDefault
)
pv
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
pv
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
PersistentVolumeClaim
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"*BadName!"
},
...
...
@@ -88,19 +82,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewPersistentVolumeClaim
(
"foo"
,
api
.
NamespaceDefault
),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
PersistentVolumeClaim
)
...
...
@@ -142,26 +127,16 @@ func TestDelete(t *testing.T) {
test
.
TestDelete
(
createFn
,
gracefulSetFn
)
}
func
Test
EtcdGetPersistentVolumeClaims
(
t
*
testing
.
T
)
{
func
Test
Get
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
claim
:=
validNewPersistentVolumeClaim
(
"foo"
,
api
.
NamespaceDefault
)
test
.
TestGet
(
claim
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewPersistentVolumeClaim
(
"foo"
,
api
.
NamespaceDefault
))
}
func
Test
EtcdListPersistentVolumeClaims
(
t
*
testing
.
T
)
{
func
Test
List
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
key
:=
etcdtest
.
AddPrefix
(
storage
.
KeyRootFunc
(
test
.
TestContext
()))
claim
:=
validNewPersistentVolumeClaim
(
"foo"
,
api
.
NamespaceDefault
)
test
.
TestList
(
claim
,
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewPersistentVolumeClaim
(
"foo"
,
api
.
NamespaceDefault
))
}
func
TestPersistentVolumeClaimsDecode
(
t
*
testing
.
T
)
{
...
...
pkg/registry/pod/etcd/etcd.go
View file @
781de2f5
...
...
@@ -53,7 +53,7 @@ type PodStorage struct {
// REST implements a RESTStorage for pods against etcd
type
REST
struct
{
etcdgeneric
.
Etcd
*
etcdgeneric
.
Etcd
}
// NewStorage returns a RESTStorage object that will work against pods.
...
...
@@ -92,19 +92,19 @@ func NewStorage(s storage.Interface, useCacher bool, k client.ConnectionInfoGett
},
EndpointName
:
"pods"
,
CreateStrategy
:
pod
.
Strategy
,
UpdateStrategy
:
pod
.
Strategy
,
DeleteStrategy
:
pod
.
Strategy
,
ReturnDeletedObject
:
true
,
Storage
:
storageInterface
,
}
statusStore
:=
*
store
store
.
CreateStrategy
=
pod
.
Strategy
store
.
UpdateStrategy
=
pod
.
Strategy
store
.
DeleteStrategy
=
pod
.
Strategy
store
.
ReturnDeletedObject
=
true
statusStore
.
UpdateStrategy
=
pod
.
StatusStrategy
return
PodStorage
{
Pod
:
&
REST
{
*
store
},
Pod
:
&
REST
{
store
},
Binding
:
&
BindingREST
{
store
:
store
},
Status
:
&
StatusREST
{
store
:
&
statusStore
},
Log
:
&
LogREST
{
store
:
store
,
kubeletConn
:
k
},
...
...
pkg/registry/pod/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -82,7 +82,7 @@ func validChangedPod() *api.Pod {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
_
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
pod
:=
validNewPod
()
pod
.
ObjectMeta
=
api
.
ObjectMeta
{}
// Make an invalid pod with an an incorrect label.
...
...
@@ -94,12 +94,6 @@ func TestCreate(t *testing.T) {
test
.
TestCreate
(
// valid
pod
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid (empty contains list)
&
api
.
Pod
{
Spec
:
api
.
PodSpec
{
...
...
@@ -113,19 +107,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
_
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewPod
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
Pod
)
...
...
@@ -383,40 +368,23 @@ func TestDeletePod(t *testing.T) {
}
}
func
Test
Etcd
Get
(
t
*
testing
.
T
)
{
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
_
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewPod
())
}
func
Test
Etcd
List
(
t
*
testing
.
T
)
{
func
TestList
(
t
*
testing
.
T
)
{
storage
,
_
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
key
:=
etcdtest
.
AddPrefix
(
storage
.
Etcd
.
KeyRootFunc
(
test
.
TestContext
()))
test
.
TestList
(
validNewPod
(),
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewPod
())
}
func
Test
Etcd
Watch
(
t
*
testing
.
T
)
{
func
TestWatch
(
t
*
testing
.
T
)
{
storage
,
_
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestWatch
(
validNewPod
(),
func
()
{
fakeClient
.
WaitForWatchCompletion
()
},
func
(
err
error
)
{
fakeClient
.
WatchInjectError
<-
err
},
func
(
obj
runtime
.
Object
,
action
string
)
error
{
return
registrytest
.
EmitObject
(
fakeClient
,
obj
,
action
)
},
// matching labels
[]
labels
.
Set
{},
// not matching labels
...
...
@@ -431,7 +399,6 @@ func TestEtcdWatch(t *testing.T) {
[]
fields
.
Set
{
{
"metadata.name"
:
"bar"
},
},
registrytest
.
WatchActions
,
)
}
...
...
pkg/registry/podtemplate/etcd/etcd.go
View file @
781de2f5
...
...
@@ -28,13 +28,13 @@ import (
)
type
REST
struct
{
etcdgeneric
.
Etcd
*
etcdgeneric
.
Etcd
}
// NewREST returns a RESTStorage object that will work against pod templates.
func
NewREST
(
s
storage
.
Interface
)
*
REST
{
prefix
:=
"/podtemplates"
store
:=
etcdgeneric
.
Etcd
{
store
:=
&
etcdgeneric
.
Etcd
{
NewFunc
:
func
()
runtime
.
Object
{
return
&
api
.
PodTemplate
{}
},
NewListFunc
:
func
()
runtime
.
Object
{
return
&
api
.
PodTemplateList
{}
},
KeyRootFunc
:
func
(
ctx
api
.
Context
)
string
{
...
...
pkg/registry/podtemplate/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -20,7 +20,6 @@ import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/tools"
...
...
@@ -60,18 +59,12 @@ func validNewPodTemplate(name string) *api.PodTemplate {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
pod
:=
validNewPodTemplate
(
"foo"
)
pod
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
pod
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
PodTemplate
{
Template
:
api
.
PodTemplateSpec
{},
...
...
@@ -81,19 +74,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
//valid
validNewPodTemplate
(
"foo"
),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
PodTemplate
)
...
...
@@ -102,3 +86,15 @@ func TestUpdate(t *testing.T) {
},
)
}
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewPodTemplate
(
"foo"
))
}
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewPodTemplate
(
"foo"
))
}
pkg/registry/registrytest/etcd.go
View file @
781de2f5
...
...
@@ -22,7 +22,11 @@ import (
"github.com/coreos/go-etcd/etcd"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
etcdgeneric
"k8s.io/kubernetes/pkg/registry/generic/etcd"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage"
etcdstorage
"k8s.io/kubernetes/pkg/storage/etcd"
...
...
@@ -37,68 +41,132 @@ func NewEtcdStorage(t *testing.T) (storage.Interface, *tools.FakeEtcdClient) {
return
etcdStorage
,
fakeClient
}
var
WatchActions
=
[]
string
{
etcdstorage
.
EtcdCreate
,
etcdstorage
.
EtcdSet
,
etcdstorage
.
EtcdCAS
,
etcdstorage
.
EtcdDelete
}
type
keyFunc
func
(
api
.
Context
,
string
)
(
string
,
error
)
type
newFunc
func
()
runtime
.
Object
type
Tester
struct
{
tester
*
resttest
.
Tester
fakeClient
*
tools
.
FakeEtcdClient
storage
*
etcdgeneric
.
Etcd
}
type
UpdateFunc
func
(
runtime
.
Object
)
runtime
.
Object
func
EmitObject
(
fakeClient
*
tools
.
FakeEtcdClient
,
obj
runtime
.
Object
,
action
string
)
error
{
encoded
,
err
:=
testapi
.
Codec
()
.
Encode
(
obj
)
if
err
!=
nil
{
return
err
}
node
:=
&
etcd
.
Node
{
Value
:
string
(
encoded
),
func
New
(
t
*
testing
.
T
,
fakeClient
*
tools
.
FakeEtcdClient
,
storage
*
etcdgeneric
.
Etcd
)
*
Tester
{
return
&
Tester
{
tester
:
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
),
fakeClient
:
fakeClient
,
storage
:
storage
,
}
var
prevNode
*
etcd
.
Node
=
nil
if
action
==
etcdstorage
.
EtcdDelete
{
prevNode
=
node
}
fakeClient
.
WatchResponse
<-
&
etcd
.
Response
{
Action
:
action
,
Node
:
node
,
PrevNode
:
prevNode
,
}
func
(
t
*
Tester
)
TestNamespace
()
string
{
return
t
.
tester
.
TestNamespace
()
}
func
(
t
*
Tester
)
ClusterScope
()
*
Tester
{
t
.
tester
=
t
.
tester
.
ClusterScope
()
return
t
}
func
(
t
*
Tester
)
AllowCreateOnUpdate
()
*
Tester
{
t
.
tester
=
t
.
tester
.
AllowCreateOnUpdate
()
return
t
}
func
(
t
*
Tester
)
GeneratesName
()
*
Tester
{
t
.
tester
=
t
.
tester
.
GeneratesName
()
return
t
}
func
(
t
*
Tester
)
TestCreate
(
valid
runtime
.
Object
,
invalid
...
runtime
.
Object
)
{
t
.
tester
.
TestCreate
(
valid
,
t
.
setObject
,
t
.
getObject
,
invalid
...
,
)
}
func
(
t
*
Tester
)
TestUpdate
(
valid
runtime
.
Object
,
validUpdateFunc
UpdateFunc
,
invalidUpdateFunc
...
UpdateFunc
)
{
var
invalidFuncs
[]
resttest
.
UpdateFunc
for
_
,
f
:=
range
invalidUpdateFunc
{
invalidFuncs
=
append
(
invalidFuncs
,
resttest
.
UpdateFunc
(
f
))
}
return
nil
t
.
tester
.
TestUpdate
(
valid
,
t
.
setObject
,
t
.
setResourceVersion
,
t
.
getObject
,
resttest
.
UpdateFunc
(
validUpdateFunc
),
invalidFuncs
...
,
)
}
func
GetObject
(
fakeClient
*
tools
.
FakeEtcdClient
,
keyFn
keyFunc
,
newFn
newFunc
,
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
func
(
t
*
Tester
)
TestGet
(
valid
runtime
.
Object
)
{
t
.
tester
.
TestGet
(
valid
)
}
func
(
t
*
Tester
)
TestList
(
valid
runtime
.
Object
)
{
t
.
tester
.
TestList
(
valid
,
t
.
setObjectsForList
,
t
.
setResourceVersion
,
)
}
func
(
t
*
Tester
)
TestWatch
(
valid
runtime
.
Object
,
labelsPass
,
labelsFail
[]
labels
.
Set
,
fieldsPass
,
fieldsFail
[]
fields
.
Set
)
{
t
.
tester
.
TestWatch
(
valid
,
t
.
fakeClient
.
WaitForWatchCompletion
,
t
.
injectWatchError
,
t
.
emitObject
,
labelsPass
,
labelsFail
,
fieldsPass
,
fieldsFail
,
[]
string
{
etcdstorage
.
EtcdCreate
,
etcdstorage
.
EtcdSet
,
etcdstorage
.
EtcdCAS
,
etcdstorage
.
EtcdDelete
},
)
}
// =============================================================================
// Helper functions
func
(
t
*
Tester
)
getObject
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
meta
,
err
:=
api
.
ObjectMetaFor
(
obj
)
if
err
!=
nil
{
return
nil
,
err
}
key
,
err
:=
keyFn
(
ctx
,
meta
.
Name
)
key
,
err
:=
t
.
storage
.
KeyFunc
(
ctx
,
meta
.
Name
)
if
err
!=
nil
{
return
nil
,
err
}
key
=
etcdtest
.
AddPrefix
(
key
)
resp
,
err
:=
fakeClient
.
Get
(
key
,
false
,
false
)
resp
,
err
:=
t
.
fakeClient
.
Get
(
key
,
false
,
false
)
if
err
!=
nil
{
return
nil
,
err
}
result
:=
newFn
()
result
:=
t
.
storage
.
NewFunc
()
if
err
:=
testapi
.
Codec
()
.
DecodeInto
([]
byte
(
resp
.
Node
.
Value
),
result
);
err
!=
nil
{
return
nil
,
err
}
return
result
,
nil
}
func
SetObject
(
fakeClient
*
tools
.
FakeEtcdClient
,
keyFn
keyFunc
,
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
func
(
t
*
Tester
)
setObject
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
meta
,
err
:=
api
.
ObjectMetaFor
(
obj
)
if
err
!=
nil
{
return
err
}
key
,
err
:=
keyFn
(
ctx
,
meta
.
Name
)
key
,
err
:=
t
.
storage
.
KeyFunc
(
ctx
,
meta
.
Name
)
if
err
!=
nil
{
return
err
}
key
=
etcdtest
.
AddPrefix
(
key
)
_
,
err
=
fakeClient
.
Set
(
key
,
runtime
.
EncodeOrDie
(
testapi
.
Codec
(),
obj
),
0
)
_
,
err
=
t
.
fakeClient
.
Set
(
key
,
runtime
.
EncodeOrDie
(
testapi
.
Codec
(),
obj
),
0
)
return
err
}
func
SetObjectsForKey
(
fakeClient
*
tools
.
FakeEtcdClient
,
key
string
,
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
func
(
t
*
Tester
)
setObjectsForList
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
result
:=
make
([]
runtime
.
Object
,
len
(
objects
))
key
:=
etcdtest
.
AddPrefix
(
t
.
storage
.
KeyRootFunc
(
t
.
tester
.
TestContext
()))
if
len
(
objects
)
>
0
{
nodes
:=
make
([]
*
etcd
.
Node
,
len
(
objects
))
for
i
,
obj
:=
range
objects
{
...
...
@@ -107,7 +175,7 @@ func SetObjectsForKey(fakeClient *tools.FakeEtcdClient, key string, objects []ru
nodes
[
i
]
=
&
etcd
.
Node
{
Value
:
encoded
}
result
[
i
]
=
decoded
}
fakeClient
.
Data
[
key
]
=
tools
.
EtcdResponseWithError
{
t
.
fakeClient
.
Data
[
key
]
=
tools
.
EtcdResponseWithError
{
R
:
&
etcd
.
Response
{
Node
:
&
etcd
.
Node
{
Nodes
:
nodes
,
...
...
@@ -116,14 +184,38 @@ func SetObjectsForKey(fakeClient *tools.FakeEtcdClient, key string, objects []ru
E
:
nil
,
}
}
else
{
fakeClient
.
Data
[
key
]
=
tools
.
EtcdResponseWithError
{
t
.
fakeClient
.
Data
[
key
]
=
tools
.
EtcdResponseWithError
{
R
:
&
etcd
.
Response
{},
E
:
fakeClient
.
NewError
(
tools
.
EtcdErrorCodeNotFound
),
E
:
t
.
fakeClient
.
NewError
(
tools
.
EtcdErrorCodeNotFound
),
}
}
return
result
}
func
SetResourceVersion
(
fakeClient
*
tools
.
FakeEtcdClient
,
resourceVersion
uint64
)
{
fakeClient
.
ChangeIndex
=
resourceVersion
func
(
t
*
Tester
)
setResourceVersion
(
resourceVersion
uint64
)
{
t
.
fakeClient
.
ChangeIndex
=
resourceVersion
}
func
(
t
*
Tester
)
injectWatchError
(
err
error
)
{
t
.
fakeClient
.
WatchInjectError
<-
err
}
func
(
t
*
Tester
)
emitObject
(
obj
runtime
.
Object
,
action
string
)
error
{
encoded
,
err
:=
testapi
.
Codec
()
.
Encode
(
obj
)
if
err
!=
nil
{
return
err
}
node
:=
&
etcd
.
Node
{
Value
:
string
(
encoded
),
}
var
prevNode
*
etcd
.
Node
=
nil
if
action
==
etcdstorage
.
EtcdDelete
{
prevNode
=
node
}
t
.
fakeClient
.
WatchResponse
<-
&
etcd
.
Response
{
Action
:
action
,
Node
:
node
,
PrevNode
:
prevNode
,
}
return
nil
}
pkg/registry/resourcequota/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -22,7 +22,6 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
...
...
@@ -60,29 +59,14 @@ func validNewResourceQuota() *api.ResourceQuota {
}
}
func
validChangedResourceQuota
()
*
api
.
ResourceQuota
{
resourcequota
:=
validNewResourceQuota
()
resourcequota
.
ResourceVersion
=
"1"
resourcequota
.
Labels
=
map
[
string
]
string
{
"foo"
:
"bar"
,
}
return
resourcequota
}
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
resourcequota
:=
validNewResourceQuota
()
resourcequota
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
resourcequota
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
ResourceQuota
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"_-a123-a_"
},
...
...
@@ -177,40 +161,23 @@ func TestDeleteResourceQuota(t *testing.T) {
}
}
func
Test
Etcd
Get
(
t
*
testing
.
T
)
{
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewResourceQuota
())
}
func
Test
Etcd
List
(
t
*
testing
.
T
)
{
func
TestList
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
key
:=
etcdtest
.
AddPrefix
(
storage
.
Etcd
.
KeyRootFunc
(
test
.
TestContext
()))
test
.
TestList
(
validNewResourceQuota
(),
func
(
objects
[]
runtime
.
Object
)
[]
runtime
.
Object
{
return
registrytest
.
SetObjectsForKey
(
fakeClient
,
key
,
objects
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
})
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewResourceQuota
())
}
func
Test
Etcd
Watch
(
t
*
testing
.
T
)
{
func
TestWatch
(
t
*
testing
.
T
)
{
storage
,
_
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestWatch
(
validNewResourceQuota
(),
func
()
{
fakeClient
.
WaitForWatchCompletion
()
},
func
(
err
error
)
{
fakeClient
.
WatchInjectError
<-
err
},
func
(
obj
runtime
.
Object
,
action
string
)
error
{
return
registrytest
.
EmitObject
(
fakeClient
,
obj
,
action
)
},
// matching labels
[]
labels
.
Set
{},
// not matching labels
...
...
@@ -225,7 +192,6 @@ func TestEtcdWatch(t *testing.T) {
[]
fields
.
Set
{
{
"metadata.name"
:
"bar"
},
},
registrytest
.
WatchActions
,
)
}
...
...
pkg/registry/secret/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -20,7 +20,6 @@ import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/tools"
...
...
@@ -45,18 +44,12 @@ func validNewSecret(name string) *api.Secret {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
secret
:=
validNewSecret
(
"foo"
)
secret
.
ObjectMeta
=
api
.
ObjectMeta
{
GenerateName
:
"foo-"
}
test
.
TestCreate
(
// valid
secret
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
Secret
{},
&
api
.
Secret
{
...
...
@@ -72,19 +65,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewSecret
(
"foo"
),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
Secret
)
...
...
@@ -93,3 +77,15 @@ func TestUpdate(t *testing.T) {
},
)
}
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewSecret
(
"foo"
))
}
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewSecret
(
"foo"
))
}
pkg/registry/service/etcd/etcd.go
View file @
781de2f5
...
...
@@ -30,13 +30,13 @@ import (
)
type
REST
struct
{
etcdgeneric
.
Etcd
*
etcdgeneric
.
Etcd
}
// NewREST returns a RESTStorage object that will work against services.
func
NewREST
(
s
storage
.
Interface
)
*
REST
{
prefix
:=
"/services/specs"
store
:=
etcdgeneric
.
Etcd
{
store
:=
&
etcdgeneric
.
Etcd
{
NewFunc
:
func
()
runtime
.
Object
{
return
&
api
.
Service
{}
},
NewListFunc
:
func
()
runtime
.
Object
{
return
&
api
.
ServiceList
{}
},
KeyRootFunc
:
func
(
ctx
api
.
Context
)
string
{
...
...
pkg/registry/service/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -20,7 +20,6 @@ import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/registry/registrytest"
...
...
@@ -56,18 +55,12 @@ func validService() *api.Service {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
validService
:=
validService
()
validService
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
validService
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
Service
{
Spec
:
api
.
ServiceSpec
{},
...
...
@@ -91,19 +84,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
.
AllowCreateOnUpdate
()
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
AllowCreateOnUpdate
()
test
.
TestUpdate
(
// valid
validService
(),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
Service
)
...
...
@@ -122,20 +106,23 @@ func TestUpdate(t *testing.T) {
)
}
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
AllowCreateOnUpdate
()
test
.
TestGet
(
validService
())
}
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
.
AllowCreateOnUpdate
()
test
.
TestList
(
validService
())
}
func
TestWatch
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestWatch
(
validService
(),
func
()
{
fakeClient
.
WaitForWatchCompletion
()
},
func
(
err
error
)
{
fakeClient
.
WatchInjectError
<-
err
},
func
(
obj
runtime
.
Object
,
action
string
)
error
{
return
registrytest
.
EmitObject
(
fakeClient
,
obj
,
action
)
},
// matching labels
[]
labels
.
Set
{},
// not matching labels
...
...
@@ -150,6 +137,5 @@ func TestWatch(t *testing.T) {
[]
fields
.
Set
{
{
"metadata.name"
:
"bar"
},
},
registrytest
.
WatchActions
,
)
}
pkg/registry/serviceaccount/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -20,7 +20,6 @@ import (
"testing"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/tools"
...
...
@@ -43,18 +42,12 @@ func validNewServiceAccount(name string) *api.ServiceAccount {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
serviceAccount
:=
validNewServiceAccount
(
"foo"
)
serviceAccount
.
ObjectMeta
=
api
.
ObjectMeta
{
GenerateName
:
"foo-"
}
test
.
TestCreate
(
// valid
serviceAccount
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
api
.
ServiceAccount
{},
&
api
.
ServiceAccount
{
...
...
@@ -65,19 +58,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewServiceAccount
(
"foo"
),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
api
.
ServiceAccount
)
...
...
@@ -86,3 +70,15 @@ func TestUpdate(t *testing.T) {
},
)
}
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewServiceAccount
(
"foo"
))
}
func
TestList
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewServiceAccount
(
"foo"
))
}
pkg/registry/thirdpartyresource/etcd/etcd_test.go
View file @
781de2f5
...
...
@@ -23,9 +23,8 @@ import (
"k8s.io/kubernetes/pkg/api/rest/resttest"
"k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/expapi"
"k8s.io/kubernetes/pkg/expapi/v1"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
// Ensure that expapi/v1 package is initialized.
_
"k8s.io/kubernetes/pkg/expapi/v1"
"k8s.io/kubernetes/pkg/registry/registrytest"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/tools"
...
...
@@ -34,11 +33,6 @@ import (
"github.com/coreos/go-etcd/etcd"
)
func
init
()
{
// Ensure that expapi/v1 packege is used, so that it will get initialized and register HorizontalPodAutoscaler object.
_
=
v1
.
ThirdPartyResource
{}
}
func
newStorage
(
t
*
testing
.
T
)
(
*
REST
,
*
tools
.
FakeEtcdClient
)
{
etcdStorage
,
fakeClient
:=
registrytest
.
NewEtcdStorage
(
t
)
return
NewREST
(
etcdStorage
),
fakeClient
...
...
@@ -60,18 +54,12 @@ func validNewThirdPartyResource(name string) *expapi.ThirdPartyResource {
func
TestCreate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
rsrc
:=
validNewThirdPartyResource
(
"foo"
)
rsrc
.
ObjectMeta
=
api
.
ObjectMeta
{}
test
.
TestCreate
(
// valid
rsrc
,
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// invalid
&
expapi
.
ThirdPartyResource
{},
)
...
...
@@ -79,19 +67,10 @@ func TestCreate(t *testing.T) {
func
TestUpdate
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
re
sttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
test
:=
re
gistrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestUpdate
(
// valid
validNewThirdPartyResource
(
"foo"
),
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
error
{
return
registrytest
.
SetObject
(
fakeClient
,
storage
.
KeyFunc
,
ctx
,
obj
)
},
func
(
resourceVersion
uint64
)
{
registrytest
.
SetResourceVersion
(
fakeClient
,
resourceVersion
)
},
func
(
ctx
api
.
Context
,
obj
runtime
.
Object
)
(
runtime
.
Object
,
error
)
{
return
registrytest
.
GetObject
(
fakeClient
,
storage
.
KeyFunc
,
storage
.
NewFunc
,
ctx
,
obj
)
},
// updateFunc
func
(
obj
runtime
.
Object
)
runtime
.
Object
{
object
:=
obj
.
(
*
expapi
.
ThirdPartyResource
)
...
...
@@ -130,73 +109,12 @@ func TestDelete(t *testing.T) {
func
TestGet
(
t
*
testing
.
T
)
{
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
resttest
.
New
(
t
,
storage
,
fakeClient
.
SetError
)
rsrc
:=
validNewThirdPartyResource
(
"foo"
)
test
.
TestGet
(
rsrc
)
}
func
TestEmptyList
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
registry
,
fakeClient
:=
newStorage
(
t
)
fakeClient
.
ChangeIndex
=
1
key
:=
registry
.
KeyRootFunc
(
ctx
)
key
=
etcdtest
.
AddPrefix
(
key
)
fakeClient
.
Data
[
key
]
=
tools
.
EtcdResponseWithError
{
R
:
&
etcd
.
Response
{},
E
:
fakeClient
.
NewError
(
tools
.
EtcdErrorCodeNotFound
),
}
rsrcList
,
err
:=
registry
.
List
(
ctx
,
labels
.
Everything
(),
fields
.
Everything
())
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
len
(
rsrcList
.
(
*
expapi
.
ThirdPartyResourceList
)
.
Items
)
!=
0
{
t
.
Errorf
(
"Unexpected non-zero autoscaler list: %#v"
,
rsrcList
)
}
if
rsrcList
.
(
*
expapi
.
ThirdPartyResourceList
)
.
ResourceVersion
!=
"1"
{
t
.
Errorf
(
"Unexpected resource version: %#v"
,
rsrcList
)
}
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestGet
(
validNewThirdPartyResource
(
"foo"
))
}
func
TestList
(
t
*
testing
.
T
)
{
ctx
:=
api
.
NewDefaultContext
()
registry
,
fakeClient
:=
newStorage
(
t
)
fakeClient
.
ChangeIndex
=
1
key
:=
registry
.
KeyRootFunc
(
ctx
)
key
=
etcdtest
.
AddPrefix
(
key
)
fakeClient
.
Data
[
key
]
=
tools
.
EtcdResponseWithError
{
R
:
&
etcd
.
Response
{
Node
:
&
etcd
.
Node
{
Nodes
:
[]
*
etcd
.
Node
{
{
Value
:
runtime
.
EncodeOrDie
(
testapi
.
Codec
(),
&
expapi
.
ThirdPartyResource
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
},
}),
},
{
Value
:
runtime
.
EncodeOrDie
(
testapi
.
Codec
(),
&
expapi
.
ThirdPartyResource
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
},
}),
},
},
},
},
}
obj
,
err
:=
registry
.
List
(
ctx
,
labels
.
Everything
(),
fields
.
Everything
())
if
err
!=
nil
{
t
.
Fatalf
(
"Unexpected error %v"
,
err
)
}
rsrcList
:=
obj
.
(
*
expapi
.
ThirdPartyResourceList
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
len
(
rsrcList
.
Items
)
!=
2
{
t
.
Errorf
(
"Unexpected ThirdPartyResource list: %#v"
,
rsrcList
)
}
if
rsrcList
.
Items
[
0
]
.
Name
!=
"foo"
{
t
.
Errorf
(
"Unexpected ThirdPartyResource: %#v"
,
rsrcList
.
Items
[
0
])
}
if
rsrcList
.
Items
[
1
]
.
Name
!=
"bar"
{
t
.
Errorf
(
"Unexpected ThirdPartyResource: %#v"
,
rsrcList
.
Items
[
1
])
}
storage
,
fakeClient
:=
newStorage
(
t
)
test
:=
registrytest
.
New
(
t
,
fakeClient
,
storage
.
Etcd
)
test
.
TestList
(
validNewThirdPartyResource
(
"foo"
))
}
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