From f50cd10061c75891343b3ba803d68087edc5bafd Mon Sep 17 00:00:00 2001 From: Alexandre Beslic Date: Mon, 15 Jun 2015 17:36:10 -0700 Subject: [PATCH] migrate from pkg/store to docker/libkv Signed-off-by: Alexandre Beslic --- .travis.yml | 8 ----- discovery/kv/kv.go | 5 +-- discovery/kv/kv_test.go | 59 +++++++++++++++++++++++++----------- leadership/candidate.go | 2 +- leadership/candidate_test.go | 11 ++++--- leadership/follower.go | 2 +- leadership/follower_test.go | 22 ++++++++------ 7 files changed, 65 insertions(+), 44 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8779c75e6b..9289a40952 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,15 +19,7 @@ install: - go get github.com/golang/lint/golint - go get github.com/GeertJohan/fgt -before_script: - - script/travis_consul.sh 0.5.2 - - script/travis_etcd.sh 2.0.11 - - script/travis_zk.sh 3.4.6 - script: - - ./consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -config-file=./config.json 1>/dev/null & - - ./etcd/etcd --listen-client-urls 'http://0.0.0.0:4001' --advertise-client-urls 'http://127.0.0.1:4001' >/dev/null 2>&1 & - - ./zk/bin/zkServer.sh start ./zk/conf/zoo.cfg 1> /dev/null - script/validate-gofmt - go vet ./... - fgt golint ./... diff --git a/discovery/kv/kv.go b/discovery/kv/kv.go index cd5f6506cf..8a3d2b96f1 100644 --- a/discovery/kv/kv.go +++ b/discovery/kv/kv.go @@ -7,8 +7,9 @@ import ( "time" log "github.com/Sirupsen/logrus" + "github.com/docker/libkv" + "github.com/docker/libkv/store" "github.com/docker/swarm/discovery" - "github.com/docker/swarm/pkg/store" ) const ( @@ -55,7 +56,7 @@ func (s *Discovery) Initialize(uris string, heartbeat time.Duration, ttl time.Du // Creates a new store, will ignore options given // if not supported by the chosen store - s.store, err = store.NewStore( + s.store, err = libkv.NewStore( s.backend, addrs, &store.Config{ diff --git a/discovery/kv/kv_test.go b/discovery/kv/kv_test.go index 39d5d74063..f57dca2c03 100644 --- a/discovery/kv/kv_test.go +++ b/discovery/kv/kv_test.go @@ -6,42 +6,67 @@ import ( "testing" "time" + "github.com/docker/libkv/store" + libkvmock "github.com/docker/libkv/store/mock" "github.com/docker/swarm/discovery" - "github.com/docker/swarm/pkg/store" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) func TestInitialize(t *testing.T) { - d := &Discovery{backend: store.MOCK} - assert.NoError(t, d.Initialize("127.0.0.1", 0, 0)) - s := d.store.(*store.Mock) + storeMock, err := libkvmock.New([]string{"127.0.0.1"}, nil) + assert.NotNil(t, storeMock) + assert.NoError(t, err) + + d := &Discovery{backend: store.CONSUL} + d.Initialize("127.0.0.1", 0, 0) + d.store = storeMock + + s := d.store.(*libkvmock.Mock) assert.Len(t, s.Endpoints, 1) assert.Equal(t, s.Endpoints[0], "127.0.0.1") assert.Equal(t, d.path, discoveryPath) - d = &Discovery{backend: store.MOCK} - assert.NoError(t, d.Initialize("127.0.0.1:1234/path", 0, 0)) - s = d.store.(*store.Mock) + storeMock, err = libkvmock.New([]string{"127.0.0.1:1234"}, nil) + assert.NotNil(t, storeMock) + assert.NoError(t, err) + + d = &Discovery{backend: store.CONSUL} + d.Initialize("127.0.0.1:1234/path", 0, 0) + d.store = storeMock + + s = d.store.(*libkvmock.Mock) assert.Len(t, s.Endpoints, 1) assert.Equal(t, s.Endpoints[0], "127.0.0.1:1234") assert.Equal(t, d.path, "path/"+discoveryPath) - d = &Discovery{backend: store.MOCK} - assert.NoError(t, d.Initialize("127.0.0.1:1234,127.0.0.2:1234,127.0.0.3:1234/path", 0, 0)) - s = d.store.(*store.Mock) - assert.Len(t, s.Endpoints, 3) - assert.Equal(t, s.Endpoints[0], "127.0.0.1:1234") - assert.Equal(t, s.Endpoints[1], "127.0.0.2:1234") - assert.Equal(t, s.Endpoints[2], "127.0.0.3:1234") + storeMock, err = libkvmock.New([]string{"127.0.0.1:1234", "127.0.0.2:1234", "127.0.0.3:1234"}, nil) + assert.NotNil(t, storeMock) + assert.NoError(t, err) + + 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) + if assert.Len(t, s.Endpoints, 3) { + assert.Equal(t, s.Endpoints[0], "127.0.0.1:1234") + assert.Equal(t, s.Endpoints[1], "127.0.0.2:1234") + assert.Equal(t, s.Endpoints[2], "127.0.0.3:1234") + } assert.Equal(t, d.path, "path/"+discoveryPath) } func TestWatch(t *testing.T) { - d := &Discovery{backend: store.MOCK} - assert.NoError(t, d.Initialize("127.0.0.1:1234/path", 0, 0)) - s := d.store.(*store.Mock) + storeMock, err := libkvmock.New([]string{"127.0.0.1:1234"}, nil) + assert.NotNil(t, storeMock) + assert.NoError(t, err) + 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. diff --git a/leadership/candidate.go b/leadership/candidate.go index b7f7007208..614041b2d4 100644 --- a/leadership/candidate.go +++ b/leadership/candidate.go @@ -4,7 +4,7 @@ import ( "sync" log "github.com/Sirupsen/logrus" - "github.com/docker/swarm/pkg/store" + "github.com/docker/libkv/store" ) // Candidate runs the leader election algorithm asynchronously diff --git a/leadership/candidate_test.go b/leadership/candidate_test.go index 86ef3b115e..3240915d22 100644 --- a/leadership/candidate_test.go +++ b/leadership/candidate_test.go @@ -3,17 +3,18 @@ package leadership import ( "testing" - kv "github.com/docker/swarm/pkg/store" + libkvmock "github.com/docker/libkv/store/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) func TestCandidate(t *testing.T) { - store, err := kv.NewStore("mock", []string{}, nil) + kv, err := libkvmock.New([]string{}, nil) assert.NoError(t, err) + assert.NotNil(t, kv) - mockStore := store.(*kv.Mock) - mockLock := &kv.MockLock{} + mockStore := kv.(*libkvmock.Mock) + mockLock := &libkvmock.Lock{} mockStore.On("NewLock", "test_key", mock.Anything).Return(mockLock, nil) // Lock and unlock always succeeds. @@ -22,7 +23,7 @@ func TestCandidate(t *testing.T) { mockLock.On("Lock").Return(mockLostCh, nil) mockLock.On("Unlock").Return(nil) - candidate := NewCandidate(store, "test_key", "test_node") + candidate := NewCandidate(kv, "test_key", "test_node") candidate.RunForElection() electedCh := candidate.ElectedCh() diff --git a/leadership/follower.go b/leadership/follower.go index ff75a28cbc..50ed3b86d1 100644 --- a/leadership/follower.go +++ b/leadership/follower.go @@ -1,6 +1,6 @@ package leadership -import "github.com/docker/swarm/pkg/store" +import "github.com/docker/libkv/store" // Follower can folow an election in real-time and push notifications whenever // there is a change in leadership. diff --git a/leadership/follower_test.go b/leadership/follower_test.go index 943162ae77..c95e307091 100644 --- a/leadership/follower_test.go +++ b/leadership/follower_test.go @@ -3,31 +3,33 @@ package leadership import ( "testing" - kv "github.com/docker/swarm/pkg/store" + "github.com/docker/libkv/store" + libkvmock "github.com/docker/libkv/store/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" ) func TestFollower(t *testing.T) { - store, err := kv.NewStore("mock", []string{}, nil) + kv, err := libkvmock.New([]string{}, nil) assert.NoError(t, err) + assert.NotNil(t, kv) - mockStore := store.(*kv.Mock) + mockStore := kv.(*libkvmock.Mock) - kvCh := make(chan *kv.KVPair) - var mockKVCh <-chan *kv.KVPair = kvCh + kvCh := make(chan *store.KVPair) + var mockKVCh <-chan *store.KVPair = kvCh mockStore.On("Watch", "test_key", mock.Anything).Return(mockKVCh, nil) - follower := NewFollower(store, "test_key") + follower := NewFollower(kv, "test_key") follower.FollowElection() leaderCh := follower.LeaderCh() // Simulate leader updates go func() { - kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader1")} - kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader1")} - kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader2")} - kvCh <- &kv.KVPair{Key: "test_key", Value: []byte("leader1")} + kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader1")} + kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader1")} + kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader2")} + kvCh <- &store.KVPair{Key: "test_key", Value: []byte("leader1")} }() // We shouldn't see duplicate events.