2013-12-10 23:16:41 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/rpc"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-10-13 23:43:52 +00:00
|
|
|
|
2017-06-15 13:16:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
2019-07-25 18:26:22 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2019-06-27 20:22:07 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2020-01-28 23:50:41 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2019-07-25 18:26:22 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2013-12-10 23:16:41 +00:00
|
|
|
)
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
func rpcClient(t *testing.T, s *Server) rpc.ClientCodec {
|
2017-06-25 19:36:03 +00:00
|
|
|
addr := s.config.RPCAdvertise
|
2014-01-01 00:46:56 +00:00
|
|
|
conn, err := net.DialTimeout("tcp", addr.String(), time.Second)
|
2013-12-10 23:16:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
|
2013-12-10 23:16:41 +00:00
|
|
|
// Write the Consul RPC byte to set the mode
|
2017-06-15 13:16:16 +00:00
|
|
|
conn.Write([]byte{byte(pool.RPCConsul)})
|
2020-02-07 21:50:24 +00:00
|
|
|
return msgpackrpc.NewCodecFromHandle(true, true, conn, structs.MsgpackHandle)
|
2013-12-10 23:16:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
func insecureRPCClient(s *Server, c tlsutil.Config) (rpc.ClientCodec, error) {
|
|
|
|
addr := s.config.RPCAdvertise
|
2020-03-09 20:59:02 +00:00
|
|
|
configurator, err := tlsutil.NewConfigurator(c, s.logger)
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wrapper := configurator.OutgoingRPCWrapper()
|
|
|
|
if wrapper == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
conn, _, err := pool.DialTimeoutWithRPCTypeDirectly(
|
|
|
|
s.config.Datacenter,
|
|
|
|
s.config.NodeName,
|
|
|
|
addr,
|
|
|
|
nil,
|
|
|
|
time.Second,
|
|
|
|
true,
|
|
|
|
wrapper,
|
|
|
|
pool.RPCTLSInsecure,
|
|
|
|
pool.RPCTLSInsecure,
|
|
|
|
)
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-07 21:50:24 +00:00
|
|
|
return msgpackrpc.NewCodecFromHandle(true, true, conn, structs.MsgpackHandle), nil
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 23:16:41 +00:00
|
|
|
func TestStatusLeader(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2013-12-10 23:16:41 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-10 23:16:41 +00:00
|
|
|
|
|
|
|
arg := struct{}{}
|
|
|
|
var leader string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Status.Leader", arg, &leader); err != nil {
|
2013-12-10 23:16:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leader != "" {
|
|
|
|
t.Fatalf("unexpected leader: %v", leader)
|
|
|
|
}
|
|
|
|
|
2018-11-20 11:27:26 +00:00
|
|
|
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
|
2013-12-10 23:16:41 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Status.Leader", arg, &leader); err != nil {
|
2013-12-10 23:16:41 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leader == "" {
|
|
|
|
t.Fatalf("no leader")
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 19:50:58 +00:00
|
|
|
|
2019-07-25 18:26:22 +00:00
|
|
|
func TestStatusLeader_ForwardDC(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerDC(t, "primary")
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
dir2, s2 := testServerDC(t, "secondary")
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
joinWAN(t, s2, s1)
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "secondary")
|
|
|
|
testrpc.WaitForLeader(t, s2.RPC, "primary")
|
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "secondary",
|
|
|
|
}
|
|
|
|
|
|
|
|
var out string
|
|
|
|
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Status.Leader", &args, &out))
|
|
|
|
require.Equal(t, s2.config.RPCAdvertise.String(), out)
|
|
|
|
}
|
|
|
|
|
2013-12-23 19:50:58 +00:00
|
|
|
func TestStatusPeers(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2013-12-23 19:50:58 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-23 19:50:58 +00:00
|
|
|
|
|
|
|
arg := struct{}{}
|
|
|
|
var peers []string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Status.Peers", arg, &peers); err != nil {
|
2013-12-23 19:50:58 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(peers) != 1 {
|
|
|
|
t.Fatalf("no peers: %v", peers)
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 18:26:22 +00:00
|
|
|
|
|
|
|
func TestStatusPeers_ForwardDC(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir1, s1 := testServerDC(t, "primary")
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
dir2, s2 := testServerDC(t, "secondary")
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
joinWAN(t, s2, s1)
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "secondary")
|
|
|
|
testrpc.WaitForLeader(t, s2.RPC, "primary")
|
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "secondary",
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []string
|
|
|
|
require.NoError(t, msgpackrpc.CallWithCodec(codec, "Status.Peers", &args, &out))
|
|
|
|
require.Equal(t, []string{s2.config.RPCAdvertise.String()}, out)
|
|
|
|
}
|