Skip to content

Commit

Permalink
FakeEtcdClient should return KeyNotFound on Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Mar 16, 2015
1 parent 1430150 commit 4ca90e0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
16 changes: 11 additions & 5 deletions pkg/registry/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func TestEtcdDeleteController(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
registry := NewTestEtcdRegistry(fakeClient)
key, _ := makeControllerKey(ctx, "foo")
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
err := registry.DeleteController(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
Expand Down Expand Up @@ -532,6 +533,11 @@ func TestEtcdDeleteService(t *testing.T) {
ctx := api.NewDefaultContext()
fakeClient := tools.NewFakeEtcdClient(t)
registry := NewTestEtcdRegistry(fakeClient)
key, _ := makeServiceKey(ctx, "foo")
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
endpointsKey, _ := makeServiceEndpointsKey(ctx, "foo")
fakeClient.Set(endpointsKey, runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ObjectMeta: api.ObjectMeta{Name: "foo"}, Protocol: "TCP"}), 0)

err := registry.DeleteService(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
Expand All @@ -540,13 +546,11 @@ func TestEtcdDeleteService(t *testing.T) {
if len(fakeClient.DeletedKeys) != 2 {
t.Errorf("Expected 2 delete, found %#v", fakeClient.DeletedKeys)
}
key, _ := makeServiceKey(ctx, "foo")
if fakeClient.DeletedKeys[0] != key {
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key)
}
key, _ = makeServiceEndpointsKey(ctx, "foo")
if fakeClient.DeletedKeys[1] != key {
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[1], key)
if fakeClient.DeletedKeys[1] != endpointsKey {
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[1], endpointsKey)
}
}

Expand Down Expand Up @@ -906,6 +910,9 @@ func TestEtcdDeleteMinion(t *testing.T) {
ctx := api.NewContext()
fakeClient := tools.NewFakeEtcdClient(t)
registry := NewTestEtcdRegistry(fakeClient)
key := "/registry/minions/foo"
fakeClient.Set("/registry/minions/foo", runtime.EncodeOrDie(latest.Codec, &api.Node{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)

err := registry.DeleteMinion(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
Expand All @@ -914,7 +921,6 @@ func TestEtcdDeleteMinion(t *testing.T) {
if len(fakeClient.DeletedKeys) != 1 {
t.Errorf("Expected 1 delete, found %#v", fakeClient.DeletedKeys)
}
key := "/registry/minions/foo"
if fakeClient.DeletedKeys[0] != key {
t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/registry/generic/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func TestEtcdDelete(t *testing.T) {
"notExisting": {
existing: emptyNode,
expect: emptyNode,
errOK: func(err error) bool { return err == nil },
errOK: func(err error) bool { return errors.IsNotFound(err) },
},
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/tools/fake_etcd_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,13 @@ func (f *FakeEtcdClient) Delete(key string, recursive bool) (*etcd.Response, err

f.Mutex.Lock()
defer f.Mutex.Unlock()
existing := f.Data[key]
existing, ok := f.Data[key]
if !ok {
return &etcd.Response{}, &etcd.EtcdError{
ErrorCode: EtcdErrorCodeNotFound,
Index: f.ChangeIndex,
}
}
index := f.generateIndex()
f.Data[key] = EtcdResponseWithError{
R: &etcd.Response{},
Expand Down

0 comments on commit 4ca90e0

Please sign in to comment.