2017-07-06 10:48:37 +00:00
|
|
|
package metadata_test
|
2016-02-20 01:26:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-06 10:48:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2016-02-20 01:26:45 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2020-06-05 19:28:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-20 01:26:45 +00:00
|
|
|
)
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
func TestServer_Key_params(t *testing.T) {
|
2016-03-27 00:58:12 +00:00
|
|
|
ipv4a := net.ParseIP("127.0.0.1")
|
|
|
|
ipv4b := net.ParseIP("1.2.3.4")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2017-07-06 10:48:37 +00:00
|
|
|
sd1 *metadata.Server
|
|
|
|
sd2 *metadata.Server
|
2016-03-27 00:58:12 +00:00
|
|
|
equal bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Addr inequality",
|
2017-07-06 10:48:37 +00:00
|
|
|
sd1: &metadata.Server{
|
2016-03-27 00:58:12 +00:00
|
|
|
Name: "s1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Port: 8300,
|
|
|
|
Addr: &net.IPAddr{IP: ipv4a},
|
|
|
|
},
|
2017-07-06 10:48:37 +00:00
|
|
|
sd2: &metadata.Server{
|
2016-03-27 00:58:12 +00:00
|
|
|
Name: "s1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Port: 8300,
|
|
|
|
Addr: &net.IPAddr{IP: ipv4b},
|
|
|
|
},
|
2016-03-28 19:53:19 +00:00
|
|
|
equal: true,
|
2016-03-27 00:58:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
if test.sd1.Key().Equal(test.sd2.Key()) != test.equal {
|
|
|
|
t.Errorf("Expected a %v result from test %s", test.equal, test.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Key to make sure it actually works as a key
|
2017-07-06 10:48:37 +00:00
|
|
|
m := make(map[metadata.Key]bool)
|
2016-03-27 00:58:12 +00:00
|
|
|
m[*test.sd1.Key()] = true
|
|
|
|
if _, found := m[*test.sd2.Key()]; found != test.equal {
|
|
|
|
t.Errorf("Expected a %v result from map test %s", test.equal, test.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 01:26:45 +00:00
|
|
|
func TestIsConsulServer(t *testing.T) {
|
|
|
|
m := serf.Member{
|
|
|
|
Name: "foo",
|
|
|
|
Addr: net.IP([]byte{127, 0, 0, 1}),
|
|
|
|
Tags: map[string]string{
|
2017-03-15 19:26:54 +00:00
|
|
|
"role": "consul",
|
|
|
|
"id": "asdf",
|
|
|
|
"dc": "east-aws",
|
|
|
|
"port": "10000",
|
2017-03-21 23:36:44 +00:00
|
|
|
"build": "0.8.0",
|
2017-03-15 19:26:54 +00:00
|
|
|
"wan_join_port": "1234",
|
|
|
|
"vsn": "1",
|
|
|
|
"expect": "3",
|
|
|
|
"raft_vsn": "3",
|
2017-05-10 21:25:48 +00:00
|
|
|
"use_tls": "1",
|
2020-11-17 15:53:57 +00:00
|
|
|
"read_replica": "1",
|
2016-02-20 01:26:45 +00:00
|
|
|
},
|
2017-03-18 01:42:28 +00:00
|
|
|
Status: serf.StatusLeft,
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, parts := metadata.IsConsulServer(m)
|
2016-03-25 19:11:35 +00:00
|
|
|
if !ok || parts.Datacenter != "east-aws" || parts.Port != 10000 {
|
|
|
|
t.Fatalf("bad: %v %v", ok, parts)
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
|
|
|
if parts.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
2017-02-22 20:53:32 +00:00
|
|
|
if parts.ID != "asdf" {
|
|
|
|
t.Fatalf("bad: %v", parts.ID)
|
|
|
|
}
|
2016-02-20 01:26:45 +00:00
|
|
|
if parts.Bootstrap {
|
|
|
|
t.Fatalf("unexpected bootstrap")
|
|
|
|
}
|
2017-03-14 17:43:43 +00:00
|
|
|
if parts.Expect != 3 {
|
2016-02-20 01:26:45 +00:00
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
2017-03-15 19:26:54 +00:00
|
|
|
if parts.Port != 10000 {
|
|
|
|
t.Fatalf("bad: %v", parts.Port)
|
|
|
|
}
|
|
|
|
if parts.WanJoinPort != 1234 {
|
|
|
|
t.Fatalf("bad: %v", parts.WanJoinPort)
|
|
|
|
}
|
2017-02-22 20:53:32 +00:00
|
|
|
if parts.RaftVersion != 3 {
|
|
|
|
t.Fatalf("bad: %v", parts.RaftVersion)
|
|
|
|
}
|
2017-03-18 01:42:28 +00:00
|
|
|
if parts.Status != serf.StatusLeft {
|
|
|
|
t.Fatalf("bad: %v", parts.Status)
|
|
|
|
}
|
2017-05-10 21:25:48 +00:00
|
|
|
if !parts.UseTLS {
|
|
|
|
t.Fatalf("bad: %v", parts.UseTLS)
|
|
|
|
}
|
2020-11-17 15:53:57 +00:00
|
|
|
if !parts.ReadReplica {
|
2018-09-20 00:41:36 +00:00
|
|
|
t.Fatalf("unexpected voter")
|
|
|
|
}
|
2016-02-20 01:26:45 +00:00
|
|
|
m.Tags["bootstrap"] = "1"
|
|
|
|
m.Tags["disabled"] = "1"
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, parts = metadata.IsConsulServer(m)
|
2016-03-25 19:11:35 +00:00
|
|
|
if !ok {
|
2016-02-20 01:26:45 +00:00
|
|
|
t.Fatalf("expected a valid consul server")
|
|
|
|
}
|
|
|
|
if !parts.Bootstrap {
|
|
|
|
t.Fatalf("expected bootstrap")
|
|
|
|
}
|
|
|
|
if parts.Addr.String() != "127.0.0.1:10000" {
|
|
|
|
t.Fatalf("bad addr: %v", parts.Addr)
|
|
|
|
}
|
|
|
|
if parts.Version != 1 {
|
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
|
|
|
m.Tags["expect"] = "3"
|
|
|
|
delete(m.Tags, "bootstrap")
|
|
|
|
delete(m.Tags, "disabled")
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, parts = metadata.IsConsulServer(m)
|
2016-03-25 19:11:35 +00:00
|
|
|
if !ok || parts.Expect != 3 {
|
2016-02-20 01:26:45 +00:00
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
2016-03-25 19:20:12 +00:00
|
|
|
if parts.Bootstrap {
|
|
|
|
t.Fatalf("unexpected bootstrap")
|
|
|
|
}
|
2016-03-25 19:22:33 +00:00
|
|
|
|
2020-11-17 15:53:57 +00:00
|
|
|
delete(m.Tags, "read_replica")
|
2018-09-20 00:41:36 +00:00
|
|
|
ok, parts = metadata.IsConsulServer(m)
|
2020-11-17 15:53:57 +00:00
|
|
|
if !ok || parts.ReadReplica {
|
|
|
|
t.Fatalf("unexpected read replica")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Tags["nonvoter"] = "1"
|
|
|
|
ok, parts = metadata.IsConsulServer(m)
|
|
|
|
if !ok || !parts.ReadReplica {
|
|
|
|
t.Fatalf("expected read replica")
|
2018-09-20 00:41:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 21:31:48 +00:00
|
|
|
delete(m.Tags, "role")
|
2020-06-05 19:28:03 +00:00
|
|
|
ok, _ = metadata.IsConsulServer(m)
|
|
|
|
require.False(t, ok, "expected to not be a consul server")
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
2017-03-14 17:43:43 +00:00
|
|
|
|
|
|
|
func TestIsConsulServer_Optional(t *testing.T) {
|
|
|
|
m := serf.Member{
|
|
|
|
Name: "foo",
|
|
|
|
Addr: net.IP([]byte{127, 0, 0, 1}),
|
|
|
|
Tags: map[string]string{
|
2017-03-21 23:36:44 +00:00
|
|
|
"role": "consul",
|
|
|
|
"id": "asdf",
|
|
|
|
"dc": "east-aws",
|
|
|
|
"port": "10000",
|
|
|
|
"vsn": "1",
|
|
|
|
"build": "0.8.0",
|
2017-03-15 19:26:54 +00:00
|
|
|
// wan_join_port, raft_vsn, and expect are optional and
|
|
|
|
// should default to zero.
|
2017-03-14 17:43:43 +00:00
|
|
|
},
|
|
|
|
}
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, parts := metadata.IsConsulServer(m)
|
2017-03-14 17:43:43 +00:00
|
|
|
if !ok || parts.Datacenter != "east-aws" || parts.Port != 10000 {
|
|
|
|
t.Fatalf("bad: %v %v", ok, parts)
|
|
|
|
}
|
|
|
|
if parts.Name != "foo" {
|
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
|
|
|
if parts.ID != "asdf" {
|
|
|
|
t.Fatalf("bad: %v", parts.ID)
|
|
|
|
}
|
|
|
|
if parts.Bootstrap {
|
|
|
|
t.Fatalf("unexpected bootstrap")
|
|
|
|
}
|
|
|
|
if parts.Expect != 0 {
|
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
2017-03-15 19:26:54 +00:00
|
|
|
if parts.Port != 10000 {
|
|
|
|
t.Fatalf("bad: %v", parts.Port)
|
|
|
|
}
|
|
|
|
if parts.WanJoinPort != 0 {
|
|
|
|
t.Fatalf("bad: %v", parts.WanJoinPort)
|
|
|
|
}
|
2017-03-14 17:43:43 +00:00
|
|
|
if parts.RaftVersion != 0 {
|
|
|
|
t.Fatalf("bad: %v", parts.RaftVersion)
|
|
|
|
}
|
|
|
|
m.Tags["bootstrap"] = "1"
|
|
|
|
m.Tags["disabled"] = "1"
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, parts = metadata.IsConsulServer(m)
|
2017-03-14 17:43:43 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected a valid consul server")
|
|
|
|
}
|
|
|
|
if !parts.Bootstrap {
|
|
|
|
t.Fatalf("expected bootstrap")
|
|
|
|
}
|
|
|
|
if parts.Addr.String() != "127.0.0.1:10000" {
|
|
|
|
t.Fatalf("bad addr: %v", parts.Addr)
|
|
|
|
}
|
|
|
|
if parts.Version != 1 {
|
|
|
|
t.Fatalf("bad: %v", parts)
|
|
|
|
}
|
|
|
|
m.Tags["expect"] = "3"
|
|
|
|
delete(m.Tags, "bootstrap")
|
|
|
|
delete(m.Tags, "disabled")
|
2017-07-06 10:48:37 +00:00
|
|
|
ok, parts = metadata.IsConsulServer(m)
|
2017-03-14 17:43:43 +00:00
|
|
|
if !ok || parts.Expect != 3 {
|
|
|
|
t.Fatalf("bad: %v", parts.Expect)
|
|
|
|
}
|
|
|
|
if parts.Bootstrap {
|
|
|
|
t.Fatalf("unexpected bootstrap")
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(m.Tags, "role")
|
2020-06-05 19:28:03 +00:00
|
|
|
ok, _ = metadata.IsConsulServer(m)
|
|
|
|
require.False(t, ok, "expected to not be a consul server")
|
2017-03-14 17:43:43 +00:00
|
|
|
}
|