migrate from pkg/store to docker/libkv

Signed-off-by: Alexandre Beslic <abronan@docker.com>
This commit is contained in:
Alexandre Beslic 2015-06-15 17:36:10 -07:00
parent 04d55f3951
commit f50cd10061
7 changed files with 65 additions and 44 deletions

View File

@ -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 ./...

View File

@ -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{

View File

@ -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.

View File

@ -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

View File

@ -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()

View File

@ -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.

View File

@ -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.