2013-12-19 22:54:32 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-12-19 23:08:55 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-05-16 22:49:47 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2014-01-01 00:46:56 +00:00
|
|
|
"net"
|
2013-12-19 22:54:32 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-05-26 22:47:47 +00:00
|
|
|
func testClientConfig(t *testing.T, NodeName string) (string, *Config) {
|
2013-12-19 22:54:32 +00:00
|
|
|
dir := tmpDir(t)
|
|
|
|
config := DefaultConfig()
|
2014-04-07 21:36:32 +00:00
|
|
|
config.Datacenter = "dc1"
|
2013-12-19 22:54:32 +00:00
|
|
|
config.DataDir = dir
|
2014-05-26 22:47:47 +00:00
|
|
|
config.NodeName = NodeName
|
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) {
|
|
|
|
return testClientDC(t, "dc1")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testClientDC(t *testing.T, dc string) (string, *Client) {
|
2014-05-26 22:47:47 +00:00
|
|
|
dir, config := testClientConfig(t, "testco.internal")
|
2014-04-07 21:36:32 +00:00
|
|
|
config.Datacenter = dc
|
|
|
|
|
2013-12-19 22:54:32 +00:00
|
|
|
client, err := NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return dir, client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_StartStop(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
2013-12-30 20:20:17 +00:00
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
2013-12-19 22:56:38 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the members
|
2014-05-26 22:55:20 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
server_check := len(s1.LANMembers()) == 2
|
|
|
|
client_check := len(c1.LANMembers()) == 2
|
|
|
|
return server_check && client_check, nil
|
|
|
|
}, func(err error) {
|
2013-12-19 22:56:38 +00:00
|
|
|
t.Fatalf("bad len")
|
2014-05-26 22:55:20 +00:00
|
|
|
})
|
2013-12-19 22:56:38 +00:00
|
|
|
|
|
|
|
// Check we have a new consul
|
2014-05-09 00:29:51 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
return len(c1.consuls) == 1, nil
|
|
|
|
}, func(err error) {
|
2013-12-19 22:56:38 +00:00
|
|
|
t.Fatalf("expected consul server")
|
2014-05-09 00:29:51 +00:00
|
|
|
})
|
2013-12-19 22:56:38 +00:00
|
|
|
}
|
2013-12-19 23:08:55 +00:00
|
|
|
|
|
|
|
func TestClient_RPC(t *testing.T) {
|
|
|
|
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
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
2013-12-30 20:20:17 +00:00
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
2013-12-19 23:08:55 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
err := c1.RPC("Status.Ping", struct{}{}, &out)
|
|
|
|
return err == nil, err
|
|
|
|
}, func(err error) {
|
2013-12-19 23:08:55 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-05-09 00:29:51 +00:00
|
|
|
})
|
2013-12-19 23:08:55 +00:00
|
|
|
}
|
2014-04-07 21:36:32 +00:00
|
|
|
|
|
|
|
func TestClient_RPC_TLS(t *testing.T) {
|
2014-05-26 22:47:47 +00:00
|
|
|
dir1, conf1 := testServerConfig(t, "a.testco.internal")
|
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()
|
|
|
|
|
2014-05-26 22:47:47 +00:00
|
|
|
dir2, conf2 := testClientConfig(t, "b.testco.internal")
|
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
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := c1.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2014-05-26 21:49:23 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
err := c1.RPC("Status.Ping", struct{}{}, &out)
|
|
|
|
return err == nil, err
|
|
|
|
}, func(err error) {
|
2014-04-07 21:36:32 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-05-26 21:49:23 +00:00
|
|
|
})
|
2014-04-07 21:36:32 +00:00
|
|
|
}
|