Implement FakeEtcdClient.ExpectNotFoundGet

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

View File

@ -37,13 +37,14 @@ type TestLogger interface {
type FakeEtcdClient struct { type FakeEtcdClient struct {
watchCompletedChan chan bool watchCompletedChan chan bool
Data map[string]EtcdResponseWithError Data map[string]EtcdResponseWithError
DeletedKeys []string DeletedKeys []string
Err error expectNotFoundGetSet map[string]struct{}
t TestLogger Err error
Ix int t TestLogger
TestIndex bool Ix int
ChangeIndex uint64 TestIndex bool
ChangeIndex uint64
// Will become valid after Watch is called; tester may write to it. Tester may // Will become valid after Watch is called; tester may write to it. Tester may
// also read from it to verify that it's closed after injecting an error. // also read from it to verify that it's closed after injecting an error.
@ -55,8 +56,9 @@ type FakeEtcdClient struct {
func MakeFakeEtcdClient(t TestLogger) *FakeEtcdClient { func MakeFakeEtcdClient(t TestLogger) *FakeEtcdClient {
ret := &FakeEtcdClient{ ret := &FakeEtcdClient{
t: t, t: t,
Data: map[string]EtcdResponseWithError{}, expectNotFoundGetSet: map[string]struct{}{},
Data: map[string]EtcdResponseWithError{},
} }
// There are three publicly accessible channels in FakeEtcdClient: // There are three publicly accessible channels in FakeEtcdClient:
// - WatchResponse // - WatchResponse
@ -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 {
f.t.Errorf("Unexpected get for %s", key) if _, ok := f.expectNotFoundGetSet[key]; !ok {
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)