Unification refactoring to pkg/registry

This commit is contained in:
Wojciech Tyczynski
2015-08-20 11:17:04 +02:00
parent e5735fdc7c
commit 5931cc74cc
33 changed files with 501 additions and 649 deletions

View File

@@ -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}
}

View File

@@ -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)
})
}