Skip to content

Commit

Permalink
fix variable shadowing bug in etcd_tools.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedanese committed Mar 6, 2015
1 parent 54b2b47 commit 438052c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pkg/tools/etcd_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,14 @@ func (h *EtcdHelper) SetObj(key string, obj, out runtime.Object, ttl uint64) err

create := true
if h.ResourceVersioner != nil {
version, err := h.ResourceVersioner.ResourceVersion(obj)
if err == nil && version != 0 {
if version, err := h.ResourceVersioner.ResourceVersion(obj); err == nil && version != 0 {
create = false
response, err = h.Client.CompareAndSwap(key, string(data), ttl, "", version)
if err != nil {
return err
}
}
}
if err != nil {
return err
}
if create {
// Create will fail if a key already exists.
response, err = h.Client.Create(key, string(data), ttl)
Expand Down
11 changes: 11 additions & 0 deletions pkg/tools/etcd_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ func TestSetObj(t *testing.T) {
}
}

func TestSetObjFailCAS(t *testing.T) {
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"}}
fakeClient := NewFakeEtcdClient(t)
fakeClient.CasErr = fakeClient.NewError(123)
helper := EtcdHelper{fakeClient, testapi.Codec(), versioner}
err := helper.SetObj("/some/key", obj, nil, 5)
if err == nil {
t.Errorf("Expecting error.")
}
}

func TestSetObjWithVersion(t *testing.T) {
obj := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"}}
fakeClient := NewFakeEtcdClient(t)
Expand Down
5 changes: 5 additions & 0 deletions pkg/tools/fake_etcd_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type FakeEtcdClient struct {
expectNotFoundGetSet map[string]struct{}
sync.Mutex
Err error
CasErr error
t TestLogger
Ix int
TestIndex bool
Expand Down Expand Up @@ -225,6 +226,10 @@ func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue
f.t.Logf("c&s: returning err %v", f.Err)
return nil, f.Err
}
if f.CasErr != nil {
f.t.Logf("c&s: returning err %v", f.CasErr)
return nil, f.CasErr
}

if !f.TestIndex {
f.t.Errorf("Enable TestIndex for test involving CompareAndSwap")
Expand Down

0 comments on commit 438052c

Please sign in to comment.