open-consul/agent/consul/status_endpoint_test.go

71 lines
1.5 KiB
Go
Raw Normal View History

2013-12-10 23:16:41 +00:00
package consul
import (
"net"
"net/rpc"
"os"
"testing"
"time"
"github.com/hashicorp/consul/agent/pool"
"github.com/hashicorp/consul/testrpc"
"github.com/hashicorp/net-rpc-msgpackrpc"
2013-12-10 23:16:41 +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)
}
2013-12-10 23:16:41 +00:00
// Write the Consul RPC byte to set the mode
conn.Write([]byte{byte(pool.RPCConsul)})
return msgpackrpc.NewClientCodec(conn)
2013-12-10 23:16:41 +00:00
}
func TestStatusLeader(t *testing.T) {
t.Parallel()
2013-12-10 23:16:41 +00:00
dir1, s1 := testServer(t)
defer os.RemoveAll(dir1)
defer s1.Shutdown()
codec := rpcClient(t, s1)
defer codec.Close()
2013-12-10 23:16:41 +00:00
arg := struct{}{}
var leader string
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)
}
testrpc.WaitForTestAgent(t, s1.RPC, "dc1")
2013-12-10 23:16:41 +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")
}
}
func TestStatusPeers(t *testing.T) {
t.Parallel()
dir1, s1 := testServer(t)
defer os.RemoveAll(dir1)
defer s1.Shutdown()
codec := rpcClient(t, s1)
defer codec.Close()
arg := struct{}{}
var peers []string
if err := msgpackrpc.CallWithCodec(codec, "Status.Peers", arg, &peers); err != nil {
t.Fatalf("err: %v", err)
}
if len(peers) != 1 {
t.Fatalf("no peers: %v", peers)
}
}