2017-03-27 07:15:42 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2017-03-27 07:15:42 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2021-11-15 15:51:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/types"
|
2017-03-27 07:15:42 +00:00
|
|
|
)
|
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
func TestMerge_LAN(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
members []*serf.Member
|
|
|
|
expect string
|
2017-03-27 07:15:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
const thisNodeID = "ee954a2f-80de-4b34-8780-97b942a50a99"
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
delegate := &lanMergeDelegate{
|
|
|
|
dc: "dc1",
|
|
|
|
nodeID: types.NodeID(thisNodeID),
|
|
|
|
nodeName: "node0",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := delegate.NotifyMerge(tc.members)
|
|
|
|
|
|
|
|
if tc.expect == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
testutil.RequireErrorContains(t, err, tc.expect)
|
|
|
|
}
|
2017-03-27 07:15:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
cases := map[string]testcase{
|
|
|
|
"client in the wrong datacenter": {
|
2017-03-27 07:15:42 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc2",
|
|
|
|
name: "node1",
|
|
|
|
server: false,
|
|
|
|
build: "0.7.5",
|
|
|
|
}),
|
2017-03-27 07:15:42 +00:00
|
|
|
},
|
|
|
|
expect: "wrong datacenter",
|
|
|
|
},
|
2021-11-15 15:51:14 +00:00
|
|
|
"server in the wrong datacenter": {
|
2017-03-27 07:15:42 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc2",
|
|
|
|
name: "node1",
|
|
|
|
server: true,
|
|
|
|
build: "0.7.5",
|
|
|
|
}),
|
2017-03-27 07:15:42 +00:00
|
|
|
},
|
|
|
|
expect: "wrong datacenter",
|
|
|
|
},
|
2021-11-15 15:51:14 +00:00
|
|
|
"node ID conflict with delegate's ID": {
|
2017-03-27 07:15:42 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc1",
|
|
|
|
name: "node1",
|
|
|
|
id: thisNodeID,
|
|
|
|
server: true,
|
|
|
|
build: "0.7.5",
|
|
|
|
}),
|
2017-03-27 07:15:42 +00:00
|
|
|
},
|
|
|
|
expect: "with this agent's ID",
|
|
|
|
},
|
2021-11-15 15:51:14 +00:00
|
|
|
"cluster with existing conflicting node IDs": {
|
2017-03-27 07:15:42 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc1",
|
|
|
|
name: "node1",
|
|
|
|
id: "6185913b-98d7-4441-bd8f-f7f7d854a4af",
|
|
|
|
server: true,
|
|
|
|
build: "0.8.5",
|
|
|
|
}),
|
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc1",
|
|
|
|
name: "node2",
|
|
|
|
id: "6185913b-98d7-4441-bd8f-f7f7d854a4af",
|
|
|
|
server: true,
|
|
|
|
build: "0.9.0",
|
|
|
|
}),
|
2017-09-06 05:57:29 +00:00
|
|
|
},
|
|
|
|
expect: "with member",
|
|
|
|
},
|
2021-11-15 15:51:14 +00:00
|
|
|
"cluster with existing conflicting node IDs, but version is old enough to skip the check": {
|
2017-09-06 05:57:29 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc1",
|
|
|
|
name: "node1",
|
|
|
|
id: "6185913b-98d7-4441-bd8f-f7f7d854a4af",
|
|
|
|
server: true,
|
|
|
|
build: "0.8.5",
|
|
|
|
}),
|
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc1",
|
|
|
|
name: "node2",
|
|
|
|
id: "6185913b-98d7-4441-bd8f-f7f7d854a4af",
|
|
|
|
server: true,
|
|
|
|
build: "0.8.4",
|
|
|
|
}),
|
2017-03-27 07:15:42 +00:00
|
|
|
},
|
|
|
|
expect: "with member",
|
|
|
|
},
|
2021-11-15 15:51:14 +00:00
|
|
|
"good cluster": {
|
2017-03-27 07:15:42 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc1",
|
|
|
|
name: "node1",
|
|
|
|
server: true,
|
|
|
|
build: "0.8.5",
|
|
|
|
}),
|
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc1",
|
|
|
|
name: "node2",
|
|
|
|
server: true,
|
|
|
|
build: "0.8.5",
|
|
|
|
}),
|
2017-03-27 07:15:42 +00:00
|
|
|
},
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
2017-03-27 07:15:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMerge_WAN(t *testing.T) {
|
2021-11-15 15:51:14 +00:00
|
|
|
type testcase struct {
|
2017-03-27 07:15:42 +00:00
|
|
|
members []*serf.Member
|
|
|
|
expect string
|
2021-11-15 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testcase) {
|
|
|
|
delegate := &wanMergeDelegate{}
|
|
|
|
err := delegate.NotifyMerge(tc.members)
|
|
|
|
if tc.expect == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
testutil.RequireErrorContains(t, err, tc.expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testcase{
|
|
|
|
"not a server": {
|
2017-03-27 07:15:42 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc2",
|
|
|
|
name: "node1",
|
|
|
|
server: false,
|
|
|
|
build: "0.7.5",
|
|
|
|
}),
|
2017-03-27 07:15:42 +00:00
|
|
|
},
|
|
|
|
expect: "not a server",
|
|
|
|
},
|
2021-11-15 15:51:14 +00:00
|
|
|
"good cluster": {
|
2017-03-27 07:15:42 +00:00
|
|
|
members: []*serf.Member{
|
2021-11-15 15:51:14 +00:00
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc2",
|
|
|
|
name: "node1",
|
|
|
|
server: true,
|
|
|
|
build: "0.7.5",
|
|
|
|
}),
|
|
|
|
makeTestNode(t, testMember{
|
|
|
|
dc: "dc3",
|
|
|
|
name: "node2",
|
|
|
|
server: true,
|
|
|
|
build: "0.7.5",
|
|
|
|
}),
|
2017-03-27 07:15:42 +00:00
|
|
|
},
|
|
|
|
expect: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testMember struct {
|
|
|
|
dc string
|
|
|
|
name string
|
|
|
|
id string
|
|
|
|
server bool
|
|
|
|
build string
|
|
|
|
segment string
|
|
|
|
partition string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm testMember) role() string {
|
|
|
|
if tm.server {
|
|
|
|
return "consul"
|
|
|
|
}
|
|
|
|
return "node"
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestNode(t *testing.T, tm testMember) *serf.Member {
|
|
|
|
if tm.id == "" {
|
|
|
|
uuid, err := uuid.GenerateUUID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
tm.id = uuid
|
|
|
|
}
|
|
|
|
m := &serf.Member{
|
|
|
|
Name: tm.name,
|
|
|
|
Tags: map[string]string{
|
|
|
|
"role": tm.role(),
|
|
|
|
"dc": tm.dc,
|
|
|
|
"id": tm.id,
|
|
|
|
"port": "8300",
|
|
|
|
"segment": tm.segment,
|
|
|
|
"build": tm.build,
|
|
|
|
"vsn": "2",
|
|
|
|
"vsn_max": "3",
|
|
|
|
"vsn_min": "2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if tm.partition != "" {
|
|
|
|
m.Tags["ap"] = tm.partition
|
2017-03-27 07:15:42 +00:00
|
|
|
}
|
2021-11-15 15:51:14 +00:00
|
|
|
return m
|
2017-03-27 07:15:42 +00:00
|
|
|
}
|