rpc: provide unique node names for server and client

This commit is contained in:
Frank Schroeder 2017-06-26 14:23:09 +02:00 committed by Frank Schröder
parent 457910b191
commit 7af30dd7d7
3 changed files with 30 additions and 29 deletions

View File

@ -2,7 +2,6 @@ package consul
import ( import (
"bytes" "bytes"
"fmt"
"net" "net"
"os" "os"
"sync" "sync"
@ -17,12 +16,12 @@ import (
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
) )
func testClientConfig(t *testing.T, NodeName string) (string, *Config) { func testClientConfig(t *testing.T) (string, *Config) {
dir := testutil.TempDir(t, "consul") dir := testutil.TempDir(t, "consul")
config := DefaultConfig() config := DefaultConfig()
config.Datacenter = "dc1" config.Datacenter = "dc1"
config.DataDir = dir config.DataDir = dir
config.NodeName = NodeName config.NodeName = uniqueNodeName(t.Name())
config.RPCAddr = &net.TCPAddr{ config.RPCAddr = &net.TCPAddr{
IP: []byte{127, 0, 0, 1}, IP: []byte{127, 0, 0, 1},
Port: getPort(), Port: getPort(),
@ -37,24 +36,24 @@ func testClientConfig(t *testing.T, NodeName string) (string, *Config) {
} }
func testClient(t *testing.T) (string, *Client) { func testClient(t *testing.T) (string, *Client) {
return testClientDC(t, "dc1") return testClientWithConfig(t, func(c *Config) {
c.Datacenter = "dc1"
c.NodeName = uniqueNodeName(t.Name())
})
} }
func testClientDC(t *testing.T, dc string) (string, *Client) { func testClientDC(t *testing.T, dc string) (string, *Client) {
dir, config := testClientConfig(t, "testco.internal") return testClientWithConfig(t, func(c *Config) {
config.Datacenter = dc c.Datacenter = dc
c.NodeName = uniqueNodeName(t.Name())
client, err := NewClient(config) })
if err != nil {
t.Fatalf("err: %v", err)
}
return dir, client
} }
func testClientWithConfig(t *testing.T, cb func(c *Config)) (string, *Client) { func testClientWithConfig(t *testing.T, cb func(c *Config)) (string, *Client) {
name := fmt.Sprintf("Client %d", getPort()) dir, config := testClientConfig(t)
dir, config := testClientConfig(t, name) if cb != nil {
cb(config) cb(config)
}
client, err := NewClient(config) client, err := NewClient(config)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -282,7 +281,7 @@ func TestClient_RPC_ConsulServerPing(t *testing.T) {
} }
func TestClient_RPC_TLS(t *testing.T) { func TestClient_RPC_TLS(t *testing.T) {
dir1, conf1 := testServerConfig(t, "a.testco.internal") dir1, conf1 := testServerConfig(t)
conf1.VerifyIncoming = true conf1.VerifyIncoming = true
conf1.VerifyOutgoing = true conf1.VerifyOutgoing = true
configureTLS(conf1) configureTLS(conf1)
@ -293,7 +292,7 @@ func TestClient_RPC_TLS(t *testing.T) {
defer os.RemoveAll(dir1) defer os.RemoveAll(dir1)
defer s1.Shutdown() defer s1.Shutdown()
dir2, conf2 := testClientConfig(t, "b.testco.internal") dir2, conf2 := testClientConfig(t)
conf2.VerifyOutgoing = true conf2.VerifyOutgoing = true
configureTLS(conf2) configureTLS(conf2)
c1, err := NewClient(conf2) c1, err := NewClient(conf2)
@ -369,7 +368,7 @@ func TestClient_SnapshotRPC(t *testing.T) {
} }
func TestClient_SnapshotRPC_TLS(t *testing.T) { func TestClient_SnapshotRPC_TLS(t *testing.T) {
dir1, conf1 := testServerConfig(t, "a.testco.internal") dir1, conf1 := testServerConfig(t)
conf1.VerifyIncoming = true conf1.VerifyIncoming = true
conf1.VerifyOutgoing = true conf1.VerifyOutgoing = true
configureTLS(conf1) configureTLS(conf1)
@ -380,7 +379,7 @@ func TestClient_SnapshotRPC_TLS(t *testing.T) {
defer os.RemoveAll(dir1) defer os.RemoveAll(dir1)
defer s1.Shutdown() defer s1.Shutdown()
dir2, conf2 := testClientConfig(t, "b.testco.internal") dir2, conf2 := testClientConfig(t)
conf2.VerifyOutgoing = true conf2.VerifyOutgoing = true
configureTLS(conf2) configureTLS(conf2)
c1, err := NewClient(conf2) c1, err := NewClient(conf2)

View File

@ -42,8 +42,7 @@ func verifyCoordinatesEqual(t *testing.T, a, b *coordinate.Coordinate) {
} }
func TestCoordinate_Update(t *testing.T) { func TestCoordinate_Update(t *testing.T) {
name := fmt.Sprintf("Node %d", getPort()) dir1, config1 := testServerConfig(t)
dir1, config1 := testServerConfig(t, name)
defer os.RemoveAll(dir1) defer os.RemoveAll(dir1)
config1.CoordinateUpdatePeriod = 500 * time.Millisecond config1.CoordinateUpdatePeriod = 500 * time.Millisecond

View File

@ -29,11 +29,17 @@ func configureTLS(config *Config) {
config.KeyFile = "../../test/key/ourdomain.key" config.KeyFile = "../../test/key/ourdomain.key"
} }
func testServerConfig(t *testing.T, NodeName string) (string, *Config) { var id int64
func uniqueNodeName(name string) string {
return fmt.Sprintf("%s-node-%d", name, atomic.AddInt64(&id, 1))
}
func testServerConfig(t *testing.T) (string, *Config) {
dir := testutil.TempDir(t, "consul") dir := testutil.TempDir(t, "consul")
config := DefaultConfig() config := DefaultConfig()
config.NodeName = NodeName config.NodeName = uniqueNodeName(t.Name())
config.Bootstrap = true config.Bootstrap = true
config.Datacenter = "dc1" config.Datacenter = "dc1"
config.DataDir = dir config.DataDir = dir
@ -111,11 +117,8 @@ func testServerDCExpect(t *testing.T, dc string, expect int) (string, *Server) {
}) })
} }
var id int64
func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) { func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) {
nodeName := fmt.Sprintf("%s-node-%d", t.Name(), atomic.AddInt64(&id, 1)) dir, config := testServerConfig(t)
dir, config := testServerConfig(t, nodeName)
if cb != nil { if cb != nil {
cb(config) cb(config)
} }
@ -427,7 +430,7 @@ func TestServer_RPC(t *testing.T) {
} }
func TestServer_JoinLAN_TLS(t *testing.T) { func TestServer_JoinLAN_TLS(t *testing.T) {
dir1, conf1 := testServerConfig(t, "a.testco.internal") dir1, conf1 := testServerConfig(t)
conf1.VerifyIncoming = true conf1.VerifyIncoming = true
conf1.VerifyOutgoing = true conf1.VerifyOutgoing = true
configureTLS(conf1) configureTLS(conf1)
@ -438,7 +441,7 @@ func TestServer_JoinLAN_TLS(t *testing.T) {
defer os.RemoveAll(dir1) defer os.RemoveAll(dir1)
defer s1.Shutdown() defer s1.Shutdown()
dir2, conf2 := testServerConfig(t, "b.testco.internal") dir2, conf2 := testServerConfig(t)
conf2.Bootstrap = false conf2.Bootstrap = false
conf2.VerifyIncoming = true conf2.VerifyIncoming = true
conf2.VerifyOutgoing = true conf2.VerifyOutgoing = true