From 7af30dd7d7619ada67733056ec499717bf290950 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Mon, 26 Jun 2017 14:23:09 +0200 Subject: [PATCH] rpc: provide unique node names for server and client --- agent/consul/client_test.go | 37 ++++++++++++------------ agent/consul/coordinate_endpoint_test.go | 3 +- agent/consul/server_test.go | 19 +++++++----- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/agent/consul/client_test.go b/agent/consul/client_test.go index dd453eb2b..52503c8b9 100644 --- a/agent/consul/client_test.go +++ b/agent/consul/client_test.go @@ -2,7 +2,6 @@ package consul import ( "bytes" - "fmt" "net" "os" "sync" @@ -17,12 +16,12 @@ import ( "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") config := DefaultConfig() config.Datacenter = "dc1" config.DataDir = dir - config.NodeName = NodeName + config.NodeName = uniqueNodeName(t.Name()) config.RPCAddr = &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: getPort(), @@ -37,24 +36,24 @@ func testClientConfig(t *testing.T, NodeName string) (string, *Config) { } 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) { - dir, config := testClientConfig(t, "testco.internal") - config.Datacenter = dc - - client, err := NewClient(config) - if err != nil { - t.Fatalf("err: %v", err) - } - return dir, client + return testClientWithConfig(t, func(c *Config) { + c.Datacenter = dc + c.NodeName = uniqueNodeName(t.Name()) + }) } func testClientWithConfig(t *testing.T, cb func(c *Config)) (string, *Client) { - name := fmt.Sprintf("Client %d", getPort()) - dir, config := testClientConfig(t, name) - cb(config) + dir, config := testClientConfig(t) + if cb != nil { + cb(config) + } client, err := NewClient(config) if err != nil { t.Fatalf("err: %v", err) @@ -282,7 +281,7 @@ func TestClient_RPC_ConsulServerPing(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.VerifyOutgoing = true configureTLS(conf1) @@ -293,7 +292,7 @@ func TestClient_RPC_TLS(t *testing.T) { defer os.RemoveAll(dir1) defer s1.Shutdown() - dir2, conf2 := testClientConfig(t, "b.testco.internal") + dir2, conf2 := testClientConfig(t) conf2.VerifyOutgoing = true configureTLS(conf2) c1, err := NewClient(conf2) @@ -369,7 +368,7 @@ func TestClient_SnapshotRPC(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.VerifyOutgoing = true configureTLS(conf1) @@ -380,7 +379,7 @@ func TestClient_SnapshotRPC_TLS(t *testing.T) { defer os.RemoveAll(dir1) defer s1.Shutdown() - dir2, conf2 := testClientConfig(t, "b.testco.internal") + dir2, conf2 := testClientConfig(t) conf2.VerifyOutgoing = true configureTLS(conf2) c1, err := NewClient(conf2) diff --git a/agent/consul/coordinate_endpoint_test.go b/agent/consul/coordinate_endpoint_test.go index 2debf2872..6bb166d9d 100644 --- a/agent/consul/coordinate_endpoint_test.go +++ b/agent/consul/coordinate_endpoint_test.go @@ -42,8 +42,7 @@ func verifyCoordinatesEqual(t *testing.T, a, b *coordinate.Coordinate) { } func TestCoordinate_Update(t *testing.T) { - name := fmt.Sprintf("Node %d", getPort()) - dir1, config1 := testServerConfig(t, name) + dir1, config1 := testServerConfig(t) defer os.RemoveAll(dir1) config1.CoordinateUpdatePeriod = 500 * time.Millisecond diff --git a/agent/consul/server_test.go b/agent/consul/server_test.go index 821e64d01..f02e6a435 100644 --- a/agent/consul/server_test.go +++ b/agent/consul/server_test.go @@ -29,11 +29,17 @@ func configureTLS(config *Config) { 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") config := DefaultConfig() - config.NodeName = NodeName + config.NodeName = uniqueNodeName(t.Name()) config.Bootstrap = true config.Datacenter = "dc1" 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) { - nodeName := fmt.Sprintf("%s-node-%d", t.Name(), atomic.AddInt64(&id, 1)) - dir, config := testServerConfig(t, nodeName) + dir, config := testServerConfig(t) if cb != nil { cb(config) } @@ -427,7 +430,7 @@ func TestServer_RPC(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.VerifyOutgoing = true configureTLS(conf1) @@ -438,7 +441,7 @@ func TestServer_JoinLAN_TLS(t *testing.T) { defer os.RemoveAll(dir1) defer s1.Shutdown() - dir2, conf2 := testServerConfig(t, "b.testco.internal") + dir2, conf2 := testServerConfig(t) conf2.Bootstrap = false conf2.VerifyIncoming = true conf2.VerifyOutgoing = true