2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2013-12-12 19:07:14 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-10-27 21:30:29 +00:00
|
|
|
"fmt"
|
2014-05-08 23:31:03 +00:00
|
|
|
"regexp"
|
2013-12-12 19:07:14 +00:00
|
|
|
"testing"
|
2014-06-16 21:36:12 +00:00
|
|
|
|
2017-04-13 01:38:36 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2014-06-16 21:36:12 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2019-04-26 17:49:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-22 23:34:14 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2013-12-12 19:07:14 +00:00
|
|
|
)
|
|
|
|
|
2015-10-27 21:30:29 +00:00
|
|
|
func TestUtil_CanServersUnderstandProtocol(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2015-10-27 21:30:29 +00:00
|
|
|
var members []serf.Member
|
|
|
|
|
|
|
|
// All empty list cases should return false.
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if grok {
|
|
|
|
t.Fatalf("empty list should always return false")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a non-server member.
|
|
|
|
members = append(members, serf.Member{
|
|
|
|
Tags: map[string]string{
|
2015-10-27 22:56:36 +00:00
|
|
|
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
|
2015-10-27 21:30:29 +00:00
|
|
|
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make sure it doesn't get counted.
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if grok {
|
|
|
|
t.Fatalf("non-server members should not be counted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a server member.
|
|
|
|
members = append(members, serf.Member{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"role": "consul",
|
2015-10-27 22:56:36 +00:00
|
|
|
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
|
2015-10-27 21:30:29 +00:00
|
|
|
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Now it should report that it understands.
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !grok {
|
|
|
|
t.Fatalf("server should grok")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody should understand anything from the future.
|
|
|
|
for v := uint8(ProtocolVersionMax + 1); v <= uint8(ProtocolVersionMax+10); v++ {
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if grok {
|
|
|
|
t.Fatalf("server should not grok")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an older server.
|
|
|
|
members = append(members, serf.Member{
|
|
|
|
Tags: map[string]string{
|
|
|
|
"role": "consul",
|
2015-10-27 22:56:36 +00:00
|
|
|
"vsn_min": fmt.Sprintf("%d", ProtocolVersionMin),
|
2015-10-27 21:30:29 +00:00
|
|
|
"vsn_max": fmt.Sprintf("%d", ProtocolVersionMax-1),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// The servers should no longer understand the max version.
|
|
|
|
for v := ProtocolVersionMin; v <= ProtocolVersionMax; v++ {
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, v)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
expected := v < ProtocolVersionMax
|
|
|
|
if grok != expected {
|
|
|
|
t.Fatalf("bad: %v != %v", grok, expected)
|
|
|
|
}
|
|
|
|
}
|
2015-10-27 22:56:36 +00:00
|
|
|
|
|
|
|
// Try a version that's too low for the minimum.
|
|
|
|
{
|
|
|
|
grok, err := CanServersUnderstandProtocol(members, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if grok {
|
|
|
|
t.Fatalf("server should not grok")
|
|
|
|
}
|
|
|
|
}
|
2015-10-27 21:30:29 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 01:22:01 +00:00
|
|
|
func TestIsConsulNode(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2014-01-10 01:22:01 +00:00
|
|
|
m := serf.Member{
|
2014-01-30 21:13:29 +00:00
|
|
|
Tags: map[string]string{
|
|
|
|
"role": "node",
|
|
|
|
"dc": "east-aws",
|
|
|
|
},
|
2014-01-10 01:22:01 +00:00
|
|
|
}
|
|
|
|
valid, dc := isConsulNode(m)
|
|
|
|
if !valid || dc != "east-aws" {
|
2014-11-01 21:56:48 +00:00
|
|
|
t.Fatalf("bad: %v %v", valid, dc)
|
2014-01-10 01:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:31:03 +00:00
|
|
|
func TestGenerateUUID(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2014-05-08 23:31:03 +00:00
|
|
|
prev := generateUUID()
|
2020-05-14 21:02:52 +00:00
|
|
|
re, err := regexp.Compile("[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2014-05-08 23:31:03 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
id := generateUUID()
|
|
|
|
if prev == id {
|
|
|
|
t.Fatalf("Should get a new ID!")
|
|
|
|
}
|
|
|
|
|
2020-05-14 21:02:52 +00:00
|
|
|
matched := re.MatchString(id)
|
|
|
|
if !matched {
|
2014-05-08 23:31:03 +00:00
|
|
|
t.Fatalf("expected match %s %v %s", id, matched, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-05 15:53:41 +00:00
|
|
|
|
2020-03-27 16:31:43 +00:00
|
|
|
type testServersProvider []metadata.Server
|
2017-04-13 01:38:36 +00:00
|
|
|
|
2020-03-27 16:31:43 +00:00
|
|
|
func (p testServersProvider) CheckServers(datacenter string, fn func(*metadata.Server) bool) {
|
|
|
|
for _, srv := range p {
|
|
|
|
// filter these out - now I don't have to modify the tests. Originally the dc filtering
|
|
|
|
// happened in the ServersInDCMeetMinimumVersion, now we get a list of servers to check
|
|
|
|
// through the routing infrastructure or server lookup which will map a datacenter to a
|
|
|
|
// list of metadata.Server structs that are all in that datacenter.
|
|
|
|
if srv.Datacenter != datacenter {
|
|
|
|
continue
|
|
|
|
}
|
2017-04-13 01:38:36 +00:00
|
|
|
|
2020-03-27 16:31:43 +00:00
|
|
|
if !fn(&srv) {
|
|
|
|
return
|
2017-04-13 01:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
|
2019-07-26 19:57:57 +00:00
|
|
|
func TestServersInDCMeetMinimumVersion(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-03-27 16:31:43 +00:00
|
|
|
makeServer := func(versionStr string, datacenter string) metadata.Server {
|
|
|
|
return metadata.Server{
|
|
|
|
Name: "foo",
|
|
|
|
ShortName: "foo",
|
|
|
|
ID: "asdf",
|
|
|
|
Port: 10000,
|
|
|
|
Expect: 3,
|
|
|
|
RaftVersion: 3,
|
|
|
|
Status: serf.StatusAlive,
|
|
|
|
WanJoinPort: 1234,
|
|
|
|
Version: 1,
|
|
|
|
Build: *version.Must(version.NewVersion(versionStr)),
|
|
|
|
Datacenter: datacenter,
|
2019-07-26 19:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
2020-03-27 16:31:43 +00:00
|
|
|
servers testServersProvider
|
2019-07-26 19:57:57 +00:00
|
|
|
ver *version.Version
|
|
|
|
expected bool
|
|
|
|
expectedFound bool
|
|
|
|
}{
|
|
|
|
// One server, meets reqs
|
|
|
|
{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("0.7.5", "primary"),
|
|
|
|
makeServer("0.7.3", "secondary"),
|
2019-07-26 19:57:57 +00:00
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.7.5")),
|
|
|
|
expected: true,
|
|
|
|
expectedFound: true,
|
|
|
|
},
|
|
|
|
// One server, doesn't meet reqs
|
|
|
|
{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("0.7.5", "primary"),
|
|
|
|
makeServer("0.8.1", "secondary"),
|
2019-07-26 19:57:57 +00:00
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.8.0")),
|
|
|
|
expected: false,
|
|
|
|
expectedFound: true,
|
|
|
|
},
|
|
|
|
// Multiple servers, meets req version
|
|
|
|
{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("0.7.5", "primary"),
|
|
|
|
makeServer("0.8.0", "primary"),
|
|
|
|
makeServer("0.7.0", "secondary"),
|
2019-07-26 19:57:57 +00:00
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.7.5")),
|
|
|
|
expected: true,
|
|
|
|
expectedFound: true,
|
|
|
|
},
|
|
|
|
// Multiple servers, doesn't meet req version
|
|
|
|
{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("0.7.5", "primary"),
|
|
|
|
makeServer("0.8.0", "primary"),
|
|
|
|
makeServer("0.9.1", "secondary"),
|
2019-07-26 19:57:57 +00:00
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.8.0")),
|
|
|
|
expected: false,
|
|
|
|
expectedFound: true,
|
|
|
|
},
|
|
|
|
{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("0.7.5", "secondary"),
|
|
|
|
makeServer("0.8.0", "secondary"),
|
|
|
|
makeServer("0.9.1", "secondary"),
|
2019-07-26 19:57:57 +00:00
|
|
|
},
|
|
|
|
ver: version.Must(version.NewVersion("0.7.0")),
|
|
|
|
expected: true,
|
|
|
|
expectedFound: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2020-03-27 16:31:43 +00:00
|
|
|
result, found := ServersInDCMeetMinimumVersion(tc.servers, "primary", tc.ver)
|
2019-07-26 19:57:57 +00:00
|
|
|
require.Equal(t, tc.expected, result)
|
|
|
|
require.Equal(t, tc.expectedFound, found)
|
|
|
|
}
|
|
|
|
}
|