2013-12-12 19:07:14 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2015-08-15 21:32:38 +00:00
|
|
|
"errors"
|
2015-10-27 21:30:29 +00:00
|
|
|
"fmt"
|
2014-05-27 21:45:36 +00:00
|
|
|
"net"
|
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
|
|
|
|
2020-03-27 16:31:43 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2019-08-16 14:34:18 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
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"
|
2013-12-12 19:07:14 +00:00
|
|
|
)
|
|
|
|
|
2015-08-15 21:32:38 +00:00
|
|
|
func TestGetPrivateIP(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2015-08-15 21:32:38 +00:00
|
|
|
ip, _, err := net.ParseCIDR("10.1.2.3/32")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse private cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pubIP, _, err := net.ParseCIDR("8.8.8.8/32")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse public cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
addrs []net.Addr
|
|
|
|
expected net.IP
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: ip,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: errors.New("No private IP address found"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: errors.New("Multiple private IPs found. Please configure one."),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ip, err := getPrivateIP(test.addrs)
|
|
|
|
switch {
|
|
|
|
case test.err != nil && err != nil:
|
|
|
|
if err.Error() != test.err.Error() {
|
|
|
|
t.Fatalf("unexpected error: %v != %v", test.err, err)
|
|
|
|
}
|
|
|
|
case (test.err == nil && err != nil) || (test.err != nil && err == nil):
|
|
|
|
t.Fatalf("unexpected error: %v != %v", test.err, err)
|
|
|
|
default:
|
|
|
|
if !test.expected.Equal(ip) {
|
|
|
|
t.Fatalf("unexpected ip: %v != %v", ip, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
func TestIsPrivateIP(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2013-12-31 23:44:17 +00:00
|
|
|
if !isPrivateIP("192.168.1.1") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !isPrivateIP("172.16.45.100") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if !isPrivateIP("10.1.2.3") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-06-19 19:20:30 +00:00
|
|
|
if !isPrivateIP("100.115.110.19") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2013-12-31 23:44:17 +00:00
|
|
|
if isPrivateIP("8.8.8.8") {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-02 10:24:14 +00:00
|
|
|
if !isPrivateIP("127.0.0.1") {
|
2013-12-31 23:44:17 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 01:22:01 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestByteConversion(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2014-01-10 01:22:01 +00:00
|
|
|
var val uint64 = 2 << 50
|
|
|
|
raw := uint64ToBytes(val)
|
|
|
|
if bytesToUint64(raw) != val {
|
|
|
|
t.Fatalf("no match")
|
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
id := generateUUID()
|
|
|
|
if prev == id {
|
|
|
|
t.Fatalf("Should get a new ID!")
|
|
|
|
}
|
|
|
|
|
|
|
|
matched, err := regexp.MatchString(
|
|
|
|
"[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", id)
|
|
|
|
if !matched || err != nil {
|
|
|
|
t.Fatalf("expected match %s %v %s", id, matched, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-05 15:53:41 +00:00
|
|
|
|
|
|
|
func TestGetPublicIPv6(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2015-09-05 15:53:41 +00:00
|
|
|
ip, _, err := net.ParseCIDR("fe80::1/128")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse link-local cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ip2, _, err := net.ParseCIDR("::1/128")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse loopback cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-19 23:14:45 +00:00
|
|
|
ip3, _, err := net.ParseCIDR("fc00::1/128")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse ULA cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-05 15:53:41 +00:00
|
|
|
pubIP, _, err := net.ParseCIDR("2001:0db8:85a3::8a2e:0370:7334/128")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse public cidr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
addrs []net.Addr
|
|
|
|
expected net.IP
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip2,
|
|
|
|
},
|
2016-03-19 23:14:45 +00:00
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip3,
|
|
|
|
},
|
2015-09-05 15:53:41 +00:00
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: pubIP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip2,
|
|
|
|
},
|
2016-03-19 23:14:45 +00:00
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip3,
|
|
|
|
},
|
2015-09-05 15:53:41 +00:00
|
|
|
},
|
|
|
|
err: errors.New("No public IPv6 address found"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
addrs: []net.Addr{
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: ip,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
&net.IPAddr{
|
|
|
|
IP: pubIP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: errors.New("Multiple public IPv6 addresses found. Please configure one."),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
ip, err := getPublicIPv6(test.addrs)
|
|
|
|
switch {
|
|
|
|
case test.err != nil && err != nil:
|
|
|
|
if err.Error() != test.err.Error() {
|
|
|
|
t.Fatalf("unexpected error: %v != %v", test.err, err)
|
|
|
|
}
|
|
|
|
case (test.err == nil && err != nil) || (test.err != nil && err == nil):
|
|
|
|
t.Fatalf("unexpected error: %v != %v", test.err, err)
|
|
|
|
default:
|
|
|
|
if !test.expected.Equal(ip) {
|
|
|
|
t.Fatalf("unexpected ip: %v != %v", ip, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 01:38:36 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
func TestInterpolateHIL(t *testing.T) {
|
2020-05-01 22:35:28 +00:00
|
|
|
for name, test := range map[string]struct {
|
|
|
|
in string
|
|
|
|
vars map[string]string
|
|
|
|
exp string // when lower=false
|
|
|
|
expLower string // when lower=true
|
|
|
|
ok bool
|
2019-04-26 17:49:28 +00:00
|
|
|
}{
|
|
|
|
// valid HIL
|
2020-05-01 22:35:28 +00:00
|
|
|
"empty": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"",
|
|
|
|
map[string]string{},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"no vars": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"nothing",
|
|
|
|
map[string]string{},
|
|
|
|
"nothing",
|
2020-05-01 22:35:28 +00:00
|
|
|
"nothing",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"just lowercase var": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"${item}",
|
|
|
|
map[string]string{"item": "value"},
|
|
|
|
"value",
|
2020-05-01 22:35:28 +00:00
|
|
|
"value",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"just uppercase var": {
|
|
|
|
"${item}",
|
|
|
|
map[string]string{"item": "VaLuE"},
|
|
|
|
"VaLuE",
|
|
|
|
"value",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"lowercase var in middle": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"before ${item}after",
|
|
|
|
map[string]string{"item": "value"},
|
|
|
|
"before valueafter",
|
2020-05-01 22:35:28 +00:00
|
|
|
"before valueafter",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"uppercase var in middle": {
|
|
|
|
"before ${item}after",
|
|
|
|
map[string]string{"item": "VaLuE"},
|
|
|
|
"before VaLuEafter",
|
|
|
|
"before valueafter",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"two vars": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"before ${item}after ${more}",
|
|
|
|
map[string]string{"item": "value", "more": "xyz"},
|
|
|
|
"before valueafter xyz",
|
2020-05-01 22:35:28 +00:00
|
|
|
"before valueafter xyz",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"missing map val": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"${item}",
|
|
|
|
map[string]string{"item": ""},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
// "weird" HIL, but not technically invalid
|
2020-05-01 22:35:28 +00:00
|
|
|
"just end": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"}",
|
|
|
|
map[string]string{},
|
|
|
|
"}",
|
2020-05-01 22:35:28 +00:00
|
|
|
"}",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"var without start": {
|
2019-04-26 17:49:28 +00:00
|
|
|
" item }",
|
|
|
|
map[string]string{"item": "value"},
|
|
|
|
" item }",
|
2020-05-01 22:35:28 +00:00
|
|
|
" item }",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"two vars missing second start": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"before ${ item }after more }",
|
|
|
|
map[string]string{"item": "value", "more": "xyz"},
|
|
|
|
"before valueafter more }",
|
2020-05-01 22:35:28 +00:00
|
|
|
"before valueafter more }",
|
2019-04-26 17:49:28 +00:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
// invalid HIL
|
2020-05-01 22:35:28 +00:00
|
|
|
"just start": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"${",
|
|
|
|
map[string]string{},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
false,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"backwards": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"}${",
|
|
|
|
map[string]string{},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
false,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"no varname": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"${}",
|
|
|
|
map[string]string{},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
false,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"missing map key": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"${item}",
|
|
|
|
map[string]string{},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
false,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"var without end": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"${ item ",
|
|
|
|
map[string]string{"item": "value"},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
false,
|
|
|
|
},
|
2020-05-01 22:35:28 +00:00
|
|
|
"two vars missing first end": {
|
2019-04-26 17:49:28 +00:00
|
|
|
"before ${ item after ${ more }",
|
|
|
|
map[string]string{"item": "value", "more": "xyz"},
|
|
|
|
"",
|
2020-05-01 22:35:28 +00:00
|
|
|
"",
|
2019-04-26 17:49:28 +00:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
} {
|
2020-05-01 22:35:28 +00:00
|
|
|
test := test
|
|
|
|
t.Run(name+" lower=false", func(t *testing.T) {
|
|
|
|
out, err := InterpolateHIL(test.in, test.vars, false)
|
2019-04-26 17:49:28 +00:00
|
|
|
if test.ok {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, test.exp, out)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Equal(t, out, "")
|
|
|
|
}
|
|
|
|
})
|
2020-05-01 22:35:28 +00:00
|
|
|
t.Run(name+" lower=true", func(t *testing.T) {
|
|
|
|
out, err := InterpolateHIL(test.in, test.vars, true)
|
|
|
|
if test.ok {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, test.expLower, out)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Equal(t, out, "")
|
|
|
|
}
|
|
|
|
})
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 14:34:18 +00:00
|
|
|
|
|
|
|
func TestServersGetACLMode(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-03-27 16:31:43 +00:00
|
|
|
makeServer := func(datacenter string, acls structs.ACLMode, status serf.MemberStatus, addr net.IP) metadata.Server {
|
|
|
|
return metadata.Server{
|
|
|
|
Name: "foo",
|
|
|
|
ShortName: "foo",
|
|
|
|
ID: "asdf",
|
|
|
|
Port: 10000,
|
|
|
|
Expect: 3,
|
|
|
|
RaftVersion: 3,
|
|
|
|
Status: status,
|
|
|
|
WanJoinPort: 1234,
|
|
|
|
Version: 1,
|
|
|
|
Addr: &net.TCPAddr{IP: addr, Port: 10000},
|
|
|
|
// shouldn't matter for these tests
|
|
|
|
Build: *version.Must(version.NewVersion("1.7.0")),
|
|
|
|
Datacenter: datacenter,
|
|
|
|
ACLs: acls,
|
2019-08-16 14:34:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tcase struct {
|
2020-03-27 16:31:43 +00:00
|
|
|
servers testServersProvider
|
|
|
|
leaderAddr string
|
|
|
|
datacenter string
|
|
|
|
foundServers bool
|
|
|
|
minMode structs.ACLMode
|
|
|
|
leaderMode structs.ACLMode
|
2019-08-16 14:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]tcase{
|
|
|
|
"filter-members": tcase{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("primary", structs.ACLModeLegacy, serf.StatusAlive, net.IP([]byte{127, 0, 0, 1})),
|
|
|
|
makeServer("primary", structs.ACLModeLegacy, serf.StatusFailed, net.IP([]byte{127, 0, 0, 2})),
|
2019-08-16 14:34:18 +00:00
|
|
|
// filtered datacenter
|
2020-03-27 16:31:43 +00:00
|
|
|
makeServer("secondary", structs.ACLModeUnknown, serf.StatusAlive, net.IP([]byte{127, 0, 0, 4})),
|
2019-08-16 14:34:18 +00:00
|
|
|
// filtered status
|
2020-03-27 16:31:43 +00:00
|
|
|
makeServer("primary", structs.ACLModeUnknown, serf.StatusLeaving, net.IP([]byte{127, 0, 0, 5})),
|
2019-08-16 14:34:18 +00:00
|
|
|
// filtered status
|
2020-03-27 16:31:43 +00:00
|
|
|
makeServer("primary", structs.ACLModeUnknown, serf.StatusLeft, net.IP([]byte{127, 0, 0, 6})),
|
2019-08-16 14:34:18 +00:00
|
|
|
// filtered status
|
2020-03-27 16:31:43 +00:00
|
|
|
makeServer("primary", structs.ACLModeUnknown, serf.StatusNone, net.IP([]byte{127, 0, 0, 7})),
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
2020-03-27 16:31:43 +00:00
|
|
|
foundServers: true,
|
|
|
|
leaderAddr: "127.0.0.1:10000",
|
|
|
|
datacenter: "primary",
|
|
|
|
minMode: structs.ACLModeLegacy,
|
|
|
|
leaderMode: structs.ACLModeLegacy,
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
|
|
|
"disabled": tcase{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("primary", structs.ACLModeLegacy, serf.StatusAlive, net.IP([]byte{127, 0, 0, 1})),
|
|
|
|
makeServer("primary", structs.ACLModeUnknown, serf.StatusAlive, net.IP([]byte{127, 0, 0, 2})),
|
|
|
|
makeServer("primary", structs.ACLModeDisabled, serf.StatusAlive, net.IP([]byte{127, 0, 0, 3})),
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
2020-03-27 16:31:43 +00:00
|
|
|
foundServers: true,
|
|
|
|
leaderAddr: "127.0.0.1:10000",
|
|
|
|
datacenter: "primary",
|
|
|
|
minMode: structs.ACLModeDisabled,
|
|
|
|
leaderMode: structs.ACLModeLegacy,
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
|
|
|
"unknown": tcase{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("primary", structs.ACLModeLegacy, serf.StatusAlive, net.IP([]byte{127, 0, 0, 1})),
|
|
|
|
makeServer("primary", structs.ACLModeUnknown, serf.StatusAlive, net.IP([]byte{127, 0, 0, 2})),
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
2020-03-27 16:31:43 +00:00
|
|
|
foundServers: true,
|
|
|
|
leaderAddr: "127.0.0.1:10000",
|
|
|
|
datacenter: "primary",
|
|
|
|
minMode: structs.ACLModeUnknown,
|
|
|
|
leaderMode: structs.ACLModeLegacy,
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
|
|
|
"legacy": tcase{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("primary", structs.ACLModeEnabled, serf.StatusAlive, net.IP([]byte{127, 0, 0, 1})),
|
|
|
|
makeServer("primary", structs.ACLModeLegacy, serf.StatusAlive, net.IP([]byte{127, 0, 0, 2})),
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
2020-03-27 16:31:43 +00:00
|
|
|
foundServers: true,
|
|
|
|
leaderAddr: "127.0.0.1:10000",
|
|
|
|
datacenter: "primary",
|
|
|
|
minMode: structs.ACLModeLegacy,
|
|
|
|
leaderMode: structs.ACLModeEnabled,
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
|
|
|
"enabled": tcase{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("primary", structs.ACLModeEnabled, serf.StatusAlive, net.IP([]byte{127, 0, 0, 1})),
|
|
|
|
makeServer("primary", structs.ACLModeEnabled, serf.StatusAlive, net.IP([]byte{127, 0, 0, 2})),
|
|
|
|
makeServer("primary", structs.ACLModeEnabled, serf.StatusAlive, net.IP([]byte{127, 0, 0, 3})),
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
2020-03-27 16:31:43 +00:00
|
|
|
foundServers: true,
|
|
|
|
leaderAddr: "127.0.0.1:10000",
|
|
|
|
datacenter: "primary",
|
|
|
|
minMode: structs.ACLModeEnabled,
|
|
|
|
leaderMode: structs.ACLModeEnabled,
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
|
|
|
"failed-members": tcase{
|
2020-03-27 16:31:43 +00:00
|
|
|
servers: testServersProvider{
|
|
|
|
makeServer("primary", structs.ACLModeLegacy, serf.StatusAlive, net.IP([]byte{127, 0, 0, 1})),
|
|
|
|
makeServer("primary", structs.ACLModeUnknown, serf.StatusFailed, net.IP([]byte{127, 0, 0, 2})),
|
|
|
|
makeServer("primary", structs.ACLModeLegacy, serf.StatusFailed, net.IP([]byte{127, 0, 0, 3})),
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
2020-03-27 16:31:43 +00:00
|
|
|
foundServers: true,
|
|
|
|
leaderAddr: "127.0.0.1:10000",
|
|
|
|
datacenter: "primary",
|
|
|
|
minMode: structs.ACLModeUnknown,
|
|
|
|
leaderMode: structs.ACLModeLegacy,
|
2019-08-16 14:34:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
name := name
|
|
|
|
tc := tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2020-03-27 16:31:43 +00:00
|
|
|
actualServers, actualMinMode, actualLeaderMode := ServersGetACLMode(tc.servers, tc.leaderAddr, tc.datacenter)
|
2019-08-16 14:34:18 +00:00
|
|
|
|
|
|
|
require.Equal(t, tc.minMode, actualMinMode)
|
|
|
|
require.Equal(t, tc.leaderMode, actualLeaderMode)
|
2020-03-27 16:31:43 +00:00
|
|
|
require.Equal(t, tc.foundServers, actualServers)
|
2019-08-16 14:34:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|