2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2014-09-25 06:51:27 +04:00
|
|
|
package etcdserver
|
|
|
|
|
|
|
|
import (
|
2014-11-06 10:12:48 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2014-10-23 03:15:37 +04:00
|
|
|
"path"
|
2014-09-27 03:50:15 +04:00
|
|
|
"reflect"
|
2014-09-25 06:51:27 +04:00
|
|
|
"testing"
|
2014-10-23 03:15:37 +04:00
|
|
|
|
2015-01-02 22:21:23 +03:00
|
|
|
"github.com/coreos/etcd/pkg/testutil"
|
2014-10-31 03:31:14 +03:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-11-06 10:12:48 +03:00
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
2014-10-23 03:15:37 +04:00
|
|
|
"github.com/coreos/etcd/store"
|
2014-09-25 06:51:27 +04:00
|
|
|
)
|
|
|
|
|
2014-10-23 03:15:37 +04:00
|
|
|
func TestClusterMember(t *testing.T) {
|
2014-11-11 22:19:34 +03:00
|
|
|
membs := []*Member{
|
|
|
|
newTestMember(1, nil, "node1", nil),
|
|
|
|
newTestMember(2, nil, "node2", nil),
|
2014-10-23 03:15:37 +04:00
|
|
|
}
|
2014-09-25 06:51:27 +04:00
|
|
|
tests := []struct {
|
2014-10-31 03:31:14 +03:00
|
|
|
id types.ID
|
2014-10-23 03:15:37 +04:00
|
|
|
match bool
|
2014-09-25 06:51:27 +04:00
|
|
|
}{
|
2014-10-23 03:15:37 +04:00
|
|
|
{1, true},
|
|
|
|
{2, true},
|
|
|
|
{3, false},
|
2014-09-25 06:51:27 +04:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2014-10-23 03:15:37 +04:00
|
|
|
c := newTestCluster(membs)
|
|
|
|
m := c.Member(tt.id)
|
|
|
|
if g := m != nil; g != tt.match {
|
|
|
|
t.Errorf("#%d: find member = %v, want %v", i, g, tt.match)
|
2014-09-25 06:51:27 +04:00
|
|
|
}
|
2014-10-23 03:15:37 +04:00
|
|
|
if m != nil && m.ID != tt.id {
|
|
|
|
t.Errorf("#%d: id = %x, want %x", i, m.ID, tt.id)
|
2014-09-25 06:51:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 00:22:51 +04:00
|
|
|
func TestClusterMemberByName(t *testing.T) {
|
2014-11-11 22:19:34 +03:00
|
|
|
membs := []*Member{
|
|
|
|
newTestMember(1, nil, "node1", nil),
|
|
|
|
newTestMember(2, nil, "node2", nil),
|
2014-10-11 15:20:14 +04:00
|
|
|
}
|
2014-10-23 03:15:37 +04:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
match bool
|
|
|
|
}{
|
|
|
|
{"node1", true},
|
|
|
|
{"node2", true},
|
|
|
|
{"node3", false},
|
2014-09-25 06:51:27 +04:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2014-10-23 03:15:37 +04:00
|
|
|
c := newTestCluster(membs)
|
2014-10-23 22:08:42 +04:00
|
|
|
m := c.MemberByName(tt.name)
|
2014-10-23 03:15:37 +04:00
|
|
|
if g := m != nil; g != tt.match {
|
|
|
|
t.Errorf("#%d: find member = %v, want %v", i, g, tt.match)
|
|
|
|
}
|
|
|
|
if m != nil && m.Name != tt.name {
|
|
|
|
t.Errorf("#%d: name = %v, want %v", i, m.Name, tt.name)
|
2014-09-25 06:51:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 15:20:14 +04:00
|
|
|
func TestClusterMemberIDs(t *testing.T) {
|
2014-11-11 22:19:34 +03:00
|
|
|
c := newTestCluster([]*Member{
|
|
|
|
newTestMember(1, nil, "", nil),
|
|
|
|
newTestMember(4, nil, "", nil),
|
|
|
|
newTestMember(100, nil, "", nil),
|
2014-10-04 01:52:29 +04:00
|
|
|
})
|
2014-10-31 03:31:14 +03:00
|
|
|
w := []types.ID{1, 4, 100}
|
2014-10-23 03:15:37 +04:00
|
|
|
g := c.MemberIDs()
|
2014-10-04 01:52:29 +04:00
|
|
|
if !reflect.DeepEqual(w, g) {
|
2014-10-23 03:15:37 +04:00
|
|
|
t.Errorf("IDs = %+v, want %+v", g, w)
|
2014-09-25 06:51:27 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-27 03:50:15 +04:00
|
|
|
|
2014-09-29 01:38:40 +04:00
|
|
|
func TestClusterPeerURLs(t *testing.T) {
|
2014-09-29 01:35:48 +04:00
|
|
|
tests := []struct {
|
2014-11-11 22:19:34 +03:00
|
|
|
mems []*Member
|
2014-09-29 01:38:40 +04:00
|
|
|
wurls []string
|
2014-09-29 01:35:48 +04:00
|
|
|
}{
|
|
|
|
// single peer with a single address
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(1, []string{"http://192.0.2.1"}, "", nil),
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
2014-09-29 01:38:40 +04:00
|
|
|
wurls: []string{"http://192.0.2.1"},
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// single peer with a single address with a port
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(1, []string{"http://192.0.2.1:8001"}, "", nil),
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
2014-09-29 01:38:40 +04:00
|
|
|
wurls: []string{"http://192.0.2.1:8001"},
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// several members explicitly unsorted
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(2, []string{"http://192.0.2.3", "http://192.0.2.4"}, "", nil),
|
|
|
|
newTestMember(3, []string{"http://192.0.2.5", "http://192.0.2.6"}, "", nil),
|
|
|
|
newTestMember(1, []string{"http://192.0.2.1", "http://192.0.2.2"}, "", nil),
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
2014-09-29 01:38:40 +04:00
|
|
|
wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// no members
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{},
|
2014-09-29 01:38:40 +04:00
|
|
|
wurls: []string{},
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
|
|
|
|
2014-09-29 01:38:40 +04:00
|
|
|
// peer with no peer urls
|
2014-09-29 01:35:48 +04:00
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(3, []string{}, "", nil),
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
2014-09-29 01:38:40 +04:00
|
|
|
wurls: []string{},
|
2014-09-29 01:35:48 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-10-23 03:15:37 +04:00
|
|
|
c := newTestCluster(tt.mems)
|
2014-09-29 01:38:40 +04:00
|
|
|
urls := c.PeerURLs()
|
|
|
|
if !reflect.DeepEqual(urls, tt.wurls) {
|
|
|
|
t.Errorf("#%d: PeerURLs = %v, want %v", i, urls, tt.wurls)
|
2014-09-29 01:35:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 03:50:15 +04:00
|
|
|
func TestClusterClientURLs(t *testing.T) {
|
|
|
|
tests := []struct {
|
2014-11-11 22:19:34 +03:00
|
|
|
mems []*Member
|
2014-09-27 03:50:15 +04:00
|
|
|
wurls []string
|
|
|
|
}{
|
|
|
|
// single peer with a single address
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(1, nil, "", []string{"http://192.0.2.1"}),
|
2014-09-27 03:50:15 +04:00
|
|
|
},
|
|
|
|
wurls: []string{"http://192.0.2.1"},
|
|
|
|
},
|
|
|
|
|
|
|
|
// single peer with a single address with a port
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(1, nil, "", []string{"http://192.0.2.1:8001"}),
|
2014-09-27 03:50:15 +04:00
|
|
|
},
|
|
|
|
wurls: []string{"http://192.0.2.1:8001"},
|
|
|
|
},
|
|
|
|
|
|
|
|
// several members explicitly unsorted
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(2, nil, "", []string{"http://192.0.2.3", "http://192.0.2.4"}),
|
|
|
|
newTestMember(3, nil, "", []string{"http://192.0.2.5", "http://192.0.2.6"}),
|
|
|
|
newTestMember(1, nil, "", []string{"http://192.0.2.1", "http://192.0.2.2"}),
|
2014-09-27 03:50:15 +04:00
|
|
|
},
|
|
|
|
wurls: []string{"http://192.0.2.1", "http://192.0.2.2", "http://192.0.2.3", "http://192.0.2.4", "http://192.0.2.5", "http://192.0.2.6"},
|
|
|
|
},
|
|
|
|
|
|
|
|
// no members
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{},
|
2014-09-27 03:50:15 +04:00
|
|
|
wurls: []string{},
|
|
|
|
},
|
|
|
|
|
2014-09-29 01:38:40 +04:00
|
|
|
// peer with no client urls
|
2014-09-27 03:50:15 +04:00
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
mems: []*Member{
|
|
|
|
newTestMember(3, nil, "", []string{}),
|
2014-09-27 03:50:15 +04:00
|
|
|
},
|
|
|
|
wurls: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-10-23 03:15:37 +04:00
|
|
|
c := newTestCluster(tt.mems)
|
2014-09-27 03:50:15 +04:00
|
|
|
urls := c.ClientURLs()
|
|
|
|
if !reflect.DeepEqual(urls, tt.wurls) {
|
2014-09-29 01:38:40 +04:00
|
|
|
t.Errorf("#%d: ClientURLs = %v, want %v", i, urls, tt.wurls)
|
2014-09-27 03:50:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 03:15:37 +04:00
|
|
|
|
2014-10-24 03:41:58 +04:00
|
|
|
func TestClusterValidateAndAssignIDsBad(t *testing.T) {
|
|
|
|
tests := []struct {
|
2014-11-11 22:19:34 +03:00
|
|
|
clmembs []*Member
|
2014-10-24 03:41:58 +04:00
|
|
|
membs []*Member
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// unmatched length
|
2014-11-11 22:19:34 +03:00
|
|
|
[]*Member{
|
|
|
|
newTestMember(1, []string{"http://127.0.0.1:2379"}, "", nil),
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
|
|
|
[]*Member{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// unmatched peer urls
|
2014-11-11 22:19:34 +03:00
|
|
|
[]*Member{
|
|
|
|
newTestMember(1, []string{"http://127.0.0.1:2379"}, "", nil),
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
|
|
|
[]*Member{
|
2014-11-11 22:09:33 +03:00
|
|
|
newTestMember(1, []string{"http://127.0.0.1:4001"}, "", nil),
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// unmatched peer urls
|
2014-11-11 22:19:34 +03:00
|
|
|
[]*Member{
|
|
|
|
newTestMember(1, []string{"http://127.0.0.1:2379"}, "", nil),
|
|
|
|
newTestMember(2, []string{"http://127.0.0.2:2379"}, "", nil),
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
|
|
|
[]*Member{
|
2014-11-11 22:09:33 +03:00
|
|
|
newTestMember(1, []string{"http://127.0.0.1:2379"}, "", nil),
|
|
|
|
newTestMember(2, []string{"http://127.0.0.2:4001"}, "", nil),
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2014-11-18 01:08:45 +03:00
|
|
|
ecl := newTestCluster(tt.clmembs)
|
|
|
|
lcl := newTestCluster(tt.membs)
|
|
|
|
if err := ValidateClusterAndAssignIDs(lcl, ecl); err == nil {
|
2014-10-24 03:41:58 +04:00
|
|
|
t.Errorf("#%d: unexpected update success", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClusterValidateAndAssignIDs(t *testing.T) {
|
|
|
|
tests := []struct {
|
2014-11-11 22:19:34 +03:00
|
|
|
clmembs []*Member
|
2014-10-24 03:41:58 +04:00
|
|
|
membs []*Member
|
2014-10-31 03:31:14 +03:00
|
|
|
wids []types.ID
|
2014-10-24 03:41:58 +04:00
|
|
|
}{
|
|
|
|
{
|
2014-11-11 22:19:34 +03:00
|
|
|
[]*Member{
|
|
|
|
newTestMember(1, []string{"http://127.0.0.1:2379"}, "", nil),
|
|
|
|
newTestMember(2, []string{"http://127.0.0.2:2379"}, "", nil),
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
|
|
|
[]*Member{
|
2014-11-11 22:09:33 +03:00
|
|
|
newTestMember(3, []string{"http://127.0.0.1:2379"}, "", nil),
|
|
|
|
newTestMember(4, []string{"http://127.0.0.2:2379"}, "", nil),
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
2014-10-31 03:31:14 +03:00
|
|
|
[]types.ID{3, 4},
|
2014-10-24 03:41:58 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
2014-11-18 01:08:45 +03:00
|
|
|
lcl := newTestCluster(tt.clmembs)
|
|
|
|
ecl := newTestCluster(tt.membs)
|
|
|
|
if err := ValidateClusterAndAssignIDs(lcl, ecl); err != nil {
|
2014-10-24 03:41:58 +04:00
|
|
|
t.Errorf("#%d: unexpect update error: %v", i, err)
|
|
|
|
}
|
2014-11-18 01:08:45 +03:00
|
|
|
if !reflect.DeepEqual(lcl.MemberIDs(), tt.wids) {
|
|
|
|
t.Errorf("#%d: ids = %v, want %v", i, lcl.MemberIDs(), tt.wids)
|
2014-10-24 03:41:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 10:12:48 +03:00
|
|
|
func TestClusterValidateConfigurationChange(t *testing.T) {
|
|
|
|
cl := newCluster("")
|
|
|
|
cl.SetStore(store.New())
|
|
|
|
for i := 1; i <= 4; i++ {
|
|
|
|
attr := RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", i)}}
|
2015-04-19 21:08:03 +03:00
|
|
|
cl.AddMember(&Member{ID: types.ID(i), RaftAttributes: attr})
|
2014-11-06 10:12:48 +03:00
|
|
|
}
|
2015-04-19 21:08:03 +03:00
|
|
|
cl.RemoveMember(4)
|
2014-11-06 10:12:48 +03:00
|
|
|
|
|
|
|
attr := RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", 1)}}
|
2014-11-11 22:46:49 +03:00
|
|
|
ctx, err := json.Marshal(&Member{ID: types.ID(5), RaftAttributes: attr})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr = RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", 5)}}
|
|
|
|
ctx5, err := json.Marshal(&Member{ID: types.ID(5), RaftAttributes: attr})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr = RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", 3)}}
|
|
|
|
ctx2to3, err := json.Marshal(&Member{ID: types.ID(2), RaftAttributes: attr})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attr = RaftAttributes{PeerURLs: []string{fmt.Sprintf("http://127.0.0.1:%d", 5)}}
|
|
|
|
ctx2to5, err := json.Marshal(&Member{ID: types.ID(2), RaftAttributes: attr})
|
2014-11-06 10:12:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
cc raftpb.ConfChange
|
|
|
|
werr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
|
|
|
NodeID: 3,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeAddNode,
|
|
|
|
NodeID: 4,
|
|
|
|
},
|
|
|
|
ErrIDRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
|
|
|
NodeID: 4,
|
|
|
|
},
|
|
|
|
ErrIDRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeAddNode,
|
|
|
|
NodeID: 1,
|
|
|
|
},
|
|
|
|
ErrIDExists,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeAddNode,
|
|
|
|
NodeID: 5,
|
2014-11-11 22:46:49 +03:00
|
|
|
Context: ctx,
|
2014-11-06 10:12:48 +03:00
|
|
|
},
|
|
|
|
ErrPeerURLexists,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeRemoveNode,
|
|
|
|
NodeID: 5,
|
|
|
|
},
|
|
|
|
ErrIDNotFound,
|
|
|
|
},
|
2014-11-11 22:46:49 +03:00
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeAddNode,
|
|
|
|
NodeID: 5,
|
|
|
|
Context: ctx5,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeUpdateNode,
|
|
|
|
NodeID: 5,
|
|
|
|
Context: ctx,
|
|
|
|
},
|
|
|
|
ErrIDNotFound,
|
|
|
|
},
|
|
|
|
// try to change the peer url of 2 to the peer url of 3
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeUpdateNode,
|
|
|
|
NodeID: 2,
|
|
|
|
Context: ctx2to3,
|
|
|
|
},
|
|
|
|
ErrPeerURLexists,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
raftpb.ConfChange{
|
|
|
|
Type: raftpb.ConfChangeUpdateNode,
|
|
|
|
NodeID: 2,
|
|
|
|
Context: ctx2to5,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
2014-11-06 10:12:48 +03:00
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
err := cl.ValidateConfigurationChange(tt.cc)
|
|
|
|
if err != tt.werr {
|
|
|
|
t.Errorf("#%d: validateConfigurationChange error = %v, want %v", i, err, tt.werr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 03:15:37 +04:00
|
|
|
func TestClusterGenID(t *testing.T) {
|
2014-11-11 22:19:34 +03:00
|
|
|
cs := newTestCluster([]*Member{
|
|
|
|
newTestMember(1, nil, "", nil),
|
|
|
|
newTestMember(2, nil, "", nil),
|
2014-10-23 03:15:37 +04:00
|
|
|
})
|
|
|
|
|
2014-10-23 22:50:15 +04:00
|
|
|
cs.genID()
|
2014-10-23 03:15:37 +04:00
|
|
|
if cs.ID() == 0 {
|
|
|
|
t.Fatalf("cluster.ID = %v, want not 0", cs.ID())
|
|
|
|
}
|
|
|
|
previd := cs.ID()
|
|
|
|
|
|
|
|
cs.SetStore(&storeRecorder{})
|
2015-04-19 21:08:03 +03:00
|
|
|
cs.AddMember(newTestMember(3, nil, "", nil))
|
2014-10-23 22:50:15 +04:00
|
|
|
cs.genID()
|
2014-10-23 03:15:37 +04:00
|
|
|
if cs.ID() == previd {
|
|
|
|
t.Fatalf("cluster.ID = %v, want not %v", cs.ID(), previd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeToMemberBad(t *testing.T) {
|
|
|
|
tests := []*store.NodeExtern{
|
|
|
|
{Key: "/1234", Nodes: []*store.NodeExtern{
|
|
|
|
{Key: "/1234/strange"},
|
|
|
|
}},
|
|
|
|
{Key: "/1234", Nodes: []*store.NodeExtern{
|
2014-11-06 03:40:15 +03:00
|
|
|
{Key: "/1234/raftAttributes", Value: stringp("garbage")},
|
2014-10-23 03:15:37 +04:00
|
|
|
}},
|
|
|
|
{Key: "/1234", Nodes: []*store.NodeExtern{
|
2014-11-06 03:40:15 +03:00
|
|
|
{Key: "/1234/attributes", Value: stringp(`{"name":"node1","clientURLs":null}`)},
|
2014-10-23 03:15:37 +04:00
|
|
|
}},
|
|
|
|
{Key: "/1234", Nodes: []*store.NodeExtern{
|
2014-11-06 03:40:15 +03:00
|
|
|
{Key: "/1234/raftAttributes", Value: stringp(`{"peerURLs":null}`)},
|
2014-10-23 03:15:37 +04:00
|
|
|
{Key: "/1234/strange"},
|
|
|
|
}},
|
|
|
|
{Key: "/1234", Nodes: []*store.NodeExtern{
|
2014-11-06 03:40:15 +03:00
|
|
|
{Key: "/1234/raftAttributes", Value: stringp(`{"peerURLs":null}`)},
|
|
|
|
{Key: "/1234/attributes", Value: stringp("garbage")},
|
2014-10-23 03:15:37 +04:00
|
|
|
}},
|
|
|
|
{Key: "/1234", Nodes: []*store.NodeExtern{
|
2014-11-06 03:40:15 +03:00
|
|
|
{Key: "/1234/raftAttributes", Value: stringp(`{"peerURLs":null}`)},
|
|
|
|
{Key: "/1234/attributes", Value: stringp(`{"name":"node1","clientURLs":null}`)},
|
2014-10-23 03:15:37 +04:00
|
|
|
{Key: "/1234/strange"},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
if _, err := nodeToMember(tt); err == nil {
|
|
|
|
t.Errorf("#%d: unexpected nil error", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClusterAddMember(t *testing.T) {
|
|
|
|
st := &storeRecorder{}
|
|
|
|
c := newTestCluster(nil)
|
|
|
|
c.SetStore(st)
|
2015-04-19 21:08:03 +03:00
|
|
|
c.AddMember(newTestMember(1, nil, "node1", nil))
|
2014-10-23 03:15:37 +04:00
|
|
|
|
2015-01-02 22:21:23 +03:00
|
|
|
wactions := []testutil.Action{
|
2014-10-23 03:15:37 +04:00
|
|
|
{
|
2015-01-02 22:21:23 +03:00
|
|
|
Name: "Create",
|
|
|
|
Params: []interface{}{
|
2014-10-23 03:15:37 +04:00
|
|
|
path.Join(storeMembersPrefix, "1", "raftAttributes"),
|
|
|
|
false,
|
2014-10-24 21:57:56 +04:00
|
|
|
`{"peerURLs":null}`,
|
2014-10-23 03:15:37 +04:00
|
|
|
false,
|
|
|
|
store.Permanent,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if g := st.Action(); !reflect.DeepEqual(g, wactions) {
|
|
|
|
t.Errorf("actions = %v, want %v", g, wactions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 02:57:27 +04:00
|
|
|
func TestClusterMembers(t *testing.T) {
|
2015-05-13 03:22:06 +03:00
|
|
|
cls := &cluster{
|
2014-10-31 03:31:14 +03:00
|
|
|
members: map[types.ID]*Member{
|
2014-10-24 02:57:27 +04:00
|
|
|
1: &Member{ID: 1},
|
|
|
|
20: &Member{ID: 20},
|
|
|
|
100: &Member{ID: 100},
|
|
|
|
5: &Member{ID: 5},
|
|
|
|
50: &Member{ID: 50},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
w := []*Member{
|
|
|
|
&Member{ID: 1},
|
|
|
|
&Member{ID: 5},
|
|
|
|
&Member{ID: 20},
|
|
|
|
&Member{ID: 50},
|
|
|
|
&Member{ID: 100},
|
|
|
|
}
|
|
|
|
if g := cls.Members(); !reflect.DeepEqual(g, w) {
|
|
|
|
t.Fatalf("Members()=%#v, want %#v", g, w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 03:15:37 +04:00
|
|
|
func TestClusterRemoveMember(t *testing.T) {
|
2015-03-24 09:19:38 +03:00
|
|
|
st := &storeRecorder{}
|
2015-04-19 21:08:03 +03:00
|
|
|
c := newTestCluster(nil)
|
2014-10-23 03:15:37 +04:00
|
|
|
c.SetStore(st)
|
2015-04-19 21:08:03 +03:00
|
|
|
c.RemoveMember(1)
|
2014-10-23 03:15:37 +04:00
|
|
|
|
2015-01-02 22:21:23 +03:00
|
|
|
wactions := []testutil.Action{
|
|
|
|
{Name: "Delete", Params: []interface{}{memberStoreKey(1), true, true}},
|
|
|
|
{Name: "Create", Params: []interface{}{removedMemberStoreKey(1), false, "", false, store.Permanent}},
|
2014-10-23 03:15:37 +04:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(st.Action(), wactions) {
|
|
|
|
t.Errorf("actions = %v, want %v", st.Action(), wactions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeToMember(t *testing.T) {
|
|
|
|
n := &store.NodeExtern{Key: "/1234", Nodes: []*store.NodeExtern{
|
2014-10-24 21:57:56 +04:00
|
|
|
{Key: "/1234/attributes", Value: stringp(`{"name":"node1","clientURLs":null}`)},
|
|
|
|
{Key: "/1234/raftAttributes", Value: stringp(`{"peerURLs":null}`)},
|
2014-10-23 03:15:37 +04:00
|
|
|
}}
|
|
|
|
wm := &Member{ID: 0x1234, RaftAttributes: RaftAttributes{}, Attributes: Attributes{Name: "node1"}}
|
|
|
|
m, err := nodeToMember(n)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected nodeToMember error: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(m, wm) {
|
|
|
|
t.Errorf("member = %+v, want %+v", m, wm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 03:22:06 +03:00
|
|
|
func newTestCluster(membs []*Member) *cluster {
|
|
|
|
c := &cluster{members: make(map[types.ID]*Member), removed: make(map[types.ID]bool)}
|
2014-11-06 03:40:15 +03:00
|
|
|
for _, m := range membs {
|
|
|
|
c.members[m.ID] = m
|
2014-10-23 03:15:37 +04:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
2014-12-31 01:51:26 +03:00
|
|
|
|
|
|
|
func stringp(s string) *string { return &s }
|