diff --git a/discovery/kv/kv_test.go b/discovery/kv/kv_test.go index 514093e..fae2c10 100644 --- a/discovery/kv/kv_test.go +++ b/discovery/kv/kv_test.go @@ -8,8 +8,6 @@ import ( "github.com/docker/docker/pkg/discovery" "github.com/docker/libkv/store" - libkvmock "github.com/docker/libkv/store/mock" - "github.com/stretchr/testify/mock" "github.com/go-check/check" ) @@ -22,41 +20,38 @@ type DiscoverySuite struct{} var _ = check.Suite(&DiscoverySuite{}) func (ds *DiscoverySuite) TestInitialize(c *check.C) { - storeMock, err := libkvmock.New([]string{"127.0.0.1"}, nil) - c.Assert(storeMock, check.NotNil) - c.Assert(err, check.IsNil) - + storeMock := &FakeStore{ + Endpoints: []string{"127.0.0.1"}, + } d := &Discovery{backend: store.CONSUL} d.Initialize("127.0.0.1", 0, 0) d.store = storeMock - s := d.store.(*libkvmock.Mock) + s := d.store.(*FakeStore) c.Assert(s.Endpoints, check.HasLen, 1) c.Assert(s.Endpoints[0], check.Equals, "127.0.0.1") c.Assert(d.path, check.Equals, discoveryPath) - storeMock, err = libkvmock.New([]string{"127.0.0.1:1234"}, nil) - c.Assert(storeMock, check.NotNil) - c.Assert(err, check.IsNil) - + storeMock = &FakeStore{ + Endpoints: []string{"127.0.0.1:1234"}, + } d = &Discovery{backend: store.CONSUL} d.Initialize("127.0.0.1:1234/path", 0, 0) d.store = storeMock - s = d.store.(*libkvmock.Mock) + s = d.store.(*FakeStore) c.Assert(s.Endpoints, check.HasLen, 1) c.Assert(s.Endpoints[0], check.Equals, "127.0.0.1:1234") c.Assert(d.path, check.Equals, "path/"+discoveryPath) - storeMock, err = libkvmock.New([]string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"}, nil) - c.Assert(storeMock, check.NotNil) - c.Assert(err, check.IsNil) - + storeMock = &FakeStore{ + Endpoints: []string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"}, + } d = &Discovery{backend: store.CONSUL} d.Initialize("127.0.0.1:1234,127.0.0.2:1234,127.0.0.3:1234/path", 0, 0) d.store = storeMock - s = d.store.(*libkvmock.Mock) + s = d.store.(*FakeStore) c.Assert(s.Endpoints, check.HasLen, 3) c.Assert(s.Endpoints[0], check.Equals, "127.0.0.1:1234") c.Assert(s.Endpoints[1], check.Equals, "127.0.0.2:1234") @@ -66,21 +61,17 @@ func (ds *DiscoverySuite) TestInitialize(c *check.C) { } func (ds *DiscoverySuite) TestWatch(c *check.C) { - storeMock, err := libkvmock.New([]string{"127.0.0.1:1234"}, nil) - c.Assert(storeMock, check.NotNil) - c.Assert(err, check.IsNil) + mockCh := make(chan []*store.KVPair) + + storeMock := &FakeStore{ + Endpoints: []string{"127.0.0.1:1234"}, + mockKVChan: mockCh, + } d := &Discovery{backend: store.CONSUL} d.Initialize("127.0.0.1:1234/path", 0, 0) d.store = storeMock - s := d.store.(*libkvmock.Mock) - mockCh := make(chan []*store.KVPair) - - // The first watch will fail. - s.On("WatchTree", "path/"+discoveryPath, mock.Anything).Return(mockCh, errors.New("test error")).Once() - // The second one will succeed. - s.On("WatchTree", "path/"+discoveryPath, mock.Anything).Return(mockCh, nil).Once() expected := discovery.Entries{ &discovery.Entry{Host: "1.1.1.1", Port: "1111"}, &discovery.Entry{Host: "2.2.2.2", Port: "2222"}, @@ -111,9 +102,6 @@ func (ds *DiscoverySuite) TestWatch(c *check.C) { mockCh <- kvs c.Assert(<-ch, check.DeepEquals, expected) - // Make sure that if an error occurs it retries. - // This third call to WatchTree will be checked later by AssertExpectations. - s.On("WatchTree", "path/"+discoveryPath, mock.Anything).Return(mockCh, nil) close(mockCh) // Give it enough time to call WatchTree. time.Sleep(3) @@ -122,6 +110,68 @@ func (ds *DiscoverySuite) TestWatch(c *check.C) { close(stopCh) c.Assert(<-ch, check.IsNil) c.Assert(<-errCh, check.IsNil) - - s.AssertExpectations(c) +} + +// FakeStore implements store.Store methods. It mocks all store +// function in a simple, naive way. +type FakeStore struct { + Endpoints []string + Options *store.Config + mockKVChan <-chan []*store.KVPair + + watchTreeCallCount int +} + +func (s *FakeStore) Put(key string, value []byte, options *store.WriteOptions) error { + return nil +} + +func (s *FakeStore) Get(key string) (*store.KVPair, error) { + return nil, nil +} + +func (s *FakeStore) Delete(key string) error { + return nil +} + +func (s *FakeStore) Exists(key string) (bool, error) { + return true, nil +} + +func (s *FakeStore) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) { + return nil, nil +} + +// WatchTree will fail the first time, and return the mockKVchan afterwards. +// This is the behaviour we need for testing.. If we need 'moar', should update this. +func (s *FakeStore) WatchTree(directory string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) { + if s.watchTreeCallCount == 0 { + s.watchTreeCallCount = 1 + return nil, errors.New("test error") + } + // First calls error + return s.mockKVChan, nil +} + +func (s *FakeStore) NewLock(key string, options *store.LockOptions) (store.Locker, error) { + return nil, nil +} + +func (s *FakeStore) List(directory string) ([]*store.KVPair, error) { + return []*store.KVPair{}, nil +} + +func (s *FakeStore) DeleteTree(directory string) error { + return nil +} + +func (s *FakeStore) AtomicPut(key string, value []byte, previous *store.KVPair, options *store.WriteOptions) (bool, *store.KVPair, error) { + return true, nil, nil +} + +func (s *FakeStore) AtomicDelete(key string, previous *store.KVPair) (bool, error) { + return true, nil +} + +func (s *FakeStore) Close() { }