Implement FakeEtcdClient.ExpectNotFoundGet

This commit is contained in:
Kouhei Ueno 2014-07-31 20:33:18 +09:00
parent a3771c9042
commit 648b80e5d7

View File

@ -39,6 +39,7 @@ type FakeEtcdClient struct {
Data map[string]EtcdResponseWithError Data map[string]EtcdResponseWithError
DeletedKeys []string DeletedKeys []string
expectNotFoundGetSet map[string]struct{}
Err error Err error
t TestLogger t TestLogger
Ix int Ix int
@ -56,6 +57,7 @@ type FakeEtcdClient struct {
func MakeFakeEtcdClient(t TestLogger) *FakeEtcdClient { func MakeFakeEtcdClient(t TestLogger) *FakeEtcdClient {
ret := &FakeEtcdClient{ ret := &FakeEtcdClient{
t: t, t: t,
expectNotFoundGetSet: map[string]struct{}{},
Data: map[string]EtcdResponseWithError{}, Data: map[string]EtcdResponseWithError{},
} }
// There are three publicly accessible channels in FakeEtcdClient: // There are three publicly accessible channels in FakeEtcdClient:
@ -73,6 +75,10 @@ func MakeFakeEtcdClient(t TestLogger) *FakeEtcdClient {
return ret return ret
} }
func (f *FakeEtcdClient) ExpectNotFoundGet(key string) {
f.expectNotFoundGetSet[key] = struct{}{}
}
func (f *FakeEtcdClient) generateIndex() uint64 { func (f *FakeEtcdClient) generateIndex() uint64 {
if !f.TestIndex { if !f.TestIndex {
return 0 return 0
@ -90,7 +96,9 @@ func (f *FakeEtcdClient) AddChild(key, data string, ttl uint64) (*etcd.Response,
func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error) { func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, error) {
result := f.Data[key] result := f.Data[key]
if result.R == nil { if result.R == nil {
if _, ok := f.expectNotFoundGetSet[key]; !ok {
f.t.Errorf("Unexpected get for %s", key) f.t.Errorf("Unexpected get for %s", key)
}
return &etcd.Response{}, EtcdErrorNotFound return &etcd.Response{}, EtcdErrorNotFound
} }
f.t.Logf("returning %v: %v %#v", key, result.R, result.E) f.t.Logf("returning %v: %v %#v", key, result.R, result.E)