func TestEtcdDeleteService(t *testing.T) { ctx := api.NewDefaultContext() fakeClient := tools.NewFakeEtcdClient(t) registry := NewTestEtcdRegistryWithPods(fakeClient) key, _ := etcdgeneric.NamespaceKeyFunc(ctx, "/services/specs", "foo") key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "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) err := registry.DeleteService(ctx, "foo") if err != nil { t.Errorf("unexpected error: %v", err) } if len(fakeClient.DeletedKeys) != 2 { t.Errorf("Expected 2 delete, found %#v", fakeClient.DeletedKeys) } if fakeClient.DeletedKeys[0] != key { t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key) } if fakeClient.DeletedKeys[1] != endpointsKey { t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[1], endpointsKey) } }
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 = etcdtest.AddPrefix(key) createFn := func() runtime.Object { rc := validController rc.ResourceVersion = "1" fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, &rc), ModifiedIndex: 1, }, }, } return &rc } gracefulSetFn := func() bool { // If the controller is still around after trying to delete either the delete // failed, or we're deleting it gracefully. if fakeClient.Data[key].R.Node != nil { return true } return false } test.TestDelete(createFn, gracefulSetFn) }
func TestUpdateWithConflictingNamespace(t *testing.T) { fakeEtcdClient, helper := newHelper(t) storage := NewStorage(helper, nil).Pod ctx := api.NewDefaultContext() key, _ := storage.Etcd.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "default"}, Spec: api.PodSpec{NodeName: "machine"}, }), ModifiedIndex: 1, }, }, } pod := validChangedPod() pod.Namespace = "not-default" obj, created, err := storage.Update(api.NewDefaultContext(), pod) if obj != nil || created { t.Error("Expected a nil channel, but we got a value or created") } if err == nil { t.Errorf("Expected an error, but we didn't get one") } else if strings.Index(err.Error(), "the namespace of the provided object does not match the namespace sent on the request") == -1 { t.Errorf("Expected 'Pod.Namespace does not match the provided context' error, got '%v'", err.Error()) } }
func TestGet(t *testing.T) { expect := validNewPod() expect.Status.Phase = api.PodRunning expect.Spec.NodeName = "machine" fakeEtcdClient, helper := newHelper(t) key := etcdtest.AddPrefix("/pods/test/foo") fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, expect), }, }, } storage := NewStorage(helper, nil).Pod obj, err := storage.Get(api.WithNamespace(api.NewContext(), "test"), "foo") pod := obj.(*api.Pod) if err != nil { t.Fatalf("unexpected error: %v", err) } if e, a := expect, pod; !api.Semantic.DeepEqual(e, a) { t.Errorf("Unexpected pod: %s", util.ObjectDiff(e, a)) } }
func TestDelete(t *testing.T) { fakeEtcdClient, helper := newHelper(t) storage := NewStorage(helper, nil).Pod ctx := api.NewDefaultContext() key, _ := storage.Etcd.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) test := resttest.New(t, storage, fakeEtcdClient.SetError) createFn := func() runtime.Object { pod := validChangedPod() fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, pod), ModifiedIndex: 1, }, }, } return pod } gracefulSetFn := func() bool { if fakeEtcdClient.Data[key].R.Node == nil { return false } return fakeEtcdClient.Data[key].R.Node.TTL == 30 } test.TestDelete(createFn, gracefulSetFn) }
func TestEtcdUpdateNotFound(t *testing.T) { registry, _, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true key, _ := registry.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: tools.EtcdErrorNotFound, } podIn := api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "foo", ResourceVersion: "1", Labels: map[string]string{ "foo": "bar", }, }, } _, _, err := registry.Update(ctx, &podIn) if err == nil { t.Errorf("unexpected non-error") } }
func TestEtcdListControllers(t *testing.T) { fakeClient := tools.NewFakeEtcdClient(t) registry := NewTestEtcdRegistry(fakeClient) ctx := api.NewDefaultContext() key := makeControllerListKey(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "bar"}}), }, }, }, }, E: nil, } controllers, err := registry.ListControllers(ctx) if err != nil { t.Errorf("unexpected error: %v", err) } if len(controllers.Items) != 2 || controllers.Items[0].Name != "foo" || controllers.Items[1].Name != "bar" { t.Errorf("Unexpected controller list: %#v", controllers) } }
func TestGuaranteedUpdateKeyNotFound(t *testing.T) { fakeClient := NewFakeEtcdClient(t) fakeClient.TestIndex = true helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix()) key := etcdtest.AddPrefix("/some/key") // Create a new node. fakeClient.ExpectNotFoundGet(key) obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1} f := SimpleUpdate(func(in runtime.Object) (runtime.Object, error) { return obj, nil }) ignoreNotFound := false err := helper.GuaranteedUpdate("/some/key", &TestResource{}, ignoreNotFound, f) if err == nil { t.Errorf("Expected error for key not found.") } ignoreNotFound = true err = helper.GuaranteedUpdate("/some/key", &TestResource{}, ignoreNotFound, f) if err != nil { t.Errorf("Unexpected error %v.", err) } }
func TestEtcdCreateService(t *testing.T) { ctx := api.NewDefaultContext() fakeClient := tools.NewFakeEtcdClient(t) registry := NewTestEtcdRegistry(fakeClient) _, err := registry.CreateService(ctx, &api.Service{ ObjectMeta: api.ObjectMeta{Name: "foo"}, }) if err != nil { t.Errorf("unexpected error: %v", err) } key, _ := makeServiceKey(ctx, "foo") key = etcdtest.AddPrefix(key) resp, err := fakeClient.Get(key, false, false) if err != nil { t.Errorf("unexpected error: %v", err) } var service api.Service err = latest.Codec.DecodeInto([]byte(resp.Node.Value), &service) if err != nil { t.Errorf("unexpected error: %v", err) } if service.Name != "foo" { t.Errorf("Unexpected service: %#v %s", service, resp.Node.Value) } }
func TestEtcdCreateController(t *testing.T) { ctx := api.NewDefaultContext() fakeClient := tools.NewFakeEtcdClient(t) registry := NewTestEtcdRegistry(fakeClient) key, _ := makeControllerKey(ctx, "foo") key = etcdtest.AddPrefix(key) _, err := registry.CreateController(ctx, &api.ReplicationController{ ObjectMeta: api.ObjectMeta{ Name: "foo", }, }) if err != nil { t.Errorf("unexpected error: %v", err) } 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) if err != nil { t.Errorf("unexpected error: %v", err) } if ctrl.Name != "foo" { t.Errorf("Unexpected pod: %#v %s", ctrl, resp.Node.Value) } }
func TestEtcdList(t *testing.T) { registry, _, fakeClient, _ := newStorage(t) ctx := api.NewDefaultContext() key := registry.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{Name: "foo"}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{Name: "bar"}, }), }, }, }, }, E: nil, } obj, err := registry.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } resourcequotas := obj.(*api.ResourceQuotaList) if len(resourcequotas.Items) != 2 || resourcequotas.Items[0].Name != "foo" || resourcequotas.Items[1].Name != "bar" { t.Errorf("Unexpected resourcequota list: %#v", resourcequotas) } }
func TestListEmptyResourceQuotaList(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 storage, _ := NewStorage(helper) ctx := api.NewContext() key := storage.Etcd.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: fakeEtcdClient.NewError(tools.EtcdErrorCodeNotFound), } resourcequotas, err := storage.List(api.NewContext(), labels.Everything(), fields.Everything()) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(resourcequotas.(*api.ResourceQuotaList).Items) != 0 { t.Errorf("Unexpected non-zero resourcequota list: %#v", resourcequotas) } if resourcequotas.(*api.ResourceQuotaList).ResourceVersion != "1" { t.Errorf("Unexpected resource version: %#v", resourcequotas) } }
func TestDelete(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeEtcdClient := newStorage(t) test := resttest.New(t, storage, fakeEtcdClient.SetError) endpoints := validChangedEndpoints() key, _ := storage.KeyFunc(ctx, endpoints.Name) key = etcdtest.AddPrefix(key) createFn := func() runtime.Object { fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, endpoints), ModifiedIndex: 1, }, }, } return endpoints } gracefulSetFn := func() bool { if fakeEtcdClient.Data[key].R.Node == nil { return false } return fakeEtcdClient.Data[key].R.Node.TTL == 30 } test.TestDeleteNoGraceful(createFn, gracefulSetFn) }
func TestEtcdUpdateEndpoints(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) endpoints := validChangedEndpoints() key, _ := storage.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, validNewEndpoints()), 0) _, _, err := storage.Update(ctx, endpoints) if err != nil { t.Errorf("unexpected error: %v", err) } response, err := fakeClient.Get(key, false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var endpointsOut api.Endpoints err = latest.Codec.DecodeInto([]byte(response.Node.Value), &endpointsOut) if err != nil { t.Errorf("unexpected error: %v", err) } endpoints.ObjectMeta.ResourceVersion = endpointsOut.ObjectMeta.ResourceVersion if !api.Semantic.DeepEqual(endpoints, &endpointsOut) { t.Errorf("Unexpected endpoints: %#v, expected %#v", &endpointsOut, endpoints) } }
func TestEtcdGetEndpoints(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) endpoints := validNewEndpoints() name := endpoints.Name key, _ := storage.KeyFunc(ctx, name) key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, endpoints), 0) response, err := fakeClient.Get(key, false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var endpointsOut api.Endpoints err = latest.Codec.DecodeInto([]byte(response.Node.Value), &endpointsOut) if err != nil { t.Errorf("unexpected error: %v", err) } obj, err := storage.Get(ctx, name) if err != nil { t.Errorf("unexpected error: %v", err) } got := obj.(*api.Endpoints) endpoints.ObjectMeta.ResourceVersion = got.ObjectMeta.ResourceVersion if e, a := endpoints, got; !api.Semantic.DeepEqual(*e, *a) { t.Errorf("Unexpected endpoints: %#v, expected %#v", e, a) } }
func TestSetObjWithoutResourceVersioner(t *testing.T) { obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}} fakeClient := NewFakeEtcdClient(t) helper := NewEtcdHelper(fakeClient, testapi.Codec(), etcdtest.PathPrefix()) helper.Versioner = nil returnedObj := &api.Pod{} err := helper.SetObj("/some/key", obj, returnedObj, 3) key := etcdtest.AddPrefix("/some/key") if err != nil { t.Errorf("Unexpected error %#v", err) } data, err := testapi.Codec().Encode(obj) if err != nil { t.Errorf("Unexpected error %#v", err) } expect := string(data) got := fakeClient.Data[key].R.Node.Value if expect != got { t.Errorf("Wanted %v, got %v", expect, got) } if e, a := uint64(3), fakeClient.LastSetTTL; e != a { t.Errorf("Wanted %v, got %v", e, a) } if obj.ResourceVersion != returnedObj.ResourceVersion || obj.Name != returnedObj.Name { t.Errorf("If set was successful but returned object did not have correct resource version") } }
func TestGuaranteedUpdateNoChange(t *testing.T) { fakeClient := NewFakeEtcdClient(t) fakeClient.TestIndex = true helper := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix()) key := etcdtest.AddPrefix("/some/key") // Create a new node. fakeClient.ExpectNotFoundGet(key) obj := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1} err := helper.GuaranteedUpdate("/some/key", &TestResource{}, true, SimpleUpdate(func(in runtime.Object) (runtime.Object, error) { return obj, nil })) if err != nil { t.Errorf("Unexpected error %#v", err) } // Update an existing node with the same data callbackCalled := false objUpdate := &TestResource{ObjectMeta: api.ObjectMeta{Name: "foo"}, Value: 1} err = helper.GuaranteedUpdate("/some/key", &TestResource{}, true, SimpleUpdate(func(in runtime.Object) (runtime.Object, error) { fakeClient.Err = errors.New("should not be called") callbackCalled = true return objUpdate, nil })) if err != nil { t.Fatalf("Unexpected error %#v", err) } if !callbackCalled { t.Errorf("tryUpdate callback should have been called.") } }
func TestDeletePod(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 storage := NewStorage(helper, nil).Pod ctx := api.NewDefaultContext() key, _ := storage.Etcd.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "foo", Namespace: api.NamespaceDefault, }, Spec: api.PodSpec{NodeName: "machine"}, }), ModifiedIndex: 1, CreatedIndex: 1, }, }, } _, err := storage.Delete(api.NewDefaultContext(), "foo", nil) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func TestUpdate(t *testing.T) { fakeEtcdClient, helper := newHelper(t) storage := NewStorage(helper) test := resttest.New(t, storage, fakeEtcdClient.SetError) key, err := storage.KeyFunc(test.TestContext(), "foo") if err != nil { t.Fatal(err) } key = etcdtest.AddPrefix(key) fakeEtcdClient.ExpectNotFoundGet(key) fakeEtcdClient.ChangeIndex = 2 serviceAccount := validNewServiceAccount("foo") existing := validNewServiceAccount("exists") existing.Namespace = test.TestNamespace() obj, err := storage.Create(test.TestContext(), existing) if err != nil { t.Fatalf("unable to create object: %v", err) } older := obj.(*api.ServiceAccount) older.ResourceVersion = "1" test.TestUpdate( serviceAccount, existing, older, ) }
func TestDelete(t *testing.T) { ctx := api.NewContext() storage, _, fakeEtcdClient, _ := newStorage(t) test := resttest.New(t, storage, fakeEtcdClient.SetError).ClusterScope() pv := validChangedPersistentVolume() key, _ := storage.KeyFunc(ctx, pv.Name) key = etcdtest.AddPrefix(key) createFn := func() runtime.Object { fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, pv), ModifiedIndex: 1, }, }, } return pv } gracefulSetFn := func() bool { if fakeEtcdClient.Data[key].R.Node == nil { return false } return fakeEtcdClient.Data[key].R.Node.TTL == 30 } test.TestDeleteNoGraceful(createFn, gracefulSetFn) }
func TestEtcdListControllers(t *testing.T) { storage, fakeClient := newStorage(t) ctx := api.NewDefaultContext() key := makeControllerListKey(ctx) key = etcdtest.AddPrefix(key) controller := validController controller.Name = "bar" fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &validController), }, { Value: runtime.EncodeOrDie(latest.Codec, &controller), }, }, }, }, E: nil, } objList, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } controllers, _ := objList.(*api.ReplicationControllerList) if len(controllers.Items) != 2 || controllers.Items[0].Name != validController.Name || controllers.Items[1].Name != controller.Name { t.Errorf("Unexpected controller list: %#v", controllers) } }
func TestEtcdGetPersistentVolumes(t *testing.T) { ctx := api.NewContext() storage, _, fakeClient, _ := newStorage(t) persistentVolume := validNewPersistentVolume("foo") name := persistentVolume.Name key, _ := storage.KeyFunc(ctx, name) key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, persistentVolume), 0) response, err := fakeClient.Get(key, false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var persistentVolumeOut api.PersistentVolume err = latest.Codec.DecodeInto([]byte(response.Node.Value), &persistentVolumeOut) if err != nil { t.Errorf("unexpected error: %v", err) } obj, err := storage.Get(ctx, name) if err != nil { t.Errorf("unexpected error: %v", err) } got := obj.(*api.PersistentVolume) persistentVolume.ObjectMeta.ResourceVersion = got.ObjectMeta.ResourceVersion if e, a := persistentVolume, got; !api.Semantic.DeepEqual(*e, *a) { t.Errorf("Unexpected persistentVolume: %#v, expected %#v", e, a) } }
func TestEtcdUpdatePersistentVolumes(t *testing.T) { ctx := api.NewContext() 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) _, _, err := storage.Update(ctx, persistentVolume) if err != nil { t.Errorf("unexpected error: %v", err) } response, err := fakeClient.Get(key, false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var persistentVolumeOut api.PersistentVolume err = latest.Codec.DecodeInto([]byte(response.Node.Value), &persistentVolumeOut) if err != nil { t.Errorf("unexpected error: %v", err) } persistentVolume.ObjectMeta.ResourceVersion = persistentVolumeOut.ObjectMeta.ResourceVersion if !api.Semantic.DeepEqual(persistentVolume, &persistentVolumeOut) { t.Errorf("Unexpected persistentVolume: %#v, expected %#v", &persistentVolumeOut, persistentVolume) } }
func TestDeleteNamespaceWithCompleteFinalizers(t *testing.T) { now := util.Now() fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 key := etcdtest.AddPrefix("/namespaces/foo") fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{ Name: "foo", DeletionTimestamp: &now, }, Spec: api.NamespaceSpec{ Finalizers: []api.FinalizerName{}, }, Status: api.NamespaceStatus{Phase: api.NamespaceActive}, }), ModifiedIndex: 1, CreatedIndex: 1, }, }, } storage, _, _ := NewStorage(helper) _, err := storage.Delete(api.NewContext(), "foo", nil) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func TestEtcdListPersistentVolumes(t *testing.T) { ctx := api.NewContext() storage, _, fakeClient, _ := newStorage(t) key := storage.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, validNewPersistentVolume("foo")), }, { Value: runtime.EncodeOrDie(latest.Codec, validNewPersistentVolume("bar")), }, }, }, }, E: nil, } pvObj, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } pvs := pvObj.(*api.PersistentVolumeList) if len(pvs.Items) != 2 || pvs.Items[0].Name != "foo" || pvs.Items[1].Name != "bar" { t.Errorf("Unexpected persistentVolume list: %#v", pvs) } }
func TestDeleteNamespace(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 storage, _, _ := NewStorage(helper) ctx := api.NewContext() key, err := storage.Etcd.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{ Name: "foo", }, Status: api.NamespaceStatus{Phase: api.NamespaceActive}, }), ModifiedIndex: 1, CreatedIndex: 1, }, }, } _, err = storage.Delete(api.NewContext(), "foo", nil) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func TestGet(t *testing.T) { expect := validNewNamespace() expect.Status.Phase = api.NamespaceActive storage, fakeEtcdClient, _ := newStorage(t) ctx := api.NewContext() key, err := storage.Etcd.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) if err != nil { t.Fatalf("unexpected key error: %v", err) } fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, expect), }, }, } obj, err := storage.Get(api.NewContext(), "foo") namespace := obj.(*api.Namespace) if err != nil { t.Fatalf("unexpected error: %v", err) } expect.Status.Phase = api.NamespaceActive if e, a := expect, namespace; !api.Semantic.DeepEqual(e, a) { t.Errorf("Unexpected namespace: %s", util.ObjectDiff(e, a)) } }
func TestWatchPurposefulShutdown(t *testing.T) { fakeClient := NewFakeEtcdClient(t) h := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix()) key := "/some/key" prefixedKey := etcdtest.AddPrefix(key) fakeClient.expectNotFoundGetSet[prefixedKey] = struct{}{} // Test purposeful shutdown watching, err := h.Watch(key, 0, Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) } fakeClient.WaitForWatchCompletion() watching.Stop() // Did everything shut down? if _, open := <-fakeClient.WatchResponse; open { t.Errorf("A stop did not cause a graceful shutdown") } if _, open := <-watching.ResultChan(); open { t.Errorf("An injected error did not cause a graceful shutdown") } }
func TestWatchFromNotFound(t *testing.T) { fakeClient := NewFakeEtcdClient(t) key := "/some/key" prefixedKey := etcdtest.AddPrefix(key) fakeClient.Data[prefixedKey] = EtcdResponseWithError{ R: &etcd.Response{ Node: nil, }, E: &etcd.EtcdError{ Index: 2, ErrorCode: 100, }, } h := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix()) watching, err := h.Watch(key, 0, Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) } fakeClient.WaitForWatchCompletion() if fakeClient.WatchIndex != 3 { t.Errorf("Expected client to wait for %d, got %#v", 3, fakeClient) } watching.Stop() }
func TestWatch(t *testing.T) { codec := latest.Codec fakeClient := NewFakeEtcdClient(t) key := "/some/key" prefixedKey := etcdtest.AddPrefix(key) fakeClient.expectNotFoundGetSet[prefixedKey] = struct{}{} h := NewEtcdHelper(fakeClient, codec, etcdtest.PathPrefix()) watching, err := h.Watch(key, 0, Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) } fakeClient.WaitForWatchCompletion() // when server returns not found, the watch index starts at the next value (1) if fakeClient.WatchIndex != 1 { t.Errorf("Expected client to be at index %d, got %#v", 1, fakeClient) } // Test normal case pod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}} podBytes, _ := codec.Encode(pod) fakeClient.WatchResponse <- &etcd.Response{ Action: "set", Node: &etcd.Node{ Value: string(podBytes), }, } event := <-watching.ResultChan() if e, a := watch.Added, event.Type; e != a { t.Errorf("Expected %v, got %v", e, a) } if e, a := pod, event.Object; !api.Semantic.DeepDerivative(e, a) { t.Errorf("Expected %v, got %v", e, a) } // Test error case fakeClient.WatchInjectError <- fmt.Errorf("Injected error") if errEvent, ok := <-watching.ResultChan(); !ok { t.Errorf("no error result?") } else { if e, a := watch.Error, errEvent.Type; e != a { t.Errorf("Expected %v, got %v", e, a) } if e, a := "Injected error", errEvent.Object.(*api.Status).Message; e != a { t.Errorf("Expected %v, got %v", e, a) } } // Did everything shut down? if _, open := <-fakeClient.WatchResponse; open { t.Errorf("An injected error did not cause a graceful shutdown") } if _, open := <-watching.ResultChan(); open { t.Errorf("An injected error did not cause a graceful shutdown") } }