From de66412bddaf5580853188d09950692f022b133d Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Fri, 5 May 2017 12:29:49 +0200 Subject: [PATCH] test: simplify joining clusters --- consul/acl_replication_test.go | 6 +- consul/acl_test.go | 63 ++----------- consul/autopilot_test.go | 42 ++------- consul/catalog_endpoint_test.go | 46 ++-------- consul/client_test.go | 58 +++--------- consul/helper_test.go | 69 ++++++++++++++ consul/internal_endpoint_test.go | 7 +- consul/leader_test.go | 101 ++++----------------- consul/operator_autopilot_endpoint_test.go | 11 +-- consul/prepared_query_endpoint_test.go | 24 +---- consul/server_test.go | 78 +++------------- consul/session_ttl_test.go | 11 +-- consul/snapshot_endpoint_test.go | 13 +-- consul/stats_fetcher_test.go | 11 +-- 14 files changed, 153 insertions(+), 387 deletions(-) create mode 100644 consul/helper_test.go diff --git a/consul/acl_replication_test.go b/consul/acl_replication_test.go index 57e2a93bf6..019bc3072e 100644 --- a/consul/acl_replication_test.go +++ b/consul/acl_replication_test.go @@ -338,11 +338,7 @@ func TestACLReplication(t *testing.T) { defer s2.Shutdown() // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s1.RPC, "dc2") diff --git a/consul/acl_test.go b/consul/acl_test.go index ce864126ac..a085330b85 100644 --- a/consul/acl_test.go +++ b/consul/acl_test.go @@ -1,7 +1,6 @@ package consul import ( - "fmt" "os" "reflect" "strings" @@ -227,10 +226,7 @@ func TestACL_NonAuthority_NotFound(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) }) client := rpcClient(t, s1) @@ -272,11 +268,7 @@ func TestACL_NonAuthority_Found(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) }) testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -343,11 +335,7 @@ func TestACL_NonAuthority_Management(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) }) testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -395,11 +383,7 @@ func TestACL_DownPolicy_Deny(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) }) testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -464,11 +448,7 @@ func TestACL_DownPolicy_Allow(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) }) testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -535,11 +515,7 @@ func TestACL_DownPolicy_ExtendCache(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) }) testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -627,14 +603,8 @@ func TestACL_Replication(t *testing.T) { defer s3.Shutdown() // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) + joinWAN(t, s3, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s1.RPC, "dc2") testrpc.WaitForLeader(t, s1.RPC, "dc3") @@ -729,11 +699,7 @@ func TestACL_MultiDC_Found(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s1.RPC, "dc2") @@ -1881,14 +1847,3 @@ service "service" { t.Fatalf("err: %v", err) } } - -func wantPeers(s *Server, peers int) error { - n, err := s.numPeers() - if err != nil { - return err - } - if got, want := n, peers; got != want { - return fmt.Errorf("got %d peers want %d", got, want) - } - return nil -} diff --git a/consul/autopilot_test.go b/consul/autopilot_test.go index 783132ad31..32d8271d18 100644 --- a/consul/autopilot_test.go +++ b/consul/autopilot_test.go @@ -1,7 +1,6 @@ package consul import ( - "fmt" "os" "testing" "time" @@ -40,14 +39,8 @@ func testCleanupDeadServer(t *testing.T, raftVersion int) { servers := []*Server{s1, s2, s3} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) for _, s := range servers { retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) }) @@ -73,9 +66,7 @@ func testCleanupDeadServer(t *testing.T, raftVersion int) { }) // Join the new server - if _, err := s4.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s4, s1) servers[2] = s4 // Make sure the dead server is removed and we're back to 3 total peers @@ -111,12 +102,8 @@ func TestAutopilot_CleanupDeadServerPeriodic(t *testing.T) { servers := []*Server{s1, s2, s3, s4} // Join the servers to s1 - addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) - for _, s := range servers[1:] { - if _, err := s.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s, s1) } for _, s := range servers { @@ -152,13 +139,8 @@ func TestAutopilot_CleanupStaleRaftServer(t *testing.T) { servers := []*Server{s1, s2, s3} // Join the servers to s1 - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - for _, s := range servers[1:] { - if _, err := s.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s, s1) } for _, s := range servers { @@ -168,9 +150,7 @@ func TestAutopilot_CleanupStaleRaftServer(t *testing.T) { testrpc.WaitForLeader(t, s1.RPC, "dc1") // Add s4 to peers directly - s4addr := fmt.Sprintf("127.0.0.1:%d", - s4.config.SerfLANConfig.MemberlistConfig.BindPort) - s1.raft.AddVoter(raft.ServerID(s4.config.NodeID), raft.ServerAddress(s4addr), 0, 0) + s1.raft.AddVoter(raft.ServerID(s4.config.NodeID), raft.ServerAddress(joinAddrLAN(s4)), 0, 0) // Verify we have 4 peers peers, err := s1.numPeers() @@ -208,11 +188,7 @@ func TestAutopilot_PromoteNonVoter(t *testing.T) { }) defer os.RemoveAll(dir2) defer s2.Shutdown() - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") // Wait for the new server to be added as a non-voter, but make sure @@ -252,9 +228,7 @@ func TestAutopilot_PromoteNonVoter(t *testing.T) { }) defer os.RemoveAll(dir3) defer s3.Shutdown() - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s3, s1) retry.Run(t, func(r *retry.R) { future := s1.raft.GetConfiguration() if err := future.Error(); err != nil { diff --git a/consul/catalog_endpoint_test.go b/consul/catalog_endpoint_test.go index 747f4efde0..f27207f7ed 100644 --- a/consul/catalog_endpoint_test.go +++ b/consul/catalog_endpoint_test.go @@ -196,11 +196,7 @@ func TestCatalog_Register_ForwardLeader(t *testing.T) { defer codec2.Close() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s2.RPC, "dc1") @@ -241,11 +237,7 @@ func TestCatalog_Register_ForwardDC(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc2") @@ -513,11 +505,7 @@ func TestCatalog_ListDatacenters(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -554,14 +542,8 @@ func TestCatalog_ListDatacenters_DistanceSort(t *testing.T) { defer s3.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) + joinWAN(t, s3, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") var out []string @@ -699,11 +681,7 @@ func TestCatalog_ListNodes_StaleRaad(t *testing.T) { defer codec2.Close() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s2.RPC, "dc1") @@ -767,11 +745,7 @@ func TestCatalog_ListNodes_ConsistentRead_Fail(t *testing.T) { defer codec2.Close() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s2.RPC, "dc1") @@ -817,11 +791,7 @@ func TestCatalog_ListNodes_ConsistentRead(t *testing.T) { defer codec2.Close() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s2.RPC, "dc1") diff --git a/consul/client_test.go b/consul/client_test.go index 509f69411d..b9ce3ccd6a 100644 --- a/consul/client_test.go +++ b/consul/client_test.go @@ -80,10 +80,7 @@ func TestClient_JoinLAN(t *testing.T) { defer c1.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) retry.Run(t, func(r *retry.R) { if got, want := c1.servers.NumServers(), 1; got != want { r.Fatal("got %d servers want %d", got, want) @@ -107,9 +104,8 @@ func TestClient_JoinLAN_Invalid(t *testing.T) { defer c1.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err == nil { - t.Fatalf("should error") + if _, err := c1.JoinLAN([]string{joinAddrLAN(s1)}); err == nil { + t.Fatal("should error") } time.Sleep(50 * time.Millisecond) @@ -131,10 +127,8 @@ func TestClient_JoinWAN_Invalid(t *testing.T) { defer c1.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err == nil { - t.Fatalf("should error") + if _, err := c1.JoinLAN([]string{joinAddrWAN(s1)}); err == nil { + t.Fatal("should error") } time.Sleep(50 * time.Millisecond) @@ -162,11 +156,7 @@ func TestClient_RPC(t *testing.T) { } // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) // Check the members if len(s1.LANMembers()) != 2 { @@ -195,11 +185,7 @@ func TestClient_RPC_Pool(t *testing.T) { defer c1.Shutdown() // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) // Wait for both agents to finish joining retry.Run(t, func(r *retry.R) { @@ -256,11 +242,7 @@ func TestClient_RPC_ConsulServerPing(t *testing.T) { // Join all servers. for _, s := range servers { - addr := fmt.Sprintf("127.0.0.1:%d", - s.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c, s) } // Sleep to allow Serf to sync, shuffle, and let the shuffle complete @@ -327,11 +309,7 @@ func TestClient_RPC_TLS(t *testing.T) { } // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) // Wait for joins to finish/RPC to succeed retry.Run(t, func(r *retry.R) { @@ -360,11 +338,7 @@ func TestClient_SnapshotRPC(t *testing.T) { testrpc.WaitForLeader(t, s1.RPC, "dc1") // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) if len(s1.LANMembers()) != 2 || len(c1.LANMembers()) != 2 { t.Fatalf("Server has %v of %v expected members; Client has %v of %v expected members.", len(s1.LANMembers()), 2, len(c1.LANMembers()), 2) } @@ -419,11 +393,7 @@ func TestClient_SnapshotRPC_TLS(t *testing.T) { testrpc.WaitForLeader(t, s1.RPC, "dc1") // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) if len(s1.LANMembers()) != 2 || len(c1.LANMembers()) != 2 { t.Fatalf("Server has %v of %v expected members; Client has %v of %v expected members.", len(s1.LANMembers()), 2, len(c1.LANMembers()), 2) } @@ -472,11 +442,7 @@ func TestClientServer_UserEvent(t *testing.T) { defer s1.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) // Wait for the leader testrpc.WaitForLeader(t, s1.RPC, "dc1") diff --git a/consul/helper_test.go b/consul/helper_test.go new file mode 100644 index 0000000000..1a4287226d --- /dev/null +++ b/consul/helper_test.go @@ -0,0 +1,69 @@ +package consul + +import ( + "fmt" + "testing" +) + +// wantPeers determines whether the server has the given +// number of peers. +func wantPeers(s *Server, peers int) error { + n, err := s.numPeers() + if err != nil { + return err + } + if got, want := n, peers; got != want { + return fmt.Errorf("got %d peers want %d", got, want) + } + return nil +} + +// joinAddrLAN returns the address other servers can +// use to join the cluster on the LAN interface. +func joinAddrLAN(s *Server) string { + if s == nil { + panic("no server") + } + port := s.config.SerfLANConfig.MemberlistConfig.BindPort + return fmt.Sprintf("127.0.0.1:%d", port) +} + +// joinAddrWAN returns the address other servers can +// use to join the cluster on the WAN interface. +func joinAddrWAN(s *Server) string { + if s == nil { + panic("no server") + } + port := s.config.SerfWANConfig.MemberlistConfig.BindPort + return fmt.Sprintf("127.0.0.1:%d", port) +} + +type clientOrServer interface { + JoinLAN(addrs []string) (int, error) +} + +// joinLAN is a convenience function for +// +// member.JoinLAN("127.0.0.1:"+leader.config.SerfLANConfig.MemberlistConfig.BindPort) +func joinLAN(t *testing.T, member clientOrServer, leader *Server) { + if member == nil || leader == nil { + panic("no server") + } + addr := []string{joinAddrLAN(leader)} + if _, err := member.JoinLAN(addr); err != nil { + t.Fatal(err) + } +} + +// joinWAN is a convenience function for +// +// member.JoinWAN("127.0.0.1:"+leader.config.SerfWANConfig.MemberlistConfig.BindPort) +func joinWAN(t *testing.T, member, leader *Server) { + if member == nil || leader == nil { + panic("no server") + } + addr := []string{joinAddrWAN(leader)} + if _, err := member.JoinWAN(addr); err != nil { + t.Fatal(err) + } +} diff --git a/consul/internal_endpoint_test.go b/consul/internal_endpoint_test.go index ddf72f68ea..8b1fc51ace 100644 --- a/consul/internal_endpoint_test.go +++ b/consul/internal_endpoint_test.go @@ -2,7 +2,6 @@ package consul import ( "encoding/base64" - "fmt" "os" "testing" @@ -212,11 +211,7 @@ func TestInternal_KeyringOperation(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) var out2 structs.KeyringResponses req2 := structs.KeyringRequest{ diff --git a/consul/leader_test.go b/consul/leader_test.go index e22f95a17a..29a72c46b0 100644 --- a/consul/leader_test.go +++ b/consul/leader_test.go @@ -1,7 +1,6 @@ package consul import ( - "fmt" "os" "testing" "time" @@ -29,11 +28,7 @@ func TestLeader_RegisterMember(t *testing.T) { defer c1.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") @@ -103,11 +98,7 @@ func TestLeader_FailedMember(t *testing.T) { testrpc.WaitForLeader(t, s1.RPC, "dc1") // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) // Fail the member c1.Shutdown() @@ -165,11 +156,7 @@ func TestLeader_LeftMember(t *testing.T) { defer c1.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) state := s1.fsm.State() @@ -214,11 +201,7 @@ func TestLeader_ReapMember(t *testing.T) { defer c1.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) state := s1.fsm.State() @@ -326,11 +309,7 @@ func TestLeader_Reconcile(t *testing.T) { defer c1.Shutdown() // Join before we have a leader, this should cause a reconcile! - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) // Should not be registered state := s1.fsm.State() @@ -365,11 +344,7 @@ func TestLeader_Reconcile_Races(t *testing.T) { defer os.RemoveAll(dir2) defer c1.Shutdown() - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := c1.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, c1, s1) // Wait for the server to reconcile the client and register it. state := s1.fsm.State() @@ -462,14 +437,8 @@ func TestLeader_LeftServer(t *testing.T) { servers := []*Server{s1, s2, s3} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) for _, s := range servers { retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) }) @@ -505,14 +474,8 @@ func TestLeader_LeftLeader(t *testing.T) { servers := []*Server{s1, s2, s3} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) for _, s := range servers { retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) }) @@ -567,11 +530,7 @@ func TestLeader_MultiBootstrap(t *testing.T) { servers := []*Server{s1, s2} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) for _, s := range servers { retry.Run(t, func(r *retry.R) { @@ -605,14 +564,8 @@ func TestLeader_TombstoneGC_Reset(t *testing.T) { servers := []*Server{s1, s2, s3} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) for _, s := range servers { retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) }) @@ -749,14 +702,8 @@ func TestLeader_RollRaftServer(t *testing.T) { servers := []*Server{s1, s2, s3} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) for _, s := range servers { retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) }) @@ -785,9 +732,7 @@ func TestLeader_RollRaftServer(t *testing.T) { }) defer os.RemoveAll(dir4) defer s4.Shutdown() - if _, err := s4.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s4, s1) servers[1] = s4 // Make sure the dead server is removed and we're back to 3 total peers @@ -838,14 +783,8 @@ func TestLeader_ChangeServerID(t *testing.T) { servers := []*Server{s1, s2, s3} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) for _, s := range servers { retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s, 3)) }) @@ -878,9 +817,7 @@ func TestLeader_ChangeServerID(t *testing.T) { }) defer os.RemoveAll(dir4) defer s4.Shutdown() - if _, err := s4.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s4, s1) servers[2] = s4 // Make sure the dead server is removed and we're back to 3 total peers diff --git a/consul/operator_autopilot_endpoint_test.go b/consul/operator_autopilot_endpoint_test.go index 912b0a987a..80e1eec9be 100644 --- a/consul/operator_autopilot_endpoint_test.go +++ b/consul/operator_autopilot_endpoint_test.go @@ -1,7 +1,6 @@ package consul import ( - "fmt" "os" "strings" "testing" @@ -215,18 +214,12 @@ func TestOperator_ServerHealth(t *testing.T) { dir2, s2 := testServerWithConfig(t, conf) defer os.RemoveAll(dir2) defer s2.Shutdown() - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) dir3, s3 := testServerWithConfig(t, conf) defer os.RemoveAll(dir3) defer s3.Shutdown() - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s3, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") retry.Run(t, func(r *retry.R) { diff --git a/consul/prepared_query_endpoint_test.go b/consul/prepared_query_endpoint_test.go index e138877da8..7ca4f51a77 100644 --- a/consul/prepared_query_endpoint_test.go +++ b/consul/prepared_query_endpoint_test.go @@ -477,11 +477,7 @@ func TestPreparedQuery_Apply_ForwardLeader(t *testing.T) { defer codec2.Close() // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s2.RPC, "dc1") @@ -1450,11 +1446,7 @@ func TestPreparedQuery_Execute(t *testing.T) { testrpc.WaitForLeader(t, s2.RPC, "dc2") // Try to WAN join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) retry.Run(t, func(r *retry.R) { if got, want := len(s1.WANMembers()), 2; got != want { r.Fatalf("got %d WAN members want %d", got, want) @@ -2464,11 +2456,7 @@ func TestPreparedQuery_Execute_ForwardLeader(t *testing.T) { defer codec2.Close() // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s2.RPC, "dc1") @@ -2699,11 +2687,7 @@ func TestPreparedQuery_Wrapper(t *testing.T) { testrpc.WaitForLeader(t, s2.RPC, "dc2") // Try to WAN join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) retry.Run(t, func(r *retry.R) { if got, want := len(s1.WANMembers()), 2; got != want { r.Fatalf("got %d WAN members want %d", got, want) diff --git a/consul/server_test.go b/consul/server_test.go index 88b0f7da58..ade7be77b5 100644 --- a/consul/server_test.go +++ b/consul/server_test.go @@ -152,11 +152,7 @@ func TestServer_JoinLAN(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { if got, want := len(s1.LANMembers()), 2; got != want { r.Fatalf("got %d s1 LAN members want %d", got, want) @@ -177,11 +173,7 @@ func TestServer_JoinWAN(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) retry.Run(t, func(r *retry.R) { if got, want := len(s1.WANMembers()), 2; got != want { r.Fatalf("got %d s1 WAN members want %d", got, want) @@ -212,11 +204,7 @@ func TestServer_JoinWAN_Flood(t *testing.T) { defer os.RemoveAll(dir2) defer s2.Shutdown() - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) for _, s := range []*Server{s1, s2} { retry.Run(t, func(r *retry.R) { @@ -232,11 +220,7 @@ func TestServer_JoinWAN_Flood(t *testing.T) { // Do just a LAN join for the new server and make sure it // shows up in the WAN. - addr = fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s3, s1) for _, s := range []*Server{s1, s2, s3} { retry.Run(t, func(r *retry.R) { @@ -269,18 +253,10 @@ func TestServer_JoinSeparateLanAndWanAddresses(t *testing.T) { defer s3.Shutdown() // Join s2 to s1 on wan - addrs1 := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addrs1}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) // Join s3 to s2 on lan - addrs2 := fmt.Sprintf("127.0.0.1:%d", - s2.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s3.JoinLAN([]string{addrs2}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s3, s2) retry.Run(t, func(r *retry.R) { if got, want := len(s1.WANMembers()), 2; got != want { r.Fatalf("got %d s1 WAN members want %d", got, want) @@ -343,11 +319,7 @@ func TestServer_LeaveLeader(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) @@ -382,11 +354,7 @@ func TestServer_Leave(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 2)) @@ -446,11 +414,7 @@ func TestServer_JoinLAN_TLS(t *testing.T) { defer s2.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) retry.Run(t, func(r *retry.R) { if got, want := len(s1.LANMembers()), 2; got != want { r.Fatalf("got %d s1 LAN members want %d", got, want) @@ -487,11 +451,7 @@ func TestServer_Expect(t *testing.T) { defer s4.Shutdown() // Join the first two servers. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) // Should have no peers yet since the bootstrap didn't occur. retry.Run(t, func(r *retry.R) { @@ -500,9 +460,7 @@ func TestServer_Expect(t *testing.T) { }) // Join the third node. - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s3, s1) // Now we have three servers so we should bootstrap. retry.Run(t, func(r *retry.R) { @@ -515,9 +473,7 @@ func TestServer_Expect(t *testing.T) { // the fourth server. testrpc.WaitForLeader(t, s1.RPC, "dc1") termBefore := s1.raft.Stats()["last_log_term"] - if _, err := s4.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s4, s1) // Wait for the new server to see itself added to the cluster. retry.Run(t, func(r *retry.R) { @@ -553,11 +509,7 @@ func TestServer_BadExpect(t *testing.T) { defer s3.Shutdown() // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) // should have no peers yet retry.Run(t, func(r *retry.R) { @@ -566,9 +518,7 @@ func TestServer_BadExpect(t *testing.T) { }) // join the third node - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s3, s1) // should still have no peers (because s2 is in expect=2 mode) retry.Run(t, func(r *retry.R) { diff --git a/consul/session_ttl_test.go b/consul/session_ttl_test.go index c674d15149..f7f57098d2 100644 --- a/consul/session_ttl_test.go +++ b/consul/session_ttl_test.go @@ -1,7 +1,6 @@ package consul import ( - "fmt" "os" "strings" "testing" @@ -290,14 +289,8 @@ func TestServer_SessionTTL_Failover(t *testing.T) { servers := []*Server{s1, s2, s3} // Try to join - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) retry.Run(t, func(r *retry.R) { r.Check(wantPeers(s1, 3)) }) // Find the leader diff --git a/consul/snapshot_endpoint_test.go b/consul/snapshot_endpoint_test.go index 64c65ff807..ff8885a30e 100644 --- a/consul/snapshot_endpoint_test.go +++ b/consul/snapshot_endpoint_test.go @@ -2,7 +2,6 @@ package consul import ( "bytes" - "fmt" "os" "strings" "testing" @@ -297,11 +296,7 @@ func TestSnapshot_Forward_Leader(t *testing.T) { defer s2.Shutdown() // Try to join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") testrpc.WaitForLeader(t, s2.RPC, "dc1") @@ -326,11 +321,7 @@ func TestSnapshot_Forward_Datacenter(t *testing.T) { testrpc.WaitForLeader(t, s2.RPC, "dc2") // Try to WAN join. - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfWANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinWAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinWAN(t, s2, s1) retry.Run(t, func(r *retry.R) { if got, want := len(s1.WANMembers()), 2; got < want { r.Fatalf("got %d WAN members want at least %d", got, want) diff --git a/consul/stats_fetcher_test.go b/consul/stats_fetcher_test.go index 493316ee9c..d8615545fb 100644 --- a/consul/stats_fetcher_test.go +++ b/consul/stats_fetcher_test.go @@ -2,7 +2,6 @@ package consul import ( "context" - "fmt" "os" "testing" "time" @@ -25,14 +24,8 @@ func TestStatsFetcher(t *testing.T) { defer os.RemoveAll(dir3) defer s3.Shutdown() - addr := fmt.Sprintf("127.0.0.1:%d", - s1.config.SerfLANConfig.MemberlistConfig.BindPort) - if _, err := s2.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } - if _, err := s3.JoinLAN([]string{addr}); err != nil { - t.Fatalf("err: %v", err) - } + joinLAN(t, s2, s1) + joinLAN(t, s3, s1) testrpc.WaitForLeader(t, s1.RPC, "dc1") members := s1.serfLAN.Members()