Private EtcdHelper

This commit is contained in:
Wojciech Tyczynski
2015-07-24 13:09:49 +02:00
parent 5bd82ffe6d
commit 9d943df397
56 changed files with 471 additions and 482 deletions

View File

@@ -40,17 +40,17 @@ import (
"github.com/coreos/go-etcd/etcd"
)
func newHelper(t *testing.T) (*tools.FakeEtcdClient, tools.EtcdHelper) {
func newEtcdStorage(t *testing.T) (*tools.FakeEtcdClient, tools.StorageInterface) {
fakeEtcdClient := tools.NewFakeEtcdClient(t)
fakeEtcdClient.TestIndex = true
helper := tools.NewEtcdHelper(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
return fakeEtcdClient, helper
etcdStorage := tools.NewEtcdStorage(fakeEtcdClient, latest.Codec, etcdtest.PathPrefix())
return fakeEtcdClient, etcdStorage
}
func newStorage(t *testing.T) (*REST, *BindingREST, *StatusREST, *tools.FakeEtcdClient, tools.EtcdHelper) {
fakeEtcdClient, h := newHelper(t)
storage := NewStorage(h, nil)
return storage.Pod, storage.Binding, storage.Status, fakeEtcdClient, h
func newStorage(t *testing.T) (*REST, *BindingREST, *StatusREST, *tools.FakeEtcdClient, tools.StorageInterface) {
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil)
return storage.Pod, storage.Binding, storage.Status, fakeEtcdClient, etcdStorage
}
func validNewPod() *api.Pod {
@@ -91,8 +91,8 @@ func TestStorage(t *testing.T) {
}
func TestCreate(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
storage := NewStorage(helper, nil).Pod
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
test := resttest.New(t, storage, fakeEtcdClient.SetError)
pod := validNewPod()
pod.ObjectMeta = api.ObjectMeta{}
@@ -109,8 +109,8 @@ func TestCreate(t *testing.T) {
}
func TestDelete(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
storage := NewStorage(helper, nil).Pod
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
@@ -147,9 +147,9 @@ func expectPod(t *testing.T, out runtime.Object) (*api.Pod, bool) {
}
func TestCreateRegistryError(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
fakeEtcdClient.Err = fmt.Errorf("test error")
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
pod := validNewPod()
_, err := storage.Create(api.NewDefaultContext(), pod)
@@ -159,8 +159,8 @@ func TestCreateRegistryError(t *testing.T) {
}
func TestCreateSetsFields(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
storage := NewStorage(helper, nil).Pod
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
pod := validNewPod()
_, err := storage.Create(api.NewDefaultContext(), pod)
if err != fakeEtcdClient.Err {
@@ -169,7 +169,7 @@ func TestCreateSetsFields(t *testing.T) {
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
actual := &api.Pod{}
if err := helper.ExtractObj(key, actual, false); err != nil {
if err := etcdStorage.ExtractObj(key, actual, false); err != nil {
t.Fatalf("unexpected extraction error: %v", err)
}
if actual.Name != pod.Name {
@@ -181,9 +181,9 @@ func TestCreateSetsFields(t *testing.T) {
}
func TestListError(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
fakeEtcdClient.Err = fmt.Errorf("test error")
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
pods, err := storage.List(api.NewDefaultContext(), labels.Everything(), fields.Everything())
if err != fakeEtcdClient.Err {
t.Fatalf("Expected %#v, Got %#v", fakeEtcdClient.Err, err)
@@ -194,10 +194,10 @@ func TestListError(t *testing.T) {
}
func TestListEmptyPodList(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
fakeEtcdClient.ChangeIndex = 1
ctx := api.NewContext()
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
key := storage.Etcd.KeyRootFunc(ctx)
key = etcdtest.AddPrefix(key)
@@ -219,9 +219,9 @@ func TestListEmptyPodList(t *testing.T) {
}
func TestListPodList(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
ctx := api.NewDefaultContext()
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
key := storage.Etcd.KeyRootFunc(ctx)
key = etcdtest.AddPrefix(key)
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
@@ -264,9 +264,9 @@ func TestListPodList(t *testing.T) {
}
func TestListPodListSelection(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
ctx := api.NewDefaultContext()
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
rootKey := etcdtest.AddPrefix("pods/default")
key := etcdtest.AddPrefix("pods/default/zot")
fakeEtcdClient.Data[rootKey] = tools.EtcdResponseWithError{
@@ -368,7 +368,8 @@ func TestListPodListSelection(t *testing.T) {
}
func TestPodDecode(t *testing.T) {
storage := NewStorage(tools.EtcdHelper{}, nil).Pod
_, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
expected := validNewPod()
body, err := latest.Codec.Encode(expected)
if err != nil {
@@ -390,7 +391,7 @@ func TestGet(t *testing.T) {
expect.Status.Phase = api.PodRunning
expect.Spec.NodeName = "machine"
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
key := etcdtest.AddPrefix("/pods/test/foo")
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
@@ -399,7 +400,7 @@ func TestGet(t *testing.T) {
},
},
}
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
obj, err := storage.Get(api.WithNamespace(api.NewContext(), "test"), "foo")
pod := obj.(*api.Pod)
@@ -414,9 +415,9 @@ func TestGet(t *testing.T) {
// TODO: remove, this is covered by RESTTest.TestCreate
func TestPodStorageValidatesCreate(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
fakeEtcdClient.Err = fmt.Errorf("test error")
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
pod := validNewPod()
pod.Labels = map[string]string{
@@ -433,8 +434,8 @@ func TestPodStorageValidatesCreate(t *testing.T) {
// TODO: remove, this is covered by RESTTest.TestCreate
func TestCreatePod(t *testing.T) {
_, helper := newHelper(t)
storage := NewStorage(helper, nil).Pod
_, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
@@ -447,7 +448,7 @@ func TestCreatePod(t *testing.T) {
t.Fatalf("unexpected object: %#v", obj)
}
actual := &api.Pod{}
if err := helper.ExtractObj(key, actual, false); err != nil {
if err := etcdStorage.ExtractObj(key, actual, false); err != nil {
t.Fatalf("unexpected extraction error: %v", err)
}
if !api.HasObjectMetaSystemFieldValues(&actual.ObjectMeta) {
@@ -457,8 +458,8 @@ func TestCreatePod(t *testing.T) {
// TODO: remove, this is covered by RESTTest.TestCreate
func TestCreateWithConflictingNamespace(t *testing.T) {
_, helper := newHelper(t)
storage := NewStorage(helper, nil).Pod
_, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
pod := validNewPod()
pod.Namespace = "not-default"
@@ -475,8 +476,8 @@ func TestCreateWithConflictingNamespace(t *testing.T) {
}
func TestUpdateWithConflictingNamespace(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
storage := NewStorage(helper, nil).Pod
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
@@ -600,8 +601,8 @@ func TestResourceLocation(t *testing.T) {
ctx := api.NewDefaultContext()
for _, tc := range testCases {
fakeEtcdClient, helper := newHelper(t)
storage := NewStorage(helper, nil).Pod
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
storage := NewStorage(etcdStorage, nil).Pod
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
@@ -631,9 +632,9 @@ func TestResourceLocation(t *testing.T) {
}
func TestDeletePod(t *testing.T) {
fakeEtcdClient, helper := newHelper(t)
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
fakeEtcdClient.ChangeIndex = 1
storage := NewStorage(helper, nil).Pod
storage := NewStorage(etcdStorage, nil).Pod
ctx := api.NewDefaultContext()
key, _ := storage.Etcd.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
@@ -1158,7 +1159,7 @@ func TestEtcdUpdateScheduled(t *testing.T) {
}
func TestEtcdUpdateStatus(t *testing.T) {
registry, _, status, fakeClient, helper := newStorage(t)
registry, _, status, fakeClient, etcdStorage := newStorage(t)
ctx := api.NewDefaultContext()
fakeClient.TestIndex = true
@@ -1221,7 +1222,7 @@ func TestEtcdUpdateStatus(t *testing.T) {
}
var podOut api.Pod
key, _ = registry.KeyFunc(ctx, "foo")
if err := helper.ExtractObj(key, &podOut, false); err != nil {
if err := etcdStorage.ExtractObj(key, &podOut, false); err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !api.Semantic.DeepEqual(expected, podOut) {