Unification refactoring to pkg/registry
This commit is contained in:
@@ -27,17 +27,13 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// rest implements a RESTStorage for replication controllers against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// controllerPrefix is the location for controllers in etcd, only exposed
|
||||
// for testing
|
||||
var controllerPrefix = "/controllers"
|
||||
|
||||
// NewREST returns a RESTStorage object that will work against replication controllers.
|
||||
func NewREST(s storage.Interface) *REST {
|
||||
prefix := "/controllers"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.ReplicationController{} },
|
||||
|
||||
@@ -46,12 +42,12 @@ func NewREST(s storage.Interface) *REST {
|
||||
// Produces a path that etcd understands, to the root of the resource
|
||||
// by combining the namespace in the context with the given prefix
|
||||
KeyRootFunc: func(ctx api.Context) string {
|
||||
return etcdgeneric.NamespaceKeyRootFunc(ctx, controllerPrefix)
|
||||
return etcdgeneric.NamespaceKeyRootFunc(ctx, prefix)
|
||||
},
|
||||
// Produces a path that etcd understands, to the resource by combining
|
||||
// the namespace in the context with the given prefix
|
||||
KeyFunc: func(ctx api.Context, name string) (string, error) {
|
||||
return etcdgeneric.NamespaceKeyFunc(ctx, controllerPrefix, name)
|
||||
return etcdgeneric.NamespaceKeyFunc(ctx, prefix, name)
|
||||
},
|
||||
// Retrieve the name field of a replication controller
|
||||
ObjectNameFunc: func(obj runtime.Object) (string, error) {
|
||||
@@ -71,6 +67,5 @@ func NewREST(s storage.Interface) *REST {
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
return &REST{store}
|
||||
}
|
||||
|
@@ -25,14 +25,13 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"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/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
@@ -43,18 +42,9 @@ const (
|
||||
FAIL
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
}
|
||||
|
||||
// newStorage creates a REST storage backed by etcd helpers
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
fakeEtcdClient, s := newEtcdStorage(t)
|
||||
storage := NewREST(s)
|
||||
return storage, fakeEtcdClient
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage), fakeClient
|
||||
}
|
||||
|
||||
// createController is a helper function that returns a controller with the updated resource version.
|
||||
@@ -97,11 +87,6 @@ var validController = api.ReplicationController{
|
||||
Spec: validControllerSpec,
|
||||
}
|
||||
|
||||
// makeControllerKey constructs etcd paths to controller items enforcing namespace rules.
|
||||
func makeControllerKey(ctx api.Context, id string) (string, error) {
|
||||
return etcdgeneric.NamespaceKeyFunc(ctx, controllerPrefix, id)
|
||||
}
|
||||
|
||||
func TestEtcdCreateController(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, fakeClient := newStorage(t)
|
||||
@@ -109,14 +94,14 @@ func TestEtcdCreateController(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
key, _ := makeControllerKey(ctx, validController.Name)
|
||||
key, _ := storage.KeyFunc(ctx, validController.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
resp, err := fakeClient.Get(key, false, false)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var ctrl api.ReplicationController
|
||||
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &ctrl)
|
||||
err = testapi.Codec().DecodeInto([]byte(resp.Node.Value), &ctrl)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -129,9 +114,9 @@ func TestEtcdCreateController(t *testing.T) {
|
||||
func TestEtcdCreateControllerAlreadyExisting(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, fakeClient := newStorage(t)
|
||||
key, _ := makeControllerKey(ctx, validController.Name)
|
||||
key, _ := storage.KeyFunc(ctx, validController.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &validController), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &validController), 0)
|
||||
|
||||
_, err := storage.Create(ctx, &validController)
|
||||
if !errors.IsAlreadyExists(err) {
|
||||
@@ -332,11 +317,11 @@ func TestEtcdListControllers(t *testing.T) {
|
||||
func TestEtcdUpdateController(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, fakeClient := newStorage(t)
|
||||
key, _ := makeControllerKey(ctx, validController.Name)
|
||||
key, _ := storage.KeyFunc(ctx, validController.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
|
||||
// set a key, then retrieve the current resource version and try updating it
|
||||
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &validController), 0)
|
||||
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &validController), 0)
|
||||
update := validController
|
||||
update.ResourceVersion = strconv.FormatUint(resp.Node.ModifiedIndex, 10)
|
||||
update.Spec.Replicas = validController.Spec.Replicas + 1
|
||||
@@ -357,10 +342,10 @@ func TestEtcdUpdateController(t *testing.T) {
|
||||
func TestEtcdDeleteController(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, fakeClient := newStorage(t)
|
||||
key, _ := makeControllerKey(ctx, validController.Name)
|
||||
key, _ := storage.KeyFunc(ctx, validController.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &validController), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &validController), 0)
|
||||
obj, err := storage.Delete(ctx, validController.Name, nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
@@ -430,7 +415,7 @@ func TestEtcdWatchControllersMatch(t *testing.T) {
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
controllerBytes, _ := latest.Codec.Encode(controller)
|
||||
controllerBytes, _ := testapi.Codec().Encode(controller)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
@@ -483,7 +468,7 @@ func TestEtcdWatchControllersFields(t *testing.T) {
|
||||
Replicas: 0,
|
||||
},
|
||||
}
|
||||
controllerBytes, _ := latest.Codec.Encode(controller)
|
||||
controllerBytes, _ := testapi.Codec().Encode(controller)
|
||||
|
||||
for expectedResult, fieldSet := range testFieldMap {
|
||||
for _, field := range fieldSet {
|
||||
@@ -552,7 +537,7 @@ func TestEtcdWatchControllersNotMatch(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
controllerBytes, _ := latest.Codec.Encode(controller)
|
||||
controllerBytes, _ := testapi.Codec().Encode(controller)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
@@ -595,7 +580,7 @@ func TestDelete(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key, _ := makeControllerKey(ctx, validController.Name)
|
||||
key, _ := storage.KeyFunc(ctx, validController.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
|
||||
createFn := func() runtime.Object {
|
||||
@@ -604,7 +589,7 @@ func TestDelete(t *testing.T) {
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &rc),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &rc),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
|
@@ -27,36 +27,34 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// rest implements a RESTStorage for endpoints against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// NewStorage returns a RESTStorage object that will work against endpoints.
|
||||
func NewStorage(s storage.Interface) *REST {
|
||||
// NewREST returns a RESTStorage object that will work against endpoints.
|
||||
func NewREST(s storage.Interface) *REST {
|
||||
prefix := "/services/endpoints"
|
||||
return &REST{
|
||||
&etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.Endpoints{} },
|
||||
NewListFunc: func() runtime.Object { return &api.EndpointsList{} },
|
||||
KeyRootFunc: func(ctx api.Context) string {
|
||||
return etcdgeneric.NamespaceKeyRootFunc(ctx, prefix)
|
||||
},
|
||||
KeyFunc: func(ctx api.Context, name string) (string, error) {
|
||||
return etcdgeneric.NamespaceKeyFunc(ctx, prefix, name)
|
||||
},
|
||||
ObjectNameFunc: func(obj runtime.Object) (string, error) {
|
||||
return obj.(*api.Endpoints).Name, nil
|
||||
},
|
||||
PredicateFunc: func(label labels.Selector, field fields.Selector) generic.Matcher {
|
||||
return endpoint.MatchEndpoints(label, field)
|
||||
},
|
||||
EndpointName: "endpoints",
|
||||
|
||||
CreateStrategy: endpoint.Strategy,
|
||||
UpdateStrategy: endpoint.Strategy,
|
||||
|
||||
Storage: s,
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.Endpoints{} },
|
||||
NewListFunc: func() runtime.Object { return &api.EndpointsList{} },
|
||||
KeyRootFunc: func(ctx api.Context) string {
|
||||
return etcdgeneric.NamespaceKeyRootFunc(ctx, prefix)
|
||||
},
|
||||
KeyFunc: func(ctx api.Context, name string) (string, error) {
|
||||
return etcdgeneric.NamespaceKeyFunc(ctx, prefix, name)
|
||||
},
|
||||
ObjectNameFunc: func(obj runtime.Object) (string, error) {
|
||||
return obj.(*api.Endpoints).Name, nil
|
||||
},
|
||||
PredicateFunc: func(label labels.Selector, field fields.Selector) generic.Matcher {
|
||||
return endpoint.MatchEndpoints(label, field)
|
||||
},
|
||||
EndpointName: "endpoints",
|
||||
|
||||
CreateStrategy: endpoint.Strategy,
|
||||
UpdateStrategy: endpoint.Strategy,
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
return &REST{store}
|
||||
}
|
||||
|
@@ -20,12 +20,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -33,17 +31,9 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
}
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
fakeEtcdClient, s := newEtcdStorage(t)
|
||||
storage := NewStorage(s)
|
||||
return storage, fakeEtcdClient
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage), fakeClient
|
||||
}
|
||||
|
||||
func validNewEndpoints() *api.Endpoints {
|
||||
@@ -70,8 +60,8 @@ func validChangedEndpoints() *api.Endpoints {
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
storage, fakeEtcdClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
endpoints := validNewEndpoints()
|
||||
endpoints.ObjectMeta = api.ObjectMeta{}
|
||||
test.TestCreate(
|
||||
@@ -86,17 +76,17 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, fakeEtcdClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
|
||||
endpoints := validChangedEndpoints()
|
||||
key, _ := storage.KeyFunc(ctx, endpoints.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
createFn := func() runtime.Object {
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, endpoints),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), endpoints),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
@@ -104,10 +94,10 @@ func TestDelete(t *testing.T) {
|
||||
return endpoints
|
||||
}
|
||||
gracefulSetFn := func() bool {
|
||||
if fakeEtcdClient.Data[key].R.Node == nil {
|
||||
if fakeClient.Data[key].R.Node == nil {
|
||||
return false
|
||||
}
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL == 30
|
||||
return fakeClient.Data[key].R.Node.TTL == 30
|
||||
}
|
||||
test.TestDelete(createFn, gracefulSetFn)
|
||||
}
|
||||
@@ -137,13 +127,13 @@ func TestEtcdListEndpoints(t *testing.T) {
|
||||
func TestEndpointsDecode(t *testing.T) {
|
||||
storage, _ := newStorage(t)
|
||||
expected := validNewEndpoints()
|
||||
body, err := latest.Codec.Encode(expected)
|
||||
body, err := testapi.Codec().Encode(expected)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := storage.New()
|
||||
if err := latest.Codec.DecodeInto(body, actual); err != nil {
|
||||
if err := testapi.Codec().DecodeInto(body, actual); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -159,7 +149,7 @@ func TestEtcdUpdateEndpoints(t *testing.T) {
|
||||
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewEndpoints()), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), validNewEndpoints()), 0)
|
||||
|
||||
_, _, err := storage.Update(ctx, endpoints)
|
||||
if err != nil {
|
||||
@@ -171,7 +161,7 @@ func TestEtcdUpdateEndpoints(t *testing.T) {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var endpointsOut api.Endpoints
|
||||
err = latest.Codec.DecodeInto([]byte(response.Node.Value), &endpointsOut)
|
||||
err = testapi.Codec().DecodeInto([]byte(response.Node.Value), &endpointsOut)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -193,7 +183,7 @@ func TestDeleteEndpoints(t *testing.T) {
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, endpoints),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), endpoints),
|
||||
ModifiedIndex: 1,
|
||||
CreatedIndex: 1,
|
||||
},
|
||||
|
@@ -31,7 +31,8 @@ type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
func NewStorage(s storage.Interface, ttl uint64) *REST {
|
||||
// NewREST returns a RESTStorage object that will work against events.
|
||||
func NewREST(s storage.Interface, ttl uint64) *REST {
|
||||
prefix := "/events"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.Event{} },
|
||||
|
@@ -24,8 +24,8 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
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"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -35,12 +35,9 @@ import (
|
||||
|
||||
var testTTL uint64 = 60
|
||||
|
||||
func NewTestEventStorage(t *testing.T) (*tools.FakeEtcdClient, *REST) {
|
||||
f := tools.NewFakeEtcdClient(t)
|
||||
f.TestIndex = true
|
||||
|
||||
s := etcdstorage.NewEtcdStorage(f, testapi.Codec(), etcdtest.PathPrefix())
|
||||
return f, NewStorage(s, testTTL)
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage, testTTL), fakeClient
|
||||
}
|
||||
|
||||
func TestEventCreate(t *testing.T) {
|
||||
@@ -101,7 +98,7 @@ func TestEventCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
for name, item := range table {
|
||||
fakeClient, storage := NewTestEventStorage(t)
|
||||
storage, fakeClient := newStorage(t)
|
||||
fakeClient.Data[path] = item.existing
|
||||
_, err := storage.Create(ctx, item.toCreate)
|
||||
if !item.errOK(err) {
|
||||
@@ -217,7 +214,7 @@ func TestEventUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
for name, item := range table {
|
||||
fakeClient, storage := NewTestEventStorage(t)
|
||||
storage, fakeClient := newStorage(t)
|
||||
fakeClient.Data[path] = item.existing
|
||||
_, _, err := storage.Update(ctx, item.toUpdate)
|
||||
if !item.errOK(err) {
|
||||
|
@@ -20,7 +20,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
@@ -36,7 +36,7 @@ import (
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, testapi.Codec(), etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func TestGet(t *testing.T) {
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &validController),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &validController),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
@@ -123,7 +123,7 @@ func TestUpdate(t *testing.T) {
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &validController),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &validController),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
@@ -146,7 +146,7 @@ func TestUpdate(t *testing.T) {
|
||||
}
|
||||
|
||||
var controller api.ReplicationController
|
||||
latest.Codec.DecodeInto([]byte(response.Node.Value), &controller)
|
||||
testapi.Codec().DecodeInto([]byte(response.Node.Value), &controller)
|
||||
if controller.Spec.Replicas != replicas {
|
||||
t.Errorf("wrong replicas count expected: %d got: %d", replicas, controller.Spec.Replicas)
|
||||
}
|
||||
|
@@ -28,14 +28,13 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// rest implements a RESTStorage for horizontal pod autoscalers against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// NewREST returns a RESTStorage object that will work against horizontal pod autoscalers.
|
||||
func NewREST(s storage.Interface) *REST {
|
||||
var prefix = "/horizontalpodautoscalers"
|
||||
prefix := "/horizontalpodautoscalers"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &expapi.HorizontalPodAutoscaler{} },
|
||||
// NewListFunc returns an object capable of storing results of an etcd list.
|
||||
@@ -68,6 +67,5 @@ func NewREST(s storage.Interface) *REST {
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
return &REST{store}
|
||||
}
|
||||
|
@@ -24,32 +24,19 @@ 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"
|
||||
// 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/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
var scheme *runtime.Scheme
|
||||
var codec runtime.Codec
|
||||
|
||||
func init() {
|
||||
// Ensure that expapi/v1 packege is used, so that it will get initialized and register HorizontalPodAutoscaler object.
|
||||
dummy := v1.HorizontalPodAutoscaler{}
|
||||
dummy.Spec = v1.HorizontalPodAutoscalerSpec{}
|
||||
}
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, testapi.Codec(), etcdtest.PathPrefix())
|
||||
storage := NewREST(etcdStorage)
|
||||
return storage, fakeEtcdClient, etcdStorage
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage), fakeClient
|
||||
}
|
||||
|
||||
func validNewHorizontalPodAutoscaler(name string) *expapi.HorizontalPodAutoscaler {
|
||||
@@ -70,8 +57,8 @@ func validNewHorizontalPodAutoscaler(name string) *expapi.HorizontalPodAutoscale
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
storage, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
autoscaler := validNewHorizontalPodAutoscaler("foo")
|
||||
autoscaler.ObjectMeta = api.ObjectMeta{}
|
||||
test.TestCreate(
|
||||
@@ -83,15 +70,15 @@ func TestCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
storage, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key, err := storage.KeyFunc(test.TestContext(), "foo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeEtcdClient.ExpectNotFoundGet(key)
|
||||
fakeEtcdClient.ChangeIndex = 2
|
||||
fakeClient.ExpectNotFoundGet(key)
|
||||
fakeClient.ChangeIndex = 2
|
||||
autoscaler := validNewHorizontalPodAutoscaler("foo")
|
||||
existing := validNewHorizontalPodAutoscaler("exists")
|
||||
existing.Namespace = test.TestNamespace()
|
||||
@@ -110,13 +97,13 @@ func TestUpdate(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
autoscaler := validNewHorizontalPodAutoscaler("foo2")
|
||||
key, _ := storage.KeyFunc(ctx, "foo2")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
createFn := func() runtime.Object {
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), autoscaler),
|
||||
@@ -127,32 +114,32 @@ func TestDelete(t *testing.T) {
|
||||
return autoscaler
|
||||
}
|
||||
gracefulSetFn := func() bool {
|
||||
if fakeEtcdClient.Data[key].R.Node == nil {
|
||||
if fakeClient.Data[key].R.Node == nil {
|
||||
return false
|
||||
}
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL == 30
|
||||
return fakeClient.Data[key].R.Node.TTL == 30
|
||||
}
|
||||
test.TestDelete(createFn, gracefulSetFn)
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
storage, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
autoscaler := validNewHorizontalPodAutoscaler("foo")
|
||||
test.TestGet(autoscaler)
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
storage, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
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(fakeEtcdClient, key, objects)
|
||||
return registrytest.SetObjectsForKey(fakeClient, key, objects)
|
||||
},
|
||||
func(resourceVersion uint64) {
|
||||
registrytest.SetResourceVersion(fakeEtcdClient, resourceVersion)
|
||||
registrytest.SetResourceVersion(fakeClient, resourceVersion)
|
||||
})
|
||||
}
|
||||
|
@@ -31,7 +31,8 @@ type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
func NewStorage(s storage.Interface) *REST {
|
||||
// NewREST returns a RESTStorage object that will work against horizontal pod autoscalers.
|
||||
func NewREST(s storage.Interface) *REST {
|
||||
prefix := "/limitranges"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.LimitRange{} },
|
||||
|
@@ -25,8 +25,8 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
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"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -34,11 +34,9 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func NewTestLimitRangeStorage(t *testing.T) (*tools.FakeEtcdClient, *REST) {
|
||||
f := tools.NewFakeEtcdClient(t)
|
||||
f.TestIndex = true
|
||||
s := etcdstorage.NewEtcdStorage(f, testapi.Codec(), etcdtest.PathPrefix())
|
||||
return f, NewStorage(s)
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage), fakeClient
|
||||
}
|
||||
|
||||
func TestLimitRangeCreate(t *testing.T) {
|
||||
@@ -110,7 +108,7 @@ func TestLimitRangeCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
for name, item := range table {
|
||||
fakeClient, storage := NewTestLimitRangeStorage(t)
|
||||
storage, fakeClient := newStorage(t)
|
||||
fakeClient.Data[path] = item.existing
|
||||
_, err := storage.Create(ctx, item.toCreate)
|
||||
if !item.errOK(err) {
|
||||
|
@@ -49,7 +49,7 @@ func (r *StatusREST) Update(ctx api.Context, obj runtime.Object) (runtime.Object
|
||||
}
|
||||
|
||||
// NewStorage returns a RESTStorage object that will work against nodes.
|
||||
func NewStorage(s storage.Interface, connection client.ConnectionInfoGetter) (*REST, *StatusREST) {
|
||||
func NewREST(s storage.Interface, connection client.ConnectionInfoGetter) (*REST, *StatusREST) {
|
||||
prefix := "/minions"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.Node{} },
|
||||
|
@@ -22,26 +22,19 @@ import (
|
||||
"time"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"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"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
const (
|
||||
PASS = iota
|
||||
FAIL
|
||||
)
|
||||
|
||||
type fakeConnectionInfoGetter struct {
|
||||
}
|
||||
|
||||
@@ -49,17 +42,10 @@ func (fakeConnectionInfoGetter) GetConnectionInfo(host string) (string, uint, ht
|
||||
return "http", 12345, nil, nil
|
||||
}
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
}
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
fakeEtcdClient, s := newEtcdStorage(t)
|
||||
storage, _ := NewStorage(s, fakeConnectionInfoGetter{})
|
||||
return storage, fakeEtcdClient
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
storage, _ := NewREST(etcdStorage, fakeConnectionInfoGetter{})
|
||||
return storage, fakeClient
|
||||
}
|
||||
|
||||
func validNewNode() *api.Node {
|
||||
@@ -89,8 +75,8 @@ func validChangedNode() *api.Node {
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
storage, fakeEtcdClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope()
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
node := validNewNode()
|
||||
node.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
|
||||
test.TestCreate(
|
||||
@@ -105,17 +91,17 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ctx := api.NewContext()
|
||||
storage, fakeEtcdClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope()
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
|
||||
node := validChangedNode()
|
||||
key, _ := storage.KeyFunc(ctx, node.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
createFn := func() runtime.Object {
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, node),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), node),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
@@ -123,10 +109,10 @@ func TestDelete(t *testing.T) {
|
||||
return node
|
||||
}
|
||||
gracefulSetFn := func() bool {
|
||||
if fakeEtcdClient.Data[key].R.Node == nil {
|
||||
if fakeClient.Data[key].R.Node == nil {
|
||||
return false
|
||||
}
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL == 30
|
||||
return fakeClient.Data[key].R.Node.TTL == 30
|
||||
}
|
||||
test.TestDelete(createFn, gracefulSetFn)
|
||||
}
|
||||
@@ -160,7 +146,7 @@ func TestEtcdUpdateEndpoints(t *testing.T) {
|
||||
|
||||
key, _ := storage.KeyFunc(ctx, node.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewNode()), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), validNewNode()), 0)
|
||||
|
||||
_, _, err := storage.Update(ctx, node)
|
||||
if err != nil {
|
||||
@@ -172,7 +158,7 @@ func TestEtcdUpdateEndpoints(t *testing.T) {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var nodeOut api.Node
|
||||
err = latest.Codec.DecodeInto([]byte(response.Node.Value), &nodeOut)
|
||||
err = testapi.Codec().DecodeInto([]byte(response.Node.Value), &nodeOut)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -189,7 +175,7 @@ func TestEtcdDeleteNode(t *testing.T) {
|
||||
node := validNewNode()
|
||||
key, _ := storage.KeyFunc(ctx, node.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, node), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), node), 0)
|
||||
_, err := storage.Delete(ctx, node.Name, nil)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
@@ -245,7 +231,7 @@ func TestEtcdWatchNodesMatch(t *testing.T) {
|
||||
}
|
||||
fakeClient.WaitForWatchCompletion()
|
||||
|
||||
nodeBytes, _ := latest.Codec.Encode(node)
|
||||
nodeBytes, _ := testapi.Codec().Encode(node)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
@@ -278,7 +264,7 @@ func TestEtcdWatchNodesNotMatch(t *testing.T) {
|
||||
}
|
||||
fakeClient.WaitForWatchCompletion()
|
||||
|
||||
nodeBytes, _ := latest.Codec.Encode(node)
|
||||
nodeBytes, _ := testapi.Codec().Encode(node)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
|
@@ -48,8 +48,8 @@ type FinalizeREST struct {
|
||||
store *etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// NewStorage returns a RESTStorage object that will work against namespaces
|
||||
func NewStorage(s storage.Interface) (*REST, *StatusREST, *FinalizeREST) {
|
||||
// NewREST returns a RESTStorage object that will work against namespaces.
|
||||
func NewREST(s storage.Interface) (*REST, *StatusREST, *FinalizeREST) {
|
||||
prefix := "/namespaces"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.Namespace{} },
|
||||
@@ -67,11 +67,13 @@ func NewStorage(s storage.Interface) (*REST, *StatusREST, *FinalizeREST) {
|
||||
return namespace.MatchNamespace(label, field)
|
||||
},
|
||||
EndpointName: "namespaces",
|
||||
Storage: s,
|
||||
|
||||
CreateStrategy: namespace.Strategy,
|
||||
UpdateStrategy: namespace.Strategy,
|
||||
ReturnDeletedObject: true,
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
store.CreateStrategy = namespace.Strategy
|
||||
store.UpdateStrategy = namespace.Strategy
|
||||
store.ReturnDeletedObject = true
|
||||
|
||||
statusStore := *store
|
||||
statusStore.UpdateStrategy = namespace.StatusStrategy
|
||||
|
@@ -20,13 +20,11 @@ import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"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/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -34,17 +32,10 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
}
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient, s := newEtcdStorage(t)
|
||||
storage, _, _ := NewStorage(s)
|
||||
return storage, fakeEtcdClient, s
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
storage, _, _ := NewREST(etcdStorage)
|
||||
return storage, fakeClient
|
||||
}
|
||||
|
||||
func validNewNamespace() *api.Namespace {
|
||||
@@ -65,14 +56,13 @@ func validChangedNamespace() *api.Namespace {
|
||||
}
|
||||
|
||||
func TestStorage(t *testing.T) {
|
||||
storage, _, _ := newStorage(t)
|
||||
storage, _ := newStorage(t)
|
||||
namespace.NewRegistry(storage)
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage, _, _ := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope()
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
namespace := validNewNamespace()
|
||||
namespace.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
|
||||
test.TestCreate(
|
||||
@@ -95,23 +85,19 @@ func expectNamespace(t *testing.T, out runtime.Object) (*api.Namespace, bool) {
|
||||
}
|
||||
|
||||
func TestCreateSetsFields(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage, _, _ := NewStorage(etcdStorage)
|
||||
storage, fakeClient := newStorage(t)
|
||||
namespace := validNewNamespace()
|
||||
_, err := storage.Create(api.NewContext(), namespace)
|
||||
if err != fakeEtcdClient.Err {
|
||||
ctx := api.NewContext()
|
||||
_, err := storage.Create(ctx, namespace)
|
||||
if err != fakeClient.Err {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := &api.Namespace{}
|
||||
ctx := api.NewContext()
|
||||
key, err := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
object, err := storage.Get(ctx, "foo")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected key error: %v", err)
|
||||
}
|
||||
if err := etcdStorage.Get(key, actual, false); err != nil {
|
||||
t.Fatalf("unexpected extraction error: %v", err)
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actual := object.(*api.Namespace)
|
||||
if actual.Name != namespace.Name {
|
||||
t.Errorf("unexpected namespace: %#v", actual)
|
||||
}
|
||||
@@ -124,18 +110,17 @@ func TestCreateSetsFields(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNamespaceDecode(t *testing.T) {
|
||||
_, etcdStorage := newEtcdStorage(t)
|
||||
storage, _, _ := NewStorage(etcdStorage)
|
||||
storage, _ := newStorage(t)
|
||||
expected := validNewNamespace()
|
||||
expected.Status.Phase = api.NamespaceActive
|
||||
expected.Spec.Finalizers = []api.FinalizerName{api.FinalizerKubernetes}
|
||||
body, err := latest.Codec.Encode(expected)
|
||||
body, err := testapi.Codec().Encode(expected)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := storage.New()
|
||||
if err := latest.Codec.DecodeInto(body, actual); err != nil {
|
||||
if err := testapi.Codec().DecodeInto(body, actual); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -145,38 +130,37 @@ func TestNamespaceDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
storage, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope()
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
namespace := validNewNamespace()
|
||||
test.TestGet(namespace)
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
storage, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope()
|
||||
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(fakeEtcdClient, key, objects)
|
||||
return registrytest.SetObjectsForKey(fakeClient, key, objects)
|
||||
},
|
||||
func(resourceVersion uint64) {
|
||||
registrytest.SetResourceVersion(fakeEtcdClient, resourceVersion)
|
||||
registrytest.SetResourceVersion(fakeClient, resourceVersion)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteNamespace(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
fakeEtcdClient.ChangeIndex = 1
|
||||
storage, _, _ := NewStorage(etcdStorage)
|
||||
storage, fakeClient := newStorage(t)
|
||||
fakeClient.ChangeIndex = 1
|
||||
ctx := api.NewContext()
|
||||
key, err := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Namespace{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
},
|
||||
@@ -195,14 +179,14 @@ func TestDeleteNamespace(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteNamespaceWithIncompleteFinalizers(t *testing.T) {
|
||||
now := util.Now()
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
fakeEtcdClient.ChangeIndex = 1
|
||||
storage, fakeClient := newStorage(t)
|
||||
fakeClient.ChangeIndex = 1
|
||||
key := etcdtest.AddPrefix("/namespaces/foo")
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
now := util.Now()
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Namespace{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
DeletionTimestamp: &now,
|
||||
@@ -217,7 +201,6 @@ func TestDeleteNamespaceWithIncompleteFinalizers(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
storage, _, _ := NewStorage(etcdStorage)
|
||||
_, err := storage.Delete(api.NewContext(), "foo", nil)
|
||||
if err == nil {
|
||||
t.Fatalf("expected error: %v", err)
|
||||
@@ -225,14 +208,14 @@ func TestDeleteNamespaceWithIncompleteFinalizers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteNamespaceWithCompleteFinalizers(t *testing.T) {
|
||||
now := util.Now()
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
fakeEtcdClient.ChangeIndex = 1
|
||||
storage, fakeClient := newStorage(t)
|
||||
fakeClient.ChangeIndex = 1
|
||||
key := etcdtest.AddPrefix("/namespaces/foo")
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
now := util.Now()
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Namespace{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
DeletionTimestamp: &now,
|
||||
@@ -247,7 +230,6 @@ func TestDeleteNamespaceWithCompleteFinalizers(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
storage, _, _ := NewStorage(etcdStorage)
|
||||
_, err := storage.Delete(api.NewContext(), "foo", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
|
@@ -29,13 +29,12 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// rest implements a RESTStorage for persistentvolumes against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// NewREST returns a RESTStorage object that will work against PersistentVolume objects.
|
||||
func NewStorage(s storage.Interface) (*REST, *StatusREST) {
|
||||
// NewREST returns a RESTStorage object that will work against persistent volumes.
|
||||
func NewREST(s storage.Interface) (*REST, *StatusREST) {
|
||||
prefix := "/persistentvolumes"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.PersistentVolume{} },
|
||||
@@ -54,13 +53,13 @@ func NewStorage(s storage.Interface) (*REST, *StatusREST) {
|
||||
},
|
||||
EndpointName: "persistentvolume",
|
||||
|
||||
CreateStrategy: persistentvolume.Strategy,
|
||||
UpdateStrategy: persistentvolume.Strategy,
|
||||
ReturnDeletedObject: true,
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
store.CreateStrategy = persistentvolume.Strategy
|
||||
store.UpdateStrategy = persistentvolume.Strategy
|
||||
store.ReturnDeletedObject = true
|
||||
|
||||
statusStore := *store
|
||||
statusStore.UpdateStrategy = persistentvolume.StatusStrategy
|
||||
|
||||
|
@@ -20,13 +20,11 @@ import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -34,12 +32,10 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
storage, statusStorage := NewStorage(etcdStorage)
|
||||
return storage, statusStorage, fakeEtcdClient, etcdStorage
|
||||
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
storage, statusStorage := NewREST(etcdStorage)
|
||||
return storage, statusStorage, fakeClient
|
||||
}
|
||||
|
||||
func validNewPersistentVolume(name string) *api.PersistentVolume {
|
||||
@@ -73,8 +69,8 @@ func validChangedPersistentVolume() *api.PersistentVolume {
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
storage, _, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope()
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
pv := validNewPersistentVolume("foo")
|
||||
pv.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
|
||||
test.TestCreate(
|
||||
@@ -89,17 +85,17 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ctx := api.NewContext()
|
||||
storage, _, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope()
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
|
||||
pv := validChangedPersistentVolume()
|
||||
key, _ := storage.KeyFunc(ctx, pv.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
createFn := func() runtime.Object {
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, pv),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), pv),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
@@ -107,23 +103,23 @@ func TestDelete(t *testing.T) {
|
||||
return pv
|
||||
}
|
||||
gracefulSetFn := func() bool {
|
||||
if fakeEtcdClient.Data[key].R.Node == nil {
|
||||
if fakeClient.Data[key].R.Node == nil {
|
||||
return false
|
||||
}
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL == 30
|
||||
return fakeClient.Data[key].R.Node.TTL == 30
|
||||
}
|
||||
test.TestDelete(createFn, gracefulSetFn)
|
||||
}
|
||||
|
||||
func TestEtcdGetPersistentVolumes(t *testing.T) {
|
||||
storage, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
persistentVolume := validNewPersistentVolume("foo")
|
||||
test.TestGet(persistentVolume)
|
||||
}
|
||||
|
||||
func TestEtcdListPersistentVolumes(t *testing.T) {
|
||||
storage, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError).ClusterScope()
|
||||
key := etcdtest.AddPrefix(storage.KeyRootFunc(test.TestContext()))
|
||||
persistentVolume := validNewPersistentVolume("foo")
|
||||
@@ -138,15 +134,15 @@ func TestEtcdListPersistentVolumes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPersistentVolumesDecode(t *testing.T) {
|
||||
storage, _, _, _ := newStorage(t)
|
||||
storage, _, _ := newStorage(t)
|
||||
expected := validNewPersistentVolume("foo")
|
||||
body, err := latest.Codec.Encode(expected)
|
||||
body, err := testapi.Codec().Encode(expected)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := storage.New()
|
||||
if err := latest.Codec.DecodeInto(body, actual); err != nil {
|
||||
if err := testapi.Codec().DecodeInto(body, actual); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -157,12 +153,12 @@ func TestPersistentVolumesDecode(t *testing.T) {
|
||||
|
||||
func TestEtcdUpdatePersistentVolumes(t *testing.T) {
|
||||
ctx := api.NewContext()
|
||||
storage, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
persistentVolume := validChangedPersistentVolume()
|
||||
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewPersistentVolume("foo")), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), validNewPersistentVolume("foo")), 0)
|
||||
|
||||
_, _, err := storage.Update(ctx, persistentVolume)
|
||||
if err != nil {
|
||||
@@ -174,7 +170,7 @@ func TestEtcdUpdatePersistentVolumes(t *testing.T) {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var persistentVolumeOut api.PersistentVolume
|
||||
err = latest.Codec.DecodeInto([]byte(response.Node.Value), &persistentVolumeOut)
|
||||
err = testapi.Codec().DecodeInto([]byte(response.Node.Value), &persistentVolumeOut)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -187,7 +183,7 @@ func TestEtcdUpdatePersistentVolumes(t *testing.T) {
|
||||
|
||||
func TestDeletePersistentVolumes(t *testing.T) {
|
||||
ctx := api.NewContext()
|
||||
storage, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
persistentVolume := validNewPersistentVolume("foo")
|
||||
name := persistentVolume.Name
|
||||
key, _ := storage.KeyFunc(ctx, name)
|
||||
@@ -196,7 +192,7 @@ func TestDeletePersistentVolumes(t *testing.T) {
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, persistentVolume),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), persistentVolume),
|
||||
ModifiedIndex: 1,
|
||||
CreatedIndex: 1,
|
||||
},
|
||||
@@ -209,14 +205,14 @@ func TestDeletePersistentVolumes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdUpdateStatus(t *testing.T) {
|
||||
storage, statusStorage, fakeClient, etcdStorage := newStorage(t)
|
||||
ctx := api.NewContext()
|
||||
storage, statusStorage, fakeClient := newStorage(t)
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
ctx := api.NewContext()
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
pvStart := validNewPersistentVolume("foo")
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, pvStart), 1)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), pvStart), 1)
|
||||
|
||||
pvIn := &api.PersistentVolume{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
@@ -237,12 +233,11 @@ func TestEtcdUpdateStatus(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
var pvOut api.PersistentVolume
|
||||
key, _ = storage.KeyFunc(ctx, "foo")
|
||||
if err := etcdStorage.Get(key, &pvOut, false); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
pvOut, err := storage.Get(ctx, "foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !api.Semantic.DeepEqual(expected, pvOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(expected, pvOut))
|
||||
if !api.Semantic.DeepEqual(&expected, pvOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(&expected, pvOut))
|
||||
}
|
||||
}
|
||||
|
@@ -27,13 +27,12 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// rest implements a RESTStorage for persistentvolumeclaims against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// NewREST returns a RESTStorage object that will work against PersistentVolumeClaim objects.
|
||||
func NewStorage(s storage.Interface) (*REST, *StatusREST) {
|
||||
// NewREST returns a RESTStorage object that will work against persistent volume claims.
|
||||
func NewREST(s storage.Interface) (*REST, *StatusREST) {
|
||||
prefix := "/persistentvolumeclaims"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.PersistentVolumeClaim{} },
|
||||
@@ -52,13 +51,13 @@ func NewStorage(s storage.Interface) (*REST, *StatusREST) {
|
||||
},
|
||||
EndpointName: "persistentvolumeclaims",
|
||||
|
||||
CreateStrategy: persistentvolumeclaim.Strategy,
|
||||
UpdateStrategy: persistentvolumeclaim.Strategy,
|
||||
ReturnDeletedObject: true,
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
store.CreateStrategy = persistentvolumeclaim.Strategy
|
||||
store.UpdateStrategy = persistentvolumeclaim.Strategy
|
||||
store.ReturnDeletedObject = true
|
||||
|
||||
statusStore := *store
|
||||
statusStore.UpdateStrategy = persistentvolumeclaim.StatusStrategy
|
||||
|
||||
|
@@ -20,13 +20,11 @@ import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -34,12 +32,10 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
storage, statusStorage := NewStorage(etcdStorage)
|
||||
return storage, statusStorage, fakeEtcdClient, etcdStorage
|
||||
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
storage, statusStorage := NewREST(etcdStorage)
|
||||
return storage, statusStorage, fakeClient
|
||||
}
|
||||
|
||||
func validNewPersistentVolumeClaim(name, ns string) *api.PersistentVolumeClaim {
|
||||
@@ -70,9 +66,8 @@ func validChangedPersistentVolumeClaim() *api.PersistentVolumeClaim {
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
|
||||
registry, _, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, registry, fakeEtcdClient.SetError)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
pv := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
|
||||
pv.ObjectMeta = api.ObjectMeta{}
|
||||
test.TestCreate(
|
||||
@@ -87,17 +82,17 @@ func TestCreate(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
storage, _, fakeEtcdClient, _ := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
|
||||
pv := validChangedPersistentVolumeClaim()
|
||||
key, _ := storage.KeyFunc(ctx, pv.Name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
createFn := func() runtime.Object {
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, pv),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), pv),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
@@ -105,23 +100,23 @@ func TestDelete(t *testing.T) {
|
||||
return pv
|
||||
}
|
||||
gracefulSetFn := func() bool {
|
||||
if fakeEtcdClient.Data[key].R.Node == nil {
|
||||
if fakeClient.Data[key].R.Node == nil {
|
||||
return false
|
||||
}
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL == 30
|
||||
return fakeClient.Data[key].R.Node.TTL == 30
|
||||
}
|
||||
test.TestDelete(createFn, gracefulSetFn)
|
||||
}
|
||||
|
||||
func TestEtcdGetPersistentVolumeClaims(t *testing.T) {
|
||||
storage, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
claim := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
|
||||
test.TestGet(claim)
|
||||
}
|
||||
|
||||
func TestEtcdListPersistentVolumeClaims(t *testing.T) {
|
||||
storage, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key := etcdtest.AddPrefix(storage.KeyRootFunc(test.TestContext()))
|
||||
claim := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
|
||||
@@ -136,15 +131,15 @@ func TestEtcdListPersistentVolumeClaims(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPersistentVolumeClaimsDecode(t *testing.T) {
|
||||
registry, _, _, _ := newStorage(t)
|
||||
storage, _, _ := newStorage(t)
|
||||
expected := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
|
||||
body, err := latest.Codec.Encode(expected)
|
||||
body, err := testapi.Codec().Encode(expected)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := registry.New()
|
||||
if err := latest.Codec.DecodeInto(body, actual); err != nil {
|
||||
actual := storage.New()
|
||||
if err := testapi.Codec().DecodeInto(body, actual); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -155,14 +150,14 @@ func TestPersistentVolumeClaimsDecode(t *testing.T) {
|
||||
|
||||
func TestEtcdUpdatePersistentVolumeClaims(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
registry, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
persistentVolume := validChangedPersistentVolumeClaim()
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewPersistentVolumeClaim("foo", api.NamespaceDefault)), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), validNewPersistentVolumeClaim("foo", api.NamespaceDefault)), 0)
|
||||
|
||||
_, _, err := registry.Update(ctx, persistentVolume)
|
||||
_, _, err := storage.Update(ctx, persistentVolume)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -172,7 +167,7 @@ func TestEtcdUpdatePersistentVolumeClaims(t *testing.T) {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var persistentVolumeOut api.PersistentVolumeClaim
|
||||
err = latest.Codec.DecodeInto([]byte(response.Node.Value), &persistentVolumeOut)
|
||||
err = testapi.Codec().DecodeInto([]byte(response.Node.Value), &persistentVolumeOut)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -185,36 +180,36 @@ func TestEtcdUpdatePersistentVolumeClaims(t *testing.T) {
|
||||
|
||||
func TestDeletePersistentVolumeClaims(t *testing.T) {
|
||||
ctx := api.NewDefaultContext()
|
||||
registry, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
pvClaim := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
|
||||
name := pvClaim.Name
|
||||
key, _ := registry.KeyFunc(ctx, name)
|
||||
key, _ := storage.KeyFunc(ctx, name)
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.ChangeIndex = 1
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, pvClaim),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), pvClaim),
|
||||
ModifiedIndex: 1,
|
||||
CreatedIndex: 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
_, err := registry.Delete(ctx, name, nil)
|
||||
_, err := storage.Delete(ctx, name, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdUpdateStatus(t *testing.T) {
|
||||
storage, statusStorage, fakeClient, etcdStorage := newStorage(t)
|
||||
storage, statusStorage, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
pvcStart := validNewPersistentVolumeClaim("foo", api.NamespaceDefault)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, pvcStart), 1)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), pvcStart), 1)
|
||||
|
||||
pvc := &api.PersistentVolumeClaim{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
@@ -244,12 +239,11 @@ func TestEtcdUpdateStatus(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
var pvcOut api.PersistentVolumeClaim
|
||||
key, _ = storage.KeyFunc(ctx, "foo")
|
||||
if err := etcdStorage.Get(key, &pvcOut, false); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
pvcOut, err := storage.Get(ctx, "foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !api.Semantic.DeepEqual(expected, pvcOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(expected, pvcOut))
|
||||
if !api.Semantic.DeepEqual(&expected, pvcOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(&expected, pvcOut))
|
||||
}
|
||||
}
|
||||
|
@@ -25,17 +25,15 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
etcderrors "k8s.io/kubernetes/pkg/api/errors/etcd"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/rest"
|
||||
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
"k8s.io/kubernetes/pkg/registry/pod"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/securitycontext"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -43,17 +41,10 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
}
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *BindingREST, *StatusREST, *tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
func newStorage(t *testing.T) (*REST, *BindingREST, *StatusREST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil)
|
||||
return storage.Pod, storage.Binding, storage.Status, fakeEtcdClient, etcdStorage
|
||||
return storage.Pod, storage.Binding, storage.Status, fakeClient
|
||||
}
|
||||
|
||||
func validNewPod() *api.Pod {
|
||||
@@ -89,14 +80,13 @@ func validChangedPod() *api.Pod {
|
||||
}
|
||||
|
||||
func TestStorage(t *testing.T) {
|
||||
storage, _, _, _, _ := newStorage(t)
|
||||
storage, _, _, _ := newStorage(t)
|
||||
pod.NewRegistry(storage)
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
pod := validNewPod()
|
||||
pod.ObjectMeta = api.ObjectMeta{}
|
||||
// Make an invalid pod with an an incorrect label.
|
||||
@@ -120,19 +110,18 @@ func TestCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
|
||||
createFn := func() runtime.Object {
|
||||
pod := validChangedPod()
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, pod),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), pod),
|
||||
ModifiedIndex: 1,
|
||||
},
|
||||
},
|
||||
@@ -140,10 +129,10 @@ func TestDelete(t *testing.T) {
|
||||
return pod
|
||||
}
|
||||
gracefulSetFn := func() bool {
|
||||
if fakeEtcdClient.Data[key].R.Node == nil {
|
||||
if fakeClient.Data[key].R.Node == nil {
|
||||
return false
|
||||
}
|
||||
return fakeEtcdClient.Data[key].R.Node.TTL == 30
|
||||
return fakeClient.Data[key].R.Node.TTL == 30
|
||||
}
|
||||
test.TestDelete(createFn, gracefulSetFn)
|
||||
}
|
||||
@@ -158,31 +147,29 @@ func expectPod(t *testing.T, out runtime.Object) (*api.Pod, bool) {
|
||||
}
|
||||
|
||||
func TestCreateRegistryError(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
fakeEtcdClient.Err = fmt.Errorf("test error")
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
fakeClient.Err = fmt.Errorf("test error")
|
||||
|
||||
pod := validNewPod()
|
||||
_, err := storage.Create(api.NewDefaultContext(), pod)
|
||||
if err != fakeEtcdClient.Err {
|
||||
if err != fakeClient.Err {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSetsFields(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
pod := validNewPod()
|
||||
_, err := storage.Create(api.NewDefaultContext(), pod)
|
||||
if err != fakeEtcdClient.Err {
|
||||
if err != fakeClient.Err {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
ctx := api.NewDefaultContext()
|
||||
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
actual := &api.Pod{}
|
||||
if err := etcdStorage.Get(key, actual, false); err != nil {
|
||||
t.Fatalf("unexpected extraction error: %v", err)
|
||||
object, err := storage.Get(ctx, "foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actual := object.(*api.Pod)
|
||||
if actual.Name != pod.Name {
|
||||
t.Errorf("unexpected pod: %#v", actual)
|
||||
}
|
||||
@@ -192,16 +179,15 @@ func TestCreateSetsFields(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPodDecode(t *testing.T) {
|
||||
_, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
storage, _, _, _ := newStorage(t)
|
||||
expected := validNewPod()
|
||||
body, err := latest.Codec.Encode(expected)
|
||||
body, err := testapi.Codec().Encode(expected)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := storage.New()
|
||||
if err := latest.Codec.DecodeInto(body, actual); err != nil {
|
||||
if err := testapi.Codec().DecodeInto(body, actual); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -211,15 +197,14 @@ func TestPodDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateWithConflictingNamespace(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "default"},
|
||||
Spec: api.PodSpec{NodeName: "machine"},
|
||||
}),
|
||||
@@ -336,14 +321,13 @@ func TestResourceLocation(t *testing.T) {
|
||||
|
||||
ctx := api.NewDefaultContext()
|
||||
for _, tc := range testCases {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &tc.pod),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &tc.pod),
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -367,16 +351,15 @@ func TestResourceLocation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeletePod(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
fakeEtcdClient.ChangeIndex = 1
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
fakeClient.ChangeIndex = 1
|
||||
ctx := api.NewDefaultContext()
|
||||
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Namespace: api.NamespaceDefault,
|
||||
@@ -395,34 +378,32 @@ func TestDeletePod(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdGet(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
pod := validNewPod()
|
||||
test.TestGet(pod)
|
||||
}
|
||||
|
||||
func TestEtcdList(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage, nil).Pod
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key := etcdtest.AddPrefix(storage.Etcd.KeyRootFunc(test.TestContext()))
|
||||
pod := validNewPod()
|
||||
test.TestList(
|
||||
pod,
|
||||
func(objects []runtime.Object) []runtime.Object {
|
||||
return registrytest.SetObjectsForKey(fakeEtcdClient, key, objects)
|
||||
return registrytest.SetObjectsForKey(fakeClient, key, objects)
|
||||
},
|
||||
func(resourceVersion uint64) {
|
||||
registrytest.SetResourceVersion(fakeEtcdClient, resourceVersion)
|
||||
registrytest.SetResourceVersion(fakeClient, resourceVersion)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEtcdCreate(t *testing.T) {
|
||||
registry, bindingRegistry, _, fakeClient, _ := newStorage(t)
|
||||
storage, bindingStorage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
@@ -430,13 +411,13 @@ func TestEtcdCreate(t *testing.T) {
|
||||
},
|
||||
E: tools.EtcdErrorNotFound,
|
||||
}
|
||||
_, err := registry.Create(ctx, validNewPod())
|
||||
_, err := storage.Create(ctx, validNewPod())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Suddenly, a wild scheduler appears:
|
||||
_, err = bindingRegistry.Create(ctx, &api.Binding{
|
||||
_, err = bindingStorage.Create(ctx, &api.Binding{
|
||||
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
|
||||
Target: api.ObjectReference{Name: "machine"},
|
||||
})
|
||||
@@ -449,7 +430,7 @@ func TestEtcdCreate(t *testing.T) {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var pod api.Pod
|
||||
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &pod)
|
||||
err = testapi.Codec().DecodeInto([]byte(resp.Node.Value), &pod)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -457,17 +438,16 @@ func TestEtcdCreate(t *testing.T) {
|
||||
if pod.Name != "foo" {
|
||||
t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Ensure that when scheduler creates a binding for a pod that has already been deleted
|
||||
// by the API server, API server returns not-found error.
|
||||
func TestEtcdCreateBindingNoPod(t *testing.T) {
|
||||
registry, bindingRegistry, _, fakeClient, _ := newStorage(t)
|
||||
storage, bindingStorage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
@@ -479,7 +459,7 @@ func TestEtcdCreateBindingNoPod(t *testing.T) {
|
||||
// - Create (apiserver)
|
||||
// - Schedule (scheduler)
|
||||
// - Delete (apiserver)
|
||||
_, err := bindingRegistry.Create(ctx, &api.Binding{
|
||||
_, err := bindingStorage.Create(ctx, &api.Binding{
|
||||
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
|
||||
Target: api.ObjectReference{Name: "machine"},
|
||||
})
|
||||
@@ -490,7 +470,7 @@ func TestEtcdCreateBindingNoPod(t *testing.T) {
|
||||
t.Fatalf("Unexpected error returned: %#v", err)
|
||||
}
|
||||
|
||||
_, err = registry.Get(ctx, "foo")
|
||||
_, err = storage.Get(ctx, "foo")
|
||||
if err == nil {
|
||||
t.Fatalf("Expected not-found-error but got nothing")
|
||||
}
|
||||
@@ -500,11 +480,11 @@ func TestEtcdCreateBindingNoPod(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdCreateFailsWithoutNamespace(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
fakeClient.TestIndex = true
|
||||
pod := validNewPod()
|
||||
pod.Namespace = ""
|
||||
_, err := registry.Create(api.NewContext(), pod)
|
||||
_, err := storage.Create(api.NewContext(), pod)
|
||||
// Accept "namespace" or "Namespace".
|
||||
if err == nil || !strings.Contains(err.Error(), "amespace") {
|
||||
t.Fatalf("expected error that namespace was missing from context, got: %v", err)
|
||||
@@ -512,29 +492,29 @@ func TestEtcdCreateFailsWithoutNamespace(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdCreateAlreadyExisting(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}),
|
||||
},
|
||||
},
|
||||
E: nil,
|
||||
}
|
||||
_, err := registry.Create(ctx, validNewPod())
|
||||
_, err := storage.Create(ctx, validNewPod())
|
||||
if !errors.IsAlreadyExists(err) {
|
||||
t.Errorf("Unexpected error returned: %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdCreateWithContainersNotFound(t *testing.T) {
|
||||
registry, bindingRegistry, _, fakeClient, _ := newStorage(t)
|
||||
storage, bindingStorage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
@@ -542,13 +522,13 @@ func TestEtcdCreateWithContainersNotFound(t *testing.T) {
|
||||
},
|
||||
E: tools.EtcdErrorNotFound,
|
||||
}
|
||||
_, err := registry.Create(ctx, validNewPod())
|
||||
_, err := storage.Create(ctx, validNewPod())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Suddenly, a wild scheduler appears:
|
||||
_, err = bindingRegistry.Create(ctx, &api.Binding{
|
||||
_, err = bindingStorage.Create(ctx, &api.Binding{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: api.NamespaceDefault,
|
||||
Name: "foo",
|
||||
@@ -565,7 +545,7 @@ func TestEtcdCreateWithContainersNotFound(t *testing.T) {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var pod api.Pod
|
||||
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &pod)
|
||||
err = testapi.Codec().DecodeInto([]byte(resp.Node.Value), &pod)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -579,10 +559,10 @@ func TestEtcdCreateWithContainersNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdCreateWithConflict(t *testing.T) {
|
||||
registry, bindingRegistry, _, fakeClient, _ := newStorage(t)
|
||||
storage, bindingStorage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: nil,
|
||||
@@ -590,7 +570,7 @@ func TestEtcdCreateWithConflict(t *testing.T) {
|
||||
E: tools.EtcdErrorNotFound,
|
||||
}
|
||||
|
||||
_, err := registry.Create(ctx, validNewPod())
|
||||
_, err := storage.Create(ctx, validNewPod())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -604,22 +584,22 @@ func TestEtcdCreateWithConflict(t *testing.T) {
|
||||
},
|
||||
Target: api.ObjectReference{Name: "machine"},
|
||||
}
|
||||
_, err = bindingRegistry.Create(ctx, &binding)
|
||||
_, err = bindingStorage.Create(ctx, &binding)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
_, err = bindingRegistry.Create(ctx, &binding)
|
||||
_, err = bindingStorage.Create(ctx, &binding)
|
||||
if err == nil || !errors.IsConflict(err) {
|
||||
t.Fatalf("expected resource conflict error, not: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdCreateWithExistingContainers(t *testing.T) {
|
||||
registry, bindingRegistry, _, fakeClient, _ := newStorage(t)
|
||||
storage, bindingStorage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
@@ -627,13 +607,13 @@ func TestEtcdCreateWithExistingContainers(t *testing.T) {
|
||||
},
|
||||
E: tools.EtcdErrorNotFound,
|
||||
}
|
||||
_, err := registry.Create(ctx, validNewPod())
|
||||
_, err := storage.Create(ctx, validNewPod())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Suddenly, a wild scheduler appears:
|
||||
_, err = bindingRegistry.Create(ctx, &api.Binding{
|
||||
_, err = bindingStorage.Create(ctx, &api.Binding{
|
||||
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
|
||||
Target: api.ObjectReference{Name: "machine"},
|
||||
})
|
||||
@@ -646,7 +626,7 @@ func TestEtcdCreateWithExistingContainers(t *testing.T) {
|
||||
t.Fatalf("Unexpected error %v", err)
|
||||
}
|
||||
var pod api.Pod
|
||||
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &pod)
|
||||
err = testapi.Codec().DecodeInto([]byte(resp.Node.Value), &pod)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -657,7 +637,7 @@ func TestEtcdCreateWithExistingContainers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdCreateBinding(t *testing.T) {
|
||||
registry, bindingRegistry, _, fakeClient, _ := newStorage(t)
|
||||
storage, bindingStorage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
@@ -702,7 +682,7 @@ func TestEtcdCreateBinding(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for k, test := range testCases {
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
@@ -710,14 +690,14 @@ func TestEtcdCreateBinding(t *testing.T) {
|
||||
},
|
||||
E: tools.EtcdErrorNotFound,
|
||||
}
|
||||
if _, err := registry.Create(ctx, validNewPod()); err != nil {
|
||||
if _, err := storage.Create(ctx, validNewPod()); err != nil {
|
||||
t.Fatalf("%s: unexpected error: %v", k, err)
|
||||
}
|
||||
if _, err := bindingRegistry.Create(ctx, &test.binding); !test.errOK(err) {
|
||||
if _, err := bindingStorage.Create(ctx, &test.binding); !test.errOK(err) {
|
||||
t.Errorf("%s: unexpected error: %v", k, err)
|
||||
} else if err == nil {
|
||||
// If bind succeeded, verify Host field in pod's Spec.
|
||||
pod, err := registry.Get(ctx, validNewPod().ObjectMeta.Name)
|
||||
pod, err := storage.Get(ctx, validNewPod().ObjectMeta.Name)
|
||||
if err != nil {
|
||||
t.Errorf("%s: unexpected error: %v", k, err)
|
||||
} else if pod.(*api.Pod).Spec.NodeName != test.binding.Target.Name {
|
||||
@@ -728,11 +708,11 @@ func TestEtcdCreateBinding(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdUpdateNotFound(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{},
|
||||
@@ -748,23 +728,23 @@ func TestEtcdUpdateNotFound(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
_, _, err := registry.Update(ctx, &podIn)
|
||||
_, _, err := storage.Update(ctx, &podIn)
|
||||
if err == nil {
|
||||
t.Errorf("unexpected non-error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdUpdateNotScheduled(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewPod()), 1)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), validNewPod()), 1)
|
||||
|
||||
podIn := validChangedPod()
|
||||
_, _, err := registry.Update(ctx, podIn)
|
||||
_, _, err := storage.Update(ctx, podIn)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
@@ -773,20 +753,20 @@ func TestEtcdUpdateNotScheduled(t *testing.T) {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
podOut := &api.Pod{}
|
||||
latest.Codec.DecodeInto([]byte(response.Node.Value), podOut)
|
||||
testapi.Codec().DecodeInto([]byte(response.Node.Value), podOut)
|
||||
if !api.Semantic.DeepEqual(podOut, podIn) {
|
||||
t.Errorf("objects differ: %v", util.ObjectDiff(podOut, podIn))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdUpdateScheduled(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Namespace: api.NamespaceDefault,
|
||||
@@ -826,7 +806,7 @@ func TestEtcdUpdateScheduled(t *testing.T) {
|
||||
DNSPolicy: api.DNSClusterFirst,
|
||||
},
|
||||
}
|
||||
_, _, err := registry.Update(ctx, &podIn)
|
||||
_, _, err := storage.Update(ctx, &podIn)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
@@ -835,7 +815,7 @@ func TestEtcdUpdateScheduled(t *testing.T) {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
var podOut api.Pod
|
||||
latest.Codec.DecodeInto([]byte(response.Node.Value), &podOut)
|
||||
testapi.Codec().DecodeInto([]byte(response.Node.Value), &podOut)
|
||||
if !api.Semantic.DeepEqual(podOut, podIn) {
|
||||
t.Errorf("expected: %#v, got: %#v", podOut, podIn)
|
||||
}
|
||||
@@ -843,11 +823,11 @@ func TestEtcdUpdateScheduled(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdUpdateStatus(t *testing.T) {
|
||||
registry, _, status, fakeClient, etcdStorage := newStorage(t)
|
||||
storage, _, statusStorage, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
podStart := api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
@@ -864,7 +844,7 @@ func TestEtcdUpdateStatus(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &podStart), 1)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &podStart), 1)
|
||||
|
||||
podIn := api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
@@ -900,32 +880,31 @@ func TestEtcdUpdateStatus(t *testing.T) {
|
||||
expected.Labels = podIn.Labels
|
||||
expected.Status = podIn.Status
|
||||
|
||||
_, _, err := status.Update(ctx, &podIn)
|
||||
_, _, err := statusStorage.Update(ctx, &podIn)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
var podOut api.Pod
|
||||
key, _ = registry.KeyFunc(ctx, "foo")
|
||||
if err := etcdStorage.Get(key, &podOut, false); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
podOut, err := storage.Get(ctx, "foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if !api.Semantic.DeepEqual(expected, podOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(expected, podOut))
|
||||
if !api.Semantic.DeepEqual(&expected, podOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(&expected, podOut))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdDeletePod(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
||||
Spec: api.PodSpec{NodeName: "machine"},
|
||||
}), 0)
|
||||
_, err := registry.Delete(ctx, "foo", api.NewDeleteOptions(0))
|
||||
_, err := storage.Delete(ctx, "foo", api.NewDeleteOptions(0))
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -938,16 +917,16 @@ func TestEtcdDeletePod(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdDeletePodMultipleContainers(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Pod{
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{Name: "foo"},
|
||||
Spec: api.PodSpec{NodeName: "machine"},
|
||||
}), 0)
|
||||
_, err := registry.Delete(ctx, "foo", api.NewDeleteOptions(0))
|
||||
_, err := storage.Delete(ctx, "foo", api.NewDeleteOptions(0))
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -961,9 +940,9 @@ func TestEtcdDeletePodMultipleContainers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdWatchPods(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
watching, err := registry.Watch(ctx,
|
||||
watching, err := storage.Watch(ctx,
|
||||
labels.Everything(),
|
||||
fields.Everything(),
|
||||
"1",
|
||||
@@ -988,9 +967,9 @@ func TestEtcdWatchPods(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdWatchPodsMatch(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
watching, err := registry.Watch(ctx,
|
||||
watching, err := storage.Watch(ctx,
|
||||
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
||||
fields.Everything(),
|
||||
"1",
|
||||
@@ -1008,7 +987,7 @@ func TestEtcdWatchPodsMatch(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
podBytes, _ := latest.Codec.Encode(pod)
|
||||
podBytes, _ := testapi.Codec().Encode(pod)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
@@ -1027,9 +1006,9 @@ func TestEtcdWatchPodsMatch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdWatchPodsNotMatch(t *testing.T) {
|
||||
registry, _, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
watching, err := registry.Watch(ctx,
|
||||
watching, err := storage.Watch(ctx,
|
||||
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
||||
fields.Everything(),
|
||||
"1",
|
||||
@@ -1047,7 +1026,7 @@ func TestEtcdWatchPodsNotMatch(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
podBytes, _ := latest.Codec.Encode(pod)
|
||||
podBytes, _ := testapi.Codec().Encode(pod)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
|
@@ -27,7 +27,6 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// rest implements a RESTStorage for pod templates against etcd
|
||||
type REST struct {
|
||||
etcdgeneric.Etcd
|
||||
}
|
||||
@@ -58,6 +57,5 @@ func NewREST(s storage.Interface) *REST {
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
return &REST{store}
|
||||
}
|
||||
|
@@ -21,18 +21,14 @@ import (
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, testapi.Codec(), etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage), fakeClient
|
||||
}
|
||||
|
||||
func validNewPodTemplate(name string) *api.PodTemplate {
|
||||
@@ -63,9 +59,8 @@ func validNewPodTemplate(name string) *api.PodTemplate {
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewREST(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
pod := validNewPodTemplate("foo")
|
||||
pod.ObjectMeta = api.ObjectMeta{}
|
||||
test.TestCreate(
|
||||
@@ -79,17 +74,16 @@ func TestCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewREST(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key, err := storage.KeyFunc(test.TestContext(), "foo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key = etcdtest.AddPrefix(key)
|
||||
|
||||
fakeEtcdClient.ExpectNotFoundGet(key)
|
||||
fakeEtcdClient.ChangeIndex = 2
|
||||
fakeClient.ExpectNotFoundGet(key)
|
||||
fakeClient.ChangeIndex = 2
|
||||
pod := validNewPodTemplate("foo")
|
||||
existing := validNewPodTemplate("exists")
|
||||
existing.Namespace = test.TestNamespace()
|
||||
|
@@ -17,13 +17,25 @@ limitations under the License.
|
||||
package registrytest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
)
|
||||
|
||||
func NewEtcdStorage(t *testing.T) (storage.Interface, *tools.FakeEtcdClient) {
|
||||
fakeClient := tools.NewFakeEtcdClient(t)
|
||||
fakeClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeClient, testapi.Codec(), etcdtest.PathPrefix())
|
||||
return etcdStorage, fakeClient
|
||||
}
|
||||
|
||||
func SetResourceVersion(fakeClient *tools.FakeEtcdClient, resourceVersion uint64) {
|
||||
fakeClient.ChangeIndex = resourceVersion
|
||||
}
|
||||
@@ -33,8 +45,8 @@ func SetObjectsForKey(fakeClient *tools.FakeEtcdClient, key string, objects []ru
|
||||
if len(objects) > 0 {
|
||||
nodes := make([]*etcd.Node, len(objects))
|
||||
for i, obj := range objects {
|
||||
encoded := runtime.EncodeOrDie(latest.Codec, obj)
|
||||
decoded, _ := latest.Codec.Decode([]byte(encoded))
|
||||
encoded := runtime.EncodeOrDie(testapi.Codec(), obj)
|
||||
decoded, _ := testapi.Codec().Decode([]byte(encoded))
|
||||
nodes[i] = &etcd.Node{Value: encoded}
|
||||
result[i] = decoded
|
||||
}
|
||||
|
@@ -27,13 +27,12 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// rest implements a RESTStorage for resourcequotas against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// NewStorage returns a RESTStorage object that will work against ResourceQuota objects.
|
||||
func NewStorage(s storage.Interface) (*REST, *StatusREST) {
|
||||
// NewREST returns a RESTStorage object that will work against resource quotas.
|
||||
func NewREST(s storage.Interface) (*REST, *StatusREST) {
|
||||
prefix := "/resourcequotas"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.ResourceQuota{} },
|
||||
@@ -52,13 +51,13 @@ func NewStorage(s storage.Interface) (*REST, *StatusREST) {
|
||||
},
|
||||
EndpointName: "resourcequotas",
|
||||
|
||||
CreateStrategy: resourcequota.Strategy,
|
||||
UpdateStrategy: resourcequota.Strategy,
|
||||
ReturnDeletedObject: true,
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
store.CreateStrategy = resourcequota.Strategy
|
||||
store.UpdateStrategy = resourcequota.Strategy
|
||||
store.ReturnDeletedObject = true
|
||||
|
||||
statusStore := *store
|
||||
statusStore.UpdateStrategy = resourcequota.StatusStrategy
|
||||
|
||||
|
@@ -24,16 +24,14 @@ import (
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"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"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/registry/resourcequota"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
"k8s.io/kubernetes/pkg/util"
|
||||
@@ -41,17 +39,10 @@ import (
|
||||
"github.com/coreos/go-etcd/etcd"
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
}
|
||||
|
||||
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient, h := newEtcdStorage(t)
|
||||
storage, statusStorage := NewStorage(h)
|
||||
return storage, statusStorage, fakeEtcdClient, h
|
||||
func newStorage(t *testing.T) (*REST, *StatusREST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
storage, statusStorage := NewREST(etcdStorage)
|
||||
return storage, statusStorage, fakeClient
|
||||
}
|
||||
|
||||
func validNewResourceQuota() *api.ResourceQuota {
|
||||
@@ -83,14 +74,13 @@ func validChangedResourceQuota() *api.ResourceQuota {
|
||||
}
|
||||
|
||||
func TestStorage(t *testing.T) {
|
||||
storage, _, _, _ := newStorage(t)
|
||||
storage, _, _ := newStorage(t)
|
||||
resourcequota.NewRegistry(storage)
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage, _ := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
resourcequota := validNewResourceQuota()
|
||||
resourcequota.ObjectMeta = api.ObjectMeta{}
|
||||
test.TestCreate(
|
||||
@@ -113,32 +103,30 @@ func expectResourceQuota(t *testing.T, out runtime.Object) (*api.ResourceQuota,
|
||||
}
|
||||
|
||||
func TestCreateRegistryError(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
fakeEtcdClient.Err = fmt.Errorf("test error")
|
||||
storage, _ := NewStorage(etcdStorage)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
fakeClient.Err = fmt.Errorf("test error")
|
||||
|
||||
resourcequota := validNewResourceQuota()
|
||||
_, err := storage.Create(api.NewDefaultContext(), resourcequota)
|
||||
if err != fakeEtcdClient.Err {
|
||||
if err != fakeClient.Err {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSetsFields(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage, _ := NewStorage(etcdStorage)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
resourcequota := validNewResourceQuota()
|
||||
_, err := storage.Create(api.NewDefaultContext(), resourcequota)
|
||||
if err != fakeEtcdClient.Err {
|
||||
if err != fakeClient.Err {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := &api.ResourceQuota{}
|
||||
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
if err := etcdStorage.Get(key, actual, false); err != nil {
|
||||
t.Fatalf("unexpected extraction error: %v", err)
|
||||
object, err := storage.Get(ctx, "foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actual := object.(*api.ResourceQuota)
|
||||
if actual.Name != resourcequota.Name {
|
||||
t.Errorf("unexpected resourcequota: %#v", actual)
|
||||
}
|
||||
@@ -148,16 +136,15 @@ func TestCreateSetsFields(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResourceQuotaDecode(t *testing.T) {
|
||||
_, etcdStorage := newEtcdStorage(t)
|
||||
storage, _ := NewStorage(etcdStorage)
|
||||
storage, _, _ := newStorage(t)
|
||||
expected := validNewResourceQuota()
|
||||
body, err := latest.Codec.Encode(expected)
|
||||
body, err := testapi.Codec().Encode(expected)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
actual := storage.New()
|
||||
if err := latest.Codec.DecodeInto(body, actual); err != nil {
|
||||
if err := testapi.Codec().DecodeInto(body, actual); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
@@ -167,16 +154,15 @@ func TestResourceQuotaDecode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDeleteResourceQuota(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
fakeEtcdClient.ChangeIndex = 1
|
||||
storage, _ := NewStorage(etcdStorage)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
fakeClient.ChangeIndex = 1
|
||||
ctx := api.NewDefaultContext()
|
||||
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.ResourceQuota{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "foo",
|
||||
Namespace: api.NamespaceDefault,
|
||||
@@ -195,35 +181,32 @@ func TestDeleteResourceQuota(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdGet(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage, _ := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
resourcequota := validNewResourceQuota()
|
||||
test.TestGet(resourcequota)
|
||||
}
|
||||
|
||||
func TestEtcdList(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage, _ := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key := etcdtest.AddPrefix(storage.Etcd.KeyRootFunc(test.TestContext()))
|
||||
resourcequota := validNewResourceQuota()
|
||||
test.TestList(
|
||||
resourcequota,
|
||||
func(objects []runtime.Object) []runtime.Object {
|
||||
return registrytest.SetObjectsForKey(fakeEtcdClient, key, objects)
|
||||
return registrytest.SetObjectsForKey(fakeClient, key, objects)
|
||||
},
|
||||
func(resourceVersion uint64) {
|
||||
registrytest.SetResourceVersion(fakeEtcdClient, resourceVersion)
|
||||
registrytest.SetResourceVersion(fakeClient, resourceVersion)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEtcdCreateFailsWithoutNamespace(t *testing.T) {
|
||||
registry, _, fakeClient, _ := newStorage(t)
|
||||
fakeClient.TestIndex = true
|
||||
storage, _, _ := newStorage(t)
|
||||
resourcequota := validNewResourceQuota()
|
||||
resourcequota.Namespace = ""
|
||||
_, err := registry.Create(api.NewContext(), resourcequota)
|
||||
_, err := storage.Create(api.NewContext(), resourcequota)
|
||||
// Accept "namespace" or "Namespace".
|
||||
if err == nil || !strings.Contains(err.Error(), "amespace") {
|
||||
t.Fatalf("expected error that namespace was missing from context, got: %v", err)
|
||||
@@ -231,33 +214,32 @@ func TestEtcdCreateFailsWithoutNamespace(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdCreateAlreadyExisting(t *testing.T) {
|
||||
registry, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Data[key] = tools.EtcdResponseWithError{
|
||||
R: &etcd.Response{
|
||||
Node: &etcd.Node{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "foo"}}),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), &api.ResourceQuota{ObjectMeta: api.ObjectMeta{Name: "foo"}}),
|
||||
},
|
||||
},
|
||||
E: nil,
|
||||
}
|
||||
_, err := registry.Create(ctx, validNewResourceQuota())
|
||||
_, err := storage.Create(ctx, validNewResourceQuota())
|
||||
if !errors.IsAlreadyExists(err) {
|
||||
t.Errorf("Unexpected error returned: %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdUpdateStatus(t *testing.T) {
|
||||
registry, status, fakeClient, etcdStorage := newStorage(t)
|
||||
storage, status, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
fakeClient.TestIndex = true
|
||||
|
||||
key, _ := registry.KeyFunc(ctx, "foo")
|
||||
key, _ := storage.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
resourcequotaStart := validNewResourceQuota()
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, resourcequotaStart), 1)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), resourcequotaStart), 1)
|
||||
|
||||
resourcequotaIn := &api.ResourceQuota{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
@@ -294,20 +276,16 @@ func TestEtcdUpdateStatus(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
var resourcequotaOut api.ResourceQuota
|
||||
key, _ = registry.KeyFunc(ctx, "foo")
|
||||
if err := etcdStorage.Get(key, &resourcequotaOut, false); err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
if !api.Semantic.DeepEqual(expected, resourcequotaOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(expected, resourcequotaOut))
|
||||
rqOut, err := storage.Get(ctx, "foo")
|
||||
if !api.Semantic.DeepEqual(&expected, rqOut) {
|
||||
t.Errorf("unexpected object: %s", util.ObjectDiff(&expected, rqOut))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEtcdWatchResourceQuotas(t *testing.T) {
|
||||
registry, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
watching, err := registry.Watch(ctx,
|
||||
watching, err := storage.Watch(ctx,
|
||||
labels.Everything(),
|
||||
fields.Everything(),
|
||||
"1",
|
||||
@@ -332,9 +310,9 @@ func TestEtcdWatchResourceQuotas(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdWatchResourceQuotasMatch(t *testing.T) {
|
||||
registry, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
watching, err := registry.Watch(ctx,
|
||||
watching, err := storage.Watch(ctx,
|
||||
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
||||
fields.Everything(),
|
||||
"1",
|
||||
@@ -352,7 +330,7 @@ func TestEtcdWatchResourceQuotasMatch(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
resourcequotaBytes, _ := latest.Codec.Encode(resourcequota)
|
||||
resourcequotaBytes, _ := testapi.Codec().Encode(resourcequota)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
@@ -371,9 +349,9 @@ func TestEtcdWatchResourceQuotasMatch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEtcdWatchResourceQuotasNotMatch(t *testing.T) {
|
||||
registry, _, fakeClient, _ := newStorage(t)
|
||||
storage, _, fakeClient := newStorage(t)
|
||||
ctx := api.NewDefaultContext()
|
||||
watching, err := registry.Watch(ctx,
|
||||
watching, err := storage.Watch(ctx,
|
||||
labels.SelectorFromSet(labels.Set{"name": "foo"}),
|
||||
fields.Everything(),
|
||||
"1",
|
||||
@@ -391,7 +369,7 @@ func TestEtcdWatchResourceQuotasNotMatch(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
resourcequotaBytes, _ := latest.Codec.Encode(resourcequota)
|
||||
resourcequotaBytes, _ := testapi.Codec().Encode(resourcequota)
|
||||
fakeClient.WatchResponse <- &etcd.Response{
|
||||
Action: "create",
|
||||
Node: &etcd.Node{
|
||||
|
@@ -27,13 +27,12 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// REST implements a RESTStorage for secrets against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
// NewStorage returns a registry which will store Secret in the given etcdStorage
|
||||
func NewStorage(s storage.Interface) *REST {
|
||||
// NewREST returns a RESTStorage object that will work against secrets.
|
||||
func NewREST(s storage.Interface) *REST {
|
||||
prefix := "/secrets"
|
||||
|
||||
store := &etcdgeneric.Etcd{
|
||||
@@ -53,11 +52,10 @@ func NewStorage(s storage.Interface) *REST {
|
||||
},
|
||||
EndpointName: "secrets",
|
||||
|
||||
CreateStrategy: secret.Strategy,
|
||||
UpdateStrategy: secret.Strategy,
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
store.CreateStrategy = secret.Strategy
|
||||
store.UpdateStrategy = secret.Strategy
|
||||
|
||||
return &REST{store}
|
||||
}
|
||||
|
@@ -21,18 +21,14 @@ import (
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, testapi.Codec(), etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage), fakeClient
|
||||
}
|
||||
|
||||
func validNewSecret(name string) *api.Secret {
|
||||
@@ -48,9 +44,8 @@ func validNewSecret(name string) *api.Secret {
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
secret := validNewSecret("foo")
|
||||
secret.ObjectMeta = api.ObjectMeta{GenerateName: "foo-"}
|
||||
test.TestCreate(
|
||||
@@ -70,17 +65,16 @@ func TestCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key, err := storage.KeyFunc(test.TestContext(), "foo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key = etcdtest.AddPrefix(key)
|
||||
|
||||
fakeEtcdClient.ExpectNotFoundGet(key)
|
||||
fakeEtcdClient.ChangeIndex = 2
|
||||
fakeClient.ExpectNotFoundGet(key)
|
||||
fakeClient.ChangeIndex = 2
|
||||
secret := validNewSecret("foo")
|
||||
existing := validNewSecret("exists")
|
||||
existing.Namespace = test.TestNamespace()
|
||||
|
@@ -33,7 +33,8 @@ type REST struct {
|
||||
etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
func NewStorage(s storage.Interface) *REST {
|
||||
// NewREST returns a RESTStorage object that will work against services.
|
||||
func NewREST(s storage.Interface) *REST {
|
||||
prefix := "/services/specs"
|
||||
store := etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.Service{} },
|
||||
@@ -60,6 +61,7 @@ func NewStorage(s storage.Interface) *REST {
|
||||
return &REST{store}
|
||||
}
|
||||
|
||||
// FIXME: Move it.
|
||||
func MatchServices(label labels.Selector, field fields.Selector) generic.Matcher {
|
||||
return &generic.SelectionPredicate{Label: label, Field: field, GetAttrs: ServiceAttributes}
|
||||
}
|
||||
|
@@ -22,7 +22,7 @@ import (
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/latest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
|
||||
@@ -37,8 +37,8 @@ import (
|
||||
)
|
||||
|
||||
func NewTestEtcdRegistry(client tools.EtcdClient) (Registry, *etcdservice.REST) {
|
||||
storage := etcdstorage.NewEtcdStorage(client, latest.Codec, etcdtest.PathPrefix())
|
||||
rest := etcdservice.NewStorage(storage)
|
||||
storage := etcdstorage.NewEtcdStorage(client, testapi.Codec(), etcdtest.PathPrefix())
|
||||
rest := etcdservice.NewREST(storage)
|
||||
registry := NewRegistry(rest)
|
||||
return registry, rest
|
||||
}
|
||||
@@ -87,10 +87,10 @@ func TestEtcdListServices(t *testing.T) {
|
||||
Node: &etcd.Node{
|
||||
Nodes: []*etcd.Node{
|
||||
{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, makeTestService("foo")),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), makeTestService("foo")),
|
||||
},
|
||||
{
|
||||
Value: runtime.EncodeOrDie(latest.Codec, makeTestService("bar")),
|
||||
Value: runtime.EncodeOrDie(testapi.Codec(), makeTestService("bar")),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -124,7 +124,7 @@ func TestEtcdCreateService(t *testing.T) {
|
||||
}
|
||||
|
||||
var service api.Service
|
||||
err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &service)
|
||||
err = testapi.Codec().DecodeInto([]byte(resp.Node.Value), &service)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func TestEtcdCreateServiceAlreadyExisting(t *testing.T) {
|
||||
registry, rest := NewTestEtcdRegistry(fakeClient)
|
||||
key, _ := rest.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, makeTestService("foo")), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), makeTestService("foo")), 0)
|
||||
_, err := registry.CreateService(ctx, makeTestService("foo"))
|
||||
if !errors.IsAlreadyExists(err) {
|
||||
t.Errorf("expected already exists err, got %#v", err)
|
||||
@@ -161,8 +161,8 @@ func TestEtcdGetServiceDifferentNamespace(t *testing.T) {
|
||||
key1 = etcdtest.AddPrefix(key1)
|
||||
key2 = etcdtest.AddPrefix(key2)
|
||||
|
||||
fakeClient.Set(key1, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Namespace: "default", Name: "foo"}}), 0)
|
||||
fakeClient.Set(key2, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Namespace: "other", Name: "foo"}}), 0)
|
||||
fakeClient.Set(key1, runtime.EncodeOrDie(testapi.Codec(), &api.Service{ObjectMeta: api.ObjectMeta{Namespace: "default", Name: "foo"}}), 0)
|
||||
fakeClient.Set(key2, runtime.EncodeOrDie(testapi.Codec(), &api.Service{ObjectMeta: api.ObjectMeta{Namespace: "other", Name: "foo"}}), 0)
|
||||
|
||||
service1, err := registry.GetService(ctx1, "foo")
|
||||
if err != nil {
|
||||
@@ -194,7 +194,7 @@ func TestEtcdGetService(t *testing.T) {
|
||||
registry, rest := NewTestEtcdRegistry(fakeClient)
|
||||
key, _ := rest.KeyFunc(ctx, "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, makeTestService("foo")), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), makeTestService("foo")), 0)
|
||||
service, err := registry.GetService(ctx, "foo")
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
@@ -229,10 +229,10 @@ func TestEtcdDeleteService(t *testing.T) {
|
||||
registry, _ := NewTestEtcdRegistry(fakeClient)
|
||||
key, _ := etcdgeneric.NamespaceKeyFunc(ctx, "/services/specs", "foo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, makeTestService("foo")), 0)
|
||||
fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), makeTestService("foo")), 0)
|
||||
path, _ := etcdgeneric.NamespaceKeyFunc(ctx, "/services/endpoints", "foo")
|
||||
endpointsKey := etcdtest.AddPrefix(path)
|
||||
fakeClient.Set(endpointsKey, runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
||||
fakeClient.Set(endpointsKey, runtime.EncodeOrDie(testapi.Codec(), &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
|
||||
|
||||
err := registry.DeleteService(ctx, "foo")
|
||||
if err != nil {
|
||||
@@ -254,7 +254,7 @@ func TestEtcdUpdateService(t *testing.T) {
|
||||
registry, rest := NewTestEtcdRegistry(fakeClient)
|
||||
key, _ := rest.KeyFunc(ctx, "uniquefoo")
|
||||
key = etcdtest.AddPrefix(key)
|
||||
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, makeTestService("uniquefoo")), 0)
|
||||
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(testapi.Codec(), makeTestService("uniquefoo")), 0)
|
||||
testService := api.Service{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "uniquefoo",
|
||||
|
@@ -27,23 +27,21 @@ import (
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
)
|
||||
|
||||
// REST implements a RESTStorage for service accounts against etcd
|
||||
type REST struct {
|
||||
*etcdgeneric.Etcd
|
||||
}
|
||||
|
||||
const Prefix = "/serviceaccounts"
|
||||
|
||||
// NewStorage returns a RESTStorage object that will work against service accounts objects.
|
||||
func NewStorage(s storage.Interface) *REST {
|
||||
// NewREST returns a RESTStorage object that will work against service accounts.
|
||||
func NewREST(s storage.Interface) *REST {
|
||||
prefix := "/serviceaccounts"
|
||||
store := &etcdgeneric.Etcd{
|
||||
NewFunc: func() runtime.Object { return &api.ServiceAccount{} },
|
||||
NewListFunc: func() runtime.Object { return &api.ServiceAccountList{} },
|
||||
KeyRootFunc: func(ctx api.Context) string {
|
||||
return etcdgeneric.NamespaceKeyRootFunc(ctx, Prefix)
|
||||
return etcdgeneric.NamespaceKeyRootFunc(ctx, prefix)
|
||||
},
|
||||
KeyFunc: func(ctx api.Context, name string) (string, error) {
|
||||
return etcdgeneric.NamespaceKeyFunc(ctx, Prefix, name)
|
||||
return etcdgeneric.NamespaceKeyFunc(ctx, prefix, name)
|
||||
},
|
||||
ObjectNameFunc: func(obj runtime.Object) (string, error) {
|
||||
return obj.(*api.ServiceAccount).Name, nil
|
||||
@@ -53,12 +51,11 @@ func NewStorage(s storage.Interface) *REST {
|
||||
},
|
||||
EndpointName: "serviceaccounts",
|
||||
|
||||
CreateStrategy: serviceaccount.Strategy,
|
||||
UpdateStrategy: serviceaccount.Strategy,
|
||||
ReturnDeletedObject: true,
|
||||
|
||||
Storage: s,
|
||||
}
|
||||
|
||||
store.CreateStrategy = serviceaccount.Strategy
|
||||
store.UpdateStrategy = serviceaccount.Strategy
|
||||
store.ReturnDeletedObject = true
|
||||
|
||||
return &REST{store}
|
||||
}
|
||||
|
@@ -21,18 +21,14 @@ import (
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
||||
"k8s.io/kubernetes/pkg/api/testapi"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
etcdstorage "k8s.io/kubernetes/pkg/storage/etcd"
|
||||
"k8s.io/kubernetes/pkg/registry/registrytest"
|
||||
"k8s.io/kubernetes/pkg/tools"
|
||||
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
||||
)
|
||||
|
||||
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, storage.Interface) {
|
||||
fakeEtcdClient := tools.NewFakeEtcdClient(t)
|
||||
fakeEtcdClient.TestIndex = true
|
||||
etcdStorage := etcdstorage.NewEtcdStorage(fakeEtcdClient, testapi.Codec(), etcdtest.PathPrefix())
|
||||
return fakeEtcdClient, etcdStorage
|
||||
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
||||
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
||||
return NewREST(etcdStorage), fakeClient
|
||||
}
|
||||
|
||||
func validNewServiceAccount(name string) *api.ServiceAccount {
|
||||
@@ -46,9 +42,8 @@ func validNewServiceAccount(name string) *api.ServiceAccount {
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
serviceAccount := validNewServiceAccount("foo")
|
||||
serviceAccount.ObjectMeta = api.ObjectMeta{GenerateName: "foo-"}
|
||||
test.TestCreate(
|
||||
@@ -63,17 +58,16 @@ func TestCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
|
||||
storage := NewStorage(etcdStorage)
|
||||
test := resttest.New(t, storage, fakeEtcdClient.SetError)
|
||||
storage, fakeClient := newStorage(t)
|
||||
test := resttest.New(t, storage, fakeClient.SetError)
|
||||
key, err := storage.KeyFunc(test.TestContext(), "foo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key = etcdtest.AddPrefix(key)
|
||||
|
||||
fakeEtcdClient.ExpectNotFoundGet(key)
|
||||
fakeEtcdClient.ChangeIndex = 2
|
||||
fakeClient.ExpectNotFoundGet(key)
|
||||
fakeClient.ChangeIndex = 2
|
||||
serviceAccount := validNewServiceAccount("foo")
|
||||
existing := validNewServiceAccount("exists")
|
||||
existing.Namespace = test.TestNamespace()
|
||||
|
Reference in New Issue
Block a user