2013-12-30 22:42:41 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-05-16 22:49:47 +00:00
|
|
|
"errors"
|
2013-12-30 22:42:41 +00:00
|
|
|
"fmt"
|
2014-05-08 23:19:05 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2014-05-16 22:49:47 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2013-12-30 22:42:41 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rpcParts struct {
|
|
|
|
dir string
|
|
|
|
client *RPCClient
|
|
|
|
agent *Agent
|
|
|
|
rpc *AgentRPC
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rpcParts) Close() {
|
|
|
|
r.client.Close()
|
|
|
|
r.rpc.Shutdown()
|
|
|
|
r.agent.Shutdown()
|
|
|
|
os.RemoveAll(r.dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testRPCClient returns an RPCClient connected to an RPC server that
|
|
|
|
// serves only this connection.
|
|
|
|
func testRPCClient(t *testing.T) *rpcParts {
|
2014-09-11 04:56:31 +00:00
|
|
|
return testRPCClientWithConfig(t, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRPCClientWithConfig(t *testing.T, c *Config) *rpcParts {
|
2013-12-30 22:42:41 +00:00
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lw := NewLogWriter(512)
|
|
|
|
mult := io.MultiWriter(os.Stderr, lw)
|
|
|
|
|
|
|
|
conf := nextConfig()
|
2014-09-11 04:56:31 +00:00
|
|
|
if c != nil {
|
|
|
|
conf = MergeConfig(conf, c)
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:42:41 +00:00
|
|
|
dir, agent := makeAgentLog(t, conf, mult)
|
|
|
|
rpc := NewAgentRPC(agent, l, mult, lw)
|
|
|
|
|
|
|
|
rpcClient, err := NewRPCClient(l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rpcParts{
|
|
|
|
dir: dir,
|
|
|
|
client: rpcClient,
|
|
|
|
agent: agent,
|
|
|
|
rpc: rpc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCClientForceLeave(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
p2 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
2014-04-11 22:46:55 +00:00
|
|
|
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfLan)
|
2013-12-30 22:42:41 +00:00
|
|
|
if _, err := p1.agent.JoinLAN([]string{s2Addr}); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p2.agent.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p1.client.ForceLeave(p2.agent.config.NodeName); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := p1.agent.LANMembers()
|
|
|
|
if len(m) != 2 {
|
|
|
|
t.Fatalf("should have 2 members: %#v", m)
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:19:05 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
m := p1.agent.LANMembers()
|
|
|
|
success := m[1].Status == serf.StatusLeft
|
|
|
|
return success, errors.New(m[1].Status.String())
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("member status is %v, should be left", err)
|
|
|
|
})
|
2013-12-30 22:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCClientJoinLAN(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
p2 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
2014-04-11 22:46:55 +00:00
|
|
|
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfLan)
|
2013-12-30 22:42:41 +00:00
|
|
|
n, err := p1.client.Join([]string{s2Addr}, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != 1 {
|
|
|
|
t.Fatalf("n != 1: %d", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCClientJoinWAN(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
p2 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
2014-04-11 22:46:55 +00:00
|
|
|
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfWan)
|
2013-12-30 22:42:41 +00:00
|
|
|
n, err := p1.client.Join([]string{s2Addr}, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != 1 {
|
|
|
|
t.Fatalf("n != 1: %d", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCClientLANMembers(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
p2 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
mem, err := p1.client.LANMembers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mem) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", mem)
|
|
|
|
}
|
|
|
|
|
2014-04-11 22:46:55 +00:00
|
|
|
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfLan)
|
2013-12-30 22:42:41 +00:00
|
|
|
_, err = p1.client.Join([]string{s2Addr}, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mem, err = p1.client.LANMembers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mem) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", mem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCClientWANMembers(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
p2 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
mem, err := p1.client.WANMembers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mem) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", mem)
|
|
|
|
}
|
|
|
|
|
2014-04-11 22:46:55 +00:00
|
|
|
s2Addr := fmt.Sprintf("127.0.0.1:%d", p2.agent.config.Ports.SerfWan)
|
2013-12-30 22:42:41 +00:00
|
|
|
_, err = p1.client.Join([]string{s2Addr}, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mem, err = p1.client.WANMembers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mem) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", mem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:19:05 +00:00
|
|
|
func TestRPCClientStats(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
|
|
|
|
stats, err := p1.client.Stats()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := stats["agent"]; !ok {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := stats["consul"]; !ok {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:42:41 +00:00
|
|
|
func TestRPCClientLeave(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
|
|
|
|
if err := p1.client.Leave(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:19:05 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2013-12-30 22:42:41 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-p1.agent.ShutdownCh():
|
|
|
|
default:
|
|
|
|
t.Fatalf("agent should be shutdown!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCClientMonitor(t *testing.T) {
|
|
|
|
p1 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
|
|
|
|
eventCh := make(chan string, 64)
|
|
|
|
if handle, err := p1.client.Monitor("debug", eventCh); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
} else {
|
|
|
|
defer p1.client.Stop(handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
OUTER1:
|
2014-10-16 17:14:36 +00:00
|
|
|
for i := 0; ; i++ {
|
2013-12-30 22:42:41 +00:00
|
|
|
select {
|
|
|
|
case e := <-eventCh:
|
|
|
|
if strings.Contains(e, "Accepted client") {
|
|
|
|
found = true
|
2014-10-16 01:23:21 +00:00
|
|
|
break OUTER1
|
2013-12-30 22:42:41 +00:00
|
|
|
}
|
|
|
|
default:
|
2014-10-16 17:14:36 +00:00
|
|
|
if i > 100 {
|
|
|
|
break OUTER1
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2013-12-30 22:42:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("should log client accept")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join a bad thing to generate more events
|
|
|
|
p1.agent.JoinLAN(nil)
|
2014-05-08 23:19:05 +00:00
|
|
|
|
2013-12-30 22:42:41 +00:00
|
|
|
found = false
|
|
|
|
OUTER2:
|
2014-10-16 17:14:36 +00:00
|
|
|
for i := 0; ; i++ {
|
2013-12-30 22:42:41 +00:00
|
|
|
select {
|
|
|
|
case e := <-eventCh:
|
|
|
|
if strings.Contains(e, "joining") {
|
|
|
|
found = true
|
2014-10-16 17:14:36 +00:00
|
|
|
break OUTER2
|
2013-12-30 22:42:41 +00:00
|
|
|
}
|
|
|
|
default:
|
2014-10-16 17:14:36 +00:00
|
|
|
if i > 100 {
|
|
|
|
break OUTER2
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2013-12-30 22:42:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("should log joining")
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 04:56:31 +00:00
|
|
|
|
2014-09-11 05:51:33 +00:00
|
|
|
func TestRPCClientListKeys(t *testing.T) {
|
2014-09-11 04:56:31 +00:00
|
|
|
key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
|
2014-09-29 16:31:55 +00:00
|
|
|
conf := Config{EncryptKey: key1, Datacenter: "dc1"}
|
2014-09-11 04:56:31 +00:00
|
|
|
p1 := testRPCClientWithConfig(t, &conf)
|
|
|
|
defer p1.Close()
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// Key is initially installed to both wan/lan
|
|
|
|
keys := listKeys(t, p1.client)
|
|
|
|
if _, ok := keys["dc1"][key1]; !ok {
|
2014-09-11 05:51:33 +00:00
|
|
|
t.Fatalf("bad: %#v", keys)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
2014-09-29 16:31:55 +00:00
|
|
|
if _, ok := keys["wan"][key1]; !ok {
|
2014-09-11 04:56:31 +00:00
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
2014-09-11 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCClientInstallKey(t *testing.T) {
|
|
|
|
key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
|
|
|
|
key2 := "xAEZ3uVHRMZD9GcYMZaRQw=="
|
2014-10-01 01:23:55 +00:00
|
|
|
conf := Config{EncryptKey: key1, Server: true}
|
2014-09-11 05:51:33 +00:00
|
|
|
p1 := testRPCClientWithConfig(t, &conf)
|
|
|
|
defer p1.Close()
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// key2 is not installed yet
|
|
|
|
keys := listKeys(t, p1.client)
|
|
|
|
if num, ok := keys["dc1"][key2]; ok || num != 0 {
|
2014-09-11 05:51:33 +00:00
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
2014-09-29 16:31:55 +00:00
|
|
|
if num, ok := keys["wan"][key2]; ok || num != 0 {
|
2014-09-11 04:56:31 +00:00
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// install key2
|
2014-09-29 16:54:46 +00:00
|
|
|
r, err := p1.client.InstallKey(key2)
|
|
|
|
if err != nil {
|
2014-09-29 16:31:55 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
2014-09-29 16:54:46 +00:00
|
|
|
keyringSuccess(t, r)
|
2014-09-11 04:56:31 +00:00
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// key2 should now be installed
|
|
|
|
keys = listKeys(t, p1.client)
|
|
|
|
if num, ok := keys["dc1"][key2]; !ok || num != 1 {
|
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
|
|
|
if num, ok := keys["wan"][key2]; !ok || num != 1 {
|
2014-09-11 05:51:33 +00:00
|
|
|
t.Fatalf("bad: %#v", keys)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
func TestRPCClientUseKey(t *testing.T) {
|
2014-09-11 04:56:31 +00:00
|
|
|
key1 := "tbLJg26ZJyJ9pK3qhc9jig=="
|
2014-09-11 05:51:33 +00:00
|
|
|
key2 := "xAEZ3uVHRMZD9GcYMZaRQw=="
|
2014-10-04 20:43:10 +00:00
|
|
|
conf := Config{EncryptKey: key1, Server: true}
|
2014-09-11 04:56:31 +00:00
|
|
|
p1 := testRPCClientWithConfig(t, &conf)
|
|
|
|
defer p1.Close()
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// add a second key to the ring
|
2014-09-29 16:54:46 +00:00
|
|
|
r, err := p1.client.InstallKey(key2)
|
|
|
|
if err != nil {
|
2014-09-29 16:31:55 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
2014-09-29 16:54:46 +00:00
|
|
|
keyringSuccess(t, r)
|
2014-09-11 04:56:31 +00:00
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// key2 is installed
|
|
|
|
keys := listKeys(t, p1.client)
|
|
|
|
if num, ok := keys["dc1"][key2]; !ok || num != 1 {
|
2014-09-11 04:56:31 +00:00
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
2014-09-29 16:31:55 +00:00
|
|
|
if num, ok := keys["wan"][key2]; !ok || num != 1 {
|
2014-09-11 04:56:31 +00:00
|
|
|
t.Fatalf("bad: %#v", keys)
|
|
|
|
}
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// can't remove key1 yet
|
2014-09-29 16:54:46 +00:00
|
|
|
r, err = p1.client.RemoveKey(key1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
2014-09-29 16:54:46 +00:00
|
|
|
keyringError(t, r)
|
2014-09-11 04:56:31 +00:00
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
// change primary key
|
2014-09-29 16:54:46 +00:00
|
|
|
r, err = p1.client.UseKey(key2)
|
|
|
|
if err != nil {
|
2014-09-29 16:31:55 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-09-11 05:51:33 +00:00
|
|
|
}
|
2014-09-29 16:54:46 +00:00
|
|
|
keyringSuccess(t, r)
|
2014-09-29 16:31:55 +00:00
|
|
|
|
|
|
|
// can remove key1 now
|
2014-09-29 16:54:46 +00:00
|
|
|
r, err = p1.client.RemoveKey(key1)
|
|
|
|
if err != nil {
|
2014-09-29 16:31:55 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
2014-09-29 16:54:46 +00:00
|
|
|
keyringSuccess(t, r)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 05:51:33 +00:00
|
|
|
func TestRPCClientKeyOperation_encryptionDisabled(t *testing.T) {
|
2014-09-11 04:56:31 +00:00
|
|
|
p1 := testRPCClient(t)
|
|
|
|
defer p1.Close()
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
r, err := p1.client.ListKeys()
|
2014-09-29 16:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2014-09-11 05:51:33 +00:00
|
|
|
}
|
2014-09-29 16:54:46 +00:00
|
|
|
keyringError(t, r)
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 16:31:55 +00:00
|
|
|
func listKeys(t *testing.T, c *RPCClient) map[string]map[string]int {
|
|
|
|
resp, err := c.ListKeys()
|
2014-09-11 05:51:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-09-29 16:31:55 +00:00
|
|
|
out := make(map[string]map[string]int)
|
|
|
|
for _, k := range resp.Keys {
|
|
|
|
respID := k.Datacenter
|
|
|
|
if k.Pool == "WAN" {
|
|
|
|
respID = "wan"
|
|
|
|
}
|
|
|
|
out[respID] = map[string]int{k.Key: k.Count}
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
2014-09-29 16:31:55 +00:00
|
|
|
return out
|
2014-09-11 04:56:31 +00:00
|
|
|
}
|
2014-09-29 16:54:46 +00:00
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
func keyringError(t *testing.T, r keyringResponse) {
|
2014-09-29 16:54:46 +00:00
|
|
|
for _, i := range r.Info {
|
|
|
|
if i.Error == "" {
|
|
|
|
t.Fatalf("no error reported from %s (%s)", i.Datacenter, i.Pool)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
func keyringSuccess(t *testing.T, r keyringResponse) {
|
2014-09-29 16:54:46 +00:00
|
|
|
for _, i := range r.Info {
|
|
|
|
if i.Error != "" {
|
|
|
|
t.Fatalf("error from %s (%s): %s", i.Datacenter, i.Pool, i.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|