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. <opensource@akamai.com>
pull/587/head
Jeff Mitchell 2015-01-14 17:44:03 +00:00
parent 0cc009c480
commit 32d2c6b848
2 changed files with 53 additions and 5 deletions

View File

@ -85,9 +85,14 @@ func NewRPCClient(addr string) (*RPCClient, error) {
if len(sanedAddr) == 0 { if len(sanedAddr) == 0 {
sanedAddr = addr sanedAddr = addr
} }
mode := "tcp" mode := "tcp"
if strings.HasPrefix(sanedAddr, "unix://") { if strings.HasPrefix(sanedAddr, "unix://") {
sanedAddr = strings.TrimPrefix(sanedAddr, "unix://") sanedAddr = strings.TrimPrefix(sanedAddr, "unix://")
}
if strings.HasPrefix(sanedAddr, "/") {
mode = "unix" mode = "unix"
} }

View File

@ -6,8 +6,11 @@ import (
"github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/testutil"
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
"io" "io"
"io/ioutil"
"net" "net"
"os" "os"
"os/user"
"runtime"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -34,17 +37,22 @@ func testRPCClient(t *testing.T) *rpcParts {
} }
func testRPCClientWithConfig(t *testing.T, cb func(c *Config)) *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) lw := NewLogWriter(512)
mult := io.MultiWriter(os.Stderr, lw) mult := io.MultiWriter(os.Stderr, lw)
conf := nextConfig() conf := nextConfig()
cb(conf) 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) dir, agent := makeAgentLog(t, conf, mult)
rpc := NewAgentRPC(agent, l, mult, lw) 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) { func TestRPCClientLeave(t *testing.T) {
p1 := testRPCClient(t) p1 := testRPCClient(t)
defer p1.Close() defer p1.Close()