2013-12-10 23:16:41 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-05-08 23:23:54 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2013-12-10 23:16:41 +00:00
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
"net"
|
|
|
|
"net/rpc"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func rpcClient(t *testing.T, s *Server) *rpc.Client {
|
|
|
|
addr := s.config.RPCAddr
|
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)
|
|
|
|
}
|
|
|
|
// Write the Consul RPC byte to set the mode
|
|
|
|
conn.Write([]byte{byte(rpcConsul)})
|
|
|
|
|
2014-06-08 21:02:42 +00:00
|
|
|
cc := codec.GoRpc.ClientCodec(conn, msgpackHandle)
|
2013-12-10 23:16:41 +00:00
|
|
|
return rpc.NewClientWithCodec(cc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusLeader(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
arg := struct{}{}
|
|
|
|
var leader string
|
|
|
|
if err := client.Call("Status.Leader", arg, &leader); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leader != "" {
|
|
|
|
t.Fatalf("unexpected leader: %v", leader)
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:23:54 +00:00
|
|
|
testutil.WaitForLeader(t, client.Call, "dc1")
|
2013-12-10 23:16:41 +00:00
|
|
|
|
|
|
|
if err := client.Call("Status.Leader", arg, &leader); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leader == "" {
|
|
|
|
t.Fatalf("no leader")
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 19:50:58 +00:00
|
|
|
|
|
|
|
func TestStatusPeers(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
client := rpcClient(t, s1)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
arg := struct{}{}
|
|
|
|
var peers []string
|
|
|
|
if err := client.Call("Status.Peers", arg, &peers); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(peers) != 1 {
|
|
|
|
t.Fatalf("no peers: %v", peers)
|
|
|
|
}
|
|
|
|
}
|