2015-06-07 19:08:47 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2015-11-16 02:27:02 +00:00
|
|
|
"net"
|
|
|
|
"net/rpc"
|
2015-06-07 19:08:47 +00:00
|
|
|
"testing"
|
2015-11-16 02:27:02 +00:00
|
|
|
"time"
|
2015-06-07 19:08:47 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
2015-11-16 02:27:02 +00:00
|
|
|
// rpcClient is a test helper method to return a ClientCodec to use to make rpc
|
|
|
|
// calls to the passed server.
|
|
|
|
func rpcClient(t *testing.T, s *Server) rpc.ClientCodec {
|
|
|
|
addr := s.config.RPCAddr
|
|
|
|
conn, err := net.DialTimeout("tcp", addr.String(), time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// Write the Consul RPC byte to set the mode
|
|
|
|
conn.Write([]byte{byte(rpcNomad)})
|
|
|
|
return NewClientCodec(conn)
|
|
|
|
}
|
|
|
|
|
2015-06-07 19:08:47 +00:00
|
|
|
func TestRPC_forwardLeader(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2015-06-07 19:08:47 +00:00
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
s2 := testServer(t, func(c *Config) {
|
|
|
|
c.DevDisableBootstrap = true
|
|
|
|
})
|
|
|
|
defer s2.Shutdown()
|
|
|
|
testJoin(t, s1, s2)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
2015-08-26 00:36:52 +00:00
|
|
|
testutil.WaitForLeader(t, s2.RPC)
|
2015-06-07 19:08:47 +00:00
|
|
|
|
2016-07-10 17:36:55 +00:00
|
|
|
isLeader, remote := s1.getLeader()
|
|
|
|
if !isLeader && remote == nil {
|
|
|
|
t.Fatalf("missing leader")
|
2015-06-07 19:08:47 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 17:36:55 +00:00
|
|
|
if remote != nil {
|
|
|
|
var out struct{}
|
|
|
|
err := s1.forwardLeader(remote, "Status.Ping", struct{}{}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isLeader, remote = s2.getLeader()
|
|
|
|
if !isLeader && remote == nil {
|
|
|
|
t.Fatalf("missing leader")
|
|
|
|
}
|
|
|
|
|
|
|
|
if remote != nil {
|
|
|
|
var out struct{}
|
|
|
|
err := s2.forwardLeader(remote, "Status.Ping", struct{}{}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-06-07 19:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPC_forwardRegion(t *testing.T) {
|
2017-07-23 22:04:38 +00:00
|
|
|
t.Parallel()
|
2015-06-07 19:08:47 +00:00
|
|
|
s1 := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
s2 := testServer(t, func(c *Config) {
|
|
|
|
c.Region = "region2"
|
|
|
|
})
|
|
|
|
defer s2.Shutdown()
|
|
|
|
testJoin(t, s1, s2)
|
2015-10-08 23:32:42 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
testutil.WaitForLeader(t, s2.RPC)
|
2015-06-07 19:08:47 +00:00
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
err := s1.forwardRegion("region2", "Status.Ping", struct{}{}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-14 01:18:40 +00:00
|
|
|
err = s2.forwardRegion("global", "Status.Ping", struct{}{}, &out)
|
2015-06-07 19:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|