2013-12-19 22:54:32 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2016-10-26 02:20:24 +00:00
|
|
|
"bytes"
|
2014-01-01 00:46:56 +00:00
|
|
|
"net"
|
2013-12-19 22:54:32 +00:00
|
|
|
"os"
|
2015-08-13 01:48:15 +00:00
|
|
|
"sync"
|
2013-12-19 22:54:32 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-08-27 02:26:55 +00:00
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2017-05-05 11:58:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2016-03-24 05:35:49 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2014-08-27 02:26:55 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2013-12-19 22:54:32 +00:00
|
|
|
)
|
|
|
|
|
2017-06-26 12:23:09 +00:00
|
|
|
func testClientConfig(t *testing.T) (string, *Config) {
|
2017-05-12 13:41:13 +00:00
|
|
|
dir := testutil.TempDir(t, "consul")
|
2013-12-19 22:54:32 +00:00
|
|
|
config := DefaultConfig()
|
2014-04-07 21:36:32 +00:00
|
|
|
config.Datacenter = "dc1"
|
2013-12-19 22:54:32 +00:00
|
|
|
config.DataDir = dir
|
2017-06-26 12:23:09 +00:00
|
|
|
config.NodeName = uniqueNodeName(t.Name())
|
2014-01-01 00:46:56 +00:00
|
|
|
config.RPCAddr = &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
2014-05-26 22:47:47 +00:00
|
|
|
Port: getPort(),
|
2014-01-01 00:46:56 +00:00
|
|
|
}
|
2013-12-19 22:54:32 +00:00
|
|
|
config.SerfLANConfig.MemberlistConfig.BindAddr = "127.0.0.1"
|
2013-12-30 20:20:17 +00:00
|
|
|
config.SerfLANConfig.MemberlistConfig.BindPort = getPort()
|
2013-12-19 22:54:32 +00:00
|
|
|
config.SerfLANConfig.MemberlistConfig.ProbeTimeout = 200 * time.Millisecond
|
|
|
|
config.SerfLANConfig.MemberlistConfig.ProbeInterval = time.Second
|
|
|
|
config.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond
|
|
|
|
|
2014-04-07 21:36:32 +00:00
|
|
|
return dir, config
|
|
|
|
}
|
|
|
|
|
|
|
|
func testClient(t *testing.T) (string, *Client) {
|
2017-06-26 12:23:09 +00:00
|
|
|
return testClientWithConfig(t, func(c *Config) {
|
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.NodeName = uniqueNodeName(t.Name())
|
|
|
|
})
|
2014-04-07 21:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testClientDC(t *testing.T, dc string) (string, *Client) {
|
2017-06-26 12:23:09 +00:00
|
|
|
return testClientWithConfig(t, func(c *Config) {
|
|
|
|
c.Datacenter = dc
|
|
|
|
c.NodeName = uniqueNodeName(t.Name())
|
|
|
|
})
|
2013-12-19 22:54:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-27 02:26:55 +00:00
|
|
|
func testClientWithConfig(t *testing.T, cb func(c *Config)) (string, *Client) {
|
2017-06-26 12:23:09 +00:00
|
|
|
dir, config := testClientConfig(t)
|
|
|
|
if cb != nil {
|
|
|
|
cb(config)
|
|
|
|
}
|
2014-08-27 02:26:55 +00:00
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return dir, client
|
|
|
|
}
|
|
|
|
|
2013-12-19 22:54:32 +00:00
|
|
|
func TestClient_StartStop(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2013-12-19 22:54:32 +00:00
|
|
|
dir, client := testClient(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
if err := client.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2013-12-19 22:56:38 +00:00
|
|
|
|
|
|
|
func TestClient_JoinLAN(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2013-12-19 22:56:38 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c1, s1)
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if got, want := c1.servers.NumServers(), 1; got != want {
|
2017-05-09 05:31:41 +00:00
|
|
|
r.Fatalf("got %d servers want %d", got, want)
|
2017-05-05 11:58:13 +00:00
|
|
|
}
|
|
|
|
if got, want := len(s1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d server LAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
if got, want := len(c1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d client LAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2013-12-19 22:56:38 +00:00
|
|
|
}
|
2013-12-19 23:08:55 +00:00
|
|
|
|
2015-01-06 23:48:46 +00:00
|
|
|
func TestClient_JoinLAN_Invalid(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:48:46 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClientDC(t, "other")
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
2017-05-05 10:29:49 +00:00
|
|
|
if _, err := c1.JoinLAN([]string{joinAddrLAN(s1)}); err == nil {
|
|
|
|
t.Fatal("should error")
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
if len(s1.LANMembers()) != 1 {
|
|
|
|
t.Fatalf("should not join")
|
|
|
|
}
|
|
|
|
if len(c1.LANMembers()) != 1 {
|
|
|
|
t.Fatalf("should not join")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_JoinWAN_Invalid(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:48:46 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClientDC(t, "dc2")
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
2017-05-05 10:29:49 +00:00
|
|
|
if _, err := c1.JoinLAN([]string{joinAddrWAN(s1)}); err == nil {
|
|
|
|
t.Fatal("should error")
|
2015-01-06 23:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
if len(s1.WANMembers()) != 1 {
|
|
|
|
t.Fatalf("should not join")
|
|
|
|
}
|
|
|
|
if len(c1.LANMembers()) != 1 {
|
|
|
|
t.Fatalf("should not join")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 23:08:55 +00:00
|
|
|
func TestClient_RPC(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2013-12-19 23:08:55 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try an RPC
|
|
|
|
var out struct{}
|
|
|
|
if err := c1.RPC("Status.Ping", struct{}{}, &out); err != structs.ErrNoServers {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to join
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c1, s1)
|
2013-12-19 23:08:55 +00:00
|
|
|
|
|
|
|
// Check the members
|
|
|
|
if len(s1.LANMembers()) != 2 {
|
|
|
|
t.Fatalf("bad len")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c1.LANMembers()) != 2 {
|
|
|
|
t.Fatalf("bad len")
|
|
|
|
}
|
|
|
|
|
2014-05-09 00:29:51 +00:00
|
|
|
// RPC should succeed
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if err := c1.RPC("Status.Ping", struct{}{}, &out); err != nil {
|
|
|
|
r.Fatal("ping failed", err)
|
|
|
|
}
|
|
|
|
})
|
2013-12-19 23:08:55 +00:00
|
|
|
}
|
2014-04-07 21:36:32 +00:00
|
|
|
|
2015-08-13 01:48:15 +00:00
|
|
|
func TestClient_RPC_Pool(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2015-08-13 01:48:15 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join.
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c1, s1)
|
2016-10-25 21:58:45 +00:00
|
|
|
|
|
|
|
// Wait for both agents to finish joining
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if got, want := len(s1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d server LAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
if got, want := len(c1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d client LAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2015-08-13 01:48:15 +00:00
|
|
|
|
|
|
|
// Blast out a bunch of RPC requests at the same time to try to get
|
|
|
|
// contention opening new connections.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 150; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
var out struct{}
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if err := c1.RPC("Status.Ping", struct{}{}, &out); err != nil {
|
|
|
|
r.Fatal("ping failed", err)
|
|
|
|
}
|
|
|
|
})
|
2015-08-13 01:48:15 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-03-27 06:52:06 +00:00
|
|
|
func TestClient_RPC_ConsulServerPing(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2016-03-27 06:52:06 +00:00
|
|
|
var servers []*Server
|
|
|
|
var serverDirs []string
|
|
|
|
const numServers = 5
|
|
|
|
|
2017-07-05 10:37:19 +00:00
|
|
|
for n := 0; n < numServers; n++ {
|
|
|
|
bootstrap := n == 0
|
2016-03-27 06:52:06 +00:00
|
|
|
dir, s := testServerDCBootstrap(t, "dc1", bootstrap)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
servers = append(servers, s)
|
|
|
|
serverDirs = append(serverDirs, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
const numClients = 1
|
|
|
|
clientDir, c := testClient(t)
|
|
|
|
defer os.RemoveAll(clientDir)
|
|
|
|
defer c.Shutdown()
|
|
|
|
|
|
|
|
// Join all servers.
|
|
|
|
for _, s := range servers {
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c, s)
|
2016-03-27 06:52:06 +00:00
|
|
|
}
|
|
|
|
|
2017-07-05 10:37:19 +00:00
|
|
|
for _, s := range servers {
|
|
|
|
retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, numServers)) })
|
|
|
|
}
|
|
|
|
|
2016-03-27 06:52:06 +00:00
|
|
|
// Sleep to allow Serf to sync, shuffle, and let the shuffle complete
|
2016-03-29 22:58:15 +00:00
|
|
|
c.servers.ResetRebalanceTimer()
|
2017-07-05 10:37:19 +00:00
|
|
|
time.Sleep(time.Second)
|
2016-03-27 06:52:06 +00:00
|
|
|
|
|
|
|
if len(c.LANMembers()) != numServers+numClients {
|
|
|
|
t.Errorf("bad len: %d", len(c.LANMembers()))
|
|
|
|
}
|
|
|
|
for _, s := range servers {
|
|
|
|
if len(s.LANMembers()) != numServers+numClients {
|
|
|
|
t.Errorf("bad len: %d", len(s.LANMembers()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping each server in the list
|
|
|
|
var pingCount int
|
|
|
|
for range servers {
|
2017-07-05 10:37:19 +00:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2016-03-29 22:58:15 +00:00
|
|
|
s := c.servers.FindServer()
|
2017-06-15 13:16:16 +00:00
|
|
|
ok, err := c.connPool.Ping(s.Datacenter, s.Addr, s.Version, s.UseTLS)
|
2016-03-28 20:38:58 +00:00
|
|
|
if !ok {
|
|
|
|
t.Errorf("Unable to ping server %v: %s", s.String(), err)
|
2016-03-27 06:52:06 +00:00
|
|
|
}
|
2017-04-20 19:00:03 +00:00
|
|
|
pingCount++
|
2016-03-28 21:12:41 +00:00
|
|
|
|
|
|
|
// Artificially fail the server in order to rotate the server
|
|
|
|
// list
|
2016-03-29 22:58:15 +00:00
|
|
|
c.servers.NotifyFailedServer(s)
|
2016-03-27 06:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pingCount != numServers {
|
|
|
|
t.Errorf("bad len: %d/%d", pingCount, numServers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-07 21:36:32 +00:00
|
|
|
func TestClient_RPC_TLS(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-06-26 12:23:09 +00:00
|
|
|
dir1, conf1 := testServerConfig(t)
|
2014-04-07 21:36:32 +00:00
|
|
|
conf1.VerifyIncoming = true
|
|
|
|
conf1.VerifyOutgoing = true
|
|
|
|
configureTLS(conf1)
|
|
|
|
s1, err := NewServer(conf1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-06-26 12:23:09 +00:00
|
|
|
dir2, conf2 := testClientConfig(t)
|
2014-04-07 21:36:32 +00:00
|
|
|
conf2.VerifyOutgoing = true
|
|
|
|
configureTLS(conf2)
|
|
|
|
c1, err := NewClient(conf2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Try an RPC
|
|
|
|
var out struct{}
|
|
|
|
if err := c1.RPC("Status.Ping", struct{}{}, &out); err != structs.ErrNoServers {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to join
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c1, s1)
|
2014-04-07 21:36:32 +00:00
|
|
|
|
2016-10-26 00:28:45 +00:00
|
|
|
// Wait for joins to finish/RPC to succeed
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if got, want := len(s1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d server LAN members want %d", got, want)
|
2016-10-26 00:28:45 +00:00
|
|
|
}
|
2017-05-05 11:58:13 +00:00
|
|
|
if got, want := len(c1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d client LAN members want %d", got, want)
|
2016-10-26 00:28:45 +00:00
|
|
|
}
|
2017-05-05 11:58:13 +00:00
|
|
|
if err := c1.RPC("Status.Ping", struct{}{}, &out); err != nil {
|
|
|
|
r.Fatal("ping failed", err)
|
|
|
|
}
|
|
|
|
})
|
2014-04-07 21:36:32 +00:00
|
|
|
}
|
2014-08-27 02:26:55 +00:00
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
func TestClient_SnapshotRPC(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2016-10-26 02:20:24 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
dir2, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Wait for the leader
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-10-26 02:20:24 +00:00
|
|
|
|
|
|
|
// Try to join.
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c1, s1)
|
2016-10-26 02:20:24 +00:00
|
|
|
if len(s1.LANMembers()) != 2 || len(c1.LANMembers()) != 2 {
|
|
|
|
t.Fatalf("Server has %v of %v expected members; Client has %v of %v expected members.", len(s1.LANMembers()), 2, len(c1.LANMembers()), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until we've got a healthy server.
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if got, want := c1.servers.NumServers(), 1; got != want {
|
2017-05-09 05:31:41 +00:00
|
|
|
r.Fatalf("got %d servers want %d", got, want)
|
2017-05-05 11:58:13 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-26 02:20:24 +00:00
|
|
|
|
|
|
|
// Take a snapshot.
|
|
|
|
var snap bytes.Buffer
|
|
|
|
args := structs.SnapshotRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SnapshotSave,
|
|
|
|
}
|
|
|
|
if err := c1.SnapshotRPC(&args, bytes.NewReader([]byte("")), &snap, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore a snapshot.
|
|
|
|
args.Op = structs.SnapshotRestore
|
|
|
|
if err := c1.SnapshotRPC(&args, &snap, nil, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_SnapshotRPC_TLS(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2017-06-26 12:23:09 +00:00
|
|
|
dir1, conf1 := testServerConfig(t)
|
2016-10-26 02:20:24 +00:00
|
|
|
conf1.VerifyIncoming = true
|
|
|
|
conf1.VerifyOutgoing = true
|
|
|
|
configureTLS(conf1)
|
|
|
|
s1, err := NewServer(conf1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2017-06-26 12:23:09 +00:00
|
|
|
dir2, conf2 := testClientConfig(t)
|
2016-10-26 02:20:24 +00:00
|
|
|
conf2.VerifyOutgoing = true
|
|
|
|
configureTLS(conf2)
|
|
|
|
c1, err := NewClient(conf2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// Wait for the leader
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2016-10-26 02:20:24 +00:00
|
|
|
|
|
|
|
// Try to join.
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c1, s1)
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-07-04 16:35:48 +00:00
|
|
|
if got, want := len(s1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d server members want %d", got, want)
|
|
|
|
}
|
|
|
|
if got, want := len(c1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d client members want %d", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until we've got a healthy server.
|
2017-05-05 11:58:13 +00:00
|
|
|
if got, want := c1.servers.NumServers(), 1; got != want {
|
2017-05-09 05:31:41 +00:00
|
|
|
r.Fatalf("got %d servers want %d", got, want)
|
2017-05-05 11:58:13 +00:00
|
|
|
}
|
|
|
|
})
|
2016-10-26 02:20:24 +00:00
|
|
|
|
|
|
|
// Take a snapshot.
|
|
|
|
var snap bytes.Buffer
|
|
|
|
args := structs.SnapshotRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SnapshotSave,
|
|
|
|
}
|
|
|
|
if err := c1.SnapshotRPC(&args, bytes.NewReader([]byte("")), &snap, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore a snapshot.
|
|
|
|
args.Op = structs.SnapshotRestore
|
|
|
|
if err := c1.SnapshotRPC(&args, &snap, nil, nil); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 02:26:55 +00:00
|
|
|
func TestClientServer_UserEvent(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-27 02:26:55 +00:00
|
|
|
clientOut := make(chan serf.UserEvent, 2)
|
|
|
|
dir1, c1 := testClientWithConfig(t, func(conf *Config) {
|
|
|
|
conf.UserEventHandler = func(e serf.UserEvent) {
|
|
|
|
clientOut <- e
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
serverOut := make(chan serf.UserEvent, 2)
|
|
|
|
dir2, s1 := testServerWithConfig(t, func(conf *Config) {
|
|
|
|
conf.UserEventHandler = func(e serf.UserEvent) {
|
|
|
|
serverOut <- e
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
2017-05-05 10:29:49 +00:00
|
|
|
joinLAN(t, c1, s1)
|
2014-08-27 02:26:55 +00:00
|
|
|
|
2015-07-14 18:38:12 +00:00
|
|
|
// Wait for the leader
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
2015-07-14 18:38:12 +00:00
|
|
|
|
2014-08-27 02:26:55 +00:00
|
|
|
// Check the members
|
2017-05-05 11:58:13 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
if got, want := len(s1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d server LAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
if got, want := len(c1.LANMembers()), 2; got != want {
|
|
|
|
r.Fatalf("got %d client LAN members want %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2014-08-27 02:26:55 +00:00
|
|
|
|
|
|
|
// Fire the user event
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
2015-07-14 18:38:12 +00:00
|
|
|
event := structs.EventFireRequest{
|
|
|
|
Name: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Payload: []byte("baz"),
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Internal.EventFire", &event, nil); err != nil {
|
2014-08-27 02:26:55 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the events
|
2015-06-18 23:20:24 +00:00
|
|
|
var clientReceived, serverReceived bool
|
|
|
|
for i := 0; i < 2; i++ {
|
2014-08-27 02:26:55 +00:00
|
|
|
select {
|
|
|
|
case e := <-clientOut:
|
|
|
|
switch e.Name {
|
|
|
|
case "foo":
|
2015-06-18 23:20:24 +00:00
|
|
|
clientReceived = true
|
2014-08-27 02:26:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("Bad: %#v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
case e := <-serverOut:
|
|
|
|
switch e.Name {
|
|
|
|
case "foo":
|
2015-06-18 23:20:24 +00:00
|
|
|
serverReceived = true
|
2014-08-27 02:26:55 +00:00
|
|
|
default:
|
|
|
|
t.Fatalf("Bad: %#v", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 23:20:24 +00:00
|
|
|
if !serverReceived || !clientReceived {
|
2014-08-27 02:26:55 +00:00
|
|
|
t.Fatalf("missing events")
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 18:05:31 +00:00
|
|
|
|
|
|
|
func TestClient_Encrypted(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-10-07 18:05:31 +00:00
|
|
|
dir1, c1 := testClient(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
key := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
|
|
|
dir2, c2 := testClientWithConfig(t, func(c *Config) {
|
|
|
|
c.SerfLANConfig.MemberlistConfig.SecretKey = key
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer c2.Shutdown()
|
|
|
|
|
|
|
|
if c1.Encrypted() {
|
|
|
|
t.Fatalf("should not be encrypted")
|
|
|
|
}
|
|
|
|
if !c2.Encrypted() {
|
|
|
|
t.Fatalf("should be encrypted")
|
|
|
|
}
|
|
|
|
}
|