From 32d2c6b848bd68b13b8aabfe01f7aea42246c2d2 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 14 Jan 2015 17:44:03 +0000 Subject: [PATCH] Add a Unix socket RPC test. I modified some code in the testing library to not make assumptions about the listening socket; all RPC tests still pass. Still to do: Unix socket HTTP test. This code is copyright 2014 Akamai Technologies, Inc. --- command/agent/rpc_client.go | 5 +++ command/agent/rpc_client_test.go | 53 +++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/command/agent/rpc_client.go b/command/agent/rpc_client.go index c769ef1d4f..1490674f14 100644 --- a/command/agent/rpc_client.go +++ b/command/agent/rpc_client.go @@ -85,9 +85,14 @@ func NewRPCClient(addr string) (*RPCClient, error) { if len(sanedAddr) == 0 { sanedAddr = addr } + mode := "tcp" + if strings.HasPrefix(sanedAddr, "unix://") { sanedAddr = strings.TrimPrefix(sanedAddr, "unix://") + } + + if strings.HasPrefix(sanedAddr, "/") { mode = "unix" } diff --git a/command/agent/rpc_client_test.go b/command/agent/rpc_client_test.go index 3bf03d6dc6..24f0acadf3 100644 --- a/command/agent/rpc_client_test.go +++ b/command/agent/rpc_client_test.go @@ -6,8 +6,11 @@ import ( "github.com/hashicorp/consul/testutil" "github.com/hashicorp/serf/serf" "io" + "io/ioutil" "net" "os" + "os/user" + "runtime" "strings" "testing" "time" @@ -34,17 +37,22 @@ func testRPCClient(t *testing.T) *rpcParts { } func testRPCClientWithConfig(t *testing.T, cb func(c *Config)) *rpcParts { - 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() cb(conf) + rpcAddr, err := conf.ClientListener(conf.Addresses.RPC, conf.Ports.RPC) + if err != nil { + t.Fatalf("err: %s", err) + } + + l, err := net.Listen(rpcAddr.Network(), rpcAddr.String()) + if err != nil { + t.Fatalf("err: %s", err) + } + dir, agent := makeAgentLog(t, conf, mult) rpc := NewAgentRPC(agent, l, mult, lw) @@ -208,6 +216,41 @@ func TestRPCClientStats(t *testing.T) { } } +func TestRPCClientStatsUnix(t *testing.T) { + if runtime.GOOS == "windows" { + t.SkipNow() + } + + tempdir, err := ioutil.TempDir("", "consul-test-") + if err != nil { + t.Fatal("Could not create a working directory") + } + + user, err := user.Current() + if err != nil { + t.Fatal("Could not get current user") + } + + cb := func(c *Config) { + c.Addresses.RPC = "unix://" + tempdir + "/unix-rpc-test.sock;" + user.Uid + ";" + user.Gid + ";640" + } + + p1 := testRPCClientWithConfig(t, cb) + + 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) + } +} + func TestRPCClientLeave(t *testing.T) { p1 := testRPCClient(t) defer p1.Close()